|
| 1 | +import type { DevframeNodeContext } from 'devframe/types' |
| 2 | +import type { CreateContextRpcServerOptions } from '../rpc-core' |
| 3 | +import { createRpcClient } from 'devframe/rpc/client' |
| 4 | +import { expect, it, vi } from 'vitest' |
| 5 | +import { RpcFunctionsHostImpl } from '../host-functions' |
| 6 | +import { createContextRpcServer } from '../rpc-core' |
| 7 | + |
| 8 | +// Simulate WebContainer losing AsyncLocalStorage context across awaits. |
| 9 | +vi.mock('node:async_hooks', () => ({ |
| 10 | + AsyncLocalStorage: class { |
| 11 | + store: unknown |
| 12 | + run(store: unknown, callback: () => unknown) { |
| 13 | + const previous = this.store |
| 14 | + this.store = store |
| 15 | + try { |
| 16 | + return callback() |
| 17 | + } |
| 18 | + finally { |
| 19 | + this.store = previous |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + getStore() { return this.store } |
| 24 | + }, |
| 25 | +})) |
| 26 | + |
| 27 | +function createServer(authorize?: CreateContextRpcServerOptions['authorize']) { |
| 28 | + const context = {} as DevframeNodeContext |
| 29 | + const rpc = new RpcFunctionsHostImpl(context) |
| 30 | + Object.assign(context, { rpc }) |
| 31 | + const { rpcGroup } = createContextRpcServer({ context, authorize }) |
| 32 | + |
| 33 | + // Connect real birpc peers through an in-memory channel. |
| 34 | + function connect(id: string) { |
| 35 | + let receiveServer: (data: unknown) => void |
| 36 | + let receiveClient: (data: unknown) => void |
| 37 | + rpcGroup.updateChannels((channels) => { |
| 38 | + channels.push({ |
| 39 | + meta: { id }, |
| 40 | + post: data => queueMicrotask(() => receiveClient(data)), |
| 41 | + on: fn => receiveServer = fn, |
| 42 | + }) |
| 43 | + }) |
| 44 | + return createRpcClient<Record<string, (...args: any[]) => any>>({}, { |
| 45 | + channel: { |
| 46 | + post: data => queueMicrotask(() => receiveServer(data)), |
| 47 | + on: fn => receiveClient = fn, |
| 48 | + }, |
| 49 | + rpcOptions: { timeout: 1000 }, |
| 50 | + }) |
| 51 | + } |
| 52 | + |
| 53 | + return { rpc, connect } |
| 54 | +} |
| 55 | + |
| 56 | +it.each([false, true])('keeps concurrent sessions isolated with schema validation: %s', async (withSchema) => { |
| 57 | + const { rpc, connect } = createServer() |
| 58 | + const handler = () => rpc.getCurrentRpcSession()?.meta.id |
| 59 | + const setup = vi.fn(async () => ({ handler })) |
| 60 | + rpc.register({ |
| 61 | + name: 'test:session', |
| 62 | + type: 'query', |
| 63 | + args: withSchema ? [{ '~standard': { version: 1, vendor: 'test', validate: async (value: unknown) => ({ value }) } }] : undefined, |
| 64 | + setup, |
| 65 | + }) |
| 66 | + const first = connect('first') |
| 67 | + const second = connect('second') |
| 68 | + const results = await Promise.all([ |
| 69 | + first.$call('test:session'), |
| 70 | + second.$call('test:session'), |
| 71 | + ]) |
| 72 | + expect(results).toEqual(['first', 'second']) |
| 73 | + expect(rpc.getCurrentRpcSession()).toBeUndefined() |
| 74 | + expect(setup).toHaveBeenCalledTimes(1) |
| 75 | +}) |
| 76 | + |
| 77 | +it('rejects unknown methods', async () => { |
| 78 | + const { connect } = createServer() |
| 79 | + await expect(connect('first').$call('test:missing')).rejects.toThrow('not found') |
| 80 | +}) |
| 81 | + |
| 82 | +it('rejects unauthorized calls before running setup', async () => { |
| 83 | + const { rpc, connect } = createServer(() => false) |
| 84 | + const setup = vi.fn(async () => ({ handler: () => 'value' })) |
| 85 | + rpc.register({ name: 'test:private', type: 'query', setup }) |
| 86 | + await expect(connect('first').$call('test:private')).rejects.toThrow('not authorized') |
| 87 | + expect(setup).not.toHaveBeenCalled() |
| 88 | +}) |
| 89 | + |
| 90 | +it('returns setup errors to the caller and allows a retry', async () => { |
| 91 | + const { rpc, connect } = createServer() |
| 92 | + const setup = vi.fn() |
| 93 | + .mockRejectedValueOnce(new Error('setup failed')) |
| 94 | + .mockResolvedValue({ handler: () => 'ready' }) |
| 95 | + rpc.register({ name: 'test:setup', type: 'query', setup }) |
| 96 | + const client = connect('first') |
| 97 | + await expect(client.$call('test:setup')).rejects.toThrow('setup failed') |
| 98 | + await expect(client.$call('test:setup')).resolves.toBe('ready') |
| 99 | + expect(setup).toHaveBeenCalledTimes(2) |
| 100 | +}) |
0 commit comments