|
| 1 | +import type { ConnectionMeta } from 'devframe/types' |
| 2 | +import type { DevframeRpcClientMode } from './rpc' |
| 3 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 4 | + |
| 5 | +// `getDevframeRpcClient` kicks off the auth bootstrap (`requestTrust`, then |
| 6 | +// the URL OTP, then a native-prompt fallback) without awaiting it, so it can |
| 7 | +// return the client immediately. The regression this guards is a caller's |
| 8 | +// very first `rpc.call(...)` — issued the moment `connectDevframe()` resolves |
| 9 | +// — racing that still-in-flight bootstrap: without a gate, the transport |
| 10 | +// sends the call over an already-open-but-not-yet-trusted socket and the |
| 11 | +// server rejects it with DF0036, even though the exact same call succeeds a |
| 12 | +// moment later once the handshake lands. |
| 13 | +// |
| 14 | +// Exercising this through a real WebSocket would mean hand-rolling birpc's |
| 15 | +// wire protocol, so instead this mocks `./rpc-ws` with a controllable fake |
| 16 | +// mode — a `requestTrust()` the test resolves on its own schedule, and a |
| 17 | +// `call` spy whose invocation timing is exactly what's under test. |
| 18 | +const fakeMode = vi.hoisted(() => { |
| 19 | + const requestTrustGate = { resolve: undefined as ((value: boolean) => void) | undefined } |
| 20 | + const call = vi.fn(async () => 'ok') |
| 21 | + const callOptional = vi.fn(async () => 'ok') |
| 22 | + const callEvent = vi.fn(async () => {}) |
| 23 | + return { requestTrustGate, call, callOptional, callEvent } |
| 24 | +}) |
| 25 | + |
| 26 | +vi.mock('./rpc-ws', () => ({ |
| 27 | + createWsRpcClientMode: vi.fn((): DevframeRpcClientMode => ({ |
| 28 | + isTrusted: false, |
| 29 | + status: 'connecting', |
| 30 | + connectionError: null, |
| 31 | + ensureTrusted: async () => true, |
| 32 | + requestTrust: () => new Promise<boolean>((resolve) => { |
| 33 | + fakeMode.requestTrustGate.resolve = resolve |
| 34 | + }), |
| 35 | + requestTrustWithToken: async () => true, |
| 36 | + requestTrustWithCode: async () => null, |
| 37 | + call: fakeMode.call as DevframeRpcClientMode['call'], |
| 38 | + callOptional: fakeMode.callOptional as DevframeRpcClientMode['callOptional'], |
| 39 | + callEvent: fakeMode.callEvent as DevframeRpcClientMode['callEvent'], |
| 40 | + })), |
| 41 | +})) |
| 42 | + |
| 43 | +class FakeBroadcastChannel { |
| 44 | + onmessage: ((e: any) => void) | null = null |
| 45 | + postMessage(): void {} |
| 46 | + close(): void {} |
| 47 | +} |
| 48 | + |
| 49 | +const connectionMeta: ConnectionMeta = { backend: 'websocket', websocket: { path: '__ws' } } |
| 50 | + |
| 51 | +describe('getDevframeRpcClient — auth bootstrap gates outbound calls', () => { |
| 52 | + beforeEach(() => { |
| 53 | + fakeMode.requestTrustGate.resolve = undefined |
| 54 | + fakeMode.call.mockClear() |
| 55 | + fakeMode.callOptional.mockClear() |
| 56 | + fakeMode.callEvent.mockClear() |
| 57 | + vi.stubGlobal('BroadcastChannel', FakeBroadcastChannel) |
| 58 | + vi.stubGlobal('navigator', { userAgent: 'test' }) |
| 59 | + vi.stubGlobal('location', { |
| 60 | + protocol: 'http:', |
| 61 | + host: 'localhost:5173', |
| 62 | + hostname: 'localhost', |
| 63 | + href: 'http://localhost:5173/index.html', |
| 64 | + origin: 'http://localhost:5173', |
| 65 | + }) |
| 66 | + }) |
| 67 | + |
| 68 | + afterEach(() => { |
| 69 | + vi.unstubAllGlobals() |
| 70 | + }) |
| 71 | + |
| 72 | + it('holds `call` until the in-flight bootstrap settles, instead of sending it early', async () => { |
| 73 | + const { getDevframeRpcClient } = await import('./rpc') |
| 74 | + const rpc = await getDevframeRpcClient({ |
| 75 | + connectionMeta, |
| 76 | + otpParam: false, |
| 77 | + simpleAuth: false, |
| 78 | + }) |
| 79 | + |
| 80 | + // Fired the instant the client is available — mirrors a component's |
| 81 | + // `onMount` calling a trusted method right after `connectDevframe()`. |
| 82 | + const pending = rpc.call('test:probe' as any) |
| 83 | + await Promise.resolve() |
| 84 | + await Promise.resolve() |
| 85 | + // The bootstrap's `requestTrust()` hasn't resolved yet — the call must |
| 86 | + // not have reached the (mocked) transport. |
| 87 | + expect(fakeMode.call).not.toHaveBeenCalled() |
| 88 | + |
| 89 | + // The handshake lands. |
| 90 | + fakeMode.requestTrustGate.resolve?.(true) |
| 91 | + |
| 92 | + await expect(pending).resolves.toBe('ok') |
| 93 | + expect(fakeMode.call).toHaveBeenCalledTimes(1) |
| 94 | + expect(fakeMode.call).toHaveBeenCalledWith('test:probe') |
| 95 | + }) |
| 96 | + |
| 97 | + it('holds `callOptional` and `callEvent` the same way', async () => { |
| 98 | + const { getDevframeRpcClient } = await import('./rpc') |
| 99 | + const rpc = await getDevframeRpcClient({ |
| 100 | + connectionMeta, |
| 101 | + otpParam: false, |
| 102 | + simpleAuth: false, |
| 103 | + }) |
| 104 | + |
| 105 | + const pendingOptional = rpc.callOptional('test:optional' as any) |
| 106 | + const pendingEvent = rpc.callEvent('test:event' as any) |
| 107 | + await Promise.resolve() |
| 108 | + await Promise.resolve() |
| 109 | + expect(fakeMode.callOptional).not.toHaveBeenCalled() |
| 110 | + expect(fakeMode.callEvent).not.toHaveBeenCalled() |
| 111 | + |
| 112 | + fakeMode.requestTrustGate.resolve?.(true) |
| 113 | + |
| 114 | + await expect(pendingOptional).resolves.toBe('ok') |
| 115 | + await pendingEvent |
| 116 | + expect(fakeMode.callOptional).toHaveBeenCalledTimes(1) |
| 117 | + expect(fakeMode.callEvent).toHaveBeenCalledTimes(1) |
| 118 | + }) |
| 119 | + |
| 120 | + it('stops gating once the first bootstrap attempt has settled', async () => { |
| 121 | + const { getDevframeRpcClient } = await import('./rpc') |
| 122 | + const rpc = await getDevframeRpcClient({ |
| 123 | + connectionMeta, |
| 124 | + otpParam: false, |
| 125 | + simpleAuth: false, |
| 126 | + }) |
| 127 | + |
| 128 | + fakeMode.requestTrustGate.resolve?.(true) |
| 129 | + // Let the bootstrap's own promise chain fully settle before the next |
| 130 | + // call — a few microtask ticks cover `await mode.requestTrust()` |
| 131 | + // resuming, `bootstrapAuth()` returning, and its `.then()` flipping |
| 132 | + // `bootstrapAuthSettled`. |
| 133 | + for (let i = 0; i < 5; i++) |
| 134 | + await Promise.resolve() |
| 135 | + |
| 136 | + await rpc.call('test:probe' as any) |
| 137 | + // Sent straight through — no more waiting once bootstrap is over. |
| 138 | + expect(fakeMode.call).toHaveBeenCalledTimes(1) |
| 139 | + }) |
| 140 | +}) |
0 commit comments