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

68 lines
2.9 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { ChatAccessError, ChatChannels, hasMessageContentIntent, MESSAGE_LIMIT, splitMessage } from "../src/core/chat.js";
describe("splitMessage", () => {
it("leaves short text alone", () => {
expect(splitMessage("안녕하세요")).toEqual(["안녕하세요"]);
});
it("breaks at the last newline, then space, within the limit and loses only the break", () => {
const text = "aaaa bbbb\ncccc dddd";
const parts = splitMessage(text, 12);
expect(parts).toEqual(["aaaa bbbb", "cccc dddd"]);
expect(splitMessage("aaaa bbbb cccc", 10)).toEqual(["aaaa bbbb", "cccc"]);
});
it("cuts hard when there is no break, keeping emoji whole", () => {
const text = "x".repeat(MESSAGE_LIMIT - 1) + "😀" + "y";
const parts = splitMessage(text);
expect(parts.every((p) => p.length <= MESSAGE_LIMIT)).toBe(true);
expect(parts.join("")).toBe(text);
expect(parts[1]!.startsWith("😀")).toBe(true);
});
it("every part fits for long mixed text", () => {
const text = Array.from({ length: 900 }, (_, i) => `줄 ${i} 내용입니다`).join("\n");
const parts = splitMessage(text);
expect(parts.length).toBeGreaterThan(1);
expect(parts.every((p) => p.length > 0 && p.length <= MESSAGE_LIMIT)).toBe(true);
expect(parts.join("\n")).toBe(text);
});
});
describe("ChatChannels", () => {
const ch = new ChatChannels([
{ id: "111111111111111111", name: "일정", description: "", read: true, send: false },
{ id: "222222222222222222", name: "공지", description: "", read: false, send: true },
]);
it("follows each channel's read and send switches", () => {
expect(ch.canRead("111111111111111111")).toBe(true);
expect(ch.canSend("111111111111111111")).toBe(false);
expect(ch.canRead("222222222222222222")).toBe(false);
expect(ch.canSend("222222222222222222")).toBe(true);
expect(() => ch.assertSend("111111111111111111")).toThrow(ChatAccessError);
expect(() => ch.assertRead("222222222222222222")).toThrow(ChatAccessError);
});
it("refuses channels that are not registered", () => {
expect(ch.canRead("333333333333333333")).toBe(false);
expect(() => ch.assertRead("333333333333333333")).toThrow(/not a registered/);
expect(() => ch.assertSend("333333333333333333")).toThrow(/not a registered/);
});
it("needs the message intents only when some channel is readable", () => {
expect(ch.anyReadable).toBe(true);
expect(new ChatChannels([{ id: "1".repeat(18), name: "", description: "", read: false, send: true }]).anyReadable)
.toBe(false);
});
});
describe("hasMessageContentIntent", () => {
it("accepts the full and the limited intent flag", () => {
expect(hasMessageContentIntent(1 << 18)).toBe(true);
expect(hasMessageContentIntent(1 << 19)).toBe(true);
expect(hasMessageContentIntent((1 << 15) | (1 << 13))).toBe(false);
});
});