Open History to see the last commit
1import { EventEmitter } from 'node:events'
2import { beforeEach, describe, expect, it, vi } from 'vitest'
3import { normalizePath } from 'vite'
4import { handleDevToolsViteRequest, parseOpenSourceParam } from '../src/utils'
5β
6function createMockReq(url?: string) {
7 const emitter = new EventEmitter() as unknown as EventEmitter & {
8 url?: string
9 on: (event: string, listener: (...args: Array<any>) => void) => any
10 }
11 ;(emitter as any).url = url
12 return emitter as any
13}
14β
15function createMockRes() {
16 return {
17 setHeader: vi.fn(),
18 write: vi.fn(),
19 end: vi.fn(),
20 }
21}
22β
23describe('handleDevToolsViteRequest', () => {
24 let next: ReturnType<typeof vi.fn>
25 let cb: ReturnType<typeof vi.fn>
26β
27 beforeEach(() => {
28 next = vi.fn()
29 cb = vi.fn()
30 })
31β
32 it('calls next() when url does not include __tsd', () => {
33 const req = createMockReq('/some/other/path')
34 const res = createMockRes()
35β
36 handleDevToolsViteRequest(req, res as any, next as any, cb as any)
37β
38 expect(next).toHaveBeenCalledTimes(1)
39 expect(cb).not.toHaveBeenCalled()
40 expect(res.setHeader).not.toHaveBeenCalled()
41 expect(res.write).not.toHaveBeenCalled()
42 expect(res.end).not.toHaveBeenCalled()
43 })
44β
45 it('handles __tsd/open-source with valid source and responds/ends', () => {
46 const file = 'src/file.ts'
47 const line = '12'
48 const column = '5'
49 const url = `/__tsd/open-source?source=${encodeURIComponent(`${file}:${line}:${column}`)}`
50β
51 const req = createMockReq(url)
52 const res = createMockRes()
53β
54 handleDevToolsViteRequest(req, res as any, next as any, cb as any)
55β
56 // callback payload
57 expect(cb).toHaveBeenCalledTimes(1)
58 const payload = cb.mock.calls[0]?.[0]
59 expect(payload).toMatchObject({
60 type: 'open-source',
61 routine: 'open-source',
62 })
63 expect(payload.data.line).toBe(line)
64 expect(payload.data.column).toBe(column)
65β
66 const expectedSource = normalizePath(`${process.cwd()}/${file}`)
67 expect(payload.data.source).toBe(expectedSource)
68β
69 // response behavior
70 expect(res.setHeader).toHaveBeenCalledWith('Content-Type', 'text/html')
71 expect(res.write).toHaveBeenCalledWith('<script> window.close(); </script>')
72 expect(res.end).toHaveBeenCalled()
73β
74 // next() is not called for handled route
75 expect(next).not.toHaveBeenCalled()
76 })
77β
78 it('does nothing for __tsd/open-source when source is missing', () => {
79 const req = createMockReq('/__tsd/open-source')
80 const res = createMockRes()
81β
82 handleDevToolsViteRequest(req, res as any, next as any, cb as any)
83β
84 expect(cb).not.toHaveBeenCalled()
85 expect(res.setHeader).not.toHaveBeenCalled()
86 expect(res.write).not.toHaveBeenCalled()
87 expect(res.end).not.toHaveBeenCalled()
88 expect(next).not.toHaveBeenCalled()
89 })
90β
91 it('does nothing for __tsd/open-source when source is malformed', () => {
92 const malformed = encodeURIComponent('src/file.ts:abc:def')
93 const req = createMockReq(`/__tsd/open-source?source=${malformed}`)
94 const res = createMockRes()
95β
96 handleDevToolsViteRequest(req, res as any, next as any, cb as any)
97β
98 expect(cb).not.toHaveBeenCalled()
99 expect(res.setHeader).not.toHaveBeenCalled()
100 expect(res.write).not.toHaveBeenCalled()
101 expect(res.end).not.toHaveBeenCalled()
102 expect(next).not.toHaveBeenCalled()
103 })
104β
105 it('parses JSON body for other __tsd requests and writes OK', () => {
106 const req = createMockReq('/__tsd/some-endpoint')
107 const res = createMockRes()
108β
109 handleDevToolsViteRequest(req, res as any, next as any, cb as any)
110β
111 // simulate streaming body
112 const chunks = [Buffer.from('{"foo":'), Buffer.from('1}')]
113 req.emit('data', chunks[0])
114 req.emit('data', chunks[1])
115 req.emit('end')
116β
117 expect(cb).toHaveBeenCalledTimes(1)
118 expect(cb).toHaveBeenCalledWith({ foo: 1 })
119 expect(res.write).toHaveBeenCalledWith('OK')
120 expect(res.end).toHaveBeenCalled()
121β
122 // these are not used in this branch
123 expect(res.setHeader).not.toHaveBeenCalled()
124 expect(next).not.toHaveBeenCalled()
125 })
126β
127 it('swallows JSON parse errors but still writes OK', () => {
128 const req = createMockReq('/__tsd/another')
129 const res = createMockRes()
130β
131 handleDevToolsViteRequest(req, res as any, next as any, cb as any)
132 req.emit('data', Buffer.from('{ invalid json'))
133 req.emit('end')
134β
135 expect(cb).not.toHaveBeenCalled()
136 expect(res.write).toHaveBeenCalledWith('OK')
137 expect(res.end).toHaveBeenCalled()
138 })
139})
140β
141describe('parseOpenSourceParam', () => {
142 it('parses simple filename foo.tsx with line/column', () => {
143 const input = 'foo.tsx:10:20'
144 const parsed = parseOpenSourceParam(input)
145 expect(parsed).toEqual({ file: 'foo.tsx', line: '10', column: '20' })
146 })
147β
148 it('parses filename containing colon bar:baz.tsx with line/column', () => {
149 const input = 'bar:baz.tsx:3:7'
150 const parsed = parseOpenSourceParam(input)
151 expect(parsed).toEqual({ file: 'bar:baz.tsx', line: '3', column: '7' })
152 })
153})