|
| 1 | +import type { D1Database } from "@cloudflare/workers-types"; |
| 2 | +import { describe, expect, it, vi } from "vitest"; |
| 3 | +import { D1MemoryAdapter } from "./memory-adapter"; |
| 4 | + |
| 5 | +function createMockBinding(): D1Database { |
| 6 | + return { |
| 7 | + prepare: vi.fn(() => ({ |
| 8 | + bind: vi.fn().mockReturnThis(), |
| 9 | + run: vi.fn().mockResolvedValue({}), |
| 10 | + all: vi.fn().mockResolvedValue({ results: [] }), |
| 11 | + })), |
| 12 | + batch: vi.fn().mockResolvedValue([]), |
| 13 | + } as unknown as D1Database; |
| 14 | +} |
| 15 | + |
| 16 | +describe("D1MemoryAdapter queryWorkflowRuns", () => { |
| 17 | + it("builds metadata filters with JSON-aware comparisons", async () => { |
| 18 | + vi.spyOn(D1MemoryAdapter.prototype as any, "ensureInitialized").mockResolvedValue(undefined); |
| 19 | + |
| 20 | + const adapter = new D1MemoryAdapter({ |
| 21 | + binding: createMockBinding(), |
| 22 | + tablePrefix: "test", |
| 23 | + }); |
| 24 | + |
| 25 | + const allSpy = vi.spyOn(adapter as any, "all").mockResolvedValue([ |
| 26 | + { |
| 27 | + id: "exec-1", |
| 28 | + workflow_id: "workflow-1", |
| 29 | + workflow_name: "Workflow 1", |
| 30 | + status: "completed", |
| 31 | + input: '{"requestId":"req-1"}', |
| 32 | + context: '[["tenantId","acme"]]', |
| 33 | + workflow_state: '{"phase":"done"}', |
| 34 | + suspension: null, |
| 35 | + events: null, |
| 36 | + output: null, |
| 37 | + cancellation: null, |
| 38 | + user_id: "user-1", |
| 39 | + conversation_id: null, |
| 40 | + metadata: '{"tenantId":"acme"}', |
| 41 | + created_at: "2024-01-02T00:00:00Z", |
| 42 | + updated_at: "2024-01-02T00:00:00Z", |
| 43 | + }, |
| 44 | + ]); |
| 45 | + |
| 46 | + const result = await adapter.queryWorkflowRuns({ |
| 47 | + workflowId: "workflow-1", |
| 48 | + status: "completed", |
| 49 | + from: new Date("2024-01-01T00:00:00Z"), |
| 50 | + to: new Date("2024-01-03T00:00:00Z"), |
| 51 | + userId: "user-1", |
| 52 | + metadata: { tenantId: "acme" }, |
| 53 | + limit: 5, |
| 54 | + offset: 2, |
| 55 | + }); |
| 56 | + |
| 57 | + expect(result).toHaveLength(1); |
| 58 | + expect(result[0]?.input).toEqual({ requestId: "req-1" }); |
| 59 | + expect(result[0]?.context).toEqual([["tenantId", "acme"]]); |
| 60 | + expect(result[0]?.workflowState).toEqual({ phase: "done" }); |
| 61 | + |
| 62 | + expect(allSpy).toHaveBeenCalledTimes(1); |
| 63 | + const [sql, args] = allSpy.mock.calls[0]; |
| 64 | + |
| 65 | + expect(sql).toContain("workflow_id = ?"); |
| 66 | + expect(sql).toContain("status = ?"); |
| 67 | + expect(sql).toContain("created_at >="); |
| 68 | + expect(sql).toContain("user_id = ?"); |
| 69 | + expect(sql).toContain("json_extract(metadata, ?) = json_extract(json(?), '$')"); |
| 70 | + expect(sql).toContain("ORDER BY created_at DESC"); |
| 71 | + expect(args).toEqual([ |
| 72 | + "workflow-1", |
| 73 | + "completed", |
| 74 | + "2024-01-01T00:00:00.000Z", |
| 75 | + "2024-01-03T00:00:00.000Z", |
| 76 | + "user-1", |
| 77 | + '$."tenantId"', |
| 78 | + '"acme"', |
| 79 | + 5, |
| 80 | + 2, |
| 81 | + ]); |
| 82 | + }); |
| 83 | +}); |
0 commit comments