Files
client/test/whisper-main.test.mjs
T
omar c75c8f3d74
release / linux-x64 (push) Failing after 59s
ci / linux-x64 (push) Failing after 1m9s
ci / linux-arm64-cross (push) Failing after 4m15s
release / linux-arm64 (push) Successful in 4m36s
release / appimage-startup (push) Skipped
release / publish (push) Skipped
release: prepare v0.1.5 verified account-free snapshot
2026-11-02 10:50:54 +03:00

148 lines
8.6 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { EventEmitter } from 'node:events';
import { createRequire } from 'node:module';
const require = createRequire(import.meta.url);
const { WhisperStore, KeybindStore, HotkeyHelper } = require('../app/main/whisper-main.js');
const { conflicts } = require('../app/main/keybind-schema.js');
const tempDir = () => fs.mkdtempSync(path.join(os.tmpdir(), 'tp-whisper-'));
test('local lists and profile bindings persist without starting any voice route; deleting target drops binding', () => {
const dir = tempDir();
try {
const store = new WhisperStore(dir);
const list = store.saveList({ name: 'Team', type: 1, whisper_entry_list: [{ type: 1, uuid: 'uid', name: 'Name' }] });
const keys = new KeybindStore(dir);
const profile = keys.addProfile('Main');
keys.setBindings(profile, [{ action: 'whisper_list', target: list.list_uuid, code: 'F9', global: false }]);
assert.equal(new WhisperStore(dir).get().lists[0].whisper_entry_list[0].uuid, 'uid');
assert.equal(new KeybindStore(dir).activeBindings().length, 1);
store.deleteList(list.list_uuid); keys.dropTarget(list.list_uuid);
assert.equal(keys.activeBindings().length, 0);
} finally { fs.rmSync(dir, { recursive: true, force: true }); }
});
test('version 1 whisper bindings migrate losslessly into one active profile and the v1 bytes are kept', () => {
const dir = tempDir();
try {
const v1 = { version: 1, bindings: [
{ id: '11111111-1111-4111-8111-111111111111', action: 'whisper_list', target: '22222222-2222-4222-8222-222222222222', code: 'F9', ctrl: true, alt: false, shift: false, meta: false, global: false },
{ id: '33333333-3333-4333-8333-333333333333', action: 'reply_whisper', target: '', code: 'KeyR', ctrl: false, alt: true, shift: false, meta: false, global: true },
] };
const raw = JSON.stringify(v1);
fs.writeFileSync(path.join(dir, 'keybinds.json'), raw);
const keys = new KeybindStore(dir, { now: () => 42, defaultProfileName: 'Standard' });
const state = keys.get();
assert.equal(state.profiles.length, 1);
assert.equal(state.profiles[0].name, 'Standard');
assert.equal(state.activeProfile, state.profiles[0].id);
assert.equal(state.requireExactMatch, true, 'version 1 always matched exactly');
assert.deepEqual(state.profiles[0].bindings, v1.bindings);
assert.equal(fs.readFileSync(path.join(dir, 'keybinds.v1-42.json'), 'utf8'), raw);
const saved = JSON.parse(fs.readFileSync(path.join(dir, 'keybinds.json'), 'utf8'));
assert.equal(saved.version, 2);
assert.deepEqual(new KeybindStore(dir).get(), state, 'v2 reload is stable');
} finally { fs.rmSync(dir, { recursive: true, force: true }); }
});
test('fresh store is "No Profile" with exact match off; profiles add/rename/activate/delete persist', () => {
const dir = tempDir();
try {
const keys = new KeybindStore(dir);
assert.deepEqual([keys.get().activeProfile, keys.get().profiles.length, keys.get().requireExactMatch], [null, 0, false]);
const a = keys.addProfile(' Gaming ');
const b = keys.addProfile('Work');
assert.equal(keys.get().activeProfile, a, 'the first profile becomes active');
keys.renameProfile(b, 'Office');
assert.throws(() => keys.deleteProfile(a), /activate another profile/);
keys.activateProfile(b);
keys.deleteProfile(a);
keys.setExact(true);
const reloaded = new KeybindStore(dir).get();
assert.deepEqual(reloaded.profiles.map((p) => p.name), ['Office']);
assert.equal(reloaded.activeProfile, b);
assert.equal(reloaded.requireExactMatch, true);
assert.throws(() => keys.addProfile(' '), /name/);
} finally { fs.rmSync(dir, { recursive: true, force: true }); }
});
test('bindings need a physical key; at most six bindings per action group', () => {
const dir = tempDir();
try {
const keys = new KeybindStore(dir);
const p = keys.addProfile('P');
assert.throws(() => keys.setBindings(p, [{ action: 'push_to_talk' }]), /code/);
assert.throws(() => keys.setBindings(p, [{ action: 'push_to_talk', code: 'F1', remote: { remoteApp: 'a', remoteButton: 'b' } }]), /unknown field/);
const seven = ['F1', 'F2', 'F3', 'F4', 'F5', 'F6', 'F7'].map((code) => ({ action: code === 'F1' ? 'microphone_mute' : 'microphone_toggle', code }));
assert.throws(() => keys.setBindings(p, seven), /at most 6/);
} finally { fs.rmSync(dir, { recursive: true, force: true }); }
});
test('global bindings default empty; offscreen helper cannot launch or register any key', () => {
let spawned = 0;
const helper = new HotkeyHelper({ enabled: false, resolveBinary: () => 'unused', spawnProcess: () => { spawned++; }, onEvent() {}, onStatus() {} });
helper.bind([{ id: 'key', code: 'F9', global: true }]);
assert.equal(spawned, 0); assert.equal(helper.status.global, false); assert.equal(helper.status.detail, 'test_offscreen');
});
test('binding conflicts include duplicate chords, the Audio PTT key and missing lists', () => {
const base = { code: 'F8', ctrl: false, alt: false, shift: false, meta: false, action: 'quick_whisper' };
const result = conflicts([{ ...base, id: 'a' }, { ...base, id: 'b' }], 'F8');
assert.deepEqual(new Set(result.map(value => value.id)), new Set(['a', 'b']));
for (const id of ['a', 'b']) {
assert.ok(result.some(value => value.id === id && value.reason === 'duplicate'), `${id} cannot activate a duplicate chord`);
assert.ok(result.some(value => value.id === id && value.reason === 'ptt'), `${id} cannot steal the push-to-talk chord`);
}
assert.deepEqual(conflicts([{ ...base, id: 'p', action: 'push_to_talk' }], 'F8'), [], 'a PTT binding on the PTT key is not a conflict');
assert.deepEqual(conflicts([{ ...base, id: 'w', action: 'whisper_list', target: '' }], null, new Set()), [{ id: 'w', reason: 'select_list' }]);
});
/** Fake `teamspeach-core hotkeys` child: records stdin lines, lets the test emit stdout lines. */
function fakeHelper() {
const child = new EventEmitter();
child.lines = [];
child.stdin = { write: (s) => { child.lines.push(JSON.parse(s)); }, end() {}, on() {} };
child.stdout = new EventEmitter(); child.stdout.setEncoding = () => {};
child.stderr = { resume() {} };
child.kill = () => {};
child.say = (msg) => child.stdout.emit('data', `${JSON.stringify(msg)}\n`);
return child;
}
test('exact match off registers extra-modifier variants on RegisterHotKey and reports them as the binding', () => {
const child = fakeHelper();
const events = [];
const helper = new HotkeyHelper({ enabled: true, resolveBinary: () => 'core', spawnProcess: () => child, onEvent: (e) => events.push(e), onStatus() {} });
const id = '44444444-4444-4444-8444-444444444444';
helper.bind([{ id, action: 'push_to_talk', code: 'F9', ctrl: false, alt: false, shift: false, meta: false, global: true }], { exact: false });
child.say({ event: 'ready', backend: 'windows_registerhotkey', global: true, release: true });
const bind = child.lines.at(-1);
assert.equal(bind.cmd, 'bind');
assert.equal(bind.bindings.length, 16, 'own chord plus 15 modifier supersets');
assert.equal(new Set(bind.bindings.map((b) => `${b.ctrl}${b.alt}${b.shift}${b.meta}`)).size, 16);
child.say({ event: 'bound', seq: bind.seq, results: bind.bindings.map((b) => ({ id: b.id, ok: true, trigger: '' })) });
assert.deepEqual(helper.status.results.map((r) => r.id), [id]);
assert.equal(helper.status.variants.registered, 15);
const variant = bind.bindings.find((b) => b.ctrl && !b.alt && !b.shift && !b.meta).id;
child.say({ event: 'press', id: variant });
child.say({ event: 'release', id: variant });
assert.deepEqual(events, [{ id, state: 'press', source: 'global' }, { id, state: 'release', source: 'global' }]);
child.say({ event: 'press', id });
helper.bind([], { exact: false });
assert.deepEqual(events.at(-1), { id, state: 'release', source: 'global', synthetic: true }, 'rebind releases held keys synthetically');
});
test('exact match on (and the portal backend) register only the bindings themselves', () => {
for (const [backend, exact] of [['windows_registerhotkey', true], ['xdg_portal_global_shortcuts', false]]) {
const child = fakeHelper();
const helper = new HotkeyHelper({ enabled: true, resolveBinary: () => 'core', spawnProcess: () => child, onEvent() {}, onStatus() {} });
helper.bind([{ id: '55555555-5555-4555-8555-555555555555', action: 'quick_whisper', code: 'F10', ctrl: true, global: true }], { exact });
child.say({ event: 'ready', backend, global: true, release: true });
assert.equal(child.lines.at(-1).bindings.length, 1, backend);
}
});