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); }); });