|
| 1 | +import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'; |
| 2 | + |
| 3 | +import type { BaseContext, RequestHandlerExtra, ServerContext } from '../../src/shared/protocol.js'; |
| 4 | +import { Protocol } from '../../src/shared/protocol.js'; |
| 5 | +import type { Transport } from '../../src/shared/transport.js'; |
| 6 | +import type { JSONRPCMessage } from '../../src/types/index.js'; |
| 7 | + |
| 8 | +class TestProtocolImpl extends Protocol<BaseContext> { |
| 9 | + protected assertCapabilityForMethod(): void {} |
| 10 | + protected assertNotificationCapability(): void {} |
| 11 | + protected assertRequestHandlerCapability(): void {} |
| 12 | + protected assertTaskCapability(): void {} |
| 13 | + protected assertTaskHandlerCapability(): void {} |
| 14 | + protected buildContext(ctx: BaseContext): BaseContext { |
| 15 | + return ctx; |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +class MockTransport implements Transport { |
| 20 | + onclose?: () => void; |
| 21 | + onerror?: (error: Error) => void; |
| 22 | + onmessage?: (message: unknown) => void; |
| 23 | + |
| 24 | + async start(): Promise<void> {} |
| 25 | + async close(): Promise<void> { |
| 26 | + this.onclose?.(); |
| 27 | + } |
| 28 | + async send(_message: JSONRPCMessage): Promise<void> {} |
| 29 | +} |
| 30 | + |
| 31 | +describe('v1-compat: flat ctx.* getters', () => { |
| 32 | + let protocol: Protocol<BaseContext>; |
| 33 | + let transport: MockTransport; |
| 34 | + let warnSpy: ReturnType<typeof vi.spyOn>; |
| 35 | + |
| 36 | + beforeEach(() => { |
| 37 | + warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 38 | + protocol = new TestProtocolImpl(); |
| 39 | + transport = new MockTransport(); |
| 40 | + }); |
| 41 | + |
| 42 | + afterEach(() => { |
| 43 | + warnSpy.mockRestore(); |
| 44 | + }); |
| 45 | + |
| 46 | + test('flat getters forward to nested fields without warning', async () => { |
| 47 | + await protocol.connect(transport); |
| 48 | + |
| 49 | + let captured: BaseContext | undefined; |
| 50 | + const done = new Promise<void>(resolve => { |
| 51 | + protocol.setRequestHandler('ping', (_request, ctx) => { |
| 52 | + captured = ctx; |
| 53 | + resolve(); |
| 54 | + return {}; |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + transport.onmessage?.({ jsonrpc: '2.0', id: 1, method: 'ping', params: {} }); |
| 59 | + await done; |
| 60 | + |
| 61 | + expect(captured).toBeDefined(); |
| 62 | + const ctx = captured as BaseContext; |
| 63 | + |
| 64 | + expect(ctx.signal).toBe(ctx.mcpReq.signal); |
| 65 | + expect(ctx.requestId).toBe(ctx.mcpReq.id); |
| 66 | + expect(ctx._meta).toBe(ctx.mcpReq._meta); |
| 67 | + expect(ctx.authInfo).toBe(ctx.http?.authInfo); |
| 68 | + expect(ctx.sendNotification).toBe(ctx.mcpReq.notify); |
| 69 | + expect(ctx.sendRequest).toBeTypeOf('function'); |
| 70 | + expect(ctx.taskStore).toBe(ctx.task?.store); |
| 71 | + expect(ctx.taskId).toBe(ctx.task?.id); |
| 72 | + expect(ctx.taskRequestedTtl).toBe(ctx.task?.requestedTtl); |
| 73 | + |
| 74 | + expect(warnSpy).not.toHaveBeenCalled(); |
| 75 | + }); |
| 76 | + |
| 77 | + test('flat getters are non-enumerable (do not pollute spreads)', async () => { |
| 78 | + await protocol.connect(transport); |
| 79 | + |
| 80 | + let captured: BaseContext | undefined; |
| 81 | + const done = new Promise<void>(resolve => { |
| 82 | + protocol.setRequestHandler('ping', (_request, ctx) => { |
| 83 | + captured = ctx; |
| 84 | + resolve(); |
| 85 | + return {}; |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + transport.onmessage?.({ jsonrpc: '2.0', id: 2, method: 'ping', params: {} }); |
| 90 | + await done; |
| 91 | + |
| 92 | + const keys = Object.keys(captured as BaseContext); |
| 93 | + expect(keys).not.toContain('signal'); |
| 94 | + expect(keys).not.toContain('requestId'); |
| 95 | + void { ...(captured as BaseContext) }; |
| 96 | + expect(warnSpy).not.toHaveBeenCalled(); |
| 97 | + }); |
| 98 | + |
| 99 | + test('RequestHandlerExtra<R, N> is a ServerContext alias (type-level)', () => { |
| 100 | + const check = (ctx: ServerContext): RequestHandlerExtra<unknown, unknown> => ctx; |
| 101 | + void check; |
| 102 | + }); |
| 103 | +}); |
0 commit comments