Files
piik/tests/room-preferences.test.ts
T
AAA c8e85263df feat(voice): hold a talk shortcut for push to talk
Push to talk needs a key that is held, not toggled. A third binding,
talk, follows the existing binding rule and defaults to Ctrl+Shift+V
(Command+Shift+V on macOS), unless another action already holds that
chord. Stored bindings from before parse unchanged, and a reset restores
all three.

In push-to-talk mode the page now listens for key up as well as key
down. Repeats are ignored, and editors keep the chord as before. The
chord ends when its key or any of its modifiers comes up, because macOS
sends no key-up for a letter released while Command is down. Losing
focus, a hidden page or a frozen page also releases it, through the
existing document-inactive owner, so a lost key-up cannot leave the
microphone open. Changing the dock's room or leaving push to talk
releases a held key too.

Holding the key on a muted microphone sends nothing, and the dock says
"Microphone off" in a small notice above the microphone. It is a manual
popover placed by the shared anchored-popover owner, so the controls do
not move. The talk row appears in the shortcut settings only in push to
talk; with background App shortcuts on, it says it works while the page
or room tool has focus. A clash with a hidden binding now names the
action holding the chord, in all five languages.

The App still receives only the two toggles it registers, because it
rejects unknown fields; nothing changes on the wire.

Verified with tsc -b and tests for key down, key up, repeats, release by
a modifier, blur, hidden and frozen pages, editors, mode and room
changes, the default merge, preferences stored before the talk binding,
and the App request.
2026-11-25 14:05:45 +08:00

184 lines
11 KiB
TypeScript

import { afterEach, expect, it, vi } from "vitest";
import { readRoomPreferences, saveRoomPreferences } from "../src/client/lib/room-preferences";
import { RoomInteractionSession } from "../src/client/lib/room-interactions";
import { FEEDBACK_CLIP_MAX_BYTES } from "../src/client/channel/feedback-clip";
afterEach(() => vi.unstubAllGlobals());
function storage() {
const values = new Map<string, string>();
vi.stubGlobal("window", { localStorage: {
getItem: (key: string) => values.get(key) ?? null,
setItem: (key: string, value: string) => values.set(key, value),
} });
return values;
}
it("keeps independent room-tool choices across reads without storing private runtime fields", () => {
const values = storage();
expect(readRoomPreferences().previewAutoPause).toBe(true);
expect(readRoomPreferences().sharingView).toBe(false);
expect(readRoomPreferences().interfaceLayout).toBeUndefined();
expect(readRoomPreferences().remoteTouchMode).toBe("direct");
saveRoomPreferences({ sharingView: true, interfaceLayout: "quick", pictureFit: "cover", pictureRotation: 270, previewAutoPause: false, keyboardPage: "text", combineKeys: true });
saveRoomPreferences({ remoteControlMode: "password", remoteReleaseKey: 9, remoteStopKey: 8, remoteTouchMode: "trackpad",
voiceListeningVolume: 1.5, voiceInput: { deviceId: "", volume: 2, voiceProcessing: true } });
expect(readRoomPreferences()).toMatchObject({ sharingView: true, interfaceLayout: "quick", pictureFit: "cover", pictureRotation: 270, previewAutoPause: false, keyboardPage: "text",
combineKeys: true, remoteControlMode: "password", remoteReleaseKey: 9, remoteStopKey: 8, remoteTouchMode: "trackpad",
voiceListeningVolume: 1.5, voiceInput: { deviceId: "", volume: 2, voiceProcessing: true } });
// Storage accepts only the preference contract, even if a caller supplies a
// room snapshot with credentials, text or actual held input by mistake.
saveRoomPreferences(Object.assign({ overlayEnabled: true }, {
password: "private", grant: "private", text: "private", modifiers: ["ControlLeft"], controller: "other-peer",
}));
const saved = JSON.parse([...values.values()][0]);
expect(saved.overlayEnabled).toBe(true);
expect(saved.remoteControlMode).toBe("password");
for (const field of ["password", "grant", "text", "modifiers", "controller"]) expect(saved).not.toHaveProperty(field);
expect([...values.values()][0]).not.toContain("private");
expect(readRoomPreferences().pictureFit).toBe("cover");
saveRoomPreferences(JSON.parse('{"remoteTouchMode":"unknown"}'));
expect(readRoomPreferences().remoteTouchMode).toBe("trackpad");
values.set("piik:room-preferences:v1", JSON.stringify({ pictureFit: "cover", pictureRotation: 45 }));
expect(readRoomPreferences()).toMatchObject({ pictureFit: "cover", pictureRotation: 0, remoteTouchMode: "direct" });
values.set("piik:room-preferences:v1", JSON.stringify({ overlayAppearance: { scale: 1.2, opacity: .7 } }));
expect(readRoomPreferences().overlayAppearance).toEqual({ scale: 1.2, opacity: .7, speed: 1, colorBySender: false });
});
it("isolates nested preferences across reads and observes external edits after a failed write", () => {
const values = storage();
const key = "piik:room-preferences:v1";
const clip = { name: "short.wav", data: "AAAA" };
values.set(key, JSON.stringify({ feedbackSound: { enabled: true, volume: .5, clips: { message: clip } } }));
readRoomPreferences().feedbackSound.clips!.message!.name = "first edit";
readRoomPreferences().feedbackSound.clips!.message!.name = "second edit";
expect(readRoomPreferences().feedbackSound.clips?.message).toEqual(clip);
vi.spyOn(window.localStorage, "setItem").mockImplementation(() => { throw new Error("quota full"); });
expect(saveRoomPreferences({ pictureFit: "cover" })).toBe(false);
expect(readRoomPreferences().pictureFit).toBe("contain");
values.set(key, JSON.stringify({ pictureFit: "fill" }));
expect(readRoomPreferences().pictureFit).toBe("fill");
values.delete(key);
expect(readRoomPreferences().pictureFit).toBe("contain");
});
it("keeps layout undecided until an explicit choice, independently of other preferences", () => {
const values = storage();
expect(saveRoomPreferences({ feedbackSound: { enabled: true, volume: .5 } })).toBe(true);
expect(readRoomPreferences().interfaceLayout).toBeUndefined();
expect(JSON.parse(values.get("piik:room-preferences:v1")!)).not.toHaveProperty("interfaceLayout");
for (const layout of ["workspace", "quick"] as const) {
expect(saveRoomPreferences({ interfaceLayout: layout })).toBe(true);
expect(saveRoomPreferences({ overlayEnabled: true })).toBe(true);
expect(readRoomPreferences().interfaceLayout).toBe(layout);
expect(JSON.parse(values.get("piik:room-preferences:v1")!).interfaceLayout).toBe(layout);
}
});
it("adds stick defaults to existing preferences and persists only normalized positions", () => {
const values = storage();
const previous = { interfaceLayout: "quick", pictureFit: "cover", remoteTouchMode: "trackpad" };
values.set("piik:room-preferences:v1", JSON.stringify(previous));
expect(readRoomPreferences()).toMatchObject({
...previous, virtualSticksEnabled: true, stickPositions: {}, touchOnlySticks: false,
});
const sticks = {
virtualSticksEnabled: false,
stickPositions: { left: { x: 0, y: .25 }, right: { x: 1, y: .75 } },
touchOnlySticks: true,
};
expect(saveRoomPreferences(sticks)).toBe(true);
expect(saveRoomPreferences({ stickPositions: { left: { x: 1.1, y: 0 } } })).toBe(false);
expect(saveRoomPreferences({ stickPositions: { right: { x: 1, y: -.1 } } })).toBe(false);
expect(readRoomPreferences()).toMatchObject({ ...previous, ...sticks });
});
it("rejects malformed saved data and invalid patches while retaining useful defaults", () => {
const values = storage();
const defaults = readRoomPreferences();
saveRoomPreferences({ remoteControlMode: "room" });
const [key] = [...values.keys()];
saveRoomPreferences(JSON.parse('{"remoteControlMode":"off"}'));
expect(readRoomPreferences().remoteControlMode).toBe("room");
for (const raw of ["{", "null", JSON.stringify({ overlayAppearance: { scale: 100, opacity: 0 } }), JSON.stringify({ remoteTouchMode: "unknown" })]) {
values.set(key, raw);
expect(readRoomPreferences()).toEqual(defaults);
}
});
it("restores overlay choices in a new room without restoring messages or room authority", () => {
storage();
const first = new RoomInteractionSession(() => true);
first.setOverlayEnabled(true);
first.setOverlayAppearance({ scale: 100, opacity: -1, speed: .8, colorBySender: true });
first.close();
const next = new RoomInteractionSession(() => true);
expect(next.getSnapshot()).toMatchObject({ overlayEnabled: true, overlayAppearance: { scale: 1.3, opacity: .4, speed: .8, colorBySender: true },
ready: false, peerId: null, messages: [], overlayMessages: [], reactions: [], pending: null });
next.close();
});
it("keeps room tools working when Browser storage is unavailable", () => {
vi.stubGlobal("window", { get localStorage() { throw new Error("storage denied"); } });
expect(readRoomPreferences()).toMatchObject({ pictureFit: "contain", remoteControlMode: "ask", remoteTouchMode: "direct" });
expect(() => saveRoomPreferences({ keyboardPage: "symbols" })).not.toThrow();
const session = new RoomInteractionSession(() => true);
session.setOverlayEnabled(true);
session.setOverlayAppearance({ scale: 1.2, opacity: .5 });
expect(session.getSnapshot()).toMatchObject({ overlayEnabled: true, overlayAppearance: { scale: 1.2, opacity: .5 } });
session.close();
});
it("keeps bounded custom sounds with their preferences and reports a failed persistent write", () => {
storage();
const clip = { name: "short.wav", data: Buffer.alloc(FEEDBACK_CLIP_MAX_BYTES).toString("base64") };
expect(saveRoomPreferences({ feedbackSound: { enabled: true, volume: .6, clips: { message: clip } } })).toBe(true);
saveRoomPreferences({ pictureFit: "cover" });
expect(readRoomPreferences().feedbackSound).toEqual({ enabled: true, volume: .6, clips: { message: clip } });
expect(saveRoomPreferences({ feedbackSound: { enabled: true, volume: .6,
clips: { message: { ...clip, data: Buffer.alloc(FEEDBACK_CLIP_MAX_BYTES + 1).toString("base64") } } } })).toBe(false);
expect(readRoomPreferences().feedbackSound.clips?.message).toEqual(clip);
vi.spyOn(window.localStorage, "setItem").mockImplementation(() => { throw new Error("quota full"); });
expect(saveRoomPreferences({ feedbackSound: { enabled: true, volume: .6, clips: {} } })).toBe(false);
expect(readRoomPreferences().feedbackSound.clips?.message).toEqual(clip);
});
it("adds the microphone mode to stored preferences that predate it, as open microphone", () => {
const values = storage();
const key = "piik:room-preferences:v1";
const previous = { voiceInput: { deviceId: "saved", volume: .8, voiceProcessing: false }, voiceListeningVolume: 1.4, pictureFit: "cover" };
values.set(key, JSON.stringify(previous));
expect(readRoomPreferences()).toMatchObject({ ...previous, voiceTransmission: { mode: "open", activationThreshold: -40 } });
expect(saveRoomPreferences({ voiceTransmission: { mode: "activity", activationThreshold: -52 } })).toBe(true);
expect(readRoomPreferences()).toMatchObject({ ...previous, voiceTransmission: { mode: "activity", activationThreshold: -52 } });
// A value this version cannot use falls back on its own, keeping the other choices.
values.set(key, JSON.stringify({ ...previous, voiceTransmission: { mode: "whisper", activationThreshold: -52 } }));
expect(readRoomPreferences()).toMatchObject({ ...previous, voiceTransmission: { mode: "open", activationThreshold: -40 } });
});
it("keeps voice shortcuts stored before the talk binding existed, and stores it once chosen", () => {
const values = storage();
const key = "piik:room-preferences:v1";
const microphone = { key: "Q", ctrl: true, alt: false, shift: true, meta: false };
values.set(key, JSON.stringify({ voiceShortcuts: { microphone, listening: null } }));
expect(readRoomPreferences().voiceShortcuts).toEqual({ microphone, listening: null });
expect(saveRoomPreferences({ voiceShortcuts: { microphone, listening: null, talk: { ...microphone, key: "T" } } })).toBe(true);
expect(readRoomPreferences().voiceShortcuts.talk).toEqual({ ...microphone, key: "T" });
expect(saveRoomPreferences({ voiceShortcuts: { microphone, listening: null, talk: null } })).toBe(true);
expect(readRoomPreferences().voiceShortcuts.talk).toBeNull();
});
it("persists the total screen buffer target independently, including zero and automatic", () => {
storage();
expect(readRoomPreferences().screenBufferTargetMs).toBeNull();
for (const target of [300, 0, 4000, null]) {
expect(saveRoomPreferences({ screenBufferTargetMs: target })).toBe(true);
expect(readRoomPreferences().screenBufferTargetMs).toBe(target);
}
expect(saveRoomPreferences({ screenBufferTargetMs: 4001 })).toBe(false);
expect(readRoomPreferences().screenBufferTargetMs).toBeNull();
});