"use strict";
const test = require("node:test");
const assert = require("node:assert/strict");
const { extractRawPath, derivePipeline } = require("../main.js").__test__;

test("extractRawPath pulls the backticked path", () => {
  const page = [
    "# You're reading way too much code",
    "",
    "#source",
    "",
    "## Source Metadata",
    "",
    "- **Date:** YouTube video, 24:11",
    "- **Raw path:** `raw/sources/You're reading way too much code.md`",
    "- **Source type:** video essay",
  ].join("\n");
  assert.equal(extractRawPath(page), "raw/sources/You're reading way too much code.md");
});

test("extractRawPath handles cyrillic and em dashes", () => {
  const page = "- **Raw path:** `raw/sources/Скиллы на базе git — новая память AI-агентов.md`";
  assert.equal(extractRawPath(page), "raw/sources/Скиллы на базе git — новая память AI-агентов.md");
});

test("extractRawPath returns null when the line is absent", () => {
  assert.equal(extractRawPath("# A page\n\n#source\n\nNo metadata here."), null);
});

test("derivePipeline splits claimed from unclaimed raw files", () => {
  const rawFiles = [
    { path: "raw/sources/Nina interview.md", name: "Nina interview.md", size: 6614 },
    { path: "raw/sources/Webinar script.md", name: "Webinar script.md", size: 15841 },
  ];
  const sourcePages = [
    {
      path: "wiki/sources/2026-07-14-nina-interview.md",
      name: "2026-07-14-nina-interview.md",
      rawPath: "raw/sources/Nina interview.md",
    },
  ];
  const out = derivePipeline({ rawFiles, sourcePages });
  assert.equal(out.processed.length, 1);
  assert.equal(out.processed[0].name, "Nina interview.md");
  assert.equal(out.processed[0].page.name, "2026-07-14-nina-interview.md");
  assert.equal(out.unprocessed.length, 1);
  assert.equal(out.unprocessed[0].name, "Webinar script.md");
  assert.equal(out.orphaned.length, 0);
});

test("derivePipeline reports source pages whose raw file is gone", () => {
  const out = derivePipeline({
    rawFiles: [],
    sourcePages: [
      { path: "wiki/sources/x.md", name: "x.md", rawPath: "raw/sources/deleted.md" },
    ],
  });
  assert.equal(out.orphaned.length, 1);
  assert.equal(out.orphaned[0].name, "x.md");
});

test("derivePipeline treats a page with no raw path as orphaned", () => {
  const out = derivePipeline({
    rawFiles: [{ path: "raw/sources/a.md", name: "a.md", size: 10 }],
    sourcePages: [{ path: "wiki/sources/y.md", name: "y.md", rawPath: null }],
  });
  assert.equal(out.orphaned.length, 1);
  assert.equal(out.unprocessed.length, 1);
});

test("derivePipeline routes a duplicate raw-path claim to orphaned", () => {
  const out = derivePipeline({
    rawFiles: [{ path: "raw/sources/a.md", name: "a.md", size: 10 }],
    sourcePages: [
      { path: "wiki/sources/first.md", name: "first.md", rawPath: "raw/sources/a.md" },
      { path: "wiki/sources/second.md", name: "second.md", rawPath: "raw/sources/a.md" },
    ],
  });
  assert.equal(out.processed.length, 1);
  assert.equal(out.processed[0].page.name, "first.md");
  assert.equal(out.unprocessed.length, 0);
  assert.deepEqual(out.orphaned.map((p) => p.name), ["second.md"]);
});

test("derivePipeline lists a page once when it is both a duplicate claim and missing its raw file", () => {
  const out = derivePipeline({
    rawFiles: [{ path: "raw/sources/other.md", name: "other.md", size: 10 }],
    sourcePages: [
      { path: "wiki/sources/first.md", name: "first.md", rawPath: "raw/sources/gone.md" },
      { path: "wiki/sources/second.md", name: "second.md", rawPath: "raw/sources/gone.md" },
    ],
  });
  assert.deepEqual(out.orphaned.map((p) => p.name), ["first.md", "second.md"]);
});

test("derivePipeline sorts unprocessed by name and processed newest first", () => {
  const out = derivePipeline({
    rawFiles: [
      { path: "raw/sources/b.md", name: "b.md", size: 1 },
      { path: "raw/sources/a.md", name: "a.md", size: 1 },
      { path: "raw/sources/c.md", name: "c.md", size: 1 },
    ],
    sourcePages: [
      { path: "wiki/sources/2026-07-14-x.md", name: "2026-07-14-x.md", rawPath: "raw/sources/c.md" },
    ],
  });
  assert.deepEqual(out.unprocessed.map((f) => f.name), ["a.md", "b.md"]);
  assert.equal(out.processed.length, 1);
});
