| 58 | }; |
| 59 | |
| 60 | export const cloudTarget = (): Target => ({ |
| 61 | name: "cloud", |
| 62 | baseUrl: CLOUD_BASE_URL, |
| 63 | mcpUrl: `${CLOUD_BASE_URL}/mcp`, |
| 64 | capabilities: new Set(["api", "browser", "billing", "mcp-oauth"]), |
| 65 | // Cloud's authorization server is the WorkOS emulator, so token-expiry |
| 66 | // scenarios can compress the lifecycle (the TtlControl service). |
| 67 | setAccessTokenTtl: (seconds) => |
| 68 | Effect.promise(async () => { |
| 69 | const workos = await connectEmulator({ |
| 70 | baseUrl: `http://127.0.0.1:${WORKOS_EMULATOR_PORT}`, |
| 71 | }); |
| 72 | await workos.seed({ oauth: { default_access_token_ttl_seconds: seconds } }); |
| 73 | }), |
| 74 | newIdentity: ({ org = true } = {}) => |
| 75 | Effect.promise(async (): Promise<Identity> => { |
| 76 | const label = `user-${randomUUID().slice(0, 8)}`; |
| 77 | const email = `${label}@e2e.test`; |
| 78 | let session = await signIn(email); |
| 79 | let orgSlug: string | null = null; |
| 80 | if (org) { |
| 81 | // The real create-organization flow; the refreshed sealed session in |
| 82 | // the response carries the new org. |
| 83 | const response = await fetch(new URL("/api/auth/create-organization", CLOUD_BASE_URL), { |
| 84 | method: "POST", |
| 85 | headers: { |
| 86 | "content-type": "application/json", |
| 87 | origin: new URL(CLOUD_BASE_URL).origin, |
| 88 | cookie: session, |
| 89 | }, |
| 90 | body: JSON.stringify({ name: `Org ${label}` }), |
| 91 | }); |
| 92 | if (!response.ok) { |
| 93 | throw new Error(`cloud newIdentity: create-organization failed (${response.status})`); |
| 94 | } |
| 95 | session = cookiePair(response, "wos-session") ?? session; |
| 96 | orgSlug = ((await response.json()) as { slug?: string }).slug ?? null; |
| 97 | } |
| 98 | const [name, value] = session.split(/=(.*)/s); |
| 99 | return { |
| 100 | label: email, |
| 101 | // The org selector header rides along exactly as the web client sends |
| 102 | // it from the console URL's slug: org-scoped API reads fail closed |
| 103 | // without it (no session-org fallback). |
| 104 | headers: { |
| 105 | cookie: session, |
| 106 | ...(orgSlug ? { "x-executor-organization": orgSlug } : {}), |
| 107 | }, |
| 108 | cookies: [{ name: name!, value: value! }], |
| 109 | credentials: { email, password: "emulated" }, |
| 110 | }; |
| 111 | }), |
| 112 | // MCP OAuth against the emulator's authorization server: complete the |
| 113 | // hosted flow headlessly as this identity. |
| 114 | mcpConsent: (identity) => async (request) => { |
| 115 | const authorizeUrl = new URL(request.authorizationUrl); |
| 116 | authorizeUrl.searchParams.set("login_hint", identity.credentials?.email ?? "mcp@e2e.test"); |
| 117 | const response = await fetch(authorizeUrl, { redirect: "manual" }); |