Skip to content

Commit aaef1f2

Browse files
committed
fix(ios): runner demand and managed admission for the runner clipboard write
writeClipboard now needs the runner, so the simulator runner-demand table marks it runner and the managed owner withholds it on iOS, where no managed path starts a runner. A tvOS simulator has no UIPasteboard for its runner, so it keeps simctl pbcopy. Runner tests pin the command's missing-text refusal and that the general pasteboard holds the text.
1 parent a0af80e commit aaef1f2

10 files changed

Lines changed: 121 additions & 18 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import XCTest
2+
3+
#if AGENT_DEVICE_RUNNER_UNIT_TESTS
4+
extension RunnerTests {
5+
@MainActor
6+
func testPasteboardWriteRefusesARequestWithoutText() throws {
7+
let response = executePasteboardWrite(command: try runnerCommandFixture(#"{"command":"pasteboardWrite"}"#))
8+
XCTAssertFalse(response.ok)
9+
XCTAssertEqual(response.error?.code, "INVALID_ARGS")
10+
}
11+
12+
#if os(iOS) || os(visionOS)
13+
@MainActor
14+
func testPasteboardWriteLeavesTheTextOnTheGeneralPasteboard() throws {
15+
let text = "one-time code \(UUID().uuidString)"
16+
let request = #"{"command":"pasteboardWrite","text":"\#(text)"}"#
17+
let response = executePasteboardWrite(command: try runnerCommandFixture(request))
18+
XCTAssertTrue(response.ok)
19+
XCTAssertEqual(UIPasteboard.general.string, text)
20+
}
21+
#else
22+
@MainActor
23+
func testPasteboardWriteIsUnsupportedWithoutAUIKitPasteboard() throws {
24+
let request = #"{"command":"pasteboardWrite","text":"code"}"#
25+
let response = executePasteboardWrite(command: try runnerCommandFixture(request))
26+
XCTAssertFalse(response.ok)
27+
XCTAssertEqual(response.error?.code, "UNSUPPORTED_OPERATION")
28+
}
29+
#endif
30+
}
31+
#endif
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import assert from 'node:assert/strict';
2+
import { test, vi } from 'vitest';
3+
import { withAppleRunnerProvider } from '../../runner/index.ts';
4+
import { IOS_SIMULATOR, TVOS_SIMULATOR } from '../../runner/__tests__/device-fixtures.ts';
5+
import {
6+
recordingRunnerProvider,
7+
type RecordedRunnerCall,
8+
} from '../../__tests__/recording-runner-provider.ts';
9+
import { createLocalAppleToolProvider, withAppleToolProvider } from '../tool-provider.ts';
10+
import { writeIosClipboardText } from '../app-device-io.ts';
11+
12+
vi.mock('../simulator.ts', async (importOriginal) => ({
13+
...(await importOriginal<typeof import('../simulator.ts')>()),
14+
ensureBootedSimulator: async () => {},
15+
}));
16+
17+
async function writeThroughBothRoutes(device: typeof IOS_SIMULATOR, text: string) {
18+
const runnerCalls: RecordedRunnerCall[] = [];
19+
const toolCalls: Array<{ args: string[]; stdin: unknown }> = [];
20+
const tools = createLocalAppleToolProvider({
21+
runCommand: async (_cmd, args, options) => {
22+
toolCalls.push({ args, stdin: options?.stdin });
23+
return { exitCode: 0, stdout: '', stderr: '' };
24+
},
25+
});
26+
await withAppleToolProvider(tools, () =>
27+
withAppleRunnerProvider(recordingRunnerProvider(runnerCalls), { deviceId: device.id }, () =>
28+
writeIosClipboardText(device, text),
29+
),
30+
);
31+
return { runnerCommands: runnerCalls.map(({ command }) => command), toolCalls };
32+
}
33+
34+
test('an iOS simulator clipboard write is the runner setting the pasteboard, not simctl', async () => {
35+
const { runnerCommands, toolCalls } = await writeThroughBothRoutes(IOS_SIMULATOR, 'code 246810');
36+
assert.equal(runnerCommands.length, 1);
37+
assert.equal(runnerCommands[0]?.command, 'pasteboardWrite');
38+
assert.equal(runnerCommands[0]?.text, 'code 246810');
39+
assert.deepEqual(toolCalls, []);
40+
});
41+
42+
test('a tvOS simulator, whose runner has no pasteboard, writes through simctl pbcopy', async () => {
43+
const { runnerCommands, toolCalls } = await writeThroughBothRoutes(TVOS_SIMULATOR, 'code 246810');
44+
assert.deepEqual(runnerCommands, []);
45+
assert.equal(toolCalls.length, 1);
46+
assert.deepEqual(toolCalls[0]?.args.slice(-2), ['pbcopy', TVOS_SIMULATOR.id]);
47+
assert.equal(toolCalls[0]?.stdin, 'code 246810');
48+
});

‎packages/platform-apple/src/core/app-device-io.ts‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isMacOs, type DeviceInfo } from '@agent-device/kernel/device';
1+
import { isMacOs, isTvOsDevice, type DeviceInfo } from '@agent-device/kernel/device';
22
import { requireExecSuccess } from '@agent-device/host-kit/command';
33
import { ensureBootedSimulator, requireSimulatorDevice } from './simulator.ts';
44
import { readMacOsClipboardText, writeMacOsClipboardText } from '../os/macos/apps.ts';
@@ -20,9 +20,10 @@ export async function readIosClipboardText(device: DeviceInfo): Promise<string>
2020
}
2121

2222
/**
23-
* Writes the device's pasteboard: the macOS host's directly, a simulator's from the runner's own
24-
* process. `simctl pbcopy` hands the simulator only a promise of the data, owned by the `simctl`
25-
* process that exits before anything on the device reads it, so the pasteboard ends up empty.
23+
* Writes the device's pasteboard: the macOS host's directly, and a simulator's from the runner's
24+
* own process. `simctl pbcopy` hands the simulator only a promise of the data, owned by the `simctl`
25+
* process that exits before anything on the device reads it, so the pasteboard ends up empty. tvOS
26+
* has no `UIPasteboard` for its runner to write, so a tvOS simulator keeps `simctl pbcopy`.
2627
*/
2728
export async function writeIosClipboardText(
2829
device: DeviceInfo,
@@ -34,5 +35,13 @@ export async function writeIosClipboardText(
3435
return;
3536
}
3637
requireSimulatorDevice(device, 'clipboard');
38+
if (isTvOsDevice(device)) {
39+
await ensureBootedSimulator(device);
40+
requireExecSuccess(
41+
await runSimctlForDevice(device, ['pbcopy', device.id], { allowFailure: true, stdin: text }),
42+
'Failed to write tvOS simulator clipboard',
43+
);
44+
return;
45+
}
3746
await runAppleRunnerCommand(device, { command: 'pasteboardWrite', text }, runnerOptions);
3847
}

‎packages/platform-apple/src/runner-demand.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ test.each([
2424
['custom actions', 'captureSnapshotWithCustomActions'],
2525
['an alert', 'readAlert'],
2626
['runner preparation', 'prepareAppleRunner'],
27+
['a clipboard write', 'writeClipboard'],
2728
] as const)('a plan containing %s requires the runner', (_name, operation) => {
2829
expect(resolveAppleSimulatorRunnerDemand(['captureSnapshot', operation])).toBe('required');
2930
});

‎packages/platform-apple/src/runner-demand.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ const APPLE_SIMULATOR_OPERATION_HOSTS: Readonly<
4747
audioProbeReattach: 'simulator',
4848
audioProbeCleanup: 'simulator',
4949
readClipboard: 'simulator',
50-
writeClipboard: 'simulator',
50+
// The runner writes the pasteboard: `simctl pbcopy` leaves it empty under Xcode 27.
51+
writeClipboard: 'runner',
5152
setSetting: 'simulator',
5253
readSetting: 'simulator',
5354
setFoldPose: 'simulator',

‎packages/platform-apple/src/system/runtime.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ const settingsReadHostUnavailable = Object.freeze({
4646
} as const);
4747
/**
4848
* Parity with the retired `supportsHostOrSimulatorSurface` closure: the Apple pasteboard is
49-
* reachable on any simulator (read through `simctl pbpaste`, written by the runner), and directly on
50-
* the macOS host;
51-
* a physical iOS/iPadOS/tvOS/visionOS device has neither route.
49+
* reachable on any simulator (read through `simctl pbpaste`, written by the runner or, on tvOS,
50+
* `simctl pbcopy`) and directly on the macOS host; a physical iOS/iPadOS/tvOS/visionOS device has
51+
* neither route.
5252
*/
5353
const clipboardLeafUnavailable = Object.freeze({
5454
available: false,
@@ -85,9 +85,9 @@ function appleHostOrSimulatorFact(
8585
}
8686

8787
/**
88-
* Read and write share one cell: both routes (the simulator's `simctl pbpaste` and runner write, and the macOS host
89-
* pasteboard) expose the pair or neither, so splitting them here would invent a cell no Apple
90-
* owner can actually be in.
88+
* Read and write share one cell: both routes (the simulator's `simctl pbpaste` read and runner
89+
* write, and the macOS host pasteboard) expose the pair or neither, so splitting them here would
90+
* invent a cell no Apple owner can actually be in.
9191
*/
9292
function appleClipboardFact(device: DeviceInfo): RuntimeOperationFact {
9393
return appleHostOrSimulatorFact(device, clipboardKindUnavailable, clipboardLeafUnavailable);

‎src/platform-runtime-managed-owner.test.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ describe('managed local runtime owner', () => {
131131
'setSetting',
132132
'readSetting',
133133
'readClipboard',
134-
'writeClipboard',
135134
];
136135
expect(Object.keys(binding.operations).sort()).toEqual(enabled.sort());
137136
for (const key of Object.keys(binding.facts.operations) as Array<

‎src/platform-runtime-managed-owner.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ const REVIEWED_MANAGED_OPERATIONS: Partial<Record<ManagedOperationKey, 'both' |
3838
// local-tool path as the write (`simctl ui ... content_size`, `settings get system font_scale`).
3939
readSetting: 'both',
4040
readClipboard: 'both',
41-
writeClipboard: 'both',
41+
// An iOS simulator's pasteboard is written by the XCTest runner, which no managed-device
42+
// automation path has been reviewed to start.
43+
writeClipboard: 'android',
4244
captureScreenshot: 'android',
4345
};
4446

‎test/integration/provider-scenarios/managed-runtime-automation.test.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ test('managed iOS delegates deployment and system operations without local readi
7676
}
7777
await ops.sendPushNotification!({ appId: AUTOMATION_APP_ID, payload: {} });
7878
expect(await ops.readClipboard!({})).toBe('managed clipboard');
79+
// The runner writes an iOS simulator's pasteboard, so managed iOS withholds the write.
80+
expect(binding.facts.operations.writeClipboard).toMatchObject({
81+
available: false,
82+
reason: 'owner-capability-missing',
83+
});
84+
expect(ops.writeClipboard).toBeUndefined();
7985
await ops.setSetting!({ setting: 'appearance', state: 'dark' });
8086
await ops.setSetting!({ setting: 'faceid', state: 'match' });
8187
expect(biometricPosts).toBe(1);

‎website/docs/docs/known-limitations.md‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,21 @@ This is an Apple platform constraint that affects all XCUITest-based automation
1414

1515
**Workarounds:**
1616

17-
- **Pre-fill the pasteboard** — set clipboard content without triggering the dialog:
17+
- **Pre-fill the pasteboard** — put the text the app will read there before it reads:
1818
```bash
19-
agent-device clipboard write "some text"
19+
agent-device clipboard write "some text" --platform ios --udid <simulator-udid>
2020
```
21-
On a simulator the runner writes the pasteboard from inside the device. Under Xcode 27,
22-
`xcrun simctl pbcopy` exits 0 but leaves the pasteboard empty: it hands the simulator only a
23-
promise of the data, owned by a `simctl` process that has exited by the time anything reads it.
2421
- **Test the dialog manually** — the "Allow Paste" UX cannot be exercised through XCUITest-based automation.
2522

23+
## iOS simulator: `simctl pbcopy` writes nothing under Xcode 27
24+
25+
Under Xcode 27, `xcrun simctl pbcopy <udid>` exits 0 but leaves the simulator's pasteboard empty:
26+
it hands the simulator only a promise of the data, owned by the `simctl` process, which has exited
27+
by the time anything reads it. `simctl pbinfo` then lists a `Promised` item that reads back empty.
28+
`agent-device clipboard write` therefore writes an iOS simulator's pasteboard from the XCTest runner
29+
inside the device. A tvOS simulator has no pasteboard the runner can write, so it still goes
30+
through `simctl pbcopy`.
31+
2632
## Android: non-ASCII text on real devices without the test IME helper
2733

2834
`adb shell input text` (the local ASCII-only fallback) cannot inject non-ASCII text (for example Chinese characters or emoji) on any Android system image. `agent-device` ships its own headless test IME (`android-ime-helper`) that handles this natively — it also removes the visible system keyboard from snapshots entirely, which the manual-ADBKeyBoard workaround this section used to describe never did.

0 commit comments

Comments
 (0)