|
| 1 | +/** |
| 2 | + * HTTP-level tests for the steering side-channel and abort queue restore. |
| 3 | + * The session registry is mocked so no real Pi session (auth, model) is |
| 4 | + * needed; the fake exposes exactly the surface the routes touch. |
| 5 | + */ |
| 6 | +import { afterAll, beforeEach, describe, expect, it, vi } from "vitest"; |
| 7 | +import fs from "node:fs"; |
| 8 | + |
| 9 | +const fakeSessions = new Map<string, FakeSession>(); |
| 10 | + |
| 11 | +class FakeSession { |
| 12 | + isStreaming = true; |
| 13 | + steered: string[] = []; |
| 14 | + aborted = false; |
| 15 | + clearQueueCalls = 0; |
| 16 | + calls: string[] = []; |
| 17 | + /** Called by steer(); lets a test flip isStreaming mid-call. */ |
| 18 | + onSteer: (() => void) | null = null; |
| 19 | + |
| 20 | + async steer(text: string): Promise<void> { |
| 21 | + this.calls.push("steer"); |
| 22 | + this.steered.push(text); |
| 23 | + this.onSteer?.(); |
| 24 | + } |
| 25 | + getSteeringMessages(): readonly string[] { |
| 26 | + return this.steered; |
| 27 | + } |
| 28 | + clearQueue(): { steering: string[]; followUp: string[] } { |
| 29 | + this.calls.push("clearQueue"); |
| 30 | + this.clearQueueCalls += 1; |
| 31 | + const steering = [...this.steered]; |
| 32 | + this.steered = []; |
| 33 | + return { steering, followUp: [] }; |
| 34 | + } |
| 35 | + async abort(): Promise<void> { |
| 36 | + this.calls.push("abort"); |
| 37 | + this.aborted = true; |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +vi.mock("../src/agent/session-registry.ts", () => ({ |
| 42 | + getAuthStorage: vi.fn(), |
| 43 | + getModelRegistry: vi.fn(), |
| 44 | + createSession: vi.fn(), |
| 45 | + getSession: vi.fn(async (_projectId: string, _paths: unknown, id: string) => |
| 46 | + fakeSessions.get(id) ?? null, |
| 47 | + ), |
| 48 | + listSessions: vi.fn(async () => []), |
| 49 | + disposeSession: vi.fn(), |
| 50 | +})); |
| 51 | + |
| 52 | +import { buildApp } from "../src/index.ts"; |
| 53 | +import { PROJECTS_ROOT } from "../src/config.ts"; |
| 54 | +import { createProject } from "../src/projects.ts"; |
| 55 | +import { recordRun } from "../src/cost/ledger.ts"; |
| 56 | + |
| 57 | +const app = await buildApp(); |
| 58 | + |
| 59 | +beforeEach(() => { |
| 60 | + fakeSessions.clear(); |
| 61 | + fs.rmSync(PROJECTS_ROOT, { recursive: true, force: true }); |
| 62 | + fs.mkdirSync(PROJECTS_ROOT, { recursive: true }); |
| 63 | +}); |
| 64 | + |
| 65 | +afterAll(async () => { |
| 66 | + await app.close(); |
| 67 | + fs.rmSync(PROJECTS_ROOT, { recursive: true, force: true }); |
| 68 | +}); |
| 69 | + |
| 70 | +function steer(id: string, body: unknown, projectId = "default") { |
| 71 | + return app.inject({ |
| 72 | + method: "POST", |
| 73 | + url: `/sessions/${id}/steer`, |
| 74 | + headers: { "x-project-id": projectId, "content-type": "application/json" }, |
| 75 | + payload: body as Record<string, unknown>, |
| 76 | + }); |
| 77 | +} |
| 78 | + |
| 79 | +describe("POST /sessions/:id/abort", () => { |
| 80 | + it("clears the queue before aborting and returns the texts", async () => { |
| 81 | + const s = new FakeSession(); |
| 82 | + await s.steer("pending steer"); |
| 83 | + fakeSessions.set("s1", s); |
| 84 | + const res = await app.inject({ |
| 85 | + method: "POST", |
| 86 | + url: "/sessions/s1/abort", |
| 87 | + headers: { "x-project-id": "default" }, |
| 88 | + }); |
| 89 | + expect(res.statusCode).toBe(200); |
| 90 | + expect(res.json()).toEqual({ ok: true, restored: ["pending steer"] }); |
| 91 | + expect(s.aborted).toBe(true); |
| 92 | + expect(s.clearQueueCalls).toBe(1); |
| 93 | + expect(s.calls).toEqual(["steer", "clearQueue", "abort"]); |
| 94 | + }); |
| 95 | + |
| 96 | + it("returns ok with empty restored for an unknown session", async () => { |
| 97 | + const res = await app.inject({ |
| 98 | + method: "POST", |
| 99 | + url: "/sessions/nope/abort", |
| 100 | + headers: { "x-project-id": "default" }, |
| 101 | + }); |
| 102 | + expect(res.statusCode).toBe(200); |
| 103 | + expect(res.json()).toEqual({ ok: true, restored: [] }); |
| 104 | + }); |
| 105 | +}); |
| 106 | + |
| 107 | +describe("POST /sessions/:id/steer", () => { |
| 108 | + it("404s for an unknown session", async () => { |
| 109 | + const res = await steer("nope", { message: "hi" }); |
| 110 | + expect(res.statusCode).toBe(404); |
| 111 | + }); |
| 112 | + |
| 113 | + it("400s for an empty message", async () => { |
| 114 | + fakeSessions.set("s1", new FakeSession()); |
| 115 | + const res = await steer("s1", { message: " " }); |
| 116 | + expect(res.statusCode).toBe(400); |
| 117 | + }); |
| 118 | + |
| 119 | + it("409s with reason not_streaming when no run is live", async () => { |
| 120 | + const s = new FakeSession(); |
| 121 | + s.isStreaming = false; |
| 122 | + fakeSessions.set("s1", s); |
| 123 | + const res = await steer("s1", { message: "hi" }); |
| 124 | + expect(res.statusCode).toBe(409); |
| 125 | + expect(res.json()).toMatchObject({ reason: "not_streaming" }); |
| 126 | + expect(s.steered).toEqual([]); |
| 127 | + }); |
| 128 | + |
| 129 | + it("queues the message and returns the pending list", async () => { |
| 130 | + const s = new FakeSession(); |
| 131 | + fakeSessions.set("s1", s); |
| 132 | + const res = await steer("s1", { message: "exclude sample 7" }); |
| 133 | + expect(res.statusCode).toBe(200); |
| 134 | + expect(res.json()).toEqual({ ok: true, pending: ["exclude sample 7"] }); |
| 135 | + expect(s.steered).toEqual(["exclude sample 7"]); |
| 136 | + }); |
| 137 | + |
| 138 | + it("409s and clears the queue when the run ends while queueing", async () => { |
| 139 | + const s = new FakeSession(); |
| 140 | + s.onSteer = () => { |
| 141 | + s.isStreaming = false; |
| 142 | + }; |
| 143 | + fakeSessions.set("s1", s); |
| 144 | + const res = await steer("s1", { message: "too late" }); |
| 145 | + expect(res.statusCode).toBe(409); |
| 146 | + expect(res.json()).toMatchObject({ reason: "not_streaming" }); |
| 147 | + // The stale steer must not leak into the next run. |
| 148 | + expect(s.clearQueueCalls).toBe(1); |
| 149 | + expect(s.steered).toEqual([]); |
| 150 | + }); |
| 151 | + |
| 152 | + it("403s with reason budget when the project cap is reached", async () => { |
| 153 | + const p = createProject({ name: "Capped", spendLimitUsd: 0.01 }); |
| 154 | + const zero = { costUsd: 0, input: 0, output: 0, cacheRead: 0, total: 0 }; |
| 155 | + recordRun({ |
| 156 | + sessionId: "s1", |
| 157 | + projectId: p.id, |
| 158 | + model: "m", |
| 159 | + before: zero, |
| 160 | + after: { costUsd: 0.02, input: 10, output: 10, cacheRead: 0, total: 20 }, |
| 161 | + }); |
| 162 | + fakeSessions.set("s1", new FakeSession()); |
| 163 | + const res = await steer("s1", { message: "hi" }, p.id); |
| 164 | + expect(res.statusCode).toBe(403); |
| 165 | + expect(res.json()).toMatchObject({ reason: "budget" }); |
| 166 | + }); |
| 167 | +}); |
0 commit comments