access-confirmation.e2e.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Web e2e scenario: every visible permission picker gates Full access behind
  2. // the same locale-aware, in-page risk confirmation. Zero model calls: the
  3. // scenario boots the shipped Web composition and exercises the real
  4. // permission projection, client command path, HTTP RPC, and pushed update.
  5. import { fileURLToPath } from 'node:url'
  6. import { join } from 'node:path'
  7. import type { Browser, Page } from 'playwright'
  8. import { chromium } from 'playwright'
  9. import { afterAll, beforeAll, describe, expect, it, onTestFailed } from 'vitest'
  10. import {
  11. assertFixtureInventory, captureStableAria, compareOrRefreshGolden,
  12. launchWebScaffold, watchConsole, webSnapshotMode, type WebScaffold,
  13. } from './scaffold.ts'
  14. import { ZH_BROWSER_LOCALE, connectFreshWorkspaceZh, saveFailureShot } from './support.ts'
  15. const SNAPSHOT_DIR = fileURLToPath(new URL('./expected/access-confirmation', import.meta.url))
  16. const UI_EXPECTED = join(SNAPSHOT_DIR, 'ui.expected.md')
  17. const MODE = webSnapshotMode()
  18. describe('web e2e: Full access confirmation', () => {
  19. let scaffold: WebScaffold
  20. let browser: Browser
  21. let page: Page
  22. let tripwire: ReturnType<typeof watchConsole>
  23. beforeAll(async () => {
  24. scaffold = await launchWebScaffold({})
  25. // CI uses Playwright's pinned browser. A developer may point this one
  26. // scenario at an installed Chromium when the matching browser download
  27. // is temporarily unavailable.
  28. const executablePath = process.env.DSH_PLAYWRIGHT_EXECUTABLE_PATH
  29. browser = await chromium.launch(executablePath === undefined ? {} : { executablePath })
  30. // Keep the Chinese surface via {@link ZH_BROWSER_LOCALE}: the golden pins
  31. // the actual registered dictionary rather than a test-local translation
  32. // callback.
  33. page = await browser.newPage({ viewport: { width: 1680, height: 1000 }, locale: ZH_BROWSER_LOCALE })
  34. tripwire = watchConsole(page)
  35. await page.goto(scaffold.authenticatedUrl, { waitUntil: 'load' })
  36. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  37. await connectFreshWorkspaceZh(page, scaffold.workspaceCwd)
  38. }, 120_000)
  39. afterAll(async () => {
  40. await browser?.close()
  41. await scaffold?.close()
  42. })
  43. it('requires acknowledgement before the composer picker can enable Full access', async () => {
  44. onTestFailed(() => saveFailureShot(page, 'web-e2e-full-access-confirmation'))
  45. const access = page.locator('button[aria-label^="访问模式"]').first()
  46. await access.waitFor({ timeout: 10_000 })
  47. expect(await access.getAttribute('aria-label')).toBe('访问模式,当前:工作区内修改')
  48. await access.click()
  49. await page.getByRole('menuitem', { name: '完全权限' }).click()
  50. const dialog = page.getByRole('dialog', { name: '确认启用完全权限?' })
  51. await dialog.waitFor({ timeout: 10_000 })
  52. const enable = dialog.getByRole('button', { name: '启用完全权限' })
  53. expect(await enable.isDisabled()).toBe(true)
  54. // The modal is in this page's body (not a native/new window) and escapes
  55. // the sticky composer's stacking context.
  56. expect(await dialog.evaluate(node => node.parentElement?.parentElement === document.body)).toBe(true)
  57. const snapshot = await captureStableAria(page, '[role="dialog"]', scaffold.workspaceCwd)
  58. await compareOrRefreshGolden(UI_EXPECTED, snapshot, MODE)
  59. await dialog.getByRole('checkbox', { name: '我已了解风险,并愿意继续' }).check()
  60. expect(await enable.isEnabled()).toBe(true)
  61. await enable.click()
  62. await expect.poll(() => access.getAttribute('aria-label'), { timeout: 10_000 })
  63. .toBe('访问模式,当前:完全权限')
  64. expect(await dialog.count()).toBe(0)
  65. expect(tripwire.pageErrors).toEqual([])
  66. }, 60_000)
  67. it('keeps its snapshot inventory closed', async () => {
  68. expect(tripwire.warnings).toEqual([])
  69. await assertFixtureInventory(SNAPSHOT_DIR, ['ui.expected.md'])
  70. })
  71. })