Files
javis_bot/app/test/playback.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

122 lines
4.5 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { Playback, type PlaybackEnd } from "../src/core/playback.js";
/** A fake worker and speaker: records what was asked of them and what was played. */
function setup() {
const sent: { id: string; text: string; more: boolean }[] = [];
const cancelled: string[] = [];
const played: string[] = [];
let stops = 0;
const pb = new Playback(
{ tts: (id, text, more = false) => (sent.push({ id, text, more }), true), ttsCancel: (id) => void cancelled.push(id) },
{ play: (pcm) => void played.push(pcm.toString()), stop: () => void stops++ },
);
const ends: PlaybackEnd[] = [];
const starts: string[] = [];
pb.on("end", (e) => ends.push(e));
pb.on("start", (id) => starts.push(id));
return { pb, sent, cancelled, played, ends, starts, stops: () => stops };
}
const pcm = (s: string) => Buffer.from(s);
describe("Playback", () => {
it("plays the first sentence as soon as it arrives, then the next when the clip ends", () => {
const t = setup();
const id = t.pb.say("하나. 둘.");
expect(t.sent).toEqual([{ id, text: "하나. 둘.", more: false }]);
t.pb.audio(id, 0, pcm("s0"));
expect(t.played).toEqual(["s0"]);
expect(t.starts).toEqual([id]);
t.pb.audio(id, 1, pcm("s1"));
expect(t.played).toEqual(["s0"]); // still playing s0
t.pb.clipEnded();
expect(t.played).toEqual(["s0", "s1"]);
t.pb.done({ id, chunks: 2, cancelled: false, failed: false });
expect(t.ends).toEqual([]); // s1 is still playing
t.pb.clipEnded();
expect(t.ends).toEqual([{ id, played: 2, cancelled: false, failed: false }]);
expect(t.pb.busy).toBe(false);
});
it("plays requests in the order they were made", () => {
const t = setup();
const a = t.pb.say("a");
const b = t.pb.say("b");
t.pb.audio(b, 0, pcm("b0")); // b's audio arrives first
expect(t.played).toEqual([]);
t.pb.audio(a, 0, pcm("a0"));
t.pb.done({ id: a, chunks: 1, cancelled: false, failed: false });
t.pb.done({ id: b, chunks: 1, cancelled: false, failed: false });
t.pb.clipEnded();
t.pb.clipEnded();
expect(t.played).toEqual(["a0", "b0"]);
expect(t.ends.map((e) => e.id)).toEqual([a, b]);
});
it("keeps sentences in order even when they arrive out of order", () => {
const t = setup();
const id = t.pb.say("x", true);
t.pb.audio(id, 1, pcm("s1"));
expect(t.played).toEqual([]);
t.pb.audio(id, 0, pcm("s0"));
t.pb.clipEnded();
expect(t.played).toEqual(["s0", "s1"]);
});
it("skips a sentence that failed and still plays the rest", () => {
const t = setup();
const id = t.pb.say("a. b. c.");
t.pb.audio(id, 0, pcm("s0"));
t.pb.audio(id, 2, pcm("s2"));
t.pb.done({ id, chunks: 2, cancelled: false, failed: true });
t.pb.clipEnded();
t.pb.clipEnded();
expect(t.played).toEqual(["s0", "s2"]);
expect(t.ends).toEqual([{ id, played: 2, cancelled: false, failed: true }]);
});
it("stop silences the clip, cancels unfinished requests and drops the queue", () => {
const t = setup();
const a = t.pb.say("a", true);
const b = t.pb.say("b");
t.pb.audio(a, 0, pcm("a0"));
t.pb.done({ id: b, chunks: 0, cancelled: false, failed: false });
expect(t.pb.stop()).toBe(2);
expect(t.stops()).toBe(1);
expect(t.cancelled).toEqual([a]); // b was already done at the worker
expect(t.ends).toEqual([
{ id: a, played: 1, cancelled: true, failed: false },
{ id: b, played: 0, cancelled: true, failed: false },
]);
// Late audio for a stopped request is ignored.
t.pb.audio(a, 1, pcm("a1"));
t.pb.clipEnded();
expect(t.played).toEqual(["a0"]);
expect(t.pb.busy).toBe(false);
});
it("stop with nothing to say reports zero and touches nothing", () => {
const t = setup();
expect(t.pb.stop()).toBe(0);
expect(t.stops()).toBe(0);
});
it("text can be added to an open request", () => {
const t = setup();
const id = t.pb.say("첫 문장.", true);
t.pb.continueText(id, " 둘째 문장.", false);
expect(t.sent.map((s) => [s.text, s.more])).toEqual([["첫 문장.", true], [" 둘째 문장.", false]]);
});
it("a request the worker never accepted ends as failed without blocking the next", () => {
const t = setup();
const a = t.pb.say("a");
const b = t.pb.say("b");
t.pb.done({ id: a, chunks: 0, cancelled: false, failed: true });
t.pb.audio(b, 0, pcm("b0"));
expect(t.played).toEqual(["b0"]);
expect(t.ends[0]).toEqual({ id: a, played: 0, cancelled: false, failed: true });
});
});