import assert from "node:assert/strict";
import test from "node:test";
import { renderWorker } from "../helpers/render-worker.mjs";

test("renders the public personal audiobook library shell", async () => {
  const response = await renderWorker("/", {
    headers: {
      "x-forwarded-host": "hearbooks-reader.example.workers.dev",
      "x-forwarded-proto": "https",
    },
  });

  assert.equal(response.status, 200);
  assert.match(response.headers.get("content-type") ?? "", /^text\/html\b/i);

  const html = await response.text();
  assert.match(html, /<html[^>]+lang="zh-CN"/i);
  assert.match(html, /<title>听阅 · HearBooks<\/title>/i);
  assert.match(
    html,
    /<meta[^>]+property="og:image"[^>]+content="https:\/\/hearbooks-reader\.example\.workers\.dev\/og-hearbooks\.png"/i,
  );
  assert.doesNotMatch(html, /chatgpt\.site|localhost:3000\/og-hearbooks\.png/i);
  assert.match(html, /我的书架/);
  assert.match(html, /上传一本书/);
  assert.match(html, /知道网址的人都可以访问和修改此书架/);
  assert.match(html, /href="\/source\.zip"/);
  assert.match(html, /下载当前版本源码/);
  assert.match(html, /第三方与语音说明/);
  assert.match(html, /正在打开书架/);
  assert.match(html, /<main\b/i);
  assert.doesNotMatch(html, /Your site is taking shape|codex-preview|SkeletonPreview/);
});

test("library source offers progress, metadata settings, and explicit deletion", async () => {
  const source = await import("node:fs/promises").then(({ readFile }) =>
    readFile(new URL("../../components/library/LibraryView.tsx", import.meta.url), "utf8"),
  );
  assert.match(source, /storage\.usedBytes/);
  assert.match(source, /继续收听/);
  assert.match(source, /编辑信息/);
  assert.match(source, /删除书籍/);
  assert.match(source, /x-confirm-delete/);
  assert.match(source, /preferredZhVoice/);
  assert.match(source, /preferredEnVoice/);
  assert.match(source, /accept="image\/jpeg,image\/png,image\/webp"/);
  assert.match(source, /book\.progress\.percent/);
});
