import { describe, expect, test } from "bun:test";
import { join } from "node:path";
import { createAssetAudioRenderer, resolveCaptchaAudioAssetsDir } from "../src/audio/renderer.ts";

const packageRoot = join(import.meta.dir, "..");
const expectedAssets = join(packageRoot, "assets", "audio");

describe("CAPTCHA audio renderer", () => {
  test("resolves the packaged audio directory", () => {
    expect(resolveCaptchaAudioAssetsDir(expectedAssets)).toBe(expectedAssets);
  });

  test("renders a playable PCM WAV challenge", async () => {
    const renderer = createAssetAudioRenderer({ assetsDir: expectedAssets, gapMs: 100 });
    const bytes = await renderer.render(["one", "two", "three"], "en-IN");

    expect(bytes.byteLength).toBeGreaterThan(44);
    expect(new TextDecoder().decode(bytes.slice(0, 4))).toBe("RIFF");
    expect(new TextDecoder().decode(bytes.slice(8, 12))).toBe("WAVE");
  });
});
