Repository navigation
Expand file tree
/
Copy pathplatform-runtime-gateway.test.ts
More file actions
765 lines (736 loc) · 28.1 KB
/
Copy pathplatform-runtime-gateway.test.ts
File metadata and controls
765 lines (736 loc) · 28.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
import fs from 'node:fs';
import {
type DeviceBinding,
localRuntimeOwner,
providerRuntimeOwner,
} from '@agent-device/contracts/platform-runtime';
import type {
PlatformRuntimeHost,
PlatformRuntimeOperations,
PlatformRuntimeOwner,
} from '@agent-device/contracts/platform-runtime-operations';
import { applicationLifecycleOperationFacts } from '@agent-device/contracts/application-lifecycle-runtime';
import { createUnavailablePlatformRuntimeFacts } from '@agent-device/contracts/platform-runtime-unavailable';
import { screenshotRuntimeOperationFacts } from '@agent-device/contracts/screenshot-runtime';
import { viewportRuntimeOperationFacts } from '@agent-device/contracts/viewport-runtime';
import type { DeviceInfo } from '@agent-device/kernel/device';
import { AppError } from '@agent-device/kernel/errors';
import { createLimrunRuntime } from '@agent-device/provider-limrun';
import { describe, expect, expectTypeOf, test, vi } from 'vitest';
import type { AgentDeviceHost } from './sdk/host-types.ts';
import { resolveDaemonPaths } from '@agent-device/daemon-contracts/daemon-resolution';
import { createTestHostEnvironment, mkdtempForTestSync } from '@agent-device/test-kit';
import { startDaemonRuntime } from '@agent-device/daemon';
import { composeDaemonRuntimeDeps } from './daemon-composition.ts';
import { createComposedPlatformRuntimeGateway } from './platform-runtime-gateway.ts';
import {
gatewayFixture as gateway,
gatewayFixtureDevice as device,
gatewayFixtureScope as scope,
LIFECYCLE_FACETS,
limrunTestDependencies,
providerLifecycleOwnerFixture as providerLifecycleOwner,
providerRuntimeFixture as providerRuntime,
runtimeOwnerFixture as runtimeOwner,
} from './platform-runtime-gateway.fixtures.ts';
describe('composed platform runtime gateway', () => {
test('the SDK close report exposes each platform owner outcome', () => {
expectTypeOf<Awaited<ReturnType<AgentDeviceHost['close']>>['platformOwners']>().toEqualTypeOf<
'closed' | 'pending' | 'failed'
>();
});
test('shutdown waits for an owner load already in progress', async () => {
const ref = providerRuntimeOwner('limrun', 'loading');
const shutdown = vi.fn(async () => {});
let finishLoad!: () => void;
const loadReady = new Promise<void>((resolve) => {
finishLoad = resolve;
});
const registration = providerRuntime({
ref,
load: async () => {
await loadReady;
return { ...runtimeOwner({ ref }), shutdown };
},
});
const runtimeGateway = gateway([registration]);
const binding = runtimeGateway.bind({
device,
intent: { kind: 'exact-owner', owner: ref, fence: { token: 'fence', generation: 1 } },
scope,
});
const closing = runtimeGateway.shutdown();
finishLoad();
await binding;
await closing;
expect(shutdown).toHaveBeenCalledOnce();
});
test('an unfinished owner load does not delay shutdown of a loaded owner', async () => {
const readyRef = providerRuntimeOwner('limrun', 'ready');
const loadingRef = providerRuntimeOwner('limrun', 'loading');
const readyShutdown = vi.fn(async () => {});
const lateShutdown = vi.fn(async () => {});
let finishLoad!: () => void;
const loadReady = new Promise<void>((resolve) => {
finishLoad = resolve;
});
const runtimeGateway = gateway([
providerRuntime({
ref: readyRef,
load: async () => ({ ...runtimeOwner({ ref: readyRef }), shutdown: readyShutdown }),
}),
providerRuntime({
ref: loadingRef,
load: async () => {
await loadReady;
return { ...runtimeOwner({ ref: loadingRef }), shutdown: lateShutdown };
},
}),
]);
await runtimeGateway.bind({
device,
intent: { kind: 'exact-owner', owner: readyRef, fence: { token: 'ready', generation: 1 } },
scope,
});
const loading = runtimeGateway.bind({
device,
intent: {
kind: 'exact-owner',
owner: loadingRef,
fence: { token: 'loading', generation: 1 },
},
scope,
});
const closing = runtimeGateway.shutdown();
try {
await new Promise<void>((resolve) => setImmediate(resolve));
expect(readyShutdown).toHaveBeenCalledOnce();
expect(lateShutdown).not.toHaveBeenCalled();
} finally {
finishLoad();
await loading;
await closing;
}
expect(readyShutdown).toHaveBeenCalledOnce();
expect(lateShutdown).toHaveBeenCalledOnce();
});
test('daemon reports a composed owner failure and retains registration while completing cleanup', async () => {
const ref = providerRuntimeOwner('limrun', 'failed');
const shutdown = vi.fn(async () => {
throw new AppError('COMMAND_FAILED', 'owner refused', {
reason: 'owner-test-refusal',
hint: 'Inspect the original owner failure.',
});
});
const registration = providerRuntime({
ref,
load: async () => ({ ...runtimeOwner({ ref }), shutdown }),
});
const composed = gateway([registration]);
await composed.bind({
device,
intent: { kind: 'exact-owner', owner: ref, fence: { token: 'fence', generation: 1 } },
scope,
});
const stateDir = mkdtempForTestSync('agent-device-owner-failure-');
const paths = resolveDaemonPaths(stateDir);
const finalize = vi.fn(async () => ['runner']);
const pluginClose = vi.fn(async () => []);
const clearLock = vi.fn(async () => {});
const runtime = await startDaemonRuntime({
host: createTestHostEnvironment({
env: {
...process.env,
AGENT_DEVICE_STATE_DIR: stateDir,
AGENT_DEVICE_DAEMON_IDLE_TIMEOUT_MS: '0',
AGENT_DEVICE_DAEMON_SERVER_MODE: 'http',
},
}),
stderr: { write: () => {} },
stdout: { write: () => {} },
composeDeps: async (input) => {
const deps = await composeDaemonRuntimeDeps(input, []);
const applicationLifecycle = deps.deviceRuntimeGateway.applicationLifecycle;
if (!applicationLifecycle)
throw new Error('Root composition must supply application lifecycle');
return {
...deps,
deviceRuntimeGateway: {
...composed,
applicationLifecycle: {
...applicationLifecycle,
finalizeDaemonShutdown: finalize,
},
},
shutdownProviderRuntimes: pluginClose,
ownerLifecycle: { ...deps.ownerLifecycle, clearDaemonLockConfiguration: clearLock },
};
},
});
const report = await runtime.close();
expect(report.platformOwners).toBe('failed');
expect(report.runners.stopped).toEqual(['runner']);
const log = fs.readFileSync(paths.logPath, 'utf8');
expect(log).toContain('daemon_shutdown_platform_owner_failed');
expect(log).toContain('owner refused');
expect(log).toContain('owner-test-refusal');
expect(log).toContain('Inspect the original owner failure.');
expect(fs.existsSync(paths.infoPath)).toBe(true);
expect(fs.existsSync(paths.lockPath)).toBe(true);
expect(clearLock).not.toHaveBeenCalled();
expect(pluginClose).toHaveBeenCalledOnce();
expect(finalize).toHaveBeenCalledOnce();
expect(await runtime.close()).toBe(report);
expect(shutdown).toHaveBeenCalledOnce();
});
test('settles all owners and preserves the failure across concurrent and repeated shutdown', async () => {
const failedRef = providerRuntimeOwner('limrun', 'failed');
const healthyRef = providerRuntimeOwner('limrun', 'healthy');
const failure = new Error('owner refused');
const failedShutdown = vi.fn(async () => {
throw failure;
});
const healthyShutdown = vi.fn(async () => {});
const failed = providerRuntime({
ref: failedRef,
load: async () => ({ ...runtimeOwner({ ref: failedRef }), shutdown: failedShutdown }),
});
const healthy = providerRuntime({
ref: healthyRef,
load: async () => ({ ...runtimeOwner({ ref: healthyRef }), shutdown: healthyShutdown }),
});
const runtimeGateway = gateway([failed, healthy]);
for (const ref of [failedRef, healthyRef]) {
await runtimeGateway.bind({
device,
intent: { kind: 'exact-owner', owner: ref, fence: { token: 'fence', generation: 1 } },
scope,
});
}
const first = runtimeGateway.shutdown();
const second = runtimeGateway.shutdown();
const results = await Promise.allSettled([first, second]);
for (const result of results) {
expect(result.status).toBe('rejected');
if (result.status === 'rejected') {
expect(result.reason).toBeInstanceOf(AggregateError);
expect(result.reason.message).toContain('failed');
expect(result.reason.errors[0].cause).toBe(failure);
}
}
await expect(runtimeGateway.shutdown()).rejects.toBe(
(results[0] as PromiseRejectedResult).reason,
);
expect(failedShutdown).toHaveBeenCalledOnce();
expect(healthyShutdown).toHaveBeenCalledOnce();
await expect(
runtimeGateway.bind({
device,
intent: {
kind: 'exact-owner',
owner: healthyRef,
fence: { token: 'fence', generation: 1 },
},
scope,
}),
).rejects.toThrow('shut down');
});
test('loads only the selected Apple owner for screenshot facts and binding', async () => {
const appleDevice: DeviceInfo = {
platform: 'apple',
appleOs: 'ios',
id: 'ios-simulator',
name: 'iPhone',
kind: 'simulator',
target: 'mobile',
booted: true,
};
const unavailable = { available: false, reason: 'unsupported-platform-leaf' } as const;
const owner = localRuntimeOwner('apple');
const baseFacts = createUnavailablePlatformRuntimeFacts(appleDevice, owner, {
appLog: unavailable,
network: unavailable,
screenshot: unavailable,
elementText: unavailable,
viewport: unavailable,
focus: unavailable,
gesture: unavailable,
scroll: unavailable,
typeText: unavailable,
back: unavailable,
orientation: unavailable,
tvRemote: unavailable,
keyboard: unavailable,
clipboard: unavailable,
systemButton: unavailable,
fold: unavailable,
telephony: unavailable,
triggerAppEvent: unavailable,
settings: unavailable,
readAlert: unavailable,
awaitAlert: unavailable,
acceptAlert: unavailable,
dismissAlert: unavailable,
touch: unavailable,
audioProbeCapture: unavailable,
audioProbeQuery: unavailable,
lifecycle: applicationLifecycleOperationFacts({
resolveOpenTarget: unavailable,
prepareApplicationOpen: unavailable,
openApplication: unavailable,
applyRuntimeHints: unavailable,
clearRuntimeHints: unavailable,
closeApplication: unavailable,
finalizeApplicationClose: unavailable,
prepareAppleRunner: unavailable,
configureProviderPortReverse: unavailable,
}),
});
const facts = {
...baseFacts,
operations: {
...baseFacts.operations,
...screenshotRuntimeOperationFacts({ capture: { available: true } }),
},
};
const captureScreenshot = vi.fn(async () => ({}));
const binding: DeviceBinding<PlatformRuntimeOperations> = {
device: appleDevice,
owner,
facts,
operations: { captureScreenshot },
[Symbol.asyncDispose]: async () => {},
};
const appleLoad = vi.fn(async () => ({
owner,
ownsDevice: () => true,
inspectFacts: async () => facts,
bind: async () => binding,
shutdown: async () => {},
}));
const webLoad = vi.fn(async () => {
throw new Error('unselected web runtime must stay lazy');
});
const runtimeGateway = createComposedPlatformRuntimeGateway({
modules: new Map([
['apple', { family: 'apple', loadRuntime: appleLoad }],
['web', { family: 'web', loadRuntime: webLoad }],
]),
loadHost: async () => ({}) as PlatformRuntimeHost,
});
await expect(runtimeGateway.inspectFacts(appleDevice)).resolves.toMatchObject({
operations: { captureScreenshot: { available: true } },
});
const selected = await runtimeGateway.bind({
device: appleDevice,
intent: { kind: 'ordinary' },
scope,
});
await selected.operations.captureScreenshot?.({ outPath: '/tmp/gateway.png' });
expect(appleLoad).toHaveBeenCalledTimes(1);
expect(webLoad).not.toHaveBeenCalled();
expect(captureScreenshot).toHaveBeenCalledWith({ outPath: '/tmp/gateway.png' });
});
test('loads only the selected web owner for viewport facts and binding', async () => {
const webDevice: DeviceInfo = {
platform: 'web',
id: 'web',
name: 'Web',
kind: 'device',
target: 'desktop',
booted: true,
};
const unavailable = { available: false, reason: 'unsupported-platform-leaf' } as const;
const available = { available: true } as const;
const owner = localRuntimeOwner('web');
const baseFacts = createUnavailablePlatformRuntimeFacts(webDevice, owner, {
appLog: unavailable,
network: unavailable,
screenshot: unavailable,
viewport: unavailable,
focus: unavailable,
gesture: unavailable,
scroll: unavailable,
typeText: unavailable,
touch: unavailable,
elementText: unavailable,
back: unavailable,
orientation: unavailable,
tvRemote: unavailable,
keyboard: unavailable,
clipboard: unavailable,
systemButton: unavailable,
fold: unavailable,
telephony: unavailable,
triggerAppEvent: unavailable,
settings: unavailable,
readAlert: unavailable,
awaitAlert: unavailable,
acceptAlert: unavailable,
dismissAlert: unavailable,
audioProbeCapture: unavailable,
audioProbeQuery: unavailable,
lifecycle: applicationLifecycleOperationFacts({
resolveOpenTarget: unavailable,
prepareApplicationOpen: unavailable,
openApplication: unavailable,
applyRuntimeHints: unavailable,
clearRuntimeHints: unavailable,
closeApplication: unavailable,
finalizeApplicationClose: unavailable,
prepareAppleRunner: unavailable,
configureProviderPortReverse: unavailable,
}),
});
const facts = {
...baseFacts,
operations: {
...baseFacts.operations,
...viewportRuntimeOperationFacts({ setViewport: available }),
},
};
const setViewport = vi.fn(async () => undefined);
const binding: DeviceBinding<PlatformRuntimeOperations> = {
device: webDevice,
owner,
facts,
operations: { setViewport },
[Symbol.asyncDispose]: async () => {},
};
const webLoad = vi.fn(async () => ({
owner,
ownsDevice: () => true,
inspectFacts: async () => facts,
bind: async () => binding,
shutdown: async () => {},
}));
const appleLoad = vi.fn(async () => {
throw new Error('unselected Apple runtime must stay lazy');
});
const runtimeGateway = createComposedPlatformRuntimeGateway({
modules: new Map([
['web', { family: 'web', loadRuntime: webLoad }],
['apple', { family: 'apple', loadRuntime: appleLoad }],
]),
loadHost: async () => ({}) as PlatformRuntimeHost,
});
await expect(runtimeGateway.inspectFacts(webDevice)).resolves.toMatchObject({
operations: { setViewport: { available: true } },
});
const selected = await runtimeGateway.bind({
device: webDevice,
intent: { kind: 'ordinary' },
scope,
});
await selected.operations.setViewport?.({ width: 1280, height: 900 });
expect(webLoad).toHaveBeenCalledTimes(1);
expect(appleLoad).not.toHaveBeenCalled();
expect(setViewport).toHaveBeenCalledWith({ width: 1280, height: 900 });
});
// Which resources need recovering is the host's composition (see
// platform-runtime-application-resources.test.ts). The gateway owns only the lazy host load and
// the once-per-process shape of each durable phase.
test('durable lifecycle phases load the host lazily and run once per process', async () => {
const recoverStartupResources = vi.fn(async () => {});
const detachForDaemonShutdown = vi.fn(async () => {});
const finalizeDaemonShutdown = vi.fn(async () => {});
const host = {
applicationResources: {
recoverStartupResources,
detachForDaemonShutdown,
finalizeDaemonShutdown,
},
} as unknown as PlatformRuntimeHost;
const loadHost = vi.fn(async () => host);
const runtimeGateway = createComposedPlatformRuntimeGateway({
modules: new Map(),
loadHost,
});
const lifecycle = runtimeGateway.applicationLifecycle;
if (!lifecycle) throw new Error('expected the composed lifecycle gateway');
const stateDir = `/tmp/platform-runtime-marker-evidence-${Date.now()}`;
await lifecycle.recoverStartupResources({ stateDir });
expect(loadHost).toHaveBeenCalledOnce();
expect(recoverStartupResources).toHaveBeenCalledExactlyOnceWith({ stateDir });
await lifecycle.detachForDaemonShutdown();
await lifecycle.detachForDaemonShutdown();
await lifecycle.finalizeDaemonShutdown();
await lifecycle.finalizeDaemonShutdown();
expect(detachForDaemonShutdown).toHaveBeenCalledOnce();
expect(finalizeDaemonShutdown).toHaveBeenCalledOnce();
});
test('inspects only the selected lazy owner and does not bind it', async () => {
const selectedRef = providerRuntimeOwner('limrun', 'selected');
const selectedOwner = runtimeOwner({ ref: selectedRef });
const inspectFacts = vi.spyOn(selectedOwner, 'inspectFacts');
const bind = vi.spyOn(selectedOwner, 'bind');
const unrelatedLoad = vi.fn(async () => {
throw new Error('must stay lazy');
});
const runtimeGateway = gateway([
providerRuntime({
ref: providerRuntimeOwner('limrun', 'unrelated'),
ownsDevice: () => false,
load: unrelatedLoad,
}),
providerRuntime({ ref: selectedRef, load: async () => selectedOwner }),
]);
await expect(runtimeGateway.inspectFacts(device)).resolves.toMatchObject({
operations: { ensureReady: { available: false } },
});
expect(inspectFacts).toHaveBeenCalledOnce();
expect(bind).not.toHaveBeenCalled();
expect(unrelatedLoad).not.toHaveBeenCalled();
});
test('keeps a stale Limrun Android id unavailable without binding or local fallback', async () => {
const registration = createLimrunRuntime(
{ apiKey: 'test-key', runtimeInstance: 'test-instance' },
limrunTestDependencies,
{ includePlatformModule: true },
);
const staleDevice: DeviceInfo = {
platform: 'android',
id: 'limrun:android:released-lease',
name: 'Released Limrun Android',
kind: 'emulator',
target: 'mobile',
booted: true,
};
let inspectCount = 0;
let bindCount = 0;
const providerModule = {
...registration.platformModule,
loadRuntime: async (host: PlatformRuntimeHost) => {
const owner = await registration.platformModule.loadRuntime(host);
return {
...owner,
inspectFacts: async (device: DeviceInfo) => {
inspectCount += 1;
return await owner.inspectFacts(device);
},
bind: async (request: Parameters<PlatformRuntimeOwner['bind']>[0]) => {
bindCount += 1;
return await owner.bind(request);
},
};
},
};
const localLoad = vi.fn(async () => {
throw new Error('stale Limrun identity must not fall back to local Android');
});
const runtimeGateway = createComposedPlatformRuntimeGateway({
modules: new Map([['android', { family: 'android', loadRuntime: localLoad }]]),
loadHost: async () => ({}) as PlatformRuntimeHost,
providerRuntimes: [registration.runtime],
providerModules: [{ runtime: registration.runtime, module: providerModule }],
});
try {
const facts = await runtimeGateway.inspectFacts(staleDevice);
expect(facts.operations.appState).toMatchObject({
available: false,
reason: 'owner-capability-missing',
hint: 'Limrun requires a matching live provider session for this device.',
});
expect(inspectCount).toBe(1);
expect(bindCount).toBe(0);
expect(localLoad).not.toHaveBeenCalled();
} finally {
await runtimeGateway.shutdown();
await registration.runtime.shutdown();
}
});
test('rejects inspected facts with a provider-mode mismatch', async () => {
const ref = providerRuntimeOwner('limrun', 'stable');
await expect(
gateway([providerRuntime({ ref, mismatch: 'facts' })]).inspectFacts(device),
).rejects.toMatchObject({ details: { reason: 'runtime-contract-invalid' } });
});
test('selects an exact provider owner without ordinary ownsDevice arbitration', async () => {
const ownsDevice = vi.fn(() => false);
const ref = providerRuntimeOwner('limrun', 'stable');
const runtime = providerRuntime({ ref, ownsDevice });
const runtimeGateway = gateway([runtime]);
const binding = await runtimeGateway.bind({
device,
intent: { kind: 'exact-owner', owner: ref, fence: { token: 'fence', generation: 1 } },
scope,
});
expect(binding.owner).toEqual(ref);
expect(ownsDevice).not.toHaveBeenCalled();
});
test('rejects ambiguous ordinary provider ownership', async () => {
const first = providerRuntime({
provider: 'first',
ref: providerRuntimeOwner('first', 'stable'),
ownsDevice: () => true,
});
const second = providerRuntime({
provider: 'second',
ref: providerRuntimeOwner('second', 'stable'),
ownsDevice: () => true,
});
await expect(
gateway([first, second]).bind({ device, intent: { kind: 'ordinary' }, scope }),
).rejects.toMatchObject({ details: { reason: 'runtime-contract-invalid' } });
});
test('rejects duplicate stable provider owner refs', async () => {
const ref = providerRuntimeOwner('limrun', 'stable');
expect(() => gateway([providerRuntime({ ref }), providerRuntime({ ref })])).toThrow(
'Duplicate platform runtime owner',
);
});
test('loads only the exact provider instance selected by stable owner metadata', async () => {
const target = providerRuntimeOwner('limrun', 'target');
const unrelatedLoad = vi.fn(async () => {
throw new Error('must stay lazy');
});
const runtimeGateway = gateway([
providerRuntime({ ref: providerRuntimeOwner('limrun', 'other'), load: unrelatedLoad }),
providerRuntime({ ref: target, ownsDevice: () => false }),
]);
await expect(
runtimeGateway.bind({
device,
intent: { kind: 'exact-owner', owner: target, fence: { token: 'fence', generation: 1 } },
scope,
}),
).resolves.toMatchObject({ owner: target });
expect(unrelatedLoad).not.toHaveBeenCalled();
});
test('does not fall back to a local runtime for a provider without an app-log module', async () => {
const hostLoad = vi.fn(async () => ({}) as PlatformRuntimeHost);
const localLoad = vi.fn(async () =>
runtimeOwner({ ref: { kind: 'local-family', family: 'apple' } }),
);
const runtimeGateway = createComposedPlatformRuntimeGateway({
modules: new Map([['apple', { family: 'apple', loadRuntime: localLoad }]]),
loadHost: hostLoad,
providerRuntimes: [
{
provider: 'webdriver',
leaseLifecycle: {},
deviceInventoryProvider: async () => null,
ownsDevice: () => true,
getInteractor: () => undefined,
shutdown: async () => {},
},
],
});
const binding = await runtimeGateway.bind({ device, intent: { kind: 'ordinary' }, scope });
expect(binding.facts.operations.appLogInspect).toMatchObject({
available: false,
reason: 'unsupported-provider-mode',
});
expect(binding.facts.operations.listApps).toMatchObject({
available: false,
reason: 'unsupported-provider-mode',
});
expect(binding.facts.operations.appState).toMatchObject({
available: false,
reason: 'unsupported-provider-mode',
});
expect(hostLoad).not.toHaveBeenCalled();
expect(localLoad).not.toHaveBeenCalled();
const inspected = await runtimeGateway.inspectFacts(device);
// The bind and inspect arms build this fact set independently; both must derive from the same
// construction path rather than two hand-maintained maps that can drift from each other.
expect(binding.facts).toStrictEqual(inspected);
});
test.each(LIFECYCLE_FACETS)(
'keeps provider-owned unsupported $0 operations closed without loading a local runtime',
async (facet, operations) => {
const ref = providerRuntimeOwner('webdriver', `unsupported-${facet}`);
const owner = providerLifecycleOwner(ref, facet);
const localLoad = vi.fn(async () => {
throw new Error('local lifecycle fallback must not load');
});
const registration = providerRuntime({
ref,
provider: 'webdriver',
ownsDevice: () => true,
load: async () => owner,
});
const runtimeGateway = createComposedPlatformRuntimeGateway({
modules: new Map([['apple', { family: 'apple', loadRuntime: localLoad }]]),
loadHost: async () => ({}) as PlatformRuntimeHost,
providerRuntimes: [registration.runtime],
providerModules: [registration],
});
const facts = await runtimeGateway.inspectFacts(device);
expect(facts.operations.captureScreenshot).toMatchObject({
available: false,
reason: 'owner-capability-missing',
});
expect(facts.operations.appLogInspect).toMatchObject({
available: false,
reason: 'unsupported-provider-mode',
});
for (const operation of operations) {
expect(facts.operations[operation].available).toBe(false);
}
const binding = await runtimeGateway.bind({ device, intent: { kind: 'ordinary' }, scope });
expect(binding.owner).toEqual(ref);
for (const operation of operations) {
expect(binding.operations[operation]).toBeUndefined();
}
expect(localLoad).not.toHaveBeenCalled();
},
);
test('rejects a swapped local module before loading host mechanics', async () => {
const hostLoad = vi.fn(async () => ({}) as PlatformRuntimeHost);
const runtimeGateway = createComposedPlatformRuntimeGateway({
modules: new Map([
[
'apple',
{
family: 'android',
loadRuntime: async () =>
runtimeOwner({ ref: { kind: 'local-family', family: 'android' } }),
},
],
]),
loadHost: hostLoad,
});
await expect(
runtimeGateway.bind({ device, intent: { kind: 'ordinary' }, scope }),
).rejects.toMatchObject({ details: { reason: 'runtime-contract-invalid' } });
expect(hostLoad).not.toHaveBeenCalled();
});
test('accepts transport-composed facts from the selected local family owner', async () => {
const ref = { kind: 'local-family', family: 'apple' } as const;
const runtimeGateway = createComposedPlatformRuntimeGateway({
modules: new Map([
[
'apple',
{
family: 'apple',
loadRuntime: async () => runtimeOwner({ ref, providerMode: 'transport-composed' }),
},
],
]),
loadHost: async () => ({}) as PlatformRuntimeHost,
});
await expect(
runtimeGateway.bind({ device, intent: { kind: 'ordinary' }, scope }),
).resolves.toMatchObject({
owner: ref,
facts: { device: { providerMode: 'transport-composed' } },
});
});
test.each(['owner', 'device', 'facts'] as const)(
'disposes a provider binding with mismatched %s identity',
async (mismatch) => {
const disposed = vi.fn(async () => {});
const ref = providerRuntimeOwner('limrun', 'stable');
const runtime = providerRuntime({ ref, mismatch, disposed });
await expect(
gateway([runtime]).bind({
device,
intent: { kind: 'exact-owner', owner: ref, fence: { token: 'fence', generation: 1 } },
scope,
}),
).rejects.toMatchObject({ details: { reason: 'runtime-contract-invalid' } });
expect(disposed).toHaveBeenCalledOnce();
},
);
});