Open History to see the last commit
1/// <reference lib="dom" />
2import { expect } from "e2e";
3import * as Effect from "effect/Effect";
4import * as Schema from "effect/Schema";
5
6import { T3_HOME } from "../e2e.config.ts";
7import { SECOND_HOME, startSecondServer } from "./second-server.ts";
8import { environmentId, makeState, rpcClient, runPromise, test, WEB } from "./t3.ts";
9
10const decodePairing = Schema.decodeEffect(
11 Schema.fromJsonString(Schema.Struct({ credential: Schema.String })),
12);
13
14const linkEnvironments = Effect.fn("scenario.linkEnvironments")(function* (
15 primaryOrigin: string,
16 secondOrigin: string,
17 fallback: string,
18) {
19 const primary = yield* rpcClient(T3_HOME, primaryOrigin);
20 const second = yield* rpcClient(SECOND_HOME, secondOrigin);
21 for (const client of [primary, second]) {
22 yield* client["server.updateSettings"]({ patch: { linkedEnvironmentsEnabled: true } });
23 }
24 const pairing = yield* makeState(SECOND_HOME).cli(["auth", "pairing", "create", "--json"]);
25 const { credential } = yield* decodePairing(pairing.stdout);
26 yield* primary["peerLinks.link"]({
27 url: secondOrigin,
28 alternateUrls: [fallback],
29 pairingCode: credential,
30 access: "full-access",
31 });
32}, Effect.scoped);
33
34test(
35 "Connect to interact opens the editable thread before a silent fallback times out",
36 WEB,
37 async ({ t3, app, browser, screen }) => {
38 await t3.project("primary");
39 const primary = app.baseUrl ?? "";
40 const secondServer = startSecondServer(primary);
41 try {
42 const { host } = await secondServer.ready;
43 const alternate = new URL(host);
44 alternate.hostname = "127.0.0.2";
45 await runPromise(linkEnvironments(primary, host, alternate.origin));
46
47 const second = makeState(SECOND_HOME).state;
48 const project = await second.project("linked");
49 const thread = await second.thread({
50 project,
51 title: "Linked handoff",
52 messages: ["first"],
53 });
54 const via = await runPromise(environmentId(primary));
55 const target = await runPromise(environmentId(host));
56
57 await browser.addInitScript((fallback) => {
58 const fetchActual = globalThis.fetch;
59 globalThis.fetch = (input, init) => {
60 const url = new URL(input instanceof Request ? input.url : String(input), location.href);
61 if (url.origin !== fallback || url.pathname !== "/.well-known/t3/environment") {
62 // @effect-diagnostics-next-line globalFetch:off - This interception runs inside the browser.
63 return fetchActual(input, init);
64 }
65 // The browser's network silently drops this route. The preferred address is still real.
66 return new Promise((_, reject) => {
67 const signal = init?.signal ?? (input instanceof Request ? input.signal : undefined);
68 signal?.addEventListener("abort", () => reject(signal.reason), { once: true });
69 });
70 };
71 }, alternate.origin);
72
73 await t3.open(`/linked/${via}/${target}/${thread.threadId}`);
74 await screen.getByRole("heading", "Linked handoff").waitFor();
75 await screen.getByRole("button", "Connect to interact").tap();
76
77 try {
78 await expect
79 .poll(() => browser.evaluate(() => location.pathname), { timeout: 4_000 })
80 .toBe(`/${target}/${encodeURIComponent(thread.threadId)}`);
81 } finally {
82 await app.screenshot("linked-thread-after-connect");
83 }
84 } finally {
85 await secondServer.stop();
86 }
87 },
88);