fix(webui): derive temporary chats from session policy

This commit is contained in:
chengyongru
2026-08-08 23:20:59 +08:00
committed by Xubin Ren
parent a5bc3bfbb9
commit 75e333a3c5
13 changed files with 647 additions and 185 deletions
+55 -8
View File
@@ -71,19 +71,25 @@ afterEach(() => {
});
describe("NanobotClient", () => {
it("keeps temporary chats out of attachment and reconnect state", () => {
it("keeps temporary chats out of attachment and reconnect state", async () => {
const client = new NanobotClient({
url: "ws://test",
reconnect: false,
socketFactory: (url) => new FakeSocket(url) as unknown as WebSocket,
});
const chatId = "temporary-test";
const chatId = "temp-server-id";
client.connect();
lastSocket().fakeOpen();
const creation = client.newTemporaryChat();
expect(JSON.parse(lastSocket().sent.at(-1) as string)).toEqual({
type: "new_temporary_chat",
});
lastSocket().fakeMessage({ event: "attached", chat_id: chatId, temporary: true });
await expect(creation).resolves.toBe(chatId);
lastSocket().sent = [];
client.onChat(chatId, vi.fn());
client.sendMessage(chatId, "hello", undefined, { turnId: "turn-1" });
lastSocket().fakeOpen();
expect(lastSocket().sent.map((raw) => JSON.parse(raw))).toEqual([
{
type: "message",
@@ -101,6 +107,30 @@ describe("NanobotClient", () => {
});
});
it("waits for the temporary attachment when creating a temporary chat", async () => {
const client = new NanobotClient({
url: "ws://test",
reconnect: false,
socketFactory: (url) => new FakeSocket(url) as unknown as WebSocket,
});
client.connect();
lastSocket().fakeOpen();
const creation = client.newTemporaryChat();
let resolved = false;
void creation.then(() => { resolved = true; });
lastSocket().fakeMessage({ event: "attached", chat_id: "ordinary-chat" });
await Promise.resolve();
expect(resolved).toBe(false);
lastSocket().fakeMessage({
event: "attached",
chat_id: "server-temporary-chat",
temporary: true,
});
await expect(creation).resolves.toBe("server-temporary-chat");
});
it("forgets every temporary chat when the socket drops", async () => {
const client = new NanobotClient({
url: "ws://test",
@@ -112,20 +142,37 @@ describe("NanobotClient", () => {
const secondHandler = vi.fn();
client.connect();
lastSocket().fakeOpen();
client.onChat("temporary-drop-a", firstHandler);
client.onChat("temporary-drop-b", secondHandler);
const firstCreation = client.newTemporaryChat();
lastSocket().fakeMessage({
event: "attached",
chat_id: "temp-drop-a",
temporary: true,
});
await firstCreation;
const secondCreation = client.newTemporaryChat();
lastSocket().fakeMessage({
event: "attached",
chat_id: "temp-drop-b",
temporary: true,
});
await secondCreation;
lastSocket().sent = [];
client.onChat("temp-drop-a", firstHandler);
client.onChat("temp-drop-b", secondHandler);
firstHandler.mockClear();
secondHandler.mockClear();
lastSocket().close();
await vi.advanceTimersByTimeAsync(1);
lastSocket().fakeOpen();
lastSocket().fakeMessage({
event: "message",
chat_id: "temporary-drop-a",
chat_id: "temp-drop-a",
text: "stale first chat",
});
lastSocket().fakeMessage({
event: "message",
chat_id: "temporary-drop-b",
chat_id: "temp-drop-b",
text: "stale second chat",
});