termchat: 极简双人终端聊天 (Node + TypeScript + Ink)

This commit is contained in:
2026-09-30 15:27:06 +08:00
commit c6b52e2574
9 changed files with 1541 additions and 0 deletions
+35
View File
@@ -0,0 +1,35 @@
/** 不依赖 UI 的通信层冒烟测试:本机起 server + client,验证双向收发 */
import { createSession, type ChatMessage } from "../src/net.js";
const port = 19321;
const aGot: ChatMessage[] = [];
const bGot: ChatMessage[] = [];
const a = createSession("server", { port, name: "A" }, {
onMessage: (m) => aGot.push(m),
onPhase: () => {},
});
const b = createSession("client", { host: "127.0.0.1", port, name: "B" }, {
onMessage: (m) => bGot.push(m),
onPhase: () => {},
});
setTimeout(() => {
b.send("你好,A!");
a.send("收到,B");
}, 300);
setTimeout(() => {
const texts = (arr: ChatMessage[]) =>
arr.filter((m) => m.type === "msg").map((m) => `${m.name}:${m.text}`);
const aMsgs = texts(aGot);
const bMsgs = texts(bGot);
console.log("A 收到:", aMsgs);
console.log("B 收到:", bMsgs);
const ok = aMsgs.includes("B:你好,A!") && bMsgs.includes("A:收到,B");
console.log(ok ? "✅ 双向通信正常" : "❌ 测试失败");
a.close();
b.close();
process.exit(ok ? 0 : 1);
}, 1200);