Skip to content

Commit 81049fc

Browse files
committed
test: preserve cropped screenshot destinations across failures
1 parent aa7549f commit 81049fc

2 files changed

Lines changed: 126 additions & 2 deletions

File tree

‎packages/daemon/src/execution/screenshot-runtime-execution.test.ts‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,59 @@ test('the crop runs against the platform screenshot dimensions', async () => {
454454
});
455455
});
456456

457+
for (const failure of ['sparse', 'canceled'] as const) {
458+
for (const destinationExists of [true, false]) {
459+
test(`${failure} crop preserves ${destinationExists ? 'existing' : 'absent'} destination and removes staging`, async () => {
460+
const controller = new AbortController();
461+
const fixture = screenshotRuntimeFixture({
462+
snapshotResult: () => {
463+
if (failure === 'canceled') controller.abort();
464+
return {
465+
nodes: CROP_TREE,
466+
backend: 'android',
467+
producer: 'android-uiautomator',
468+
...(failure === 'sparse'
469+
? { quality: { state: 'sparse' as const, backend: 'tree' } }
470+
: {}),
471+
};
472+
},
473+
});
474+
const session = makeSession('default', { device: ANDROID_EMULATOR });
475+
const outPath = path.join(mkdtempForTestSync('crop-publication'), 'capture.png');
476+
const sentinel = Buffer.from('existing destination');
477+
if (destinationExists) fs.writeFileSync(outPath, sentinel);
478+
const req = screenshotRequest({
479+
positionals: [outPath],
480+
flags: { screenshotCropOn: 'label="Save"' },
481+
});
482+
const resolved = await resolveScreenshotGenericExecution({
483+
hostCwd: process.cwd(),
484+
createCommandSurface: testCreateCommandSurface,
485+
platformServices: daemonPlatformServicesFixture(),
486+
req,
487+
session,
488+
inspectFacts: fixture.inspectFacts,
489+
bindDevice: fixture.bindDevice,
490+
});
491+
await expect(
492+
executeResult(resolved, session, req, { signal: controller.signal }),
493+
).rejects.toMatchObject(
494+
failure === 'sparse'
495+
? { details: { reason: SCREENSHOT_CROP_REASONS.captureUnreadable } }
496+
: { name: 'AbortError' },
497+
);
498+
expect(fixture.captureScreenshot).toHaveBeenCalledTimes(1);
499+
expect(fixture.captureSnapshot).toHaveBeenCalledTimes(1);
500+
const stagedPath = fixture.captureScreenshot.mock.calls[0]?.[0].outPath;
501+
expect(stagedPath).toBeDefined();
502+
expect(stagedPath).not.toBe(outPath);
503+
if (stagedPath) expect(fs.existsSync(path.dirname(stagedPath))).toBe(false);
504+
if (destinationExists) expect(fs.readFileSync(outPath)).toEqual(sentinel);
505+
else expect(fs.existsSync(outPath)).toBe(false);
506+
});
507+
}
508+
}
509+
457510
test('a partial crop surfaces its warning once in the result record and annotates nothing', async () => {
458511
const fixture = screenshotRuntimeFixture({
459512
snapshotResult: () => ({

‎test/integration/provider-scenarios/android-screenshot-crop.test.ts‎

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,33 @@
11
import assert from 'node:assert/strict';
22
import fs from 'node:fs';
33
import path from 'node:path';
4-
import { test } from 'vitest';
4+
import { afterEach, beforeEach, test, vi } from 'vitest';
55
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';
78
import { withProviderScenarioResource } from './harness.ts';
89

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+
931
for (const destinationExists of [true, false]) {
1032
test(`crop failure preserves ${destinationExists ? 'existing' : 'absent'} screenshot destination`, async () => {
1133
await withProviderScenarioResource(createAndroidSettingsWorld, async (world) => {
@@ -35,6 +57,55 @@ for (const destinationExists of [true, false]) {
3557
});
3658
}
3759

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+
38109
for (const destinationExists of [true, false]) {
39110
test(`capture failure preserves ${destinationExists ? 'existing' : 'absent'} crop destination`, async () => {
40111
await withProviderScenarioResource(

0 commit comments

Comments
 (0)