Monorepo for Aesthetic.Computer aesthetic.computer
at main 113 lines 3.9 kB view raw
1// Chat Tests, 25.11.28 2// Tests for the ChatManager multi-instance chat system 3 4import { describe, it, expect, beforeAll, afterAll, vi } from "vitest"; 5import { WebSocket } from "ws"; 6import { ChatManager, chatInstances } from "../chat-manager.mjs"; 7 8describe("ChatManager", () => { 9 let chatManager; 10 11 beforeAll(async () => { 12 // Create ChatManager without MongoDB for testing 13 chatManager = new ChatManager({ 14 dev: true, 15 mongoConnectionString: null, // Explicitly disable MongoDB 16 }); 17 // Don't call init() - it would try to connect to MongoDB 18 }); 19 20 describe("Instance Configuration", () => { 21 it("should have three chat instances configured", () => { 22 const instances = Object.keys(chatInstances); 23 expect(instances).toContain("chat-system.aesthetic.computer"); 24 expect(instances).toContain("chat-clock.aesthetic.computer"); 25 expect(instances).toContain("chat.sotce.net"); 26 }); 27 28 it("should identify chat hosts correctly", () => { 29 expect(chatManager.isChatHost("chat-system.aesthetic.computer")).toBe(true); 30 expect(chatManager.isChatHost("chat-clock.aesthetic.computer")).toBe(true); 31 expect(chatManager.isChatHost("chat.sotce.net")).toBe(true); 32 expect(chatManager.isChatHost("session-server.aesthetic.computer")).toBe(false); 33 expect(chatManager.isChatHost("random.com")).toBe(false); 34 }); 35 36 it("should get correct instance for host", () => { 37 const instance = chatManager.getInstance("chat-system.aesthetic.computer"); 38 expect(instance).toBeDefined(); 39 expect(instance.config.name).toBe("chat-system"); 40 }); 41 }); 42 43 describe("Message Packing", () => { 44 it("should pack messages with type and content", () => { 45 const packed = chatManager.pack("test", { hello: "world" }, 1); 46 const parsed = JSON.parse(packed); 47 expect(parsed.type).toBe("test"); 48 expect(parsed.id).toBe(1); 49 expect(JSON.parse(parsed.content)).toEqual({ hello: "world" }); 50 }); 51 52 it("should handle string content", () => { 53 const packed = chatManager.pack("test", "hello", 1); 54 const parsed = JSON.parse(packed); 55 expect(parsed.content).toBe("hello"); 56 }); 57 }); 58 59 describe("Chat Status", () => { 60 it("should return status for all instances", () => { 61 const status = chatManager.getStatus(); 62 expect(Array.isArray(status)).toBe(true); 63 expect(status.length).toBe(3); 64 65 const names = status.map(s => s.name); 66 expect(names).toContain("chat-system"); 67 expect(names).toContain("chat-clock"); 68 expect(names).toContain("chat-sotce"); 69 }); 70 71 it("should return status for specific host", () => { 72 const status = chatManager.getStatus("chat-system.aesthetic.computer"); 73 expect(status).toBeDefined(); 74 expect(status.name).toBe("chat-system"); 75 expect(status.connections).toBe(0); 76 expect(status.messages).toBe(0); 77 }); 78 }); 79}); 80 81describe("Filter Integration", () => { 82 it("should filter profanity", async () => { 83 const { filter } = await import("../filter.mjs"); 84 85 // Clean text should pass through 86 expect(filter("hello world")).toBe("hello world"); 87 88 // Profanity should be filtered (replaced with underscores) 89 // Note: actual profanity filtering is tested in the obscenity library 90 }); 91}); 92 93describe("Redact Module", () => { 94 it("should redact message text", async () => { 95 const { redact, unredact } = await import("../redact.mjs"); 96 97 const msg = { text: "hello world", from: "@user" }; 98 redact(msg); 99 100 expect(msg.text).toBe("_____ _____"); 101 expect(msg.redactedText).toBe("hello world"); 102 }); 103 104 it("should unredact message text", async () => { 105 const { redact, unredact } = await import("../redact.mjs"); 106 107 const msg = { text: "hello world", from: "@user" }; 108 redact(msg); 109 unredact(msg); 110 111 expect(msg.text).toBe("hello world"); 112 }); 113});