| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- // 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('新名字 <script>');
- await page.locator('#renameForm button[type="submit"]').click();
- assert.equal(await page.locator('.container-head h3').first().textContent(), '新名字 <script>');
- assert.equal(await page.locator('#main script').count(), 0);
- checks.push('Container rename is escaped and stays in memory');
- await open('?screen=enterprise');
- await event('join');
- assert.equal((await snapshot()).quota, 5); assert.equal((await snapshot()).plan, 'enterprise');
- checks.push('Enterprise invitation confirmation grants a shared quota of 5 in the demo');
- await open('?scenario=advanced');
- const personalBeforeJoin = await snapshot();
- await page.locator('#desktopNav [data-view="enterprise"]').evaluate((element) => element.click());
- await event('join');
- const companyBefore = await snapshot();
- assert.equal(companyBefore.quota, 5);
- await page.locator('[data-subject="personal"]').click();
- const personalAfterJoin = await snapshot();
- assert.equal(personalAfterJoin.plan, 'advanced');
- assert.deepEqual(personalAfterJoin.containers, personalBeforeJoin.containers);
- await page.locator('[data-subject="company"]').click();
- assert.deepEqual((await snapshot()).containers, companyBefore.containers);
- checks.push('Joining/switching enterprise preserves independent personal plan, containers and shared company quota');
- await open('?scenario=enterprise&screen=plans');
- await page.locator('#main [data-action="apply"][data-plan="starter"]').click();
- await page.locator('#applyForm button[type="submit"]').click();
- await event('approve');
- assert.equal((await snapshot()).plan, 'starter');
- assert.equal((await snapshot()).containers.length, 0);
- await page.locator('[data-subject="company"]').click();
- assert.equal((await snapshot()).quota, 5);
- assert.equal((await snapshot()).containers.length, 2);
- checks.push('Buying a personal plan from a company context does not transfer or downgrade enterprise assets');
- await open('?scenario=waiting');
- assert.equal(await page.locator('[role="progressbar"]').getAttribute('aria-valuenow'), '50');
- assert.equal((await snapshot()).job.completed, 3);
- assert.equal(await page.locator('#main .job-step').count(), 0);
- assert.equal(await page.locator('#main').getByText('正在开通', { exact:true }).count(), 1);
- checks.push('Simple current status and true 50% progress; internal stage list remains out of product UI');
- for (const [width,height] of [[320,568],[390,600],[760,420]]) {
- await page.setViewportSize({ width,height }); await open();
- await page.locator('#main [data-action="trial"]').click();
- const box = await page.locator('#dialog').boundingBox();
- assert(box.y >= 0 && box.y + box.height <= height);
- await page.keyboard.press('Tab');
- assert.equal(await page.evaluate(() => document.getElementById('dialog').contains(document.activeElement)), true);
- await page.keyboard.press('Escape');
- }
- checks.push('Short-screen modal fit, scroll, keyboard focus containment');
- await page.setViewportSize({ width:390,height:844 }); await open('?theme=dark&scenario=waiting'); await noOverflow('dark waiting'); await capture('waiting-dark-390');
- await page.setViewportSize({ width:1440,height:960 }); await open('?theme=dark'); await noOverflow('dark plans'); await capture('plans-dark-1440');
- checks.push('Light and dark theme');
- assert.deepEqual(externalRequests, [], 'Prototype must not call any real platform or resource');
- assert.deepEqual(errors, []);
- const report = { success:true, scope:'offline prototype review only; no real API/container/timing verification', checks, browserErrors:errors, externalRequests };
- await fs.writeFile(path.join(output,'results.json'),JSON.stringify(report,null,2));
- console.log(JSON.stringify(report,null,2));
- } finally { await browser.close(); }
|