support.ts 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 { fileURLToPath } from 'node:url'
  5. import type { Page } from 'playwright'
  6. /** The built page under test; `pnpm run test:web` rebuilds it before running. */
  7. export const DIST_INDEX = fileURLToPath(new URL('../dist/index.html', import.meta.url))
  8. export const REPO_ROOT = fileURLToPath(new URL('../../..', import.meta.url))
  9. /** Fail loud on a stale checkout instead of testing yesterday's bundle. */
  10. export function requireDist(): void {
  11. if (!existsSync(DIST_INDEX)) {
  12. throw new Error('web app dist not built — run `pnpm --filter @deepseek-ai/dsh-frontend build` (pnpm run test:web does this first)')
  13. }
  14. }
  15. /** OS-assigned free port, released before use (the spawned `dsh web` needs a concrete --port). */
  16. export function probeFreePort(): Promise<number> {
  17. return new Promise((resolvePort, reject) => {
  18. const probe = createServer()
  19. probe.once('error', reject)
  20. probe.listen(0, '127.0.0.1', () => {
  21. const address = probe.address()
  22. if (address === null || typeof address === 'string') {
  23. probe.close(() => { reject(new Error('port probe returned no address')) })
  24. return
  25. }
  26. probe.close(() => { resolvePort(address.port) })
  27. })
  28. })
  29. }
  30. /**
  31. * Drive the hero's workspace picker through its create-by-name dialog until
  32. * the live composer unlocks. A fresh world has no Workspace, so the boot
  33. * lands in the locked view state (startup auto-selection has nothing to
  34. * select); every scenario that types into the composer must connect one
  35. * first. The default name 'workspace' keeps the session header cwd at
  36. * <workspaceRoot>/workspace — the materialization proof several scenarios
  37. * assert.
  38. * @param page - the page under test.
  39. * @param name - workspace name typed into the create dialog.
  40. */
  41. export async function connectFreshWorkspace(page: Page, name = 'workspace'): Promise<void> {
  42. await page.getByRole('button', { name: 'Choose workspace' }).click()
  43. await page.getByRole('menuitem', { name: 'Create a new workspace' }).click()
  44. const dialog = page.getByRole('dialog', { name: 'Create a new workspace' })
  45. await dialog.waitFor({ timeout: 10_000 })
  46. await dialog.getByLabel('New workspace name').fill(name)
  47. await dialog.getByRole('button', { name: 'Create workspace' }).click()
  48. // The pick connected the workspace: the blank session's live composer
  49. // replaces the locked placeholder and enables.
  50. await page.locator('textarea:enabled[placeholder="Describe what you want to build"]')
  51. .waitFor({ timeout: 15_000 })
  52. }
  53. /** Failure evidence goes to the gitignored .artifacts/ (repo convention). */
  54. export async function saveFailureShot(page: Page, name: string): Promise<void> {
  55. const dir = fileURLToPath(new URL('../../../.artifacts', import.meta.url))
  56. mkdirSync(dir, { recursive: true })
  57. try {
  58. await page.screenshot({ path: `${dir}/${name}.png`, fullPage: true })
  59. } catch {
  60. // Best-effort evidence: a dead page/browser at failure time must not mask the real assertion error.
  61. }
  62. }