feat(webui): add completion notification sound preference

When a turn finishes while the user is watching the page there is no
audible cue; the existing browser notifications only cover the
background case. Add a `notificationSound` local preference (off by
default, next to `browserNotifications`) and play a short bundled
chime from the `turn_end` handler only while the page is visible, so
background tabs stay silent.

- webui/src/lib/notification-sound.ts: visibility- and preference-gated
  playback of the bundled /notification.wav, one reused Audio element
- Overview settings toggle with localized copy in all 10 locales
- Tests cover the default-off, visible, hidden, reuse and
  playback-rejection paths
This commit is contained in:
Li Fengmin authored and chengyongru committed 2026-09-24 17:34:33 +08:00
1 parent 3ecc3d3556
commit 7c2bf8949c
17 files changed
+156

No files matched your search

+2
View File
@@ -164,6 +164,7 @@ const LOCALIZED_SETTINGS_COPY_KEYS = [
"settings.rows.codeWrap",
"settings.rows.brandLogos",
"settings.rows.browserNotifications",
"settings.rows.notificationSound",
"settings.rows.currentModel",
"settings.rows.localServiceAccess",
"settings.rows.webuiDefaultAccess",
@@ -175,6 +176,7 @@ const LOCALIZED_SETTINGS_COPY_KEYS = [
"settings.help.codeWrap",
"settings.help.brandLogos",
"settings.help.browserNotifications",
"settings.help.notificationSound",
"settings.help.currentModel",
"settings.help.localServiceAccess",
"settings.help.webuiDefaultAccess",
@@ -0,0 +1,87 @@
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import {
DEFAULT_LOCAL_PREFS,
writeLocalPreferences,
} from "@/lib/local-preferences";
import { TURN_COMPLETE_SOUND_PATH } from "@/lib/notification-sound";
class MockAudio {
static instances: MockAudio[] = [];
src: string;
currentTime = 0;
play = vi.fn().mockResolvedValue(undefined);
constructor(src: string) {
this.src = src;
MockAudio.instances.push(this);
}
}
function setVisibility(state: DocumentVisibilityState): void {
Object.defineProperty(document, "visibilityState", {
configurable: true,
get: () => state,
});
}
describe("playTurnCompleteSound", () => {
beforeEach(() => {
MockAudio.instances = [];
vi.stubGlobal("Audio", MockAudio);
vi.resetModules();
setVisibility("visible");
});
afterEach(() => {
vi.unstubAllGlobals();
setVisibility("visible");
window.localStorage.clear();
});
// Fresh module per test: the module keeps one HTMLAudioElement cached for
// the page's lifetime, and the cache must not leak across cases.
async function play(): Promise<void> {
const { playTurnCompleteSound } = await import("@/lib/notification-sound");
playTurnCompleteSound();
}
it("stays silent by default", async () => {
await play();
expect(MockAudio.instances).toHaveLength(0);
});
it("plays the bundled chime when enabled and the page is visible", async () => {
writeLocalPreferences({ ...DEFAULT_LOCAL_PREFS, notificationSound: true });
await play();
expect(MockAudio.instances).toHaveLength(1);
expect(MockAudio.instances[0].src).toBe(TURN_COMPLETE_SOUND_PATH);
expect(MockAudio.instances[0].play).toHaveBeenCalledTimes(1);
});
it("stays silent when the page is in the background", async () => {
writeLocalPreferences({ ...DEFAULT_LOCAL_PREFS, notificationSound: true });
setVisibility("hidden");
await play();
expect(MockAudio.instances).toHaveLength(0);
});
it("reuses one audio element across turns and restarts it", async () => {
writeLocalPreferences({ ...DEFAULT_LOCAL_PREFS, notificationSound: true });
await play();
await play();
expect(MockAudio.instances).toHaveLength(1);
const first = MockAudio.instances[0];
expect(first.currentTime).toBe(0);
expect(first.play).toHaveBeenCalledTimes(2);
});
it("swallows playback rejections", async () => {
class RejectingAudio extends MockAudio {
play = vi.fn().mockRejectedValue(new Error("autoplay blocked"));
}
vi.stubGlobal("Audio", RejectingAudio);
writeLocalPreferences({ ...DEFAULT_LOCAL_PREFS, notificationSound: true });
await expect(play()).resolves.toBeUndefined();
});
});