import assert from "node:assert/strict";
import { existsSync, readFileSync } from "node:fs";
import test from "node:test";

const html = readFileSync(new URL("./index.html", import.meta.url), "utf8");
const js = readFileSync(new URL("./app.js", import.meta.url), "utf8");
const css = readFileSync(new URL("./styles.css", import.meta.url), "utf8");
const setupPs1 = readFileSync(new URL("../scripts/setup.ps1", import.meta.url), "utf8");
const packagePs1 = readFileSync(new URL("../scripts/package_release.ps1", import.meta.url), "utf8");
const swPath = new URL("./sw.js", import.meta.url);
const manifestPath = new URL("./manifest.webmanifest", import.meta.url);
const windowsClickInstallerPath = new URL("../Install ORC Local Windows.cmd", import.meta.url);
const macClickInstallerPath = new URL("../Install ORC Local macOS.command", import.meta.url);

test("result panel exposes a disabled markdown download button", () => {
  assert.match(html, /id="downloadMarkdown"/);
  assert.match(html, /Markdown/);
  assert.match(html, /id="downloadMarkdown"[^>]*disabled/);
});

test("top bar exposes installer instead of raw OCR package download", () => {
  assert.match(html, /id="installLocal"/);
  assert.match(html, /Cài đặt/);
  assert.match(html, /href="\.\/downloads\/orc-local-installer\.zip"/);
  assert.doesNotMatch(html, /Tải OCR Local/);
});

test("frontend turns successful markdown payload into a browser download", () => {
  assert.match(js, /downloadMarkdown/);
  assert.match(js, /URL\.createObjectURL/);
  assert.match(js, /markdown_filename/);
});

test("mobile copy can wrap and disabled secondary buttons look disabled", () => {
  assert.match(css, /\.statusline\s*{(?s:.*?)overflow-wrap:\s*anywhere/);
  assert.match(css, /\.topbar\s*>\s*div\s*{(?s:.*?)min-width:\s*0/);
  assert.match(css, /button\.secondary:disabled/);
});

test("frontend exposes stop OCR control while conversion is running", () => {
  assert.match(html, /id="cancelConvert"/);
  assert.match(html, /Dừng OCR/);
  assert.match(js, /cancelConvert/);
  assert.match(js, /\/cancel/);
});

test("hosted app is installable and cacheable for offline use", () => {
  assert.match(html, /rel="manifest"/);
  assert.match(js, /serviceWorker\.register\("\.\/sw\.js"\)/);
  assert.equal(existsSync(swPath), true);
  assert.equal(existsSync(manifestPath), true);

  const sw = readFileSync(swPath, "utf8");
  const manifest = readFileSync(manifestPath, "utf8");
  assert.match(sw, /CACHE_NAME/);
  assert.match(sw, /fetch/);
  assert.match(manifest, /"name":\s*"OCR PDF Tiếng Việt"/);
});

test("windows setup script can run away from the Codex machine", () => {
  assert.doesNotMatch(setupPs1, /codex-runtimes/);
  assert.doesNotMatch(setupPs1, /C:\\Users\\hiiam/);
  assert.match(setupPs1, /Python\.Python\.3\.12/);
  assert.match(setupPs1, /Get-Python312/);
});

test("install button confirms the local worker with a green check", () => {
  assert.match(html, /id="installLocal"/);
  assert.match(js, /markInstallReady/);
  assert.match(js, /is-installed/);
  assert.match(js, /Đã cài đặt/);
  assert.match(css, /\.button-link\.is-installed/);
  assert.match(css, /content:\s*"✓"/);
});

test("release package includes double-click installers at the zip root", () => {
  assert.equal(existsSync(windowsClickInstallerPath), true);
  assert.equal(existsSync(macClickInstallerPath), true);
  assert.match(packagePs1, /Install ORC Local Windows\.cmd/);
  assert.match(packagePs1, /Install ORC Local macOS\.command/);
});
