-
-
Notifications
You must be signed in to change notification settings - Fork 232
feat: switch notification playback to Web Audio API to avoid macOS media session #442
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Dimillian
merged 4 commits into
main
from
codex/github-mention-notification-sounds-register-app-in-macos-no
Feb 16, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
62f30da
Use Web Audio API for notification sounds
Dimillian 78bdf33
Merge branch 'main' into codex/github-mention-notification-sounds-reg…
Dimillian edbc6ee
Merge branch 'main' into codex/github-mention-notification-sounds-reg…
Dimillian f195dea
Merge branch 'main' into codex/github-mention-notification-sounds-reg…
Dimillian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| // @vitest-environment jsdom | ||
|
|
||
| import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; | ||
|
|
||
| type MockAudioContextState = "running" | "suspended" | "closed"; | ||
|
|
||
| describe("playNotificationSound", () => { | ||
| const originalAudioContext = window.AudioContext; | ||
| const originalWebkitAudioContext = ( | ||
| window as typeof window & { webkitAudioContext?: typeof AudioContext } | ||
| ).webkitAudioContext; | ||
| const originalFetch = globalThis.fetch; | ||
|
|
||
| beforeEach(() => { | ||
| vi.resetModules(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| window.AudioContext = originalAudioContext; | ||
| ( | ||
| window as typeof window & { webkitAudioContext?: typeof AudioContext } | ||
| ).webkitAudioContext = originalWebkitAudioContext; | ||
| globalThis.fetch = originalFetch; | ||
| vi.restoreAllMocks(); | ||
| }); | ||
|
|
||
| function installAudioMocks(state: MockAudioContextState = "running") { | ||
| const source = { | ||
| buffer: null as AudioBuffer | null, | ||
| connect: vi.fn(), | ||
| start: vi.fn(), | ||
| }; | ||
| const gainNode = { | ||
| gain: { value: 0 }, | ||
| connect: vi.fn(), | ||
| }; | ||
| const decodeAudioData = vi.fn().mockResolvedValue({} as AudioBuffer); | ||
| const resume = vi.fn().mockResolvedValue(undefined); | ||
|
|
||
| class MockAudioContext { | ||
| state = state; | ||
| destination = {} as AudioNode; | ||
| decodeAudioData = decodeAudioData; | ||
| createBufferSource = vi.fn(() => source); | ||
| createGain = vi.fn(() => gainNode); | ||
| resume = resume; | ||
| } | ||
|
|
||
| window.AudioContext = MockAudioContext as unknown as typeof AudioContext; | ||
|
|
||
| return { decodeAudioData, gainNode, source, resume }; | ||
| } | ||
|
|
||
| it("plays notification audio via Web Audio API", async () => { | ||
| const { decodeAudioData, gainNode, source } = installAudioMocks("running"); | ||
| const arrayBuffer = new ArrayBuffer(8); | ||
| globalThis.fetch = vi.fn().mockResolvedValue({ | ||
| arrayBuffer: vi.fn().mockResolvedValue(arrayBuffer), | ||
| } as unknown as Response); | ||
|
|
||
| const { playNotificationSound } = await import("./notificationSounds"); | ||
|
|
||
| playNotificationSound("https://example.com/success.mp3", "success"); | ||
| await vi.waitFor(() => { | ||
| expect(source.start).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| expect(globalThis.fetch).toHaveBeenCalledWith("https://example.com/success.mp3"); | ||
| expect(decodeAudioData).toHaveBeenCalledWith(arrayBuffer); | ||
| expect(gainNode.gain.value).toBe(0.05); | ||
| }); | ||
|
|
||
| it("logs debug information when fetch fails", async () => { | ||
| installAudioMocks("running"); | ||
| const onDebug = vi.fn(); | ||
| globalThis.fetch = vi.fn().mockRejectedValue(new Error("network")); | ||
| const { playNotificationSound } = await import("./notificationSounds"); | ||
|
|
||
| playNotificationSound("https://example.com/fail.mp3", "error", onDebug); | ||
|
|
||
| await vi.waitFor(() => { | ||
| expect(onDebug).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| label: "audio/error load/play error", | ||
| payload: "network", | ||
| }), | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| it("attempts to resume suspended contexts before playback", async () => { | ||
| const { resume, source } = installAudioMocks("suspended"); | ||
| globalThis.fetch = vi.fn().mockResolvedValue({ | ||
| arrayBuffer: vi.fn().mockResolvedValue(new ArrayBuffer(4)), | ||
| } as unknown as Response); | ||
| const { playNotificationSound } = await import("./notificationSounds"); | ||
|
|
||
| playNotificationSound("https://example.com/test.mp3", "test"); | ||
|
|
||
| await vi.waitFor(() => { | ||
| expect(source.start).toHaveBeenCalledTimes(1); | ||
| }); | ||
| expect(resume).toHaveBeenCalledTimes(1); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.