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

test("renders the import workflow and its safety limits", async () => {
  const response = await renderWorker("/import");
  assert.equal(response.status, 200);
  const html = await response.text();
  assert.match(html, /上传电子书/);
  assert.match(html, /选择 EPUB 或 PDF/);
  assert.match(html, /最大 200 MB/);
  assert.match(html, /只支持无 DRM 文件/);
  assert.match(html, /Windows Edge/);
  assert.match(html, /上传|解析|文字识别|整理章节|可以收听/);
  assert.match(html, /type="file"/);
});

test("the import client saves parsed metadata before chapter synchronization", async () => {
  const source = await import("node:fs/promises").then(({ readFile }) =>
    readFile(new URL("../../components/import/ImportWizard.tsx", import.meta.url), "utf8"),
  );
  const metadataUpdate = source.indexOf("title: parsed.title");
  const chapterPreparation = source.indexOf("const prepared = await prepareChaptersForSync");
  assert.ok(metadataUpdate > 0, "parsed title and author should be saved");
  assert.ok(metadataUpdate < chapterPreparation, "metadata should be saved before chapters");
});

test("the import client restores and clears IndexedDB upload checkpoints", async () => {
  const source = await import("node:fs/promises").then(({ readFile }) =>
    readFile(new URL("../../components/import/ImportWizard.tsx", import.meta.url), "utf8"),
  );
  assert.match(source, /createBrowserUploadCheckpointStore/);
  assert.match(source, /resumeUpload/);
  assert.match(source, /checkpointStore\.clear/);
});
