|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test"; |
| 2 | + |
| 3 | +const SOUND_SECONDS = 0.6; |
| 4 | + |
| 5 | +const audio = { currentTime: 0, started: 0 }; |
| 6 | + |
| 7 | +class 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 | + |
| 30 | +async function loadUnlocked() { |
| 31 | + const notifications = await import("./threadNotifications"); |
| 32 | + notifications.unlockNotificationAudio(); |
| 33 | + return notifications; |
| 34 | +} |
| 35 | + |
| 36 | +beforeEach(() => { |
| 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 | + |
| 47 | +afterEach(() => { |
| 48 | + vi.unstubAllGlobals(); |
| 49 | +}); |
| 50 | + |
| 51 | +describe("playNotificationSound", () => { |
| 52 | + it("plays one sound for threads that complete together", async () => { |
| 53 | + const { playNotificationSound } = await loadUnlocked(); |
| 54 | + |
| 55 | + // All four are waiting on the first decode when the sound becomes playable. |
| 56 | + await Promise.all( |
| 57 | + Array.from({ length: 4 }, () => playNotificationSound("completion", () => true)), |
| 58 | + ); |
| 59 | + |
| 60 | + expect(audio.started).toBe(1); |
| 61 | + }); |
| 62 | + |
| 63 | + it("skips a completion that lands while the sound is still playing", async () => { |
| 64 | + const { playNotificationSound } = await loadUnlocked(); |
| 65 | + await playNotificationSound("completion", () => true); |
| 66 | + |
| 67 | + audio.currentTime = SOUND_SECONDS / 2; |
| 68 | + await playNotificationSound("completion", () => true); |
| 69 | + |
| 70 | + expect(audio.started).toBe(1); |
| 71 | + }); |
| 72 | + |
| 73 | + it("plays again once the previous sound has ended", async () => { |
| 74 | + const { playNotificationSound } = await loadUnlocked(); |
| 75 | + await playNotificationSound("completion", () => true); |
| 76 | + |
| 77 | + audio.currentTime = SOUND_SECONDS; |
| 78 | + await playNotificationSound("completion", () => true); |
| 79 | + |
| 80 | + expect(audio.started).toBe(2); |
| 81 | + }); |
| 82 | + |
| 83 | + it("still plays an input alert over a completion sound", async () => { |
| 84 | + const { playNotificationSound } = await loadUnlocked(); |
| 85 | + await playNotificationSound("completion", () => true); |
| 86 | + |
| 87 | + await playNotificationSound("input", () => true); |
| 88 | + |
| 89 | + expect(audio.started).toBe(2); |
| 90 | + }); |
| 91 | + |
| 92 | + it("leaves the slot free when a sound is turned off before it plays", async () => { |
| 93 | + const { playNotificationSound } = await loadUnlocked(); |
| 94 | + await playNotificationSound("completion", () => false); |
| 95 | + |
| 96 | + await playNotificationSound("completion", () => true); |
| 97 | + |
| 98 | + expect(audio.started).toBe(1); |
| 99 | + }); |
| 100 | +}); |
0 commit comments