STT_PROVIDER now defaults to "builtin": Whisper (onnx-community/whisper-base, q8) runs in the bridge process on the CPU via @huggingface/transformers, so no provider account is needed and recordings stay on the bridge host. Silent captures are detected by level before the model runs. The external OpenAI-compatible adapter remains available. Tests: 130 unit/integration; 4 scenarios with the real model on a real speech recording (npm run test:real-stt). Not tested on audio from the glasses or on a droplet. deploy/ and docs/deploy-droplet.md are examples that have not been run. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
152 lines
7.5 KiB
TypeScript
152 lines
7.5 KiB
TypeScript
import { describe, expect, it } from 'vitest';
|
|
import { ConfigError, loadConfig } from '../src/config.ts';
|
|
import { openDb } from '../src/db.ts';
|
|
import { Devices } from '../src/devices.ts';
|
|
import { buildRunBody } from '../src/hermes/adapter.ts';
|
|
import { mapRunStatus } from '../src/hermes/runsAdapter.ts';
|
|
import { RateLimiter } from '../src/ratelimit.ts';
|
|
import { pcmToWav } from '../src/stt/adapter.ts';
|
|
|
|
const base = { HERMES_API_KEY: 'k', HERMEVEN_DB_PATH: ':memory:', STT_PROVIDER: 'none' };
|
|
|
|
describe('configuration', () => {
|
|
it('binds loopback by default and refuses a public bind without an explicit acknowledgement', () => {
|
|
expect(loadConfig(base).host).toBe('127.0.0.1');
|
|
expect(() => loadConfig({ ...base, HERMEVEN_HOST: '0.0.0.0' })).toThrow(ConfigError);
|
|
expect(loadConfig({ ...base, HERMEVEN_HOST: '0.0.0.0', HERMEVEN_ALLOW_NON_LOOPBACK: 'true' }).host).toBe('0.0.0.0');
|
|
});
|
|
|
|
it('requires https for Hermes and STT outside loopback', () => {
|
|
expect(() => loadConfig({ ...base, HERMES_BASE_URL: 'http://hermes.example:8642' })).toThrow(/https/);
|
|
expect(loadConfig({ ...base, HERMES_BASE_URL: 'https://hermes.example/p/work/' }).hermes.baseUrl).toBe('https://hermes.example/p/work');
|
|
expect(() =>
|
|
loadConfig({ ...base, STT_PROVIDER: 'openai-compatible', STT_BASE_URL: 'http://stt.example/v1', STT_MODEL: 'm' }),
|
|
).toThrow(/https/);
|
|
});
|
|
|
|
it('requires a Hermes key and exact CORS origins', () => {
|
|
expect(() => loadConfig({ HERMEVEN_DB_PATH: ':memory:' })).toThrow(/HERMES_API_KEY/);
|
|
expect(() => loadConfig({ ...base, HERMEVEN_ALLOWED_ORIGINS: '*' })).toThrow(ConfigError);
|
|
expect(() => loadConfig({ ...base, HERMEVEN_ALLOWED_ORIGINS: 'https://a.example/path' })).toThrow(ConfigError);
|
|
expect(loadConfig({ ...base, HERMEVEN_ALLOWED_ORIGINS: 'https://a.example, http://localhost:5173' }).allowedOrigins).toEqual([
|
|
'https://a.example',
|
|
'http://localhost:5173',
|
|
]);
|
|
});
|
|
|
|
it('derives the audio byte cap from the capture cap and never puts secrets in error text', () => {
|
|
const config = loadConfig({ ...base, HERMEVEN_CAPTURE_MAX_SECONDS: '10' });
|
|
expect(config.limits.audioMaxBytes).toBe(11 * 32000);
|
|
try {
|
|
loadConfig({ ...base, HERMES_API_KEY: 'super-secret', HERMEVEN_PORT: 'abc' });
|
|
throw new Error('should have thrown');
|
|
} catch (err) {
|
|
expect(String(err)).not.toContain('super-secret');
|
|
}
|
|
});
|
|
});
|
|
|
|
describe('device credentials', () => {
|
|
const setup = (env: Record<string, string> = {}) => {
|
|
let now = 1_000_000;
|
|
const config = loadConfig({ ...base, ...env });
|
|
const devices = new Devices(openDb(':memory:'), config, () => now);
|
|
return { devices, config, advance: (ms: number) => (now += ms) };
|
|
};
|
|
|
|
it('stores only a hash of the token and of the pairing code', () => {
|
|
const { devices } = setup();
|
|
const { code } = devices.createPairingCode();
|
|
const claimed = devices.claimPairingCode(code, 'g2');
|
|
if (!claimed.ok) throw new Error('claim failed');
|
|
const row = devices.get(claimed.device.id)!;
|
|
expect(row.token_hash).not.toContain(claimed.token);
|
|
expect(row.token_hash).toMatch(/^[0-9a-f]{64}$/);
|
|
expect(JSON.stringify(row)).not.toContain(claimed.token);
|
|
});
|
|
|
|
it('pairing codes expire, are case/format tolerant, and a new code voids the old one', () => {
|
|
const { devices, advance } = setup({ HERMEVEN_PAIRING_CODE_TTL_SECONDS: '60' });
|
|
const first = devices.createPairingCode();
|
|
const second = devices.createPairingCode();
|
|
expect(devices.claimPairingCode(first.code, 'x').ok).toBe(false);
|
|
expect(devices.claimPairingCode(second.code.toLowerCase().replace('-', ' '), 'x').ok).toBe(true);
|
|
const third = devices.createPairingCode();
|
|
advance(61_000);
|
|
expect(devices.claimPairingCode(third.code, 'x')).toEqual({ ok: false, reason: 'pairing_invalid' });
|
|
});
|
|
|
|
it('tokens expire, can be renewed while valid, and never outlive the pairing', () => {
|
|
const { devices, advance } = setup({ HERMEVEN_TOKEN_TTL_SECONDS: '100', HERMEVEN_PAIRING_TTL_SECONDS: '250' });
|
|
const claimed = devices.claimPairingCode(devices.createPairingCode().code, 'g2');
|
|
if (!claimed.ok) throw new Error('claim failed');
|
|
advance(90_000);
|
|
expect(devices.authenticate(claimed.token).ok).toBe(true);
|
|
const renewed = devices.renew(claimed.device.id);
|
|
expect(devices.authenticate(claimed.token)).toEqual({ ok: false, reason: 'unauthorized' });
|
|
advance(90_000);
|
|
expect(devices.authenticate(renewed.token).ok).toBe(true);
|
|
const again = devices.renew(claimed.device.id);
|
|
// Renewal cannot push past the absolute pairing lifetime (t = 250 s).
|
|
expect(again.device.token_expires_at).toBe(again.device.pairing_expires_at);
|
|
advance(80_000);
|
|
expect(devices.authenticate(again.token)).toEqual({ ok: false, reason: 'credential_expired' });
|
|
});
|
|
|
|
it('a device paired for one profile is not valid for another', () => {
|
|
const db = openDb(':memory:');
|
|
const a = new Devices(db, loadConfig({ ...base, HERMES_PROFILE_LABEL: 'personal' }));
|
|
const claimed = a.claimPairingCode(a.createPairingCode().code, 'g2');
|
|
if (!claimed.ok) throw new Error('claim failed');
|
|
const b = new Devices(db, loadConfig({ ...base, HERMES_PROFILE_LABEL: 'work' }));
|
|
expect(b.authenticate(claimed.token)).toEqual({ ok: false, reason: 'unauthorized' });
|
|
});
|
|
|
|
it('pairing codes have enough entropy and an unambiguous alphabet', () => {
|
|
const { devices } = setup();
|
|
const codes = new Set(Array.from({ length: 200 }, () => devices.createPairingCode().code));
|
|
expect(codes.size).toBe(200);
|
|
for (const code of codes) expect(code).toMatch(/^[A-HJ-KM-NP-Z2-9]{5}-[A-HJ-KM-NP-Z2-9]{5}$/);
|
|
});
|
|
});
|
|
|
|
describe('Hermes adapter helpers', () => {
|
|
it('builds a byte-stable request body', () => {
|
|
const a = buildRunBody({ input: 'hi "there"', sessionId: 'hermeven-1', instructions: 'be brief' });
|
|
expect(a).toBe('{"input":"hi \\"there\\"","session_id":"hermeven-1","instructions":"be brief"}');
|
|
expect(buildRunBody({ input: 'hi', sessionId: 's', instructions: '' })).toBe('{"input":"hi","session_id":"s"}');
|
|
});
|
|
|
|
it('maps run statuses, treating anything unrecognised as still running', () => {
|
|
expect(mapRunStatus({ status: 'completed', output: 'x' })).toEqual({ state: 'completed', output: 'x', finished: true, error: null });
|
|
expect(mapRunStatus({ status: 'failed', output: 'part', completed: false, partial: true }).finished).toBe(false);
|
|
expect(mapRunStatus({ status: 'waiting_for_approval' }).state).toBe('waiting_for_approval');
|
|
expect(mapRunStatus({ status: 'unknown' }).state).toBe('unknown');
|
|
expect(mapRunStatus({ status: 'some_future_status' }).state).toBe('running');
|
|
expect(mapRunStatus({ status: 'completed', output: { not: 'text' } }).output).toBe('');
|
|
});
|
|
});
|
|
|
|
describe('small utilities', () => {
|
|
it('rate limiter enforces a fixed window per key', () => {
|
|
let now = 0;
|
|
const limiter = new RateLimiter(2, 1000, () => now);
|
|
expect([limiter.take('a'), limiter.take('a'), limiter.take('a'), limiter.take('b')]).toEqual([true, true, false, true]);
|
|
now = 1000;
|
|
expect(limiter.take('a')).toBe(true);
|
|
});
|
|
|
|
it('wraps PCM in a correct WAV header', () => {
|
|
const wav = pcmToWav(Buffer.alloc(3200, 1));
|
|
expect(wav.length).toBe(3244);
|
|
expect(wav.toString('ascii', 0, 4)).toBe('RIFF');
|
|
expect(wav.readUInt32LE(4)).toBe(3236);
|
|
expect(wav.readUInt16LE(20)).toBe(1);
|
|
expect(wav.readUInt16LE(22)).toBe(1);
|
|
expect(wav.readUInt32LE(24)).toBe(16000);
|
|
expect(wav.readUInt32LE(28)).toBe(32000);
|
|
expect(wav.readUInt16LE(34)).toBe(16);
|
|
expect(wav.readUInt32LE(40)).toBe(3200);
|
|
});
|
|
});
|