@flighthq/application 0.5.0-next.1944.cd6b6e7 → 0.5.1-edge.638.d4dbd4e

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.
@@ -1,7 +1,32 @@
1
+ import { createEntity } from '@flighthq/entity/contract';
1
2
  import { cancelSignal, connectSignal, emitSignal } from '@flighthq/signals/contract';
2
- import type { Matrix, RenderState, WindowBackend } from '@flighthq/types/contract';
3
+ import { EntityRuntimeKey } from '@flighthq/types/contract';
4
+ import type {
5
+ EntityWithoutRuntime,
6
+ FullscreenBackend,
7
+ FullscreenTargetHandle,
8
+ HasGraphicsRenderContextSubscription,
9
+ HasGraphicsRenderSurface,
10
+ HasInputDropFileSubscription,
11
+ HasInputFocusSubscription,
12
+ HasInputPointerLock,
13
+ InputDropFileBackend,
14
+ InputFocusBackend,
15
+ InputPointerLockBackend,
16
+ InputPointerLockExitOutcome,
17
+ InputPointerLockRequestOutcome,
18
+ InputTargetBackend,
19
+ InputTargetHandle,
20
+ Matrix,
21
+ RenderContextBackend,
22
+ RenderState,
23
+ RenderSurfaceBackend,
24
+ WindowBackend,
25
+ WindowResizeTargetHandle,
26
+ } from '@flighthq/types/contract';
3
27
 
4
28
  import {
29
+ attachWindow,
5
30
  attachWindowClose,
6
31
  attachWindowDropFile,
7
32
  attachWindowFocus,
@@ -29,27 +54,21 @@ import {
29
54
  disposeApplicationWindow,
30
55
  exitApplicationFullscreen,
31
56
  exitApplicationPointerLock,
32
- explainWindowBackend,
33
57
  flashWindowFrame,
34
58
  focusWindow,
35
- getWindowBackend,
36
59
  getWindowBounds,
37
- getWindowDisplay,
38
60
  hideWindow,
39
- installWindowHostBackend,
40
61
  lockApplicationPointer,
41
62
  maximizeWindow,
42
63
  minimizeWindow,
43
- observeWindowHostResult,
64
+ notifyWindowClosed,
44
65
  openWindow,
45
66
  prepareElementForInput,
46
67
  requestApplicationFullscreen,
47
68
  requestWindowAttention,
48
69
  requestWindowClose,
49
- resetWindowBackendForTest,
50
70
  restoreWindow,
51
71
  setWindowAlwaysOnTop,
52
- setWindowBackend,
53
72
  setWindowContentProtection,
54
73
  setWindowFullscreen,
55
74
  setWindowHasShadow,
@@ -68,14 +87,104 @@ import {
68
87
  showWindow,
69
88
  } from './window';
70
89
 
90
+ type RecordingWindowBackend = Required<WindowBackend> & {
91
+ readonly calls: string[];
92
+ emitCloseRequest(): boolean;
93
+ emitClosed(): void;
94
+ emitMove(x: number, y: number): void;
95
+ emitOrientation(): void;
96
+ emitResize(width: number, height: number, devicePixelRatio: number): void;
97
+ emitVisibility(visible: boolean): void;
98
+ };
99
+
100
+ type RecordingFullscreenBackend = Required<FullscreenBackend> & {
101
+ readonly calls: string[];
102
+ emit(fullscreen: boolean): void;
103
+ };
104
+
105
+ type RecordingInputDropFileBackend = InputDropFileBackend & {
106
+ readonly calls: string[];
107
+ emit(path: string): void;
108
+ };
109
+
110
+ type RecordingInputFocusBackend = InputFocusBackend & {
111
+ readonly calls: string[];
112
+ emitBlur(): void;
113
+ emitFocus(): void;
114
+ };
115
+
116
+ type RecordingInputPointerLockBackend = InputPointerLockBackend & { readonly calls: string[] };
117
+
118
+ type RecordingRenderContextBackend = RenderContextBackend & {
119
+ readonly calls: string[];
120
+ emitLost(): void;
121
+ emitRestored(): void;
122
+ };
123
+
124
+ type RecordingRenderSurfaceBackend = RenderSurfaceBackend & { readonly calls: string[] };
125
+
126
+ type TestHost = {
127
+ readonly graphics: {
128
+ readonly renderContext: RecordingRenderContextBackend;
129
+ readonly renderSurface: RecordingRenderSurfaceBackend;
130
+ };
131
+ readonly input: {
132
+ readonly dropFile: RecordingInputDropFileBackend;
133
+ readonly focus: RecordingInputFocusBackend;
134
+ readonly pointerLock: RecordingInputPointerLockBackend;
135
+ };
136
+ readonly ui: { readonly fullscreen: RecordingFullscreenBackend };
137
+ readonly window: RecordingWindowBackend;
138
+ };
139
+
140
+ type WindowTargetHost = HasGraphicsRenderContextSubscription &
141
+ HasGraphicsRenderSurface &
142
+ HasInputDropFileSubscription &
143
+ HasInputFocusSubscription &
144
+ HasInputPointerLock;
145
+
71
146
  function makeRenderState(): RenderState {
72
147
  return { renderTransform2D: { a: 0, b: 0, c: 0, d: 0, tx: 0, ty: 0 } } as unknown as RenderState;
73
148
  }
74
149
 
75
- function recordingWindowBackend(): WindowBackend & { calls: string[] } {
150
+ function recordingWindowBackend(): RecordingWindowBackend {
151
+ const closeSubscriptions = new Set<{
152
+ readonly onClose: () => void;
153
+ readonly onCloseRequest: () => boolean;
154
+ }>();
155
+ const moveListeners = new Set<(x: number, y: number) => void>();
156
+ const orientationListeners = new Set<() => void>();
157
+ const resizeListeners = new Set<(width: number, height: number, devicePixelRatio: number) => void>();
158
+ const visibilityListeners = new Set<(visible: boolean) => void>();
76
159
  const calls: string[] = [];
77
160
  return {
78
161
  calls,
162
+ emitCloseRequest() {
163
+ let cancelled = false;
164
+ for (const subscription of closeSubscriptions) {
165
+ if (subscription.onCloseRequest()) cancelled = true;
166
+ }
167
+ return cancelled;
168
+ },
169
+ emitClosed() {
170
+ for (const subscription of closeSubscriptions) subscription.onClose();
171
+ },
172
+ emitMove(x, y) {
173
+ for (const listener of moveListeners) listener(x, y);
174
+ },
175
+ emitOrientation() {
176
+ for (const listener of orientationListeners) listener();
177
+ },
178
+ emitResize(width, height, devicePixelRatio) {
179
+ for (const listener of resizeListeners) listener(width, height, devicePixelRatio);
180
+ },
181
+ emitVisibility(visible) {
182
+ for (const listener of visibilityListeners) listener(visible);
183
+ },
184
+ attach(_win, _handle, ownership) {
185
+ calls.push(`attach:${ownership}`);
186
+ return true;
187
+ },
79
188
  open(_win, options) {
80
189
  calls.push(`open:${options.title ?? ''}`);
81
190
  return true;
@@ -93,6 +202,7 @@ function recordingWindowBackend(): WindowBackend & { calls: string[] } {
93
202
  calls.push(`setSize:${width},${height}`);
94
203
  },
95
204
  getBounds(_win, out) {
205
+ calls.push('getBounds');
96
206
  out.x = 1;
97
207
  out.y = 2;
98
208
  out.width = 3;
@@ -165,96 +275,370 @@ function recordingWindowBackend(): WindowBackend & { calls: string[] } {
165
275
  setHasShadow(_win, hasShadow) {
166
276
  calls.push(`setHasShadow:${hasShadow}`);
167
277
  },
278
+ subscribeClose(onCloseRequest, onClose) {
279
+ const subscription = { onClose, onCloseRequest };
280
+ closeSubscriptions.add(subscription);
281
+ return () => closeSubscriptions.delete(subscription);
282
+ },
283
+ subscribeMove(listener) {
284
+ moveListeners.add(listener);
285
+ return () => moveListeners.delete(listener);
286
+ },
287
+ subscribeOrientation(listener) {
288
+ orientationListeners.add(listener);
289
+ return () => orientationListeners.delete(listener);
290
+ },
291
+ subscribeResize(_target, listener) {
292
+ resizeListeners.add(listener);
293
+ return () => resizeListeners.delete(listener);
294
+ },
295
+ subscribeVisibility(listener) {
296
+ visibilityListeners.add(listener);
297
+ return () => visibilityListeners.delete(listener);
298
+ },
299
+ };
300
+ }
301
+
302
+ function createInputTarget(): InputTargetHandle {
303
+ return createEntity({ __brand: 'InputTargetHandle' as const });
304
+ }
305
+
306
+ function createWindowResizeTarget(): WindowResizeTargetHandle {
307
+ return { __brand: 'WindowResizeTargetHandle' };
308
+ }
309
+
310
+ function recordingFullscreenBackend(): RecordingFullscreenBackend {
311
+ const callbacks = new Set<(fullscreen: boolean) => void>();
312
+ const calls: string[] = [];
313
+ return {
314
+ calls,
315
+ emit(fullscreen) {
316
+ for (const callback of callbacks) callback(fullscreen);
317
+ },
318
+ async exit() {
319
+ calls.push('exit');
320
+ return true;
321
+ },
322
+ async request() {
323
+ calls.push('request');
324
+ return true;
325
+ },
326
+ subscribe(callback) {
327
+ calls.push('subscribe');
328
+ callbacks.add(callback);
329
+ },
330
+ unsubscribe(callback) {
331
+ calls.push('unsubscribe');
332
+ callbacks.delete(callback);
333
+ },
334
+ };
335
+ }
336
+
337
+ function recordingInputDropFileBackend(): RecordingInputDropFileBackend {
338
+ let listener: ((path: string) => void) | null = null;
339
+ const calls: string[] = [];
340
+ return createEntity<EntityWithoutRuntime<RecordingInputDropFileBackend>>({
341
+ calls,
342
+ emit(path: string) {
343
+ listener?.(path);
344
+ },
345
+ subscribe(_target: InputTargetHandle, next: (path: string) => void) {
346
+ calls.push('subscribe');
347
+ listener = next;
348
+ return () => {
349
+ calls.push('release');
350
+ if (listener === next) listener = null;
351
+ };
352
+ },
353
+ });
354
+ }
355
+
356
+ function recordingInputFocusBackend(): RecordingInputFocusBackend {
357
+ let onBlur: (() => void) | null = null;
358
+ let onFocus: (() => void) | null = null;
359
+ const calls: string[] = [];
360
+ return createEntity<EntityWithoutRuntime<RecordingInputFocusBackend>>({
361
+ calls,
362
+ emitBlur() {
363
+ onBlur?.();
364
+ },
365
+ emitFocus() {
366
+ onFocus?.();
367
+ },
368
+ subscribe(_target: InputTargetHandle, nextFocus: () => void, nextBlur: () => void) {
369
+ calls.push('subscribe');
370
+ onFocus = nextFocus;
371
+ onBlur = nextBlur;
372
+ return () => {
373
+ calls.push('release');
374
+ if (onFocus === nextFocus) onFocus = null;
375
+ if (onBlur === nextBlur) onBlur = null;
376
+ };
377
+ },
378
+ });
379
+ }
380
+
381
+ function recordingInputPointerLockBackend(): RecordingInputPointerLockBackend {
382
+ const calls: string[] = [];
383
+ return createEntity<EntityWithoutRuntime<RecordingInputPointerLockBackend>>({
384
+ calls,
385
+ async exit() {
386
+ calls.push('exit');
387
+ return { reason: 'ok' };
388
+ },
389
+ async request(_target: InputTargetHandle) {
390
+ calls.push('request');
391
+ return { reason: 'ok' };
392
+ },
393
+ });
394
+ }
395
+
396
+ function recordingRenderContextBackend(): RecordingRenderContextBackend {
397
+ let onLost: (() => void) | null = null;
398
+ let onRestored: (() => void) | null = null;
399
+ const calls: string[] = [];
400
+ return createEntity<EntityWithoutRuntime<RecordingRenderContextBackend>>({
401
+ calls,
402
+ emitLost() {
403
+ onLost?.();
404
+ },
405
+ emitRestored() {
406
+ onRestored?.();
407
+ },
408
+ subscribe(_target: InputTargetHandle, nextLost: () => void, nextRestored: () => void) {
409
+ calls.push('subscribe');
410
+ onLost = nextLost;
411
+ onRestored = nextRestored;
412
+ return () => {
413
+ calls.push('release');
414
+ if (onLost === nextLost) onLost = null;
415
+ if (onRestored === nextRestored) onRestored = null;
416
+ };
417
+ },
418
+ });
419
+ }
420
+
421
+ function recordingRenderSurfaceBackend(): RecordingRenderSurfaceBackend {
422
+ const calls: string[] = [];
423
+ return createEntity<EntityWithoutRuntime<RecordingRenderSurfaceBackend>>({
424
+ calls,
425
+ resize(_target: InputTargetHandle, width: number, height: number) {
426
+ calls.push(`resize:${width},${height}`);
427
+ },
428
+ });
429
+ }
430
+
431
+ function createTestHost(windowBackend: RecordingWindowBackend = recordingWindowBackend()): TestHost {
432
+ return {
433
+ graphics: { renderContext: recordingRenderContextBackend(), renderSurface: recordingRenderSurfaceBackend() },
434
+ input: {
435
+ dropFile: recordingInputDropFileBackend(),
436
+ focus: recordingInputFocusBackend(),
437
+ pointerLock: recordingInputPointerLockBackend(),
438
+ },
439
+ ui: { fullscreen: recordingFullscreenBackend() },
440
+ window: windowBackend,
168
441
  };
169
442
  }
170
443
 
171
- afterEach(() => resetWindowBackendForTest());
444
+ let host: TestHost;
445
+
446
+ beforeEach(() => {
447
+ host = createTestHost();
448
+ });
449
+
450
+ describe('attachWindow', () => {
451
+ it('attaches an existing handle without creating an Application and preserves identity', () => {
452
+ const backend = recordingWindowBackend();
453
+ const seen: { handle: unknown; win: unknown }[] = [];
454
+ backend.attach = (win, handle) => {
455
+ seen.push({ handle, win });
456
+ return true;
457
+ };
458
+ host = createTestHost(backend);
459
+ const win = createApplicationWindow();
460
+ const handle = { id: 41 };
461
+
462
+ expect(attachWindow(host, win, handle, 'host')).toBe(true);
463
+ expect(seen).toEqual([{ handle, win }]);
464
+ });
465
+
466
+ it('uses one explicit host for two independently attached windows', () => {
467
+ const backend = recordingWindowBackend();
468
+ const attached: unknown[] = [];
469
+ const closed: unknown[] = [];
470
+ backend.attach = (win) => {
471
+ attached.push(win);
472
+ return true;
473
+ };
474
+ backend.close = (win) => closed.push(win);
475
+ host = createTestHost(backend);
476
+ const first = createApplicationWindow();
477
+ const second = createApplicationWindow();
478
+
479
+ expect(attachWindow(host, first, { id: 1 }, 'host')).toBe(true);
480
+ expect(attachWindow(host, second, { id: 2 }, 'flight')).toBe(true);
481
+ expect(attached).toEqual([first, second]);
482
+ expect(closeWindow(host, first)).toBe(true);
483
+ expect(closeWindow(host, first)).toBe(true);
484
+ expect(closed).toEqual([first]);
485
+ });
486
+
487
+ it('emits one terminal close when a synchronous native callback and closeWindow converge', () => {
488
+ const backend = recordingWindowBackend();
489
+ backend.close = (win) => notifyWindowClosed(win);
490
+ host = createTestHost(backend);
491
+ const win = createApplicationWindow();
492
+ let closed = 0;
493
+ let forwarded = 0;
494
+ connectSignal(win.onClose, () => closed++);
495
+ connectSignal(win.onFullscreenChanged, () => forwarded++);
496
+ expect(attachWindow(host, win, { id: 1 }, 'flight')).toBe(true);
497
+ attachWindowFullscreen(host, win);
498
+
499
+ expect(closeWindow(host, win)).toBe(true);
500
+ notifyWindowClosed(win);
501
+ host.ui.fullscreen.emit(true);
502
+ expect(closed).toBe(1);
503
+ expect(forwarded).toBe(0);
504
+ });
505
+
506
+ it('clears terminal state after the same window successfully reattaches', () => {
507
+ const backend = recordingWindowBackend();
508
+ host = createTestHost(backend);
509
+ const win = createApplicationWindow();
510
+ let closed = 0;
511
+ connectSignal(win.onClose, () => closed++);
512
+
513
+ expect(attachWindow(host, win, { id: 1 }, 'host')).toBe(true);
514
+ expect(closeWindow(host, win)).toBe(true);
515
+ expect(attachWindow(host, win, { id: 1 }, 'host')).toBe(true);
516
+ expect(closeWindow(host, win)).toBe(true);
517
+ expect(closed).toBe(2);
518
+ });
519
+
520
+ it('pins close to the host that attached the window', () => {
521
+ const origin = createTestHost();
522
+ const active = createTestHost();
523
+ const win = createApplicationWindow();
524
+
525
+ expect(attachWindow(origin, win, { id: 1 }, 'host')).toBe(true);
526
+ expect(closeWindow(active, win)).toBe(true);
527
+
528
+ expect(origin.window.calls).toEqual(['attach:host', 'close']);
529
+ expect(active.window.calls).toEqual([]);
530
+ });
531
+ });
172
532
 
173
533
  describe('attachWindowClose', () => {
174
- it('emits onClose on pagehide', () => {
534
+ it('emits onClose when the host reports a terminal close', () => {
175
535
  const win = createApplicationWindow();
176
536
  let closed = false;
177
537
  connectSignal(win.onClose, () => {
178
538
  closed = true;
179
539
  });
180
- attachWindowClose(win);
181
- window.dispatchEvent(new Event('pagehide'));
540
+ attachWindowClose(host, win);
541
+ host.window.emitClosed();
182
542
  expect(closed).toBe(true);
183
543
  });
184
544
 
185
- it('emits onCloseRequest on beforeunload', () => {
545
+ it('emits onCloseRequest and reports whether it was cancelled to the host', () => {
186
546
  const win = createApplicationWindow();
187
547
  let requested = false;
188
548
  connectSignal(win.onCloseRequest, () => {
189
549
  requested = true;
550
+ cancelSignal(win.onCloseRequest);
190
551
  });
191
- attachWindowClose(win);
192
- window.dispatchEvent(new Event('beforeunload'));
552
+ attachWindowClose(host, win);
553
+
554
+ expect(host.window.emitCloseRequest()).toBe(true);
193
555
  expect(requested).toBe(true);
194
556
  });
195
557
  });
196
558
 
197
559
  describe('attachWindowDropFile', () => {
198
- it('emits onDropFile with file name', () => {
199
- const element = document.createElement('div');
560
+ it('forwards host-emitted paths to onDropFile', () => {
200
561
  const win = createApplicationWindow();
201
562
  let received: string | null = null;
202
563
  connectSignal(win.onDropFile, (path) => {
203
564
  received = path;
204
565
  });
205
- attachWindowDropFile(win, element);
206
-
207
- const event = new Event('drop') as Event & { dataTransfer: { files: { name: string }[] } };
208
- Object.defineProperty(event, 'dataTransfer', {
209
- value: { files: [{ name: 'test.png' }] },
210
- });
211
- element.dispatchEvent(event);
566
+ attachWindowDropFile(host, win, createInputTarget());
567
+ host.input.dropFile.emit('test.png');
212
568
 
213
569
  expect(received).toBe('test.png');
214
570
  });
571
+
572
+ it('releases the origin provider before attaching a replacement provider', () => {
573
+ const origin = createTestHost();
574
+ const replacement = createTestHost();
575
+ const win = createApplicationWindow();
576
+ const target = createInputTarget();
577
+
578
+ attachWindowDropFile(origin, win, target);
579
+ attachWindowDropFile(replacement, win, target);
580
+ detachWindowDropFile(win);
581
+
582
+ expect(origin.input.dropFile.calls).toEqual(['subscribe', 'release']);
583
+ expect(replacement.input.dropFile.calls).toEqual(['subscribe', 'release']);
584
+ });
215
585
  });
216
586
 
217
587
  describe('attachWindowFocus', () => {
218
- it('emits onFocusIn on focus', () => {
219
- const element = document.createElement('div');
588
+ it('emits onFocusIn from host focus ingress', () => {
220
589
  const win = createApplicationWindow();
221
590
  let called = false;
222
591
  connectSignal(win.onFocusIn, () => {
223
592
  called = true;
224
593
  });
225
- attachWindowFocus(win, element);
226
- element.dispatchEvent(new Event('focus'));
594
+ attachWindowFocus(host, win, createInputTarget());
595
+ host.input.focus.emitFocus();
227
596
  expect(called).toBe(true);
228
597
  });
229
598
 
230
- it('emits onFocusOut on blur', () => {
231
- const element = document.createElement('div');
599
+ it('emits onFocusOut from host blur ingress', () => {
232
600
  const win = createApplicationWindow();
233
601
  let called = false;
234
602
  connectSignal(win.onFocusOut, () => {
235
603
  called = true;
236
604
  });
237
- attachWindowFocus(win, element);
238
- element.dispatchEvent(new Event('blur'));
605
+ attachWindowFocus(host, win, createInputTarget());
606
+ host.input.focus.emitBlur();
239
607
  expect(called).toBe(true);
240
608
  });
609
+
610
+ it('releases the origin provider before attaching a replacement provider', () => {
611
+ const origin = createTestHost();
612
+ const replacement = createTestHost();
613
+ const win = createApplicationWindow();
614
+ const target = createInputTarget();
615
+
616
+ attachWindowFocus(origin, win, target);
617
+ attachWindowFocus(replacement, win, target);
618
+ detachWindowFocus(win);
619
+
620
+ expect(origin.input.focus.calls).toEqual(['subscribe', 'release']);
621
+ expect(replacement.input.focus.calls).toEqual(['subscribe', 'release']);
622
+ });
241
623
  });
242
624
 
243
625
  describe('attachWindowFullscreen', () => {
244
- it('emits onFullscreenChanged', () => {
626
+ it('mirrors subscribed fullscreen state and emits onFullscreenChanged', () => {
245
627
  const win = createApplicationWindow();
246
628
  let called = false;
247
629
  connectSignal(win.onFullscreenChanged, () => {
248
630
  called = true;
249
631
  });
250
- attachWindowFullscreen(win);
251
- document.dispatchEvent(new Event('fullscreenchange'));
632
+ attachWindowFullscreen(host, win);
633
+ host.ui.fullscreen.emit(true);
634
+ expect(win.fullscreen).toBe(true);
252
635
  expect(called).toBe(true);
636
+ expect(host.ui.fullscreen.calls).toEqual(['subscribe']);
253
637
  });
254
638
  });
255
639
 
256
640
  describe('attachWindowMove', () => {
257
- it('emits onMove and updates position when screenX/screenY changes', () => {
641
+ it('emits onMove and updates position when the host position changes', () => {
258
642
  const win = createApplicationWindow();
259
643
  win.x = 0;
260
644
  win.y = 0;
@@ -263,15 +647,12 @@ describe('attachWindowMove', () => {
263
647
  moved = true;
264
648
  });
265
649
 
266
- vi.stubGlobal('screenX', 100);
267
- vi.stubGlobal('screenY', 200);
268
- attachWindowMove(win);
269
- window.dispatchEvent(new Event('resize'));
650
+ attachWindowMove(host, win);
651
+ host.window.emitMove(100, 200);
270
652
 
271
653
  expect(moved).toBe(true);
272
654
  expect(win.x).toBe(100);
273
655
  expect(win.y).toBe(200);
274
- vi.unstubAllGlobals();
275
656
  });
276
657
 
277
658
  it('does not emit onMove when position has not changed', () => {
@@ -283,13 +664,10 @@ describe('attachWindowMove', () => {
283
664
  moved = true;
284
665
  });
285
666
 
286
- vi.stubGlobal('screenX', 50);
287
- vi.stubGlobal('screenY', 50);
288
- attachWindowMove(win);
289
- window.dispatchEvent(new Event('resize'));
667
+ attachWindowMove(host, win);
668
+ host.window.emitMove(50, 50);
290
669
 
291
670
  expect(moved).toBe(false);
292
- vi.unstubAllGlobals();
293
671
  });
294
672
  });
295
673
 
@@ -301,112 +679,125 @@ describe('attachWindowOrientation', () => {
301
679
  called = true;
302
680
  });
303
681
 
304
- let capturedHandler: (() => void) | null = null;
305
- Object.defineProperty(screen, 'orientation', {
306
- value: {
307
- addEventListener: (_: string, fn: () => void) => {
308
- capturedHandler = fn;
309
- },
310
- removeEventListener: vi.fn(),
311
- },
312
- configurable: true,
313
- });
314
-
315
- attachWindowOrientation(win);
316
- capturedHandler!();
682
+ attachWindowOrientation(host, win);
683
+ host.window.emitOrientation();
317
684
  expect(called).toBe(true);
318
685
  });
319
686
  });
320
687
 
321
688
  describe('attachWindowRenderContext', () => {
322
- it('emits onRenderContextLost on webglcontextlost', () => {
323
- const canvas = document.createElement('canvas');
689
+ it('emits onRenderContextLost from host render-context ingress', () => {
324
690
  const win = createApplicationWindow();
325
691
  let called = false;
326
692
  connectSignal(win.onRenderContextLost, () => {
327
693
  called = true;
328
694
  });
329
- attachWindowRenderContext(win, canvas);
330
- canvas.dispatchEvent(new Event('webglcontextlost'));
695
+ attachWindowRenderContext(host, win, createInputTarget());
696
+ host.graphics.renderContext.emitLost();
331
697
  expect(called).toBe(true);
332
698
  });
333
699
 
334
- it('emits onRenderContextRestored on webglcontextrestored', () => {
335
- const canvas = document.createElement('canvas');
700
+ it('emits onRenderContextRestored from host render-context ingress', () => {
336
701
  const win = createApplicationWindow();
337
702
  let called = false;
338
703
  connectSignal(win.onRenderContextRestored, () => {
339
704
  called = true;
340
705
  });
341
- attachWindowRenderContext(win, canvas);
342
- canvas.dispatchEvent(new Event('webglcontextrestored'));
706
+ attachWindowRenderContext(host, win, createInputTarget());
707
+ host.graphics.renderContext.emitRestored();
343
708
  expect(called).toBe(true);
344
709
  });
710
+
711
+ it('releases the origin provider before attaching a replacement provider', () => {
712
+ const origin = createTestHost();
713
+ const replacement = createTestHost();
714
+ const win = createApplicationWindow();
715
+ const target = createInputTarget();
716
+
717
+ attachWindowRenderContext(origin, win, target);
718
+ attachWindowRenderContext(replacement, win, target);
719
+ detachWindowRenderContext(win);
720
+
721
+ expect(origin.graphics.renderContext.calls).toEqual(['subscribe', 'release']);
722
+ expect(replacement.graphics.renderContext.calls).toEqual(['subscribe', 'release']);
723
+ });
345
724
  });
346
725
 
347
726
  describe('attachWindowRenderState', () => {
348
- it('sizes the canvas backing store and writes the device transform from the window', () => {
727
+ it('sizes the opaque render surface and writes the device transform from the window', () => {
349
728
  const win = createApplicationWindow();
350
729
  win.width = 800;
351
730
  win.height = 600;
352
731
  win.devicePixelRatio = 2;
353
- const canvas = document.createElement('canvas');
354
732
  const state = makeRenderState();
355
- attachWindowRenderState(win, state, canvas);
356
- expect(canvas.width).toBe(1600);
357
- expect(canvas.height).toBe(1200);
733
+ attachWindowRenderState(host, win, state, createInputTarget());
734
+ expect(host.graphics.renderSurface.calls).toEqual(['resize:1600,1200']);
358
735
  expect(state.renderTransform2D?.a).toBe(2);
359
736
  expect(state.renderTransform2D?.d).toBe(2);
360
737
  });
361
738
 
362
- it('reapplies the backing size and transform on window resize', () => {
739
+ it('keeps onResize core-owned while reapplying the backing size and transform', () => {
363
740
  const win = createApplicationWindow();
364
741
  win.width = 800;
365
742
  win.height = 600;
366
743
  win.devicePixelRatio = 1;
367
- const canvas = document.createElement('canvas');
368
744
  const state = makeRenderState();
369
- attachWindowRenderState(win, state, canvas);
745
+ attachWindowRenderState(host, win, state, createInputTarget());
370
746
  win.width = 400;
371
747
  win.devicePixelRatio = 2;
372
748
  emitSignal(win.onResize);
373
- expect(canvas.width).toBe(800);
749
+ expect(host.graphics.renderSurface.calls).toEqual(['resize:800,600', 'resize:800,1200']);
374
750
  expect(state.renderTransform2D?.a).toBe(2);
375
751
  });
376
- });
377
752
 
378
- describe('attachWindowResize', () => {
379
- let resizeCallback: ResizeObserverCallback;
380
- let disconnectFn: ReturnType<typeof vi.fn>;
753
+ it('pins resize commands to the provider captured by the core signal attachment', () => {
754
+ const origin = createTestHost();
755
+ const active = createTestHost();
756
+ const win = createApplicationWindow();
757
+ win.width = 320;
758
+ win.height = 200;
759
+ const state = makeRenderState();
381
760
 
382
- beforeEach(() => {
383
- disconnectFn = vi.fn();
384
- vi.stubGlobal(
385
- 'ResizeObserver',
386
- class {
387
- constructor(cb: ResizeObserverCallback) {
388
- resizeCallback = cb;
389
- }
390
- observe() {}
391
- disconnect = disconnectFn;
392
- },
393
- );
761
+ attachWindowRenderState(origin, win, state, createInputTarget());
762
+ host = active;
763
+ win.width = 640;
764
+ emitSignal(win.onResize);
765
+
766
+ expect(origin.graphics.renderSurface.calls).toEqual(['resize:320,200', 'resize:640,200']);
767
+ expect(active.graphics.renderSurface.calls).toEqual([]);
394
768
  });
395
769
 
396
- afterEach(() => {
397
- vi.unstubAllGlobals();
770
+ it('composes the five retained seams as explicit trait intersections', async () => {
771
+ const targetHost = createTestHost();
772
+ const composedHost: WindowTargetHost = targetHost;
773
+ const target = createInputTarget();
774
+ const win = createApplicationWindow();
775
+
776
+ attachWindowDropFile(composedHost, win, target);
777
+ attachWindowFocus(composedHost, win, target);
778
+ attachWindowRenderContext(composedHost, win, target);
779
+ attachWindowRenderState(composedHost, win, makeRenderState(), target);
780
+ await lockApplicationPointer(composedHost, target);
781
+
782
+ expect(targetHost.input.dropFile.calls).toEqual(['subscribe']);
783
+ expect(targetHost.input.focus.calls).toEqual(['subscribe']);
784
+ expect(targetHost.graphics.renderContext.calls).toEqual(['subscribe']);
785
+ expect(targetHost.graphics.renderSurface.calls).toEqual(['resize:0,0']);
786
+ expect(targetHost.input.pointerLock.calls).toEqual(['request']);
787
+ await exitApplicationPointerLock(targetHost);
398
788
  });
789
+ });
399
790
 
791
+ describe('attachWindowResize', () => {
400
792
  it('emits onResize and updates dimensions', () => {
401
- vi.stubGlobal('devicePixelRatio', 2);
402
793
  const win = createApplicationWindow();
403
794
  let called = false;
404
795
  connectSignal(win.onResize, () => {
405
796
  called = true;
406
797
  });
407
798
 
408
- attachWindowResize(win, document.createElement('div'));
409
- resizeCallback([{ contentRect: { width: 1280, height: 720 } } as ResizeObserverEntry], {} as ResizeObserver);
799
+ attachWindowResize(host, win, createWindowResizeTarget());
800
+ host.window.emitResize(1280, 720, 2);
410
801
 
411
802
  expect(called).toBe(true);
412
803
  expect(win.width).toBe(1280);
@@ -416,9 +807,15 @@ describe('attachWindowResize', () => {
416
807
 
417
808
  it('replaces a previous observer when called again', () => {
418
809
  const win = createApplicationWindow();
419
- attachWindowResize(win, document.createElement('div'));
420
- attachWindowResize(win, document.createElement('div'));
421
- expect(disconnectFn).toHaveBeenCalledTimes(1);
810
+ let resized = 0;
811
+ connectSignal(win.onResize, () => resized++);
812
+ attachWindowResize(host, win, createWindowResizeTarget());
813
+ attachWindowResize(host, win, createWindowResizeTarget());
814
+
815
+ host.window.emitResize(320, 240, 1);
816
+ expect(resized).toBe(1);
817
+ expect(win.width).toBe(320);
818
+ expect(win.height).toBe(240);
422
819
  });
423
820
  });
424
821
 
@@ -430,10 +827,8 @@ describe('attachWindowVisibility', () => {
430
827
  called = true;
431
828
  });
432
829
 
433
- attachWindowVisibility(win);
434
- Object.defineProperty(document, 'hidden', { value: true, configurable: true });
435
- document.dispatchEvent(new Event('visibilitychange'));
436
- Object.defineProperty(document, 'hidden', { value: false, configurable: true });
830
+ attachWindowVisibility(host, win);
831
+ host.window.emitVisibility(false);
437
832
 
438
833
  expect(called).toBe(true);
439
834
  });
@@ -445,9 +840,8 @@ describe('attachWindowVisibility', () => {
445
840
  called = true;
446
841
  });
447
842
 
448
- attachWindowVisibility(win);
449
- Object.defineProperty(document, 'hidden', { value: false, configurable: true });
450
- document.dispatchEvent(new Event('visibilitychange'));
843
+ attachWindowVisibility(host, win);
844
+ host.window.emitVisibility(true);
451
845
 
452
846
  expect(called).toBe(true);
453
847
  });
@@ -456,8 +850,8 @@ describe('attachWindowVisibility', () => {
456
850
  describe('centerWindow', () => {
457
851
  it('delegates to the backend', () => {
458
852
  const backend = recordingWindowBackend();
459
- setWindowBackend(backend);
460
- centerWindow(createApplicationWindow());
853
+ host = createTestHost(backend);
854
+ centerWindow(host, createApplicationWindow());
461
855
  expect(backend.calls).toContain('center');
462
856
  });
463
857
  });
@@ -465,25 +859,37 @@ describe('centerWindow', () => {
465
859
  describe('closeWindow', () => {
466
860
  it('closes and emits onClose when not vetoed', () => {
467
861
  const backend = recordingWindowBackend();
468
- setWindowBackend(backend);
862
+ host = createTestHost(backend);
469
863
  const win = createApplicationWindow();
470
864
  let closed = false;
471
865
  connectSignal(win.onClose, () => {
472
866
  closed = true;
473
867
  });
474
- expect(closeWindow(win)).toBe(true);
868
+ expect(closeWindow(host, win)).toBe(true);
475
869
  expect(backend.calls).toContain('close');
476
870
  expect(closed).toBe(true);
477
871
  });
478
872
 
479
873
  it('aborts and returns false when a listener vetoes', () => {
480
874
  const backend = recordingWindowBackend();
481
- setWindowBackend(backend);
875
+ host = createTestHost(backend);
482
876
  const win = createApplicationWindow();
483
877
  connectSignal(win.onCloseRequest, () => cancelSignal(win.onCloseRequest));
484
- expect(closeWindow(win)).toBe(false);
878
+ expect(closeWindow(host, win)).toBe(false);
485
879
  expect(backend.calls).not.toContain('close');
486
880
  });
881
+
882
+ it('closes terminal state once when called repeatedly', () => {
883
+ const win = createApplicationWindow();
884
+ let closed = 0;
885
+ connectSignal(win.onClose, () => closed++);
886
+
887
+ expect(closeWindow(host, win)).toBe(true);
888
+ expect(closeWindow(host, win)).toBe(true);
889
+
890
+ expect(closed).toBe(1);
891
+ expect(host.window.calls).toEqual(['close']);
892
+ });
487
893
  });
488
894
 
489
895
  describe('computeWindowDeviceTransform', () => {
@@ -521,6 +927,7 @@ describe('computeWindowDeviceTransform', () => {
521
927
  describe('createApplicationWindow', () => {
522
928
  it('returns all signals with no side effects', () => {
523
929
  const win = createApplicationWindow();
930
+ expect(EntityRuntimeKey in win).toBe(true);
524
931
  expect(win.onActivate).toBeDefined();
525
932
  expect(win.onClose).toBeDefined();
526
933
  expect(win.onDeactivate).toBeDefined();
@@ -541,7 +948,7 @@ describe('createApplicationWindow', () => {
541
948
  connectSignal(win.onFullscreenChanged, () => {
542
949
  called = true;
543
950
  });
544
- document.dispatchEvent(new Event('fullscreenchange'));
951
+ host.ui.fullscreen.emit(true);
545
952
  expect(called).toBe(false);
546
953
  });
547
954
 
@@ -560,44 +967,41 @@ describe('detachWindowClose', () => {
560
967
  connectSignal(win.onClose, () => {
561
968
  closed = true;
562
969
  });
563
- attachWindowClose(win);
970
+ attachWindowClose(host, win);
564
971
  detachWindowClose(win);
565
- window.dispatchEvent(new Event('pagehide'));
972
+ host.window.emitClosed();
566
973
  expect(closed).toBe(false);
567
974
  });
568
975
  });
569
976
 
570
977
  describe('detachWindowDropFile', () => {
571
- it('removes listeners', () => {
572
- const element = document.createElement('div');
978
+ it('releases the host subscription', () => {
573
979
  const win = createApplicationWindow();
574
980
  let called = false;
575
981
  connectSignal(win.onDropFile, () => {
576
982
  called = true;
577
983
  });
578
- attachWindowDropFile(win, element);
984
+ attachWindowDropFile(host, win, createInputTarget());
579
985
  detachWindowDropFile(win);
580
-
581
- const event = new Event('drop');
582
- Object.defineProperty(event, 'dataTransfer', { value: { files: [] } });
583
- element.dispatchEvent(event);
986
+ host.input.dropFile.emit('ignored.txt');
584
987
 
585
988
  expect(called).toBe(false);
989
+ expect(host.input.dropFile.calls).toEqual(['subscribe', 'release']);
586
990
  });
587
991
  });
588
992
 
589
993
  describe('detachWindowFocus', () => {
590
- it('removes both listeners', () => {
591
- const element = document.createElement('div');
994
+ it('releases both host event paths', () => {
592
995
  const win = createApplicationWindow();
593
996
  let called = false;
594
997
  connectSignal(win.onFocusIn, () => {
595
998
  called = true;
596
999
  });
597
- attachWindowFocus(win, element);
1000
+ attachWindowFocus(host, win, createInputTarget());
598
1001
  detachWindowFocus(win);
599
- element.dispatchEvent(new Event('focus'));
1002
+ host.input.focus.emitFocus();
600
1003
  expect(called).toBe(false);
1004
+ expect(host.input.focus.calls).toEqual(['subscribe', 'release']);
601
1005
  });
602
1006
  });
603
1007
 
@@ -608,10 +1012,11 @@ describe('detachWindowFullscreen', () => {
608
1012
  connectSignal(win.onFullscreenChanged, () => {
609
1013
  called = true;
610
1014
  });
611
- attachWindowFullscreen(win);
1015
+ attachWindowFullscreen(host, win);
612
1016
  detachWindowFullscreen(win);
613
- document.dispatchEvent(new Event('fullscreenchange'));
1017
+ host.ui.fullscreen.emit(true);
614
1018
  expect(called).toBe(false);
1019
+ expect(host.ui.fullscreen.calls).toEqual(['subscribe', 'unsubscribe']);
615
1020
  });
616
1021
  });
617
1022
 
@@ -624,44 +1029,41 @@ describe('detachWindowMove', () => {
624
1029
  moved = true;
625
1030
  });
626
1031
 
627
- vi.stubGlobal('screenX', 100);
628
- vi.stubGlobal('screenY', 100);
629
- attachWindowMove(win);
1032
+ attachWindowMove(host, win);
630
1033
  detachWindowMove(win);
631
- window.dispatchEvent(new Event('resize'));
1034
+ host.window.emitMove(100, 100);
632
1035
 
633
1036
  expect(moved).toBe(false);
634
- vi.unstubAllGlobals();
635
1037
  });
636
1038
  });
637
1039
 
638
1040
  describe('detachWindowOrientation', () => {
639
1041
  it('removes the listener', () => {
640
- const removeListener = vi.fn();
641
- Object.defineProperty(screen, 'orientation', {
642
- value: { addEventListener: vi.fn(), removeEventListener: removeListener },
643
- configurable: true,
644
- });
645
-
646
1042
  const win = createApplicationWindow();
647
- attachWindowOrientation(win);
1043
+ let called = false;
1044
+ connectSignal(win.onOrientationChanged, () => {
1045
+ called = true;
1046
+ });
1047
+ attachWindowOrientation(host, win);
648
1048
  detachWindowOrientation(win);
649
- expect(removeListener).toHaveBeenCalled();
1049
+ host.window.emitOrientation();
1050
+
1051
+ expect(called).toBe(false);
650
1052
  });
651
1053
  });
652
1054
 
653
1055
  describe('detachWindowRenderContext', () => {
654
- it('removes render context listeners', () => {
655
- const canvas = document.createElement('canvas');
1056
+ it('releases render-context ingress', () => {
656
1057
  const win = createApplicationWindow();
657
1058
  let called = false;
658
1059
  connectSignal(win.onRenderContextLost, () => {
659
1060
  called = true;
660
1061
  });
661
- attachWindowRenderContext(win, canvas);
1062
+ attachWindowRenderContext(host, win, createInputTarget());
662
1063
  detachWindowRenderContext(win);
663
- canvas.dispatchEvent(new Event('webglcontextlost'));
1064
+ host.graphics.renderContext.emitLost();
664
1065
  expect(called).toBe(false);
1066
+ expect(host.graphics.renderContext.calls).toEqual(['subscribe', 'release']);
665
1067
  });
666
1068
  });
667
1069
 
@@ -671,34 +1073,24 @@ describe('detachWindowRenderState', () => {
671
1073
  win.width = 800;
672
1074
  win.height = 600;
673
1075
  win.devicePixelRatio = 1;
674
- const canvas = document.createElement('canvas');
675
1076
  const state = makeRenderState();
676
- attachWindowRenderState(win, state, canvas);
1077
+ attachWindowRenderState(host, win, state, createInputTarget());
677
1078
  detachWindowRenderState(win);
678
1079
  win.width = 400;
679
1080
  emitSignal(win.onResize);
680
- expect(canvas.width).toBe(800);
1081
+ expect(host.graphics.renderSurface.calls).toEqual(['resize:800,600']);
681
1082
  });
682
1083
  });
683
1084
 
684
1085
  describe('detachWindowResize', () => {
685
1086
  it('disconnects the observer', () => {
686
- const disconnectFn = vi.fn();
687
- vi.stubGlobal(
688
- 'ResizeObserver',
689
- class {
690
- constructor() {}
691
- observe() {}
692
- disconnect = disconnectFn;
693
- },
694
- );
695
-
696
- const win = createApplicationWindow();
697
- attachWindowResize(win, document.createElement('div'));
1087
+ const win = createApplicationWindow();
1088
+ attachWindowResize(host, win, createWindowResizeTarget());
698
1089
  detachWindowResize(win);
1090
+ host.window.emitResize(640, 480, 2);
699
1091
 
700
- expect(disconnectFn).toHaveBeenCalled();
701
- vi.unstubAllGlobals();
1092
+ expect(win.width).toBe(0);
1093
+ expect(win.height).toBe(0);
702
1094
  });
703
1095
  });
704
1096
 
@@ -710,11 +1102,9 @@ describe('detachWindowVisibility', () => {
710
1102
  called = true;
711
1103
  });
712
1104
 
713
- attachWindowVisibility(win);
1105
+ attachWindowVisibility(host, win);
714
1106
  detachWindowVisibility(win);
715
- Object.defineProperty(document, 'hidden', { value: true, configurable: true });
716
- document.dispatchEvent(new Event('visibilitychange'));
717
- Object.defineProperty(document, 'hidden', { value: false, configurable: true });
1107
+ host.window.emitVisibility(false);
718
1108
 
719
1109
  expect(called).toBe(false);
720
1110
  });
@@ -722,72 +1112,84 @@ describe('detachWindowVisibility', () => {
722
1112
 
723
1113
  describe('disposeApplicationWindow', () => {
724
1114
  it('runs all teardown so attached observers stop firing', () => {
725
- const disconnectFn = vi.fn();
726
- vi.stubGlobal(
727
- 'ResizeObserver',
728
- class {
729
- constructor() {}
730
- observe() {}
731
- disconnect = disconnectFn;
732
- },
733
- );
734
-
735
1115
  const win = createApplicationWindow();
736
- attachWindowResize(win, document.createElement('div'));
737
- attachWindowFullscreen(win);
1116
+ attachWindowResize(host, win, createWindowResizeTarget());
1117
+ attachWindowFullscreen(host, win);
738
1118
 
739
1119
  disposeApplicationWindow(win);
740
1120
 
741
- expect(disconnectFn).toHaveBeenCalled();
1121
+ host.window.emitResize(640, 480, 2);
1122
+ expect(win.width).toBe(0);
742
1123
  let called = false;
743
1124
  connectSignal(win.onFullscreenChanged, () => {
744
1125
  called = true;
745
1126
  });
746
- document.dispatchEvent(new Event('fullscreenchange'));
1127
+ host.ui.fullscreen.emit(true);
747
1128
  expect(called).toBe(false);
748
-
749
- vi.unstubAllGlobals();
1129
+ expect(host.ui.fullscreen.calls).toEqual(['subscribe', 'unsubscribe']);
750
1130
  });
751
1131
  });
752
1132
 
753
1133
  describe('exitApplicationFullscreen', () => {
754
- it('calls document.exitFullscreen', async () => {
755
- const mock = vi.fn().mockResolvedValue(undefined);
756
- Object.defineProperty(document, 'exitFullscreen', { value: mock, configurable: true });
757
- await exitApplicationFullscreen();
758
- expect(mock).toHaveBeenCalled();
1134
+ it('delegates to the fullscreen capability', async () => {
1135
+ await expect(exitApplicationFullscreen(host)).resolves.toBe(true);
1136
+ expect(host.ui.fullscreen.calls).toEqual(['exit']);
759
1137
  });
760
1138
  });
761
1139
 
762
1140
  describe('exitApplicationPointerLock', () => {
763
- it('calls document.exitPointerLock when available', async () => {
764
- const mock = vi.fn();
765
- Object.defineProperty(document, 'exitPointerLock', { value: mock, configurable: true });
766
- await exitApplicationPointerLock();
767
- expect(mock).toHaveBeenCalled();
768
- });
769
- });
1141
+ it('delegates to the input pointer-lock command capability', async () => {
1142
+ await expect(exitApplicationPointerLock(host)).resolves.toEqual({ reason: 'ok' });
1143
+ expect(host.input.pointerLock.calls).toEqual(['exit']);
1144
+ });
1145
+
1146
+ it.each(['api-unavailable', 'operation-failed'] satisfies InputPointerLockExitOutcome['reason'][])(
1147
+ 'retains the request origin after %s so exit can be retried',
1148
+ async (reason) => {
1149
+ const origin = createTestHost();
1150
+ const active = createTestHost();
1151
+ let attempts = 0;
1152
+ origin.input.pointerLock.exit = async () => {
1153
+ attempts++;
1154
+ origin.input.pointerLock.calls.push(`exit:${attempts}`);
1155
+ return attempts === 1 ? { reason } : { reason: 'ok' };
1156
+ };
1157
+
1158
+ await lockApplicationPointer(origin, createInputTarget());
1159
+ await expect(exitApplicationPointerLock(active)).resolves.toEqual({ reason });
1160
+ await expect(exitApplicationPointerLock(active)).resolves.toEqual({ reason: 'ok' });
1161
+
1162
+ expect(origin.input.pointerLock.calls).toEqual(['request', 'exit:1', 'exit:2']);
1163
+ expect(active.input.pointerLock.calls).toEqual([]);
1164
+ },
1165
+ );
770
1166
 
771
- describe('explainWindowBackend', () => {
772
- it('reports host-not-enabled when no backend is installed', () => {
773
- const explanation = explainWindowBackend();
774
- expect(explanation.layer).toBe('host-not-enabled');
775
- expect(explanation.conflict).toBe(false);
776
- });
1167
+ it('keeps backend defects visible and retains the origin for retry', async () => {
1168
+ const origin = createTestHost();
1169
+ const active = createTestHost();
1170
+ let attempts = 0;
1171
+ origin.input.pointerLock.exit = async () => {
1172
+ attempts++;
1173
+ origin.input.pointerLock.calls.push(`exit:${attempts}`);
1174
+ if (attempts === 1) throw new Error('busy');
1175
+ return { reason: 'ok' };
1176
+ };
1177
+
1178
+ await lockApplicationPointer(origin, createInputTarget());
1179
+ await expect(exitApplicationPointerLock(active)).rejects.toThrow('busy');
1180
+ await expect(exitApplicationPointerLock(active)).resolves.toEqual({ reason: 'ok' });
777
1181
 
778
- it('reports custom layer when a custom backend is set', () => {
779
- setWindowBackend(recordingWindowBackend());
780
- const explanation = explainWindowBackend();
781
- expect(explanation.layer).toBe('custom');
1182
+ expect(origin.input.pointerLock.calls).toEqual(['request', 'exit:1', 'exit:2']);
1183
+ expect(active.input.pointerLock.calls).toEqual([]);
782
1184
  });
783
1185
  });
784
1186
 
785
1187
  describe('flashWindowFrame', () => {
786
1188
  it('delegates to the backend', () => {
787
1189
  const backend = recordingWindowBackend();
788
- setWindowBackend(backend);
1190
+ host = createTestHost(backend);
789
1191
  const win = createApplicationWindow();
790
- flashWindowFrame(win);
1192
+ flashWindowFrame(host, win);
791
1193
  expect(backend.calls).toContain('flashWindowFrame');
792
1194
  });
793
1195
  });
@@ -795,96 +1197,122 @@ describe('flashWindowFrame', () => {
795
1197
  describe('focusWindow', () => {
796
1198
  it('marks focused and delegates to the backend', () => {
797
1199
  const backend = recordingWindowBackend();
798
- setWindowBackend(backend);
1200
+ host = createTestHost(backend);
799
1201
  const win = createApplicationWindow();
800
- focusWindow(win);
1202
+ focusWindow(host, win);
801
1203
  expect(win.focused).toBe(true);
802
1204
  expect(backend.calls).toContain('focus');
803
1205
  });
804
1206
  });
805
1207
 
806
- describe('getWindowBackend', () => {
807
- it('falls back to a sentinel backend', () => {
808
- expect(getWindowBackend()).not.toBeNull();
809
- });
810
-
811
- it('returns the custom backend when set', () => {
812
- const backend = recordingWindowBackend();
813
- setWindowBackend(backend);
814
- expect(getWindowBackend()).toBe(backend);
815
- });
816
- });
817
-
818
1208
  describe('getWindowBounds', () => {
819
1209
  it('fills the out bounds from the backend', () => {
820
- setWindowBackend(recordingWindowBackend());
1210
+ host = createTestHost();
821
1211
  const out = { x: 0, y: 0, width: 0, height: 0 };
822
- expect(getWindowBounds(createApplicationWindow(), out)).toBe(out);
1212
+ expect(getWindowBounds(host, createApplicationWindow(), out)).toBe(out);
823
1213
  expect(out.width).toBe(3);
824
- });
825
- });
826
-
827
- describe('getWindowDisplay', () => {
828
- it('returns -1 on web (no multi-monitor API)', () => {
829
- const win = createApplicationWindow();
830
- expect(getWindowDisplay(win)).toBe(-1);
1214
+ expect(host.window.calls).toEqual(['getBounds']);
831
1215
  });
832
1216
  });
833
1217
 
834
1218
  describe('hideWindow', () => {
835
1219
  it('marks not visible and delegates to the backend', () => {
836
1220
  const backend = recordingWindowBackend();
837
- setWindowBackend(backend);
1221
+ host = createTestHost(backend);
838
1222
  const win = createApplicationWindow();
839
- hideWindow(win);
1223
+ hideWindow(host, win);
840
1224
  expect(win.visible).toBe(false);
841
1225
  expect(backend.calls).toContain('hide');
842
1226
  });
843
1227
  });
844
1228
 
845
- describe('installWindowHostBackend', () => {
846
- it('installs the host backend on first call', () => {
847
- const backend = recordingWindowBackend();
848
- installWindowHostBackend(backend);
849
- expect(getWindowBackend()).toBe(backend);
850
- });
1229
+ describe('lockApplicationPointer', () => {
1230
+ it('passes the opaque target to the pointer-lock command capability', async () => {
1231
+ const target = createInputTarget();
1232
+ await expect(lockApplicationPointer(host, target)).resolves.toEqual({ reason: 'ok' });
1233
+ expect(host.input.pointerLock.calls).toEqual(['request']);
1234
+ await exitApplicationPointerLock(host);
1235
+ });
1236
+
1237
+ it('exits through the successful request origin after a different host becomes active', async () => {
1238
+ const origin = createTestHost();
1239
+ const active = createTestHost();
1240
+
1241
+ await lockApplicationPointer(origin, createInputTarget());
1242
+ await exitApplicationPointerLock(active);
1243
+
1244
+ expect(origin.input.pointerLock.calls).toEqual(['request', 'exit']);
1245
+ expect(active.input.pointerLock.calls).toEqual([]);
1246
+ });
1247
+
1248
+ it.each([
1249
+ 'api-unavailable',
1250
+ 'denied',
1251
+ 'operation-failed',
1252
+ 'target-not-found',
1253
+ ] satisfies InputPointerLockRequestOutcome['reason'][])(
1254
+ 'does not replace a successful origin after %s',
1255
+ async (reason) => {
1256
+ const origin = createTestHost();
1257
+ const declined = createTestHost();
1258
+ const active = createTestHost();
1259
+ declined.input.pointerLock.request = async () => {
1260
+ declined.input.pointerLock.calls.push(`request:${reason}`);
1261
+ return { reason };
1262
+ };
1263
+
1264
+ await lockApplicationPointer(origin, createInputTarget());
1265
+ await expect(lockApplicationPointer(declined, createInputTarget())).resolves.toEqual({ reason });
1266
+ await exitApplicationPointerLock(active);
1267
+
1268
+ expect(origin.input.pointerLock.calls).toEqual(['request', 'exit']);
1269
+ expect(declined.input.pointerLock.calls).toEqual([`request:${reason}`]);
1270
+ expect(active.input.pointerLock.calls).toEqual([]);
1271
+ },
1272
+ );
851
1273
 
852
- it('ignores a second different backend and sets conflict', () => {
853
- const first = recordingWindowBackend();
854
- const second = recordingWindowBackend();
855
- installWindowHostBackend(first);
856
- installWindowHostBackend(second);
857
- expect(getWindowBackend()).toBe(first);
858
- expect(explainWindowBackend().conflict).toBe(true);
859
- });
860
- });
1274
+ it('keeps backend defects visible without replacing a successful origin', async () => {
1275
+ const origin = createTestHost();
1276
+ const rejected = createTestHost();
1277
+ const active = createTestHost();
1278
+ rejected.input.pointerLock.request = async () => {
1279
+ rejected.input.pointerLock.calls.push('request:rejected');
1280
+ throw new Error('defect');
1281
+ };
861
1282
 
862
- describe('lockApplicationPointer', () => {
863
- it('calls requestPointerLock on the element', async () => {
864
- const element = document.createElement('div');
865
- const mock = vi.fn().mockResolvedValue(undefined);
866
- element.requestPointerLock = mock;
867
- await lockApplicationPointer(element);
868
- expect(mock).toHaveBeenCalled();
1283
+ await lockApplicationPointer(origin, createInputTarget());
1284
+ await expect(lockApplicationPointer(rejected, createInputTarget())).rejects.toThrow('defect');
1285
+ await exitApplicationPointerLock(active);
1286
+
1287
+ expect(origin.input.pointerLock.calls).toEqual(['request', 'exit']);
1288
+ expect(rejected.input.pointerLock.calls).toEqual(['request:rejected']);
1289
+ expect(active.input.pointerLock.calls).toEqual([]);
869
1290
  });
870
1291
 
871
- it('resolves without throwing when requestPointerLock is unavailable', async () => {
872
- const element = document.createElement('div');
873
- // Remove requestPointerLock to simulate an older browser.
874
- Object.defineProperty(element, 'requestPointerLock', { value: undefined, configurable: true });
875
- await expect(lockApplicationPointer(element)).resolves.toBeUndefined();
1292
+ it('replaces the request origin only after a later success', async () => {
1293
+ const origin = createTestHost();
1294
+ const replacement = createTestHost();
1295
+ const active = createTestHost();
1296
+
1297
+ await lockApplicationPointer(origin, createInputTarget());
1298
+ await lockApplicationPointer(replacement, createInputTarget());
1299
+ await exitApplicationPointerLock(active);
1300
+
1301
+ expect(origin.input.pointerLock.calls).toEqual(['request']);
1302
+ expect(replacement.input.pointerLock.calls).toEqual(['request', 'exit']);
1303
+ expect(active.input.pointerLock.calls).toEqual([]);
876
1304
  });
877
1305
  });
878
1306
 
879
1307
  describe('maximizeWindow', () => {
880
1308
  it('sets maximized and emits onMaximize once', () => {
881
1309
  const backend = recordingWindowBackend();
882
- setWindowBackend(backend);
1310
+ host = createTestHost(backend);
883
1311
  const win = createApplicationWindow();
884
1312
  let count = 0;
885
1313
  connectSignal(win.onMaximize, () => count++);
886
- maximizeWindow(win);
887
- maximizeWindow(win);
1314
+ maximizeWindow(host, win);
1315
+ maximizeWindow(host, win);
888
1316
  expect(win.maximized).toBe(true);
889
1317
  expect(count).toBe(1);
890
1318
  expect(backend.calls).toContain('maximize');
@@ -894,40 +1322,62 @@ describe('maximizeWindow', () => {
894
1322
  describe('minimizeWindow', () => {
895
1323
  it('sets minimized and emits onMinimize', () => {
896
1324
  const backend = recordingWindowBackend();
897
- setWindowBackend(backend);
1325
+ host = createTestHost(backend);
898
1326
  const win = createApplicationWindow();
899
1327
  let called = false;
900
1328
  connectSignal(win.onMinimize, () => {
901
1329
  called = true;
902
1330
  });
903
- minimizeWindow(win);
1331
+ minimizeWindow(host, win);
904
1332
  expect(win.minimized).toBe(true);
905
1333
  expect(called).toBe(true);
906
1334
  });
907
1335
  });
908
1336
 
909
- describe('observeWindowHostResult', () => {
910
- it('records the operation and viability', () => {
911
- installWindowHostBackend(recordingWindowBackend());
912
- observeWindowHostResult('setTitle', true);
913
- const explanation = explainWindowBackend();
914
- expect(explanation.operation).toBe('setTitle');
915
- expect(explanation.viability).toBe('available');
1337
+ describe('notifyWindowClosed', () => {
1338
+ it('emits once before draining application observers, then remains idempotent', () => {
1339
+ const win = createApplicationWindow();
1340
+ const order: string[] = [];
1341
+ let forwarded = 0;
1342
+ connectSignal(win.onFullscreenChanged, () => forwarded++);
1343
+ attachWindowFullscreen(host, win);
1344
+ connectSignal(win.onClose, () => {
1345
+ order.push('close');
1346
+ host.ui.fullscreen.emit(true);
1347
+ order.push(`observer:${forwarded}`);
1348
+ });
1349
+
1350
+ notifyWindowClosed(win);
1351
+ host.ui.fullscreen.emit(true);
1352
+ notifyWindowClosed(win);
1353
+
1354
+ expect(order).toEqual(['close', 'observer:1']);
1355
+ expect(forwarded).toBe(1);
916
1356
  });
917
1357
 
918
- it('records runtime-api-unavailable on failure', () => {
919
- installWindowHostBackend(recordingWindowBackend());
920
- observeWindowHostResult('setPosition', false);
921
- expect(explainWindowBackend().viability).toBe('runtime-api-unavailable');
1358
+ it('still drains application observers when a terminal listener throws', () => {
1359
+ const win = createApplicationWindow();
1360
+ let forwarded = 0;
1361
+ connectSignal(win.onFullscreenChanged, () => forwarded++);
1362
+ attachWindowFullscreen(host, win);
1363
+ connectSignal(win.onClose, () => {
1364
+ throw new Error('close listener failed');
1365
+ });
1366
+
1367
+ expect(() => notifyWindowClosed(win)).toThrow('close listener failed');
1368
+ host.ui.fullscreen.emit(true);
1369
+ expect(() => notifyWindowClosed(win)).not.toThrow();
1370
+
1371
+ expect(forwarded).toBe(0);
922
1372
  });
923
1373
  });
924
1374
 
925
1375
  describe('openWindow', () => {
926
1376
  it('applies options to the entity and delegates to the backend', () => {
927
1377
  const backend = recordingWindowBackend();
928
- setWindowBackend(backend);
1378
+ host = createTestHost(backend);
929
1379
  const win = createApplicationWindow();
930
- expect(openWindow(win, { title: 'Game', width: 640, height: 480, alwaysOnTop: true })).toBe(true);
1380
+ expect(openWindow(host, win, { title: 'Game', width: 640, height: 480, alwaysOnTop: true })).toBe(true);
931
1381
  expect(win.title).toBe('Game');
932
1382
  expect(win.width).toBe(640);
933
1383
  expect(win.alwaysOnTop).toBe(true);
@@ -936,64 +1386,105 @@ describe('openWindow', () => {
936
1386
 
937
1387
  it('centers the window after open when center option is true', () => {
938
1388
  const backend = recordingWindowBackend();
939
- setWindowBackend(backend);
1389
+ host = createTestHost(backend);
940
1390
  const win = createApplicationWindow();
941
- openWindow(win, { title: 'Centered', center: true });
942
- expect(backend.calls).toContain('center');
1391
+ openWindow(host, win, { title: 'Centered', center: true });
1392
+ expect(backend.calls.filter((call) => call === 'center')).toHaveLength(1);
943
1393
  });
944
1394
 
945
1395
  it('does not center when center option is not set', () => {
946
1396
  const backend = recordingWindowBackend();
947
- setWindowBackend(backend);
1397
+ host = createTestHost(backend);
948
1398
  const win = createApplicationWindow();
949
- openWindow(win, { title: 'Normal' });
1399
+ openWindow(host, win, { title: 'Normal' });
950
1400
  expect(backend.calls).not.toContain('center');
951
1401
  });
1402
+
1403
+ it('re-arms terminal close and observer disposal after a successful reopen', () => {
1404
+ const backend = recordingWindowBackend();
1405
+ host = createTestHost(backend);
1406
+ const win = createApplicationWindow();
1407
+ let closed = 0;
1408
+ let forwarded = 0;
1409
+ connectSignal(win.onClose, () => closed++);
1410
+ connectSignal(win.onFullscreenChanged, () => forwarded++);
1411
+
1412
+ expect(openWindow(host, win)).toBe(true);
1413
+ attachWindowFullscreen(host, win);
1414
+ expect(closeWindow(host, win)).toBe(true);
1415
+ host.ui.fullscreen.emit(true);
1416
+ expect(forwarded).toBe(0);
1417
+
1418
+ expect(openWindow(host, win)).toBe(true);
1419
+ attachWindowFullscreen(host, win);
1420
+ host.ui.fullscreen.emit(true);
1421
+ expect(forwarded).toBe(1);
1422
+ expect(closeWindow(host, win)).toBe(true);
1423
+ host.ui.fullscreen.emit(true);
1424
+
1425
+ expect(closed).toBe(2);
1426
+ expect(forwarded).toBe(1);
1427
+ });
1428
+
1429
+ it('releases through the origin host after a different host becomes active', () => {
1430
+ const calls: string[] = [];
1431
+ const origin = createTestHost();
1432
+ origin.window.close = () => {
1433
+ calls.push('origin:close');
1434
+ };
1435
+ origin.window.open = () => {
1436
+ calls.push('origin:open');
1437
+ return true;
1438
+ };
1439
+ const active = createTestHost();
1440
+ active.window.close = () => {
1441
+ calls.push('active:close');
1442
+ };
1443
+ const win = createApplicationWindow();
1444
+ let closed = 0;
1445
+ connectSignal(win.onClose, () => closed++);
1446
+
1447
+ expect(openWindow(origin, win)).toBe(true);
1448
+ expect(closeWindow(active, win)).toBe(true);
1449
+
1450
+ expect(calls).toEqual(['origin:open', 'origin:close']);
1451
+ expect(closed).toBe(1);
1452
+ });
952
1453
  });
953
1454
 
954
1455
  describe('prepareElementForInput', () => {
955
- it('sets touch-action and user-select', () => {
956
- const element = document.createElement('div');
957
- prepareElementForInput(element);
958
- expect(element.style.touchAction).toBe('none');
959
- expect(element.style.userSelect).toBe('none');
960
- });
1456
+ it('passes the opaque target to the explicit input preparation capability', () => {
1457
+ const prepare = vi.fn();
1458
+ const backend = createEntity<{ prepare: typeof prepare }>({ prepare }) satisfies InputTargetBackend;
1459
+ const target: InputTargetHandle = createEntity({ __brand: 'InputTargetHandle' as const });
961
1460
 
962
- it('sets transform on canvas elements', () => {
963
- const canvas = document.createElement('canvas');
964
- prepareElementForInput(canvas);
965
- expect(canvas.style.transform).toBe('translateZ(0)');
966
- });
1461
+ prepareElementForInput({ input: { target: backend } }, target);
967
1462
 
968
- it('does not set transform on non-canvas elements', () => {
969
- const div = document.createElement('div');
970
- prepareElementForInput(div);
971
- expect(div.style.transform).toBe('');
1463
+ expect(prepare).toHaveBeenCalledOnce();
1464
+ expect(prepare).toHaveBeenCalledWith(target);
972
1465
  });
973
1466
  });
974
1467
 
975
1468
  describe('requestApplicationFullscreen', () => {
976
- it('calls requestFullscreen on the element', async () => {
977
- const element = document.createElement('div');
978
- const mock = vi.fn().mockResolvedValue(undefined);
979
- element.requestFullscreen = mock;
980
- await requestApplicationFullscreen(element);
981
- expect(mock).toHaveBeenCalled();
1469
+ it('passes the opaque target to the fullscreen capability', async () => {
1470
+ const target: FullscreenTargetHandle = { __brand: 'FullscreenTargetHandle' };
1471
+ await expect(requestApplicationFullscreen(host, target)).resolves.toBe(true);
1472
+ expect(host.ui.fullscreen.calls).toEqual(['request']);
982
1473
  });
983
1474
  });
984
1475
 
985
1476
  describe('requestWindowAttention', () => {
986
1477
  it('delegates to the backend', () => {
987
1478
  const backend = recordingWindowBackend();
988
- setWindowBackend(backend);
989
- requestWindowAttention(createApplicationWindow(), true);
1479
+ host = createTestHost(backend);
1480
+ requestWindowAttention(host, createApplicationWindow(), true);
990
1481
  expect(backend.calls).toContain('requestAttention:true');
991
1482
  });
992
1483
  });
993
1484
 
994
1485
  describe('requestWindowClose', () => {
995
1486
  it('returns true when not vetoed', () => {
996
- setWindowBackend(recordingWindowBackend());
1487
+ host = createTestHost();
997
1488
  expect(requestWindowClose(createApplicationWindow())).toBe(true);
998
1489
  });
999
1490
 
@@ -1004,26 +1495,17 @@ describe('requestWindowClose', () => {
1004
1495
  });
1005
1496
  });
1006
1497
 
1007
- describe('resetWindowBackendForTest', () => {
1008
- it('clears all backend slots', () => {
1009
- installWindowHostBackend(recordingWindowBackend());
1010
- setWindowBackend(recordingWindowBackend());
1011
- resetWindowBackendForTest();
1012
- expect(explainWindowBackend().layer).toBe('host-not-enabled');
1013
- });
1014
- });
1015
-
1016
1498
  describe('restoreWindow', () => {
1017
1499
  it('clears minimized/maximized and emits onRestore', () => {
1018
1500
  const backend = recordingWindowBackend();
1019
- setWindowBackend(backend);
1501
+ host = createTestHost(backend);
1020
1502
  const win = createApplicationWindow();
1021
- maximizeWindow(win);
1503
+ maximizeWindow(host, win);
1022
1504
  let restored = false;
1023
1505
  connectSignal(win.onRestore, () => {
1024
1506
  restored = true;
1025
1507
  });
1026
- restoreWindow(win);
1508
+ restoreWindow(host, win);
1027
1509
  expect(win.maximized).toBe(false);
1028
1510
  expect(restored).toBe(true);
1029
1511
  expect(backend.calls).toContain('restore');
@@ -1033,28 +1515,20 @@ describe('restoreWindow', () => {
1033
1515
  describe('setWindowAlwaysOnTop', () => {
1034
1516
  it('sets state and delegates', () => {
1035
1517
  const backend = recordingWindowBackend();
1036
- setWindowBackend(backend);
1518
+ host = createTestHost(backend);
1037
1519
  const win = createApplicationWindow();
1038
- setWindowAlwaysOnTop(win, true);
1520
+ setWindowAlwaysOnTop(host, win, true);
1039
1521
  expect(win.alwaysOnTop).toBe(true);
1040
1522
  expect(backend.calls).toContain('setAlwaysOnTop:true');
1041
1523
  });
1042
1524
  });
1043
1525
 
1044
- describe('setWindowBackend', () => {
1045
- it('clears back to the sentinel when passed null', () => {
1046
- setWindowBackend(recordingWindowBackend());
1047
- setWindowBackend(null);
1048
- expect(getWindowBackend()).not.toBeNull();
1049
- });
1050
- });
1051
-
1052
1526
  describe('setWindowContentProtection', () => {
1053
1527
  it('delegates to the backend', () => {
1054
1528
  const backend = recordingWindowBackend();
1055
- setWindowBackend(backend);
1529
+ host = createTestHost(backend);
1056
1530
  const win = createApplicationWindow();
1057
- setWindowContentProtection(win, true);
1531
+ setWindowContentProtection(host, win, true);
1058
1532
  expect(backend.calls).toContain('setContentProtection:true');
1059
1533
  });
1060
1534
  });
@@ -1062,12 +1536,12 @@ describe('setWindowContentProtection', () => {
1062
1536
  describe('setWindowFullscreen', () => {
1063
1537
  it('sets state and emits onFullscreenChanged once', () => {
1064
1538
  const backend = recordingWindowBackend();
1065
- setWindowBackend(backend);
1539
+ host = createTestHost(backend);
1066
1540
  const win = createApplicationWindow();
1067
1541
  let count = 0;
1068
1542
  connectSignal(win.onFullscreenChanged, () => count++);
1069
- setWindowFullscreen(win, true);
1070
- setWindowFullscreen(win, true);
1543
+ setWindowFullscreen(host, win, true);
1544
+ setWindowFullscreen(host, win, true);
1071
1545
  expect(win.fullscreen).toBe(true);
1072
1546
  expect(count).toBe(1);
1073
1547
  });
@@ -1076,9 +1550,9 @@ describe('setWindowFullscreen', () => {
1076
1550
  describe('setWindowHasShadow', () => {
1077
1551
  it('delegates to the backend', () => {
1078
1552
  const backend = recordingWindowBackend();
1079
- setWindowBackend(backend);
1553
+ host = createTestHost(backend);
1080
1554
  const win = createApplicationWindow();
1081
- setWindowHasShadow(win, false);
1555
+ setWindowHasShadow(host, win, false);
1082
1556
  expect(backend.calls).toContain('setHasShadow:false');
1083
1557
  });
1084
1558
  });
@@ -1086,9 +1560,9 @@ describe('setWindowHasShadow', () => {
1086
1560
  describe('setWindowIcon', () => {
1087
1561
  it('sets the icon and delegates', () => {
1088
1562
  const backend = recordingWindowBackend();
1089
- setWindowBackend(backend);
1563
+ host = createTestHost(backend);
1090
1564
  const win = createApplicationWindow();
1091
- setWindowIcon(win, 'icon.png');
1565
+ setWindowIcon(host, win, 'icon.png');
1092
1566
  expect(win.icon).toBe('icon.png');
1093
1567
  expect(backend.calls).toContain('setIcon:icon.png');
1094
1568
  });
@@ -1097,9 +1571,9 @@ describe('setWindowIcon', () => {
1097
1571
  describe('setWindowMaximumSize', () => {
1098
1572
  it('sets constraints and delegates', () => {
1099
1573
  const backend = recordingWindowBackend();
1100
- setWindowBackend(backend);
1574
+ host = createTestHost(backend);
1101
1575
  const win = createApplicationWindow();
1102
- setWindowMaximumSize(win, 1920, 1080);
1576
+ setWindowMaximumSize(host, win, 1920, 1080);
1103
1577
  expect(win.maxWidth).toBe(1920);
1104
1578
  expect(win.maxHeight).toBe(1080);
1105
1579
  expect(backend.calls).toContain('setMaximumSize:1920,1080');
@@ -1109,8 +1583,8 @@ describe('setWindowMaximumSize', () => {
1109
1583
  describe('setWindowMenuBarVisible', () => {
1110
1584
  it('delegates to the backend', () => {
1111
1585
  const backend = recordingWindowBackend();
1112
- setWindowBackend(backend);
1113
- setWindowMenuBarVisible(createApplicationWindow(), false);
1586
+ host = createTestHost(backend);
1587
+ setWindowMenuBarVisible(host, createApplicationWindow(), false);
1114
1588
  expect(backend.calls).toContain('setMenuBarVisible:false');
1115
1589
  });
1116
1590
  });
@@ -1118,9 +1592,9 @@ describe('setWindowMenuBarVisible', () => {
1118
1592
  describe('setWindowMinimumSize', () => {
1119
1593
  it('sets constraints and delegates', () => {
1120
1594
  const backend = recordingWindowBackend();
1121
- setWindowBackend(backend);
1595
+ host = createTestHost(backend);
1122
1596
  const win = createApplicationWindow();
1123
- setWindowMinimumSize(win, 320, 240);
1597
+ setWindowMinimumSize(host, win, 320, 240);
1124
1598
  expect(win.minWidth).toBe(320);
1125
1599
  expect(win.minHeight).toBe(240);
1126
1600
  });
@@ -1129,9 +1603,9 @@ describe('setWindowMinimumSize', () => {
1129
1603
  describe('setWindowOpacity', () => {
1130
1604
  it('sets opacity and delegates', () => {
1131
1605
  const backend = recordingWindowBackend();
1132
- setWindowBackend(backend);
1606
+ host = createTestHost(backend);
1133
1607
  const win = createApplicationWindow();
1134
- setWindowOpacity(win, 0.5);
1608
+ setWindowOpacity(host, win, 0.5);
1135
1609
  expect(win.opacity).toBe(0.5);
1136
1610
  expect(backend.calls).toContain('setOpacity:0.5');
1137
1611
  });
@@ -1140,8 +1614,8 @@ describe('setWindowOpacity', () => {
1140
1614
  describe('setWindowParent', () => {
1141
1615
  it('delegates to the backend with null', () => {
1142
1616
  const backend = recordingWindowBackend();
1143
- setWindowBackend(backend);
1144
- setWindowParent(createApplicationWindow(), null);
1617
+ host = createTestHost(backend);
1618
+ setWindowParent(host, createApplicationWindow(), null);
1145
1619
  expect(backend.calls).toContain('setParent:null');
1146
1620
  });
1147
1621
  });
@@ -1149,13 +1623,13 @@ describe('setWindowParent', () => {
1149
1623
  describe('setWindowPosition', () => {
1150
1624
  it('sets position and emits onMove', () => {
1151
1625
  const backend = recordingWindowBackend();
1152
- setWindowBackend(backend);
1626
+ host = createTestHost(backend);
1153
1627
  const win = createApplicationWindow();
1154
1628
  let moved = false;
1155
1629
  connectSignal(win.onMove, () => {
1156
1630
  moved = true;
1157
1631
  });
1158
- setWindowPosition(win, 100, 50);
1632
+ setWindowPosition(host, win, 100, 50);
1159
1633
  expect(win.x).toBe(100);
1160
1634
  expect(win.y).toBe(50);
1161
1635
  expect(moved).toBe(true);
@@ -1165,8 +1639,8 @@ describe('setWindowPosition', () => {
1165
1639
  describe('setWindowProgress', () => {
1166
1640
  it('delegates to the backend', () => {
1167
1641
  const backend = recordingWindowBackend();
1168
- setWindowBackend(backend);
1169
- setWindowProgress(createApplicationWindow(), 0.25);
1642
+ host = createTestHost(backend);
1643
+ setWindowProgress(host, createApplicationWindow(), 0.25);
1170
1644
  expect(backend.calls).toContain('setProgress:0.25');
1171
1645
  });
1172
1646
  });
@@ -1174,9 +1648,9 @@ describe('setWindowProgress', () => {
1174
1648
  describe('setWindowResizable', () => {
1175
1649
  it('sets state and delegates', () => {
1176
1650
  const backend = recordingWindowBackend();
1177
- setWindowBackend(backend);
1651
+ host = createTestHost(backend);
1178
1652
  const win = createApplicationWindow();
1179
- setWindowResizable(win, false);
1653
+ setWindowResizable(host, win, false);
1180
1654
  expect(win.resizable).toBe(false);
1181
1655
  expect(backend.calls).toContain('setResizable:false');
1182
1656
  });
@@ -1185,13 +1659,13 @@ describe('setWindowResizable', () => {
1185
1659
  describe('setWindowSize', () => {
1186
1660
  it('sets size and emits onResize', () => {
1187
1661
  const backend = recordingWindowBackend();
1188
- setWindowBackend(backend);
1662
+ host = createTestHost(backend);
1189
1663
  const win = createApplicationWindow();
1190
1664
  let resized = false;
1191
1665
  connectSignal(win.onResize, () => {
1192
1666
  resized = true;
1193
1667
  });
1194
- setWindowSize(win, 800, 600);
1668
+ setWindowSize(host, win, 800, 600);
1195
1669
  expect(win.width).toBe(800);
1196
1670
  expect(win.height).toBe(600);
1197
1671
  expect(resized).toBe(true);
@@ -1201,9 +1675,9 @@ describe('setWindowSize', () => {
1201
1675
  describe('setWindowSkipTaskbar', () => {
1202
1676
  it('sets state and delegates', () => {
1203
1677
  const backend = recordingWindowBackend();
1204
- setWindowBackend(backend);
1678
+ host = createTestHost(backend);
1205
1679
  const win = createApplicationWindow();
1206
- setWindowSkipTaskbar(win, true);
1680
+ setWindowSkipTaskbar(host, win, true);
1207
1681
  expect(win.skipTaskbar).toBe(true);
1208
1682
  expect(backend.calls).toContain('setSkipTaskbar:true');
1209
1683
  });
@@ -1212,9 +1686,9 @@ describe('setWindowSkipTaskbar', () => {
1212
1686
  describe('setWindowTitle', () => {
1213
1687
  it('sets the title and delegates', () => {
1214
1688
  const backend = recordingWindowBackend();
1215
- setWindowBackend(backend);
1689
+ host = createTestHost(backend);
1216
1690
  const win = createApplicationWindow();
1217
- setWindowTitle(win, 'My App');
1691
+ setWindowTitle(host, win, 'My App');
1218
1692
  expect(win.title).toBe('My App');
1219
1693
  expect(backend.calls).toContain('setTitle:My App');
1220
1694
  });
@@ -1223,10 +1697,10 @@ describe('setWindowTitle', () => {
1223
1697
  describe('showWindow', () => {
1224
1698
  it('marks visible and delegates to the backend', () => {
1225
1699
  const backend = recordingWindowBackend();
1226
- setWindowBackend(backend);
1700
+ host = createTestHost(backend);
1227
1701
  const win = createApplicationWindow();
1228
- hideWindow(win);
1229
- showWindow(win);
1702
+ hideWindow(host, win);
1703
+ showWindow(host, win);
1230
1704
  expect(win.visible).toBe(true);
1231
1705
  expect(backend.calls).toContain('show');
1232
1706
  });