|
| 1 | +/** |
| 2 | + * #2246: once a remote daemon writes suite artifacts under a temp directory it owns (the client |
| 3 | + * redirects `--artifacts-dir` there — see `daemon-artifacts-test-command.test.ts`), the response |
| 4 | + * this handler builds must point the caller back at the REAL local root the client will download |
| 5 | + * that directory into (`req.meta.clientArtifactPaths.artifactsDir`), and must register the |
| 6 | + * directory as one downloadable artifact so the client's existing artifact transport can pull it. |
| 7 | + * A local daemon never sets that hint, so the response must come back byte-for-byte unchanged. |
| 8 | + * |
| 9 | + * `daemonRoot` here is deliberately two levels (`<tempRoot>/<suiteInvocationId>`), matching what |
| 10 | + * `resolveReplayTestArtifactsDir` actually produces: the redirected temp ROOT the client sent |
| 11 | + * joined with the daemon-generated invocation id. The caller-local mirror of that is |
| 12 | + * `clientRoot/<suiteInvocationId>` — NOT bare `clientRoot`, which is only the root the client |
| 13 | + * resolved from `--artifacts-dir` *before* the suite ran and could not yet know the invocation id. |
| 14 | + */ |
| 15 | +import assert from 'node:assert/strict'; |
| 16 | +import fs from 'node:fs'; |
| 17 | +import path from 'node:path'; |
| 18 | +import { test } from 'vitest'; |
| 19 | +import type { ReplaySuiteResult } from '@agent-device/contracts/replay'; |
| 20 | +import type { DaemonRequest } from '../../../types.ts'; |
| 21 | +import { attachRemoteReplayTestArtifacts } from '../test-command.ts'; |
| 22 | +import { mkdtempForTestSync } from '../../../../__tests__/test-utils/tmp-dir.ts'; |
| 23 | + |
| 24 | +const SUITE_INVOCATION_ID = 'cd5f9c01feec8d70'; |
| 25 | + |
| 26 | +function suiteResult(daemonRoot: string): ReplaySuiteResult { |
| 27 | + return { |
| 28 | + total: 2, |
| 29 | + executed: 2, |
| 30 | + passed: 1, |
| 31 | + failed: 1, |
| 32 | + skipped: 1, |
| 33 | + notRun: 0, |
| 34 | + durationMs: 10, |
| 35 | + tests: [ |
| 36 | + { |
| 37 | + file: path.join(daemonRoot, '..', '01-open.ad'), |
| 38 | + session: 'default:test:1', |
| 39 | + status: 'passed', |
| 40 | + durationMs: 5, |
| 41 | + attempts: 1, |
| 42 | + artifactsDir: path.join(daemonRoot, 'qa-flows__open.ad', 'attempt-1'), |
| 43 | + replayed: 1, |
| 44 | + healed: 0, |
| 45 | + }, |
| 46 | + { |
| 47 | + file: path.join(daemonRoot, '..', '02-checkout.ad'), |
| 48 | + session: 'default:test:2', |
| 49 | + status: 'failed', |
| 50 | + durationMs: 8, |
| 51 | + attempts: 1, |
| 52 | + artifactsDir: path.join(daemonRoot, 'qa-flows__checkout.ad', 'attempt-1'), |
| 53 | + error: { code: 'COMMAND_FAILED', message: 'boom' }, |
| 54 | + }, |
| 55 | + { |
| 56 | + file: 'skipped.ad', |
| 57 | + status: 'skipped', |
| 58 | + durationMs: 0, |
| 59 | + reason: 'skipped-by-filter', |
| 60 | + message: 'platform mismatch', |
| 61 | + }, |
| 62 | + ], |
| 63 | + // The failed test above is intentionally the SAME object reference here, mirroring |
| 64 | + // `summarizeReplayTestResults` (`failures: results.filter(...)`, `tests: results`) — a fix |
| 65 | + // that only rewrites `tests` and forgets `failures` would leave this one unrewritten. |
| 66 | + get failures() { |
| 67 | + return this.tests.filter( |
| 68 | + (t): t is Extract<ReplaySuiteResult['tests'][number], { status: 'failed' }> => |
| 69 | + t.status === 'failed', |
| 70 | + ); |
| 71 | + }, |
| 72 | + artifactsDir: daemonRoot, |
| 73 | + }; |
| 74 | +} |
| 75 | + |
| 76 | +function req(clientArtifactsRoot: string | undefined): DaemonRequest { |
| 77 | + return { |
| 78 | + token: 't', |
| 79 | + session: 'default', |
| 80 | + command: 'test', |
| 81 | + positionals: [], |
| 82 | + meta: clientArtifactsRoot ? { clientArtifactPaths: { artifactsDir: clientArtifactsRoot } } : {}, |
| 83 | + }; |
| 84 | +} |
| 85 | + |
| 86 | +test('rewrites the suite, every test, and every failure to the caller-local root, and registers the directory for download', () => { |
| 87 | + const tempRoot = mkdtempForTestSync('agent-device-remote-test-artifacts-daemon-'); |
| 88 | + const daemonRoot = path.join(tempRoot, SUITE_INVOCATION_ID); |
| 89 | + fs.mkdirSync(path.join(daemonRoot, 'qa-flows__open.ad', 'attempt-1'), { recursive: true }); |
| 90 | + const clientRoot = '/Users/ci/work/installer-app/remote-device-artifacts/ad-test'; |
| 91 | + const clientSuiteRoot = path.join(clientRoot, SUITE_INVOCATION_ID); |
| 92 | + |
| 93 | + const data = attachRemoteReplayTestArtifacts(suiteResult(daemonRoot), req(clientRoot)); |
| 94 | + |
| 95 | + assert.equal(data.artifactsDir, clientSuiteRoot); |
| 96 | + const tests = data.tests as ReplaySuiteResult['tests']; |
| 97 | + assert.equal( |
| 98 | + (tests[0] as { artifactsDir?: string }).artifactsDir, |
| 99 | + path.join(clientSuiteRoot, 'qa-flows__open.ad', 'attempt-1'), |
| 100 | + ); |
| 101 | + assert.equal( |
| 102 | + (tests[1] as { artifactsDir?: string }).artifactsDir, |
| 103 | + path.join(clientSuiteRoot, 'qa-flows__checkout.ad', 'attempt-1'), |
| 104 | + ); |
| 105 | + // A skipped test never had an artifactsDir; it must not gain one. |
| 106 | + const skippedTest = tests[2]; |
| 107 | + assert.ok(skippedTest); |
| 108 | + assert.equal('artifactsDir' in skippedTest, false); |
| 109 | + |
| 110 | + const failures = data.failures as ReplaySuiteResult['failures']; |
| 111 | + assert.equal(failures.length, 1); |
| 112 | + assert.equal( |
| 113 | + (failures[0] as { artifactsDir?: string }).artifactsDir, |
| 114 | + path.join(clientSuiteRoot, 'qa-flows__checkout.ad', 'attempt-1'), |
| 115 | + ); |
| 116 | + |
| 117 | + assert.deepEqual(data.artifacts, [ |
| 118 | + { |
| 119 | + field: 'artifactsDir', |
| 120 | + artifactType: 'test-artifacts', |
| 121 | + path: daemonRoot, |
| 122 | + // The download destination is the ROOT the client redirected to, not `clientSuiteRoot`: |
| 123 | + // extracting the archive reproduces the invocation-id segment on its own. |
| 124 | + localPath: clientRoot, |
| 125 | + fileName: SUITE_INVOCATION_ID, |
| 126 | + }, |
| 127 | + ]); |
| 128 | +}); |
| 129 | + |
| 130 | +test('a local daemon (no clientArtifactPaths hint) returns the suite result unchanged', () => { |
| 131 | + const tempRoot = mkdtempForTestSync('agent-device-remote-test-artifacts-local-'); |
| 132 | + const daemonRoot = path.join(tempRoot, SUITE_INVOCATION_ID); |
| 133 | + fs.mkdirSync(path.join(daemonRoot, 'qa-flows__open.ad', 'attempt-1'), { recursive: true }); |
| 134 | + const result = suiteResult(daemonRoot); |
| 135 | + |
| 136 | + const data = attachRemoteReplayTestArtifacts(result, req(undefined)); |
| 137 | + |
| 138 | + assert.equal(data, result); |
| 139 | +}); |
| 140 | + |
| 141 | +test('a suite directory that was never created (all sources skipped) still reports the caller-local root, but registers no download', () => { |
| 142 | + const tempRoot = mkdtempForTestSync('agent-device-remote-test-artifacts-empty-'); |
| 143 | + const daemonRoot = path.join(tempRoot, SUITE_INVOCATION_ID); |
| 144 | + const clientRoot = '/Users/ci/work/installer-app/remote-device-artifacts/ad-test'; |
| 145 | + const result: ReplaySuiteResult = { |
| 146 | + total: 1, |
| 147 | + executed: 0, |
| 148 | + passed: 0, |
| 149 | + failed: 0, |
| 150 | + skipped: 1, |
| 151 | + notRun: 0, |
| 152 | + durationMs: 1, |
| 153 | + failures: [], |
| 154 | + artifactsDir: daemonRoot, |
| 155 | + tests: [ |
| 156 | + { |
| 157 | + file: 'skipped.ad', |
| 158 | + status: 'skipped', |
| 159 | + durationMs: 0, |
| 160 | + reason: 'skipped-by-filter', |
| 161 | + message: 'x', |
| 162 | + }, |
| 163 | + ], |
| 164 | + }; |
| 165 | + |
| 166 | + const data = attachRemoteReplayTestArtifacts(result, req(clientRoot)); |
| 167 | + |
| 168 | + // Nothing to download, but the reported root is still the caller-local one the suite WOULD |
| 169 | + // have used — not the unreachable daemon-local temp path. |
| 170 | + assert.equal(data.artifactsDir, path.join(clientRoot, SUITE_INVOCATION_ID)); |
| 171 | + assert.equal(data.artifacts, undefined); |
| 172 | +}); |
0 commit comments