|
| 1 | +// @vitest-environment jsdom |
| 2 | + |
| 3 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 4 | + |
| 5 | +type MockAudioContextState = "running" | "suspended" | "closed"; |
| 6 | + |
| 7 | +describe("playNotificationSound", () => { |
| 8 | + const originalAudioContext = window.AudioContext; |
| 9 | + const originalWebkitAudioContext = ( |
| 10 | + window as typeof window & { webkitAudioContext?: typeof AudioContext } |
| 11 | + ).webkitAudioContext; |
| 12 | + const originalFetch = globalThis.fetch; |
| 13 | + |
| 14 | + beforeEach(() => { |
| 15 | + vi.resetModules(); |
| 16 | + }); |
| 17 | + |
| 18 | + afterEach(() => { |
| 19 | + window.AudioContext = originalAudioContext; |
| 20 | + ( |
| 21 | + window as typeof window & { webkitAudioContext?: typeof AudioContext } |
| 22 | + ).webkitAudioContext = originalWebkitAudioContext; |
| 23 | + globalThis.fetch = originalFetch; |
| 24 | + vi.restoreAllMocks(); |
| 25 | + }); |
| 26 | + |
| 27 | + function installAudioMocks(state: MockAudioContextState = "running") { |
| 28 | + const source = { |
| 29 | + buffer: null as AudioBuffer | null, |
| 30 | + connect: vi.fn(), |
| 31 | + start: vi.fn(), |
| 32 | + }; |
| 33 | + const gainNode = { |
| 34 | + gain: { value: 0 }, |
| 35 | + connect: vi.fn(), |
| 36 | + }; |
| 37 | + const decodeAudioData = vi.fn().mockResolvedValue({} as AudioBuffer); |
| 38 | + const resume = vi.fn().mockResolvedValue(undefined); |
| 39 | + |
| 40 | + class MockAudioContext { |
| 41 | + state = state; |
| 42 | + destination = {} as AudioNode; |
| 43 | + decodeAudioData = decodeAudioData; |
| 44 | + createBufferSource = vi.fn(() => source); |
| 45 | + createGain = vi.fn(() => gainNode); |
| 46 | + resume = resume; |
| 47 | + } |
| 48 | + |
| 49 | + window.AudioContext = MockAudioContext as unknown as typeof AudioContext; |
| 50 | + |
| 51 | + return { decodeAudioData, gainNode, source, resume }; |
| 52 | + } |
| 53 | + |
| 54 | + it("plays notification audio via Web Audio API", async () => { |
| 55 | + const { decodeAudioData, gainNode, source } = installAudioMocks("running"); |
| 56 | + const arrayBuffer = new ArrayBuffer(8); |
| 57 | + globalThis.fetch = vi.fn().mockResolvedValue({ |
| 58 | + arrayBuffer: vi.fn().mockResolvedValue(arrayBuffer), |
| 59 | + } as unknown as Response); |
| 60 | + |
| 61 | + const { playNotificationSound } = await import("./notificationSounds"); |
| 62 | + |
| 63 | + playNotificationSound("https://example.com/success.mp3", "success"); |
| 64 | + await vi.waitFor(() => { |
| 65 | + expect(source.start).toHaveBeenCalledTimes(1); |
| 66 | + }); |
| 67 | + |
| 68 | + expect(globalThis.fetch).toHaveBeenCalledWith("https://example.com/success.mp3"); |
| 69 | + expect(decodeAudioData).toHaveBeenCalledWith(arrayBuffer); |
| 70 | + expect(gainNode.gain.value).toBe(0.05); |
| 71 | + }); |
| 72 | + |
| 73 | + it("logs debug information when fetch fails", async () => { |
| 74 | + installAudioMocks("running"); |
| 75 | + const onDebug = vi.fn(); |
| 76 | + globalThis.fetch = vi.fn().mockRejectedValue(new Error("network")); |
| 77 | + const { playNotificationSound } = await import("./notificationSounds"); |
| 78 | + |
| 79 | + playNotificationSound("https://example.com/fail.mp3", "error", onDebug); |
| 80 | + |
| 81 | + await vi.waitFor(() => { |
| 82 | + expect(onDebug).toHaveBeenCalledWith( |
| 83 | + expect.objectContaining({ |
| 84 | + label: "audio/error load/play error", |
| 85 | + payload: "network", |
| 86 | + }), |
| 87 | + ); |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + it("attempts to resume suspended contexts before playback", async () => { |
| 92 | + const { resume, source } = installAudioMocks("suspended"); |
| 93 | + globalThis.fetch = vi.fn().mockResolvedValue({ |
| 94 | + arrayBuffer: vi.fn().mockResolvedValue(new ArrayBuffer(4)), |
| 95 | + } as unknown as Response); |
| 96 | + const { playNotificationSound } = await import("./notificationSounds"); |
| 97 | + |
| 98 | + playNotificationSound("https://example.com/test.mp3", "test"); |
| 99 | + |
| 100 | + await vi.waitFor(() => { |
| 101 | + expect(source.start).toHaveBeenCalledTimes(1); |
| 102 | + }); |
| 103 | + expect(resume).toHaveBeenCalledTimes(1); |
| 104 | + }); |
| 105 | +}); |
0 commit comments