diff --git a/tests/console-fill.test.ts b/tests/console-fill.test.ts index ecc61634..34b338b6 100644 --- a/tests/console-fill.test.ts +++ b/tests/console-fill.test.ts @@ -4,19 +4,20 @@ // installed (a launch that times out fails instead). Load-proofing (tests/helpers/browser.ts): one launch with a long // launch timeout, every context closed right after its test, event-driven waits for log lines, explicit test timeouts. // Slow on purpose (marked SLOW): the sign-in / watching tests and the account tests wait out real "not here" timeouts -// of the engine (navTimeoutMs / navMs, scaled down through the options) and the profile tests relaunch Chromium. +// of the engine (navTimeoutMs / navMs, scaled down through the options). The relaunch of storebound's own profile +// (session cookies kept) needs a real Chrome: tests/console-persist.test.ts. import { afterAll, afterEach, beforeAll, describe, expect, test } from 'bun:test'; import { mkdtempSync, rmSync } from 'node:fs'; import { tmpdir } from 'node:os'; import { join } from 'node:path'; import { openAcross, readAccounts, rememberApple, rememberPlay, withAccountIds } from '../cli/console/accounts'; import { createAppHere, fillPage, fillSession, navClick, navigate, resumeAt, run, safeClick, signInPage } from '../cli/console/fill'; -import { type ConsoleBrowser, openConsoleBrowser } from '../cli/console/session'; +import type { ConsoleBrowser } from '../cli/console/session'; import { parseArgs } from '../cli/lib/args'; import { type ShipConfig, validateExtras } from '../cli/lib/config'; import { AGE_RATING_API, consoleAnswers, consoleResult, fillValue, FORM_IDS, type FormId, formMeta } from '../cli/lib/console-answers'; import { consoleReached, CREATE_APP_OPENERS, createAppAllowed, formDef, FORM_OPENERS, forbiddenControl, hasToken, isSignInText, isSignInUrl, navAllowed, navPlan, playDeveloperId, switchAllowed } from '../cli/lib/console-forms'; -import { AFTER_ALL_MS, AFTER_EACH_MS, capped, CLOSE_TIMEOUT_MS, LAUNCH_TIMEOUT_MS, launchTestBrowser, logLines, testScope } from './helpers/browser'; +import { AFTER_ALL_MS, AFTER_EACH_MS, launchTestBrowser, logLines, testScope } from './helpers/browser'; const config: ShipConfig = { app: { name: 'Example Game', profile: 'game', privacyPolicyUrl: 'https://example.com/privacy', supportUrl: 'https://example.com/support', website: 'https://example.com', contactEmail: 'support@example.com' }, @@ -190,9 +191,6 @@ let base = ''; beforeAll(() => { server = Bun.serve({ hostname: '127.0.0.1', port: 0, fetch: req => { const path = new URL(req.url).pathname; - // a session cookie (no Expires / Max-Age): it must survive a relaunch of storebound's profile - if (path === '/session-set') return new Response('

set

', { headers: { 'content-type': 'text/html', 'set-cookie': 'sbsession=example; Path=/; HttpOnly' } }); - if (path === '/session-echo') return new Response(`

${/(?:^|; )sbsession=example(?:;|$)/.test(req.headers.get('cookie') ?? '') ? 'kept' : 'lost'}

`, { headers: { 'content-type': 'text/html' } }); const dev = path.match(/^\/playm\/u\/0\/developers(?:\/(\d+)\/app-list)?$/); // playn: the same chooser; every account's app list has a Create app button that opens the Create app form const devNew = path.match(/^\/playn\/u\/0\/developers(?:\/(\d+)\/app-list)?$/); @@ -589,32 +587,7 @@ describe.skipIf(!browser)('sign-in and watching (headless, modeled pages on the expect(await resumeAt(page, plan)).toBeNull(); await page.close(); }, BROWSER_TEST_MS); - - for (const store of ['apple', 'play'] as const) { - test(`${store}: a session cookie survives closing and relaunching storebound's own profile`, async () => { - const dir = mkdtempSync(join(tmpdir(), `storebound-profile-${store}-`)); - const root = join(import.meta.dir, '..'); - // its own Chromium (a persistent profile), launched twice: SLOW, and the launch timeout is raised like the shared one - const opts = { store, profileDir: dir, fresh: true, headless: true, say: () => {}, launchTimeoutMs: LAUNCH_TIMEOUT_MS }; - let b: ConsoleBrowser | null = null; - try { - b = await openConsoleBrowser(root, opts); - await b.page.goto(base + 'session-set'); - await b.close(); // graceful: Chromium writes its cookie store - await b.closed; - b = await openConsoleBrowser(root, opts); - expect(b.pages()).toHaveLength(1); // the restored tab was closed again - await b.page.goto(base + 'session-echo'); - expect(await b.page.textContent('#c')).toBe('kept'); - await b.close(); - await b.closed; - } finally { - // a failed assertion must not leave this Chromium running into the next test - if (b && !b.isClosed()) await capped(b.context.close(), CLOSE_TIMEOUT_MS); - rmSync(dir, { recursive: true, force: true }); - } - }, 2 * LAUNCH_TIMEOUT_MS + SLOW_TEST_MS); // two Chromium launches (each capped) plus the test itself - } + // the session-cookie persistence of storebound's own profile: tests/console-persist.test.ts (real Chrome, own shard) }); // ---- one login, several developer accounts / teams ---- diff --git a/tests/console-persist.test.ts b/tests/console-persist.test.ts new file mode 100644 index 00000000..30ad0213 --- /dev/null +++ b/tests/console-persist.test.ts @@ -0,0 +1,60 @@ +// storebound console fill, mode (c): a session cookie (no Expires / Max-Age) survives closing and relaunching +// storebound's own persistent profile (--restore-last-session plus a graceful close). Launched with the browser kind the +// product uses for that profile (cli/shots/browser.ts, kind "visible"): real Google Chrome, else Playwright's full +// Chromium, headless here so no window appears. Never the headless shell: it does not restore the last session, so the +// cookie is always lost there. Skips (with the reason) only when neither is installed. Slow (two real Chrome launches +// and graceful closes per store), so tools/ci-plan.ts lists it as HEAVY: a shard of its own. +import { afterAll, beforeAll, describe, expect, test } from 'bun:test'; +import { mkdtempSync, rmSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; +import { type ConsoleBrowser, openConsoleBrowser } from '../cli/console/session'; +import { installedBrowsers } from '../cli/shots/browser'; +import { capped, CLOSE_TIMEOUT_MS, LAUNCH_TIMEOUT_MS, persistentBrowser } from './helpers/browser'; + +const real = await persistentBrowser(); +if (!real.label) console.warn(`console-persist tests skipped: ${real.skipWhy}`); +const TEST_MS = 2 * LAUNCH_TIMEOUT_MS + 30_000; // two launches (each capped) plus the test itself + +let server: ReturnType | null = null; +let base = ''; +beforeAll(() => { + server = Bun.serve({ hostname: '127.0.0.1', port: 0, fetch: req => { + const path = new URL(req.url).pathname; + if (path === '/session-set') return new Response('

set

', { headers: { 'content-type': 'text/html', 'set-cookie': 'sbsession=example; Path=/; HttpOnly' } }); + if (path === '/session-echo') return new Response(`

${/(?:^|; )sbsession=example(?:;|$)/.test(req.headers.get('cookie') ?? '') ? 'kept' : 'lost'}

`, { headers: { 'content-type': 'text/html' } }); + return new Response('not found', { status: 404 }); + } }); + base = `http://127.0.0.1:${server.port}/`; +}); +afterAll(() => { server?.stop(true); }); + +describe.skipIf(!real.label)(`storebound's own profile (${real.label ?? 'no real Chrome'})`, () => { + for (const store of ['apple', 'play'] as const) { + test(`${store}: a session cookie survives closing and relaunching storebound's own profile`, async () => { + const dir = mkdtempSync(join(tmpdir(), `storebound-profile-${store}-`)); + const root = join(import.meta.dir, '..'); + const { chromium } = await import('playwright'); + // the resolver's own choice, without a STOREBOUND_CHROME from the environment + const installed = installedBrowsers(chromium, { ...process.env, STOREBOUND_CHROME: undefined }); + const opts = { store, profileDir: dir, fresh: true, headless: true, say: () => {}, launchTimeoutMs: LAUNCH_TIMEOUT_MS, launchDeps: { installed } }; + let b: ConsoleBrowser | null = null; + try { + b = await openConsoleBrowser(root, opts); + await b.page.goto(base + 'session-set'); + await b.close(); // graceful: Chrome writes its cookie store + await b.closed; + b = await openConsoleBrowser(root, opts); + expect(b.pages()).toHaveLength(1); // the restored tab was closed again + await b.page.goto(base + 'session-echo'); + expect(await b.page.textContent('#c')).toBe('kept'); + await b.close(); + await b.closed; + } finally { + // a failed assertion must not leave this Chrome running into the next test + if (b && !b.isClosed()) await capped(b.context.close(), CLOSE_TIMEOUT_MS); + rmSync(dir, { recursive: true, force: true }); + } + }, TEST_MS); + } +}); diff --git a/tests/helpers/browser.ts b/tests/helpers/browser.ts index 7568a95d..89d4879d 100644 --- a/tests/helpers/browser.ts +++ b/tests/helpers/browser.ts @@ -72,6 +72,22 @@ const firstLine = (e: unknown) => (e as Error).message.split('\n')[0]!; /** Playwright's "no such browser" errors (bundled build not downloaded, Chrome channel not installed). */ const notInstalled = (e: unknown) => /Executable doesn't exist|distribution '[^']+' is not found|Failed to launch .* because executable/i.test((e as Error).message); +/** + * The browser the persistent-profile tests (tests/console-persist.test.ts) launch: the same kind `console fill` uses + * for storebound's own profile (cli/shots/browser.ts, kind "visible"): real Google Chrome (channel "chrome"), else + * Playwright's FULL Chromium (channel "chromium"). Never the fast headless shell launchTestBrowser uses: it ignores + * --restore-last-session, so session cookies are dropped on relaunch. `label` is null when neither is installed. + */ +export async function persistentBrowser(): Promise<{ label: string | null; skipWhy: string }> { + const { installedBrowsers, launchCandidates } = await import('../../cli/shots/browser'); + let pw: any = null; + try { pw = (await import('playwright')).chromium; } catch (e) { return { label: null, skipWhy: `playwright is not installed (${firstLine(e)})` }; } + // STOREBOUND_CHROME left out: the tests pin the resolver's choice of an installed Chrome / full Chromium + const c = launchCandidates('visible', installedBrowsers(pw, { ...process.env, STOREBOUND_CHROME: undefined }))[0]; + return c ? { label: c.label, skipWhy: '' } + : { label: null, skipWhy: 'neither Google Chrome nor Playwright\'s full Chromium is installed (the headless shell drops session cookies on relaunch); install Google Chrome or run `bunx playwright install chromium`' }; +} + /** * The contexts and engine runs of the current test. `newPage` opens a page in a fresh context (as browser.newPage * does) and tracks it; `track` adds one opened elsewhere; `settle` (afterEach) closes them all and waits for the runs. diff --git a/tools/ci-plan.ts b/tools/ci-plan.ts index b1b9b8b1..d3edf721 100644 --- a/tools/ci-plan.ts +++ b/tools/ci-plan.ts @@ -13,7 +13,7 @@ export const TIMINGS = join(ROOT, 'node_modules/.cache/storebound-ci/test-timing * suite next to it, so they don't fight for CPU and time out. */ export const HEAVY = [ 'tests/console-fill.test.ts', 'tests/site-overflow.test.ts', 'tests/local-fonts.test.ts', 'tests/glyphs.test.ts', - 'tests/capture-web.test.ts', 'tests/console-session.test.ts', + 'tests/capture-web.test.ts', 'tests/console-session.test.ts', 'tests/console-persist.test.ts', ]; /** Changes that can affect every test: run the whole suite. */