|
1 | 1 | import assert from 'node:assert/strict'; |
2 | 2 | import fs from 'node:fs'; |
3 | 3 | import path from 'node:path'; |
4 | | -import { test } from 'vitest'; |
| 4 | +import { afterEach, beforeEach, test, vi } from 'vitest'; |
5 | 5 | import { SCREENSHOT_CROP_REASONS } from '@agent-device/contracts/capture'; |
6 | | -import { createAndroidSettingsWorld } from './android-world.ts'; |
| 6 | +import { androidSettingsXml, createAndroidSettingsWorld } from './android-world.ts'; |
| 7 | +import { assertPngDimensions, assertRpcOk } from './assertions.ts'; |
7 | 8 | import { withProviderScenarioResource } from './harness.ts'; |
8 | 9 |
|
| 10 | +function trackStaging() { |
| 11 | + return vi.spyOn(fs.promises, 'mkdtemp'); |
| 12 | +} |
| 13 | + |
| 14 | +let staging: ReturnType<typeof trackStaging>; |
| 15 | +beforeEach(() => { |
| 16 | + staging = trackStaging(); |
| 17 | +}); |
| 18 | +afterEach(async () => { |
| 19 | + const results = staging.mock.results.filter((_, index) => |
| 20 | + path.basename(String(staging.mock.calls[index]?.[0])).startsWith('agent-device-crop-'), |
| 21 | + ); |
| 22 | + staging.mockRestore(); |
| 23 | + assert.ok(results.length > 0, 'the request must acquire crop staging'); |
| 24 | + for (const result of results) { |
| 25 | + assert.equal(result.type, 'return'); |
| 26 | + const directory = await result.value; |
| 27 | + assert.equal(fs.existsSync(directory), false, `crop staging remains: ${directory}`); |
| 28 | + } |
| 29 | +}); |
| 30 | + |
9 | 31 | for (const destinationExists of [true, false]) { |
10 | 32 | test(`crop failure preserves ${destinationExists ? 'existing' : 'absent'} screenshot destination`, async () => { |
11 | 33 | await withProviderScenarioResource(createAndroidSettingsWorld, async (world) => { |
@@ -35,6 +57,55 @@ for (const destinationExists of [true, false]) { |
35 | 57 | }); |
36 | 58 | } |
37 | 59 |
|
| 60 | +for (const destinationExists of [true, false]) { |
| 61 | + test(`ambiguous crop preserves ${destinationExists ? 'existing' : 'absent'} destination`, async () => { |
| 62 | + await withProviderScenarioResource( |
| 63 | + () => |
| 64 | + createAndroidSettingsWorld({ |
| 65 | + snapshotXml: () => androidSettingsXml('', { duplicateAppsRow: true }), |
| 66 | + }), |
| 67 | + async (world) => { |
| 68 | + await world.daemon.client().apps.open({ app: 'settings', ...world.selection }); |
| 69 | + const destination = path.join(world.tempRoot, 'crop.png'); |
| 70 | + const sentinel = Buffer.from('existing destination'); |
| 71 | + if (destinationExists) fs.writeFileSync(destination, sentinel); |
| 72 | + const response = await world.daemon.callCommand('screenshot', [destination], { |
| 73 | + screenshotCropOn: 'label="Apps"', |
| 74 | + }); |
| 75 | + assert.equal( |
| 76 | + response.json?.error?.data?.details?.reason, |
| 77 | + SCREENSHOT_CROP_REASONS.targetAmbiguous, |
| 78 | + ); |
| 79 | + assert.equal(response.json?.error?.data?.details?.matches, 2); |
| 80 | + if (destinationExists) assert.deepEqual(fs.readFileSync(destination), sentinel); |
| 81 | + else assert.equal(fs.existsSync(destination), false); |
| 82 | + }, |
| 83 | + ); |
| 84 | + }); |
| 85 | + |
| 86 | + test(`successful crop publishes over ${destinationExists ? 'existing' : 'absent'} destination`, async () => { |
| 87 | + await withProviderScenarioResource( |
| 88 | + () => |
| 89 | + createAndroidSettingsWorld({ |
| 90 | + snapshotXml: () => androidSettingsXml('').replace('[24,124][152,178]', '[8,12][40,36]'), |
| 91 | + }), |
| 92 | + async (world) => { |
| 93 | + await world.daemon.client().apps.open({ app: 'settings', ...world.selection }); |
| 94 | + const destination = path.join(world.tempRoot, 'crop.png'); |
| 95 | + if (destinationExists) fs.writeFileSync(destination, 'existing destination'); |
| 96 | + const response = await world.daemon.callCommand('screenshot', [destination], { |
| 97 | + screenshotCropOn: 'label="Apps"', |
| 98 | + }); |
| 99 | + const result = assertRpcOk(response); |
| 100 | + assert.equal(result.path, destination); |
| 101 | + assert.equal(result.width, 32); |
| 102 | + assert.equal(result.height, 24); |
| 103 | + assertPngDimensions(destination, 32, 24); |
| 104 | + }, |
| 105 | + ); |
| 106 | + }); |
| 107 | +} |
| 108 | + |
38 | 109 | for (const destinationExists of [true, false]) { |
39 | 110 | test(`capture failure preserves ${destinationExists ? 'existing' : 'absent'} crop destination`, async () => { |
40 | 111 | await withProviderScenarioResource( |
|
0 commit comments