import assert from "node:assert/strict";
import test from "node:test";
import {readFileSync} from "node:fs";
import {dirname, join} from "node:path";
import {fileURLToPath} from "node:url";

import {workplanMatchesSearch} from "../src/components/workplan-search.js";

const root = dirname(fileURLToPath(import.meta.url));
const indexMd = readFileSync(join(root, "../src/index.md"), "utf-8");

const sample = {
  id: "abc-123",
  title: "Overview Workstream Stage Counts",
  repo_label: "state-hub",
  domain: "infotech",
  workplan_filename: "STATE-WP-0057-overview-workstream-stage-counts.md",
  owner: "codex",
};

test("empty query matches all workplans", () => {
  assert.equal(workplanMatchesSearch(sample, ""), true);
  assert.equal(workplanMatchesSearch(sample, "   "), true);
  assert.equal(workplanMatchesSearch(sample, null), true);
});

test("matches title, repo, domain, filename, owner, and id", () => {
  assert.equal(workplanMatchesSearch(sample, "stage counts"), true);
  assert.equal(workplanMatchesSearch(sample, "STATE-HUB"), true);
  assert.equal(workplanMatchesSearch(sample, "Infotech"), true);
  assert.equal(workplanMatchesSearch(sample, "wp-0057"), true);
  assert.equal(workplanMatchesSearch(sample, "codex"), true);
  assert.equal(workplanMatchesSearch(sample, "abc-123"), true);
  assert.equal(workplanMatchesSearch(sample, "railiance"), false);
});

test("overview chart wires the text filter into Workplans by Repository", () => {
  assert.match(indexMd, /workplanMatchesSearch/);
  assert.match(indexMd, /ws-search-input/);
  assert.match(indexMd, /Filter by title, repo, domain/);
  assert.match(indexMd, /## Workplans by Repository/);
});
