@flighthq/device 0.4.0-next.1903.50bfa67 → 0.4.0-next.1908.751021c

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +3 -3
  2. package/src/device.test.ts +81 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flighthq/device",
3
- "version": "0.4.0-next.1903.50bfa67",
3
+ "version": "0.4.0-next.1908.751021c",
4
4
  "author": "Joshua Granick and other contributors",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -38,8 +38,8 @@
38
38
  "clean:dist": "tsx ../../scripts/clean-package-dist.ts"
39
39
  },
40
40
  "dependencies": {
41
- "@flighthq/types": "0.4.0-next.1903.50bfa67",
42
- "@flighthq/useragent": "0.4.0-next.1903.50bfa67"
41
+ "@flighthq/types": "0.4.0-next.1908.751021c",
42
+ "@flighthq/useragent": "0.4.0-next.1908.751021c"
43
43
  },
44
44
  "devDependencies": {
45
45
  "typescript": "^5.3.0"
@@ -22,6 +22,10 @@ import {
22
22
  getSafeAreaInsets,
23
23
  refreshDeviceInfo,
24
24
  setDeviceBackend,
25
+ explainDeviceBackend,
26
+ installDeviceHostBackend,
27
+ observeDeviceHostResult,
28
+ resetDeviceBackendForTest,
25
29
  } from './device';
26
30
 
27
31
  function fakeBackend(): DeviceBackend {
@@ -241,6 +245,34 @@ describe('enableWebSafeAreaInsets', () => {
241
245
  });
242
246
  });
243
247
 
248
+ describe('explainDeviceBackend', () => {
249
+ afterEach(() => resetDeviceBackendForTest());
250
+
251
+ it('reports host-not-enabled when no backend is installed', () => {
252
+ resetDeviceBackendForTest();
253
+ const explanation = explainDeviceBackend();
254
+ expect(explanation.layer).toBe('host-not-enabled');
255
+ expect(explanation.conflict).toBe(false);
256
+ expect(explanation.viability).toBe('unobserved');
257
+ });
258
+
259
+ it('reports custom layer when a custom backend is set', () => {
260
+ setDeviceBackend(fakeBackend());
261
+ expect(explainDeviceBackend().layer).toBe('custom');
262
+ });
263
+
264
+ it('reports host layer when a host backend is installed', () => {
265
+ installDeviceHostBackend(fakeBackend());
266
+ expect(explainDeviceBackend().layer).toBe('host');
267
+ });
268
+
269
+ it('reports conflict when two different host backends are installed', () => {
270
+ installDeviceHostBackend(fakeBackend());
271
+ installDeviceHostBackend(fakeBackend());
272
+ expect(explainDeviceBackend().conflict).toBe(true);
273
+ });
274
+ });
275
+
244
276
  describe('getDeviceBackend', () => {
245
277
  it('falls back to a web backend when none is set', () => {
246
278
  expect(getDeviceBackend()).not.toBeNull();
@@ -352,6 +384,43 @@ describe('getSafeAreaInsets', () => {
352
384
  });
353
385
  });
354
386
 
387
+ describe('installDeviceHostBackend', () => {
388
+ afterEach(() => resetDeviceBackendForTest());
389
+
390
+ it('installs a host backend that getDeviceBackend returns', () => {
391
+ const backend = fakeBackend();
392
+ installDeviceHostBackend(backend);
393
+ expect(getDeviceBackend()).toBe(backend);
394
+ });
395
+
396
+ it('is first-host-wins: a second different backend sets conflict', () => {
397
+ const first = fakeBackend();
398
+ const second = fakeBackend();
399
+ installDeviceHostBackend(first);
400
+ installDeviceHostBackend(second);
401
+ expect(getDeviceBackend()).toBe(first);
402
+ expect(explainDeviceBackend().conflict).toBe(true);
403
+ });
404
+ });
405
+
406
+ describe('observeDeviceHostResult', () => {
407
+ afterEach(() => resetDeviceBackendForTest());
408
+
409
+ it('records a successful observation', () => {
410
+ installDeviceHostBackend(fakeBackend());
411
+ observeDeviceHostResult('getCapabilities', true);
412
+ const explanation = explainDeviceBackend();
413
+ expect(explanation.operation).toBe('getCapabilities');
414
+ expect(explanation.viability).toBe('available');
415
+ });
416
+
417
+ it('records a failed observation', () => {
418
+ installDeviceHostBackend(fakeBackend());
419
+ observeDeviceHostResult('getCapabilities', false);
420
+ expect(explainDeviceBackend().viability).toBe('runtime-api-unavailable');
421
+ });
422
+ });
423
+
355
424
  describe('refreshDeviceInfo', () => {
356
425
  it('does not throw on the default web backend', () => {
357
426
  expect(() => refreshDeviceInfo()).not.toThrow();
@@ -371,6 +440,18 @@ describe('refreshDeviceInfo', () => {
371
440
  });
372
441
  });
373
442
 
443
+ describe('resetDeviceBackendForTest', () => {
444
+ it('clears all backend slots', () => {
445
+ setDeviceBackend(fakeBackend());
446
+ installDeviceHostBackend(fakeBackend());
447
+ observeDeviceHostResult('getCapabilities', true);
448
+ resetDeviceBackendForTest();
449
+ expect(explainDeviceBackend().layer).toBe('host-not-enabled');
450
+ expect(explainDeviceBackend().conflict).toBe(false);
451
+ expect(explainDeviceBackend().viability).toBe('unobserved');
452
+ });
453
+ });
454
+
374
455
  describe('setDeviceBackend', () => {
375
456
  it('clears back to the web fallback when passed null', () => {
376
457
  setDeviceBackend(fakeBackend());