|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | +import { ChannelMessage, Unport, Unrpc, UnrpcExecutionErrorError, UnrpcNotImplementationError } from '../src'; |
| 3 | + |
| 4 | +export type Definition = { |
| 5 | + parent2child: { |
| 6 | + syn: { |
| 7 | + pid: string; |
| 8 | + }; |
| 9 | + getInfo__callback: { |
| 10 | + user: string; |
| 11 | + }; |
| 12 | + getChildInfo: { |
| 13 | + name: string; |
| 14 | + } |
| 15 | + }; |
| 16 | + child2parent: { |
| 17 | + getInfo: { |
| 18 | + id: string; |
| 19 | + }; |
| 20 | + getChildInfo__callback: { |
| 21 | + clientKey: string; |
| 22 | + }; |
| 23 | + ack: { |
| 24 | + pid: string; |
| 25 | + }; |
| 26 | + }; |
| 27 | +}; |
| 28 | + |
| 29 | +describe('Unrpc', () => { |
| 30 | + let childPort: Unport<Definition, 'child'>; |
| 31 | + let parentPort: Unport<Definition, 'parent'>; |
| 32 | + let child: Unrpc<Definition, 'child'>; |
| 33 | + let parent: Unrpc<Definition, 'parent'>; |
| 34 | + |
| 35 | + beforeEach(() => { |
| 36 | + const messageChannel = new MessageChannel(); |
| 37 | + childPort = new Unport(); |
| 38 | + childPort.implementChannel({ |
| 39 | + send(message) { |
| 40 | + messageChannel.port1.postMessage(message); |
| 41 | + }, |
| 42 | + accept(pipe) { |
| 43 | + messageChannel.port1.onmessage = (message: MessageEvent<ChannelMessage>) => pipe(message.data); |
| 44 | + }, |
| 45 | + }); |
| 46 | + child = new Unrpc(childPort); |
| 47 | + |
| 48 | + parentPort = new Unport(); |
| 49 | + parentPort.implementChannel({ |
| 50 | + send(message) { |
| 51 | + console.log(message); |
| 52 | + messageChannel.port2.postMessage(message); |
| 53 | + }, |
| 54 | + accept(pipe) { |
| 55 | + messageChannel.port2.onmessage = (message: MessageEvent<ChannelMessage>) => pipe(message.data); |
| 56 | + }, |
| 57 | + }); |
| 58 | + |
| 59 | + parent = new Unrpc(parentPort); |
| 60 | + }); |
| 61 | + |
| 62 | + it('implemented method - asynchronous implementation', async () => { |
| 63 | + parent.implement('getInfo', async ({ id }) => ({ user: id })); |
| 64 | + const response = child.call('getInfo', { id: 'name' }); |
| 65 | + expect(response).resolves.toMatchObject({ user: 'name' }); |
| 66 | + }); |
| 67 | + |
| 68 | + it('implemented method - synchronous implementation', async () => { |
| 69 | + parent.implement('getInfo', ({ id }) => ({ user: id })); |
| 70 | + const response = child.call('getInfo', { id: 'name' }); |
| 71 | + expect(response).resolves.toMatchObject({ user: 'name' }); |
| 72 | + }); |
| 73 | + |
| 74 | + it('Error: UnrpcNotImplementationError', async () => { |
| 75 | + expect(child.call('getInfo', { id: 'name' })).rejects.toMatchObject( |
| 76 | + new UnrpcNotImplementationError('Method getInfo is not implemented'), |
| 77 | + ); |
| 78 | + }); |
| 79 | + |
| 80 | + it('Error: UnrpcExecutionErrorError - asynchronous implementation', async () => { |
| 81 | + parent.implement('getInfo', async () => { |
| 82 | + // @ts-expect-error mock execution error here. |
| 83 | + const result = foo; |
| 84 | + return result; |
| 85 | + }); |
| 86 | + expect(child.call('getInfo', { id: 'name' })).rejects.toMatchObject( |
| 87 | + new UnrpcExecutionErrorError('foo is not defined'), |
| 88 | + ); |
| 89 | + }); |
| 90 | + |
| 91 | + it('Error: UnrpcExecutionErrorError - synchronous implementation', async () => { |
| 92 | + parent.implement('getInfo', () => { |
| 93 | + // @ts-expect-error mock execution error here. |
| 94 | + const result = foo; |
| 95 | + return result; |
| 96 | + }); |
| 97 | + expect(child.call('getInfo', { id: 'name' })).rejects.toMatchObject( |
| 98 | + new UnrpcExecutionErrorError('foo is not defined'), |
| 99 | + ); |
| 100 | + }); |
| 101 | + |
| 102 | + it('complicated case', async () => { |
| 103 | + parent.implement('getInfo', async ({ id }) => ({ user: id })); |
| 104 | + child.implement('getChildInfo', async ({ name }) => ({ clientKey: name })); |
| 105 | + const [response1, response2] = await Promise.all([ |
| 106 | + child.call('getInfo', { id: 'child' }), |
| 107 | + parent.call('getChildInfo', { name: 'parent' }), |
| 108 | + ]); |
| 109 | + expect(response1).toMatchObject({ user: 'child' }); |
| 110 | + expect(response2).toMatchObject({ clientKey: 'parent' }); |
| 111 | + }); |
| 112 | +}); |
0 commit comments