Open History to see the last commit
1import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
2
3const SOUND_SECONDS = 0.6;
4
5const audio = { currentTime: 0, started: 0 };
6
7class FakeAudioContext {
8 state = "running";
9 destination = {};
10 get currentTime() {
11 return audio.currentTime;
12 }
13 resume() {
14 return Promise.resolve();
15 }
16 decodeAudioData() {
17 return Promise.resolve({ duration: SOUND_SECONDS });
18 }
19 createBufferSource() {
20 return {
21 buffer: null,
22 connect() {},
23 start() {
24 audio.started += 1;
25 },
26 };
27 }
28}
29
30async function loadUnlocked() {
31 const notifications = await import("./threadNotifications");
32 notifications.unlockNotificationAudio();
33 return notifications;
34}
35
36beforeEach(() => {
37 vi.resetModules();
38 audio.currentTime = 0;
39 audio.started = 0;
40 vi.stubGlobal("AudioContext", FakeAudioContext);
41 vi.stubGlobal(
42 "fetch",
43 vi.fn(async () => ({ arrayBuffer: async () => new ArrayBuffer(0) })),
44 );
45});
46
47afterEach(() => {
48 vi.unstubAllGlobals();
49});
50
51describe("playNotificationSound", () => {
52 it("plays one sound for threads that complete together", async () => {
53 const { playNotificationSound } = await loadUnlocked();
54
55 await Promise.all(
56 Array.from({ length: 4 }, () => playNotificationSound("completion", () => true)),
57 );
58
59 expect(audio.started).toBe(1);
60 });
61
62 it("still plays an input alert over a completion sound", async () => {
63 const { playNotificationSound } = await loadUnlocked();
64 await playNotificationSound("completion", () => true);
65
66 await playNotificationSound("input", () => true);
67
68 expect(audio.started).toBe(2);
69 });
70
71 it("leaves the slot free when a sound is turned off before it plays", async () => {
72 const { playNotificationSound } = await loadUnlocked();
73 await playNotificationSound("completion", () => false);
74
75 await playNotificationSound("completion", () => true);
76
77 expect(audio.started).toBe(1);
78 });
79});