diff --git a/docs/operations/scenarios.md b/docs/operations/scenarios.md index 798d066371684..be5aefc10a3b7 100644 --- a/docs/operations/scenarios.md +++ b/docs/operations/scenarios.md @@ -68,16 +68,13 @@ configured. ### Getting to the screen Most scenarios that could not run stopped on the way to the screen the bug is on, not on the -bug. Three rules avoid that: +bug. Two rules avoid that: - **Open by link, not by sidebar.** `await t3.open(thread)` lands on the thread ([`thread-opens-by-link.e2e.ts`](../../scripts/scenarios/thread-opens-by-link.e2e.ts)), and `await t3.open("/settings/general")` on a settings page. A sidebar row is a button that covers its own title, so a tap on `getByText("Earlier work")` is intercepted. When the sidebar is what the scenario is about, target the row's button: `screen.getByRole("button", "Earlier work")`. -- **Let `t3.open` do the navigating.** It waits until the pairing page has signed in before it - goes anywhere. A second `app.open` straight after the pairing link cancels the sign-in and - shows the pairing page again. - **Seed before opening.** Create projects and threads first, then call `t3.open`. With no project the app opens its first-run page instead of the one the scenario expects. diff --git a/scripts/scenarios/t3.ts b/scripts/scenarios/t3.ts index fab377de1028b..aa71d13ef1637 100644 --- a/scripts/scenarios/t3.ts +++ b/scripts/scenarios/t3.ts @@ -3,8 +3,7 @@ import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; import * as NodeServices from "@effect/platform-node/NodeServices"; import * as NodeWS from "@effect/platform-node/NodeWS"; -import { test as base } from "@e2e-dev/web"; -import { expect } from "e2e"; +import { test as base, type Cookie } from "@e2e-dev/web"; import { CommandId, EnvironmentHttpApi, @@ -14,6 +13,8 @@ import { WsRpcGroup, } from "@t3tools/contracts"; import * as HostProcess from "@t3tools/shared/HostProcess"; +import { getPairingTokenFromUrl } from "@t3tools/shared/remote"; +import * as Clock from "effect/Clock"; import * as SecureRandom from "effect/crypto/SecureRandom"; import * as Effect from "effect/Effect"; import * as FileSystem from "effect/FileSystem"; @@ -22,7 +23,13 @@ import * as Layer from "effect/Layer"; import * as Path from "effect/Path"; import * as Schedule from "effect/Schedule"; import * as Schema from "effect/Schema"; -import { FetchHttpClient, HttpClient, HttpClientRequest, HttpClientResponse } from "effect/http"; +import { + Cookies, + FetchHttpClient, + HttpClient, + HttpClientRequest, + HttpClientResponse, +} from "effect/http"; import { RpcClient, RpcSerialization } from "effect/rpc"; import { Socket } from "effect/socket"; @@ -234,6 +241,41 @@ const readPairingPath = Effect.gen(function* () { return yield* fs.readFileString(path.join(T3_HOME, "pairing-path")); }); +const sameSite = { lax: "Lax", strict: "Strict", none: "None" } as const; + +/** + * Signs in the way the pairing page does and returns the session cookies. Opening the page + * instead races its reload after sign-in against the test's next `app.open`. + */ +const signIn = Effect.fn("scenario.signIn")(function* (baseUrl: string) { + const credential = getPairingTokenFromUrl(new URL(yield* readPairingPath, baseUrl)); + if (credential === null) { + return yield* new ScenarioError({ message: "The pairing link holds no token." }); + } + const client = yield* HttpApiClient.make(EnvironmentHttpApi, { baseUrl }); + // Only the cookies are read: a proof runs this against a base server whose body may differ. + const response = yield* client.auth + .browserSession({ payload: { credential }, responseMode: "response-only" }) + .pipe(Effect.flatMap(HttpClientResponse.filterStatusOk)); + // Drops the expired cookies that only clear an older session. + const cookies = Cookies.mergeIntoArray( + [], + baseUrl, + response.cookies, + yield* Clock.currentTimeMillis, + ); + return cookies.map(({ name, value, options = {} }): Cookie => ({ + name, + value, + // The server sets no Domain and `Path=/`, which is what a cookie for the origin gets. + url: baseUrl, + ...(options.expires && { expires: Math.floor(options.expires.getTime() / 1000) }), + ...(options.httpOnly !== undefined && { httpOnly: options.httpOnly }), + ...(options.secure !== undefined && { secure: options.secure }), + ...(options.sameSite && { sameSite: sameSite[options.sameSite] }), + })); +}, Effect.provide(FetchHttpClient.layer)); + /** The id every thread link starts with, asked of the server the app talks to. */ export const environmentId = (baseUrl: string) => HttpClient.get(`${baseUrl}/.well-known/t3/environment`).pipe( @@ -278,14 +320,13 @@ export const test = base.extend<{ t3: T3 }>({ await use({ ...state, open: async (target = "/") => { - await app.open(await runPromise(readPairingPath)); - // The pairing page signs in and then leaves itself. Going anywhere before it has done - // so cancels the sign-in, and the next page is the pairing page again. - await expect(browser).not.toHaveURL(/\/pair(?:[?#]|$)/, { timeout: 60_000 }); - if (typeof target !== "string") { - const environment = await runPromise(environmentId(app.baseUrl ?? "")); - await app.open(`/${environment}/${target.threadId}`); - } else if (target !== "/") await app.open(target); + const baseUrl = app.baseUrl ?? ""; + await browser.setCookies(await runPromise(signIn(baseUrl))); + await app.open( + typeof target === "string" + ? target + : `/${await runPromise(environmentId(baseUrl))}/${target.threadId}`, + ); }, }); await runPromise(cleanup);