|
| 1 | +import fs from 'node:fs'; |
| 2 | +import os from 'node:os'; |
| 3 | +import path from 'node:path'; |
| 4 | +import { expect, test } from 'vitest'; |
| 5 | +import { runReplayTestCase } from '../session-test-attempt.ts'; |
| 6 | +import type { ReplayTestRunEntry } from '../session-test-discovery.ts'; |
| 7 | +import type { ReplayTestAttemptOutcome } from '../session-test-types.ts'; |
| 8 | +import type { ReplaySuiteTestFailed } from '@agent-device/contracts/replay'; |
| 9 | + |
| 10 | +const FAILED_WITH_WARNINGS: ReplayTestAttemptOutcome = { |
| 11 | + status: 'failed', |
| 12 | + error: { |
| 13 | + code: 'REPLAY_DIVERGENCE', |
| 14 | + message: 'Replay failed at step 2 (tapOn "Save"): target did not resolve', |
| 15 | + }, |
| 16 | + artifactPaths: [], |
| 17 | + warnings: ['Optional Maestro assertVisible skipped at line 3: sheet owns focus'], |
| 18 | + infrastructure: false, |
| 19 | +}; |
| 20 | + |
| 21 | +const FAILED_WITHOUT_WARNINGS: ReplayTestAttemptOutcome = { |
| 22 | + status: 'failed', |
| 23 | + error: { code: 'COMMAND_FAILED', message: 'tap failed' }, |
| 24 | + artifactPaths: [], |
| 25 | + infrastructure: false, |
| 26 | +}; |
| 27 | + |
| 28 | +function makeEntry(): ReplayTestRunEntry { |
| 29 | + const root = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-device-test-attempt-')); |
| 30 | + const filePath = path.join(root, '01-flow.ad'); |
| 31 | + fs.writeFileSync(filePath, 'context platform=ios\nopen "Demo"\n'); |
| 32 | + return { |
| 33 | + kind: 'run', |
| 34 | + path: filePath, |
| 35 | + title: 'flow', |
| 36 | + manifest: { device: { platform: { kind: 'declared', value: 'ios' } } }, |
| 37 | + }; |
| 38 | +} |
| 39 | + |
| 40 | +async function runFailedCase(outcome: ReplayTestAttemptOutcome): Promise<ReplaySuiteTestFailed> { |
| 41 | + const report = await runReplayTestCase({ |
| 42 | + entry: makeEntry(), |
| 43 | + sessionName: 'default', |
| 44 | + suiteInvocationId: 'suite-attempt', |
| 45 | + caseIndex: 0, |
| 46 | + retries: 0, |
| 47 | + suiteArtifactsDir: fs.mkdtempSync(path.join(os.tmpdir(), 'agent-device-test-attempt-suite-')), |
| 48 | + suiteIndex: 1, |
| 49 | + suiteTotal: 1, |
| 50 | + runReplay: async () => outcome, |
| 51 | + cleanupSession: async () => {}, |
| 52 | + emitProgress: () => {}, |
| 53 | + isCanceled: () => false, |
| 54 | + emitDiagnostic: () => {}, |
| 55 | + bindAttemptCancellation: () => ({ cancel: () => {}, release: () => {} }), |
| 56 | + }); |
| 57 | + if (report.result.status !== 'failed') throw new Error('expected a failed test result'); |
| 58 | + return report.result; |
| 59 | +} |
| 60 | + |
| 61 | +test('a failed test result carries the warnings accumulated before the failure (#2560)', async () => { |
| 62 | + const result = await runFailedCase(FAILED_WITH_WARNINGS); |
| 63 | + expect(result.warnings).toEqual([ |
| 64 | + 'Optional Maestro assertVisible skipped at line 3: sheet owns focus', |
| 65 | + ]); |
| 66 | +}); |
| 67 | + |
| 68 | +test('a failed test result omits warnings when the attempt had none', async () => { |
| 69 | + const result = await runFailedCase(FAILED_WITHOUT_WARNINGS); |
| 70 | + expect('warnings' in result).toBe(false); |
| 71 | +}); |
0 commit comments