|
| 1 | +/* |
| 2 | +Toolbox Aid |
| 3 | +David Quesenberry |
| 4 | +04/16/2026 |
| 5 | +Phase19OverlayPluginRegistry.test.mjs |
| 6 | +*/ |
| 7 | +import assert from 'node:assert/strict'; |
| 8 | +import { LEVEL17_OVERLAY_CYCLE_KEY } from '../../samples/phase-17/shared/overlayCycleInput.js'; |
| 9 | +import { |
| 10 | + renderOverlayGameplayRuntime, |
| 11 | + stepOverlayGameplayRuntime, |
| 12 | +} from '../../samples/phase-17/shared/overlayGameplayRuntime.js'; |
| 13 | +import { definePhase19OverlayExtension } from '../../samples/phase-19/shared/overlay/createPhase19OverlayExpansionFramework.js'; |
| 14 | +import createPhase19OverlayPluginRegistry from '../../samples/phase-19/shared/overlay/createPhase19OverlayPluginRegistry.js'; |
| 15 | + |
| 16 | +function createRendererProbe(width = 960, height = 540) { |
| 17 | + return { |
| 18 | + getCanvasSize() { |
| 19 | + return { width, height }; |
| 20 | + }, |
| 21 | + clear() {}, |
| 22 | + drawRect() {}, |
| 23 | + strokeRect() {}, |
| 24 | + drawText() {}, |
| 25 | + drawLine() {}, |
| 26 | + drawPolygon() {}, |
| 27 | + drawImageFrame() {}, |
| 28 | + }; |
| 29 | +} |
| 30 | + |
| 31 | +function assertPluginRegistrationAndRuntimeCompatibility() { |
| 32 | + const counters = { step: 0, render: 0 }; |
| 33 | + const registry = createPhase19OverlayPluginRegistry(); |
| 34 | + const result = registry.registerPlugin({ |
| 35 | + id: 'phase19.runtime.plugin', |
| 36 | + version: '1.0.0', |
| 37 | + metadata: { owner: 'runtime-test' }, |
| 38 | + createOverlayExtension() { |
| 39 | + return definePhase19OverlayExtension({ |
| 40 | + id: 'phase19.runtime.plugin.overlay', |
| 41 | + overlays: [ |
| 42 | + { id: 'ui-layer', label: 'UI Layer' }, |
| 43 | + { id: 'plugin-runtime', label: 'Plugin Runtime' }, |
| 44 | + ], |
| 45 | + initialOverlayId: 'ui-layer', |
| 46 | + persistenceKey: 'phase19:plugin:overlay-index', |
| 47 | + cycleKey: LEVEL17_OVERLAY_CYCLE_KEY, |
| 48 | + runtimeExtensions: [ |
| 49 | + { |
| 50 | + overlayId: 'plugin-runtime', |
| 51 | + onStep() { |
| 52 | + counters.step += 1; |
| 53 | + }, |
| 54 | + onRender() { |
| 55 | + counters.render += 1; |
| 56 | + }, |
| 57 | + }, |
| 58 | + ], |
| 59 | + }); |
| 60 | + }, |
| 61 | + }); |
| 62 | + |
| 63 | + assert.deepEqual( |
| 64 | + result, |
| 65 | + { pluginId: 'phase19.runtime.plugin', extensionId: 'phase19.runtime.plugin.overlay' }, |
| 66 | + 'Plugin registry should return plugin id and resolved extension id.' |
| 67 | + ); |
| 68 | + assert.equal(registry.listPlugins().length, 1, 'Plugin registry should track one registered plugin.'); |
| 69 | + assert.equal( |
| 70 | + registry.getPluginExtensionId('phase19.runtime.plugin'), |
| 71 | + 'phase19.runtime.plugin.overlay', |
| 72 | + 'Plugin registry should expose extension id mapping.' |
| 73 | + ); |
| 74 | + |
| 75 | + const framework = registry.getFramework(); |
| 76 | + assert.deepEqual( |
| 77 | + framework.listExtensionIds(), |
| 78 | + ['phase19.runtime.plugin.overlay'], |
| 79 | + 'Expansion framework should receive plugin overlay extension registration.' |
| 80 | + ); |
| 81 | + |
| 82 | + const runtime = framework.createRuntimeForExtension('phase19.runtime.plugin.overlay'); |
| 83 | + assert.notEqual(runtime, null, 'Registered plugin extension should create overlay runtime.'); |
| 84 | + assert.equal(runtime.interactionCycleKey, LEVEL17_OVERLAY_CYCLE_KEY, 'Plugin runtime should preserve shared non-Tab cycle key.'); |
| 85 | + assert.equal( |
| 86 | + stepOverlayGameplayRuntime(runtime, { activeOverlayId: 'plugin-runtime' }), |
| 87 | + 1, |
| 88 | + 'Plugin runtime extension should execute step hook through existing overlay runtime.' |
| 89 | + ); |
| 90 | + assert.equal( |
| 91 | + renderOverlayGameplayRuntime(runtime, { |
| 92 | + activeOverlayId: 'plugin-runtime', |
| 93 | + renderer: createRendererProbe(), |
| 94 | + }), |
| 95 | + 1, |
| 96 | + 'Plugin runtime extension should execute render hook through existing overlay runtime.' |
| 97 | + ); |
| 98 | + assert.equal(counters.step, 1, 'Plugin runtime step hook should run exactly once.'); |
| 99 | + assert.equal(counters.render, 1, 'Plugin runtime render hook should run exactly once.'); |
| 100 | +} |
| 101 | + |
| 102 | +function assertPluginUnregisterCleansFramework() { |
| 103 | + const registry = createPhase19OverlayPluginRegistry(); |
| 104 | + registry.registerPlugin({ |
| 105 | + id: 'phase19.cleanup.plugin', |
| 106 | + extension: definePhase19OverlayExtension({ |
| 107 | + id: 'phase19.cleanup.overlay', |
| 108 | + overlays: [{ id: 'ui-layer', label: 'UI Layer' }], |
| 109 | + }), |
| 110 | + }); |
| 111 | + |
| 112 | + assert.equal(registry.unregisterPlugin('phase19.cleanup.plugin'), true, 'Registered plugin should be removable.'); |
| 113 | + assert.equal(registry.unregisterPlugin('phase19.cleanup.plugin'), false, 'Repeated unregister should return false.'); |
| 114 | + assert.equal(registry.getFramework().getExtension('phase19.cleanup.overlay'), null, 'Plugin removal should remove extension from framework.'); |
| 115 | +} |
| 116 | + |
| 117 | +export function run() { |
| 118 | + assertPluginRegistrationAndRuntimeCompatibility(); |
| 119 | + assertPluginUnregisterCleansFramework(); |
| 120 | +} |
0 commit comments