|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import type { AddressInfo } from 'node:net'; |
| 3 | +import path from 'node:path'; |
| 4 | + |
| 5 | +import { ServiceWorkerApp } from '@eggjs/service-worker'; |
| 6 | +import { afterAll, beforeAll, describe, it } from 'vitest'; |
| 7 | + |
| 8 | +describe('examples/helloworld-service-worker', () => { |
| 9 | + let app: ServiceWorkerApp; |
| 10 | + let base: string; |
| 11 | + |
| 12 | + beforeAll(async () => { |
| 13 | + app = new ServiceWorkerApp(path.join(__dirname, '../app')); |
| 14 | + const server = await app.serve(); |
| 15 | + const { address, port } = server.address() as AddressInfo; |
| 16 | + base = `http://${address}:${port}`; |
| 17 | + }); |
| 18 | + |
| 19 | + afterAll(async () => { |
| 20 | + await app.destroy(); |
| 21 | + }); |
| 22 | + |
| 23 | + it('should say hello over http', async () => { |
| 24 | + const res = await fetch(`${base}/hello/?name=egg`); |
| 25 | + assert.deepEqual(await res.json(), { message: 'hello, egg' }); |
| 26 | + }); |
| 27 | + |
| 28 | + it('should serve the calc MCP tool', async () => { |
| 29 | + const res = await fetch(`${base}/mcp/calc/stream`, { |
| 30 | + method: 'POST', |
| 31 | + headers: { |
| 32 | + accept: 'application/json, text/event-stream', |
| 33 | + 'content-type': 'application/json', |
| 34 | + }, |
| 35 | + body: JSON.stringify({ |
| 36 | + jsonrpc: '2.0', |
| 37 | + id: 1, |
| 38 | + method: 'tools/call', |
| 39 | + params: { name: 'add', arguments: { a: 1, b: 41 } }, |
| 40 | + }), |
| 41 | + }); |
| 42 | + assert.equal(res.status, 200); |
| 43 | + const text = await res.text(); |
| 44 | + assert.match(text, /hello, mcp: 42/); |
| 45 | + }); |
| 46 | +}); |
0 commit comments