|
| 1 | +import { test } from "worker-testbed"; |
| 2 | + |
| 3 | +import { |
| 4 | + addAgent, |
| 5 | + addCompletion, |
| 6 | + addEmbedding, |
| 7 | + addMCP, |
| 8 | + addState, |
| 9 | + addStorage, |
| 10 | + addSwarm, |
| 11 | + makeAutoDispose, |
| 12 | + session, |
| 13 | + setConfig, |
| 14 | + Chat, |
| 15 | + State, |
| 16 | + Storage, |
| 17 | +} from "../../build/index.mjs"; |
| 18 | +import { randomString, sleep } from "functools-kit"; |
| 19 | + |
| 20 | +const HANG = Symbol("hang"); |
| 21 | +const raceHang = (p, ms = 8000) => Promise.race([p, sleep(ms).then(() => HANG)]); |
| 22 | + |
| 23 | +const trackUnhandled = () => { |
| 24 | + const unhandled = []; |
| 25 | + process.on("unhandledRejection", (reason) => { |
| 26 | + unhandled.push(String(reason?.message ?? reason)); |
| 27 | + }); |
| 28 | + return unhandled; |
| 29 | +}; |
| 30 | + |
| 31 | +const addEcho = (name) => |
| 32 | + addCompletion({ |
| 33 | + completionName: name, |
| 34 | + getCompletion: async ({ agentName, messages }) => { |
| 35 | + const [last] = messages.slice(-1); |
| 36 | + return { agentName, content: `echo:${last.content}`, role: "assistant" }; |
| 37 | + }, |
| 38 | + }); |
| 39 | + |
| 40 | +test("Will reject without hang when MCP listTools throws", async ({ pass, fail }) => { |
| 41 | + setConfig({ CC_PERSIST_ENABLED_BY_DEFAULT: false }); |
| 42 | + const unhandled = trackUnhandled(); |
| 43 | + |
| 44 | + const MOCK_COMPLETION = addEcho("mock-completion"); |
| 45 | + const TEST_MCP = addMCP({ |
| 46 | + mcpName: "test-mcp", |
| 47 | + listTools: async () => { |
| 48 | + throw new Error("listTools exploded"); |
| 49 | + }, |
| 50 | + callTool: async () => {}, |
| 51 | + }); |
| 52 | + const TEST_AGENT = addAgent({ agentName: "test-agent", completion: MOCK_COMPLETION, prompt: "", mcp: [TEST_MCP] }); |
| 53 | + const TEST_SWARM = addSwarm({ swarmName: "test-swarm", agentList: [TEST_AGENT], defaultAgent: TEST_AGENT }); |
| 54 | + |
| 55 | + const chatSession = session(randomString(), TEST_SWARM); |
| 56 | + let out; |
| 57 | + try { |
| 58 | + out = await raceHang(chatSession.complete("hi")); |
| 59 | + } catch (error) { |
| 60 | + out = `THREW:${error.message}`; |
| 61 | + } |
| 62 | + await raceHang(chatSession.dispose()); |
| 63 | + await sleep(100); |
| 64 | + |
| 65 | + if (String(out).startsWith("THREW:listTools exploded") && unhandled.length === 0) { |
| 66 | + pass(); |
| 67 | + return; |
| 68 | + } |
| 69 | + fail(`out=${String(out)} unhandled=${JSON.stringify(unhandled)}`); |
| 70 | +}); |
| 71 | + |
| 72 | +test("Will keep storage queue alive after throwing createIndex", async ({ pass, fail }) => { |
| 73 | + setConfig({ CC_PERSIST_ENABLED_BY_DEFAULT: false }); |
| 74 | + const unhandled = trackUnhandled(); |
| 75 | + |
| 76 | + addEcho("mock-completion"); |
| 77 | + addEmbedding({ |
| 78 | + embeddingName: "test-embedding", |
| 79 | + createEmbedding: async (t) => [t.length], |
| 80 | + calculateSimilarity: async (a, b) => (a[0] === b[0] ? 1 : 0), |
| 81 | + }); |
| 82 | + let boom = true; |
| 83 | + addStorage({ |
| 84 | + storageName: "test-storage", |
| 85 | + embedding: "test-embedding", |
| 86 | + createIndex: (i) => { |
| 87 | + if (boom) throw new Error("createIndex exploded"); |
| 88 | + return i.text; |
| 89 | + }, |
| 90 | + }); |
| 91 | + const TEST_AGENT = addAgent({ |
| 92 | + agentName: "test-agent", |
| 93 | + completion: "mock-completion", |
| 94 | + prompt: "", |
| 95 | + storages: ["test-storage"], |
| 96 | + }); |
| 97 | + const TEST_SWARM = addSwarm({ swarmName: "test-swarm", agentList: [TEST_AGENT], defaultAgent: TEST_AGENT }); |
| 98 | + |
| 99 | + const CLIENT_ID = randomString(); |
| 100 | + const chatSession = session(CLIENT_ID, TEST_SWARM); |
| 101 | + const base = { clientId: CLIENT_ID, agentName: TEST_AGENT, storageName: "test-storage" }; |
| 102 | + let first = ""; |
| 103 | + try { |
| 104 | + await raceHang(Storage.upsert({ ...base, item: { id: 1, text: "x" } })); |
| 105 | + } catch (error) { |
| 106 | + first = error.message; |
| 107 | + } |
| 108 | + boom = false; |
| 109 | + const second = await raceHang(Storage.upsert({ ...base, item: { id: 2, text: "y" } })); |
| 110 | + const items = await raceHang(Storage.list(base)); |
| 111 | + await chatSession.dispose(); |
| 112 | + await sleep(100); |
| 113 | + |
| 114 | + const ok = |
| 115 | + first.includes("createIndex exploded") && |
| 116 | + second !== HANG && |
| 117 | + items !== HANG && |
| 118 | + items.some((i) => i.id === 2) && |
| 119 | + unhandled.length === 0; |
| 120 | + if (ok) { |
| 121 | + pass(); |
| 122 | + return; |
| 123 | + } |
| 124 | + fail(`first=${first} items=${JSON.stringify(items)} unhandled=${JSON.stringify(unhandled)}`); |
| 125 | +}); |
| 126 | + |
| 127 | +test("Will keep state queue alive after throwing dispatch", async ({ pass, fail }) => { |
| 128 | + setConfig({ CC_PERSIST_ENABLED_BY_DEFAULT: false }); |
| 129 | + const unhandled = trackUnhandled(); |
| 130 | + |
| 131 | + addEcho("mock-completion"); |
| 132 | + addState({ stateName: "test-state", getDefaultState: () => ({ v: 0 }) }); |
| 133 | + const TEST_AGENT = addAgent({ |
| 134 | + agentName: "test-agent", |
| 135 | + completion: "mock-completion", |
| 136 | + prompt: "", |
| 137 | + states: ["test-state"], |
| 138 | + }); |
| 139 | + const TEST_SWARM = addSwarm({ swarmName: "test-swarm", agentList: [TEST_AGENT], defaultAgent: TEST_AGENT }); |
| 140 | + |
| 141 | + const CLIENT_ID = randomString(); |
| 142 | + const chatSession = session(CLIENT_ID, TEST_SWARM); |
| 143 | + const context = { clientId: CLIENT_ID, agentName: TEST_AGENT, stateName: "test-state" }; |
| 144 | + let first = ""; |
| 145 | + try { |
| 146 | + await raceHang( |
| 147 | + State.setState(() => { |
| 148 | + throw new Error("dispatch exploded"); |
| 149 | + }, context) |
| 150 | + ); |
| 151 | + } catch (error) { |
| 152 | + first = error.message; |
| 153 | + } |
| 154 | + const second = await raceHang(State.setState(() => ({ v: 7 }), context)); |
| 155 | + const got = await raceHang(State.getState(context)); |
| 156 | + await chatSession.dispose(); |
| 157 | + await sleep(100); |
| 158 | + |
| 159 | + const ok = |
| 160 | + first.includes("dispatch exploded") && |
| 161 | + second !== HANG && |
| 162 | + got !== HANG && |
| 163 | + got.v === 7 && |
| 164 | + unhandled.length === 0; |
| 165 | + if (ok) { |
| 166 | + pass(); |
| 167 | + return; |
| 168 | + } |
| 169 | + fail(`first=${first} got=${JSON.stringify(got)} unhandled=${JSON.stringify(unhandled)}`); |
| 170 | +}); |
| 171 | + |
| 172 | +test("Will deliver rejection from throwing calculateSimilarity in take", async ({ pass, fail }) => { |
| 173 | + setConfig({ CC_PERSIST_ENABLED_BY_DEFAULT: false }); |
| 174 | + const unhandled = trackUnhandled(); |
| 175 | + |
| 176 | + addEcho("mock-completion"); |
| 177 | + addEmbedding({ |
| 178 | + embeddingName: "test-embedding", |
| 179 | + createEmbedding: async (t) => [t.length], |
| 180 | + calculateSimilarity: async () => { |
| 181 | + throw new Error("similarity exploded"); |
| 182 | + }, |
| 183 | + }); |
| 184 | + addStorage({ storageName: "test-storage", embedding: "test-embedding", createIndex: (i) => i.text }); |
| 185 | + const TEST_AGENT = addAgent({ |
| 186 | + agentName: "test-agent", |
| 187 | + completion: "mock-completion", |
| 188 | + prompt: "", |
| 189 | + storages: ["test-storage"], |
| 190 | + }); |
| 191 | + const TEST_SWARM = addSwarm({ swarmName: "test-swarm", agentList: [TEST_AGENT], defaultAgent: TEST_AGENT }); |
| 192 | + |
| 193 | + const CLIENT_ID = randomString(); |
| 194 | + const chatSession = session(CLIENT_ID, TEST_SWARM); |
| 195 | + const base = { clientId: CLIENT_ID, agentName: TEST_AGENT, storageName: "test-storage" }; |
| 196 | + await Storage.upsert({ ...base, item: { id: 1, text: "aa" } }); |
| 197 | + let takeError = ""; |
| 198 | + try { |
| 199 | + await raceHang(Storage.take({ ...base, search: "aa", total: 5, score: 0.1 })); |
| 200 | + } catch (error) { |
| 201 | + takeError = error.message; |
| 202 | + } |
| 203 | + await chatSession.dispose(); |
| 204 | + await sleep(100); |
| 205 | + |
| 206 | + if (takeError.includes("similarity exploded") && unhandled.length === 0) { |
| 207 | + pass(); |
| 208 | + return; |
| 209 | + } |
| 210 | + fail(`takeError=${takeError} unhandled=${JSON.stringify(unhandled)}`); |
| 211 | +}); |
| 212 | + |
| 213 | +test("Will survive throwing onDestroy in makeAutoDispose timer", async ({ pass, fail }) => { |
| 214 | + setConfig({ CC_PERSIST_ENABLED_BY_DEFAULT: false }); |
| 215 | + const unhandled = trackUnhandled(); |
| 216 | + |
| 217 | + const MOCK_COMPLETION = addEcho("mock-completion"); |
| 218 | + const TEST_AGENT = addAgent({ agentName: "test-agent", completion: MOCK_COMPLETION, prompt: "" }); |
| 219 | + const TEST_SWARM = addSwarm({ swarmName: "test-swarm", agentList: [TEST_AGENT], defaultAgent: TEST_AGENT }); |
| 220 | + |
| 221 | + const CLIENT_ID = randomString(); |
| 222 | + session(CLIENT_ID, TEST_SWARM); |
| 223 | + const { tick } = makeAutoDispose(CLIENT_ID, TEST_SWARM, { |
| 224 | + timeoutSeconds: 1, |
| 225 | + onDestroy: () => { |
| 226 | + throw new Error("onDestroy exploded"); |
| 227 | + }, |
| 228 | + }); |
| 229 | + tick(); |
| 230 | + await sleep(2_500); |
| 231 | + |
| 232 | + if (unhandled.length === 0) { |
| 233 | + pass(); |
| 234 | + return; |
| 235 | + } |
| 236 | + fail(`unhandled=${JSON.stringify(unhandled)}`); |
| 237 | +}); |
| 238 | + |
| 239 | +test("Will survive throwing chat onDispose in cleanup interval", async ({ pass, fail }) => { |
| 240 | + setConfig({ |
| 241 | + CC_PERSIST_ENABLED_BY_DEFAULT: false, |
| 242 | + CC_CHAT_INACTIVITY_CHECK: 100, |
| 243 | + CC_CHAT_INACTIVITY_TIMEOUT: 300, |
| 244 | + }); |
| 245 | + const unhandled = trackUnhandled(); |
| 246 | + |
| 247 | + Chat.useChatCallbacks({ |
| 248 | + onDispose: () => { |
| 249 | + throw new Error("chat onDispose exploded"); |
| 250 | + }, |
| 251 | + }); |
| 252 | + |
| 253 | + const MOCK_COMPLETION = addEcho("mock-completion"); |
| 254 | + const TEST_AGENT = addAgent({ agentName: "test-agent", completion: MOCK_COMPLETION, prompt: "" }); |
| 255 | + const TEST_SWARM = addSwarm({ swarmName: "test-swarm", agentList: [TEST_AGENT], defaultAgent: TEST_AGENT }); |
| 256 | + |
| 257 | + const CLIENT_ID = randomString(); |
| 258 | + await Chat.beginChat(CLIENT_ID, TEST_SWARM); |
| 259 | + const answer = await Chat.sendMessage(CLIENT_ID, "ping", TEST_SWARM); |
| 260 | + await sleep(1_000); |
| 261 | + |
| 262 | + if (answer === "echo:ping" && unhandled.length === 0) { |
| 263 | + pass(); |
| 264 | + return; |
| 265 | + } |
| 266 | + fail(`answer=${String(answer)} unhandled=${JSON.stringify(unhandled)}`); |
| 267 | +}); |
0 commit comments