|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import { initSession, seedEvents } from "./helpers"; |
| 3 | + |
| 4 | +describe("GET /internal/events", () => { |
| 5 | + it("lists events with default pagination", async () => { |
| 6 | + const { stub } = await initSession(); |
| 7 | + const baseTime = Date.now(); |
| 8 | + |
| 9 | + await seedEvents( |
| 10 | + stub, |
| 11 | + Array.from({ length: 5 }, (_, i) => ({ |
| 12 | + id: `evt-list-${i}`, |
| 13 | + type: "tool_call", |
| 14 | + data: JSON.stringify({ type: "tool_call", tool: "read_file", callId: `c-${i}` }), |
| 15 | + createdAt: baseTime + i, |
| 16 | + })) |
| 17 | + ); |
| 18 | + |
| 19 | + const res = await stub.fetch("http://internal/internal/events?type=tool_call"); |
| 20 | + expect(res.status).toBe(200); |
| 21 | + |
| 22 | + const body = await res.json<{ |
| 23 | + events: Array<{ id: string; type: string }>; |
| 24 | + hasMore: boolean; |
| 25 | + }>(); |
| 26 | + |
| 27 | + const seeded = body.events.filter((e) => e.id.startsWith("evt-list-")); |
| 28 | + expect(seeded).toHaveLength(5); |
| 29 | + expect(body.hasMore).toBe(false); |
| 30 | + }); |
| 31 | + |
| 32 | + it("respects limit parameter", async () => { |
| 33 | + const { stub } = await initSession(); |
| 34 | + const baseTime = Date.now(); |
| 35 | + |
| 36 | + await seedEvents( |
| 37 | + stub, |
| 38 | + Array.from({ length: 10 }, (_, i) => ({ |
| 39 | + id: `evt-lim-${i}`, |
| 40 | + type: "tool_result", |
| 41 | + data: JSON.stringify({ type: "tool_result", callId: `c-${i}`, result: "ok" }), |
| 42 | + createdAt: baseTime + i, |
| 43 | + })) |
| 44 | + ); |
| 45 | + |
| 46 | + const res = await stub.fetch("http://internal/internal/events?type=tool_result&limit=3"); |
| 47 | + expect(res.status).toBe(200); |
| 48 | + |
| 49 | + const body = await res.json<{ |
| 50 | + events: Array<{ id: string }>; |
| 51 | + hasMore: boolean; |
| 52 | + cursor: string; |
| 53 | + }>(); |
| 54 | + |
| 55 | + expect(body.events).toHaveLength(3); |
| 56 | + expect(body.hasMore).toBe(true); |
| 57 | + expect(body.cursor).toBeDefined(); |
| 58 | + }); |
| 59 | + |
| 60 | + it("cursor pagination returns next page without overlap", async () => { |
| 61 | + const { stub } = await initSession(); |
| 62 | + const baseTime = Date.now(); |
| 63 | + |
| 64 | + await seedEvents( |
| 65 | + stub, |
| 66 | + Array.from({ length: 7 }, (_, i) => ({ |
| 67 | + id: `evt-page-${i}`, |
| 68 | + type: "error", |
| 69 | + data: JSON.stringify({ type: "error", message: `error-${i}` }), |
| 70 | + createdAt: baseTime + i, |
| 71 | + })) |
| 72 | + ); |
| 73 | + |
| 74 | + // Page 1 |
| 75 | + const res1 = await stub.fetch("http://internal/internal/events?type=error&limit=3"); |
| 76 | + const page1 = await res1.json<{ |
| 77 | + events: Array<{ id: string }>; |
| 78 | + cursor: string; |
| 79 | + hasMore: boolean; |
| 80 | + }>(); |
| 81 | + expect(page1.events).toHaveLength(3); |
| 82 | + expect(page1.hasMore).toBe(true); |
| 83 | + |
| 84 | + // Page 2 |
| 85 | + const res2 = await stub.fetch( |
| 86 | + `http://internal/internal/events?type=error&limit=3&cursor=${page1.cursor}` |
| 87 | + ); |
| 88 | + const page2 = await res2.json<{ |
| 89 | + events: Array<{ id: string }>; |
| 90 | + hasMore: boolean; |
| 91 | + }>(); |
| 92 | + |
| 93 | + // No overlap between pages |
| 94 | + const page1Ids = new Set(page1.events.map((e) => e.id)); |
| 95 | + for (const event of page2.events) { |
| 96 | + expect(page1Ids.has(event.id)).toBe(false); |
| 97 | + } |
| 98 | + }); |
| 99 | + |
| 100 | + it("filters events by type", async () => { |
| 101 | + const { stub } = await initSession(); |
| 102 | + const baseTime = Date.now(); |
| 103 | + |
| 104 | + await seedEvents(stub, [ |
| 105 | + { |
| 106 | + id: "evt-filter-tc", |
| 107 | + type: "tool_call", |
| 108 | + data: JSON.stringify({ type: "tool_call", tool: "write_file" }), |
| 109 | + createdAt: baseTime, |
| 110 | + }, |
| 111 | + { |
| 112 | + id: "evt-filter-tr", |
| 113 | + type: "tool_result", |
| 114 | + data: JSON.stringify({ type: "tool_result", callId: "c1", result: "done" }), |
| 115 | + createdAt: baseTime + 1, |
| 116 | + }, |
| 117 | + { |
| 118 | + id: "evt-filter-tc2", |
| 119 | + type: "tool_call", |
| 120 | + data: JSON.stringify({ type: "tool_call", tool: "read_file" }), |
| 121 | + createdAt: baseTime + 2, |
| 122 | + }, |
| 123 | + ]); |
| 124 | + |
| 125 | + const res = await stub.fetch("http://internal/internal/events?type=tool_call"); |
| 126 | + const body = await res.json<{ events: Array<{ id: string; type: string }> }>(); |
| 127 | + |
| 128 | + const seeded = body.events.filter((e) => e.id.startsWith("evt-filter-tc")); |
| 129 | + expect(seeded).toHaveLength(2); |
| 130 | + for (const event of seeded) { |
| 131 | + expect(event.type).toBe("tool_call"); |
| 132 | + } |
| 133 | + }); |
| 134 | +}); |
| 135 | + |
| 136 | +describe("GET /internal/messages", () => { |
| 137 | + it("lists messages with status filter", async () => { |
| 138 | + const { stub } = await initSession(); |
| 139 | + |
| 140 | + // Enqueue two prompts |
| 141 | + const res1 = await stub.fetch("http://internal/internal/prompt", { |
| 142 | + method: "POST", |
| 143 | + headers: { "Content-Type": "application/json" }, |
| 144 | + body: JSON.stringify({ content: "First prompt", authorId: "user-1", source: "web" }), |
| 145 | + }); |
| 146 | + const { messageId: msgId1 } = await res1.json<{ messageId: string }>(); |
| 147 | + |
| 148 | + const res2 = await stub.fetch("http://internal/internal/prompt", { |
| 149 | + method: "POST", |
| 150 | + headers: { "Content-Type": "application/json" }, |
| 151 | + body: JSON.stringify({ content: "Second prompt", authorId: "user-1", source: "web" }), |
| 152 | + }); |
| 153 | + const { messageId: msgId2 } = await res2.json<{ messageId: string }>(); |
| 154 | + |
| 155 | + // Check that messages are listed |
| 156 | + const listRes = await stub.fetch("http://internal/internal/messages"); |
| 157 | + expect(listRes.status).toBe(200); |
| 158 | + |
| 159 | + const body = await listRes.json<{ |
| 160 | + messages: Array<{ id: string; content: string; status: string }>; |
| 161 | + hasMore: boolean; |
| 162 | + }>(); |
| 163 | + |
| 164 | + expect(body.messages.length).toBeGreaterThanOrEqual(2); |
| 165 | + const ids = body.messages.map((m) => m.id); |
| 166 | + expect(ids).toContain(msgId1); |
| 167 | + expect(ids).toContain(msgId2); |
| 168 | + }); |
| 169 | +}); |
0 commit comments