node-half.spec.ts 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /** Node half: registers the /api prefix route bridging to the api gateway. */
  2. import { EventEmitter } from 'node:events'
  3. import { Readable } from 'node:stream'
  4. import { Context } from 'cordis'
  5. import { describe, expect, it } from 'vitest'
  6. import type { IncomingMessage, ServerResponse } from 'node:http'
  7. import type { ApiProxy } from '@deepseek-ai/dsh-host-apiproxy/api'
  8. import type { HttpServerService, WebRoute } from '@deepseek-ai/dsh-host-webserver'
  9. import { API_PATH, apply, inject } from '../src/index.ts'
  10. /** Structural httpServer fake: the plugin only touches register(). */
  11. function fakeHttpServer(routes: WebRoute[]): Pick<HttpServerService, 'register' | 'tapIndex' | 'port'> {
  12. return {
  13. register(route) {
  14. routes.push(route)
  15. return () => { routes.splice(routes.indexOf(route), 1) }
  16. },
  17. tapIndex: () => () => {},
  18. port: 0,
  19. }
  20. }
  21. /** Bodyless GET carrying the given headers (enough for the trust fence + bridge). */
  22. function fakeRequest(headers: Record<string, string>): IncomingMessage {
  23. const request = Readable.from([]) as unknown as IncomingMessage
  24. Object.assign(request, { url: `${API_PATH}/session.list`, method: 'GET', headers })
  25. return request
  26. }
  27. /** Response recorder compatible with both the fence's short-circuit and the bridge. */
  28. function fakeResponse(): { response: ServerResponse; state: { status?: number; body?: unknown } } {
  29. const state: { status?: number; body?: unknown } = {}
  30. const response = Object.assign(new EventEmitter(), {
  31. writableEnded: false,
  32. writeHead(value: number) { state.status = value; return this },
  33. write() { return true },
  34. end(this: { writableEnded: boolean }, value?: unknown) {
  35. if (value !== undefined) state.body = value
  36. this.writableEnded = true
  37. return this
  38. },
  39. }) as unknown as ServerResponse
  40. return { response, state }
  41. }
  42. async function mounted(config?: { trustedHosts?: string[] }): Promise<{ routes: WebRoute[]; dispose: () => Promise<void> }> {
  43. const ctx = new Context()
  44. const routes: WebRoute[] = []
  45. ctx.provide('httpServer', fakeHttpServer(routes) as HttpServerService)
  46. ctx.provide('apiProxy', {} as unknown as ApiProxy)
  47. const fiber = ctx.plugin({ inject: [...inject], apply }, config)
  48. await fiber.await()
  49. return { routes, dispose: () => fiber.dispose() }
  50. }
  51. describe('connection node half', () => {
  52. it('registers the /api prefix route and removes it with the fiber', async () => {
  53. const { routes, dispose } = await mounted()
  54. expect(routes).toHaveLength(1)
  55. expect(routes[0]).toMatchObject({ kind: 'prefix', path: API_PATH })
  56. await dispose()
  57. expect(routes).toHaveLength(0)
  58. })
  59. it('refuses an untrusted Host on any /api path before the bridge runs', async () => {
  60. const { routes, dispose } = await mounted()
  61. const { response, state } = fakeResponse()
  62. await routes[0]!.handler(fakeRequest({
  63. host: 'harness.example', origin: 'http://harness.example', 'sec-fetch-site': 'same-origin',
  64. }), response)
  65. expect(state.status).toBe(403)
  66. expect(state.body).toBe('forbidden')
  67. await dispose()
  68. })
  69. it('passes loopback and declared-authority requests through to the bridge', async () => {
  70. const { routes, dispose } = await mounted({ trustedHosts: ['harness.example:3080'] })
  71. // Loopback, no browser markers (curl shape): the fence passes; the carrier
  72. // answers 404 for a GET unary path — proof the bridge ran.
  73. const loopback = fakeResponse()
  74. await routes[0]!.handler(fakeRequest({ host: '127.0.0.1:3080' }), loopback.response)
  75. expect(loopback.state.status).toBe(404)
  76. // Declared public authority, same-origin browser shape.
  77. const declared = fakeResponse()
  78. await routes[0]!.handler(fakeRequest({
  79. host: 'harness.example:3080', origin: 'http://harness.example:3080', 'sec-fetch-site': 'same-origin',
  80. }), declared.response)
  81. expect(declared.state.status).toBe(404)
  82. await dispose()
  83. })
  84. })