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

const here = dirname(fileURLToPath(import.meta.url));
const root = join(here, "..");
const [html, css, app, protocol] = await Promise.all([
  readFile(join(here, "index.html"), "utf8"),
  readFile(join(here, "styles.css"), "utf8"),
  readFile(join(here, "app.js"), "utf8"),
  readFile(join(root, "review-protocol.json"), "utf8").then(JSON.parse),
]);

const expected = [
  [html, "mapbox-gl-js/v3.26.0/mapbox-gl.js"],
  [html, "mapbox-gl-js/v3.26.0/mapbox-gl.css"],
  [html, "Measure 6 seconds"],
  [app, 'style: "mapbox://styles/mapbox/standard"'],
  [app, 'show3dObjects: true'],
  [app, 'show3dBuildings: true'],
  [app, 'show3dLandmarks: true'],
  [app, 'map.setRain('],
  [app, 'map.setSnow('],
  [app, 'map.addInteraction('],
  [app, 'featuresetId: "buildings"'],
  [app, 'slot: "middle"'],
  [app, '"line-emissive-strength"'],
  [app, 'anchor.download = "mapbox-3d-city-evidence.json"'],
  [app, "window.__mapboxReviewEvidence = payload"],
  [app, '"[REDACTED_MAPBOX_TOKEN]"'],
  [css, ".glass-panel"],
];

for (const [source, needle] of expected) {
  assert.ok(source.includes(needle), `Missing required demo behavior: ${needle}`);
}

for (const forbidden of ["localStorage", "sessionStorage", "URLSearchParams", "sk.ey"]) {
  assert.ok(!app.includes(forbidden), `Token-safety check failed: found ${forbidden}`);
}

assert.equal(protocol.library.version, "3.26.0");
assert.equal(protocol.scenes.length, 5);
assert.ok(protocol.evidenceBoundary.notClaimed.length >= 4);

console.log("Mapbox review demo static verification passed.");
console.log(`Checked ${expected.length} required behaviors, token handling, five scenes, and the evidence boundary.`);
