Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

PreviewAutomationBroker.test.ts

299 lines10.0 KB

1import { expect, it } from "@effect/vitest";
2import {
3 EnvironmentId,
4 PreviewAutomationClientDisconnectedError,
5 PreviewAutomationInvalidSelectorError,
6 PreviewAutomationMalformedResponseError,
7 PreviewAutomationNoFocusedOwnerError,
8 ProviderInstanceId,
9 ThreadId,
10 type PreviewAutomationOwner,
11} from "@t3tools/contracts";
12import * as Effect from "effect/Effect";
13import * as Fiber from "effect/Fiber";
14import * as Stream from "effect/Stream";
15​
16import * as PreviewAutomationBroker from "./PreviewAutomationBroker.ts";
17​
18const scope = {
19 environmentId: EnvironmentId.make("environment-1"),
20 threadId: ThreadId.make("thread-1"),
21 providerSessionId: "provider-session-1",
22 providerInstanceId: ProviderInstanceId.make("codex"),
23 capabilities: new Set(["preview"] as const),
24 issuedAt: 1,
25 expiresAt: 2,
26};
27​
28const makeOwner = (overrides: Partial<PreviewAutomationOwner> = {}): PreviewAutomationOwner => ({
29 clientId: "client-1",
30 environmentId: scope.environmentId,
31 threadId: scope.threadId,
32 tabId: null,
33 visible: false,
34 supportsAutomation: true,
35 focusedAt: "2026-06-11T00:00:00.000Z",
36 ...overrides,
37});
38​
39it.effect("atomically registers a connected owner and correlates its response", () =>
40 Effect.scoped(
41 Effect.gen(function* () {
42 const broker = yield* PreviewAutomationBroker.make;
43 const requests = yield* broker.connect(makeOwner());
44 yield* Stream.runForEach(requests, (request) =>
45 broker.respond({
46 requestId: request.requestId,
47 ok: true,
48 result: { available: true },
49 }),
50 ).pipe(Effect.forkScoped);
51 yield* Effect.yieldNow;
52​
53 const result = yield* broker.invoke<{ available: boolean }>({
54 scope,
55 operation: "open",
56 input: {},
57 });
58​
59 expect(result).toEqual({ available: true });
60 }),
61 ),
62);
63​
64it.effect("preserves bounded request and remote selector diagnostics", () => {
65 const locator = "role=button[name='request-secret']";
66 const remoteMessage = "Unexpected token near remote-secret.";
67 const remoteError = {
68 _tag: "PreviewAutomationInvalidSelectorError",
69 message: remoteMessage,
70 detail: { selector: "role=button[name='remote-secret']" },
71 } as const;
72​
73 return Effect.scoped(
74 Effect.gen(function* () {
75 const broker = yield* PreviewAutomationBroker.make;
76 const requests = yield* broker.connect(makeOwner({ tabId: "tab-1" }));
77 yield* Stream.runForEach(requests, (request) =>
78 broker.respond({
79 requestId: request.requestId,
80 ok: false,
81 error: remoteError,
82 }),
83 ).pipe(Effect.forkScoped);
84 yield* Effect.yieldNow;
85​
86 const error = yield* broker
87 .invoke<void>({
88 scope,
89 operation: "click",
90 input: { locator },
91 timeoutMs: 1_234,
92 })
93 .pipe(Effect.flip);
94​
95 expect(error).toBeInstanceOf(PreviewAutomationInvalidSelectorError);
96 expect(error).toMatchObject({
97 operation: "click",
98 environmentId: scope.environmentId,
99 threadId: scope.threadId,
100 providerSessionId: scope.providerSessionId,
101 providerInstanceId: scope.providerInstanceId,
102 clientId: "client-1",
103 requestId: "preview-0",
104 tabId: "tab-1",
105 timeoutMs: 1_234,
106 selectorKind: "locator",
107 selectorLength: locator.length,
108 remoteTag: "PreviewAutomationInvalidSelectorError",
109 remoteMessageLength: remoteMessage.length,
110 remoteDetailKind: "object",
111 });
112 expect(error.message).toBe(
113 `Preview automation click received an invalid locator (${locator.length} characters).`,
114 );
115 expect(error.message).not.toContain("secret");
116 expect(error.cause).toBe(remoteError);
117 expect("selector" in error).toBe(false);
118 expect("remoteMessage" in error).toBe(false);
119 expect("remoteDetail" in error).toBe(false);
120 }),
121 );
122});
123​
124it.effect("distinguishes malformed remote failures", () =>
125 Effect.scoped(
126 Effect.gen(function* () {
127 const broker = yield* PreviewAutomationBroker.make;
128 const requests = yield* broker.connect(makeOwner());
129 yield* Stream.runForEach(requests, (request) =>
130 broker.respond({ requestId: request.requestId, ok: false }),
131 ).pipe(Effect.forkScoped);
132 yield* Effect.yieldNow;
133​
134 const error = yield* broker
135 .invoke<void>({ scope, operation: "status", input: {}, timeoutMs: 2_000 })
136 .pipe(Effect.flip);
137​
138 expect(error).toBeInstanceOf(PreviewAutomationMalformedResponseError);
139 expect(error).toMatchObject({
140 operation: "status",
141 environmentId: scope.environmentId,
142 threadId: scope.threadId,
143 providerSessionId: scope.providerSessionId,
144 providerInstanceId: scope.providerInstanceId,
145 clientId: "client-1",
146 requestId: "preview-0",
147 timeoutMs: 2_000,
148 });
149 }),
150 ),
151);
152​
153it.effect("rejects calls when no focused owner exists", () =>
154 Effect.gen(function* () {
155 const broker = yield* PreviewAutomationBroker.make;
156 const error = yield* broker
157 .invoke<void>({ scope, operation: "status", input: {} })
158 .pipe(Effect.flip);
159 expect(error).toBeInstanceOf(PreviewAutomationNoFocusedOwnerError);
160 expect(error).toMatchObject({
161 operation: "status",
162 environmentId: scope.environmentId,
163 threadId: scope.threadId,
164 providerSessionId: scope.providerSessionId,
165 providerInstanceId: scope.providerInstanceId,
166 });
167 }),
168);
169​
170it.effect("routes interactive commands to a hidden durable browser host", () =>
171 Effect.scoped(
172 Effect.gen(function* () {
173 const broker = yield* PreviewAutomationBroker.make;
174 const requests = yield* broker.connect(
175 makeOwner({ clientId: "client-hidden", tabId: "tab-hidden" }),
176 );
177 yield* Stream.runForEach(requests, (request) =>
178 broker.respond({ requestId: request.requestId, ok: true }),
179 ).pipe(Effect.forkScoped);
180 yield* Effect.yieldNow;
181​
182 yield* broker.invoke<void>({ scope, operation: "click", input: { x: 10, y: 10 } });
183 }),
184 ),
185);
186​
187it.effect("lets the browser host resolve an active tab that has not been reported yet", () =>
188 Effect.scoped(
189 Effect.gen(function* () {
190 const broker = yield* PreviewAutomationBroker.make;
191 const requests = yield* broker.connect(makeOwner({ tabId: null }));
192 let routedTabId: string | undefined;
193 yield* Stream.runForEach(requests, (request) => {
194 routedTabId = request.tabId;
195 return broker.respond({ requestId: request.requestId, ok: true });
196 }).pipe(Effect.forkScoped);
197 yield* Effect.yieldNow;
198​
199 yield* broker.invoke<void>({ scope, operation: "click", input: { x: 10, y: 10 } });
200​
201 expect(routedTabId).toBeUndefined();
202 }),
203 ),
204);
205​
206it.effect("preserves current owner metadata when its request stream reconnects", () =>
207 Effect.scoped(
208 Effect.gen(function* () {
209 const broker = yield* PreviewAutomationBroker.make;
210 const firstRequests = yield* broker.connect(makeOwner());
211 yield* Stream.runDrain(firstRequests).pipe(Effect.forkScoped);
212 yield* broker.reportOwner(makeOwner({ tabId: "tab-current", visible: true }));
213​
214 const reconnectedRequests = yield* broker.connect(makeOwner());
215 let routedTabId: string | undefined;
216 yield* Stream.runForEach(reconnectedRequests, (request) => {
217 routedTabId = request.tabId;
218 return broker.respond({ requestId: request.requestId, ok: true });
219 }).pipe(Effect.forkScoped);
220 yield* Effect.yieldNow;
221​
222 yield* broker.invoke<void>({ scope, operation: "click", input: { x: 10, y: 10 } });
223​
224 expect(routedTabId).toBe("tab-current");
225 }),
226 ),
227);
228​
229it.effect("ignores stale owner cleanup after the client moves to another thread", () =>
230 Effect.scoped(
231 Effect.gen(function* () {
232 const broker = yield* PreviewAutomationBroker.make;
233 const requests = yield* broker.connect(makeOwner());
234 yield* Stream.runForEach(requests, (request) =>
235 broker.respond({ requestId: request.requestId, ok: true }),
236 ).pipe(Effect.forkScoped);
237 yield* Effect.yieldNow;
238​
239 yield* broker.clearOwner({
240 clientId: "client-1",
241 environmentId: scope.environmentId,
242 threadId: ThreadId.make("thread-stale"),
243 });
244​
245 yield* broker.invoke<void>({ scope, operation: "status", input: {} });
246 }),
247 ),
248);
249​
250it.effect("fails requests assigned to a browser stream when that stream reconnects", () =>
251 Effect.scoped(
252 Effect.gen(function* () {
253 const broker = yield* PreviewAutomationBroker.make;
254 const _requests = yield* broker.connect(makeOwner());
255 const pending = yield* broker
256 .invoke<void>({ scope, operation: "status", input: {} })
257 .pipe(Effect.flip, Effect.forkScoped);
258 yield* Effect.yieldNow;
259​
260 const _replacementRequests = yield* broker.connect(makeOwner());
261​
262 const error = yield* Fiber.join(pending);
263 expect(error).toBeInstanceOf(PreviewAutomationClientDisconnectedError);
264 expect(error).toMatchObject({
265 operation: "status",
266 environmentId: scope.environmentId,
267 threadId: scope.threadId,
268 providerSessionId: scope.providerSessionId,
269 providerInstanceId: scope.providerInstanceId,
270 clientId: "client-1",
271 requestId: "preview-0",
272 timeoutMs: 15_000,
273 });
274 }),
275 ),
276);
277​
278it.effect("falls back to an older connected owner when a newer report is not connected", () =>
279 Effect.scoped(
280 Effect.gen(function* () {
281 const broker = yield* PreviewAutomationBroker.make;
282 const requests = yield* broker.connect(makeOwner({ clientId: "client-connected" }));
283 yield* Stream.runForEach(requests, (request) =>
284 broker.respond({ requestId: request.requestId, ok: true, result: "connected" }),
285 ).pipe(Effect.forkScoped);
286 yield* Effect.yieldNow;
287 yield* broker.reportOwner(
288 makeOwner({
289 clientId: "client-report-only",
290 focusedAt: "2026-06-11T00:00:01.000Z",
291 }),
292 );
293​
294 const result = yield* broker.invoke<string>({ scope, operation: "status", input: {} });
295​
296 expect(result).toBe("connected");
297 }),
298 ),
299);