Skip to content

Commit 03a0621

Browse files
committed
test(limrun): cover iOS appstate through the daemon when the instance is gone
The Limrun read tests hand-build their session, so the route from a real daemon to the Limrun owner had no coverage. Also state in the docs that any provider whose session has ended refuses appstate, not only Limrun.
1 parent 3fb0c4b commit 03a0621

2 files changed

Lines changed: 133 additions & 1 deletion

File tree

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
import assert from 'node:assert/strict';
2+
import { beforeEach, test, vi } from 'vitest';
3+
import type { DeviceLease } from '@agent-device/contracts/device';
4+
import { createLimrunRuntime, type LimrunRuntimeDependencies } from '@agent-device/provider-limrun';
5+
import { limrunTestDependencies } from '../../../src/platform-runtime-gateway.fixtures.ts';
6+
import { createProviderDeviceRuntimeRequestProviders } from '../../../src/provider-device-runtime.ts';
7+
import { assertRpcError, assertRpcOk } from './assertions.ts';
8+
import { createProviderScenarioHarness } from './harness.ts';
9+
10+
const BUNDLE_ID = 'com.apple.Preferences';
11+
12+
const limrunApiState = vi.hoisted(() => ({
13+
listApps: vi.fn(),
14+
launchApp: vi.fn(async () => undefined),
15+
}));
16+
17+
vi.mock('@limrun/api', () => ({
18+
default: class MockLimrun {
19+
readonly iosInstances = {
20+
create: vi.fn(async () => ({
21+
metadata: { id: 'limrun-appstate-test-instance' },
22+
status: { apiUrl: 'https://limrun.example', token: 'limrun-test-token' },
23+
})),
24+
list: vi.fn(),
25+
delete: vi.fn(async () => undefined),
26+
};
27+
28+
readonly androidInstances = { create: vi.fn(), list: vi.fn(), delete: vi.fn() };
29+
30+
readonly assets = { getOrUpload: vi.fn(), list: vi.fn() };
31+
},
32+
}));
33+
34+
vi.mock('@limrun/api/ios-client', () => ({
35+
createInstanceClient: vi.fn(async () => ({
36+
deviceInfo: { screenWidth: 320, screenHeight: 240 },
37+
disconnect: vi.fn(),
38+
launchApp: limrunApiState.launchApp,
39+
listApps: limrunApiState.listApps,
40+
})),
41+
}));
42+
43+
beforeEach(() => {
44+
limrunApiState.listApps.mockReset();
45+
limrunApiState.launchApp.mockClear();
46+
});
47+
48+
test('Limrun iOS appstate stops answering from the session once the instance is gone', async () => {
49+
limrunApiState.listApps.mockResolvedValue([]);
50+
const registration = createLimrunRuntime(
51+
{ apiKey: 'limrun-test-key' },
52+
limrunRuntimeDependencies(),
53+
{ includePlatformModule: true },
54+
);
55+
const runtime = registration.runtime;
56+
const providers = createProviderDeviceRuntimeRequestProviders([runtime]);
57+
const daemon = await createProviderScenarioHarness({
58+
...providers,
59+
deviceInventorySource: providers.deviceInventorySource!,
60+
platformRuntime: {
61+
providerRuntimes: [runtime],
62+
providerModules: [{ runtime, module: registration.platformModule }],
63+
},
64+
});
65+
66+
try {
67+
const lease = await allocateLease(daemon);
68+
const flags = {
69+
platform: 'ios' as const,
70+
tenant: 'team-a',
71+
runId: 'run-a',
72+
leaseId: lease.leaseId,
73+
leaseProvider: 'limrun',
74+
};
75+
const meta = {
76+
tenantId: 'team-a',
77+
runId: 'run-a',
78+
leaseId: lease.leaseId,
79+
leaseBackend: 'ios-instance' as const,
80+
leaseProvider: 'limrun',
81+
deviceKey: 'ios-a',
82+
clientId: 'client-a',
83+
};
84+
assertRpcOk(await daemon.callCommand('open', [BUNDLE_ID], flags, { meta }));
85+
assert.equal(limrunApiState.launchApp.mock.calls.length, 1);
86+
87+
const live = assertRpcOk<{ appBundleId?: string; source?: string }>(
88+
await daemon.callCommand('appstate', [], flags, { meta }),
89+
);
90+
assert.equal(live.appBundleId, BUNDLE_ID);
91+
assert.equal(live.source, 'session');
92+
93+
limrunApiState.listApps.mockRejectedValue(new Error('instance not found'));
94+
const gone = await daemon.callCommand('appstate', [], flags, { meta });
95+
96+
const refusal = assertRpcError(gone, 500, 'COMMAND_FAILED', /Could not confirm/);
97+
assert.equal((refusal.details as { reason?: string }).reason, 'limrun_instance_unreachable');
98+
} finally {
99+
await daemon.close();
100+
await runtime.shutdown();
101+
}
102+
});
103+
104+
async function allocateLease(
105+
daemon: Awaited<ReturnType<typeof createProviderScenarioHarness>>,
106+
): Promise<DeviceLease> {
107+
const response = await daemon.callCommand(
108+
'lease_allocate',
109+
[],
110+
{ platform: 'ios', tenant: 'team-a', runId: 'run-a', leaseProvider: 'limrun' },
111+
{
112+
meta: {
113+
tenantId: 'team-a',
114+
runId: 'run-a',
115+
leaseBackend: 'ios-instance',
116+
leaseProvider: 'limrun',
117+
deviceKey: 'ios-a',
118+
clientId: 'client-a',
119+
},
120+
},
121+
);
122+
return assertRpcOk<{ lease: DeviceLease }>(response).lease;
123+
}
124+
125+
function limrunRuntimeDependencies(): LimrunRuntimeDependencies {
126+
return {
127+
clientVersion: 'test-version',
128+
android: {} as LimrunRuntimeDependencies['android'],
129+
host: {} as LimrunRuntimeDependencies['host'],
130+
ios: limrunTestDependencies.ios,
131+
};
132+
}

‎website/docs/docs/commands.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,7 @@ agent-device apps --platform android --all
814814
```
815815
816816
- Android and HarmonyOS `appstate` report the live foreground package and activity.
817-
- iOS and macOS `appstate` need a session and report that session's app: `appName`, `appBundleId`, and a `source`. On iOS, when a live runner answers the read, the answer carries its `state` (`runningForeground`, `runningBackground`, `runningBackgroundSuspended`, `notRunning`, or `unknown`) with `source: runner`. They never start a runner; when none is running they report `source: session` and no `state`, and on macOS the answer is always session-scoped. When the read fails, for example because the runner cannot reply right now, `appstate` fails instead of answering from the session. Limrun iOS has no live read, so it answers with `source: session` only after the instance responds; when the instance cannot be confirmed it fails with reason `limrun_instance_unreachable`, and once its lease has ended `appstate` is refused with reason `owner-capability-missing`. On iOS, after `home` the app reports a background state; which app took the screen is not reported. A command that had to bring the app back reports a [`targetActivation` disclosure](#foreground-repairs-on-ios).
817+
- iOS and macOS `appstate` need a session and report that session's app: `appName`, `appBundleId`, and a `source`. On iOS, when a live runner answers the read, the answer carries its `state` (`runningForeground`, `runningBackground`, `runningBackgroundSuspended`, `notRunning`, or `unknown`) with `source: runner`. They never start a runner; when none is running they report `source: session` and no `state`, and on macOS the answer is always session-scoped. When the read fails, for example because the runner cannot reply right now, `appstate` fails instead of answering from the session. Limrun iOS has no live read, so it answers with `source: session` only after the instance responds; when the instance cannot be confirmed it fails with reason `limrun_instance_unreachable`. Any provider whose session has ended refuses `appstate` with reason `owner-capability-missing`. On iOS, after `home` the app reports a background state; which app took the screen is not reported. A command that had to bring the app back reports a [`targetActivation` disclosure](#foreground-repairs-on-ios).
818818
- `apps` shows user-installed apps. Add `--all` to include system and OEM apps.
819819
820820
## Foreground repairs on iOS

0 commit comments

Comments
 (0)