"use strict";
const test = require("node:test");
const assert = require("node:assert/strict");
const fs = require("node:fs");
const path = require("node:path");
const C = require("../static/credits.js");

const CLIPS = [
  { id: "reef_x", title: "Reef X", license: "CC-BY-SA 4.0 — credit Joe Mabel", source: "Wikimedia <reef>" },
  { id: "cosmos", title: "Cosmic Cliffs", license: "CC-BY-class — credit NASA, ESA, CSA, STScI", source: "NASA SVS 31348" },
  { id: "pd_only", title: "PD clip", license: "public-domain (US Gov, 17 U.S.C. §105)", source: "NASA" },
];

test("creditEntries returns one entry per clip, preserving license/source", () => {
  const out = C.creditEntries(CLIPS);
  assert.equal(out.length, CLIPS.length, "no clip is silently dropped from the credits");
  const reef = out.find((e) => e.id === "reef_x");
  assert.equal(reef.license, "CC-BY-SA 4.0 — credit Joe Mabel");
  assert.equal(reef.source, "Wikimedia <reef>");
  assert.equal(reef.title, "Reef X");
});

test("creditEntries is sorted by id for deterministic output", () => {
  const ids = C.creditEntries(CLIPS).map((e) => e.id);
  assert.deepEqual(ids, ["cosmos", "pd_only", "reef_x"]);
});

test("creditEntries falls back title->id and tolerates missing fields", () => {
  const out = C.creditEntries([{ id: "bare" }]);
  assert.deepEqual(out, [{ id: "bare", title: "bare", license: "", source: "" }]);
});

test("creditEntries handles null/empty input", () => {
  assert.deepEqual(C.creditEntries(null), []);
  assert.deepEqual(C.creditEntries([]), []);
});

test("creditsListHtml renders every clip's attribution and escapes HTML", () => {
  const html = C.creditsListHtml(CLIPS);
  assert.match(html, /Cosmic Cliffs/);
  assert.match(html, /Joe Mabel/);
  // free-text source "Wikimedia <reef>" must be escaped, not injected as a tag
  assert.match(html, /Wikimedia &lt;reef&gt;/);
  assert.doesNotMatch(html, /<reef>/);
  // one block per clip
  const blocks = html.match(/class="credit"/g) || [];
  assert.equal(blocks.length, CLIPS.length);
});

test("credits.html wires config.js + credits.js, the attributions container, and a back link", () => {
  const html = fs.readFileSync(path.join(__dirname, "../static/credits.html"), "utf8");
  assert.match(html, /<script src="config\.js">/);   // static/live media-base + flag
  assert.match(html, /<script src="credits\.js">/);
  assert.match(html, /id="video-credits"/);           // JS fills this
  assert.match(html, /href="index\.html"/);           // back into the experience
});

test("credits.html carries the CC BY-SA derivative declaration (Option D1)", () => {
  const html = fs.readFileSync(path.join(__dirname, "../static/credits.html"), "utf8");
  assert.match(html, /CC BY-SA 4\.0/);                 // the altered work's own license
  assert.match(html, /altered/i);                      // states the footage is modified
});
