Files
javis_bot/app/test/frames.test.ts
javis-bot 8479112ddd feat(app): M2 Discord voice core in the Electron app
- app/ (Electron 45 + TypeScript): the core joins one voice channel, decodes each speaker's Opus to
  48 kHz stereo and streams it to the voice worker, sends speaking_end and speaker_leave, and emits
  speech_start/partial/final with the speaker's display name.
- Sentence playback from the worker's TTS, first come first served, with stop that silences the clip
  at once and cancels unfinished requests; slash command join/leave/stop (create-or-update by name).
- Transcription notice posted on every join; other bots ignored by default; at most maxSpeakers
  people transcribed at once.
- Registered chat channels: live message events, history reads, sends split at 2,000 characters with
  mentions disabled; Message Content intent checked before login.
- The app starts the Python voice worker with a fresh token in the environment, restarts it with
  backoff, and the worker client reconnects and fails in-flight TTS on a lost connection.
- Spec in app/src/core/discord_core.spec.md; Vitest unit tests, a protocol test against the real
  Python worker server with fake engines, and an opt-in live test with two bots on real Discord.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 11:14:25 +09:00

27 lines
1.2 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { decodeTtsFrame, encodeAudioFrame, KIND_AUDIO, KIND_TTS } from "../src/core/frames.js";
describe("binary frames (voice_worker/protocol.py layout)", () => {
it("audio frame is kind, id length, UTF-8 id, then PCM", () => {
const pcm = Buffer.from([1, 0, 2, 0]);
const f = encodeAudioFrame("유저", pcm);
const id = Buffer.from("유저", "utf8");
expect([...f]).toEqual([KIND_AUDIO, id.length, ...id, 1, 0, 2, 0]);
});
it("rejects ids that do not fit in a byte", () => {
expect(() => encodeAudioFrame("", Buffer.alloc(2))).toThrow();
expect(() => encodeAudioFrame("x".repeat(256), Buffer.alloc(2))).toThrow();
});
it("decodes a TTS frame and refuses truncated ones", () => {
const id = Buffer.from("r1");
const seq = Buffer.alloc(4);
seq.writeUInt32LE(7);
const frame = Buffer.concat([Buffer.from([KIND_TTS, id.length]), id, seq, Buffer.from([9, 9])]);
expect(decodeTtsFrame(frame)).toEqual({ id: "r1", seq: 7, pcm: Buffer.from([9, 9]) });
expect(decodeTtsFrame(frame.subarray(0, 5))).toBeNull();
expect(decodeTtsFrame(Buffer.from([KIND_AUDIO, 1, 65, 0, 0, 0, 0]))).toBeNull();
});
});