support.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. /** Failure evidence goes to the gitignored .artifacts/ (repo convention). */
  31. export async function saveFailureShot(page: Page, name: string): Promise<void> {
  32. const dir = fileURLToPath(new URL('../../../.artifacts', import.meta.url))
  33. mkdirSync(dir, { recursive: true })
  34. try {
  35. await page.screenshot({ path: `${dir}/${name}.png`, fullPage: true })
  36. } catch {
  37. // Best-effort evidence: a dead page/browser at failure time must not mask the real assertion error.
  38. }
  39. }