diff --git a/packages/replay-port/src/daemon-port/__tests__/session-test-source-discovery-inspection.test.ts b/packages/replay-port/src/daemon-port/__tests__/session-test-source-discovery-inspection.test.ts index a0205f1dfe..68e0d540b3 100644 --- a/packages/replay-port/src/daemon-port/__tests__/session-test-source-discovery-inspection.test.ts +++ b/packages/replay-port/src/daemon-port/__tests__/session-test-source-discovery-inspection.test.ts @@ -68,6 +68,22 @@ test('replay-test source inspection reads declared platform, target and attempt assert.deepEqual(entry?.manifest.attemptDefaults, { timeoutMs: 4000, retries: 2 }); }); +test('replay-test source inspection returns shipped yaml when the entry path is not on disk', async () => { + const flowPath = '/client-only/missing/01-flow.yaml'; + const yaml = 'appId: demo\nname: Remote Flow\n---\n- launchApp\n'; + + const [entry] = ( + await buildReplayTestSourceDiscovery( + [{ entry: flowPath, files: { [flowPath]: yaml } }], + 'maestro', + ) + )(); + + assert.equal(entry?.path, flowPath); + assert.equal(entry?.sourceText, yaml); + assert.equal(entry?.manifest.title, 'Remote Flow'); +}); + test('replay-test source inspection keeps the order the caller discovered', async () => { const root = mkdtempForTestSync('agent-device-test-inspect-order-'); const second = path.join(root, '02-second.ad'); diff --git a/packages/replay-port/src/daemon-port/session-test-source-discovery.ts b/packages/replay-port/src/daemon-port/session-test-source-discovery.ts index 6ef93fe530..baf287fc93 100644 --- a/packages/replay-port/src/daemon-port/session-test-source-discovery.ts +++ b/packages/replay-port/src/daemon-port/session-test-source-discovery.ts @@ -79,5 +79,5 @@ function inspectReplayTestSource( } : {}), }; - return { path: filePath, manifest }; + return { path: filePath, manifest, sourceText: script }; } diff --git a/packages/replay-test/src/index.ts b/packages/replay-test/src/index.ts index c4f469f001..2fb7645fe8 100644 --- a/packages/replay-test/src/index.ts +++ b/packages/replay-test/src/index.ts @@ -6,9 +6,10 @@ * result aggregation — is private to `internal/`. * * The scheduler is format-agnostic: it imports neither engine, and a source reaches it only as - * a `ReplayTestManifest`. Host authority arrives as narrow capabilities on the runtime-dependency - * half of `runReplayTestSuite`'s parameter; none of them hands over a daemon request, a session - * store, mutable session state, or an engine. + * a `ReplayTestManifest` plus the opaque entry text the host already read. Host authority + * arrives as narrow capabilities on the runtime-dependency half of `runReplayTestSuite`'s + * parameter; none of them hands over a daemon request, a session store, mutable session state, + * or an engine. * * The parameter and result types themselves are not named here. The single caller builds the * bag inline and TypeScript infers both, so re-exporting the names would add surface no consumer diff --git a/packages/replay-test/src/internal/__tests__/session-test-artifacts.test.ts b/packages/replay-test/src/internal/__tests__/session-test-artifacts.test.ts index ef16fc0b92..3d599f1bf5 100644 --- a/packages/replay-test/src/internal/__tests__/session-test-artifacts.test.ts +++ b/packages/replay-test/src/internal/__tests__/session-test-artifacts.test.ts @@ -1,4 +1,4 @@ -import { test } from 'vitest'; +import { test, vi } from 'vitest'; import assert from 'node:assert/strict'; import fs from 'node:fs'; @@ -44,10 +44,9 @@ test('materializeReplayTestAttemptArtifacts writes replay and result manifests f const replayPath = path.join(root, 'flow.ad'); const screenshotPath = path.join(root, 'capture.png'); const attemptDir = path.join(root, 'attempt-1'); - fs.writeFileSync(replayPath, 'context platform=ios\nopen "Demo"\n'); fs.writeFileSync(screenshotPath, 'png'); - prepareReplayTestAttemptArtifacts(replayPath, attemptDir); + prepareReplayTestAttemptArtifacts(replayPath, attemptDir, 'context platform=ios\nopen "Demo"\n'); materializeReplayTestAttemptArtifacts({ outcome: passedOutcome({ replayed: 4, healed: 1, artifactPaths: [screenshotPath] }), filePath: replayPath, @@ -72,25 +71,70 @@ test('prepareReplayTestAttemptArtifacts preserves original Maestro flow filename const root = mkdtempForTestSync('agent-device-test-artifacts-maestro-'); const replayPath = path.join(root, 'auth-flow.yml'); const attemptDir = path.join(root, 'attempt-1'); - fs.writeFileSync(replayPath, 'appId: demo.app\n---\n- assertVisible: Welcome\n'); - prepareReplayTestAttemptArtifacts(replayPath, attemptDir); + prepareReplayTestAttemptArtifacts( + replayPath, + attemptDir, + 'appId: demo.app\n---\n- assertVisible: Welcome\n', + ); assert.equal(fs.existsSync(path.join(attemptDir, 'replay.ad')), true); assert.equal(fs.existsSync(path.join(attemptDir, 'auth-flow.yml')), true); }); +test('prepareReplayTestAttemptArtifacts writes shipped flow text without opening a missing path', () => { + const root = mkdtempForTestSync('agent-device-test-artifacts-remote-source-'); + const replayPath = path.join(root, 'missing-client', 'auth-flow.yaml'); + const attemptDir = path.join(root, 'slug', 'attempt-1'); + const source = 'appId: demo.app\n---\n- launchApp\n'; + const copyFileSync = vi.spyOn(fs, 'copyFileSync'); + const statSync = vi.spyOn(fs, 'statSync'); + + try { + prepareReplayTestAttemptArtifacts(replayPath, attemptDir, source); + assert.equal( + copyFileSync.mock.calls.some((call) => call[0] === replayPath), + false, + ); + assert.equal( + statSync.mock.calls.some((call) => call[0] === replayPath), + false, + ); + } finally { + copyFileSync.mockRestore(); + statSync.mockRestore(); + } + + assert.equal(fs.readFileSync(path.join(attemptDir, 'replay.ad'), 'utf8'), source); + assert.equal(fs.readFileSync(path.join(attemptDir, 'auth-flow.yaml'), 'utf8'), source); +}); + +test('prepareReplayTestAttemptArtifacts writes only replay.ad when shipped text is a .ad entry', () => { + const root = mkdtempForTestSync('agent-device-test-artifacts-shipped-ad-'); + const replayPath = path.join(root, 'missing-client', 'flow.ad'); + const attemptDir = path.join(root, 'attempt-1'); + const source = 'context platform=ios\nopen "Demo"\n'; + + prepareReplayTestAttemptArtifacts(replayPath, attemptDir, source); + + assert.equal(fs.readFileSync(path.join(attemptDir, 'replay.ad'), 'utf8'), source); + assert.deepEqual(fs.readdirSync(attemptDir), ['replay.ad']); +}); + test('materializeReplayTestAttemptArtifacts writes failure manifest and copies log artifacts', () => { const root = mkdtempForTestSync('agent-device-test-artifacts-fail-'); const replayPath = path.join(root, 'flow.ad'); const screenshotPath = path.join(root, 'capture.png'); const logPath = path.join(root, 'daemon.log'); const attemptDir = path.join(root, 'attempt-2'); - fs.writeFileSync(replayPath, 'context platform=android\nopen "Demo"\n'); fs.writeFileSync(screenshotPath, 'png'); fs.writeFileSync(logPath, 'log'); - prepareReplayTestAttemptArtifacts(replayPath, attemptDir); + prepareReplayTestAttemptArtifacts( + replayPath, + attemptDir, + 'context platform=android\nopen "Demo"\n', + ); materializeReplayTestAttemptArtifacts({ outcome: { status: 'failed', @@ -139,7 +183,6 @@ test.each([ const root = mkdtempForTestSync('agent-device-artifact-collisions-'); const replayPath = path.join(root, 'flow.ad'); const attemptDir = path.join(root, 'attempt-1'); - fs.writeFileSync(replayPath, 'context platform=android\nopen demo.app\n'); const artifactPaths = names.map((name, index) => { const dir = path.join(root, String(index)); fs.mkdirSync(dir); @@ -148,7 +191,11 @@ test.each([ return artifactPath; }); - prepareReplayTestAttemptArtifacts(replayPath, attemptDir); + prepareReplayTestAttemptArtifacts( + replayPath, + attemptDir, + 'context platform=android\nopen demo.app\n', + ); materializeReplayTestAttemptArtifacts({ outcome: passedOutcome({ artifactPaths }), filePath: replayPath, @@ -170,7 +217,6 @@ test('materialization preserves replay sources and diagnostics named after attem const replayPath = path.join(root, 'flow.yml'); const attemptDir = path.join(root, 'attempt-1'); const source = 'appId: demo.app\n---\n- assertVisible: Welcome\n'; - fs.writeFileSync(replayPath, source); const names = ['replay.ad', 'flow.yml', 'result.txt', 'failure.txt']; const artifactPaths = names.map((name) => { const dir = path.join(root, 'diagnostics', name); @@ -180,7 +226,7 @@ test('materialization preserves replay sources and diagnostics named after attem return artifactPath; }); - prepareReplayTestAttemptArtifacts(replayPath, attemptDir); + prepareReplayTestAttemptArtifacts(replayPath, attemptDir, source); materializeReplayTestAttemptArtifacts({ outcome: { status: 'failed', @@ -220,9 +266,12 @@ test.each([false, true])( const attemptDir = path.join(root, 'attempt-1'); const externalTrace = path.join(root, 'replay-timing.ndjson'); const localTrace = path.join(attemptDir, 'replay-timing.ndjson'); - fs.writeFileSync(replayPath, 'context platform=android\nopen demo.app\n'); fs.writeFileSync(externalTrace, 'external trace'); - prepareReplayTestAttemptArtifacts(replayPath, attemptDir); + prepareReplayTestAttemptArtifacts( + replayPath, + attemptDir, + 'context platform=android\nopen demo.app\n', + ); fs.writeFileSync(localTrace, 'attempt trace'); const artifactPaths = localFirst ? [localTrace, externalTrace] : [externalTrace, localTrace]; @@ -256,9 +305,12 @@ test('materialization copies a log listed in both the outcome and error only onc const replayPath = path.join(root, 'flow.ad'); const logPath = path.join(root, 'daemon.log'); const attemptDir = path.join(root, 'attempt-1'); - fs.writeFileSync(replayPath, 'context platform=android\nopen demo.app\n'); fs.writeFileSync(logPath, 'diagnostic log'); - prepareReplayTestAttemptArtifacts(replayPath, attemptDir); + prepareReplayTestAttemptArtifacts( + replayPath, + attemptDir, + 'context platform=android\nopen demo.app\n', + ); materializeReplayTestAttemptArtifacts({ outcome: { @@ -289,8 +341,11 @@ test.each(['result.txt', 'failure.txt', 'RESULT.TXT'])( const root = mkdtempForTestSync('agent-device-artifact-in-place-manifest-'); const replayPath = path.join(root, 'flow.ad'); const attemptDir = path.join(root, 'attempt-1'); - fs.writeFileSync(replayPath, 'context platform=android\nopen demo.app\n'); - prepareReplayTestAttemptArtifacts(replayPath, attemptDir); + prepareReplayTestAttemptArtifacts( + replayPath, + attemptDir, + 'context platform=android\nopen demo.app\n', + ); const diagnosticPath = path.join(attemptDir, name); fs.writeFileSync(diagnosticPath, 'diagnostic contents'); diff --git a/packages/replay-test/src/internal/__tests__/session-test-attempt.test.ts b/packages/replay-test/src/internal/__tests__/session-test-attempt.test.ts index e42b3788a5..a239fbbe7e 100644 --- a/packages/replay-test/src/internal/__tests__/session-test-attempt.test.ts +++ b/packages/replay-test/src/internal/__tests__/session-test-attempt.test.ts @@ -29,12 +29,14 @@ const FAILED_WITHOUT_WARNINGS: ReplayTestAttemptOutcome = { function makeEntry(): ReplayTestRunEntry { const root = mkdtempForTestSync('agent-device-test-attempt-'); const filePath = path.join(root, '01-flow.ad'); - fs.writeFileSync(filePath, 'context platform=ios\nopen "Demo"\n'); + const sourceText = 'context platform=ios\nopen "Demo"\n'; + fs.writeFileSync(filePath, sourceText); return { kind: 'run', path: filePath, title: 'flow', manifest: { device: { platform: { kind: 'declared', value: 'ios' } } }, + sourceText, }; } @@ -70,3 +72,49 @@ test('a failed test result omits warnings when the attempt had none', async () = const result = await runFailedCase(FAILED_WITHOUT_WARNINGS); expect('warnings' in result).toBe(false); }); + +test('a missing entry path runs from shipped source text', async () => { + const suiteArtifactsDir = mkdtempForTestSync('agent-device-test-attempt-remote-'); + const filePath = path.join(suiteArtifactsDir, 'client-only', 'auth-flow.yaml'); + const source = 'appId: demo.app\n---\n- launchApp\n'; + let ran = false; + + await runReplayTestCase({ + entry: { + kind: 'run', + path: filePath, + manifest: { device: { platform: { kind: 'caller-bound' } } }, + sourceText: source, + }, + sessionName: 'default', + suiteInvocationId: 'suite-remote', + caseIndex: 0, + retries: 0, + suiteArtifactsDir, + suiteIndex: 1, + suiteTotal: 1, + runReplay: async () => { + ran = true; + return { + status: 'passed', + replayed: 1, + healed: 0, + warnings: [], + artifactPaths: [], + }; + }, + cleanupSession: async () => {}, + emitProgress: () => {}, + isCanceled: () => false, + emitDiagnostic: () => {}, + bindAttemptCancellation: () => ({ cancel: () => {}, release: () => {} }), + }); + + expect(ran).toBe(true); + expect( + fs.readFileSync( + path.join(suiteArtifactsDir, 'auth-flow.yaml', 'attempt-1', 'replay.ad'), + 'utf8', + ), + ).toBe(source); +}); diff --git a/packages/replay-test/src/internal/__tests__/session-test-discovery.test.ts b/packages/replay-test/src/internal/__tests__/session-test-discovery.test.ts index 2f620df7d5..9b3ad1a96e 100644 --- a/packages/replay-test/src/internal/__tests__/session-test-discovery.test.ts +++ b/packages/replay-test/src/internal/__tests__/session-test-discovery.test.ts @@ -29,8 +29,12 @@ test('platform filter skips sources that declared no platform', () => { const entries = discoverReplayTestEntries({ platformFilter: 'android', discoverSources: sourcesOf( - { path: '01-untyped.ad', manifest: unspecified }, - { path: '02-android.ad', manifest: declared('android') }, + { path: '01-untyped.ad', manifest: unspecified, sourceText: 'open "Demo"\n' }, + { + path: '02-android.ad', + manifest: declared('android'), + sourceText: 'context platform=android\n', + }, ), }); @@ -45,19 +49,50 @@ test('platform filter skips sources that declared no platform', () => { test('platform filter runs caller-bound sources, which take their platform from the invocation', () => { const entries = discoverReplayTestEntries({ platformFilter: 'android', - discoverSources: sourcesOf({ path: '01-flow.yaml', manifest: callerBound }), + discoverSources: sourcesOf({ + path: '01-flow.yaml', + manifest: callerBound, + sourceText: 'appId: demo\n---\n- launchApp\n', + }), }); assert.equal(entries.length, 1); assert.equal(entries[0]?.kind, 'run'); }); +test('discovery copies shipped source text onto filtered run entries', () => { + const flowText = 'appId: demo\n---\n- launchApp\n'; + const nativeText = 'context platform=ios\nopen "Demo"\n'; + const entries = discoverReplayTestEntries({ + platformFilter: 'ios', + discoverSources: sourcesOf( + { path: '01-flow.yaml', manifest: callerBound, sourceText: flowText }, + { path: '02-ios.ad', manifest: declared('ios'), sourceText: nativeText }, + ), + }); + + const flow = entries.find((entry) => entry.path === '01-flow.yaml'); + const native = entries.find((entry) => entry.path === '02-ios.ad'); + assert.equal(flow?.kind, 'run'); + if (flow?.kind === 'run') { + assert.equal(flow.sourceText, flowText); + } + assert.equal(native?.kind, 'run'); + if (native?.kind === 'run') { + assert.equal(native.sourceText, nativeText); + } +}); + test('platform filter drops declared sources that do not match, without a skip entry', () => { const entries = discoverReplayTestEntries({ platformFilter: 'android', discoverSources: sourcesOf( - { path: '01-ios.ad', manifest: declared('ios') }, - { path: '02-android.ad', manifest: declared('android') }, + { path: '01-ios.ad', manifest: declared('ios'), sourceText: 'context platform=ios\n' }, + { + path: '02-android.ad', + manifest: declared('android'), + sourceText: 'context platform=android\n', + }, ), }); @@ -72,7 +107,11 @@ test('a suite that matched nothing after filtering is rejected', () => { () => discoverReplayTestEntries({ platformFilter: 'android', - discoverSources: sourcesOf({ path: '01-ios.ad', manifest: declared('ios') }), + discoverSources: sourcesOf({ + path: '01-ios.ad', + manifest: declared('ios'), + sourceText: 'context platform=ios\n', + }), }), (error: unknown) => error instanceof AppError && @@ -90,7 +129,11 @@ test('a suite whose only source declares no platform is told how to declare one' () => discoverReplayTestEntries({ platformFilter: 'android', - discoverSources: sourcesOf({ path: '01-untyped.ad', manifest: unspecified }), + discoverSources: sourcesOf({ + path: '01-untyped.ad', + manifest: unspecified, + sourceText: 'open "Demo"\n', + }), }), (error: unknown) => error instanceof AppError && @@ -108,7 +151,11 @@ test('a filter value no script can declare is not answered with declaration advi () => discoverReplayTestEntries({ platformFilter: 'web', - discoverSources: sourcesOf({ path: '01-untyped.ad', manifest: unspecified }), + discoverSources: sourcesOf({ + path: '01-untyped.ad', + manifest: unspecified, + sourceText: 'open "Demo"\n', + }), }), (error: unknown) => error instanceof AppError && diff --git a/packages/replay-test/src/internal/session-test-artifacts.ts b/packages/replay-test/src/internal/session-test-artifacts.ts index aa052def70..6df16f449c 100644 --- a/packages/replay-test/src/internal/session-test-artifacts.ts +++ b/packages/replay-test/src/internal/session-test-artifacts.ts @@ -44,9 +44,10 @@ export function buildReplayTestArtifactSlug(filePath: string, cwd?: string): str export function prepareReplayTestAttemptArtifacts( filePath: string, attemptArtifactsDir: string, + sourceText: string, ): void { fs.mkdirSync(attemptArtifactsDir, { recursive: true }); - copyReplaySourceFile(filePath, attemptArtifactsDir); + copyReplaySourceFile(filePath, attemptArtifactsDir, sourceText); } export function materializeReplayTestAttemptArtifacts(params: { @@ -127,9 +128,16 @@ function copyReplayTestArtifacts( return copiedPaths; } -function copyReplaySourceFile(filePath: string, attemptArtifactsDir: string): void { +function copyReplaySourceFile( + filePath: string, + attemptArtifactsDir: string, + sourceText: string, +): void { const genericReplayPath = path.join(attemptArtifactsDir, 'replay.ad'); - fs.copyFileSync(filePath, genericReplayPath); + // The daemon writes the shipped text and never reads the entry path, because a remote daemon + // does not share the caller's disk and a daemon-side read would copy a daemon-host file into + // downloadable artifacts. + fs.writeFileSync(genericReplayPath, sourceText); // Sources that are not native `.ad` keep their original filename alongside `replay.ad`, so // CI artifacts point back at the flow the author wrote. This is artifact naming, not format @@ -138,9 +146,8 @@ function copyReplaySourceFile(filePath: string, attemptArtifactsDir: string): vo // Keep replay.ad for existing artifact consumers, and preserve the original // Maestro filename so CI artifacts point back to the source flow. const originalReplayPath = path.join(attemptArtifactsDir, path.basename(filePath)); - if (path.resolve(originalReplayPath) !== path.resolve(genericReplayPath)) { - fs.copyFileSync(filePath, originalReplayPath); - } + if (path.resolve(originalReplayPath) === path.resolve(genericReplayPath)) return; + fs.writeFileSync(originalReplayPath, sourceText); } function buildUniqueArtifactFileName( diff --git a/packages/replay-test/src/internal/session-test-attempt.ts b/packages/replay-test/src/internal/session-test-attempt.ts index 11101da418..c318582e24 100644 --- a/packages/replay-test/src/internal/session-test-attempt.ts +++ b/packages/replay-test/src/internal/session-test-attempt.ts @@ -142,7 +142,7 @@ async function runSingleReplayTestAttempt( attemptIndex, ); const attemptArtifactsDir = path.join(context.testArtifactsDir, `attempt-${attempt}`); - prepareReplayTestAttemptArtifacts(entry.path, attemptArtifactsDir); + prepareReplayTestAttemptArtifacts(entry.path, attemptArtifactsDir, entry.sourceText); const attemptRequestId = buildReplayTestAttemptRequestId({ requestId, suiteInvocationId, diff --git a/packages/replay-test/src/internal/session-test-discovery.ts b/packages/replay-test/src/internal/session-test-discovery.ts index 57e77bd7b5..8a7ed531eb 100644 --- a/packages/replay-test/src/internal/session-test-discovery.ts +++ b/packages/replay-test/src/internal/session-test-discovery.ts @@ -16,6 +16,8 @@ export type ReplayTestDiscoveryEntry = path: string; title?: string; manifest: ReplayTestManifest; + /** Opaque entry text from the inspected source. */ + sourceText: string; } | { kind: 'skip'; @@ -45,8 +47,14 @@ export function discoverReplayTestEntries(params: { // no-match message never re-derives one from an entry shape it does not own. const filteredOut = { undeclared: 0, declaredOther: 0 }; for (const source of sources) { - const { path: filePath, manifest } = source; - const run = { kind: 'run', path: filePath, title: manifest.title, manifest } as const; + const { path: filePath, manifest, sourceText } = source; + const run = { + kind: 'run' as const, + path: filePath, + title: manifest.title, + manifest, + sourceText, + }; if (!platformFilter) { entries.push(run); continue; diff --git a/packages/replay-test/src/internal/session-test-types.ts b/packages/replay-test/src/internal/session-test-types.ts index a7dfe1ca44..ef133d6fe2 100644 --- a/packages/replay-test/src/internal/session-test-types.ts +++ b/packages/replay-test/src/internal/session-test-types.ts @@ -97,6 +97,11 @@ export type ReplayTestManifest = Readonly<{ export type ReplayTestSource = Readonly<{ path: string; manifest: ReplayTestManifest; + /** + * Opaque entry text the host already read, always written into the attempt directory; `path` is + * never opened. + */ + sourceText: string; }>; /**