chat.test.ts (3073B)
1 import { describe, it, expect, beforeEach } from "vitest"; 2 import { useChatStore } from "./chat"; 3 4 beforeEach(() => { 5 useChatStore.setState({ 6 messages: [], 7 streaming: false, 8 }); 9 }); 10 11 describe("chat store", () => { 12 it("has initial empty state", () => { 13 const state = useChatStore.getState(); 14 expect(state.messages).toEqual([]); 15 expect(state.streaming).toBe(false); 16 }); 17 18 it("addMessage appends to messages", () => { 19 const { addMessage } = useChatStore.getState(); 20 addMessage({ 21 id: "m1", 22 role: "user", 23 content: "Hello", 24 toolCalls: [], 25 timestamp: Date.now(), 26 }); 27 28 const state = useChatStore.getState(); 29 expect(state.messages).toHaveLength(1); 30 expect(state.messages[0].content).toBe("Hello"); 31 }); 32 33 it("updateMessageContent updates text of a message", () => { 34 const { addMessage, updateMessageContent } = useChatStore.getState(); 35 addMessage({ 36 id: "m1", 37 role: "assistant", 38 content: "Hello", 39 toolCalls: [], 40 timestamp: Date.now(), 41 }); 42 43 updateMessageContent("m1", "Hello world"); 44 45 const state = useChatStore.getState(); 46 expect(state.messages[0].content).toBe("Hello world"); 47 }); 48 49 it("addToolCall adds to message's tool calls", () => { 50 const { addMessage, addToolCall } = useChatStore.getState(); 51 addMessage({ 52 id: "m1", 53 role: "assistant", 54 content: "", 55 toolCalls: [], 56 timestamp: Date.now(), 57 }); 58 59 addToolCall("m1", { 60 id: "tc1", 61 name: "search", 62 args: '{"query":"test"}', 63 result: null, 64 status: "pending", 65 }); 66 67 const state = useChatStore.getState(); 68 expect(state.messages[0].toolCalls).toHaveLength(1); 69 expect(state.messages[0].toolCalls[0].name).toBe("search"); 70 }); 71 72 it("updateToolCall updates tool call status", () => { 73 const { addMessage, addToolCall, updateToolCall } = useChatStore.getState(); 74 addMessage({ 75 id: "m1", 76 role: "assistant", 77 content: "", 78 toolCalls: [], 79 timestamp: Date.now(), 80 }); 81 addToolCall("m1", { 82 id: "tc1", 83 name: "search", 84 args: "{}", 85 result: null, 86 status: "running", 87 }); 88 89 updateToolCall("m1", "tc1", { status: "complete", result: "found it" }); 90 91 const state = useChatStore.getState(); 92 const tc = state.messages[0].toolCalls[0]; 93 expect(tc.status).toBe("complete"); 94 expect(tc.result).toBe("found it"); 95 }); 96 97 it("setStreaming toggles streaming state", () => { 98 const { setStreaming } = useChatStore.getState(); 99 setStreaming(true); 100 expect(useChatStore.getState().streaming).toBe(true); 101 102 setStreaming(false); 103 expect(useChatStore.getState().streaming).toBe(false); 104 }); 105 106 it("clearMessages resets messages", () => { 107 const { addMessage, clearMessages } = useChatStore.getState(); 108 addMessage({ 109 id: "m1", 110 role: "user", 111 content: "Hello", 112 toolCalls: [], 113 timestamp: Date.now(), 114 }); 115 116 clearMessages(); 117 expect(useChatStore.getState().messages).toEqual([]); 118 }); 119 });