mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 04:44:21 +01:00
refactor: clean up preview-server implementation and tests
This commit is contained in:
1 parent
a214212b7b
commit
1e00e68e76
2 files changed
+128
-112
No files matched your search
@@ -1,26 +1,23 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { buildPreviewUpgradeRequest } from '../../../src/services/dev-server/preview-server'
|
||||
import {
|
||||
buildUpstreamWebSocketUrl,
|
||||
selectWebSocketProtocol,
|
||||
} from '../../../src/services/dev-server/preview-server'
|
||||
|
||||
describe('buildPreviewUpgradeRequest', () => {
|
||||
it('preserves the request line and forces the loopback host', () => {
|
||||
const rawHead = [
|
||||
'GET /_next/webpack-hmr HTTP/1.1',
|
||||
'Host: manager.example:3056',
|
||||
'Upgrade: websocket',
|
||||
'Connection: Upgrade',
|
||||
].join('\r\n')
|
||||
|
||||
const result = buildPreviewUpgradeRequest(rawHead, 3055)
|
||||
const lines = result.split('\r\n')
|
||||
|
||||
expect(lines[0]).toBe('GET /_next/webpack-hmr HTTP/1.1')
|
||||
expect(result).toContain('Upgrade: websocket')
|
||||
expect(result).toContain('Connection: Upgrade')
|
||||
expect(result).toContain('Host: 127.0.0.1:3055')
|
||||
expect(result).not.toContain('Host: manager.example:3056')
|
||||
})
|
||||
|
||||
it('returns the raw head unchanged when empty', () => {
|
||||
expect(buildPreviewUpgradeRequest('', 3055)).toBe('')
|
||||
describe('buildUpstreamWebSocketUrl', () => {
|
||||
it('targets the loopback dev server with path and search', () => {
|
||||
expect(buildUpstreamWebSocketUrl(3055, '/_next/webpack-hmr', '?token=abc')).toBe(
|
||||
'ws://127.0.0.1:3055/_next/webpack-hmr?token=abc'
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe('selectWebSocketProtocol', () => {
|
||||
it('selects the first offered protocol', () => {
|
||||
expect(selectWebSocketProtocol('vite-hmr, other')).toBe('vite-hmr')
|
||||
})
|
||||
|
||||
it('returns null when no protocol is offered', () => {
|
||||
expect(selectWebSocketProtocol(null)).toBeNull()
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user