Skip to content

Commit 9f7ab6e

Browse files
luke2xclaude
andcommitted
fix(web): threads finishing together play one completion sound
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent b150867 commit 9f7ab6e

2 files changed

Lines changed: 108 additions & 0 deletions

File tree

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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+
});

‎apps/web/src/threadNotifications.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ export function setNotificationBadge(count: number) {
6666

6767
let audioContext: AudioContext | undefined;
6868
const buffers = new Map<string, Promise<AudioBuffer>>();
69+
/** When each sound's current playback ends, on the audio context's clock. */
70+
const playingUntil = new Map<string, number>();
6971

7072
/** Called from a gesture so browsers allow later background playback. */
7173
export function unlockNotificationAudio() {
@@ -90,10 +92,16 @@ export async function playNotificationSound(
9092
}
9193
const decoded = await buffer;
9294
if (!shouldPlay() || context.state !== "running") return;
95+
// Threads that finish together each ask for this sound, and full-volume copies sum into
96+
// clipping. One playback stands for the burst. Checked after the decode, since every caller
97+
// in a burst is waiting on it. The other kind keeps its own slot, so an input alert is still
98+
// heard over a completion.
99+
if (context.currentTime < (playingUntil.get(url) ?? 0)) return;
93100
const source = context.createBufferSource();
94101
source.buffer = decoded;
95102
source.connect(context.destination);
96103
source.start();
104+
playingUntil.set(url, context.currentTime + decoded.duration);
97105
} catch {
98106
buffers.delete(url);
99107
}

0 commit comments

Comments
 (0)