import assert from "node:assert/strict";
import test from "node:test";
import { buildPlaybackQueue, moveQueue, seekAcrossQueue } from "../../lib/client/player/queue.ts";

const chapters = [
  { id: "c1", position: 0, title: "一", segments: [
    { id: "s1", position: 0, included: true },
    { id: "skip", position: 1, included: false },
    { id: "s2", position: 2, included: true },
  ] },
  { id: "c2", position: 1, title: "二", segments: [{ id: "s3", position: 0, included: true }] },
];

test("builds one continuous queue from only included sentences", () => {
  assert.deepEqual(buildPlaybackQueue(chapters).map(({ chapterId, segmentId }) => ({ chapterId, segmentId })), [
    { chapterId: "c1", segmentId: "s1" },
    { chapterId: "c1", segmentId: "s2" },
    { chapterId: "c2", segmentId: "s3" },
  ]);
});

test("moves between sentences and clamps at book boundaries", () => {
  assert.equal(moveQueue(0, 1, 3), 1);
  assert.equal(moveQueue(0, -1, 3), 0);
  assert.equal(moveQueue(2, 1, 3), 2);
});

test("applies fifteen-second seeking across neighboring sentence durations", () => {
  const durations = [10_000, 12_000, 20_000];
  assert.deepEqual(seekAcrossQueue({ index: 1, offsetMs: 2_000, deltaMs: -15_000, durations }), { index: 0, offsetMs: 0 });
  assert.deepEqual(seekAcrossQueue({ index: 0, offsetMs: 8_000, deltaMs: 15_000, durations }), { index: 2, offsetMs: 1_000 });
  assert.deepEqual(seekAcrossQueue({ index: 2, offsetMs: 18_000, deltaMs: 15_000, durations }), { index: 2, offsetMs: 20_000 });
});
