Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

fix(ci): harden UI E2E execution and artifact closure (#562)

Generf6db236b3484
diff --git a/.github/workflows/e2e-ui.yml b/.github/workflows/e2e-ui.yml
index f26ab84f7..c5d9f2baa 100644
--- a/.github/workflows/e2e-ui.yml
+++ b/.github/workflows/e2e-ui.yml
@@ -9,6 +9,7 @@ on:
permissions:
contents: read
+ actions: read
concurrency:
group: e2e-ui-${{ github.event.pull_request.number || github.event.merge_group.head_sha || github.ref }}
@@ -57,6 +58,16 @@ jobs:
BETTER_AUTH_URL: http://localhost:3000
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
+ - env:
+ E2E_SPECS: ${{ toJSON(matrix.specs) }}
+ run: |
+ node scripts/ci/e2e-shard-artifacts.mjs write-manifest \
+ --output src/web/e2e-shard-manifest/shard-manifest.json \
+ --run-id "${{ github.run_id }}" \
+ --attempt "${{ github.run_attempt }}" \
+ --sha "${{ github.sha }}" \
+ --shard "${{ matrix.shard }}" \
+ --total "${{ matrix.total }}"
- uses: pnpm/action-setup@0977fd99725f1db4007ccb2928dbb4e90d06cc86 # v6
- uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7
with:
@@ -69,32 +80,16 @@ jobs:
run: |
mapfile -t specs < <(node -e 'for (const spec of JSON.parse(process.env.E2E_SPECS)) console.log(spec)')
pnpm --filter @alook/web exec playwright test "${specs[@]}"
- - if: ${{ !cancelled() && matrix.shard == 1 }}
- run: |
- test_package=$(realpath src/web/node_modules/@playwright/test)
- test_node_modules=$(dirname "$(dirname "$test_package")")
- playwright_package=$(realpath "$test_node_modules/playwright")
- playwright_node_modules=$(dirname "$playwright_package")
- core_package=$(realpath "$playwright_node_modules/playwright-core")
- runtime=src/web/playwright-merge-runtime/node_modules
- mkdir -p "$runtime/@playwright"
- cp -R "$test_package" "$runtime/@playwright/test"
- cp -R "$playwright_package" "$runtime/playwright"
- cp -R "$core_package" "$runtime/playwright-core"
- - if: ${{ !cancelled() && matrix.shard == 1 }}
- uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7
- with:
- name: playwright-merge-runtime-${{ github.run_id }}-${{ github.run_attempt }}
- path: src/web/playwright-merge-runtime/
- if-no-files-found: error
- retention-days: 1
- if: ${{ !cancelled() }}
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7
with:
- name: blob-report-${{ github.run_id }}-${{ github.run_attempt }}-${{ matrix.shard }}
- path: src/web/blob-report/
+ name: blob-report-${{ github.run_id }}-${{ matrix.shard }}
+ path: |
+ src/web/blob-report/
+ src/web/e2e-shard-manifest/
if-no-files-found: error
- retention-days: 1
+ overwrite: true
+ retention-days: 7
- if: failure()
continue-on-error: true
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7
@@ -113,20 +108,42 @@ jobs:
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- - uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8
+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
+ - uses: pnpm/action-setup@0977fd99725f1db4007ccb2928dbb4e90d06cc86 # v6
+ - uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7
with:
- pattern: blob-report-${{ github.run_id }}-${{ github.run_attempt }}-*
- path: all-blob-reports
- merge-multiple: true
+ node-version: 22
+ cache: pnpm
+ - run: pnpm install --frozen-lockfile
- uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8
with:
- name: playwright-merge-runtime-${{ github.run_id }}-${{ github.run_attempt }}
- path: playwright-merge-runtime
- - run: node playwright-merge-runtime/node_modules/@playwright/test/cli.js merge-reports --reporter html all-blob-reports
+ pattern: blob-report-${{ github.run_id }}-*
+ path: downloaded-blob-reports
+ merge-multiple: false
+ - env:
+ E2E_MATRIX: ${{ needs.scope.outputs.e2e_matrix }}
+ GITHUB_TOKEN: ${{ github.token }}
+ run: |
+ node scripts/ci/e2e-shard-artifacts.mjs verify-artifacts \
+ --root downloaded-blob-reports \
+ --output verified-blob-reports \
+ --run-id "${{ github.run_id }}" \
+ --attempt "${{ github.run_attempt }}" \
+ --sha "${{ github.sha }}"
+ - env:
+ PLAYWRIGHT_HTML_OUTPUT_DIR: ${{ github.workspace }}/playwright-report
+ PLAYWRIGHT_JSON_OUTPUT_FILE: ${{ github.workspace }}/merged-report.json
+ run: pnpm --filter @alook/web exec playwright merge-reports --reporter html,json "${{ github.workspace }}/verified-blob-reports"
+ - env:
+ E2E_MATRIX: ${{ needs.scope.outputs.e2e_matrix }}
+ run: node scripts/ci/e2e-shard-artifacts.mjs verify-merged --report merged-report.json
- uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7
with:
name: playwright-report-${{ github.run_id }}-${{ github.run_attempt }}
- path: playwright-report/
+ path: |
+ playwright-report/
+ merged-report.json
+ if-no-files-found: error
retention-days: 14
ui-e2e-gate:
diff --git a/scripts/ci/e2e-selector-contract.test.ts b/scripts/ci/e2e-selector-contract.test.ts
new file mode 100644
index 000000000..ed0df16fd
--- /dev/null
+++ b/scripts/ci/e2e-selector-contract.test.ts
@@ -0,0 +1,60 @@
+import { readdirSync, readFileSync } from "node:fs"
+import { extname, resolve } from "node:path"
+import { describe, expect, it } from "vitest"
+
+const e2eRoot = resolve(import.meta.dirname, "../../src/web/src/test/e2e-ui")
+
+function sourceFiles(directory: string): string[] {
+ return readdirSync(directory, { withFileTypes: true }).flatMap((entry) => {
+ const path = resolve(directory, entry.name)
+ if (entry.isDirectory()) return sourceFiles(path)
+ return [".ts", ".tsx"].includes(extname(entry.name)) ? [path] : []
+ })
+}
+
+function selectorViolations(source: string): string[] {
+ const rules = [
+ { label: "literal getByTestId", pattern: /getByTestId\s*\(\s*[`"']/ },
+ {
+ label: "hard-coded data-testid selector",
+ pattern: /data-testid(?:[\^$*~|])?\s*=\s*\\?["'](?!\$\{)/,
+ },
+ {
+ label: "hard-coded unquoted data-testid selector",
+ pattern: /data-testid(?:[\^$*~|])?\s*=\s*(?!\\?["']|\$\{)[^\s\]]+/,
+ },
+ { label: "bare data-testid selector", pattern: /\[\s*data-testid\s*\]/ },
+ ]
+ return rules.filter(({ pattern }) => pattern.test(source)).map(({ label }) => label)
+}
+
+describe("E2E selector contract", () => {
+ it("derives every test ID from the canonical registry", () => {
+ const violations = sourceFiles(e2eRoot).flatMap((path) => {
+ const source = readFileSync(path, "utf8")
+ return selectorViolations(source).map((label) => `${path.slice(e2eRoot.length + 1)}: ${label}`)
+ })
+
+ expect(violations).toEqual([])
+ })
+
+ it.each([
+ ["arbitrary quoted value", `page.locator('[data-testid="rogue-id"]')`],
+ ["arbitrary escaped value", `document.querySelector("[data-testid=\\"rogue-id\\"]")`],
+ ["arbitrary prefix value", `page.locator('[data-testid^="rogue-"]')`],
+ ["arbitrary unquoted value", `page.locator('[data-testid=rogue-id]')`],
+ ["bare attribute lookup", `page.locator('[data-testid]')`],
+ ])("rejects %s", (_label, fixture) => {
+ expect(selectorViolations(fixture)).not.toEqual([])
+ })
+
+ it("allows canonical registry values to be interpolated or passed into evaluate", () => {
+ const fixture = `
+ page.getByTestId(tid.messageScroller)
+ page.locator(\`[data-testid="\${tid.messageScroller}"]\`)
+ page.evaluate((id) => document.querySelector(\`[data-testid="\${id}"]\`), tid.messageScroller)
+ `
+
+ expect(selectorViolations(fixture)).toEqual([])
+ })
+})
diff --git a/scripts/ci/e2e-shard-artifacts.mjs b/scripts/ci/e2e-shard-artifacts.mjs
new file mode 100644
index 000000000..2d6eced95
--- /dev/null
+++ b/scripts/ci/e2e-shard-artifacts.mjs
@@ -0,0 +1,306 @@
+import {
+ copyFileSync,
+ mkdirSync,
+ readFileSync,
+ readdirSync,
+ rmSync,
+ statSync,
+ writeFileSync,
+} from "node:fs"
+import { basename, join, relative, resolve } from "node:path"
+import { fileURLToPath } from "node:url"
+
+export const SHARD_ARTIFACT_PREFIX = "blob-report"
+export const SHARD_MANIFEST_VERSION = 1
+
+function parsePositiveInteger(value, label) {
+ const parsed = Number(value)
+ if (!Number.isInteger(parsed) || parsed < 1) throw new Error(`${label} must be a positive integer`)
+ return parsed
+}
+
+export function normalizeSpecPath(path) {
+ const normalized = path.replaceAll("\\", "/").replace(/^\.\//, "")
+ const marker = "src/test/e2e-ui/"
+ const markerIndex = normalized.indexOf(marker)
+ return markerIndex >= 0 ? normalized.slice(markerIndex + marker.length) : normalized
+}
+
+export function expectedMatrix(matrixInput) {
+ const matrix = typeof matrixInput === "string" ? JSON.parse(matrixInput) : matrixInput
+ if (!matrix || !Array.isArray(matrix.include) || matrix.include.length === 0) {
+ throw new Error("E2E matrix must contain a non-empty include array")
+ }
+
+ const shards = new Map()
+ const owners = new Map()
+ let total = null
+ for (const entry of matrix.include) {
+ const shard = parsePositiveInteger(entry.shard, "matrix shard")
+ const entryTotal = parsePositiveInteger(entry.total, "matrix total")
+ if (total == null) total = entryTotal
+ if (total !== entryTotal) throw new Error("E2E matrix totals must agree")
+ if (shards.has(shard)) throw new Error(`duplicate matrix shard ${shard}`)
+ if (!Array.isArray(entry.specs) || entry.specs.length === 0) {
+ throw new Error(`matrix shard ${shard} must contain specs`)
+ }
+ const specs = entry.specs.map(normalizeSpecPath).sort()
+ if (new Set(specs).size !== specs.length) throw new Error(`duplicate spec within matrix shard ${shard}`)
+ for (const spec of specs) {
+ const owner = owners.get(spec)
+ if (owner) throw new Error(`spec ${spec} is assigned to shards ${owner} and ${shard}`)
+ owners.set(spec, shard)
+ }
+ shards.set(shard, { shard, total: entryTotal, specs })
+ }
+ if (shards.size !== total) throw new Error(`matrix declares ${total} shards but contains ${shards.size}`)
+ for (let shard = 1; shard <= total; shard += 1) {
+ if (!shards.has(shard)) throw new Error(`matrix is missing shard ${shard}`)
+ }
+ return { total, shards, specs: [...owners.keys()].sort() }
+}
+
+export function createShardManifest({ runId, attempt, sha, shard, total, specs }) {
+ const manifest = {
+ version: SHARD_MANIFEST_VERSION,
+ runId: String(runId),
+ attempt: parsePositiveInteger(attempt, "manifest attempt"),
+ sha: String(sha),
+ shard: parsePositiveInteger(shard, "manifest shard"),
+ total: parsePositiveInteger(total, "manifest total"),
+ specs: specs.map(normalizeSpecPath).sort(),
+ }
+ if (!manifest.runId) throw new Error("manifest runId is required")
+ if (!manifest.sha) throw new Error("manifest sha is required")
+ return manifest
+}
+
+export function resolveExecutedShards({ jobs, runStartedAt, expectedTotal }) {
+ const startedAt = Date.parse(runStartedAt)
+ if (!Number.isFinite(startedAt)) throw new Error(`invalid run_started_at: ${runStartedAt}`)
+ const roster = new Set()
+ const executed = new Set()
+ for (const job of jobs) {
+ const match = /^UI Playwright E2E \((\d+)\/(\d+)\)$/.exec(job.name ?? "")
+ if (!match) continue
+ const shard = parsePositiveInteger(match[1], "job shard")
+ const total = parsePositiveInteger(match[2], "job total")
+ if (total !== expectedTotal) throw new Error(`job ${job.name} does not match matrix total ${expectedTotal}`)
+ if (shard > expectedTotal) throw new Error(`job ${job.name} has an out-of-range shard`)
+ if (roster.has(shard)) throw new Error(`attempt job roster contains duplicate shard ${shard}`)
+ roster.add(shard)
+ const jobStartedAt = Date.parse(job.started_at)
+ if (!Number.isFinite(jobStartedAt)) throw new Error(`job ${job.name} has invalid started_at`)
+ if (jobStartedAt < startedAt) continue
+ if (executed.has(shard)) throw new Error(`current attempt contains duplicate execution for shard ${shard}`)
+ executed.add(shard)
+ }
+ const missing = Array.from(
+ { length: expectedTotal },
+ (_, index) => index + 1,
+ ).filter((shard) => !roster.has(shard))
+ if (missing.length > 0) {
+ throw new Error(`attempt job roster is missing UI Playwright shards: ${missing.join(", ")}`)
+ }
+ return executed
+}
+
+async function fetchJson(fetchImpl, url, token) {
+ const response = await fetchImpl(url, {
+ headers: {
+ Accept: "application/vnd.github+json",
+ Authorization: `Bearer ${token}`,
+ "X-GitHub-Api-Version": "2022-11-28",
+ },
+ })
+ if (!response.ok) throw new Error(`GitHub API ${response.status} for ${url}`)
+ return response.json()
+}
+
+export async function fetchExecutedShards({
+ repository,
+ runId,
+ attempt,
+ expectedTotal,
+ token,
+ fetchImpl = fetch,
+ apiUrl = "https://api.github.com",
+}) {
+ if (!repository || !token) throw new Error("repository and GitHub token are required")
+ const run = await fetchJson(fetchImpl, `${apiUrl}/repos/${repository}/actions/runs/${runId}`, token)
+ if (Number(run.run_attempt) !== Number(attempt)) {
+ throw new Error(`GitHub run attempt ${run.run_attempt} does not match ${attempt}`)
+ }
+
+ const jobs = []
+ for (let page = 1; ; page += 1) {
+ const result = await fetchJson(
+ fetchImpl,
+ `${apiUrl}/repos/${repository}/actions/runs/${runId}/attempts/${attempt}/jobs?per_page=100&page=${page}`,
+ token,
+ )
+ jobs.push(...(result.jobs ?? []))
+ if ((result.jobs ?? []).length < 100) break
+ }
+ return resolveExecutedShards({ jobs, runStartedAt: run.run_started_at, expectedTotal })
+}
+
+function walkFiles(directory) {
+ return readdirSync(directory, { withFileTypes: true }).flatMap((entry) => {
+ const path = join(directory, entry.name)
+ return entry.isDirectory() ? walkFiles(path) : [path]
+ })
+}
+
+function assertEqualArray(actual, expected, label) {
+ if (actual.length !== expected.length || actual.some((value, index) => value !== expected[index])) {
+ throw new Error(`${label}: expected ${JSON.stringify(expected)}, received ${JSON.stringify(actual)}`)
+ }
+}
+
+export function verifyArtifactClosure({
+ root,
+ output,
+ matrix: matrixInput,
+ runId,
+ attempt,
+ sha,
+ executedShards,
+}) {
+ const matrix = expectedMatrix(matrixInput)
+ const currentAttempt = parsePositiveInteger(attempt, "current attempt")
+ const expectedNames = Array.from(
+ { length: matrix.total },
+ (_, index) => `${SHARD_ARTIFACT_PREFIX}-${runId}-${index + 1}`,
+ ).sort()
+ const actualNames = readdirSync(root, { withFileTypes: true })
+ .filter((entry) => entry.isDirectory())
+ .map((entry) => entry.name)
+ .sort()
+ assertEqualArray(actualNames, expectedNames, "artifact directories must exactly match matrix shards")
+
+ rmSync(output, { recursive: true, force: true })
+ mkdirSync(output, { recursive: true })
+ const manifests = []
+ for (let shard = 1; shard <= matrix.total; shard += 1) {
+ const artifactDirectory = join(root, `${SHARD_ARTIFACT_PREFIX}-${runId}-${shard}`)
+ const files = walkFiles(artifactDirectory)
+ const manifestsFound = files.filter((path) => basename(path) === "shard-manifest.json")
+ const zips = files.filter((path) => path.endsWith(".zip"))
+ if (manifestsFound.length !== 1) throw new Error(`shard ${shard} must contain exactly one manifest`)
+ if (zips.length !== 1) throw new Error(`shard ${shard} must contain exactly one blob zip`)
+ if (statSync(zips[0]).size === 0) throw new Error(`shard ${shard} blob zip is empty`)
+
+ const manifest = JSON.parse(readFileSync(manifestsFound[0], "utf8"))
+ const expected = matrix.shards.get(shard)
+ if (manifest.version !== SHARD_MANIFEST_VERSION) throw new Error(`shard ${shard} manifest version mismatch`)
+ if (String(manifest.runId) !== String(runId)) throw new Error(`shard ${shard} run ID mismatch`)
+ if (manifest.sha !== sha) throw new Error(`shard ${shard} SHA mismatch`)
+ if (manifest.shard !== shard || manifest.total !== matrix.total) {
+ throw new Error(`shard ${shard} manifest identity mismatch`)
+ }
+ assertEqualArray(
+ (manifest.specs ?? []).map(normalizeSpecPath).sort(),
+ expected.specs,
+ `shard ${shard} specs mismatch`,
+ )
+ const manifestAttempt = parsePositiveInteger(manifest.attempt, `shard ${shard} manifest attempt`)
+ if (manifestAttempt > currentAttempt) throw new Error(`shard ${shard} manifest is from a future attempt`)
+ if (executedShards.has(shard) && manifestAttempt !== currentAttempt) {
+ throw new Error(`shard ${shard} executed in attempt ${currentAttempt} but artifact is stale from attempt ${manifestAttempt}`)
+ }
+ manifests.push(manifest)
+ copyFileSync(zips[0], join(output, `shard-${shard}-${basename(zips[0])}`))
+ }
+ return { manifests, expectedSpecs: matrix.specs }
+}
+
+function collectReportFiles(suites, files = []) {
+ for (const suite of suites ?? []) {
+ if (suite.file) files.push(normalizeSpecPath(suite.file))
+ collectReportFiles(suite.suites, files)
+ }
+ return files
+}
+
+export function verifyMergedReport({ report, matrix: matrixInput }) {
+ const matrix = expectedMatrix(matrixInput)
+ const reportJson = typeof report === "string" ? JSON.parse(report) : report
+ const actual = [...new Set(collectReportFiles(reportJson.suites))].sort()
+ assertEqualArray(actual, matrix.specs, "merged report spec set mismatch")
+ return actual
+}
+
+function parseArgs(argv) {
+ const [command, ...rest] = argv
+ const args = { command }
+ for (let index = 0; index < rest.length; index += 2) {
+ const key = rest[index]
+ if (!key?.startsWith("--") || rest[index + 1] == null) throw new Error(`invalid CLI argument ${key}`)
+ args[key.slice(2).replace(/-([a-z])/g, (_, letter) => letter.toUpperCase())] = rest[index + 1]
+ }
+ return args
+}
+
+export async function runCli(argv, env = process.env) {
+ const args = parseArgs(argv)
+ if (args.command === "write-manifest") {
+ const manifest = createShardManifest({
+ runId: args.runId,
+ attempt: args.attempt,
+ sha: args.sha,
+ shard: args.shard,
+ total: args.total,
+ specs: JSON.parse(env.E2E_SPECS ?? "[]"),
+ })
+ mkdirSync(resolve(args.output, ".."), { recursive: true })
+ writeFileSync(args.output, `${JSON.stringify(manifest, null, 2)}\n`)
+ return
+ }
+ if (args.command === "verify-artifacts") {
+ const matrix = env.E2E_MATRIX
+ if (!matrix) throw new Error("E2E_MATRIX is required")
+ const parsedMatrix = expectedMatrix(matrix)
+ const executedShards = await fetchExecutedShards({
+ repository: env.GITHUB_REPOSITORY,
+ runId: args.runId,
+ attempt: args.attempt,
+ expectedTotal: parsedMatrix.total,
+ token: env.GITHUB_TOKEN,
+ apiUrl: env.GITHUB_API_URL,
+ })
+ verifyArtifactClosure({
+ root: args.root,
+ output: args.output,
+ matrix,
+ runId: args.runId,
+ attempt: args.attempt,
+ sha: args.sha,
+ executedShards,
+ })
+ return
+ }
+ if (args.command === "verify-merged") {
+ const matrix = env.E2E_MATRIX
+ if (!matrix) throw new Error("E2E_MATRIX is required")
+ verifyMergedReport({ report: readFileSync(args.report, "utf8"), matrix })
+ return
+ }
+ throw new Error(`unknown command ${args.command}`)
+}
+
+export function runCliEntry({
+ direct = process.argv[1] === fileURLToPath(import.meta.url),
+ argv = process.argv.slice(2),
+ env = process.env,
+ stderr = process.stderr,
+ setExitCode = (code) => { process.exitCode = code },
+} = {}) {
+ if (!direct) return undefined
+ return runCli(argv, env).catch((error) => {
+ stderr.write(`${error.stack ?? error}\n`)
+ setExitCode(1)
+ })
+}
+
+void runCliEntry()
diff --git a/scripts/ci/e2e-shard-artifacts.test.ts b/scripts/ci/e2e-shard-artifacts.test.ts
new file mode 100644
index 000000000..0c5616ad5
--- /dev/null
+++ b/scripts/ci/e2e-shard-artifacts.test.ts
@@ -0,0 +1,450 @@
+import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"
+import { tmpdir } from "node:os"
+import { join } from "node:path"
+import { afterEach, describe, expect, it, vi } from "vitest"
+import {
+ createShardManifest,
+ expectedMatrix,
+ fetchExecutedShards,
+ resolveExecutedShards,
+ runCli,
+ runCliEntry,
+ verifyArtifactClosure,
+ verifyMergedReport,
+} from "./e2e-shard-artifacts.mjs"
+
+const matrix = {
+ include: [
+ { shard: 1, total: 2, specs: ["src/test/e2e-ui/a.spec.ts"] },
+ { shard: 2, total: 2, specs: ["src/test/e2e-ui/b.spec.ts"] },
+ ],
+}
+
+const roots: string[] = []
+afterEach(() => {
+ vi.unstubAllGlobals()
+ for (const root of roots.splice(0)) rmSync(root, { recursive: true, force: true })
+})
+
+function artifactRoot(attempts = [1, 2]) {
+ const root = mkdtempSync(join(tmpdir(), "alook-shard-artifacts-"))
+ roots.push(root)
+ for (let shard = 1; shard <= 2; shard += 1) {
+ const artifact = join(root, `blob-report-run-1-${shard}`)
+ mkdirSync(join(artifact, "blob-report"), { recursive: true })
+ mkdirSync(join(artifact, "e2e-shard-manifest"), { recursive: true })
+ writeFileSync(join(artifact, "blob-report", `report-${shard}.zip`), `zip-${shard}`)
+ writeFileSync(join(artifact, "e2e-shard-manifest", "shard-manifest.json"), JSON.stringify(
+ createShardManifest({
+ runId: "run-1",
+ attempt: attempts[shard - 1],
+ sha: "abc123",
+ shard,
+ total: 2,
+ specs: matrix.include[shard - 1]!.specs,
+ }),
+ ))
+ }
+ return root
+}
+
+describe("expectedMatrix", () => {
+ it.each([
+ ["missing include", {}],
+ ["empty include", { include: [] }],
+ ])("rejects a matrix with %s", (_, value) => {
+ expect(() => expectedMatrix(value)).toThrow("non-empty include array")
+ })
+
+ it("rejects a shard without specs", () => {
+ expect(() => expectedMatrix({ include: [
+ { shard: 1, total: 1, specs: [] },
+ ] })).toThrow("matrix shard 1 must contain specs")
+ })
+
+ it("rejects duplicate cross-shard assignment", () => {
+ expect(() => expectedMatrix({ include: [
+ { shard: 1, total: 2, specs: ["src/test/e2e-ui/a.spec.ts"] },
+ { shard: 2, total: 2, specs: ["src/test/e2e-ui/a.spec.ts"] },
+ ] })).toThrow("assigned to shards")
+ })
+})
+
+describe("resolveExecutedShards", () => {
+ const jobs = [
+ { name: "UI Playwright E2E (1/2)", started_at: "2026-08-25T17:02:50Z" },
+ { name: "UI Playwright E2E (2/2)", started_at: "2026-08-25T17:13:26Z" },
+ { name: "Merge Playwright Reports", started_at: "2026-08-25T17:19:19Z" },
+ ]
+
+ it("excludes carry-forward jobs older than run_started_at", () => {
+ expect([...resolveExecutedShards({
+ jobs,
+ runStartedAt: "2026-08-25T17:13:19Z",
+ expectedTotal: 2,
+ })]).toEqual([2])
+ })
+
+ it("accepts a merge-only rerun with a complete carry-forward roster", () => {
+ expect(resolveExecutedShards({
+ jobs,
+ runStartedAt: "2026-08-25T17:20:00Z",
+ expectedTotal: 2,
+ })).toEqual(new Set())
+ })
+
+ it("rejects an incomplete attempt job roster", () => {
+ expect(() => resolveExecutedShards({
+ jobs: [jobs[0]],
+ runStartedAt: "2026-08-25T17:20:00Z",
+ expectedTotal: 2,
+ })).toThrow("attempt job roster is missing UI Playwright shards: 2")
+ })
+
+ it("rejects a shard outside the declared matrix range", () => {
+ expect(() => resolveExecutedShards({
+ jobs: [{ name: "UI Playwright E2E (3/2)", started_at: "2026-08-25T17:13:26Z" }],
+ runStartedAt: "2026-08-25T17:13:19Z",
+ expectedTotal: 2,
+ })).toThrow("out-of-range shard")
+ })
+
+ it("fetches the run and paged attempt jobs with actions-read API semantics", async () => {
+ const urls: string[] = []
+ const fetchImpl = async (url: string) => {
+ urls.push(url)
+ return {
+ ok: true,
+ json: async () => url.includes("/jobs?")
+ ? { jobs }
+ : { run_attempt: 2, run_started_at: "2026-08-25T17:13:19Z" },
+ } as Response
+ }
+ await expect(fetchExecutedShards({
+ repository: "alookai/alook",
+ runId: "run-1",
+ attempt: 2,
+ expectedTotal: 2,
+ token: "redacted",
+ fetchImpl,
+ apiUrl: "https://example.invalid",
+ })).resolves.toEqual(new Set([2]))
+ expect(urls).toHaveLength(2)
+ expect(urls[1]).toContain("/attempts/2/jobs?per_page=100&page=1")
+ })
+
+ it("uses the default fetch and API URL while rejecting an attempt mismatch", async () => {
+ const fetchImpl = vi.fn(async () => ({
+ ok: true,
+ json: async () => ({ run_attempt: 1, run_started_at: "2026-08-25T17:13:19Z" }),
+ } as Response))
+ vi.stubGlobal("fetch", fetchImpl)
+
+ await expect(fetchExecutedShards({
+ repository: "alookai/alook",
+ runId: "run-1",
+ attempt: 2,
+ expectedTotal: 2,
+ token: "redacted",
+ })).rejects.toThrow("GitHub run attempt 1 does not match 2")
+ expect(fetchImpl).toHaveBeenCalledTimes(1)
+ expect(fetchImpl.mock.calls[0]![0]).toBe("https://api.github.com/repos/alookai/alook/actions/runs/run-1")
+ })
+})
+
+describe("verifyArtifactClosure", () => {
+ it("accepts all previous-attempt artifacts for a merge-only rerun", () => {
+ const root = artifactRoot([1, 1])
+ const output = join(root, "..", `${root.split("/").at(-1)}-merged`)
+ roots.push(output)
+ const result = verifyArtifactClosure({
+ root,
+ output,
+ matrix,
+ runId: "run-1",
+ attempt: 2,
+ sha: "abc123",
+ executedShards: new Set(),
+ })
+ expect(result.manifests.map((manifest) => manifest.attempt)).toEqual([1, 1])
+ expect(readFileSync(join(output, "shard-1-report-1.zip"), "utf8")).toBe("zip-1")
+ expect(readFileSync(join(output, "shard-2-report-2.zip"), "utf8")).toBe("zip-2")
+ })
+
+ it("accepts an older unexecuted shard and a fresh executed shard", () => {
+ const root = artifactRoot([1, 2])
+ const output = join(root, "..", `${root.split("/").at(-1)}-merged`)
+ roots.push(output)
+ const result = verifyArtifactClosure({
+ root,
+ output,
+ matrix,
+ runId: "run-1",
+ attempt: 2,
+ sha: "abc123",
+ executedShards: new Set([2]),
+ })
+ expect(result.expectedSpecs).toEqual(["a.spec.ts", "b.spec.ts"])
+ expect(readFileSync(join(output, "shard-2-report-2.zip"), "utf8")).toBe("zip-2")
+ })
+
+ it("fails when an executed shard still has the previous attempt manifest", () => {
+ const root = artifactRoot([1, 1])
+ expect(() => verifyArtifactClosure({
+ root,
+ output: join(root, "merged"),
+ matrix,
+ runId: "run-1",
+ attempt: 2,
+ sha: "abc123",
+ executedShards: new Set([2]),
+ })).toThrow("shard 2 executed in attempt 2 but artifact is stale from attempt 1")
+ })
+
+ it.each([
+ ["wrong run ID", (manifest: any) => { manifest.runId = "wrong" }, "run ID mismatch"],
+ ["wrong SHA", (manifest: any) => { manifest.sha = "wrong" }, "SHA mismatch"],
+ ["wrong identity", (manifest: any) => { manifest.shard = 99 }, "manifest identity mismatch"],
+ ["wrong specs", (manifest: any) => { manifest.specs = ["unexpected.spec.ts"] }, "specs mismatch"],
+ ])("rejects %s", (_, mutate, message) => {
+ const root = artifactRoot([2, 2])
+ const path = join(root, "blob-report-run-1-2", "e2e-shard-manifest", "shard-manifest.json")
+ const manifest = JSON.parse(readFileSync(path, "utf8"))
+ mutate(manifest)
+ writeFileSync(path, JSON.stringify(manifest))
+ expect(() => verifyArtifactClosure({
+ root,
+ output: join(root, "merged"),
+ matrix,
+ runId: "run-1",
+ attempt: 2,
+ sha: "abc123",
+ executedShards: new Set([1, 2]),
+ })).toThrow(message)
+ })
+
+ it("rejects a missing shard directory", () => {
+ const root = artifactRoot([2, 2])
+ rmSync(join(root, "blob-report-run-1-1"), { recursive: true })
+ expect(() => verifyArtifactClosure({
+ root,
+ output: join(root, "merged"),
+ matrix,
+ runId: "run-1",
+ attempt: 2,
+ sha: "abc123",
+ executedShards: new Set([1, 2]),
+ })).toThrow("exactly match matrix shards")
+ })
+
+ it("rejects an unexpected shard directory", () => {
+ const root = artifactRoot([2, 2])
+ mkdirSync(join(root, "blob-report-run-1-3"))
+ expect(() => verifyArtifactClosure({
+ root,
+ output: join(root, "merged"),
+ matrix,
+ runId: "run-1",
+ attempt: 2,
+ sha: "abc123",
+ executedShards: new Set([1, 2]),
+ })).toThrow("exactly match matrix shards")
+ })
+
+ it.each([
+ ["missing manifest", (artifact: string) => {
+ rmSync(join(artifact, "e2e-shard-manifest", "shard-manifest.json"))
+ }, "exactly one manifest"],
+ ["empty blob", (artifact: string) => {
+ writeFileSync(join(artifact, "blob-report", "report-1.zip"), "")
+ }, "blob zip is empty"],
+ ["multiple blobs", (artifact: string) => {
+ writeFileSync(join(artifact, "blob-report", "extra.zip"), "extra")
+ }, "exactly one blob zip"],
+ ])("rejects an artifact with a %s", (_, mutate, message) => {
+ const root = artifactRoot([2, 2])
+ mutate(join(root, "blob-report-run-1-1"))
+ expect(() => verifyArtifactClosure({
+ root,
+ output: join(root, "merged"),
+ matrix,
+ runId: "run-1",
+ attempt: 2,
+ sha: "abc123",
+ executedShards: new Set([1, 2]),
+ })).toThrow(message)
+ })
+})
+
+describe("runCli", () => {
+ it("writes a normalized shard manifest", async () => {
+ const root = mkdtempSync(join(tmpdir(), "alook-shard-cli-"))
+ roots.push(root)
+ const output = join(root, "nested", "shard-manifest.json")
+
+ await runCli([
+ "write-manifest",
+ "--run-id", "run-2",
+ "--attempt", "2",
+ "--sha", "def456",
+ "--shard", "1",
+ "--total", "2",
+ "--output", output,
+ ], {
+ E2E_SPECS: JSON.stringify(["src/test/e2e-ui/a.spec.ts"]),
+ })
+
+ const contents = readFileSync(output, "utf8")
+ expect(contents.endsWith("\n")).toBe(true)
+ expect(JSON.parse(contents)).toEqual({
+ version: 1,
+ runId: "run-2",
+ attempt: 2,
+ sha: "def456",
+ shard: 1,
+ total: 2,
+ specs: ["a.spec.ts"],
+ })
+ })
+
+ it("rejects an invalid CLI argument pair", async () => {
+ await expect(runCli(["write-manifest", "--run-id"], {})).rejects.toThrow(
+ "invalid CLI argument --run-id",
+ )
+ })
+
+ it("verifies artifacts through the default GitHub fetch path", async () => {
+ const root = artifactRoot([2, 2])
+ const output = join(root, "merged")
+ const fetchImpl = vi.fn(async (url: string) => ({
+ ok: true,
+ json: async () => url.includes("/jobs?")
+ ? { jobs: [
+ { name: "UI Playwright E2E (1/2)", started_at: "2026-08-25T17:13:20Z" },
+ { name: "UI Playwright E2E (2/2)", started_at: "2026-08-25T17:13:21Z" },
+ ] }
+ : { run_attempt: 2, run_started_at: "2026-08-25T17:13:19Z" },
+ } as Response))
+ vi.stubGlobal("fetch", fetchImpl)
+
+ await runCli([
+ "verify-artifacts",
+ "--root", root,
+ "--output", output,
+ "--run-id", "run-1",
+ "--attempt", "2",
+ "--sha", "abc123",
+ ], {
+ E2E_MATRIX: JSON.stringify(matrix),
+ GITHUB_REPOSITORY: "alookai/alook",
+ GITHUB_TOKEN: "redacted",
+ })
+
+ expect(fetchImpl.mock.calls.map(([url]) => url)).toEqual([
+ "https://api.github.com/repos/alookai/alook/actions/runs/run-1",
+ "https://api.github.com/repos/alookai/alook/actions/runs/run-1/attempts/2/jobs?per_page=100&page=1",
+ ])
+ expect(readFileSync(join(output, "shard-1-report-1.zip"), "utf8")).toBe("zip-1")
+ expect(readFileSync(join(output, "shard-2-report-2.zip"), "utf8")).toBe("zip-2")
+ })
+
+ it("requires the matrix for artifact verification", async () => {
+ await expect(runCli(["verify-artifacts"], {})).rejects.toThrow("E2E_MATRIX is required")
+ })
+
+ it("verifies an exact merged report", async () => {
+ const root = mkdtempSync(join(tmpdir(), "alook-shard-cli-"))
+ roots.push(root)
+ const report = join(root, "report.json")
+ writeFileSync(report, JSON.stringify({ suites: [
+ { file: "src/test/e2e-ui/a.spec.ts" },
+ { file: "b.spec.ts" },
+ ] }))
+
+ await expect(runCli([
+ "verify-merged",
+ "--report", report,
+ ], {
+ E2E_MATRIX: JSON.stringify(matrix),
+ })).resolves.toBeUndefined()
+ })
+
+ it("requires the matrix for merged report verification", async () => {
+ await expect(runCli(["verify-merged"], {})).rejects.toThrow("E2E_MATRIX is required")
+ })
+
+ it("rejects an unknown command", async () => {
+ await expect(runCli(["unknown"], {})).rejects.toThrow("unknown command unknown")
+ })
+})
+
+describe("runCliEntry", () => {
+ it("has no side effects for a non-direct import", () => {
+ const stderr = { write: vi.fn() }
+ const setExitCode = vi.fn()
+
+ expect(runCliEntry({
+ direct: false,
+ argv: ["unknown"],
+ env: {},
+ stderr,
+ setExitCode,
+ })).toBeUndefined()
+ expect(stderr.write).not.toHaveBeenCalled()
+ expect(setExitCode).not.toHaveBeenCalled()
+ })
+
+ it("returns and catches the direct CLI promise", async () => {
+ const stderr = { write: vi.fn() }
+ const setExitCode = vi.fn()
+ const result = runCliEntry({
+ direct: true,
+ argv: ["unknown"],
+ env: {},
+ stderr,
+ setExitCode,
+ })
+
+ expect(result).toBeInstanceOf(Promise)
+ await result
+ expect(stderr.write).toHaveBeenCalledOnce()
+ expect(stderr.write.mock.calls[0]![0]).toContain("unknown command unknown")
+ expect(setExitCode).toHaveBeenCalledWith(1)
+ })
+
+ it("sets the process exit code through the production default", async () => {
+ const previousExitCode = process.exitCode
+ const stderr = { write: vi.fn() }
+ try {
+ await runCliEntry({
+ direct: true,
+ argv: ["unknown"],
+ env: {},
+ stderr,
+ })
+ expect(process.exitCode).toBe(1)
+ } finally {
+ process.exitCode = previousExitCode
+ }
+ })
+})
+
+describe("verifyMergedReport", () => {
+ it("accepts the exact normalized unique spec set", () => {
+ expect(verifyMergedReport({
+ matrix,
+ report: { suites: [
+ { file: "a.spec.ts", suites: [] },
+ { file: "src/test/e2e-ui/b.spec.ts", suites: [] },
+ { file: "a.spec.ts", suites: [] },
+ ] },
+ })).toEqual(["a.spec.ts", "b.spec.ts"])
+ })
+
+ it.each([
+ ["missing", [{ file: "a.spec.ts" }]],
+ ["unexpected", [{ file: "a.spec.ts" }, { file: "b.spec.ts" }, { file: "c.spec.ts" }]],
+ ])("rejects an exact-set %s report", (_, suites) => {
+ expect(() => verifyMergedReport({ matrix, report: { suites } })).toThrow("spec set mismatch")
+ })
+})
diff --git a/scripts/ci/workflow-contract.test.ts b/scripts/ci/workflow-contract.test.ts
index fe167ac7d..4adad276e 100644
--- a/scripts/ci/workflow-contract.test.ts
+++ b/scripts/ci/workflow-contract.test.ts
@@ -122,14 +122,19 @@ describe("E2E UI workflow", () => {
expect(workflow).toContain("if-no-files-found: ignore")
expect(workflow).toContain("retention-days: 7")
expect(workflow).toContain(
- "name: blob-report-${{ github.run_id }}-${{ github.run_attempt }}-${{ matrix.shard }}",
+ "name: blob-report-${{ github.run_id }}-${{ matrix.shard }}",
)
expect(workflow).toContain(
- "pattern: blob-report-${{ github.run_id }}-${{ github.run_attempt }}-*",
- )
- expect(workflow).toContain(
- "name: playwright-merge-runtime-${{ github.run_id }}-${{ github.run_attempt }}",
+ "pattern: blob-report-${{ github.run_id }}-*",
)
+ expect(workflow).toContain("overwrite: true")
+ expect(workflow).toContain("merge-multiple: false")
+ expect(workflow).toContain("actions: read")
+ expect(workflow).toContain("verify-artifacts")
+ expect(workflow).toContain("verify-merged")
+ expect(workflow).toContain("--reporter html,json")
+ expect(workflow).not.toContain("playwright-merge-runtime")
+ expect(workflow.match(/pnpm install --frozen-lockfile/g)).toHaveLength(2)
expect(workflow.match(/if-no-files-found: error/g)).toHaveLength(2)
expect(workflow.match(/continue-on-error: true/g)).toHaveLength(1)
})
diff --git a/src/web/src/components/community/bots/bot-activity-modal.tsx b/src/web/src/components/community/bots/bot-activity-modal.tsx
index 6e56b16b6..2a0462363 100644
--- a/src/web/src/components/community/bots/bot-activity-modal.tsx
+++ b/src/web/src/components/community/bots/bot-activity-modal.tsx
@@ -14,6 +14,7 @@ import {
type AuditEvent,
} from "@/hooks/community/use-bot-audit-log"
import type { BotSummary } from "@/hooks/community/use-bots"
+import { tid } from "@/lib/community/testids"
import { BotActivityRow } from "./bot-activity-row"
/**
@@ -159,7 +160,7 @@ export function BotActivityModal({
return (
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent
- data-testid="bot-activity-modal"
+ data-testid={tid.botActivityModal}
showCloseButton={false}
className="flex h-[72vh] max-h-170 w-full max-w-2xl flex-col gap-0 overflow-hidden p-0 sm:max-w-2xl"
>
diff --git a/src/web/src/components/community/channels/text-channel-surface.tsx b/src/web/src/components/community/channels/text-channel-surface.tsx
index 638ede969..3e721f3a9 100644
--- a/src/web/src/components/community/channels/text-channel-surface.tsx
+++ b/src/web/src/components/community/channels/text-channel-surface.tsx
@@ -16,6 +16,7 @@ import {
import type { FileAttachment, ImagePreview } from "@/lib/community/models/message"
import type { OpenProfile } from "@/components/community/social/profile-types"
import type { RightPanel } from "@/components/community/shell/panel-types"
+import { tid } from "@/lib/community/testids"
export function TextChannelSurface({
channelId,
@@ -135,7 +136,11 @@ export function TextChannelSurface({
presentVersion={feed.presentVersion}
unreadCount={feed.unreadCount}
/>
- <div data-onboarding-target="channel-composer" className="shrink-0">
+ <div
+ data-onboarding-target="channel-composer"
+ data-testid={tid.channelComposerShell}
+ className="shrink-0"
+ >
<Composer
channel={channelName}
context="channel"
diff --git a/src/web/src/components/community/channels/thread-channel-surface.tsx b/src/web/src/components/community/channels/thread-channel-surface.tsx
index 94d1e6e2a..5390836e0 100644
--- a/src/web/src/components/community/channels/thread-channel-surface.tsx
+++ b/src/web/src/components/community/channels/thread-channel-surface.tsx
@@ -18,6 +18,7 @@ import type { FileAttachment, ImagePreview } from "@/lib/community/models/messag
import type { OpenProfile } from "@/components/community/social/profile-types"
import type { RightPanel } from "@/components/community/shell/panel-types"
import type { ChannelMemberPanelProps } from "@/components/community/members/channel-member-view-model"
+import { tid } from "@/lib/community/testids"
const ignoreNestedThread = () => {}
@@ -236,7 +237,11 @@ export function ThreadChannelSurface({
presentVersion={controller.feed.presentVersion}
unreadCount={controller.feed.unreadCount}
/>
- <div data-onboarding-target="channel-composer" className="shrink-0">
+ <div
+ data-onboarding-target="channel-composer"
+ data-testid={tid.channelComposerShell}
+ className="shrink-0"
+ >
<Composer
channel={displayName}
context="thread"
diff --git a/src/web/src/components/community/messages/composer-view.tsx b/src/web/src/components/community/messages/composer-view.tsx
index 7b0d8e25d..9e3a1effe 100644
--- a/src/web/src/components/community/messages/composer-view.tsx
+++ b/src/web/src/components/community/messages/composer-view.tsx
@@ -145,6 +145,7 @@ export function ComposerView({
</div>
)}
<input
+ data-testid={tid.composerFileInput}
ref={fileInputRef}
type="file"
multiple
diff --git a/src/web/src/components/community/messages/message-list-view.tsx b/src/web/src/components/community/messages/message-list-view.tsx
index 5ecdb5b1e..99cba495d 100644
--- a/src/web/src/components/community/messages/message-list-view.tsx
+++ b/src/web/src/components/community/messages/message-list-view.tsx
@@ -1,5 +1,6 @@
import type { ReactNode } from "react"
import { Skeleton } from "@/components/ui/skeleton"
+import { tid } from "@/lib/community/testids"
import { ChannelIcon } from "../channels/channel-icon"
import { ComposerAccessoryRail } from "./composer-accessory-rail"
import { MessageShareDialog } from "./message-share-dialog"
@@ -34,6 +35,7 @@ export function renderMessageListView(
)}
<div
ref={controller.scrollRef}
+ data-testid={tid.messageScroller}
className="flex-1 overflow-x-clip overflow-y-auto thin-scrollbar"
>
<div className="flex min-h-full flex-col justify-end px-4 py-8">
diff --git a/src/web/src/lib/community/testids.test.ts b/src/web/src/lib/community/testids.test.ts
index 999d185e9..dab2c4088 100644
--- a/src/web/src/lib/community/testids.test.ts
+++ b/src/web/src/lib/community/testids.test.ts
@@ -24,11 +24,18 @@ describe("community QA selectors", () => {
expect(tid.botAuditPreview).toBe("community-bot-audit-preview")
expect(tid.botAuditPreviewDock).toBe("community-bot-audit-preview-dock")
expect(tid.botAuditPreviewActive).toBe("community-bot-audit-preview-active")
+ expect(tid.botActivityModal).toBe("bot-activity-modal")
expect(tid.botAuditPreviewRow("event_1")).toBe(
"community-bot-audit-preview-row-event_1",
)
})
+ it("exposes stable composer and message-list surfaces", () => {
+ expect(tid.channelComposerShell).toBe("community-composer-shell")
+ expect(tid.composerFileInput).toBe("community-composer-file-input")
+ expect(tid.messageScroller).toBe("community-message-scroller")
+ })
+
it("exposes attachment preview selectors from one canonical map", () => {
expect(tid.attachmentCard("notes.md")).toBe("community-attachment-card-notes.md")
expect(tid.attachmentPreviewSheet).toBe("community-attachment-preview-sheet")
diff --git a/src/web/src/lib/community/testids.ts b/src/web/src/lib/community/testids.ts
index da94a90b9..df22fd6c3 100644
--- a/src/web/src/lib/community/testids.ts
+++ b/src/web/src/lib/community/testids.ts
@@ -3,11 +3,14 @@
// Playwright specs (via _fixtures/testids). Naming: community-<区域>-<元素>[-<标识>].
export const tid = {
composerInput: "community-composer-input",
+ channelComposerShell: "community-composer-shell",
+ composerFileInput: "community-composer-file-input",
composerSend: "community-composer-send",
composerAttach: "community-composer-attach",
composerAccessoryRail: "community-composer-accessory-rail",
scrollToPresent: "community-scroll-to-present",
messageSelectionToolbar: "community-message-selection-toolbar",
+ messageScroller: "community-message-scroller",
wsStatus: "community-ws-status",
wsRetry: "community-ws-retry",
serverAdd: "community-server-add",
@@ -35,6 +38,7 @@ export const tid = {
botAuditPreview: "community-bot-audit-preview",
botAuditPreviewDock: "community-bot-audit-preview-dock",
botAuditPreviewActive: "community-bot-audit-preview-active",
+ botActivityModal: "bot-activity-modal",
botAuditPreviewRow: (id: string) => `community-bot-audit-preview-row-${id}`,
statusPill: "community-status-pill",
inviteToken: "community-invite-token",
diff --git a/src/web/src/test/e2e-service-definitions.test.ts b/src/web/src/test/e2e-service-definitions.test.ts
index 81b59ddc4..6f0e78e1c 100644
--- a/src/web/src/test/e2e-service-definitions.test.ts
+++ b/src/web/src/test/e2e-service-definitions.test.ts
@@ -1,5 +1,14 @@
+import { EventEmitter } from "node:events"
+import type { ChildProcess } from "node:child_process"
import { describe, expect, it } from "vitest"
-import { serviceDefinitions } from "./e2e-ui/_setup/services"
+import {
+ hasExactHealth,
+ readinessExitMessage,
+ serviceDefinitions,
+ waitForHealth,
+ waitForServicesReady,
+ wranglerLogEnvironment,
+} from "./e2e-ui/_setup/services"
import { resolveMachineWsUrl, resolveWsUrl } from "./e2e-ui/_setup/paths"
describe("UI E2E service definitions", () => {
@@ -8,6 +17,10 @@ describe("UI E2E service definitions", () => {
expect(definitions).toHaveLength(1)
expect(definitions[0]).toMatchObject({ name: "web-ws-do" })
+ expect(definitions[0]).toMatchObject({
+ expectedStatus: 200,
+ expectedBody: { status: "ok" },
+ })
expect(definitions[0]?.args).toEqual(expect.arrayContaining([
"src/web/wrangler.toml",
"src/ws-do/wrangler.toml",
@@ -55,4 +68,92 @@ describe("UI E2E service definitions", () => {
singleRuntime: false,
})).toBe("http://localhost:8789")
})
+
+ it.each([
+ [200, { status: "ok" }, true],
+ [201, { status: "ok" }, false],
+ [200, { status: "ok", extra: true }, false],
+ [200, { status: "degraded" }, false],
+ ] as const)("requires the exact health contract", async (status, body, expected) => {
+ const result = await hasExactHealth(
+ "http://service.test/health",
+ async () => new Response(JSON.stringify(body), { status }),
+ )
+
+ expect(result.ok).toBe(expected)
+ expect(result.status).toBe(status)
+ })
+
+ it("places sanitized debug logs inside the uploaded service-log tree", () => {
+ expect(wranglerLogEnvironment("/logs/web-wrangler-internal")).toEqual({
+ WRANGLER_LOG: "debug",
+ WRANGLER_LOG_PATH: "/logs/web-wrangler-internal",
+ WRANGLER_LOG_SANITIZE: "true",
+ })
+ })
+
+ it("bounds a health probe even when fetch never resolves", async () => {
+ const result = await hasExactHealth(
+ "http://service.test/health",
+ async () => new Promise<Response>(() => {}),
+ { timeoutMs: 5 },
+ )
+
+ expect(result).toEqual({
+ ok: false,
+ status: null,
+ detail: "health probe timed out after 5ms",
+ })
+ })
+
+ it("turns a child close before readiness into an immediate failure", () => {
+ expect(readinessExitMessage("web-ws-do", null, null)).toBeNull()
+ expect(readinessExitMessage("web-ws-do", 1, null))
+ .toBe("web-ws-do exited before readiness (code 1, signal null)")
+ expect(readinessExitMessage("web-ws-do", null, "SIGTERM"))
+ .toBe("web-ws-do exited before readiness (code null, signal SIGTERM)")
+ })
+
+ it("races a child close that occurs during an in-flight readiness probe", async () => {
+ const proc = Object.assign(new EventEmitter(), {
+ exitCode: null as number | null,
+ signalCode: null as NodeJS.Signals | null,
+ }) as unknown as ChildProcess
+ const service = {
+ name: "web-ws-do",
+ proc,
+ healthUrl: "http://service.test/health",
+ }
+ let probeSignal: AbortSignal | undefined
+ const readiness = waitForHealth(
+ service,
+ 1_000,
+ async (_url, init) => {
+ probeSignal = init?.signal ?? undefined
+ return new Promise<Response>(() => {})
+ },
+ 500,
+ )
+
+ await Promise.resolve()
+ proc.exitCode = 1
+ proc.emit("close", 1, null)
+
+ await expect(readiness).rejects.toThrow("web-ws-do exited before readiness (code 1, signal null)")
+ expect(probeSignal?.aborted).toBe(true)
+ })
+
+ it("does not pass the service array index as a readiness timeout", async () => {
+ const observedTimeouts: Array<number | undefined> = []
+ const services = [
+ { name: "web", proc: {} as never, healthUrl: "http://localhost:3000/api/health" },
+ { name: "ws-do", proc: {} as never, healthUrl: "http://localhost:8789/health" },
+ ]
+
+ await waitForServicesReady(services, async (_service, timeoutMs) => {
+ observedTimeouts.push(timeoutMs)
+ })
+
+ expect(observedTimeouts).toEqual([undefined, undefined])
+ })
})
diff --git a/src/web/src/test/e2e-service-lifecycle.test.ts b/src/web/src/test/e2e-service-lifecycle.test.ts
new file mode 100644
index 000000000..b03317f26
--- /dev/null
+++ b/src/web/src/test/e2e-service-lifecycle.test.ts
@@ -0,0 +1,121 @@
+import { mkdtempSync, rmSync } from "fs"
+import { tmpdir } from "os"
+import { resolve } from "path"
+import { afterEach, describe, expect, it } from "vitest"
+import {
+ addServiceProcess,
+ applyHealthProbe,
+ claimServiceFailure,
+ cleanupLifecycle,
+ createLifecycleState,
+ failLifecycle,
+ serviceFailureMessage,
+ writeLifecycleState,
+ type ServiceLifecycleState,
+} from "./e2e-ui/_setup/service-lifecycle"
+
+const directories: string[] = []
+
+afterEach(() => {
+ for (const directory of directories.splice(0)) {
+ rmSync(directory, { recursive: true, force: true })
+ }
+})
+
+function healthyState(): ServiceLifecycleState {
+ const state = addServiceProcess(createLifecycleState(true), {
+ name: "web-ws-do",
+ pid: 123,
+ startedAt: new Date(0).toISOString(),
+ healthUrl: "http://localhost:3000/api/health",
+ expectedStatus: 200,
+ expectedBody: { status: "ok" },
+ logPath: "/tmp/web.log",
+ internalLogDirectory: "/tmp/wrangler",
+ })
+ return { ...state, status: "healthy" }
+}
+
+describe("UI E2E service lifecycle", () => {
+ it("tolerates a transient probe miss and fails after three misses spanning two seconds", () => {
+ let state = applyHealthProbe(healthyState(), "web-ws-do", { ok: false, now: 1_000 })
+ state = applyHealthProbe(state, "web-ws-do", { ok: true, now: 1_500 })
+ expect(state.status).toBe("healthy")
+
+ state = applyHealthProbe(state, "web-ws-do", { ok: false, now: 2_000 })
+ state = applyHealthProbe(state, "web-ws-do", { ok: false, now: 3_000 })
+ state = applyHealthProbe(state, "web-ws-do", { ok: false, now: 4_000 })
+
+ expect(state.status).toBe("failed")
+ expect(state.failure).toMatchObject({ service: "web-ws-do", kind: "health" })
+ })
+
+ it("records an unexpected close but ignores a close during cleanup", () => {
+ const failure = {
+ service: "web-ws-do",
+ kind: "child-close" as const,
+ at: new Date(0).toISOString(),
+ code: 1,
+ signal: null,
+ detail: "managed service process closed unexpectedly",
+ }
+
+ const failed = failLifecycle(healthyState(), failure)
+ expect(failed.status).toBe("failed")
+ expect(serviceFailureMessage(failed)).toContain("code=1")
+ expect(serviceFailureMessage(failed)).toContain("consoleLog=/tmp/web.log")
+ expect(failLifecycle({ ...healthyState(), status: "stopping" }, failure).failure).toBeUndefined()
+ })
+
+ it("reports an infrastructure failure once and skips subsequent tests", () => {
+ const directory = mkdtempSync(resolve(tmpdir(), "alook-e2e-lifecycle-"))
+ directories.push(directory)
+ const statePath = resolve(directory, "state.json")
+ const claimPath = resolve(directory, "failure.claim")
+ writeLifecycleState(statePath, failLifecycle(healthyState(), {
+ service: "web-ws-do",
+ kind: "health",
+ at: new Date(0).toISOString(),
+ detail: "exact health failed",
+ }))
+
+ expect(claimServiceFailure(statePath, claimPath).action).toBe("report")
+ expect(claimServiceFailure(statePath, claimPath).action).toBe("skip")
+ writeLifecycleState(statePath, healthyState())
+ expect(claimServiceFailure(statePath, claimPath)).toEqual({ action: "continue" })
+ })
+
+ it("terminates process groups, escalates, and restores state exactly once", async () => {
+ const signals: string[] = []
+ let waits = 0
+ let restores = 0
+ const dependencies = {
+ terminate: (pid: number, signal: "SIGTERM" | "SIGKILL") => signals.push(`${pid}:${signal}`),
+ waitForStop: async () => ++waits === 2,
+ restore: () => { restores += 1 },
+ }
+
+ const stopped = await cleanupLifecycle(healthyState(), dependencies)
+ const repeated = await cleanupLifecycle(stopped, dependencies)
+
+ expect(signals).toEqual(["123:SIGTERM", "123:SIGKILL"])
+ expect(restores).toBe(1)
+ expect(repeated).toEqual(stopped)
+ })
+
+ it("cleans a partial setup after backup and child spawn before login completes", async () => {
+ const starting = { ...healthyState(), status: "starting" as const }
+ const signals: string[] = []
+ let restores = 0
+
+ const stopped = await cleanupLifecycle(starting, {
+ terminate: (pid, signal) => signals.push(`${pid}:${signal}`),
+ waitForStop: async () => true,
+ restore: () => { restores += 1 },
+ })
+
+ expect(signals).toEqual(["123:SIGTERM"])
+ expect(restores).toBe(1)
+ expect(stopped).toMatchObject({ status: "stopped", restorePending: false })
+ })
+})
diff --git a/src/web/src/test/e2e-ui/02-server-channel-message.spec.ts b/src/web/src/test/e2e-ui/02-server-channel-message.spec.ts
index 7419e8634..cee6d49ca 100644
--- a/src/web/src/test/e2e-ui/02-server-channel-message.spec.ts
+++ b/src/web/src/test/e2e-ui/02-server-channel-message.spec.ts
@@ -254,7 +254,7 @@ test("server → channel → message", async ({ asUser }) => {
revokeObjectUrl(url)
}
})
- await page.locator('input[type="file"]').last().setInputFiles({
+ await page.getByTestId(tid.composerFileInput).setInputFiles({
name: "rejected.txt",
mimeType: "text/plain",
buffer: Buffer.from("keep me"),
@@ -286,7 +286,7 @@ test("server → channel → message", async ({ asUser }) => {
value: () => "00000000-0000-4000-8000-000000000002",
})
})
- await page.locator('input[type="file"]').last().setInputFiles({
+ await page.getByTestId(tid.composerFileInput).setInputFiles({
name: "accepted.txt",
mimeType: "text/plain",
buffer: Buffer.from("transfer me"),
diff --git a/src/web/src/test/e2e-ui/09-forum-thread.spec.ts b/src/web/src/test/e2e-ui/09-forum-thread.spec.ts
index 124337bde..9b9ca2177 100644
--- a/src/web/src/test/e2e-ui/09-forum-thread.spec.ts
+++ b/src/web/src/test/e2e-ui/09-forum-thread.spec.ts
@@ -1,6 +1,7 @@
import { test, expect } from "./_fixtures/community-fixture"
import { composerEditable, sendMessage } from "./_fixtures/actions"
import { seedServer, seedChannel, seedMessage } from "./_fixtures/seed"
+import { tid } from "./_fixtures/testids"
// Journey 9 — threads. Creating a thread from a message surfaces a thread
// indicator on the parent message (regression ab572e3e).
@@ -79,6 +80,6 @@ test.describe.serial("threads", () => {
// Back on the parent channel, the message now carries a thread indicator.
await page.goto(`/c/channels/${serverId}/${channelId}`)
await expect(row).toBeVisible({ timeout: 20_000 })
- await expect(page.locator(`[data-testid^="community-thread-indicator-"]`).first()).toBeVisible({ timeout: 15_000 })
+ await expect(page.locator(`[data-testid^="${tid.threadIndicator("")}"]`).first()).toBeVisible({ timeout: 15_000 })
})
})
diff --git a/src/web/src/test/e2e-ui/10-eject-nav-auth.spec.ts b/src/web/src/test/e2e-ui/10-eject-nav-auth.spec.ts
index 4bbdcb72e..f516bb542 100644
--- a/src/web/src/test/e2e-ui/10-eject-nav-auth.spec.ts
+++ b/src/web/src/test/e2e-ui/10-eject-nav-auth.spec.ts
@@ -37,9 +37,16 @@ test.describe.serial("eject, navigation & logout", () => {
await page.getByRole("button", { name: "Log Out" }).click()
await page.waitForURL(/\/sign-in/, { timeout: 20_000 , waitUntil: "commit" })
- // Session is actually cleared: revisiting a protected route stays bounced.
- await page.goto("/c")
- await page.waitForURL(/\/sign-in/, { timeout: 20_000 , waitUntil: "commit" })
+ // Session is actually cleared: a protected route returns a sign-in redirect.
+ await expect.poll(async () => (await page.context().cookies())
+ .some((cookie) => cookie.name.startsWith("better-auth"))).toBe(false)
+ const protectedResponse = await page.request.get("/c", { maxRedirects: 0 })
+ expect(protectedResponse.status()).toBe(307)
+ const redirectLocation = protectedResponse.headers().location
+ expect(redirectLocation).toBeTruthy()
+ const redirectUrl = new URL(redirectLocation!, page.url())
+ expect(redirectUrl.pathname).toBe("/sign-in")
+ expect(redirectUrl.searchParams.get("redirect")).toBe("/c")
await expect(page).toHaveURL(/\/sign-in/)
})
})
diff --git a/src/web/src/test/e2e-ui/16-jump-to-present.spec.ts b/src/web/src/test/e2e-ui/16-jump-to-present.spec.ts
index c38639955..49b266514 100644
--- a/src/web/src/test/e2e-ui/16-jump-to-present.spec.ts
+++ b/src/web/src/test/e2e-ui/16-jump-to-present.spec.ts
@@ -46,7 +46,7 @@ test("anchored channel jumps to the newest tail with one request", async ({ asUs
await expect(jumpButton).toBeHidden()
await expect(page.getByText("Loading newer messages…", { exact: true })).toHaveCount(0)
await expect.poll(() => newestGets).toBe(1)
- await page.waitForTimeout(1_000)
+ await page.waitForTimeout(1_000) // duplicate newest-fetch exclusion window
expect(newestGets).toBe(1)
expect(anchorGets).toBe(0)
})
diff --git a/src/web/src/test/e2e-ui/17-forum-sidebar-stage-b.spec.ts b/src/web/src/test/e2e-ui/17-forum-sidebar-stage-b.spec.ts
index 7f9bf7712..2d2298fce 100644
--- a/src/web/src/test/e2e-ui/17-forum-sidebar-stage-b.spec.ts
+++ b/src/web/src/test/e2e-ui/17-forum-sidebar-stage-b.spec.ts
@@ -84,7 +84,7 @@ test.describe.serial("forum sidebar Stage B request shape", () => {
await expect.poll(() => new URL(page.url()).pathname).toBe(
`/c/channels/${serverId}/${threadId}`,
)
- await page.waitForTimeout(500)
+ await page.waitForTimeout(500) // duplicate-anchor exclusion window
expect(anchorRouteRequests).toBe(1)
} finally {
releaseFirstAnchor()
@@ -128,7 +128,7 @@ test.describe.serial("forum sidebar Stage B request shape", () => {
await expect(page.getByRole("heading", { name: forumTitle })).toBeVisible({ timeout: 20_000 })
await expect(page.getByText("post body", { exact: true })).toBeVisible({ timeout: 20_000 })
await expect(page.locator('[data-slot="skeleton"]')).toHaveCount(0)
- await page.waitForTimeout(2_000)
+ await page.waitForTimeout(2_000) // late exact-channel/message fetch exclusion window
expect(requests.filter((url) => isSidebarRequest(url, serverId))).toHaveLength(1)
expect(new URL(requests.find((url) => isSidebarRequest(url, serverId))!).searchParams.get("retainId"))
@@ -168,7 +168,7 @@ test.describe.serial("forum sidebar Stage B request shape", () => {
await expect(page.getByRole("heading", { name: forumTitle })).toBeVisible({ timeout: 20_000 })
await expect(page.getByText("post body", { exact: true })).toBeVisible({ timeout: 20_000 })
await expect(page.locator('[data-slot="skeleton"]')).toHaveCount(0)
- await page.waitForTimeout(2_000)
+ await page.waitForTimeout(2_000) // late duplicate anchored/unanchored fetch exclusion window
const messageResponses = successfulResponses.filter((url) =>
isChannelMessagesRequest(url, threadId),
@@ -198,7 +198,7 @@ test.describe.serial("forum sidebar Stage B request shape", () => {
await page.getByTestId(tid.channelRow(textBId)).click()
await page.waitForURL(new RegExp(`${textBId}(?:\\?|$)`), { timeout: 20_000, waitUntil: "commit" })
await expect(page.getByTestId(tid.channelRow(textBId))).toBeVisible()
- await page.waitForTimeout(300)
+ await page.waitForTimeout(300) // post-switch sidebar refetch exclusion window
expect(requests.filter((url) => isSidebarRequest(url, serverId))).toHaveLength(0)
diff --git a/src/web/src/test/e2e-ui/18-new-divider-scroll.spec.ts b/src/web/src/test/e2e-ui/18-new-divider-scroll.spec.ts
index 3e1426f93..276350ca7 100644
--- a/src/web/src/test/e2e-ui/18-new-divider-scroll.spec.ts
+++ b/src/web/src/test/e2e-ui/18-new-divider-scroll.spec.ts
@@ -44,12 +44,7 @@ test("NEW divider does not fight upward scrolling while a peer message arrives",
const divider = page.getByTestId(tid.newDivider)
await expect(divider).toBeVisible({ timeout: 30_000 })
- await divider.evaluate((element) => {
- const scroller = element.closest<HTMLElement>(".overflow-y-auto")
- if (!scroller) throw new Error("message scroller not found")
- scroller.dataset.e2eMessageScroller = "true"
- })
- const scroller = page.locator("[data-e2e-message-scroller='true']")
+ const scroller = page.getByTestId(tid.messageScroller)
await expect(scroller).toHaveCount(1)
const box = await scroller.boundingBox()
expect(box).not.toBeNull()
@@ -66,16 +61,16 @@ test("NEW divider does not fight upward scrolling while a peer message arrives",
const samples: number[] = [await scroller.evaluate((element) => element.scrollTop)]
const liveAppend = (async () => {
- await page.waitForTimeout(250)
+ await page.waitForTimeout(250) // inject the append during the upward-scroll gesture
return seedMessage("bob", channelId, `live during upward scroll ${Date.now()}`)
})()
for (let index = 0; index < 60; index++) {
await page.mouse.wheel(0, -24)
- await page.waitForTimeout(24)
+ await page.waitForTimeout(24) // fixed wheel-event cadence under test
samples.push(await scroller.evaluate((element) => element.scrollTop))
}
await liveAppend
- await page.waitForTimeout(250)
+ await page.waitForTimeout(250) // post-append scroll-correction exclusion window
samples.push(await scroller.evaluate((element) => element.scrollTop))
const scrollEvents = await page.evaluate(() => (
window as unknown as { __newDividerScrollEvents: number[] }
diff --git a/src/web/src/test/e2e-ui/19-marketing-seo.spec.ts b/src/web/src/test/e2e-ui/19-marketing-seo.spec.ts
index 5e96a524f..62f0c4ac5 100644
--- a/src/web/src/test/e2e-ui/19-marketing-seo.spec.ts
+++ b/src/web/src/test/e2e-ui/19-marketing-seo.spec.ts
@@ -1,4 +1,4 @@
-import { expect, test } from "@playwright/test"
+import { expect, test } from "./_fixtures/community-fixture"
test("homepage SSR keeps product demos out of the heading tree", async ({ page, request }) => {
const response = await request.get("/")
diff --git a/src/web/src/test/e2e-ui/20-community-attachment-thumbnails.spec.ts b/src/web/src/test/e2e-ui/20-community-attachment-thumbnails.spec.ts
index a7ff6d47d..ca8c7d615 100644
--- a/src/web/src/test/e2e-ui/20-community-attachment-thumbnails.spec.ts
+++ b/src/web/src/test/e2e-ui/20-community-attachment-thumbnails.spec.ts
@@ -85,8 +85,54 @@ async function previewRects(page: Page) {
}
async function waitForDialogEntrance(page: Page) {
- await expect(page.getByTestId(tid.imageLightbox)).toBeVisible()
- await page.waitForTimeout(200)
+ const lightbox = page.getByTestId(tid.imageLightbox)
+ await expect(lightbox).toBeVisible()
+ await lightbox.evaluate((element, ids) => {
+ const dialog = element.closest<HTMLElement>("[data-slot='dialog-content']")
+ if (!dialog) throw new Error("lightbox dialog content disappeared")
+ return Promise.all(dialog.getAnimations({ subtree: true }).map((animation) => animation.finished))
+ .then(() => new Promise<void>((resolveStable, rejectStable) => {
+ let previous = ""
+ let unchangedFrames = 0
+ let observedFrames = 0
+ const sample = () => {
+ const container = document.querySelector<HTMLElement>(`[data-testid="${ids.container}"]`)
+ if (!container) {
+ rejectStable(new Error("lightbox container disappeared"))
+ return
+ }
+ // The failed-original state intentionally unmounts the original
+ // image. Sample whichever image layers currently exist; each test
+ // phase separately asserts its required layer before measuring.
+ const nodes = [
+ container,
+ document.querySelector<HTMLElement>(`[data-testid="${ids.thumbnail}"]`),
+ document.querySelector<HTMLElement>(`[data-testid="${ids.original}"]`),
+ ].filter((node): node is HTMLElement => node !== null)
+ const current = nodes.map((node) => {
+ const rect = node.getBoundingClientRect()
+ return [rect.x, rect.y, rect.width, rect.height].join(":")
+ }).join("|")
+ unchangedFrames = current === previous ? unchangedFrames + 1 : 0
+ previous = current
+ observedFrames += 1
+ if (unchangedFrames >= 1) {
+ resolveStable()
+ return
+ }
+ if (observedFrames >= 120) {
+ rejectStable(new Error("lightbox geometry did not stabilize within 120 animation frames"))
+ return
+ }
+ requestAnimationFrame(sample)
+ }
+ requestAnimationFrame(sample)
+ }))
+ }, {
+ container: tid.imageLightbox,
+ thumbnail: tid.imageLightboxThumbnail,
+ original: tid.imageLightboxOriginal,
+ })
}
async function attachScreenshot(testInfo: TestInfo, name: string, page: Page) {
@@ -116,7 +162,7 @@ async function uploadImage(args: {
const editable = composerEditable(page)
await editable.click()
await editable.pressSequentially(message)
- await page.locator('input[type="file"]').last().setInputFiles({
+ await page.getByTestId(tid.composerFileInput).setInputFiles({
name,
mimeType,
buffer,
diff --git a/src/web/src/test/e2e-ui/21-mobile-message-layout.spec.ts b/src/web/src/test/e2e-ui/21-mobile-message-layout.spec.ts
index e737b9dec..d4323598e 100644
--- a/src/web/src/test/e2e-ui/21-mobile-message-layout.spec.ts
+++ b/src/web/src/test/e2e-ui/21-mobile-message-layout.spec.ts
@@ -1,6 +1,7 @@
import type { Page } from "@playwright/test"
import { test, expect } from "./_fixtures/community-fixture"
import { seedChannel, seedMessage, seedServer } from "./_fixtures/seed"
+import { tid } from "./_fixtures/testids"
type RowRect = { top: number; bottom: number; height: number }
@@ -20,6 +21,39 @@ async function expectRowsNotToOverlap(page: Page, messageIds: string[]): Promise
}).toBe(true)
}
+async function waitForStableScrollerFrames(page: Page, messageId: string): Promise<void> {
+ await page.getByTestId(tid.messageScroller).evaluate((element, targetMessageId) => (
+ new Promise<void>((resolveStable, rejectStable) => {
+ let previous = ""
+ let unchangedFrames = 0
+ let observedFrames = 0
+ const sample = () => {
+ const message = document.querySelector<HTMLElement>(`[data-msg-id="${CSS.escape(targetMessageId)}"]`)
+ const row = message?.parentElement
+ if (!row) {
+ rejectStable(new Error(`message row ${targetMessageId} disappeared`))
+ return
+ }
+ const rect = row.getBoundingClientRect()
+ const current = [element.scrollTop, element.scrollHeight, rect.top, rect.bottom, rect.height].join(":")
+ unchangedFrames = current === previous ? unchangedFrames + 1 : 0
+ previous = current
+ observedFrames += 1
+ if (unchangedFrames >= 1) {
+ resolveStable()
+ return
+ }
+ if (observedFrames >= 120) {
+ rejectStable(new Error("message scroller did not stabilize within 120 animation frames"))
+ return
+ }
+ requestAnimationFrame(sample)
+ }
+ requestAnimationFrame(sample)
+ })
+ ), messageId)
+}
+
test("long Markdown rows stay measured and separated at iPhone width", async ({ asUser }) => {
test.setTimeout(120_000)
const serverId = await seedServer("alice", `Mobile rows ${Date.now()}`)
@@ -53,15 +87,13 @@ test("long Markdown rows stay measured and separated at iPhone width", async ({
expect(firstMeasurement.height).toBeGreaterThan(400)
await expectRowsNotToOverlap(page, [longId, codeId, sentinelId])
- const scroller = page.locator(`[data-msg-id="${sentinelId}"]`).locator("xpath=ancestor::*[contains(@class, 'overflow-y-auto')][1]")
+ const scroller = page.getByTestId(tid.messageScroller)
await expect(scroller).toHaveCount(1)
await scroller.evaluate((element) => { element.scrollTop = element.scrollHeight })
await expect.poll(async () => scroller.evaluate((element) => (
element.scrollHeight - element.clientHeight - element.scrollTop
))).toBeLessThanOrEqual(2)
- // Let the virtualizer consume the scroll event and leave its transient
- // scrolling state before simulating delayed Markdown growth.
- await page.waitForTimeout(250)
+ await waitForStableScrollerFrames(page, codeId)
const beforeGrowth = await messageRowRect(page, codeId)
await page.locator(`[data-msg-id="${codeId}"] [data-community-message-body]`).evaluate((messageBody: Element) => {
@@ -88,7 +120,7 @@ test("long Markdown rows stay measured and separated at iPhone width", async ({
await expect.poll(async () => scroller.evaluate((element) => (
element.scrollHeight - element.clientHeight - element.scrollTop
))).toBeGreaterThan(100)
- await page.waitForTimeout(250)
+ await waitForStableScrollerFrames(page, codeId)
const readingAnchorBefore = await messageRowRect(page, codeId)
const readingScrollBefore = await scroller.evaluate((element) => {
@@ -117,7 +149,7 @@ test("long Markdown rows stay measured and separated at iPhone width", async ({
await expect.poll(async () => (await messageRowRect(page, codeId)).height)
.toBeGreaterThan(beforeReadingGrowth.height + 200)
await expectRowsNotToOverlap(page, [longId, codeId, sentinelId])
- await page.waitForTimeout(500)
+ await page.waitForTimeout(500) // post-growth downward-yank exclusion window
const readingAnchorAfter = await messageRowRect(page, codeId)
const readingScrollAfter = await scroller.evaluate((element) => ({
diff --git a/src/web/src/test/e2e-ui/22-committed-message-delivery.spec.ts b/src/web/src/test/e2e-ui/22-committed-message-delivery.spec.ts
index 3e0f66525..63994fc08 100644
--- a/src/web/src/test/e2e-ui/22-committed-message-delivery.spec.ts
+++ b/src/web/src/test/e2e-ui/22-committed-message-delivery.spec.ts
@@ -15,6 +15,7 @@ import {
proxyCommunityWebSockets,
type CapturedCommunityFrame,
} from "./_fixtures/community-ws-proxy"
+import { tid } from "./_fixtures/testids"
function messageFrame(frame: CapturedCommunityFrame, channelId: string, content: string): boolean {
return communityFrameEvents(frame).some((event) =>
@@ -35,7 +36,7 @@ test.describe.serial("committed message delivery QA", () => {
let armed = false
const carolProxy = await proxyCommunityWebSockets(carol.context)
await gotoAfterUserWsAuth(carol.page, `/c/channels/${serverId}/${forumId}`)
- await expect(carol.page.getByTestId(`community-forum-post-${threadId}`)).toBeVisible({ timeout: 20_000 })
+ await expect(carol.page.getByTestId(tid.forumThreadCard(threadId))).toBeVisible({ timeout: 20_000 })
armed = true
const bob = await asUser("bob")
@@ -92,7 +93,7 @@ test.describe.serial("committed message delivery QA", () => {
const editable = composerEditable(alice.page)
await editable.click()
await editable.pressSequentially(`@${bobInfo.name.slice(0, 3)}`)
- await alice.page.getByTestId(`community-mention-option-${bobInfo.id}`).click()
+ await alice.page.getByTestId(tid.mentionOption(bobInfo.id)).click()
await editable.pressSequentially(` ${missing}`)
await alice.page.keyboard.press("Enter")
await expect.poll(() => dropped, { timeout: 20_000 }).toBe(true)
@@ -116,7 +117,7 @@ test.describe.serial("committed message delivery QA", () => {
const response = await bob.page.request.get("/api/community/servers")
const data = await response.json() as { servers: Array<{ id: string; mentions: number }> }
const authoritative = data.servers.find((server) => server.id === serverId)?.mentions ?? 0
- const badge = bob.page.getByTestId(`community-rail-unread-badge-${serverId}`)
+ const badge = bob.page.getByTestId(tid.railUnreadBadge(serverId))
const displayed = await badge.count() === 0
? 0
: Number((await badge.textContent())?.trim())
@@ -202,7 +203,7 @@ test.describe.serial("committed message delivery QA", () => {
const editable = composerEditable(alice.page)
await editable.click()
await editable.pressSequentially(`@${bobInfo.name.slice(0, 3)}`)
- await alice.page.getByTestId(`community-mention-option-${bobInfo.id}`).click()
+ await alice.page.getByTestId(tid.mentionOption(bobInfo.id)).click()
await editable.pressSequentially(` ${body}`)
await alice.page.keyboard.press("Enter")
await expect.poll(() => duplicated, { timeout: 20_000 }).toBe(true)
@@ -220,18 +221,18 @@ test.describe.serial("committed message delivery QA", () => {
])
const messageId = communityFrameEvents(batch)[0]?.message?.id
expect(messageId).toBeTruthy()
- await expect(bob.page.getByTestId(`community-message-${messageId}`)).toHaveCount(1)
+ await expect(bob.page.getByTestId(tid.message(messageId!))).toHaveCount(1)
const requestsBeforeReload = serverRequests.length
await gotoAfterUserWsAuth(bob.page, `/c/channels/${serverId}/${channelId}`)
- await expect(bob.page.getByTestId(`community-message-${messageId}`)).toHaveCount(1)
+ await expect(bob.page.getByTestId(tid.message(messageId!))).toHaveCount(1)
await expect.poll(() => serverRequests.length, { timeout: 20_000 })
.toBeGreaterThan(requestsBeforeReload)
const requestsBeforeReplay = serverRequests.length
proxy.replay(batch)
await expect.poll(() => serverRequests.length, { timeout: 20_000 })
.toBeGreaterThan(requestsBeforeReplay)
- await expect(bob.page.getByTestId(`community-message-${messageId}`)).toHaveCount(1)
+ await expect(bob.page.getByTestId(tid.message(messageId!))).toHaveCount(1)
})
test("Q9: every receiver gets the same batch-only channel and DM delivery", async ({ asUser }) => {
@@ -257,7 +258,7 @@ test.describe.serial("committed message delivery QA", () => {
const editable = composerEditable(alice.page)
await editable.click()
await editable.pressSequentially(`@${bobInfo.name.slice(0, 3)}`)
- await alice.page.getByTestId(`community-mention-option-${bobInfo.id}`).click()
+ await alice.page.getByTestId(tid.mentionOption(bobInfo.id)).click()
await editable.pressSequentially(" batch channel")
await alice.page.keyboard.press("Enter")
await expect.poll(() => firstProxy.frames.slice(firstChannelStart).some((frame) =>
diff --git a/src/web/src/test/e2e-ui/24-composer-overflow.spec.ts b/src/web/src/test/e2e-ui/24-composer-overflow.spec.ts
index c4b9aacfa..360f3bf08 100644
--- a/src/web/src/test/e2e-ui/24-composer-overflow.spec.ts
+++ b/src/web/src/test/e2e-ui/24-composer-overflow.spec.ts
@@ -44,9 +44,9 @@ type PlaceholderMetrics = {
async function placeholderMetrics(composer: Locator): Promise<PlaceholderMetrics> {
const paragraph = composer.locator(".tiptap p.is-editor-empty").first()
await expect(paragraph).toBeVisible()
- return paragraph.evaluate((node) => {
+ return paragraph.evaluate((node, ids) => {
const element = node as HTMLElement
- const composerElement = element.closest("[data-testid='community-composer-input']") as HTMLElement
+ const composerElement = element.closest(`[data-testid='${ids.composerInput}']`) as HTMLElement
const pseudo = getComputedStyle(element, "::before")
const text = element.dataset.placeholder ?? ""
const canvas = document.createElement("canvas")
@@ -58,7 +58,7 @@ async function placeholderMetrics(composer: Locator): Promise<PlaceholderMetrics
const composerRect = composerElement.getBoundingClientRect()
const clipsHorizontally = pseudo.overflowX === "hidden" || pseudo.overflowX === "clip"
const attachElement = composerElement.parentElement?.querySelector<HTMLElement>(
- "[data-testid='community-composer-attach']",
+ `[data-testid='${ids.composerAttach}']`,
)
const emojiElement = composerElement.parentElement?.querySelector<HTMLElement>(
"button[aria-label='Emoji picker']",
@@ -87,7 +87,7 @@ async function placeholderMetrics(composer: Locator): Promise<PlaceholderMetrics
attach: attachElement ? toRect(attachElement.getBoundingClientRect()) : null,
emoji: emojiElement ? toRect(emojiElement.getBoundingClientRect()) : null,
}
- })
+ }, { composerInput: tid.composerInput, composerAttach: tid.composerAttach })
}
function expectContainedPlaceholder(metrics: PlaceholderMetrics, expected: string, width: number): void {
diff --git a/src/web/src/test/e2e-ui/25-composer-accessory-rail-occupancy.spec.ts b/src/web/src/test/e2e-ui/25-composer-accessory-rail-occupancy.spec.ts
index 2d968f1d3..113918974 100644
--- a/src/web/src/test/e2e-ui/25-composer-accessory-rail-occupancy.spec.ts
+++ b/src/web/src/test/e2e-ui/25-composer-accessory-rail-occupancy.spec.ts
@@ -36,7 +36,7 @@ type RailMetrics = {
async function railMetrics(page: Page): Promise<RailMetrics> {
const rail = page.getByTestId(tid.composerAccessoryRail)
- const composer = page.locator("[data-onboarding-target='channel-composer']")
+ const composer = page.getByTestId(tid.channelComposerShell)
return rail.evaluate((element, ids) => {
const target = element as HTMLElement
const find = (id: string) => target.querySelector<HTMLElement>(`[data-testid='${id}']`)
@@ -56,7 +56,7 @@ async function railMetrics(page: Page): Promise<RailMetrics> {
return {
layout: target.dataset.layout ?? null,
rail: toRect(target.getBoundingClientRect()),
- composer: toRect(document.querySelector<HTMLElement>("[data-onboarding-target='channel-composer']")!.getBoundingClientRect()),
+ composer: toRect(document.querySelector<HTMLElement>(`[data-testid='${ids.composer}']`)!.getBoundingClientRect()),
typing: typing ? toRect(typing.getBoundingClientRect()) : null,
center: center ? toRect(center.getBoundingClientRect()) : null,
right: right ? toRect(right.getBoundingClientRect()) : null,
@@ -79,6 +79,7 @@ async function railMetrics(page: Page): Promise<RailMetrics> {
selection: tid.messageSelectionToolbar,
wsStatus: tid.wsStatus,
wsRetry: tid.wsRetry,
+ composer: tid.channelComposerShell,
}).then(async (metrics) => {
await expect(composer).toBeVisible()
return metrics
@@ -120,7 +121,13 @@ async function captureState(args: {
const evidence: Record<number, RailMetrics> = {}
for (const width of widths) {
await page.setViewportSize({ width, height: width >= 768 ? 900 : 844 })
- const composer = page.locator("[data-onboarding-target='channel-composer']")
+ const scrollRoot = page.getByTestId(tid.messageScroller)
+ await scrollRoot.evaluate((element, showScrollControl) => {
+ element.scrollTop = showScrollControl ? 0 : element.scrollHeight
+ element.dispatchEvent(new Event("scroll"))
+ }, center && !selection)
+ await expect(page.getByTestId(tid.scrollToPresent)).toHaveCount(center && !selection ? 1 : 0)
+ const composer = page.getByTestId(tid.channelComposerShell)
await expect.poll(() => composer.evaluate((element, viewportWidth) => {
const rect = element.getBoundingClientRect()
return viewportWidth < 640
@@ -130,7 +137,6 @@ async function captureState(args: {
const rail = page.getByTestId(tid.composerAccessoryRail)
await expect(rail).toHaveAttribute("data-layout", layout!)
await expect(page.getByTestId(tid.typingIndicator)).toHaveCount(typing ? 1 : 0)
- await expect(page.getByTestId(tid.scrollToPresent)).toHaveCount(center && !selection ? 1 : 0)
await expect(page.getByTestId(tid.messageSelectionToolbar)).toHaveCount(selection ? 1 : 0)
const rightControl = page.locator(
`[data-testid='${tid.wsStatus}'], [data-testid='${tid.wsRetry}']`,
@@ -166,8 +172,13 @@ async function captureState(args: {
async function captureEmptyState(page: Page, testInfo: TestInfo): Promise<void> {
for (const width of MOBILE_WIDTHS) {
await page.setViewportSize({ width, height: 844 })
+ await page.getByTestId(tid.messageScroller).evaluate((element) => {
+ element.scrollTop = element.scrollHeight
+ element.dispatchEvent(new Event("scroll"))
+ })
+ await expect(page.getByTestId(tid.scrollToPresent)).toHaveCount(0)
await expect(page.getByTestId(tid.composerAccessoryRail)).toHaveCount(0)
- await expect(page.locator("[data-onboarding-target='channel-composer']")).toBeVisible()
+ await expect(page.getByTestId(tid.channelComposerShell)).toBeVisible()
const documentWidths = await page.evaluate(() => ({
client: document.documentElement.clientWidth,
scroll: document.documentElement.scrollWidth,
@@ -214,11 +225,7 @@ test("composer accessory rail reallocates every occupied slot without overflow",
await sendMessage(bob.page, ping)
await expect(alice.page.getByText(ping)).toBeVisible()
- const scrollRoot = alice.page
- .locator("[data-onboarding-target='channel-composer']")
- .locator("xpath=ancestor::main[1]")
- .locator(".thin-scrollbar")
- .first()
+ const scrollRoot = alice.page.getByTestId(tid.messageScroller)
await expect.poll(() => scrollRoot.evaluate((element) => element.scrollHeight > element.clientHeight))
.toBe(true)
await scrollRoot.evaluate((element) => {
@@ -481,9 +488,19 @@ test("composer accessory rail reallocates every occupied slot without overflow",
})
await expect(alice.page.getByTestId(tid.scrollToPresent)).toBeVisible()
await alice.page.setViewportSize({ width: 430, height: 844 })
+ await scrollRoot.evaluate((element) => {
+ element.scrollTop = 0
+ element.dispatchEvent(new Event("scroll"))
+ })
+ await expect(alice.page.getByTestId(tid.scrollToPresent)).toBeVisible()
let metrics = await railMetrics(alice.page)
expect(Math.abs(metrics.center!.center - metrics.composer.center)).toBeLessThanOrEqual(1)
await alice.page.setViewportSize({ width: 1280, height: 900 })
+ await scrollRoot.evaluate((element) => {
+ element.scrollTop = 0
+ element.dispatchEvent(new Event("scroll"))
+ })
+ await expect(alice.page.getByTestId(tid.scrollToPresent)).toBeVisible()
metrics = await railMetrics(alice.page)
expect(Math.abs(metrics.center!.center - metrics.composer.center)).toBeLessThanOrEqual(1)
})
diff --git a/src/web/src/test/e2e-ui/29-multi-device-read-state.spec.ts b/src/web/src/test/e2e-ui/29-multi-device-read-state.spec.ts
index c3ee45ec2..b8d4a72e4 100644
--- a/src/web/src/test/e2e-ui/29-multi-device-read-state.spec.ts
+++ b/src/web/src/test/e2e-ui/29-multi-device-read-state.spec.ts
@@ -113,7 +113,7 @@ test("one human account converges read state across two browser profiles", async
expect((await channelRepair).status()).toBe(200)
await expect(deviceB.page.getByTestId(tid.inboxUnreadChannel(channelId))).toHaveCount(0)
await expect(deviceB.page.getByTestId(tid.inboxUnreadDm(dmId))).toBeVisible()
- await deviceA.page.waitForTimeout(1_200)
+ await deviceA.page.waitForTimeout(1_200) // duplicate channel-read repair exclusion window
expect(channelResponses).toEqual([200])
const afterChannelRead = await accountSnapshot()
expect(afterChannelRead.revision).toBe(beforeChannelRead.revision + 1)
@@ -151,7 +151,7 @@ test("one human account converges read state across two browser profiles", async
expect((await dmRepair).status()).toBe(200)
await expect(deviceB.page.getByTestId(tid.inboxUnreadDm(dmId))).toHaveCount(0)
await expect(deviceB.page.getByText("Caught up", { exact: true })).toBeVisible()
- await deviceA.page.waitForTimeout(1_200)
+ await deviceA.page.waitForTimeout(1_200) // duplicate DM-read repair exclusion window
expect(dmResponses).toEqual([200])
const afterDmRead = await accountSnapshot()
expect(afterDmRead.revision).toBe(beforeDmRead.revision + 1)
@@ -196,8 +196,8 @@ test("one human account converges read state across two browser profiles", async
await seedMessage("alice", channelId, `read-all channel ${stamp}`)
await seedDmMessage("alice", dmId, `read-all dm ${stamp}`)
- await gotoAfterUserWsAuth(deviceA.page, "/c/me")
- await gotoAfterUserWsAuth(deviceB.page, "/c/me")
+ await gotoAfterUserWsAuth(deviceA.page, "/c/me/friends")
+ await gotoAfterUserWsAuth(deviceB.page, "/c/me/friends")
await deviceA.page.getByRole("button", { name: "Inbox" }).click()
await deviceB.page.getByRole("button", { name: "Inbox" }).click()
await expect(deviceA.page.getByRole("button", { name: "Mark all read" })).toBeEnabled()
@@ -216,9 +216,22 @@ test("one human account converges read state across two browser profiles", async
])
const completedReadAllResponses = await Promise.all(readAllResponses)
expect(completedReadAllResponses.every((response) => response.status() === 200)).toBe(true)
- const readAllResults = await Promise.all(completedReadAllResponses.map(async (response) =>
- await response.json() as { changed: boolean; revision: number }))
- expect(readAllResults.filter((result) => result.changed)).toHaveLength(1)
+ const readAllResults = await Promise.all(completedReadAllResponses.map(async (response) => ({
+ path: new URL(response.url()).pathname,
+ ...await response.json() as { changed: boolean; revision: number },
+ })))
+ const changedReadAllResults = readAllResults.filter((result) => result.changed)
+ // Channel and DM read-all are deliberately separate routes. Each domain has
+ // one winner across the two concurrent profiles; requiring one winner total
+ // made the assertion depend on request scheduling rather than idempotency.
+ for (const path of [
+ "/api/community/users/me/inbox/unreads/read-all",
+ "/api/community/users/me/inbox/dms/read-all",
+ ]) {
+ expect(changedReadAllResults.filter((result) => result.path === path)).toHaveLength(1)
+ }
+ const changedRevisions = changedReadAllResults.map((result) => result.revision).sort((a, b) => a - b)
+ expect(new Set(changedRevisions).size).toBe(2)
await expect(deviceA.page.getByText("Caught up", { exact: true })).toBeVisible({ timeout: 20_000 })
await expect(deviceB.page.getByText("Caught up", { exact: true })).toBeVisible({ timeout: 20_000 })
@@ -230,15 +243,15 @@ test("one human account converges read state across two browser profiles", async
))
await expect.poll(() => readAllEvents(proxyA.frames, readAllFrameStarts[0]!).length, {
timeout: 20_000,
- }).toBe(1)
+ }).toBe(changedReadAllResults.length)
await expect.poll(() => readAllEvents(proxyB.frames, readAllFrameStarts[1]!).length, {
timeout: 20_000,
- }).toBe(1)
+ }).toBe(changedReadAllResults.length)
for (const events of [
readAllEvents(proxyA.frames, readAllFrameStarts[0]!),
readAllEvents(proxyB.frames, readAllFrameStarts[1]!),
]) {
- expect(events[0]?.revision).toBe(readAllResults.find((result) => result.changed)?.revision)
+ expect(events.map((event) => event.revision).sort((a, b) => a - b)).toEqual(changedRevisions)
expect(events.every((event) => event.inboxChanged && !("readStates" in event))).toBe(true)
}
@@ -330,7 +343,7 @@ test("hidden eager channel and DM mounts defer cross-device reads until visible"
.toBe(`/c/channels/${serverId}/${channelId}`)
await expect.poll(() => deviceA.page.evaluate(() => document.visibilityState)).toBe("hidden")
await expect(deviceA.page.getByText(channelBody, { exact: true })).toBeVisible()
- await deviceA.page.waitForTimeout(1_000)
+ await deviceA.page.waitForTimeout(1_000) // hidden-tab channel-read exclusion window
expect(channelReadResponses).toEqual([])
const hiddenChannel = await accountSnapshot()
expect(hiddenChannel.revision).toBe(beforeChannel.revision)
@@ -364,7 +377,7 @@ test("hidden eager channel and DM mounts defer cross-device reads until visible"
.toBe(`/c/me/${dmId}`)
await expect.poll(() => deviceA.page.evaluate(() => document.visibilityState)).toBe("hidden")
await expect(deviceA.page.getByText(dmBody, { exact: true })).toBeVisible()
- await deviceA.page.waitForTimeout(1_000)
+ await deviceA.page.waitForTimeout(1_000) // hidden-tab DM-read exclusion window
expect(dmReadResponses).toEqual([])
const hiddenDm = await accountSnapshot()
expect(hiddenDm.revision).toBe(beforeDm.revision)
@@ -401,11 +414,7 @@ test("a visible live tail clears both devices while an unseen tail stays unread"
await deviceB.page.getByRole("button", { name: "Inbox" }).click()
await expect(deviceB.page.getByTestId(tid.inboxUnreadChannel(channelId))).toBeVisible()
- const scroller = deviceA.page
- .locator("[data-onboarding-target='channel-composer']")
- .locator("xpath=ancestor::main[1]")
- .locator(".thin-scrollbar")
- .first()
+ const scroller = deviceA.page.getByTestId(tid.messageScroller)
await expect(deviceA.page.getByTestId(tid.scrollToPresent)).toBeVisible()
const baselineRead = deviceA.page.waitForResponse((response) =>
response.request().method() === "PUT"
@@ -475,7 +484,7 @@ test("a visible live tail clears both devices while an unseen tail stays unread"
}).toBeGreaterThan(0)
}
await expect(deviceB.page.getByTestId(tid.inboxUnreadChannel(channelId))).toBeVisible()
- await deviceA.page.waitForTimeout(1_000)
+ await deviceA.page.waitForTimeout(1_000) // hidden active-profile read exclusion window
expect(hiddenReadResponses).toEqual([])
const afterHidden = await (await deviceA.page.request.get(
"/api/community/users/me/read-state",
@@ -527,7 +536,7 @@ test("a visible live tail clears both devices while an unseen tail stays unread"
}).toBeGreaterThan(0)
}
await expect(deviceB.page.getByTestId(tid.inboxUnreadChannel(channelId))).toBeVisible()
- await deviceA.page.waitForTimeout(1_000)
+ await deviceA.page.waitForTimeout(1_000) // unseen-tail read exclusion window
expect(unseenReadResponses).toEqual([])
const afterUnseen = await (await deviceA.page.request.get(
"/api/community/users/me/read-state",
@@ -618,7 +627,7 @@ test("forum cards use the ordinary parent cursor while child cursors stay indepe
await expect.poll(() => forumResponses.length, {
timeout: 20_000,
}).toBe(1)
- await deviceA.page.waitForTimeout(1_200)
+ await deviceA.page.waitForTimeout(1_200) // duplicate forum-parent read exclusion window
expect(forumResponses).toEqual([
{ target: secondOpener!, status: 200 },
])
@@ -636,7 +645,7 @@ test("forum cards use the ordinary parent cursor while child cursors stay indepe
const beforeNavigationWrites = forumResponses.length
await gotoAfterUserWsAuth(deviceA.page, `/c/channels/${serverId}/${first.threadId}`)
- await deviceA.page.waitForTimeout(1_200)
+ await deviceA.page.waitForTimeout(1_200) // child-navigation parent-read exclusion window
expect(forumResponses).toHaveLength(beforeNavigationWrites)
expect((await accountSnapshot()).revision).toBe(afterVisible.revision)
@@ -659,7 +668,7 @@ test("forum cards use the ordinary parent cursor while child cursors stay indepe
await deviceB.page.getByRole("button", { name: "Inbox" }).click()
const third = await createEmptyPost(`Unified third ${stamp}`)
await expect(deviceB.page.getByTestId(tid.inboxUnreadChannel(forumId))).toBeVisible()
- await deviceA.page.waitForTimeout(1_200)
+ await deviceA.page.waitForTimeout(1_200) // hidden-device forum-child read exclusion window
expect(forumResponses).toHaveLength(1)
expect((await accountSnapshot()).revision).toBe(afterVisible.revision)
await setDeviceAVisibility("visible")
diff --git a/src/web/src/test/e2e-ui/30-bot-profile-audit-preview.spec.ts b/src/web/src/test/e2e-ui/30-bot-profile-audit-preview.spec.ts
index 205a635ce..4b2e0d25a 100644
--- a/src/web/src/test/e2e-ui/30-bot-profile-audit-preview.spec.ts
+++ b/src/web/src/test/e2e-ui/30-bot-profile-audit-preview.spec.ts
@@ -196,7 +196,7 @@ test("owner-only bot profile preview, owner swap, and URL-owned audit modal", as
"aria-label",
"Bot at rest. Open full bot activity log",
)
- await expect(alice.page.locator('[data-testid^="community-bot-audit-preview-row-"]'))
+ await expect(alice.page.locator(`[data-testid^="${tid.botAuditPreviewRow("")}"]`))
.toHaveCount(5)
expect(ownerAuditRequests.some((url) => new URL(url).searchParams.get("limit") === "5"))
.toBe(true)
@@ -216,7 +216,7 @@ test("owner-only bot profile preview, owner swap, and URL-owned audit modal", as
await expect.poll(() => preview.evaluate((element) =>
getComputedStyle(element, "::before").animationName))
.toBe("bot-audit-heartbeat")
- await expect(alice.page.locator('[data-testid^="community-bot-audit-preview-row-"]'))
+ await expect(alice.page.locator(`[data-testid^="${tid.botAuditPreviewRow("")}"]`))
.toHaveCount(4)
await alice.page.setViewportSize({ width: 375, height: 812 })
@@ -242,16 +242,16 @@ test("owner-only bot profile preview, owner swap, and URL-owned audit modal", as
)
await alice.page.getByTestId(tid.botAuditPreview).click()
await expect(alice.page).toHaveURL(new RegExp(`/c/me/bots\\?audit=${botId}$`))
- await expect(alice.page.getByTestId("bot-activity-modal")).toBeVisible()
+ await expect(alice.page.getByTestId(tid.botActivityModal)).toBeVisible()
await alice.page.reload()
- await expect(alice.page.getByTestId("bot-activity-modal")).toBeVisible()
+ await expect(alice.page.getByTestId(tid.botActivityModal)).toBeVisible()
await alice.page.goBack({ waitUntil: "commit" })
await expect(alice.page).toHaveURL(new RegExp(`${route}$`))
await alice.page.goForward({ waitUntil: "commit" })
- await expect(alice.page.getByTestId("bot-activity-modal")).toBeVisible()
+ await expect(alice.page.getByTestId(tid.botActivityModal)).toBeVisible()
await alice.page.goto(`/c/me/bots?machineId=${machineId}&audit=${botId}`)
- await expect(alice.page.getByTestId("bot-activity-modal")).toBeVisible()
+ await expect(alice.page.getByTestId(tid.botActivityModal)).toBeVisible()
await alice.page.getByRole("button", { name: "Close", exact: true }).click()
await expect(alice.page).toHaveURL(`/c/me/bots?machineId=${machineId}`)
@@ -272,7 +272,7 @@ test("owner-only bot profile preview, owner swap, and URL-owned audit modal", as
await expect(bob.page.getByTestId(tid.profileOwnerLink)).toHaveCount(0)
await bob.page.goto(`/c/me/bots?audit=${botId}`)
- await expect(bob.page.getByTestId("bot-activity-modal")).toHaveCount(0)
+ await expect(bob.page.getByTestId(tid.botActivityModal)).toHaveCount(0)
await expect.poll(() => new URL(bob.page.url()).searchParams.has("audit")).toBe(false)
expect(bobAuditRequests).toEqual([])
} finally {
diff --git a/src/web/src/test/e2e-ui/_fixtures/community-fixture.ts b/src/web/src/test/e2e-ui/_fixtures/community-fixture.ts
index 2da84d9a7..1d1178c37 100644
--- a/src/web/src/test/e2e-ui/_fixtures/community-fixture.ts
+++ b/src/web/src/test/e2e-ui/_fixtures/community-fixture.ts
@@ -1,7 +1,12 @@
import { test as base, type BrowserContext, type Page } from "@playwright/test"
import { readFileSync } from "fs"
import { resolve } from "path"
-import { AUTH_DIR } from "../_setup/paths"
+import {
+ AUTH_DIR,
+ SERVICE_FAILURE_CLAIM_PATH,
+ SERVICE_STATE_PATH,
+} from "../_setup/paths"
+import { claimServiceFailure, type FailureDecision } from "../_setup/service-lifecycle"
import { manifest } from "./manifest"
import type { UserKey } from "../_setup/users"
@@ -10,7 +15,37 @@ import type { UserKey } from "../_setup/users"
// realtime journeys can drive two or three sessions at once.
type AsUser = (key: UserKey) => Promise<{ context: BrowserContext; page: Page }>
-export const test = base.extend<{ asUser: AsUser }>({
+function throwOrSkipFailure(
+ decision: FailureDecision,
+ skip: (condition: boolean, description: string) => void,
+): void {
+ if (decision.action === "report") throw new Error(decision.message)
+ if (decision.action === "skip") skip(true, `${decision.message}; already reported by an earlier test`)
+}
+
+export const test = base.extend<{ asUser: AsUser; serviceGuard: void }>({
+ serviceGuard: [async ({}, provide, testInfo) => {
+ throwOrSkipFailure(
+ claimServiceFailure(SERVICE_STATE_PATH, SERVICE_FAILURE_CLAIM_PATH),
+ testInfo.skip.bind(testInfo),
+ )
+ let timer: ReturnType<typeof setInterval> | undefined
+ const failure = new Promise<never>((_, reject) => {
+ timer = setInterval(() => {
+ const decision = claimServiceFailure(SERVICE_STATE_PATH, SERVICE_FAILURE_CLAIM_PATH)
+ if (decision.action === "report") reject(new Error(decision.message))
+ }, 100)
+ })
+ try {
+ await Promise.race([provide(), failure])
+ } finally {
+ if (timer) clearInterval(timer)
+ }
+ throwOrSkipFailure(
+ claimServiceFailure(SERVICE_STATE_PATH, SERVICE_FAILURE_CLAIM_PATH),
+ testInfo.skip.bind(testInfo),
+ )
+ }, { auto: true }],
asUser: async ({ browser }, provide) => {
const opened: BrowserContext[] = []
const factory: AsUser = async (key) => {
diff --git a/src/web/src/test/e2e-ui/_setup/global-setup.ts b/src/web/src/test/e2e-ui/_setup/global-setup.ts
index 08f846a87..dcfefac38 100644
--- a/src/web/src/test/e2e-ui/_setup/global-setup.ts
+++ b/src/web/src/test/e2e-ui/_setup/global-setup.ts
@@ -1,45 +1,55 @@
import { mkdirSync, writeFileSync, rmSync } from "fs"
import { resolve } from "path"
import { chromium } from "@playwright/test"
-import { AUTH_DIR, MANIFEST_PATH } from "./paths"
+import { AUTH_DIR, MANIFEST_PATH, SERVICE_STATE_PATH } from "./paths"
import { loginAndSaveState } from "./auth"
-import { prepareServices, resetDb, startServices, backupState, REUSE_EXISTING } from "./services"
+import {
+ prepareServices,
+ resetDb,
+ startServices,
+ backupState,
+ REUSE_EXISTING,
+ stopServicesAndRestore,
+} from "./services"
+import { createLifecycleState, writeLifecycleState } from "./service-lifecycle"
import { USER_KEYS, type RunManifest, type SeededUser, type UserKey } from "./users"
export default async function globalSetup(): Promise<void> {
rmSync(AUTH_DIR, { recursive: true, force: true })
mkdirSync(AUTH_DIR, { recursive: true })
+ writeLifecycleState(SERVICE_STATE_PATH, createLifecycleState())
- prepareServices()
+ try {
+ prepareServices()
- // Local runs: back up the developer's D1/DO state, wipe to a clean DB, and
- // restore it on teardown (see global-teardown). CI has no prior state.
- let backedUp = false
- if (!REUSE_EXISTING) {
- backedUp = backupState()
- resetDb()
- }
- const services = await startServices()
+ // Local runs: back up the developer's D1/DO state, wipe to a clean DB, and
+ // restore it on teardown (see global-teardown). CI has no prior state.
+ let backedUp = false
+ if (!REUSE_EXISTING) {
+ backedUp = backupState()
+ writeLifecycleState(SERVICE_STATE_PATH, createLifecycleState(backedUp))
+ resetDb()
+ }
+ await startServices()
- // Unique-per-run stamp so re-runs against a non-reset DB don't collide.
- const stamp = `${process.pid.toString(36)}${Math.floor(process.hrtime()[1] / 1e3).toString(36)}`
+ // Unique-per-run stamp so re-runs against a non-reset DB don't collide.
+ const stamp = `${process.pid.toString(36)}${Math.floor(process.hrtime()[1] / 1e3).toString(36)}`
- const users = {} as Record<UserKey, SeededUser>
- const browser = await chromium.launch()
- try {
- for (const key of USER_KEYS) {
- const statePath = resolve(AUTH_DIR, `${key}.json`)
- users[key] = await loginAndSaveState(browser, key, stamp, statePath)
+ const users = {} as Record<UserKey, SeededUser>
+ const browser = await chromium.launch()
+ try {
+ for (const key of USER_KEYS) {
+ const statePath = resolve(AUTH_DIR, `${key}.json`)
+ users[key] = await loginAndSaveState(browser, key, stamp, statePath)
+ }
+ } finally {
+ await browser.close()
}
- } finally {
- await browser.close()
- }
- const manifest: RunManifest & { servicePids: number[]; restoreState: boolean } = {
- stamp,
- users,
- servicePids: services.map((s) => s.proc.pid).filter((p): p is number => !!p),
- restoreState: backedUp,
+ const manifest: RunManifest = { stamp, users }
+ writeFileSync(MANIFEST_PATH, JSON.stringify(manifest, null, 2))
+ } catch (error) {
+ await stopServicesAndRestore()
+ throw error
}
- writeFileSync(MANIFEST_PATH, JSON.stringify(manifest, null, 2))
}
diff --git a/src/web/src/test/e2e-ui/_setup/global-teardown.ts b/src/web/src/test/e2e-ui/_setup/global-teardown.ts
index 26b7f33d2..6efa5dd64 100644
--- a/src/web/src/test/e2e-ui/_setup/global-teardown.ts
+++ b/src/web/src/test/e2e-ui/_setup/global-teardown.ts
@@ -1,25 +1,5 @@
-import { readFileSync, existsSync } from "fs"
-import { MANIFEST_PATH } from "./paths"
-import { restoreState } from "./services"
+import { stopServicesAndRestore } from "./services"
export default async function globalTeardown(): Promise<void> {
- if (!existsSync(MANIFEST_PATH)) return
- const manifest = JSON.parse(readFileSync(MANIFEST_PATH, "utf8")) as {
- servicePids?: number[]
- restoreState?: boolean
- }
- // Stop the dev servers first so they release the D1/DO state dir before we
- // swap the developer's backed-up data back in.
- for (const pid of manifest.servicePids ?? []) {
- try {
- process.kill(-pid, "SIGTERM")
- } catch {
- // already gone
- }
- }
- if (manifest.restoreState) {
- // Give miniflare a moment to release file handles on the state dir.
- await new Promise((r) => setTimeout(r, 1500))
- restoreState()
- }
+ await stopServicesAndRestore()
}
diff --git a/src/web/src/test/e2e-ui/_setup/paths.ts b/src/web/src/test/e2e-ui/_setup/paths.ts
index fc5a60496..feb474e8d 100644
--- a/src/web/src/test/e2e-ui/_setup/paths.ts
+++ b/src/web/src/test/e2e-ui/_setup/paths.ts
@@ -8,6 +8,8 @@ export const REPO_ROOT = resolve(__dirname, "../../../../../..")
export const AUTH_DIR = resolve(__dirname, "../.auth")
export const MANIFEST_PATH = resolve(AUTH_DIR, "manifest.json")
export const SERVICE_LOG_DIR = resolve(REPO_ROOT, "src/web/e2e-service-logs")
+export const SERVICE_STATE_PATH = resolve(AUTH_DIR, "service-state.json")
+export const SERVICE_FAILURE_CLAIM_PATH = resolve(AUTH_DIR, "service-failure-claimed")
export const WEB_URL = process.env.ALOOK_SERVER_URL || "http://localhost:3000"
diff --git a/src/web/src/test/e2e-ui/_setup/service-lifecycle.ts b/src/web/src/test/e2e-ui/_setup/service-lifecycle.ts
new file mode 100644
index 000000000..97cc206cc
--- /dev/null
+++ b/src/web/src/test/e2e-ui/_setup/service-lifecycle.ts
@@ -0,0 +1,159 @@
+import {
+ closeSync,
+ existsSync,
+ openSync,
+ readFileSync,
+ renameSync,
+ writeFileSync,
+} from "fs"
+
+export type ServiceLifecycleStatus = "starting" | "healthy" | "failed" | "stopping" | "stopped"
+
+export interface ServiceProcessState {
+ name: string
+ pid?: number
+ startedAt: string
+ healthUrl: string
+ expectedStatus: number
+ expectedBody: { status: "ok" }
+ logPath: string
+ internalLogDirectory: string
+}
+
+export interface ServiceFailure {
+ service: string
+ kind: "child-close" | "health"
+ at: string
+ code?: number | null
+ signal?: NodeJS.Signals | null
+ healthStatus?: number | null
+ detail: string
+}
+
+export interface ServiceLifecycleState {
+ status: ServiceLifecycleStatus
+ restorePending: boolean
+ services: ServiceProcessState[]
+ health: Record<string, { consecutiveFailures: number; firstFailureAt?: number }>
+ failure?: ServiceFailure
+}
+
+export function createLifecycleState(restorePending = false): ServiceLifecycleState {
+ return { status: "starting", restorePending, services: [], health: {} }
+}
+
+export function readLifecycleState(path: string): ServiceLifecycleState | null {
+ if (!existsSync(path)) return null
+ return JSON.parse(readFileSync(path, "utf8")) as ServiceLifecycleState
+}
+
+export function writeLifecycleState(path: string, state: ServiceLifecycleState): void {
+ const temporary = `${path}.${process.pid}.tmp`
+ writeFileSync(temporary, `${JSON.stringify(state, null, 2)}\n`)
+ renameSync(temporary, path)
+}
+
+export function addServiceProcess(
+ state: ServiceLifecycleState,
+ service: ServiceProcessState,
+): ServiceLifecycleState {
+ return {
+ ...state,
+ services: [...state.services.filter((entry) => entry.name !== service.name), service],
+ health: {
+ ...state.health,
+ [service.name]: { consecutiveFailures: 0 },
+ },
+ }
+}
+
+export function failLifecycle(
+ state: ServiceLifecycleState,
+ failure: ServiceFailure,
+): ServiceLifecycleState {
+ if (state.status === "stopping" || state.status === "stopped" || state.failure) return state
+ return { ...state, status: "failed", failure }
+}
+
+export function applyHealthProbe(
+ state: ServiceLifecycleState,
+ service: string,
+ probe: { ok: boolean; now: number; healthStatus?: number | null; detail?: string },
+): ServiceLifecycleState {
+ if (state.status !== "healthy") return state
+ const current = state.health[service] ?? { consecutiveFailures: 0 }
+ if (probe.ok) {
+ return {
+ ...state,
+ health: { ...state.health, [service]: { consecutiveFailures: 0 } },
+ }
+ }
+ const next = {
+ consecutiveFailures: current.consecutiveFailures + 1,
+ firstFailureAt: current.firstFailureAt ?? probe.now,
+ }
+ const updated = { ...state, health: { ...state.health, [service]: next } }
+ if (next.consecutiveFailures < 3 || probe.now - next.firstFailureAt < 2_000) return updated
+ return failLifecycle(updated, {
+ service,
+ kind: "health",
+ at: new Date(probe.now).toISOString(),
+ healthStatus: probe.healthStatus,
+ detail: probe.detail ?? "three consecutive exact health probes failed",
+ })
+}
+
+export function serviceFailureMessage(state: ServiceLifecycleState): string {
+ const failure = state.failure
+ if (!failure) return "UI E2E infrastructure failure"
+ const service = state.services.find((entry) => entry.name === failure.service)
+ return [
+ `UI E2E infrastructure failure: ${failure.service} ${failure.kind}`,
+ `code=${failure.code ?? "none"}`,
+ `signal=${failure.signal ?? "none"}`,
+ `healthStatus=${failure.healthStatus ?? "none"}`,
+ `detail=${failure.detail}`,
+ `consoleLog=${service?.logPath ?? "unknown"}`,
+ `internalLogs=${service?.internalLogDirectory ?? "unknown"}`,
+ ].join("; ")
+}
+
+export type FailureDecision = { action: "continue" } | { action: "report" | "skip"; message: string }
+
+export function claimServiceFailure(statePath: string, claimPath: string): FailureDecision {
+ const state = readLifecycleState(statePath)
+ if (!state?.failure) return { action: "continue" }
+ const message = serviceFailureMessage(state)
+ try {
+ const descriptor = openSync(claimPath, "wx")
+ closeSync(descriptor)
+ return { action: "report", message }
+ } catch (error) {
+ if ((error as NodeJS.ErrnoException).code === "EEXIST") return { action: "skip", message }
+ throw error
+ }
+}
+
+export async function cleanupLifecycle(
+ state: ServiceLifecycleState,
+ dependencies: {
+ terminate: (pid: number, signal: "SIGTERM" | "SIGKILL") => void
+ waitForStop: (pids: number[], timeoutMs: number) => Promise<boolean>
+ restore: () => void
+ },
+): Promise<ServiceLifecycleState> {
+ if (state.status === "stopped") return state
+ const stopping: ServiceLifecycleState = { ...state, status: "stopping" }
+ const pids = stopping.services
+ .map((service) => service.pid)
+ .filter((pid): pid is number => Number.isInteger(pid) && Number(pid) > 0)
+ for (const pid of pids) dependencies.terminate(pid, "SIGTERM")
+ if (!(await dependencies.waitForStop(pids, 5_000))) {
+ for (const pid of pids) dependencies.terminate(pid, "SIGKILL")
+ if (!(await dependencies.waitForStop(pids, 2_000))) {
+ throw new Error(`E2E services did not stop: ${pids.join(", ")}`)
+ }
+ }
+ if (stopping.restorePending) dependencies.restore()
+ return { ...stopping, status: "stopped", restorePending: false }
+}
diff --git a/src/web/src/test/e2e-ui/_setup/services.ts b/src/web/src/test/e2e-ui/_setup/services.ts
index f2c7b1c86..27ee99976 100644
--- a/src/web/src/test/e2e-ui/_setup/services.ts
+++ b/src/web/src/test/e2e-ui/_setup/services.ts
@@ -1,7 +1,16 @@
import { spawn, spawnSync, type ChildProcess } from "child_process"
import { closeSync, cpSync, existsSync, mkdirSync, openSync, rmSync } from "fs"
import { resolve } from "path"
-import { REPO_ROOT, SERVICE_LOG_DIR, WEB_URL, WS_URL } from "./paths"
+import { REPO_ROOT, SERVICE_LOG_DIR, SERVICE_STATE_PATH, WEB_URL, WS_URL } from "./paths"
+import {
+ addServiceProcess,
+ applyHealthProbe,
+ cleanupLifecycle,
+ failLifecycle,
+ readLifecycleState,
+ writeLifecycleState,
+ type ServiceLifecycleState,
+} from "./service-lifecycle"
export interface ManagedService {
name: string
@@ -13,6 +22,29 @@ export interface ServiceDefinition {
name: string
args: string[]
healthUrl: string
+ expectedStatus: number
+ expectedBody: { status: "ok" }
+}
+
+export function wranglerLogEnvironment(internalLogDirectory: string): Record<
+ "WRANGLER_LOG" | "WRANGLER_LOG_PATH" | "WRANGLER_LOG_SANITIZE",
+ string
+> {
+ return {
+ WRANGLER_LOG: "debug",
+ WRANGLER_LOG_PATH: internalLogDirectory,
+ WRANGLER_LOG_SANITIZE: "true",
+ }
+}
+
+export function readinessExitMessage(
+ service: string,
+ exitCode: number | null,
+ signalCode: NodeJS.Signals | null,
+): string | null {
+ return exitCode === null && signalCode === null
+ ? null
+ : `${service} exited before readiness (code ${exitCode}, signal ${signalCode})`
}
// Reuse a server the developer already has running (local iteration). CI
@@ -43,24 +75,127 @@ export function restoreState(): void {
rmSync(STATE_BACKUP_DIR, { recursive: true, force: true })
}
-async function isUp(url: string): Promise<boolean> {
+export async function hasExactHealth(
+ url: string,
+ fetchImpl: typeof fetch = fetch,
+ options: { signal?: AbortSignal; timeoutMs?: number } = {},
+): Promise<{ ok: boolean; status: number | null; detail: string }> {
+ const timeoutMs = options.timeoutMs ?? 3_000
+ if (!Number.isFinite(timeoutMs) || timeoutMs <= 0) {
+ throw new Error("health probe timeout must be a positive finite number")
+ }
+ const controller = new AbortController()
+ const abortFromCaller = () => controller.abort(options.signal?.reason)
+ if (options.signal?.aborted) abortFromCaller()
+ else options.signal?.addEventListener("abort", abortFromCaller, { once: true })
+
+ let deadline: ReturnType<typeof setTimeout> | undefined
+ const timeoutResult = new Promise<{ ok: false; status: null; detail: string }>((resolveTimeout) => {
+ deadline = setTimeout(() => {
+ controller.abort(new Error(`health probe timed out after ${timeoutMs}ms`))
+ resolveTimeout({ ok: false, status: null, detail: `health probe timed out after ${timeoutMs}ms` })
+ }, timeoutMs)
+ })
+ const fetchResult = (async () => {
+ try {
+ const res = await fetchImpl(url, { signal: controller.signal })
+ const body = await res.json().catch(() => null)
+ const ok = res.status === 200
+ && body !== null
+ && typeof body === "object"
+ && Object.keys(body).length === 1
+ && (body as { status?: unknown }).status === "ok"
+ return { ok, status: res.status, detail: ok ? "exact health matched" : `unexpected health payload ${JSON.stringify(body)}` }
+ } catch (error) {
+ return { ok: false, status: null, detail: error instanceof Error ? error.message : String(error) }
+ }
+ })()
+
try {
- const res = await fetch(url)
- return res.status < 500
- } catch {
- return false
+ return await Promise.race([fetchResult, timeoutResult])
+ } finally {
+ if (deadline) clearTimeout(deadline)
+ options.signal?.removeEventListener("abort", abortFromCaller)
+ controller.abort()
}
}
-async function waitForHealth(url: string, name: string, timeoutMs = 90_000): Promise<void> {
+function childCloseRace(service: ManagedService, abortProbe: AbortController): {
+ promise: Promise<never>
+ cleanup: () => void
+} {
+ const onClose = (code: number | null, signal: NodeJS.Signals | null) => {
+ rejectClose(new Error(
+ readinessExitMessage(service.name, code, signal)
+ ?? `${service.name} closed before readiness`,
+ ))
+ abortProbe.abort()
+ }
+ let rejectClose!: (reason: Error) => void
+ const promise = new Promise<never>((_resolve, reject) => {
+ rejectClose = reject
+ service.proc.once("close", onClose)
+ })
+ return {
+ promise,
+ cleanup: () => service.proc.removeListener("close", onClose),
+ }
+}
+
+export async function waitForHealth(
+ service: ManagedService,
+ timeoutMs = 90_000,
+ fetchImpl: typeof fetch = fetch,
+ probeTimeoutMs = 3_000,
+): Promise<void> {
const start = Date.now()
// Date.now() is fine here — this runs in the Playwright config process
// (node), not inside a workflow script.
while (Date.now() - start < timeoutMs) {
- if (await isUp(url)) return
- await new Promise((r) => setTimeout(r, 2000))
+ const exitMessage = readinessExitMessage(
+ service.name,
+ service.proc.exitCode,
+ service.proc.signalCode,
+ )
+ if (exitMessage) throw new Error(exitMessage)
+ const remainingMs = timeoutMs - (Date.now() - start)
+ if (remainingMs <= 0) break
+ const probeController = new AbortController()
+ const close = childCloseRace(service, probeController)
+ let result: Awaited<ReturnType<typeof hasExactHealth>>
+ try {
+ const subscribedExitMessage = readinessExitMessage(
+ service.name,
+ service.proc.exitCode,
+ service.proc.signalCode,
+ )
+ if (subscribedExitMessage) throw new Error(subscribedExitMessage)
+ result = await Promise.race([
+ hasExactHealth(service.healthUrl, fetchImpl, {
+ signal: probeController.signal,
+ timeoutMs: Math.min(probeTimeoutMs, remainingMs),
+ }),
+ close.promise,
+ ])
+ } finally {
+ close.cleanup()
+ probeController.abort()
+ }
+ if (result.ok) return
+ const delayMs = Math.min(1_000, timeoutMs - (Date.now() - start))
+ if (delayMs > 0) await new Promise((r) => setTimeout(r, delayMs))
}
- throw new Error(`${name} not ready after ${timeoutMs}ms (${url})`)
+ throw new Error(`${service.name} not ready after ${timeoutMs}ms (${service.healthUrl})`)
+}
+
+export async function waitForServicesReady(
+ services: ManagedService[],
+ wait: (service: ManagedService, timeoutMs?: number) => Promise<void> = waitForHealth,
+): Promise<void> {
+ // Keep the callback unary. Passing waitForHealth directly to map would feed
+ // the array index into its optional timeout argument (making service 0 fail
+ // immediately with a zero-millisecond timeout).
+ await Promise.all(services.map((service) => wait(service)))
}
export function prepareServices(): void {
@@ -135,8 +270,20 @@ export function serviceDefinitions(singleRuntime: boolean): ServiceDefinition[]
const wsHealth = `${WS_URL}/health`
if (!singleRuntime) {
return [
- { name: "web", args: ["--filter", "@alook/web", "dev"], healthUrl: webHealth },
- { name: "ws-do", args: ["--filter", "@alook/ws-do", "dev"], healthUrl: wsHealth },
+ {
+ name: "web",
+ args: ["--filter", "@alook/web", "dev"],
+ healthUrl: webHealth,
+ expectedStatus: 200,
+ expectedBody: { status: "ok" },
+ },
+ {
+ name: "ws-do",
+ args: ["--filter", "@alook/ws-do", "dev"],
+ healthUrl: wsHealth,
+ expectedStatus: 200,
+ expectedBody: { status: "ok" },
+ },
]
}
@@ -158,16 +305,50 @@ export function serviceDefinitions(singleRuntime: boolean): ServiceDefinition[]
"--show-interactive-dev-session=false",
],
healthUrl: webHealth,
+ expectedStatus: 200,
+ expectedBody: { status: "ok" },
}]
}
function startService(definition: ServiceDefinition): ManagedService {
- const logFd = openSync(resolve(SERVICE_LOG_DIR, `${definition.name}.log`), "a")
+ const logPath = resolve(SERVICE_LOG_DIR, `${definition.name}.log`)
+ const internalLogDirectory = resolve(SERVICE_LOG_DIR, `${definition.name}-wrangler-internal`)
+ mkdirSync(internalLogDirectory, { recursive: true })
+ const logFd = openSync(logPath, "a")
try {
const proc = spawn("pnpm", definition.args, {
cwd: REPO_ROOT,
stdio: ["ignore", logFd, logFd],
detached: true,
+ env: {
+ ...process.env,
+ ...wranglerLogEnvironment(internalLogDirectory),
+ },
+ })
+ if (!proc.pid) throw new Error(`${definition.name} did not expose a process ID`)
+ const state = readLifecycleState(SERVICE_STATE_PATH)
+ if (!state) throw new Error("service lifecycle state was not initialized")
+ writeLifecycleState(SERVICE_STATE_PATH, addServiceProcess(state, {
+ name: definition.name,
+ pid: proc.pid,
+ startedAt: new Date().toISOString(),
+ healthUrl: definition.healthUrl,
+ expectedStatus: definition.expectedStatus,
+ expectedBody: definition.expectedBody,
+ logPath,
+ internalLogDirectory,
+ }))
+ proc.once("close", (code, signal) => {
+ const current = readLifecycleState(SERVICE_STATE_PATH)
+ if (!current) return
+ writeLifecycleState(SERVICE_STATE_PATH, failLifecycle(current, {
+ service: definition.name,
+ kind: "child-close",
+ at: new Date().toISOString(),
+ code,
+ signal,
+ detail: "managed service process closed unexpectedly",
+ }))
})
return { name: definition.name, proc, healthUrl: definition.healthUrl }
} finally {
@@ -175,6 +356,45 @@ function startService(definition: ServiceDefinition): ManagedService {
}
}
+let healthSupervisor: ReturnType<typeof setInterval> | null = null
+let healthProbeInFlight = false
+
+export function stopHealthSupervisor(): void {
+ if (healthSupervisor) clearInterval(healthSupervisor)
+ healthSupervisor = null
+ healthProbeInFlight = false
+}
+
+function startHealthSupervisor(services: ManagedService[]): void {
+ stopHealthSupervisor()
+ const probe = async () => {
+ if (healthProbeInFlight) return
+ healthProbeInFlight = true
+ try {
+ for (const service of services) {
+ const result = await hasExactHealth(service.healthUrl)
+ const state = readLifecycleState(SERVICE_STATE_PATH)
+ if (!state) return
+ const updated = applyHealthProbe(state, service.name, {
+ ok: result.ok,
+ now: Date.now(),
+ healthStatus: result.status,
+ detail: result.detail,
+ })
+ writeLifecycleState(SERVICE_STATE_PATH, updated)
+ if (updated.status === "failed") {
+ stopHealthSupervisor()
+ return
+ }
+ }
+ } finally {
+ healthProbeInFlight = false
+ }
+ }
+ healthSupervisor = setInterval(() => { void probe() }, 1_000)
+ healthSupervisor.unref()
+}
+
// Starts web (:3000) + ws-do (:8789). Realtime journeys REQUIRE ws-do, so a
// missing ws health check is a hard failure (fail fast), never a silent
// degrade. Returns started services (empty when reusing an existing server).
@@ -203,7 +423,9 @@ export async function startServices(): Promise<ManagedService[]> {
const webHealth = `${WEB_URL}/api/health`
const wsHealth = `${WS_URL}/health`
- if (REUSE_EXISTING && (await isUp(webHealth)) && (await isUp(wsHealth))) {
+ if (REUSE_EXISTING && (await hasExactHealth(webHealth)).ok && (await hasExactHealth(wsHealth)).ok) {
+ const state = readLifecycleState(SERVICE_STATE_PATH)
+ if (state) writeLifecycleState(SERVICE_STATE_PATH, { ...state, status: "healthy" })
await warmUpRoutes()
return []
}
@@ -220,7 +442,50 @@ export async function startServices(): Promise<ManagedService[]> {
const services = serviceDefinitions(SINGLE_RUNTIME).map(startService)
- await Promise.all(services.map((s) => waitForHealth(s.healthUrl, s.name)))
+ await waitForServicesReady(services)
+ const state = readLifecycleState(SERVICE_STATE_PATH)
+ if (!state) throw new Error("service lifecycle state disappeared during readiness")
+ if (state.failure) throw new Error(`service failed during readiness: ${state.failure.detail}`)
+ writeLifecycleState(SERVICE_STATE_PATH, { ...state, status: "healthy" })
+ startHealthSupervisor(services)
await warmUpRoutes()
return services
}
+
+function processGroupIsAlive(pid: number): boolean {
+ try {
+ process.kill(process.platform === "win32" ? pid : -pid, 0)
+ return true
+ } catch {
+ return false
+ }
+}
+
+async function waitForProcessesToStop(pids: number[], timeoutMs: number): Promise<boolean> {
+ const deadline = Date.now() + timeoutMs
+ while (Date.now() < deadline) {
+ if (pids.every((pid) => !processGroupIsAlive(pid))) return true
+ await new Promise((resolveDelay) => setTimeout(resolveDelay, 50))
+ }
+ return pids.every((pid) => !processGroupIsAlive(pid))
+}
+
+export async function stopServicesAndRestore(): Promise<void> {
+ stopHealthSupervisor()
+ const state = readLifecycleState(SERVICE_STATE_PATH)
+ if (!state || state.status === "stopped") return
+ const stopping: ServiceLifecycleState = { ...state, status: "stopping" }
+ writeLifecycleState(SERVICE_STATE_PATH, stopping)
+ const stopped = await cleanupLifecycle(stopping, {
+ terminate: (pid, signal) => {
+ try {
+ process.kill(process.platform === "win32" ? pid : -pid, signal)
+ } catch {
+ return
+ }
+ },
+ waitForStop: waitForProcessesToStop,
+ restore: restoreState,
+ })
+ writeLifecycleState(SERVICE_STATE_PATH, stopped)
+}
diff --git a/src/web/src/test/e2e-ui/perf/perf-capture-types.ts b/src/web/src/test/e2e-ui/perf/perf-capture-types.ts
index 3edc98d63..648dd19e1 100644
--- a/src/web/src/test/e2e-ui/perf/perf-capture-types.ts
+++ b/src/web/src/test/e2e-ui/perf/perf-capture-types.ts
@@ -71,6 +71,7 @@ declare global {
__PERF_RESIZES__?: number[]
__PERF_RESIZE_OBSERVER__?: ResizeObserver
__PERF_ATTACH_RESIZE__?: () => boolean
+ __PERF_WAIT_FOR_STABLE_RESIZE__?: () => Promise<void>
__PERF_SKELETON_TS__?: number | null
__PERF_PAINTED_TS__?: number | null
}
diff --git a/src/web/src/test/e2e-ui/perf/switch.perf.ts b/src/web/src/test/e2e-ui/perf/switch.perf.ts
index 906e43ed2..b8f0599be 100644
--- a/src/web/src/test/e2e-ui/perf/switch.perf.ts
+++ b/src/web/src/test/e2e-ui/perf/switch.perf.ts
@@ -33,6 +33,7 @@ import { test, expect, type Page, type BrowserContext } from "@playwright/test"
import { readFileSync, mkdirSync, writeFileSync } from "node:fs"
import { resolve } from "node:path"
import { DEV_PASSWORD } from "@alook/shared"
+import { tid } from "../_fixtures/testids"
import type {
CacheState,
CaptureFile,
@@ -106,14 +107,44 @@ const INPAGE_SETUP = `
// detached node and never fire again — hence NO cross-switch guard here, and
// we disconnect the prior observer before re-observing the current root.
window.__PERF_ATTACH_RESIZE__ = () => {
- const root = document.querySelector('.thin-scrollbar');
+ const root = document.querySelector('[data-testid="${tid.messageScroller}"]');
if (!root) return false;
if (window.__PERF_RESIZE_OBSERVER__) window.__PERF_RESIZE_OBSERVER__.disconnect();
const rz = new ResizeObserver(() => { window.__PERF_RESIZES__.push(performance.now()); });
rz.observe(root);
+ if (root.firstElementChild) rz.observe(root.firstElementChild);
window.__PERF_RESIZE_OBSERVER__ = rz;
return true;
};
+ window.__PERF_WAIT_FOR_STABLE_RESIZE__ = () => new Promise((resolve, reject) => {
+ let previous = '';
+ let unchangedFrames = 0;
+ let observedFrames = 0;
+ const sample = () => {
+ const root = document.querySelector('[data-testid="${tid.messageScroller}"]');
+ const content = root?.firstElementChild;
+ if (!root || !content) {
+ reject(new Error('message scroller geometry target disappeared'));
+ return;
+ }
+ const rootRect = root.getBoundingClientRect();
+ const contentRect = content.getBoundingClientRect();
+ const current = [rootRect.width, rootRect.height, contentRect.width, contentRect.height, root.scrollHeight].join(':');
+ unchangedFrames = current === previous ? unchangedFrames + 1 : 0;
+ previous = current;
+ observedFrames += 1;
+ if (window.__PERF_RESIZES__.length > 0 && unchangedFrames >= 1) {
+ resolve();
+ return;
+ }
+ if (observedFrames >= 120) {
+ reject(new Error('ResizeObserver did not report stable message geometry'));
+ return;
+ }
+ requestAnimationFrame(sample);
+ };
+ requestAnimationFrame(sample);
+ });
})()
`
@@ -266,7 +297,7 @@ test("community switch perceived-latency capture", async ({ browser }) => {
// loading spell at all.
if (
window.__PERF_SKELETON_TS__ == null &&
- document.querySelector(".thin-scrollbar [data-slot='skeleton']")
+ document.querySelector(`[data-testid="${tid.messageScroller}"] [data-slot='skeleton']`)
) {
window.__PERF_SKELETON_TS__ = performance.now()
}
@@ -286,8 +317,7 @@ test("community switch perceived-latency capture", async ({ browser }) => {
// node — the bug that left contentStableTs perpetually null.
await page.evaluate(() => window.__PERF_ATTACH_RESIZE__?.())
- // Let reflow settle so the ResizeObserver captures content-stable.
- await page.waitForTimeout(600)
+ await page.evaluate(() => window.__PERF_WAIT_FOR_STABLE_RESIZE__?.())
const drained = await drainSwitch(page, t0, markName)
captured.push({ kind, targetId, cacheState, ...drained })
@@ -355,27 +385,63 @@ test("community switch perceived-latency capture", async ({ browser }) => {
const ch = channels[i]
await clearIdb()
await measureSwitch("channel", ch.id, "cold", async () => {
- await page.getByTestId(`community-channel-row-${ch.id}`).click()
+ await page.getByTestId(tid.channelRow(ch.id)).click()
})
// memory-warm: click away and back within staleTime.
const prev = channels[i - 1]
- await page.getByTestId(`community-channel-row-${prev.id}`).click()
- await page.waitForTimeout(200)
+ await page.getByTestId(tid.channelRow(prev.id)).click()
+ await page.waitForURL(new RegExp(`/c/channels/${targetServer.id}/${prev.id}$`), {
+ waitUntil: "commit",
+ })
+ await expect(page.getByTestId(tid.messageScroller).locator("[data-msg-id]").first()).toBeVisible()
await measureSwitch("channel", ch.id, "memory-warm", async () => {
- await page.getByTestId(`community-channel-row-${ch.id}`).click()
+ await page.getByTestId(tid.channelRow(ch.id)).click()
})
}
// --- disk-warm channel pass: let the app persist to IDB, reload preserving
// it, then measure the first switch (messages paint from disk, read-state
// still refetches). ---
- await page.waitForTimeout(1500) // let the throttled persist settle
+ const persistedKey = `alook:qc:v1:${manifest.owner.userId}:client`
+ const persistedChannelId = channels[Math.min(channels.length, N_CHANNEL + 1) - 1]!.id
+ await expect.poll(() => page.evaluate(({ key, channelId }) => (
+ new Promise<boolean>((resolvePersisted, rejectPersisted) => {
+ const open = indexedDB.open("keyval-store")
+ open.onerror = () => rejectPersisted(open.error ?? new Error("failed to open query persister"))
+ open.onsuccess = () => {
+ const db = open.result
+ if (!db.objectStoreNames.contains("keyval")) {
+ db.close()
+ resolvePersisted(false)
+ return
+ }
+ const tx = db.transaction("keyval", "readonly")
+ const request = tx.objectStore("keyval").get(key)
+ request.onsuccess = () => {
+ db.close()
+ if (typeof request.result !== "string") {
+ resolvePersisted(false)
+ return
+ }
+ const client = JSON.parse(request.result) as {
+ clientState?: { queries?: Array<{ queryKey?: unknown[] }> }
+ }
+ resolvePersisted(client.clientState?.queries?.some((query) =>
+ Array.isArray(query.queryKey) && query.queryKey.includes(channelId)) ?? false)
+ }
+ request.onerror = () => {
+ db.close()
+ rejectPersisted(request.error ?? new Error("failed to read query persister"))
+ }
+ }
+ })
+ ), { key: persistedKey, channelId: persistedChannelId })).toBe(true)
await page.reload({ waitUntil: "commit" })
await page.waitForSelector("[data-msg-id]", { timeout: 20_000 }).catch(() => {})
{
const ch = channels[1]
await measureSwitch("channel", ch.id, "disk-warm", async () => {
- await page.getByTestId(`community-channel-row-${ch.id}`).click()
+ await page.getByTestId(tid.channelRow(ch.id)).click()
})
}
@@ -384,7 +450,7 @@ test("community switch perceived-latency capture", async ({ browser }) => {
for (const srv of otherServers) {
await clearIdb()
await measureSwitch("server", srv.id, "cold", async () => {
- await page.getByTestId(`community-server-icon-${srv.id}`).click()
+ await page.getByTestId(tid.serverIcon(srv.id)).click()
})
}
diff --git a/src/web/src/test/playwright-config.test.ts b/src/web/src/test/playwright-config.test.ts
index f27809c80..805265d16 100644
--- a/src/web/src/test/playwright-config.test.ts
+++ b/src/web/src/test/playwright-config.test.ts
@@ -14,6 +14,11 @@ async function loadReporter(ci: string | undefined) {
return config.reporter
}
+async function loadConfig() {
+ vi.resetModules()
+ return (await import("../../playwright.config")).default
+}
+
afterEach(() => {
if (originalCi === undefined) {
delete process.env.CI
@@ -35,4 +40,13 @@ describe("Playwright reporter configuration", () => {
it("uses the list reporter outside CI", async () => {
await expect(loadReporter(undefined)).resolves.toBe("list")
})
+
+ it("keeps the single-worker zero-retry timing contract without a global failure cap", async () => {
+ const config = await loadConfig()
+ expect(config.retries).toBe(0)
+ expect(config.workers).toBe(1)
+ expect(config.timeout).toBe(60_000)
+ expect(config.expect?.timeout).toBe(10_000)
+ expect(config.maxFailures).toBeUndefined()
+ })
})