public-runtime.test.ts (7274B)
1 import { validateRelayPolicy } from '../../src/lib/config/relays.ts'; 2 import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'; 3 let runtime: typeof import('../../src/lib/runtime/public-runtime.ts'); 4 beforeEach(async () => { 5 vi.resetModules(); 6 runtime = await import('../../src/lib/runtime/public-runtime.ts'); 7 }); 8 afterEach(() => { 9 vi.restoreAllMocks(); 10 vi.unstubAllGlobals(); 11 }); 12 describe('root public runtime context', () => { 13 it('keeps terminal run ownership separate from activity and revokes superseded or disposed ownership', () => { 14 vi.stubGlobal('window', {}); 15 let now = 0; 16 const context = runtime.createPublicRuntimeContext(); 17 const owner = runtime.mountPublicRuntime(context, undefined, { 18 now: () => now, 19 schedule: () => () => {} 20 })!; 21 const view = runtime.createPublicView(owner), 22 other = runtime.createPublicView(owner), 23 run = runtime.beginPublicViewRun(view); 24 try { 25 now = 16000; 26 expect(runtime.publicViewRunCurrent(view, run)).toBe(false); 27 expect(runtime.publicViewOwnsRun(view, run)).toBe(true); 28 expect(runtime.publicViewOwnsRun(other, run)).toBe(false); 29 const next = runtime.beginPublicViewRun(view); 30 expect(runtime.publicViewOwnsRun(view, run)).toBe(false); 31 expect(runtime.publicViewOwnsRun(view, next)).toBe(true); 32 runtime.disposePublicView(view); 33 expect(runtime.publicViewOwnsRun(view, next)).toBe(false); 34 } finally { 35 runtime.closePublicRuntime(context); 36 } 37 }); 38 it('reserves the view generation across synchronous run-creation clock reentry', () => { 39 vi.stubGlobal('window', {}); 40 const context = runtime.createPublicRuntimeContext(); 41 let armed = false; 42 let inner: ReturnType<typeof runtime.beginPublicViewRun> | undefined; 43 const owner = runtime.mountPublicRuntime(context, undefined, { 44 now: () => { 45 if (armed) { 46 armed = false; 47 inner = runtime.beginPublicViewRun(view); 48 } 49 return 0; 50 }, 51 schedule: () => () => {} 52 })!; 53 const view = runtime.createPublicView(owner); 54 armed = true; 55 try { 56 expect(() => runtime.beginPublicViewRun(view)).toThrow( 57 'public_view_run_superseded' 58 ); 59 expect(inner).toBeDefined(); 60 expect(runtime.publicViewRunCurrent(view, inner!)).toBe(true); 61 } finally { 62 runtime.closePublicRuntime(context); 63 } 64 }); 65 it('creates an inert SSR context without acquiring browser capabilities', () => { 66 vi.stubGlobal('window', undefined); 67 const context = runtime.createPublicRuntimeContext(); 68 expect(Object.isFrozen(context)).toBe(true); 69 expect(Object.keys(context)).toEqual([]); 70 expect(runtime.publicRuntime(context)).toBeUndefined(); 71 expect(runtime.mountPublicRuntime(context)).toBeUndefined(); 72 runtime.closePublicRuntime(context); 73 expect(runtime.publicRuntime(context)).toBeUndefined(); 74 }); 75 it('owns one anonymous mounted browser lifetime and terminal teardown', () => { 76 vi.stubGlobal('window', {}); 77 const context = runtime.createPublicRuntimeContext(), 78 owner = runtime.mountPublicRuntime(context)!; 79 expect(owner).toBeDefined(); 80 expect(runtime.mountPublicRuntime(context)).toBe(owner); 81 expect(runtime.publicRuntime(context)).toBe(owner); 82 const a = runtime.createPublicView(owner), 83 b = runtime.createPublicView(owner); 84 const run = runtime.beginPublicViewRun(a); 85 runtime.beginPublicViewRun(b); 86 const next = runtime.beginPublicViewRun(a); 87 expect(runtime.publicViewRunCurrent(a, run)).toBe(false); 88 expect(runtime.publicViewRunCurrent(a, next)).toBe(true); 89 runtime.disposePublicView(a); 90 expect(runtime.publicViewRunCurrent(a, next)).toBe(false); 91 runtime.closePublicRuntime(context); 92 expect(() => runtime.createPublicView(owner)).toThrow( 93 'public_runtime_closed' 94 ); 95 expect(() => runtime.mountPublicRuntime(context)).toThrow( 96 'public_runtime_closed' 97 ); 98 runtime.closePublicRuntime(context); 99 }); 100 it('rejects forged context and view ownership', () => { 101 expect(() => 102 runtime.publicRuntime( 103 {} as ReturnType<typeof runtime.createPublicRuntimeContext> 104 ) 105 ).toThrow('public_runtime_context_invalid'); 106 vi.stubGlobal('window', {}); 107 const context = runtime.createPublicRuntimeContext(), 108 owner = runtime.mountPublicRuntime(context)!; 109 expect(owner).toBeDefined(); 110 expect(() => 111 runtime.beginPublicViewRun( 112 {} as ReturnType<typeof runtime.createPublicView> 113 ) 114 ).toThrow('public_view_invalid'); 115 runtime.closePublicRuntime(context); 116 expect(runtime.publicRuntime(context)).toBeUndefined(); 117 }); 118 it('settles readiness safely when an SSR context is disposed before hydration', async () => { 119 vi.stubGlobal('window', undefined); 120 const context = runtime.createPublicRuntimeContext(); 121 const ready = runtime.publicRuntimeReady(context); 122 runtime.closePublicRuntime(context); 123 expect(await ready).toBeUndefined(); 124 expect(() => runtime.mountPublicRuntime(context)).toThrow( 125 'public_runtime_closed' 126 ); 127 }); 128 it('prevents a second root owner or changed manifest/clock from resetting admission', () => { 129 vi.stubGlobal('window', {}); 130 const context = runtime.createPublicRuntimeContext(), 131 owner = runtime.mountPublicRuntime(context)!; 132 expect(owner).toBeDefined(); 133 const other = runtime.createPublicRuntimeContext(); 134 expect(() => runtime.mountPublicRuntime(other)).toThrow( 135 'public_runtime_owner_changed' 136 ); 137 const policy = validateRelayPolicy( 138 JSON.stringify({ 139 schemaVersion: 1, 140 public: [], 141 inbox: [], 142 postingEnabled: false, 143 messagingEnabled: false, 144 operatorDenylist: [] 145 }) 146 )!; 147 expect(() => runtime.mountPublicRuntime(context, policy)).toThrow( 148 'public_runtime_policy_changed' 149 ); 150 expect(() => 151 runtime.mountPublicRuntime(context, undefined, { 152 now: () => 0, 153 schedule: () => () => {} 154 }) 155 ).toThrow('public_runtime_clock_changed'); 156 runtime.closePublicRuntime(context); 157 runtime.closePublicRuntime(other); 158 }); 159 it('keeps root teardown terminal while a failed actual SDK close remains retryable', async () => { 160 const { RelayPool } = await import('applesauce-relay/pool'); // eslint-disable-next-line @typescript-eslint/unbound-method -- Explicit receiver is provided by original.call(this) below. 161 const original = RelayPool.prototype.close; 162 let calls = 0; 163 vi.spyOn(RelayPool.prototype, 'close').mockImplementation(function ( 164 this: InstanceType<typeof RelayPool> 165 ) { 166 calls++; 167 if (calls === 1) throw new Error('RAW_SDK_CLOSE_ERROR'); 168 original.call(this); 169 }); 170 vi.stubGlobal('window', {}); 171 const context = runtime.createPublicRuntimeContext(), 172 owner = runtime.mountPublicRuntime(context)!; 173 expect(() => runtime.closePublicRuntime(context)).toThrow( 174 'public_runtime_close_failed' 175 ); 176 expect(runtime.publicRuntime(context)).toBeUndefined(); 177 expect(() => runtime.createPublicView(owner)).toThrow( 178 'public_runtime_closed' 179 ); 180 runtime.closePublicRuntime(context); 181 expect(calls).toBe(2); 182 }); 183 });