@devries-cameron20 / t3code Mirror · read-only / commits / c1f9d20263
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 23, 04:15 UTC

fix(web): threads finishing together play one completion sound (#17540)

luke committed Oct 18, 2026 at 20:21 UTC c1f9d20263657486484f2ff7607a31a2c230c5a6
2 files changed +108
apps/web/src/threadNotifications.test.ts new
+100
@@ -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
+8
@@ -66,6 +66,8 @@ export function setNotificationBadge(count: number) {
66
67 let audioContext: AudioContext | undefined;
68 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>();
71
72 /** Called from a gesture so browsers allow later background playback. */
73 export function unlockNotificationAudio() {
@@ -90,10 +92,16 @@ export async function playNotificationSound(
92 }
93 const decoded = await buffer;
94 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;
100 const source = context.createBufferSource();
101 source.buffer = decoded;
102 source.connect(context.destination);
103 source.start();
104 + playingUntil.set(url, context.currentTime + decoded.duration);
105 } catch {
106 buffers.delete(url);
107 }