access-confirmation.e2e.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 { saveFailureShot } from './support.ts'
  15. /**
  16. * connectFreshWorkspace twin over the product default Chinese locale (the
  17. * shared helper's anchors assume the English page every other scenario
  18. * boots; this scenario deliberately keeps zh, so the localized picker
  19. * copy is the anchor set).
  20. */
  21. async function connectFreshWorkspaceZh(page: Page, name = 'workspace'): Promise<void> {
  22. await page.getByRole('button', { name: '选择工作区' }).click()
  23. await page.getByRole('menuitem', { name: '新建工作区' }).click()
  24. const dialog = page.getByRole('dialog', { name: '新建工作区' })
  25. await dialog.waitFor({ timeout: 10_000 })
  26. await dialog.getByLabel('新工作区名称').fill(name)
  27. await dialog.getByRole('button', { name: '创建工作区' }).click()
  28. await page.locator('textarea:enabled[placeholder="描述你想要构建的内容"]')
  29. .waitFor({ timeout: 15_000 })
  30. }
  31. const SNAPSHOT_DIR = fileURLToPath(new URL('./snapshots/access-confirmation', import.meta.url))
  32. const UI_EXPECTED = join(SNAPSHOT_DIR, 'ui.expected.md')
  33. const MODE = webSnapshotMode()
  34. describe('web e2e: Full access confirmation', () => {
  35. let scaffold: WebScaffold
  36. let browser: Browser
  37. let page: Page
  38. let tripwire: ReturnType<typeof watchConsole>
  39. beforeAll(async () => {
  40. scaffold = await launchWebScaffold({})
  41. // CI uses Playwright's pinned browser. A developer may point this one
  42. // scenario at an installed Chromium when the matching browser download
  43. // is temporarily unavailable.
  44. const executablePath = process.env.DSH_PLAYWRIGHT_EXECUTABLE_PATH
  45. browser = await chromium.launch(executablePath === undefined ? {} : { executablePath })
  46. // Keep the product default Chinese locale: the golden pins the actual
  47. // registered dictionary rather than a test-local translation callback.
  48. page = await browser.newPage({ viewport: { width: 1680, height: 1000 } })
  49. tripwire = watchConsole(page)
  50. await page.goto(scaffold.baseUrl, { waitUntil: 'load' })
  51. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  52. await connectFreshWorkspaceZh(page)
  53. }, 120_000)
  54. afterAll(async () => {
  55. await browser?.close()
  56. await scaffold?.close()
  57. })
  58. it('requires acknowledgement before the composer picker can enable Full access', async () => {
  59. onTestFailed(() => saveFailureShot(page, 'web-e2e-full-access-confirmation'))
  60. const access = page.locator('button[aria-label^="访问模式"]').first()
  61. await access.waitFor({ timeout: 10_000 })
  62. // Normalize the starting preset through the real command path. The
  63. // shipped web config may already start at Full access.
  64. if ((await access.getAttribute('aria-label'))?.endsWith('Full access') === true) {
  65. await access.click()
  66. await page.getByRole('menuitem', { name: 'Workspace Write' }).click()
  67. await expect.poll(() => access.getAttribute('aria-label'), { timeout: 10_000 })
  68. .toBe('访问模式,当前:Workspace Write')
  69. }
  70. await access.click()
  71. await page.getByRole('menuitem', { name: 'Full access' }).click()
  72. const dialog = page.getByRole('dialog', { name: '确认启用 Full access?' })
  73. await dialog.waitFor({ timeout: 10_000 })
  74. const enable = dialog.getByRole('button', { name: '启用 Full access' })
  75. expect(await enable.isDisabled()).toBe(true)
  76. // The modal is in this page's body (not a native/new window) and escapes
  77. // the sticky composer's stacking context.
  78. expect(await dialog.evaluate(node => node.parentElement?.parentElement === document.body)).toBe(true)
  79. const snapshot = await captureStableAria(page, '[role="dialog"]', scaffold.workspaceCwd)
  80. await compareOrRefreshGolden(UI_EXPECTED, snapshot, MODE)
  81. await dialog.getByRole('checkbox', { name: '我已了解风险,并愿意继续' }).check()
  82. expect(await enable.isEnabled()).toBe(true)
  83. await enable.click()
  84. await expect.poll(() => access.getAttribute('aria-label'), { timeout: 10_000 })
  85. .toBe('访问模式,当前:Full access')
  86. expect(await dialog.count()).toBe(0)
  87. expect(tripwire.pageErrors).toEqual([])
  88. }, 60_000)
  89. it('keeps its snapshot inventory closed', async () => {
  90. expect(tripwire.warnings).toEqual([])
  91. await assertFixtureInventory(SNAPSHOT_DIR, ['ui.expected.md'])
  92. })
  93. })