|
| 1 | +import type { DevToolsNodeContext, RpcFunctionDefinition } from '@vitejs/devtools-kit' |
| 2 | +import { describe, expect, it } from 'vitest' |
| 3 | +import { RpcFunctionsHost } from '../host-functions' |
| 4 | + |
| 5 | +async function emptyHandler() { /* empty */ } |
| 6 | +const returnFirst = async () => 'first' |
| 7 | +const returnSecond = async () => 'second' |
| 8 | +const returnV1 = async () => 'v1' |
| 9 | +const returnV2 = async () => 'v2' |
| 10 | +const setupWith = <T>(handler: () => Promise<T>) => async () => ({ handler }) |
| 11 | + |
| 12 | +describe('rpcFunctionsHost', () => { |
| 13 | + const mockContext = {} as DevToolsNodeContext |
| 14 | + |
| 15 | + describe('register() collision detection', () => { |
| 16 | + it('should register a new RPC function successfully', () => { |
| 17 | + const host = new RpcFunctionsHost(mockContext) |
| 18 | + const fn: RpcFunctionDefinition<string, any, any, any> = { |
| 19 | + name: 'test-function', |
| 20 | + type: 'action', |
| 21 | + setup: setupWith(emptyHandler), |
| 22 | + } |
| 23 | + |
| 24 | + expect(() => host.register(fn)).not.toThrow() |
| 25 | + expect(host.definitions.has('test-function')).toBe(true) |
| 26 | + }) |
| 27 | + |
| 28 | + it('should throw error when registering duplicate RPC function ID', () => { |
| 29 | + const host = new RpcFunctionsHost(mockContext) |
| 30 | + const fn1: RpcFunctionDefinition<string, any, any, any> = { |
| 31 | + name: 'duplicate-fn', |
| 32 | + type: 'action', |
| 33 | + setup: setupWith(returnFirst), |
| 34 | + } |
| 35 | + const fn2: RpcFunctionDefinition<string, any, any, any> = { |
| 36 | + name: 'duplicate-fn', |
| 37 | + type: 'action', |
| 38 | + setup: setupWith(returnSecond), |
| 39 | + } |
| 40 | + |
| 41 | + host.register(fn1) |
| 42 | + |
| 43 | + const registerDuplicate = () => host.register(fn2) |
| 44 | + expect(registerDuplicate).toThrow() |
| 45 | + expect(registerDuplicate).toThrow('duplicate-fn') |
| 46 | + expect(registerDuplicate).toThrow('already registered') |
| 47 | + }) |
| 48 | + |
| 49 | + it('should include the duplicate ID in error message', () => { |
| 50 | + const host = new RpcFunctionsHost(mockContext) |
| 51 | + const fn: RpcFunctionDefinition<string, any, any, any> = { |
| 52 | + name: 'my-special-function', |
| 53 | + type: 'query', |
| 54 | + setup: setupWith(emptyHandler), |
| 55 | + } |
| 56 | + |
| 57 | + host.register(fn) |
| 58 | + |
| 59 | + const registerAgain = () => host.register(fn) |
| 60 | + expect(registerAgain).toThrow('my-special-function') |
| 61 | + }) |
| 62 | + }) |
| 63 | + |
| 64 | + describe('update() existence validation', () => { |
| 65 | + it('should throw error when updating non-existent RPC function', () => { |
| 66 | + const host = new RpcFunctionsHost(mockContext) |
| 67 | + const fn: RpcFunctionDefinition<string, any, any, any> = { |
| 68 | + name: 'nonexistent', |
| 69 | + type: 'action', |
| 70 | + setup: setupWith(emptyHandler), |
| 71 | + } |
| 72 | + |
| 73 | + const updateNonexistent = () => host.update(fn) |
| 74 | + expect(updateNonexistent).toThrow() |
| 75 | + expect(updateNonexistent).toThrow('nonexistent') |
| 76 | + expect(updateNonexistent).toThrow('not registered') |
| 77 | + expect(updateNonexistent).toThrow('Use register()') |
| 78 | + }) |
| 79 | + |
| 80 | + it('should update existing RPC function successfully', () => { |
| 81 | + const host = new RpcFunctionsHost(mockContext) |
| 82 | + const fn1: RpcFunctionDefinition<string, any, any, any> = { |
| 83 | + name: 'update-test', |
| 84 | + type: 'action', |
| 85 | + setup: setupWith(returnV1), |
| 86 | + } |
| 87 | + const fn2: RpcFunctionDefinition<string, any, any, any> = { |
| 88 | + name: 'update-test', |
| 89 | + type: 'action', |
| 90 | + setup: setupWith(returnV2), |
| 91 | + } |
| 92 | + |
| 93 | + host.register(fn1) |
| 94 | + const doUpdate = () => host.update(fn2) |
| 95 | + expect(doUpdate).not.toThrow() |
| 96 | + |
| 97 | + const updated = host.definitions.get('update-test') |
| 98 | + expect(updated).toBe(fn2) |
| 99 | + }) |
| 100 | + |
| 101 | + it('should validate that update only works on existing entries', () => { |
| 102 | + const host = new RpcFunctionsHost(mockContext) |
| 103 | + |
| 104 | + // Register one function |
| 105 | + host.register({ |
| 106 | + name: 'exists', |
| 107 | + type: 'action', |
| 108 | + setup: setupWith(emptyHandler), |
| 109 | + }) |
| 110 | + |
| 111 | + // Update should work for existing |
| 112 | + const updateExisting = () => |
| 113 | + host.update({ |
| 114 | + name: 'exists', |
| 115 | + type: 'action', |
| 116 | + setup: setupWith(emptyHandler), |
| 117 | + }) |
| 118 | + expect(updateExisting).not.toThrow() |
| 119 | + |
| 120 | + // Update should fail for non-existing |
| 121 | + const updateMissing = () => |
| 122 | + host.update({ |
| 123 | + name: 'does-not-exist', |
| 124 | + type: 'action', |
| 125 | + setup: setupWith(emptyHandler), |
| 126 | + }) |
| 127 | + expect(updateMissing).toThrow() |
| 128 | + }) |
| 129 | + }) |
| 130 | +}) |
0 commit comments