|
1 | 1 | // Moonshot tests cover kimi web search provider plugin behavior. |
2 | 2 | import type { OpenClawConfig } from "openclaw/plugin-sdk/provider-onboard"; |
3 | | -import { withEnvAsync } from "openclaw/plugin-sdk/test-env"; |
| 3 | +import { withEnv, withEnvAsync } from "openclaw/plugin-sdk/test-env"; |
4 | 4 | import { afterEach, describe, expect, it, vi } from "vitest"; |
5 | 5 | import { testing } from "../test-api.js"; |
6 | 6 | import { createKimiWebSearchProvider } from "./kimi-web-search-provider.js"; |
7 | 7 |
|
8 | 8 | const kimiApiKeyEnv = ["KIMI_API", "KEY"].join("_"); |
9 | 9 |
|
10 | | -function withEnv(overrides: Record<string, string>, run: () => void): void { |
11 | | - const previous = new Map<string, string | undefined>(); |
12 | | - for (const [key, value] of Object.entries(overrides)) { |
13 | | - previous.set(key, process.env[key]); |
14 | | - process.env[key] = value; |
15 | | - } |
16 | | - try { |
17 | | - run(); |
18 | | - } finally { |
19 | | - for (const [key, value] of previous) { |
20 | | - if (value === undefined) { |
21 | | - delete process.env[key]; |
22 | | - } else { |
23 | | - process.env[key] = value; |
24 | | - } |
25 | | - } |
26 | | - } |
27 | | -} |
28 | | - |
29 | 10 | function jsonResponse(body: unknown): Response { |
30 | 11 | return new Response(JSON.stringify(body), { |
31 | 12 | status: 200, |
@@ -286,6 +267,42 @@ describe("kimi web search provider", () => { |
286 | 267 | ).toBeUndefined(); |
287 | 268 | }); |
288 | 269 |
|
| 270 | + it("forwards the execution abort signal to an in-flight Kimi search", async () => { |
| 271 | + const fetchMock = vi.fn( |
| 272 | + (_url: string, init?: RequestInit) => |
| 273 | + new Promise<Response>((_resolve, reject) => { |
| 274 | + init?.signal?.addEventListener( |
| 275 | + "abort", |
| 276 | + () => reject(new Error(String(init.signal?.reason ?? "Aborted"))), |
| 277 | + { |
| 278 | + once: true, |
| 279 | + }, |
| 280 | + ); |
| 281 | + }), |
| 282 | + ); |
| 283 | + vi.stubGlobal("fetch", fetchMock); |
| 284 | + |
| 285 | + await withEnvAsync({ KIMI_API_KEY: "kimi-test-key" }, async () => { |
| 286 | + const controller = new AbortController(); |
| 287 | + const tool = createKimiWebSearchProvider().createTool({ config: {}, searchConfig: {} }); |
| 288 | + if (!tool) { |
| 289 | + throw new Error("Expected tool definition"); |
| 290 | + } |
| 291 | + |
| 292 | + const search = tool.execute( |
| 293 | + { query: "unique Kimi abort regression" }, |
| 294 | + { |
| 295 | + signal: controller.signal, |
| 296 | + }, |
| 297 | + ); |
| 298 | + await vi.waitFor(() => expect(fetchMock).toHaveBeenCalledOnce()); |
| 299 | + controller.abort(new Error("Kimi search cancelled")); |
| 300 | + |
| 301 | + await expect(search).rejects.toThrow("Kimi search cancelled"); |
| 302 | + expect(fetchMock.mock.calls[0]?.[1]?.signal?.aborted).toBe(true); |
| 303 | + }); |
| 304 | + }); |
| 305 | + |
289 | 306 | it("uses config apiKey when provided", () => { |
290 | 307 | expect(testing.resolveKimiApiKey({ apiKey: "kimi-test-key" })).toBe("kimi-test-key"); |
291 | 308 | }); |
|
0 commit comments