|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2025 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 8 | +import { Connection, RequestError } from './connection.js'; |
| 9 | +import { ReadableStream, WritableStream } from 'node:stream/web'; |
| 10 | + |
| 11 | +describe('Connection', () => { |
| 12 | + let toPeer: WritableStream<Uint8Array>; |
| 13 | + let fromPeer: ReadableStream<Uint8Array>; |
| 14 | + let peerController: ReadableStreamDefaultController<Uint8Array>; |
| 15 | + let receivedChunks: string[] = []; |
| 16 | + let connection: Connection; |
| 17 | + let handler: ReturnType<typeof vi.fn>; |
| 18 | + |
| 19 | + beforeEach(() => { |
| 20 | + receivedChunks = []; |
| 21 | + toPeer = new WritableStream({ |
| 22 | + write(chunk) { |
| 23 | + const str = new TextDecoder().decode(chunk); |
| 24 | + receivedChunks.push(str); |
| 25 | + }, |
| 26 | + }); |
| 27 | + |
| 28 | + fromPeer = new ReadableStream({ |
| 29 | + start(controller) { |
| 30 | + peerController = controller; |
| 31 | + }, |
| 32 | + }); |
| 33 | + |
| 34 | + handler = vi.fn(); |
| 35 | + connection = new Connection(handler, toPeer, fromPeer); |
| 36 | + }); |
| 37 | + |
| 38 | + afterEach(() => { |
| 39 | + vi.clearAllMocks(); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should send a request and receive a response', async () => { |
| 43 | + const responsePromise = connection.sendRequest('testMethod', { |
| 44 | + key: 'value', |
| 45 | + }); |
| 46 | + |
| 47 | + // Verify request was sent |
| 48 | + await vi.waitFor(() => { |
| 49 | + expect(receivedChunks.length).toBeGreaterThan(0); |
| 50 | + }); |
| 51 | + const request = JSON.parse(receivedChunks[0]); |
| 52 | + expect(request).toMatchObject({ |
| 53 | + jsonrpc: '2.0', |
| 54 | + method: 'testMethod', |
| 55 | + params: { key: 'value' }, |
| 56 | + }); |
| 57 | + expect(request.id).toBeDefined(); |
| 58 | + |
| 59 | + // Simulate response |
| 60 | + const response = { |
| 61 | + jsonrpc: '2.0', |
| 62 | + id: request.id, |
| 63 | + result: { success: true }, |
| 64 | + }; |
| 65 | + peerController.enqueue( |
| 66 | + new TextEncoder().encode(JSON.stringify(response) + '\n'), |
| 67 | + ); |
| 68 | + |
| 69 | + const result = await responsePromise; |
| 70 | + expect(result).toEqual({ success: true }); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should send a notification', async () => { |
| 74 | + await connection.sendNotification('notifyMethod', { key: 'value' }); |
| 75 | + |
| 76 | + await vi.waitFor(() => { |
| 77 | + expect(receivedChunks.length).toBeGreaterThan(0); |
| 78 | + }); |
| 79 | + const notification = JSON.parse(receivedChunks[0]); |
| 80 | + expect(notification).toMatchObject({ |
| 81 | + jsonrpc: '2.0', |
| 82 | + method: 'notifyMethod', |
| 83 | + params: { key: 'value' }, |
| 84 | + }); |
| 85 | + expect(notification.id).toBeUndefined(); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should handle incoming requests', async () => { |
| 89 | + handler.mockResolvedValue({ result: 'ok' }); |
| 90 | + |
| 91 | + const request = { |
| 92 | + jsonrpc: '2.0', |
| 93 | + id: 1, |
| 94 | + method: 'incomingMethod', |
| 95 | + params: { foo: 'bar' }, |
| 96 | + }; |
| 97 | + peerController.enqueue( |
| 98 | + new TextEncoder().encode(JSON.stringify(request) + '\n'), |
| 99 | + ); |
| 100 | + |
| 101 | + // Wait for handler to be called and response to be written |
| 102 | + await vi.waitFor(() => { |
| 103 | + expect(handler).toHaveBeenCalledWith('incomingMethod', { foo: 'bar' }); |
| 104 | + expect(receivedChunks.length).toBeGreaterThan(0); |
| 105 | + }); |
| 106 | + |
| 107 | + const response = JSON.parse(receivedChunks[receivedChunks.length - 1]); |
| 108 | + expect(response).toMatchObject({ |
| 109 | + jsonrpc: '2.0', |
| 110 | + id: 1, |
| 111 | + result: { result: 'ok' }, |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + it('should handle incoming notifications', async () => { |
| 116 | + const notification = { |
| 117 | + jsonrpc: '2.0', |
| 118 | + method: 'incomingNotify', |
| 119 | + params: { foo: 'bar' }, |
| 120 | + }; |
| 121 | + peerController.enqueue( |
| 122 | + new TextEncoder().encode(JSON.stringify(notification) + '\n'), |
| 123 | + ); |
| 124 | + |
| 125 | + // Wait for handler to be called |
| 126 | + await vi.waitFor(() => { |
| 127 | + expect(handler).toHaveBeenCalledWith('incomingNotify', { foo: 'bar' }); |
| 128 | + }); |
| 129 | + // Notifications don't send responses |
| 130 | + expect(receivedChunks.length).toBe(0); |
| 131 | + }); |
| 132 | + |
| 133 | + it('should handle request errors from handler', async () => { |
| 134 | + handler.mockRejectedValue(new Error('Handler failed')); |
| 135 | + |
| 136 | + const request = { |
| 137 | + jsonrpc: '2.0', |
| 138 | + id: 2, |
| 139 | + method: 'failMethod', |
| 140 | + }; |
| 141 | + peerController.enqueue( |
| 142 | + new TextEncoder().encode(JSON.stringify(request) + '\n'), |
| 143 | + ); |
| 144 | + |
| 145 | + await vi.waitFor(() => { |
| 146 | + expect(receivedChunks.length).toBeGreaterThan(0); |
| 147 | + }); |
| 148 | + |
| 149 | + const response = JSON.parse(receivedChunks[receivedChunks.length - 1]); |
| 150 | + expect(response).toMatchObject({ |
| 151 | + jsonrpc: '2.0', |
| 152 | + id: 2, |
| 153 | + error: { |
| 154 | + code: -32603, |
| 155 | + message: 'Internal error', |
| 156 | + data: { details: 'Handler failed' }, |
| 157 | + }, |
| 158 | + }); |
| 159 | + }); |
| 160 | + |
| 161 | + it('should handle RequestError from handler', async () => { |
| 162 | + handler.mockRejectedValue(RequestError.methodNotFound('Unknown method')); |
| 163 | + |
| 164 | + const request = { |
| 165 | + jsonrpc: '2.0', |
| 166 | + id: 3, |
| 167 | + method: 'unknown', |
| 168 | + }; |
| 169 | + peerController.enqueue( |
| 170 | + new TextEncoder().encode(JSON.stringify(request) + '\n'), |
| 171 | + ); |
| 172 | + |
| 173 | + await vi.waitFor(() => { |
| 174 | + expect(receivedChunks.length).toBeGreaterThan(0); |
| 175 | + }); |
| 176 | + |
| 177 | + const response = JSON.parse(receivedChunks[receivedChunks.length - 1]); |
| 178 | + expect(response).toMatchObject({ |
| 179 | + jsonrpc: '2.0', |
| 180 | + id: 3, |
| 181 | + error: { |
| 182 | + code: -32601, |
| 183 | + message: 'Method not found', |
| 184 | + data: { details: 'Unknown method' }, |
| 185 | + }, |
| 186 | + }); |
| 187 | + }); |
| 188 | + |
| 189 | + it('should handle response errors', async () => { |
| 190 | + const responsePromise = connection.sendRequest('testMethod'); |
| 191 | + |
| 192 | + // Verify request was sent |
| 193 | + await vi.waitFor(() => { |
| 194 | + expect(receivedChunks.length).toBeGreaterThan(0); |
| 195 | + }); |
| 196 | + const request = JSON.parse(receivedChunks[0]); |
| 197 | + |
| 198 | + // Simulate error response |
| 199 | + const response = { |
| 200 | + jsonrpc: '2.0', |
| 201 | + id: request.id, |
| 202 | + error: { |
| 203 | + code: -32000, |
| 204 | + message: 'Custom error', |
| 205 | + }, |
| 206 | + }; |
| 207 | + peerController.enqueue( |
| 208 | + new TextEncoder().encode(JSON.stringify(response) + '\n'), |
| 209 | + ); |
| 210 | + |
| 211 | + await expect(responsePromise).rejects.toMatchObject({ |
| 212 | + code: -32000, |
| 213 | + message: 'Custom error', |
| 214 | + }); |
| 215 | + }); |
| 216 | +}); |
0 commit comments