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

previewAutomationRequestConsumer.test.ts

196 lines6.3 KB

1import {
2 EnvironmentId,
3 type PreviewAutomationRequest,
4 type PreviewAutomationResponse,
5 PreviewTabId,
6 ThreadId,
7} from "@t3tools/contracts";
8import { AsyncResult, Atom, AtomRegistry } from "effect/unstable/reactivity";
9import { describe, expect, it, vi } from "vite-plus/test";
10​
11import { PreviewAutomationTargetUnavailableError } from "./previewAutomationErrors";
12import {
13 createPreviewAutomationRequestConsumerAtom,
14 serializePreviewAutomationError,
15} from "./previewAutomationRequestConsumer";
16​
17const environmentId = EnvironmentId.make("environment-1");
18const threadId = ThreadId.make("thread-1");
19const tabId = PreviewTabId.make("tab-1");
20​
21const request = (
22 requestId: string,
23 overrides: Partial<PreviewAutomationRequest> = {},
24): PreviewAutomationRequest => ({
25 requestId,
26 threadId,
27 operation: "status",
28 input: {},
29 timeoutMs: 15_000,
30 ...overrides,
31});
32​
33describe("previewAutomationRequestConsumer", () => {
34 it("consumes every request emitted before React can render", async () => {
35 const requestsAtom = Atom.make<AsyncResult.AsyncResult<PreviewAutomationRequest, Error>>(
36 AsyncResult.initial<PreviewAutomationRequest, Error>(false),
37 );
38 const handleRequest = vi.fn(async (value: PreviewAutomationRequest) => ({
39 requestId: value.requestId,
40 }));
41 const responses: PreviewAutomationResponse[] = [];
42 const respond = vi.fn(async (response: PreviewAutomationResponse) => {
43 responses.push(response);
44 });
45 const consumerAtom = createPreviewAutomationRequestConsumerAtom({
46 requestsAtom,
47 environmentId,
48 handleRequest,
49 respond,
50 label: "test:preview-automation-consumer",
51 });
52 const registry = AtomRegistry.make();
53 registry.mount(consumerAtom);
54​
55 registry.set(requestsAtom, AsyncResult.success(request("request-1")));
56 registry.set(requestsAtom, AsyncResult.success(request("request-2")));
57​
58 await vi.waitFor(() => expect(respond).toHaveBeenCalledTimes(2));
59 expect(handleRequest.mock.calls.map(([value]) => value.requestId)).toEqual([
60 "request-1",
61 "request-2",
62 ]);
63 expect(responses.map((response) => response.requestId)).toEqual(["request-1", "request-2"]);
64 registry.dispose();
65 });
66​
67 it("consumes a request that arrived immediately before the consumer mounted", async () => {
68 const requestsAtom = Atom.make(
69 AsyncResult.success<PreviewAutomationRequest, Error>(request("request-ready")),
70 );
71 const respond = vi.fn(async (_response: PreviewAutomationResponse) => undefined);
72 const consumerAtom = createPreviewAutomationRequestConsumerAtom({
73 requestsAtom,
74 environmentId,
75 handleRequest: async () => undefined,
76 respond,
77 label: "test:preview-automation-initial-request",
78 });
79 const registry = AtomRegistry.make();
80​
81 registry.mount(consumerAtom);
82​
83 await vi.waitFor(() => expect(respond).toHaveBeenCalledTimes(1));
84 expect(respond).toHaveBeenCalledWith({ requestId: "request-ready", ok: true });
85 registry.dispose();
86 });
87​
88 it("preserves tagged automation errors and their structured diagnostics", () => {
89 const error = new PreviewAutomationTargetUnavailableError({
90 requestId: "request-1",
91 operation: "click",
92 environmentId,
93 threadId,
94 tabId,
95 bridgeAvailable: false,
96 });
97​
98 expect(
99 serializePreviewAutomationError(error, {
100 requestId: "request-1",
101 operation: "click",
102 environmentId,
103 threadId,
104 tabId,
105 }),
106 ).toEqual({
107 _tag: "PreviewAutomationTabNotFoundError",
108 message:
109 "Preview automation target for click request request-1 is unavailable on environment environment-1 thread thread-1 (tab tab-1, bridge unavailable).",
110 detail: {
111 requestId: "request-1",
112 operation: "click",
113 environmentId: "environment-1",
114 threadId: "thread-1",
115 tabId: "tab-1",
116 bridgeAvailable: false,
117 },
118 });
119 });
120​
121 it("correlates unexpected failures without exposing cause details", () => {
122 const cause = new Error("private bridge token: preview-secret");
123 const context = {
124 requestId: "request-2",
125 operation: "snapshot" as const,
126 environmentId,
127 threadId,
128 tabId,
129 };
130 const response = serializePreviewAutomationError(cause, context);
131​
132 expect(response).toEqual({
133 _tag: "PreviewAutomationExecutionError",
134 message:
135 "Preview automation snapshot request request-2 failed on environment environment-1 thread thread-1 (tab tab-1).",
136 detail: {
137 requestId: "request-2",
138 operation: "snapshot",
139 environmentId: "environment-1",
140 threadId: "thread-1",
141 tabId: "tab-1",
142 },
143 });
144 expect(JSON.stringify(response)).not.toContain("preview-secret");
145 });
146​
147 it("sanitizes unexpected handler failures at the response boundary", async () => {
148 const requestsAtom = Atom.make<AsyncResult.AsyncResult<PreviewAutomationRequest, Error>>(
149 AsyncResult.initial<PreviewAutomationRequest, Error>(false),
150 );
151 const responses: PreviewAutomationResponse[] = [];
152 const consumerAtom = createPreviewAutomationRequestConsumerAtom({
153 requestsAtom,
154 environmentId,
155 handleRequest: async () => {
156 throw new Error("desktop IPC secret: do-not-return");
157 },
158 respond: async (response) => {
159 responses.push(response);
160 },
161 label: "test:preview-automation-failure-boundary",
162 });
163 const registry = AtomRegistry.make();
164 registry.mount(consumerAtom);
165​
166 registry.set(
167 requestsAtom,
168 AsyncResult.success(
169 request("request-failed", {
170 operation: "click",
171 tabId,
172 }),
173 ),
174 );
175​
176 await vi.waitFor(() => expect(responses).toHaveLength(1));
177 expect(responses[0]).toEqual({
178 requestId: "request-failed",
179 ok: false,
180 error: {
181 _tag: "PreviewAutomationExecutionError",
182 message:
183 "Preview automation click request request-failed failed on environment environment-1 thread thread-1 (tab tab-1).",
184 detail: {
185 requestId: "request-failed",
186 operation: "click",
187 environmentId: "environment-1",
188 threadId: "thread-1",
189 tabId: "tab-1",
190 },
191 },
192 });
193 expect(JSON.stringify(responses[0])).not.toContain("do-not-return");
194 registry.dispose();
195 });
196});