import { describe, expect, test, vi } from "vitest"; import { extractPeaks } from "../../../utils/player/waveform.ts"; import { calls, fail, route } from "../../fetch.ts"; describe("extractPeaks", () => { test("decodes once per URL and takes each block's peak", async () => { const data = new Float32Array([0.1, -0.5, 0.2, 0.9, -0.3, 0.4]); const decode = vi.fn(() => Promise.resolve({ getChannelData: () => data })); const close = vi.fn(); vi.stubGlobal( "AudioContext", class { decodeAudioData = decode; close = close; }, ); route("GET /api/files/:id", () => new Response(new Uint8Array([1, 2]))); const peaks = await extractPeaks("http://localhost:8000/api/files/w1", 3); expect([...peaks].map((p) => +p.toFixed(2))).toEqual([0.5, 0.9, 0.4]); expect(close).toHaveBeenCalled(); await extractPeaks("http://localhost:8000/api/files/w1", 3); expect(decode).toHaveBeenCalledTimes(1); expect(calls("GET /api/files/:id")).toHaveLength(1); }); test("a failed fetch throws", async () => { route("GET /api/files/:id", () => fail("gone", 404)); await expect(extractPeaks("http://localhost:8000/api/files/w2", 3)).rejects .toThrow(/404/); }); });