// SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-License-Identifier: AGPL-3.0-or-later import { assert, assertAlmostEquals, assertEquals } from "@std/assert"; import { walk } from "@std/fs"; import { install_web_audio, sources_ended } from "@/client/native/web_audio.ts"; import { WebAudioOutput } from "@/client/audio_output.ts"; import { SOUND_CATEGORIES } from "@/common/mod_data.ts"; import type { Volumes } from "@/client/sounds.ts"; import type { AudioCommand } from "@/client/native/messages.ts"; // any of the mods' sound files async function some_ogg(): Promise { for await (const entry of walk("mods", { exts: [".ogg"], includeDirs: false })) { return await Deno.readFile(entry.path); } throw new Error("No .ogg in mods/"); } // what the window was sent, once one of them is what's wanted async function sent_until(sent: AudioCommand[], wanted: (command: AudioCommand) => boolean) { for (let i = 0; i < 200 && !sent.some(wanted); i++) await new Promise((resolve) => setTimeout(resolve, 5)); return sent.find(wanted); } Deno.test("the desktop app's web audio plays a voice as one source: its gains multiplied, its pitch, where it is and its range", async () => { const sent: AudioCommand[] = []; install_web_audio((commands) => sent.push(...commands)); const output = new WebAudioOutput(false); const category = SOUND_CATEGORIES[0]; const volumes = Object.fromEntries(SOUND_CATEGORIES.map((c) => [c, 1])) as Volumes; output.set_volumes({ ...volumes, master: 0.5, [category]: 0.5 }); const voice = output.start({ id: "test:sound", bytes: await some_ogg() }, { category, at: { x: 1, y: 2, z: 3 }, range: 16, volume: 0.8, pitch: 1.5, loop: false, offset: 0, })!; const play = await sent_until(sent, (command) => command.op === "play"); assert(play?.op === "play"); const buffer = sent.find((command) => command.op === "buffer"); assert(buffer?.op === "buffer" && buffer.id === play.buffer, "the file goes before what plays it"); assert(buffer.channels.length > 0 && buffer.channels[0].length > 0); const { gain, ...rest } = play.settings; assertAlmostEquals(gain, 0.8 * 0.5 * 0.5); assertEquals(rest, { pitch: 1.5, at: [1, 2, 3], ref_distance: 1, max_distance: 16, rolloff: 1 }); sent.length = 0; voice.move({ x: 4, y: 5, z: 6 }); const moved = await sent_until(sent, (command) => command.op === "set"); assert(moved?.op === "set" && moved.id === play.id); assertEquals(moved.settings.at, [4, 5, 6]); // the category's volume changes every sound in it sent.length = 0; output.set_volumes({ ...volumes, master: 0.5, [category]: 1 }); const louder = await sent_until(sent, (command) => command.op === "set"); assert(louder?.op === "set"); assertAlmostEquals(louder.settings.gain, 0.8 * 0.5); sources_ended([play.id]); await new Promise((resolve) => setTimeout(resolve, 0)); assert(voice.ended); }); Deno.test("the desktop app's web audio plays a sound from nowhere as it is, and stopping it stops the window's", async () => { const sent: AudioCommand[] = []; install_web_audio((commands) => sent.push(...commands)); const output = new WebAudioOutput(false); output.set_volumes(Object.fromEntries(["master", ...SOUND_CATEGORIES].map((c) => [c, 1])) as Volumes); const voice = output.start({ id: "test:sound", bytes: await some_ogg() }, { category: undefined, at: undefined, range: 16, volume: 1, pitch: 1, loop: true, offset: 0, })!; const play = await sent_until(sent, (command) => command.op === "play"); assert(play?.op === "play"); assertEquals(play.settings.at, undefined); assertEquals(play.loop, true); voice.stop(); const stop = await sent_until(sent, (command) => command.op === "stop"); assert(stop?.op === "stop" && stop.id === play.id); });