Skip to content

Commit 7bc439a

Browse files
committed
fix(device-pin): preserve narrower platforms during resolution
1 parent 544bf2e commit 7bc439a

4 files changed

Lines changed: 110 additions & 16 deletions

File tree

‎packages/client/src/__tests__/index-device-pin.test.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,5 +202,10 @@ for (const [platform, identity] of [
202202
const { client, requests } = recordingClient({ ...PIN, selector: { platform } });
203203
await runCommand(client, 'snapshot', identity);
204204
assert.equal(requests.length, 1);
205+
if ('udid' in identity) {
206+
assert.equal(requests[0]?.flags?.udid, identity.udid);
207+
} else {
208+
assert.equal(requests[0]?.flags?.serial, identity.serial);
209+
}
205210
});
206211
}

‎packages/daemon/src/core/__tests__/request-device-resolution.test.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,36 @@ test('request lease metadata overrides stale selector flags and selects the allo
6969
expect(selection.source).toBe('provider');
7070
expect(local).not.toHaveBeenCalled();
7171
});
72+
73+
for (const resolve of [resolveRequestTargetDevice, resolveRequestTargetDeviceSelection]) {
74+
for (const platform of ['ios', 'macos'] as const) {
75+
test(`${resolve.name} preserves the ${platform} pin under an Apple-family request`, async () => {
76+
const mac: DeviceInfo = {
77+
...device,
78+
id: 'local-mac',
79+
name: 'Mac',
80+
appleOs: 'macos',
81+
kind: 'device',
82+
target: 'desktop',
83+
};
84+
const req: DaemonRequest = {
85+
token: 't',
86+
session: 'default',
87+
command: 'capabilities',
88+
positionals: [],
89+
flags: { platform: 'apple' },
90+
meta: {
91+
devicePin: {
92+
selector: { platform },
93+
source: { kind: 'project', path: '/project/agent-device.json' },
94+
},
95+
},
96+
};
97+
const result = await withTestDeviceInventory({ local: async () => [mac, device] }, async () =>
98+
resolve(req, { env: {} }),
99+
);
100+
const selected = 'device' in result ? result.device : result;
101+
expect(selected.id).toBe(platform === 'ios' ? device.id : mac.id);
102+
});
103+
}
104+
}

‎packages/daemon/src/core/request-device-resolution.ts‎

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,10 @@ export async function resolveRequestTargetDevice(
2525
options: RequestTargetDeviceOptions,
2626
flags: RequestTargetDeviceFlags = req.flags ?? {},
2727
): ReturnType<typeof resolveTargetDevice> {
28-
const device = await resolveTargetDevice(
29-
{ ...flags, ...resolveLeaseScope(req) },
30-
{
31-
...options,
32-
webSession: req.internal?.webSession,
33-
},
34-
);
28+
const device = await resolveTargetDevice(resolveRequestDeviceFlags(req, flags), {
29+
...options,
30+
webSession: req.internal?.webSession,
31+
});
3532
assertWebSelectionHasIdentity(req, device);
3633
if (req.meta?.devicePin) {
3734
const { assertDeviceHonorsPin } =
@@ -46,13 +43,10 @@ export async function resolveRequestTargetDeviceSelection(
4643
options: RequestTargetDeviceOptions,
4744
flags: RequestTargetDeviceFlags = req.flags ?? {},
4845
): ReturnType<typeof resolveTargetDeviceSelection> {
49-
const selection = await resolveTargetDeviceSelection(
50-
{ ...flags, ...resolveLeaseScope(req) },
51-
{
52-
...options,
53-
webSession: req.internal?.webSession,
54-
},
55-
);
46+
const selection = await resolveTargetDeviceSelection(resolveRequestDeviceFlags(req, flags), {
47+
...options,
48+
webSession: req.internal?.webSession,
49+
});
5650
assertWebSelectionHasIdentity(req, selection.device);
5751
if (req.meta?.devicePin) {
5852
const { assertDeviceHonorsPin } =
@@ -61,3 +55,16 @@ export async function resolveRequestTargetDeviceSelection(
6155
}
6256
return selection;
6357
}
58+
59+
function resolveRequestDeviceFlags(
60+
req: DaemonRequest,
61+
flags: RequestTargetDeviceFlags,
62+
): RequestTargetDeviceFlags {
63+
const pinnedPlatform = req.meta?.devicePin?.selector.platform;
64+
const platform = flags.platform;
65+
return {
66+
...flags,
67+
platform: platform && platform !== 'apple' ? platform : (pinnedPlatform ?? platform),
68+
...resolveLeaseScope(req),
69+
};
70+
}

‎test/integration/provider-scenarios/device-pin.test.ts‎

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@ import { expect, test } from 'vitest';
33
import { createAgentDeviceClient } from '@agent-device/client';
44
import { runCommand } from '@agent-device/command-surface';
55
import type { DevicePin } from '@agent-device/contracts/device';
6-
import { PROVIDER_SCENARIO_ANDROID, PROVIDER_SCENARIO_IOS_SIMULATOR } from './fixtures.ts';
7-
import { assertRpcError } from './assertions.ts';
6+
import {
7+
PROVIDER_SCENARIO_ANDROID,
8+
PROVIDER_SCENARIO_IOS_SIMULATOR,
9+
PROVIDER_SCENARIO_MACOS,
10+
} from './fixtures.ts';
11+
import { assertRpcError, assertRpcOk } from './assertions.ts';
812
import { createProviderScenarioHarness, withProviderScenarioResource } from './harness.ts';
913

1014
for (const platform of ['ios', 'android'] as const) {
@@ -90,3 +94,48 @@ for (const platform of ['ios', 'android'] as const) {
9094
);
9195
});
9296
}
97+
98+
for (const [pinnedPlatform, requestedPlatform, expectedId] of [
99+
['ios', 'apple', PROVIDER_SCENARIO_IOS_SIMULATOR.id],
100+
['macos', 'apple', PROVIDER_SCENARIO_MACOS.id],
101+
['apple', 'ios', PROVIDER_SCENARIO_IOS_SIMULATOR.id],
102+
['apple', 'macos', PROVIDER_SCENARIO_MACOS.id],
103+
] as const) {
104+
test(`Provider-backed integration ${pinnedPlatform} pin intersects ${requestedPlatform} before selection`, async () => {
105+
const pin: DevicePin = {
106+
selector: { platform: pinnedPlatform },
107+
source: { kind: 'project', path: '/project/agent-device.json' },
108+
};
109+
await withProviderScenarioResource(
110+
() =>
111+
createProviderScenarioHarness({
112+
deviceInventoryProvider: async () => [
113+
PROVIDER_SCENARIO_MACOS,
114+
PROVIDER_SCENARIO_IOS_SIMULATOR,
115+
],
116+
}),
117+
async (daemon) => {
118+
const flags = { platform: requestedPlatform };
119+
const direct = await daemon.callCommand('capabilities', [], flags, {
120+
meta: { devicePin: pin },
121+
});
122+
expect(assertRpcOk(direct)).toMatchObject({ device: { id: expectedId } });
123+
const client = createAgentDeviceClient(
124+
{ daemonBaseUrl: daemon.baseUrl, daemonAuthToken: daemon.token },
125+
{ devicePin: pin },
126+
);
127+
const result = await runCommand(client, 'capabilities', flags);
128+
assert.equal(result.device?.id, expectedId);
129+
const batch = await daemon.callCommand(
130+
'batch',
131+
[],
132+
{ batchSteps: [{ command: 'capabilities', flags }] },
133+
{ meta: { devicePin: pin } },
134+
);
135+
expect(assertRpcOk(batch)).toMatchObject({
136+
results: [{ data: { device: { id: expectedId } } }],
137+
});
138+
},
139+
);
140+
});
141+
}

0 commit comments

Comments
 (0)