// Browser review checks for the offline prototype; not a platform/API test suite. import assert from 'node:assert/strict'; import fs from 'node:fs/promises'; import path from 'node:path'; import { fileURLToPath, pathToFileURL } from 'node:url'; import { chromium } from 'playwright'; const root = path.dirname(fileURLToPath(import.meta.url)); const output = path.resolve(root, '../../test-results/subscription-prototype'); await fs.rm(output, { recursive: true, force: true }); await fs.mkdir(output, { recursive: true }); const browser = await chromium.launch({ headless: true, executablePath: chromium.executablePath() }); const context = await browser.newContext({ reducedMotion: 'reduce', acceptDownloads: false }); const page = await context.newPage(); const errors = [], externalRequests = [], checks = []; page.on('pageerror', (error) => errors.push(error.message)); page.on('request', (request) => { if (/^https?:/.test(request.url())) externalRequests.push(request.url()); }); const base = pathToFileURL(path.join(root, 'index.html')).href; const open = async (query = '') => { await page.goto(base + query); await page.evaluate(() => document.fonts.ready); }; const snapshot = () => page.evaluate(() => ({ plan: state.plan, access: state.access, containers: state.containers, job: state.job, started: state.trialStartedAt, expires: state.trialExpiresAt, quota: Demo.limit(state), reserved: Demo.reserved(state), application: state.application, view: state.view })); async function review() { if (!(await page.locator('#reviewControls').isVisible())) await page.locator('#reviewToggle').click(); } async function event(name) { await review(); await page.locator(`[data-event="${name}"]`).click(); } async function noOverflow(label) { const values = await page.evaluate(() => ({ outer: document.documentElement.scrollWidth <= innerWidth, main: document.getElementById('main').scrollWidth <= document.getElementById('main').clientWidth })); assert(values.outer && values.main, 'Horizontal overflow: ' + label + ' ' + JSON.stringify(values)); } const capture = (name) => page.screenshot({ path: path.join(output, name + '.png'), animations: 'disabled' }); async function createTrial() { await page.locator('#main [data-action="trial"]').first().click(); assert.equal(await page.locator('#dialog').evaluate((element) => element.open), true); await page.locator('#createForm button[type="submit"]').click(); } try { for (const width of [320, 360, 390, 430, 768, 1024, 1440, 1920]) { await page.setViewportSize({ width, height: width < 768 ? 844 : 960 }); for (const scenario of ['new', 'waiting', 'failed', 'advanced', 'quota', 'expired', 'enterprise']) { await open('?scenario=' + scenario); await noOverflow(width + '/' + scenario); if ([390,1440].includes(width)) await capture(scenario + '-' + width); if (width < 768) { const buttons = await page.locator('#main .button').evaluateAll((elements) => elements.map((element) => ({ height: element.getBoundingClientRect().height }))); assert(buttons.every((button) => button.height >= 44), 'Mobile primary actions too small'); const bodySize = await page.evaluate(() => parseFloat(getComputedStyle(document.body).fontSize)); assert(bodySize >= 14, 'Mobile body text below 14px'); const heading = await page.locator('#main h1').first().evaluate((element) => parseFloat(getComputedStyle(element).fontSize)).catch(() => 0); assert(heading === 0 || heading <= 22, 'Mobile h1 too large for narrow viewport'); } } await open('?scenario=advanced&screen=access'); await noOverflow(width + '/credentials'); if ([390,1440].includes(width)) await capture('credentials-' + width); checks.push('Layout and scenarios at ' + width + 'px'); } await open(); const perkText = await page.locator('#main .plan-card').evaluateAll((cards) => cards.map((card) => card.textContent)); assert.equal(perkText.length, 3, 'Three plan cards expected'); assert(perkText.every((text) => text.includes('2 核 4G')), 'Every plan card states the 2-core 4G spec (AC-21.1)'); checks.push('All three plan cards state the 2-core 4G baseline spec'); await page.setViewportSize({ width: 390, height: 844 }); await open(); await createTrial(); const claimed = await snapshot(); assert.equal(claimed.access, 'provisioning'); assert.equal(claimed.reserved, 1); assert.equal(claimed.started, null); await page.locator('#main [data-action="check"]').click(); assert.equal((await snapshot()).job.stage, 0, 'Check progress cannot fake a server completion'); await event('advance'); await event('advance'); const beforeFailure = await snapshot(); await event('fail'); await page.locator('#main [data-action="retry"]').click(); const retried = await snapshot(); assert.equal(retried.job.id, claimed.job.id); assert.equal(retried.reserved, 1); assert.equal(retried.job.stage, beforeFailure.job.stage); assert.equal(retried.job.attempt, 2); await event('offline'); assert.equal(await page.locator('#main [data-action="check"]').isDisabled(), true); assert.equal((await snapshot()).job.id, claimed.job.id); await event('offline'); await event('slow'); assert.equal((await snapshot()).job.status, 'provisioning'); while ((await snapshot()).job.status !== 'ready') await event('advance'); const ready = await snapshot(); assert.equal(ready.containers.length, 1); assert.equal(ready.access, 'active'); assert.equal(ready.reserved, 0); assert.equal(ready.expires - ready.started, 48 * 3600000); await page.locator('#reviewToggle').click(); await capture('ready-390'); await event('expire'); const expired = await snapshot(); assert.equal(expired.access, 'expired'); assert.equal(expired.containers[0].id, ready.containers[0].id); await page.locator('#main [data-action="apply"][data-plan="starter"]').click(); await page.locator('#applyForm button[type="submit"]').click(); assert.equal((await snapshot()).access, 'expired', 'Intent is not payment confirmation'); await event('approve'); const restored = await snapshot(); assert.equal(restored.access, 'active'); assert.equal(restored.plan, 'starter'); assert.equal(restored.containers.length, 1); assert.equal(restored.containers[0].id, ready.containers[0].id); assert.equal(restored.started, ready.started); assert.equal(restored.expires, ready.expires, 'Paid restoration cannot reset the old trial'); checks.push('Trial claim → real-event simulation → same-job retry/offline/delay → ready 48h → expiry → same-container paid restoration'); await open('?scenario=quota'); assert.equal(await page.locator('#main [data-action="create"]').isDisabled(), true); await page.locator('#main [data-action="addon"]').click(); await page.locator('#addonForm button[type="submit"]').click(); assert.equal((await snapshot()).quota, 3); await event('approve'); assert.equal((await snapshot()).quota, 4); await page.locator('#main .page-heading [data-action="create"]').click(); await page.locator('#createForm button[type="submit"]').click(); assert.equal((await snapshot()).access, 'active', 'Existing containers remain usable during a new job'); assert.equal((await snapshot()).containers.length, 3); checks.push('Quota 3, manual addon confirmation to 4, existing 3 containers remain usable while creating a new one'); await open('?scenario=advanced&screen=access'); assert.equal(await page.locator('.masked-value').count(), 9); await page.locator('#main [data-action="credential"]').first().click(); assert.equal(await page.locator('#credentialResult').isVisible(), false); await page.locator('[data-confirm-credential]').click(); assert.match(await page.locator('.demo-secret').textContent(), /demo-agent/); await page.keyboard.press('Escape'); assert.equal(await page.locator('#dialog').evaluate((element) => element.open), false); checks.push('Nine masked demo fields including userMxId/homeserver; explicit second confirmation; modal Escape'); await open('?scenario=advanced'); await page.locator('#main [data-action="settings"]').first().click(); await page.locator('#renameInput').fill('新名字