support.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Shared plumbing for the web smoke tests (dist location, free port, failure shots).
  2. import { existsSync, mkdirSync } from 'node:fs'
  3. import { createServer } from 'node:net'
  4. import { join } from 'node:path'
  5. import { fileURLToPath } from 'node:url'
  6. import type { Browser, Page } from 'playwright'
  7. /** The built page under test; `pnpm run test:web` rebuilds it before running. */
  8. export const DIST_INDEX = fileURLToPath(new URL('../dist/index.html', import.meta.url))
  9. export const REPO_ROOT = fileURLToPath(new URL('../../..', import.meta.url))
  10. /**
  11. * Browser language a page must advertise to boot into the product's Chinese
  12. * surface: with no stored preference the client derives its initial locale
  13. * from the browser, and Playwright's default browser asks for English.
  14. */
  15. export const ZH_BROWSER_LOCALE = 'zh-CN'
  16. /**
  17. * Open the standard browser-test page advertising English before client boot.
  18. * This keeps role locators and goldens deterministic while leaving the Host
  19. * settings document free to override the provisional browser-derived locale;
  20. * scenarios asserting the Chinese surface advertise
  21. * {@link ZH_BROWSER_LOCALE} instead.
  22. * @param browser - Playwright browser owning the page.
  23. * @param height - Viewport height; width is fixed to the lane baseline.
  24. * @returns the initialized page.
  25. */
  26. export async function newEnglishPage(browser: Browser, height = 1000): Promise<Page> {
  27. return await browser.newPage({ viewport: { width: 1680, height }, locale: 'en-US' })
  28. }
  29. /** Fail loud on a stale checkout instead of testing yesterday's bundle. */
  30. export function requireDist(): void {
  31. if (!existsSync(DIST_INDEX)) {
  32. throw new Error('web app dist not built — run `pnpm run build` from the repository root (`pnpm run test:web` does this first)')
  33. }
  34. }
  35. /** OS-assigned free port, released before use (the spawned `dsh web` needs a concrete --port). */
  36. export function probeFreePort(): Promise<number> {
  37. return new Promise((resolvePort, reject) => {
  38. const probe = createServer()
  39. probe.once('error', reject)
  40. probe.listen(0, '127.0.0.1', () => {
  41. const address = probe.address()
  42. if (address === null || typeof address === 'string') {
  43. probe.close(() => { reject(new Error('port probe returned no address')) })
  44. return
  45. }
  46. probe.close(() => { resolvePort(address.port) })
  47. })
  48. })
  49. }
  50. /**
  51. * Drive the hero's workspace picker through the composed directory dialog
  52. * until the live composer unlocks. A fresh world has no Workspace, so the boot
  53. * lands in the Workspace-trigger view state (startup auto-selection has nothing to
  54. * select); every scenario that types into the composer must connect one
  55. * first. With nothing to list, activating the textarea raises the dialog directly —
  56. * adding a workspace is the picker's only entry. The directory is staged here
  57. * and adopted through the path editor, which is idempotent across the repeated
  58. * connects a scenario may make; creating a folder from inside the dialog (the
  59. * product's other half of the same route) is covered by
  60. * workspace-management.e2e.ts. The default name 'workspace' keeps the session
  61. * header cwd at <root>/workspace, the materialization proof several scenarios
  62. * assert.
  63. * @param page - the page under test.
  64. * @param root - host directory the workspace folder is staged in (the scaffold's `workspaceCwd`).
  65. * @param name - folder name staged and adopted as the workspace.
  66. */
  67. export async function connectFreshWorkspace(page: Page, root: string, name = 'workspace'): Promise<void> {
  68. mkdirSync(join(root, name), { recursive: true })
  69. await page.getByRole('textbox', { name: 'Choose workspace' }).click()
  70. const dialog = page.getByRole('dialog', { name: 'Select Workspace Directory' })
  71. await dialog.waitFor({ timeout: 10_000 })
  72. await dialog.getByRole('button', { name: 'Edit path' }).click()
  73. const pathInput = dialog.getByRole('textbox', { name: 'Edit path' })
  74. await pathInput.fill(join(root, name))
  75. await pathInput.press('Enter')
  76. await dialog.getByRole('button', { name: 'Open', exact: true }).click()
  77. // The pick connected the workspace: the blank session's live composer
  78. // replaces the locked placeholder and enables.
  79. await page.locator('textarea:enabled[placeholder="Describe what you want to build"]')
  80. .waitFor({ timeout: 15_000 })
  81. }
  82. /**
  83. * {@link connectFreshWorkspace} over a page that advertises
  84. * {@link ZH_BROWSER_LOCALE}: the English helper's anchors assume the locale
  85. * most other scenarios boot, so a scenario that deliberately keeps zh needs
  86. * the localized picker copy.
  87. * @param page - the browser page under test.
  88. * @param root - workspace parent directory.
  89. * @param name - directory created under `root` and connected.
  90. */
  91. export async function connectFreshWorkspaceZh(page: Page, root: string, name = 'workspace'): Promise<void> {
  92. mkdirSync(join(root, name), { recursive: true })
  93. await page.getByRole('textbox', { name: '选择工作区' }).click()
  94. const dialog = page.getByRole('dialog', { name: '选择工作区目录' })
  95. await dialog.waitFor({ timeout: 10_000 })
  96. await dialog.getByRole('button', { name: '编辑路径' }).click()
  97. const pathInput = dialog.getByRole('textbox', { name: '编辑路径' })
  98. await pathInput.fill(join(root, name))
  99. await pathInput.press('Enter')
  100. await dialog.getByRole('button', { name: '打开', exact: true }).click()
  101. await page.locator('textarea:enabled[placeholder="描述你想要构建的内容"]')
  102. .waitFor({ timeout: 15_000 })
  103. }
  104. /** Failure evidence goes to the gitignored .artifacts/ (repo convention). */
  105. export async function saveFailureShot(page: Page, name: string): Promise<void> {
  106. const dir = fileURLToPath(new URL('../../../.artifacts', import.meta.url))
  107. mkdirSync(dir, { recursive: true })
  108. try {
  109. await page.screenshot({ path: `${dir}/${name}.png`, fullPage: true })
  110. } catch {
  111. // Best-effort evidence: a dead page/browser at failure time must not mask the real assertion error.
  112. }
  113. }
  114. /**
  115. * The conversation engine's Context key format, restated here rather than
  116. * imported: these specs live in the Host compiler aggregate, which must not
  117. * reach the Client plane. The engine's own copy is
  118. * `conversationContextKey` in ui-conversation; a drift between them makes
  119. * the key miss its rendered node, so the assertion fails loudly.
  120. * @param kind - Definition kind.
  121. * @param id - Definition-local business identity.
  122. * @returns the engine-owned Context key.
  123. */
  124. export function conversationContextKey(kind: string, id: string): string {
  125. return `${kind.length}:${kind}${id}`
  126. }