|
| 1 | +import type { DevframeRpcClientFunctions } from 'devframe/types' |
| 2 | +import type { DevframeClientRpcHost, DevframeRpcContext, RpcClientEvents } from './rpc' |
| 3 | +import { RpcFunctionsCollectorBase } from 'devframe/rpc' |
| 4 | +import { createEventEmitter } from 'devframe/utils/events' |
| 5 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 6 | +import { createLiveRpcClientMode } from './rpc-live' |
| 7 | + |
| 8 | +vi.mock('devframe/rpc/client', () => ({ |
| 9 | + createRpcClient: () => ({ $call: vi.fn(async () => ({ isTrusted: true })) }), |
| 10 | +})) |
| 11 | + |
| 12 | +function createMode() { |
| 13 | + const clientRpc: DevframeClientRpcHost = new RpcFunctionsCollectorBase<DevframeRpcClientFunctions, DevframeRpcContext>({ rpc: undefined! }) |
| 14 | + return createLiveRpcClientMode({ |
| 15 | + transport: 'websocket', |
| 16 | + connectionMeta: { backend: 'websocket', websocket: { path: '__ws' } }, |
| 17 | + events: createEventEmitter<RpcClientEvents>(), |
| 18 | + clientRpc, |
| 19 | + createChannel: () => ({ post: vi.fn(), on: vi.fn(), close: vi.fn() }), |
| 20 | + }) |
| 21 | +} |
| 22 | + |
| 23 | +describe('trust deadline cleanup', () => { |
| 24 | + beforeEach(() => { |
| 25 | + vi.useFakeTimers() |
| 26 | + vi.stubGlobal('navigator', { userAgent: 'test' }) |
| 27 | + vi.stubGlobal('location', { origin: 'http://localhost' }) |
| 28 | + }) |
| 29 | + |
| 30 | + afterEach(() => { |
| 31 | + vi.useRealTimers() |
| 32 | + vi.unstubAllGlobals() |
| 33 | + }) |
| 34 | + |
| 35 | + it('clears concurrent deadlines as soon as authentication succeeds', async () => { |
| 36 | + expect.assertions(4) |
| 37 | + const mode = createMode() |
| 38 | + const first = mode.ensureTrusted(60_000) |
| 39 | + const second = mode.ensureTrusted(30_000) |
| 40 | + expect(vi.getTimerCount()).toBe(2) |
| 41 | + await mode.requestTrustWithToken('test-token') |
| 42 | + await expect(first).resolves.toBe(true) |
| 43 | + await expect(second).resolves.toBe(true) |
| 44 | + expect(vi.getTimerCount()).toBe(0) |
| 45 | + }) |
| 46 | + |
| 47 | + it('leaves no deadline behind when already trusted', async () => { |
| 48 | + expect.assertions(2) |
| 49 | + const mode = createMode() |
| 50 | + await mode.requestTrustWithToken('test-token') |
| 51 | + await expect(mode.ensureTrusted()).resolves.toBe(true) |
| 52 | + expect(vi.getTimerCount()).toBe(0) |
| 53 | + }) |
| 54 | + |
| 55 | + it('preserves expiry and unlimited trust waits', async () => { |
| 56 | + expect.assertions(4) |
| 57 | + const mode = createMode() |
| 58 | + const unlimited = mode.ensureTrusted(0) |
| 59 | + expect(vi.getTimerCount()).toBe(0) |
| 60 | + const expiry = expect(mode.ensureTrusted(10)).rejects.toThrow('Timeout waiting for rpc to be trusted') |
| 61 | + await vi.advanceTimersByTimeAsync(10) |
| 62 | + await expiry |
| 63 | + expect(vi.getTimerCount()).toBe(0) |
| 64 | + await mode.requestTrustWithToken('test-token') |
| 65 | + await expect(unlimited).resolves.toBe(true) |
| 66 | + }) |
| 67 | +}) |
0 commit comments