|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#24287] `toResponse`'s events arm writes a string event as given, and a |
| 5 | + * producer's own header replaces the default it names. |
| 6 | + * |
| 7 | + * The standalone server's writer (`runtime/src/dispatcher-plugin.ts`, |
| 8 | + * `sendResultBase`) writes a string event verbatim and an object event as one |
| 9 | + * SSE frame, and a route's header replaces the default. This arm SSE-framed |
| 10 | + * every event outside a Vercel data stream and appended the producer's |
| 11 | + * lowercase headers beside its own capitalised defaults, so the `/mcp/skill` |
| 12 | + * domain's single markdown chunk reached the caller as `data: "# skill"` under |
| 13 | + * a doubled `Content-Type` and `no-cache, no-store`. |
| 14 | + * |
| 15 | + * The rows below are the producers that reach this arm: the `/mcp/skill` |
| 16 | + * domain (`runtime/src/domains/mcp.ts`), and the `/ai` domain |
| 17 | + * (`runtime/src/domains/ai.ts`) in both its shapes — a Vercel data stream of |
| 18 | + * pre-encoded string frames, and an SSE stream of object parts. The two `/ai` |
| 19 | + * rows pin answers this card leaves unchanged. The dispatcher is a fixture |
| 20 | + * (this package's config aliases `@objectstack/runtime` to a stub); the same |
| 21 | + * skill row through the real dispatcher and both doors is |
| 22 | + * `packages/qa/http-conformance/src/hono-mcp-skill-raw-body.conformance.test.ts`. |
| 23 | + */ |
| 24 | + |
| 25 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 26 | + |
| 27 | +const mockDispatcher = { |
| 28 | + getDiscoveryInfo: vi.fn().mockReturnValue({ version: '1.0', routes: {} }), |
| 29 | + handleAuth: vi.fn(), |
| 30 | + dispatch: vi.fn(), |
| 31 | +}; |
| 32 | + |
| 33 | +vi.mock('@objectstack/runtime', () => ({ |
| 34 | + HttpDispatcher: function HttpDispatcher() { return mockDispatcher; }, |
| 35 | +})); |
| 36 | + |
| 37 | +import { createHonoApp } from './index'; |
| 38 | + |
| 39 | +const PREFIX = '/api/v1'; |
| 40 | +const kernel = { name: 'test-kernel' } as any; |
| 41 | + |
| 42 | +async function* events(...items: unknown[]) { |
| 43 | + for (const item of items) yield item; |
| 44 | +} |
| 45 | + |
| 46 | +async function get(result: Record<string, unknown>): Promise<Response> { |
| 47 | + mockDispatcher.dispatch.mockResolvedValue({ handled: true, result: { type: 'stream', ...result } }); |
| 48 | + const app = createHonoApp({ kernel, prefix: PREFIX, cors: false }); |
| 49 | + return app.request(`http://hosted.test${PREFIX}/some/stream`); |
| 50 | +} |
| 51 | + |
| 52 | +beforeEach(() => { |
| 53 | + mockDispatcher.dispatch.mockReset(); |
| 54 | +}); |
| 55 | + |
| 56 | +describe('[#24287] toResponse events arm: string events verbatim, producer headers replace defaults', () => { |
| 57 | + it('the /mcp/skill shape: the markdown verbatim, and each declared header once', async () => { |
| 58 | + const markdown = '---\nname: objectstack\n---\n\n# skill "quoted"\n'; |
| 59 | + const res = await get({ |
| 60 | + status: 200, |
| 61 | + contentType: 'text/markdown; charset=utf-8', |
| 62 | + headers: { |
| 63 | + 'content-type': 'text/markdown; charset=utf-8', |
| 64 | + 'content-disposition': 'inline; filename="SKILL.md"', |
| 65 | + 'cache-control': 'no-store', |
| 66 | + }, |
| 67 | + events: events(markdown), |
| 68 | + }); |
| 69 | + |
| 70 | + expect(res.status).toBe(200); |
| 71 | + expect(await res.text()).toBe(markdown); |
| 72 | + expect(res.headers.get('content-type')).toBe('text/markdown; charset=utf-8'); |
| 73 | + expect(res.headers.get('cache-control')).toBe('no-store'); |
| 74 | + expect(res.headers.get('content-disposition')).toBe('inline; filename="SKILL.md"'); |
| 75 | + }); |
| 76 | + |
| 77 | + it('an object event outside a Vercel data stream is still one SSE frame, under the SSE defaults', async () => { |
| 78 | + const res = await get({ contentType: 'text/event-stream', events: events({ type: 'text-delta', text: 'hi' }, { type: 'finish' }) }); |
| 79 | + |
| 80 | + expect(await res.text()).toBe('data: {"type":"text-delta","text":"hi"}\n\ndata: {"type":"finish"}\n\n'); |
| 81 | + expect(res.headers.get('content-type')).toBe('text/event-stream'); |
| 82 | + expect(res.headers.get('cache-control')).toBe('no-cache'); |
| 83 | + expect(res.headers.get('connection')).toBe('keep-alive'); |
| 84 | + }); |
| 85 | + |
| 86 | + it('the /ai SSE shape (object parts, capitalised headers): unchanged', async () => { |
| 87 | + const res = await get({ |
| 88 | + contentType: 'text/event-stream', |
| 89 | + events: events({ type: 'text-delta', id: '0', text: 'a' }), |
| 90 | + headers: { 'Content-Type': 'text/event-stream', 'Cache-Control': 'no-cache', 'Connection': 'keep-alive' }, |
| 91 | + }); |
| 92 | + |
| 93 | + expect(await res.text()).toBe('data: {"type":"text-delta","id":"0","text":"a"}\n\n'); |
| 94 | + expect(res.headers.get('content-type')).toBe('text/event-stream'); |
| 95 | + expect(res.headers.get('cache-control')).toBe('no-cache'); |
| 96 | + }); |
| 97 | + |
| 98 | + it('the /ai Vercel data stream shape (pre-encoded string frames): unchanged', async () => { |
| 99 | + const frames = ['data: {"type":"start"}\n\n', 'data: {"type":"text-delta","id":"0","delta":"a"}\n\n', 'data: [DONE]\n\n']; |
| 100 | + const res = await get({ |
| 101 | + contentType: 'text/plain; charset=utf-8', |
| 102 | + vercelDataStream: true, |
| 103 | + events: events(...frames, { type: 'raw' }), |
| 104 | + headers: { 'Content-Type': 'text/plain; charset=utf-8', 'Cache-Control': 'no-cache', 'Connection': 'keep-alive' }, |
| 105 | + }); |
| 106 | + |
| 107 | + expect(await res.text()).toBe(`${frames.join('')}{"type":"raw"}\n`); |
| 108 | + expect(res.headers.get('content-type')).toBe('text/plain; charset=utf-8'); |
| 109 | + expect(res.headers.get('cache-control')).toBe('no-cache'); |
| 110 | + }); |
| 111 | +}); |
0 commit comments