raw-markdown.spec.ts 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /** Raw requests must not replace Vite's page-module responses. */
  2. import type { IncomingMessage, ServerResponse } from 'node:http'
  3. import { beforeEach, describe, expect, it, vi } from 'vitest'
  4. import { rawMarkdownRoute } from '../../scripts/project-doc-site.ts'
  5. import { rawMarkdownMiddleware } from '../raw-markdown.ts'
  6. vi.mock('../../scripts/project-doc-site.ts', () => ({ rawMarkdownRoute: vi.fn() }))
  7. function request(url: string | undefined, destination?: string, method = 'GET', base = '/') {
  8. const req = { url, method, headers: { 'sec-fetch-dest': destination } } as IncomingMessage
  9. const res = { statusCode: 200, setHeader: vi.fn(), end: vi.fn() }
  10. const next = vi.fn()
  11. rawMarkdownMiddleware(base, () => '# Index\n')(req, res as unknown as ServerResponse, next)
  12. return { res, next }
  13. }
  14. beforeEach(() => {
  15. vi.mocked(rawMarkdownRoute).mockReset().mockImplementation(path => path === 'en/reference/index.md' ? '# Reference\n' : undefined)
  16. })
  17. describe('raw Markdown development middleware', () => {
  18. it.each([undefined, 'document', 'empty'])('serves an explicit raw fetch with destination %s', (destination) => {
  19. const { res, next } = request('/deepseek-harness/en/reference/index.md?dsh-raw=1#section', destination, 'GET', '/deepseek-harness/')
  20. expect(rawMarkdownRoute).toHaveBeenCalledWith('en/reference/index.md')
  21. expect(res.setHeader).toHaveBeenCalledWith('Content-Type', 'text/markdown; charset=utf-8')
  22. expect(res.end).toHaveBeenCalledWith('# Reference\n')
  23. expect(next).not.toHaveBeenCalled()
  24. })
  25. it.each([undefined, 'document'])('retains ordinary raw navigation with destination %s', (destination) => {
  26. expect(request('/en/reference/index.md', destination).res.end).toHaveBeenCalledWith('# Reference\n')
  27. })
  28. it.each([
  29. ['/en/reference/index.md', 'script'],
  30. ['/en/reference/index.md?dsh-raw=1', 'script'],
  31. ['/en/reference/index.md', 'empty'],
  32. ['/en/reference/index.md?dsh-raw=0', 'empty'],
  33. ['/en/reference/index.md?dsh-raw=1', 'style'],
  34. ['/en/reference/index.md?dsh-raw=1', 'image'],
  35. ])('delegates %s (%s) to Vite', (url, destination) => {
  36. const { res, next } = request(url, destination)
  37. expect(next).toHaveBeenCalledExactlyOnceWith()
  38. expect(res.end).not.toHaveBeenCalled()
  39. expect(rawMarkdownRoute).not.toHaveBeenCalled()
  40. })
  41. it.each(['/missing.md', '/en/reference.md'])('returns 404 for explicit unpublished route %s', (url) => {
  42. const { res, next } = request(`${url}?dsh-raw=1`, 'empty')
  43. expect(res.statusCode).toBe(404)
  44. expect(res.end).toHaveBeenCalledExactlyOnceWith()
  45. expect(next).not.toHaveBeenCalled()
  46. })
  47. it('preserves llms.txt and omits bodies for HEAD', () => {
  48. expect(request('/llms.txt').res.end).toHaveBeenCalledWith('# Index\n')
  49. for (const url of ['/llms.txt', '/en/reference/index.md?dsh-raw=1']) {
  50. const { res } = request(url, undefined, 'HEAD')
  51. expect(res.setHeader).toHaveBeenCalledOnce()
  52. expect(res.end).toHaveBeenCalledWith(undefined)
  53. }
  54. })
  55. it('delegates missing paths, unrelated routes, other bases and non-read methods', () => {
  56. for (const { next } of [
  57. request(undefined), request('/missing.md'), request('/guide.html'),
  58. request('/en/reference/index.md?dsh-raw=1', 'empty', 'GET', '/deepseek-harness/'),
  59. request('/en/reference/index.md?dsh-raw=1', 'empty', 'POST'),
  60. ]) expect(next).toHaveBeenCalledExactlyOnceWith()
  61. })
  62. it.each(['http://[', 'http://[::1'])('delegates an unparseable request target %s', (url) => {
  63. const { res, next } = request(url, 'empty')
  64. expect(next).toHaveBeenCalledExactlyOnceWith()
  65. expect(res.end).not.toHaveBeenCalled()
  66. expect(rawMarkdownRoute).not.toHaveBeenCalled()
  67. })
  68. it('does not turn a projection error into a successful raw response', () => {
  69. vi.mocked(rawMarkdownRoute).mockImplementation(() => { throw new Error('Source missing') })
  70. expect(() => request('/en/reference/index.md?dsh-raw=1', 'empty')).toThrow('Source missing')
  71. })
  72. })