|
| 1 | +// Control UI tests cover application-owned overlay races. |
| 2 | +import { describe, expect, it, vi } from "vitest"; |
| 3 | +import type { GatewayBrowserClient, GatewayEventFrame } from "../api/gateway.ts"; |
| 4 | +import type { ApplicationGateway, ApplicationGatewaySnapshot } from "./gateway.ts"; |
| 5 | +import { createApplicationOverlays } from "./overlays.ts"; |
| 6 | + |
| 7 | +type RequestFn = (method: string, params?: unknown) => Promise<unknown>; |
| 8 | + |
| 9 | +function deferred<T = unknown>() { |
| 10 | + let resolve!: (value: T | PromiseLike<T>) => void; |
| 11 | + let reject!: (reason?: unknown) => void; |
| 12 | + const promise = new Promise<T>((resolvePromise, rejectPromise) => { |
| 13 | + resolve = resolvePromise; |
| 14 | + reject = rejectPromise; |
| 15 | + }); |
| 16 | + return { promise, reject, resolve }; |
| 17 | +} |
| 18 | + |
| 19 | +function approval(id: string, createdAtMs: number) { |
| 20 | + return { |
| 21 | + id, |
| 22 | + createdAtMs, |
| 23 | + expiresAtMs: Date.now() + 60_000, |
| 24 | + request: { command: `echo ${id}` }, |
| 25 | + }; |
| 26 | +} |
| 27 | + |
| 28 | +function createGatewayHarness(initialClient: GatewayBrowserClient) { |
| 29 | + let snapshot: ApplicationGatewaySnapshot = { |
| 30 | + assistantAgentId: "main", |
| 31 | + client: initialClient, |
| 32 | + connected: true, |
| 33 | + hello: null, |
| 34 | + lastError: null, |
| 35 | + lastErrorCode: null, |
| 36 | + sessionKey: "main", |
| 37 | + }; |
| 38 | + const snapshotListeners = new Set<(next: ApplicationGatewaySnapshot) => void>(); |
| 39 | + const eventListeners = new Set<(event: GatewayEventFrame) => void>(); |
| 40 | + const gateway = { |
| 41 | + get snapshot() { |
| 42 | + return snapshot; |
| 43 | + }, |
| 44 | + connection: { gatewayUrl: "ws://gateway.test", password: "", token: "" }, |
| 45 | + eventLog: [], |
| 46 | + connect() {}, |
| 47 | + setSessionKey() {}, |
| 48 | + start() {}, |
| 49 | + stop() {}, |
| 50 | + subscribe(listener: (next: ApplicationGatewaySnapshot) => void) { |
| 51 | + snapshotListeners.add(listener); |
| 52 | + return () => snapshotListeners.delete(listener); |
| 53 | + }, |
| 54 | + subscribeEventLog() { |
| 55 | + return () => {}; |
| 56 | + }, |
| 57 | + subscribeEvents(listener: (event: GatewayEventFrame) => void) { |
| 58 | + eventListeners.add(listener); |
| 59 | + return () => eventListeners.delete(listener); |
| 60 | + }, |
| 61 | + } satisfies ApplicationGateway; |
| 62 | + return { |
| 63 | + emitApproval(id: string, createdAtMs: number) { |
| 64 | + const event: GatewayEventFrame = { |
| 65 | + event: "exec.approval.requested", |
| 66 | + payload: approval(id, createdAtMs), |
| 67 | + type: "event", |
| 68 | + }; |
| 69 | + for (const listener of eventListeners) { |
| 70 | + listener(event); |
| 71 | + } |
| 72 | + }, |
| 73 | + gateway, |
| 74 | + update(next: Partial<ApplicationGatewaySnapshot>) { |
| 75 | + snapshot = { ...snapshot, ...next }; |
| 76 | + for (const listener of snapshotListeners) { |
| 77 | + listener(snapshot); |
| 78 | + } |
| 79 | + }, |
| 80 | + }; |
| 81 | +} |
| 82 | + |
| 83 | +function client(request: RequestFn): GatewayBrowserClient { |
| 84 | + return { request } as unknown as GatewayBrowserClient; |
| 85 | +} |
| 86 | + |
| 87 | +describe("application approval overlays", () => { |
| 88 | + it("does not attach an older resolve failure to a newer approval", async () => { |
| 89 | + const resolveAttempt = deferred(); |
| 90 | + const request = vi.fn<RequestFn>((method) => |
| 91 | + method.endsWith(".list") ? Promise.resolve([]) : resolveAttempt.promise, |
| 92 | + ); |
| 93 | + const harness = createGatewayHarness(client(request)); |
| 94 | + const overlays = createApplicationOverlays(harness.gateway); |
| 95 | + |
| 96 | + harness.emitApproval("approval-active", 1_000); |
| 97 | + const decision = overlays.decideApproval("allow-once"); |
| 98 | + harness.emitApproval("approval-newer", 2_000); |
| 99 | + resolveAttempt.reject(new Error("gateway unavailable")); |
| 100 | + await decision; |
| 101 | + |
| 102 | + expect(overlays.snapshot.approvalQueue.map((entry) => entry.id)).toEqual([ |
| 103 | + "approval-newer", |
| 104 | + "approval-active", |
| 105 | + ]); |
| 106 | + expect(overlays.snapshot.approvalError).toBeNull(); |
| 107 | + expect(overlays.snapshot.approvalBusy).toBe(false); |
| 108 | + overlays.dispose(); |
| 109 | + }); |
| 110 | + |
| 111 | + it("does not release a new client's busy state when an old resolve settles", async () => { |
| 112 | + const oldResolve = deferred(); |
| 113 | + const oldRequest = vi.fn<RequestFn>((method) => |
| 114 | + method.endsWith(".list") ? Promise.resolve([]) : oldResolve.promise, |
| 115 | + ); |
| 116 | + const harness = createGatewayHarness(client(oldRequest)); |
| 117 | + const overlays = createApplicationOverlays(harness.gateway); |
| 118 | + |
| 119 | + harness.emitApproval("approval-old", 1_000); |
| 120 | + const oldDecision = overlays.decideApproval("allow-once"); |
| 121 | + harness.update({ client: null, connected: false }); |
| 122 | + |
| 123 | + const newResolve = deferred(); |
| 124 | + const newClient = client((method) => |
| 125 | + method.endsWith(".list") ? Promise.resolve([]) : newResolve.promise, |
| 126 | + ); |
| 127 | + harness.update({ client: newClient, connected: true }); |
| 128 | + await Promise.resolve(); |
| 129 | + harness.emitApproval("approval-new", 2_000); |
| 130 | + const newDecision = overlays.decideApproval("deny"); |
| 131 | + expect(overlays.snapshot.approvalBusy).toBe(true); |
| 132 | + |
| 133 | + oldResolve.reject(new Error("gateway client stopped")); |
| 134 | + await oldDecision; |
| 135 | + expect(overlays.snapshot.approvalBusy).toBe(true); |
| 136 | + expect(overlays.snapshot.approvalError).toBeNull(); |
| 137 | + |
| 138 | + newResolve.resolve({ ok: true }); |
| 139 | + await newDecision; |
| 140 | + expect(overlays.snapshot.approvalBusy).toBe(false); |
| 141 | + expect(overlays.snapshot.approvalQueue).toEqual([]); |
| 142 | + overlays.dispose(); |
| 143 | + }); |
| 144 | +}); |
0 commit comments