|
| 1 | +// Cloud-only (browser): two tabs, two orgs, at the same time — independent. |
| 2 | +// |
| 3 | +// WorkOS still pins ONE org into the sealed `wos-session` cookie, and the whole |
| 4 | +// browser shares one cookie jar. Under the OLD cookie-based "active org" model |
| 5 | +// that made "active organization" a browser-global: two tabs could not be in |
| 6 | +// two orgs at once, and a switch (or the slug gate's switch-to-honor-the-URL) |
| 7 | +// silently re-scoped the other tab out from under it. |
| 8 | +// |
| 9 | +// The stateless URL model removes that hazard. The slug in the path is the |
| 10 | +// request scope: every API call carries it (the `x-executor-organization` |
| 11 | +// header), the server re-checks live membership and resolves data for THAT |
| 12 | +// org, and the session merely authenticates the user to all their orgs at |
| 13 | +// once. Nothing writes the cookie on a switch. So this scenario — once the |
| 14 | +// reproduction of the corruption — now asserts the opposite: each tab's |
| 15 | +// requests stay scoped to its own URL org, no matter what the other tab does. |
| 16 | +// |
| 17 | +// Everything runs through the browser (onboarding + the menu create-org), so |
| 18 | +// the single-use WorkOS refresh-token chain stays browser-owned and valid. |
| 19 | +import { expect } from "@effect/vitest"; |
| 20 | +import { Effect } from "effect"; |
| 21 | + |
| 22 | +import { scenario } from "../src/scenario"; |
| 23 | +import { Browser, Target } from "../src/services"; |
| 24 | + |
| 25 | +scenario( |
| 26 | + "Org tabs · two tabs on different orgs stay independent (URL-scoped, no cookie steal)", |
| 27 | + {}, |
| 28 | + Effect.gen(function* () { |
| 29 | + const target = yield* Target; |
| 30 | + const browser = yield* Browser; |
| 31 | + const identity = yield* target.newIdentity({ org: false }); |
| 32 | + |
| 33 | + yield* browser.session(identity, async ({ page: tab1, step }) => { |
| 34 | + const slugOf = (page: typeof tab1) => new URL(page.url()).pathname.replace(/^\/|\/.*$/g, ""); |
| 35 | + |
| 36 | + // The org slug a page's REAL app requests carry — read straight off the |
| 37 | + // outgoing `x-executor-organization` header, the actual request scope. |
| 38 | + // This is what makes the two tabs independent; the shared session cookie |
| 39 | + // is irrelevant to it. |
| 40 | + const requestOrgSlugOf = async (page: typeof tab1): Promise<string> => { |
| 41 | + const matching = page.waitForRequest( |
| 42 | + (request) => |
| 43 | + request.url().includes("/api/") && |
| 44 | + request.headers()["x-executor-organization"] !== undefined, |
| 45 | + { timeout: 15_000 }, |
| 46 | + ); |
| 47 | + // Nudge the app to refetch so a fresh scoped request goes out. |
| 48 | + void page.reload({ waitUntil: "commit" }); |
| 49 | + return (await matching).headers()["x-executor-organization"]!; |
| 50 | + }; |
| 51 | + |
| 52 | + let slugA = ""; |
| 53 | + let slugB = ""; |
| 54 | + |
| 55 | + await step("Onboard org A in tab 1", async () => { |
| 56 | + await tab1.goto("/", { waitUntil: "networkidle" }); |
| 57 | + await tab1.getByPlaceholder("Northwind Labs").fill("Multitab A"); |
| 58 | + await tab1.getByRole("button", { name: "Create organization" }).click(); |
| 59 | + await tab1.getByText("Connect your MCP client").waitFor({ timeout: 30_000 }); |
| 60 | + await tab1.getByRole("button", { name: "Continue to app" }).click(); |
| 61 | + await tab1.waitForURL((url) => /^\/[a-z0-9-]+\/?$/.test(url.pathname), { timeout: 30_000 }); |
| 62 | + await tab1.getByText("Integrations").first().waitFor({ timeout: 30_000 }); |
| 63 | + slugA = slugOf(tab1); |
| 64 | + }); |
| 65 | + |
| 66 | + await step("Create org B from tab 1's account menu — tab 1 is now in B", async () => { |
| 67 | + await tab1.getByRole("button", { name: /Test User/ }).click(); |
| 68 | + await tab1.getByRole("menuitem", { name: "Multitab A" }).click(); |
| 69 | + await tab1 |
| 70 | + .locator('[data-slot="dropdown-menu-sub-content"]') |
| 71 | + .getByText("Create organization", { exact: true }) |
| 72 | + .click(); |
| 73 | + await tab1.getByText("Add another organization").waitFor(); |
| 74 | + await tab1.getByPlaceholder("Northwind Labs").fill("Multitab B"); |
| 75 | + await tab1.getByRole("button", { name: "Create organization" }).click(); |
| 76 | + await tab1.waitForURL((url) => url.pathname !== `/${slugA}`, { timeout: 30_000 }); |
| 77 | + await tab1.getByText("Integrations").first().waitFor({ timeout: 30_000 }); |
| 78 | + slugB = slugOf(tab1); |
| 79 | + }); |
| 80 | + expect(slugB, "the two orgs have distinct slugs").not.toBe(slugA); |
| 81 | + |
| 82 | + // A second tab in the SAME context — shares tab 1's cookie jar. |
| 83 | + const tab2 = await tab1.context().newPage(); |
| 84 | + |
| 85 | + await step("Tab 2 opens org A's URL and stays in A — no switch, no reload loop", async () => { |
| 86 | + await tab2.goto(`/${slugA}/policies`, { waitUntil: "networkidle" }); |
| 87 | + await tab2.getByText("Policies").first().waitFor({ timeout: 30_000 }); |
| 88 | + expect(new URL(tab2.url()).pathname, "tab 2 stays on org A's URL").toBe( |
| 89 | + `/${slugA}/policies`, |
| 90 | + ); |
| 91 | + expect(await requestOrgSlugOf(tab2), "tab 2's API requests are scoped to org A").toBe( |
| 92 | + slugA, |
| 93 | + ); |
| 94 | + }); |
| 95 | + |
| 96 | + await step("Tab 1 is untouched: still org B, and its requests still scope to B", async () => { |
| 97 | + expect(new URL(tab1.url()).pathname, "tab 1's URL still says org B").toBe(`/${slugB}`); |
| 98 | + expect( |
| 99 | + await tab1.getByRole("button", { name: /Multitab B/ }).isVisible(), |
| 100 | + "tab 1's sidebar still shows org B", |
| 101 | + ).toBe(true); |
| 102 | + // The crux: tab 2 opening org A did NOT re-scope tab 1. Tab 1's own |
| 103 | + // requests still carry org B's slug — the URL is the scope, not a |
| 104 | + // shared cookie a sibling tab can steal. |
| 105 | + expect(await requestOrgSlugOf(tab1), "tab 1's API requests stay scoped to org B").toBe( |
| 106 | + slugB, |
| 107 | + ); |
| 108 | + }); |
| 109 | + }); |
| 110 | + }), |
| 111 | +); |
0 commit comments