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

test("renders the listening route with a responsive player loading shell", async () => {
  const response = await renderWorker("/books/book-1/listen");
  assert.equal(response.status, 200);
  const html = await response.text();
  assert.match(html, /正在准备播放器/);
  assert.match(html, /返回书架/);
  assert.match(html, /校对正文/);
  assert.match(html, /Player-[A-Za-z0-9_-]+\.js/);
});

test("player source includes required controls, sentence highlighting and next chapter generation", async () => {
  const source = await import("node:fs/promises").then(({ readFile }) =>
    readFile(new URL("../../components/player/Player.tsx", import.meta.url), "utf8"),
  );
  for (const label of ["后退 15 秒", "前进 15 秒", "播放速度", "音量", "睡眠定时", "本章结束", "逐句正文"]) {
    assert.match(source, new RegExp(label));
  }
  assert.match(source, /aria-current=\{segment\.id === current\?\.id \? "true" : undefined\}/);
  assert.match(source, /prefetchNextChapter/);
  assert.match(source, /mediaSession/);
  assert.match(source, /speechSynthesis|createSpeechFallback/);
  assert.match(source, /ProgressOutbox/);
  assert.match(source, /window\.addEventListener\("online"/);
});
