| 1 | import assert from 'node:assert/strict'; |
| 2 | import { createRequire } from 'node:module'; |
| 3 | import { mkdir } from 'node:fs/promises'; |
| 4 | const require = createRequire(import.meta.url); |
| 5 | const { chromium } = require(process.env.CAGE_PLAYWRIGHT_MODULE || 'playwright'); |
| 6 | const origin = process.env.CAGE_SITE_URL || 'http://127.0.0.1:4822/Claude-Cage/'; |
| 7 | const output = process.env.CAGE_SITE_REPORT || '_output/docs-website-20261107/shots'; |
| 8 | await mkdir(output, { recursive: true }); |
| 9 | const browser = await chromium.launch({ headless: true }); |
| 10 | const errors = []; |
| 11 | try { |
| 12 | for (const width of [1280, 390]) { |
| 13 | const page = await browser.newPage({ viewport: { width, height: width === 390 ? 844 : 900 } }); |
| 14 | page.on('pageerror', error => errors.push(error.message)); |
| 15 | await page.goto(origin); |
| 16 | await page.locator('header nav').getByRole('link', { name: '使用文档', exact: true }).click(); |
| 17 | await page.getByRole('heading', { level: 1, name: '从这里开始' }).waitFor(); |
| 18 | assert.equal(await page.locator('body').evaluate(body => body.scrollWidth <= innerWidth + 1), true, `${width}: guide overflows`); |
| 19 | await page.screenshot({ path: `${output}/guide-${width}.png` }); |
| 20 | const search = page.getByRole('searchbox', { name: '搜索文档' }); |
| 21 | await search.fill('多账号'); |
| 22 | await page.locator('#search-list').getByRole('link', { name: '多账号与额度', exact: true }).waitFor(); |
| 23 | assert.equal(await page.locator('#guide-article').isVisible(), false); |
| 24 | await page.screenshot({ path: `${output}/search-${width}.png` }); |
| 25 | await page.locator('#search-list').getByRole('link', { name: '多账号与额度', exact: true }).click(); |
| 26 | await page.getByRole('heading', { level: 1, name: '多账号与额度' }).waitFor(); |
| 27 | await page.getByRole('navigation', { name: '本页目录' }).getByRole('link', { name: '自动分配什么时候发生' }).click(); |
| 28 | assert.ok(decodeURIComponent(new URL(page.url()).hash).includes('自动分配什么时候发生')); |
| 29 | await page.reload(); |
| 30 | await page.getByRole('heading', { level: 2, name: '自动分配什么时候发生' }).waitFor(); |
| 31 | await page.screenshot({ path: `${output}/accounts-${width}.png` }); |
| 32 | await search.fill('不存在的功能xyz987'); |
| 33 | await page.getByRole('status').filter({ hasText: '没有找到相关文档' }).waitFor(); |
| 34 | await page.screenshot({ path: `${output}/empty-${width}.png` }); |
| 35 | await page.getByRole('button', { name: '返回正文' }).click(); |
| 36 | assert.equal(await page.locator('#guide-article').isVisible(), true); |
| 37 | await search.fill('手机 配对'); |
| 38 | await page.locator('#search-list').getByRole('link', { name: '手机连接与远程会话', exact: true }).waitFor(); |
| 39 | await search.press('Escape'); |
| 40 | assert.equal(await search.inputValue(), ''); |
| 41 | if (width === 390) { |
| 42 | await page.evaluate(() => scrollTo(0, 0)); |
| 43 | const menu = page.locator('.guide-mobile-menu'); |
| 44 | const title = page.locator('.guide-title'); |
| 45 | const before = (await title.boundingBox()).y; |
| 46 | await menu.locator('summary').focus(); |
| 47 | await page.keyboard.press('Enter'); |
| 48 | assert.ok((await title.boundingBox()).y > before + 250, 'mobile menu must occupy layout space'); |
| 49 | await page.screenshot({ path: `${output}/menu-${width}.png` }); |
| 50 | await page.keyboard.press('Escape'); |
| 51 | assert.equal(await menu.getAttribute('open'), null); |
| 52 | assert.ok(Math.abs((await title.boundingBox()).y - before) < 2, 'menu must restore layout'); |
| 53 | await menu.locator('summary').press('Enter'); |
| 54 | await menu.getByRole('link', { name: '手机连接与远程会话' }).click(); |
| 55 | await page.getByRole('heading', { level: 1, name: '手机连接与远程会话' }).waitFor(); |
| 56 | assert.equal(await page.locator('body').evaluate(body => body.scrollWidth <= innerWidth + 1), true, 'mobile article overflows'); |
| 57 | } else { |
| 58 | |
| 59 | await page.setViewportSize({ width: 640, height: 450 }); |
| 60 | await page.evaluate(() => scrollTo(0, 0)); |
| 61 | assert.equal(await page.locator('body').evaluate(body => body.scrollWidth <= innerWidth + 1), true, '200% zoom overflows'); |
| 62 | await page.screenshot({ path: `${output}/zoom-200.png` }); |
| 63 | } |
| 64 | await page.close(); |
| 65 | } |
| 66 | const context = await browser.newContext({ javaScriptEnabled: false, viewport: { width: 390, height: 844 } }); |
| 67 | const page = await context.newPage(); |
| 68 | await page.goto(new URL('docs/', origin).href); |
| 69 | await page.getByRole('heading', { level: 1, name: '从这里开始' }).waitFor(); |
| 70 | assert.equal(await page.locator('.guide-search').isVisible(), false); |
| 71 | await page.locator('.guide-mobile-menu summary').click(); |
| 72 | await page.locator('.guide-mobile-menu').getByRole('link', { name: '故障排查', exact: true }).click(); |
| 73 | await page.getByRole('heading', { level: 1, name: '故障排查' }).waitFor(); |
| 74 | await page.screenshot({ path: `${output}/no-javascript.png` }); |
| 75 | await context.close(); |
| 76 | assert.deepEqual(errors, []); |
| 77 | console.log('PASS: homepage entry, desktop/mobile guide, full-text search, no results/recovery, cross-page and hash navigation, inline mobile menu keyboard/layout, 200% zoom, no-JavaScript reading'); |
| 78 | } finally { await browser.close(); } |