/** * The shell suite's backend: the throwaway stack of `pnpm db:sandbox`, never * DEV. The app is built against a fake origin that resolves nowhere; every * request to it is replayed on the sandbox's loopback services, the way Kong * maps them on the CLI stack. Accounts are fictitious and made per test. */ import { createHash, randomBytes, randomUUID } from 'node:crypto' import type { BrowserContext } from '@playwright/test' import { sql, stack } from '../../../../packages/supabase/tests/sandbox/stack' /** The Supabase URL the e2e:shell dev server is built with (RFC 6761 `.invalid`). */ export const FAKE_SUPABASE_ORIGIN = 'http://sandbox.faunosteo.invalid' const storageUrl = process.env.SANDBOX_STORAGE_URL if (!storageUrl) throw new Error('SANDBOX_STORAGE_URL missing: run pnpm e2e:shell') const ROUTES: [prefix: string, target: string][] = [ ['/auth/v1/', `${stack.authUrl}/`], ['/rest/v1/', `${stack.restUrl}/`], ['/storage/v1/', `${storageUrl}/`], ] /** The sandbox URL a request to the fake Supabase origin maps to, or null when unmapped. */ export function sandboxUrl(url: string): string | null { const { pathname, search } = new URL(url) const path = pathname + search const mapped = ROUTES.find(([prefix]) => path.startsWith(prefix)) return mapped ? mapped[1] + path.slice(mapped[0].length) : null } /** Replays the page's Supabase calls on the sandbox; an unmapped path is refused. */ export async function routeToSandbox(context: BrowserContext): Promise { await context.route(`${FAKE_SUPABASE_ORIGIN}/**`, async (route) => { const target = sandboxUrl(route.request().url()) if (!target) return route.abort('blockedbyclient') const response = await route.fetch({ url: target }) await route.fulfill({ response }) }) } /** * After a test: removes the replay without waiting for it. A page may still be * reading (a list, a section reloaded after a change) when the last check * passed; that answer is dropped with the context instead of failing the test. */ export async function dropLateReplays(context: BrowserContext): Promise { await context.unrouteAll({ behavior: 'ignoreErrors' }) } /** 1 × 1 lossless WebP: a real image for the clean avatar the account starts with. */ const AVATAR_WEBP = Buffer.from( 'UklGRhoAAABXRUJQVlA4TA0AAAAvAAAAEAcQERGIiP4HAA==', 'base64', ) export interface SandboxAccount { id: string email: string password: string displayName: string } async function admin(path: string, init: RequestInit): Promise { const response = await fetch(`${stack.authUrl}${path}`, { ...init, headers: { Authorization: `Bearer ${stack.serviceKey}`, apikey: stack.serviceKey, 'Content-Type': 'application/json', }, }) if (!response.ok) throw new Error(`sandbox admin ${path}: HTTP ${response.status}`) return response } /** The clean avatar the worker would have produced: row as postgres, object with the service key. */ async function seedAvatar(ownerId: string): Promise { const assetId = randomUUID() const key = `profiles/${ownerId}/${assetId}/avatar.webp` const sha256 = createHash('sha256').update(AVATAR_WEBP).digest('hex') const upload = await fetch(`${storageUrl}/object/avatars/${key}`, { method: 'POST', headers: { Authorization: `Bearer ${stack.serviceKey}`, 'Content-Type': 'image/webp', }, body: AVATAR_WEBP, }) if (!upload.ok) throw new Error(`sandbox avatar upload: HTTP ${upload.status}`) await sql(`insert into public.assets (id, owner_id, kind, bucket_id, object_key, status, declared_mime, declared_size, byte_size, sha256) values ('${assetId}', '${ownerId}', 'profile_avatar', 'avatars', '${key}', 'clean', 'image/webp', ${AVATAR_WEBP.length}, ${AVATAR_WEBP.length}, '${sha256}')`) } /** * A confirmed fictitious account, its profile in `locale`, with or without a * photo; `localPart` starts the address (a long one tests narrow screens). */ export async function createAccount( locale: 'fr' | 'en', withAvatar: boolean, localPart = 'e2e-shell', ): Promise { const tag = randomUUID().slice(0, 8) const account = { email: `${localPart}-${tag}@sandbox.local`, password: randomBytes(18).toString('base64url'), displayName: `Praticienne Fictive ${tag}`, } const response = await admin('/admin/users', { method: 'POST', body: JSON.stringify({ email: account.email, password: account.password, email_confirm: true, user_metadata: { display_name: account.displayName, locale }, }), }) const { id } = (await response.json()) as { id: string } if (withAvatar) await seedAvatar(id) return { ...account, id } } /** The personal workspace of a sandbox account (created by the server at sign-up). */ export function workspaceOf(account: SandboxAccount): Promise { return sql( `select id from public.workspaces where personal_user_id = '${account.id}'`, ) }