135 lines
5.5 KiB
TypeScript
135 lines
5.5 KiB
TypeScript
import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'
|
|
import os from 'node:os'
|
|
import path from 'node:path'
|
|
|
|
import { afterAll, beforeAll, describe, expect, test } from 'bun:test'
|
|
|
|
import { DesktopConfigStore, deriveBackendWsUrl } from '../src/main/config'
|
|
import { resolveExternalTargetUrl } from '../src/main/external-links'
|
|
import {
|
|
createEarthProtocolHandler,
|
|
createStaticFileResponse,
|
|
isProxiedBackendPath,
|
|
parseByteRange,
|
|
resolveEarthStaticPath,
|
|
} from '../src/main/protocol-core'
|
|
|
|
let tempRoot = ''
|
|
let bundleRoot = ''
|
|
|
|
beforeAll(() => {
|
|
tempRoot = mkdtempSync(path.join(os.tmpdir(), 'planet-client-test-'))
|
|
bundleRoot = path.join(tempRoot, 'earth')
|
|
const dataDir = path.join(bundleRoot, 'data')
|
|
const fs = require('node:fs')
|
|
fs.mkdirSync(dataDir, { recursive: true })
|
|
writeFileSync(path.join(bundleRoot, 'index.html'), '<html><body>Earth</body></html>', 'utf8')
|
|
writeFileSync(path.join(dataDir, 'sample.pmtiles'), '0123456789ABCDEF', 'utf8')
|
|
})
|
|
|
|
afterAll(() => {
|
|
if (tempRoot) {
|
|
rmSync(tempRoot, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
describe('protocol-core static and range handling', () => {
|
|
test('resolves earth entry and blocks directory traversal', () => {
|
|
expect(resolveEarthStaticPath(bundleRoot, '/')).toBe(path.join(bundleRoot, 'index.html'))
|
|
expect(resolveEarthStaticPath(bundleRoot, '/earth/')).toBe(path.join(bundleRoot, 'index.html'))
|
|
expect(resolveEarthStaticPath(bundleRoot, '/earth/data/sample.pmtiles')).toBe(
|
|
path.join(bundleRoot, 'data/sample.pmtiles'),
|
|
)
|
|
expect(resolveEarthStaticPath(bundleRoot, '/earth/../../etc/passwd')).toBeNull()
|
|
expect(resolveEarthStaticPath(bundleRoot, '/..%2f..%2fsecret')).toBeNull()
|
|
})
|
|
|
|
test('parses byte ranges and serves 206 Partial Content for PMTiles', async () => {
|
|
expect(parseByteRange('bytes=0-3', 16)).toEqual({ kind: 'range', start: 0, end: 3 })
|
|
expect(parseByteRange('bytes=10-', 16)).toEqual({ kind: 'range', start: 10, end: 15 })
|
|
expect(parseByteRange('bytes=-4', 16)).toEqual({ kind: 'range', start: 12, end: 15 })
|
|
expect(parseByteRange('bytes=30-40', 16)).toEqual({ kind: 'unsatisfiable' })
|
|
|
|
const pmtilesPath = path.join(bundleRoot, 'data/sample.pmtiles')
|
|
const partial = createStaticFileResponse(pmtilesPath, new Headers({ Range: 'bytes=2-5' }))
|
|
expect(partial.status).toBe(206)
|
|
expect(partial.headers.get('Content-Range')).toBe('bytes 2-5/16')
|
|
expect(partial.headers.get('Accept-Ranges')).toBe('bytes')
|
|
expect(await partial.text()).toBe('2345')
|
|
|
|
const invalid = createStaticFileResponse(pmtilesPath, new Headers({ Range: 'bytes=99-120' }))
|
|
expect(invalid.status).toBe(416)
|
|
})
|
|
|
|
test('proxies /api/*, /health, and /earth-brand-assets/* to backendUrl', async () => {
|
|
expect(isProxiedBackendPath('/api/v1/earth/brand')).toBe(true)
|
|
expect(isProxiedBackendPath('/health')).toBe(true)
|
|
expect(isProxiedBackendPath('/earth-brand-assets/logo.png')).toBe(true)
|
|
expect(isProxiedBackendPath('/earth/index.html')).toBe(false)
|
|
|
|
const seen: string[] = []
|
|
const handler = createEarthProtocolHandler({
|
|
earthBundleRoot: bundleRoot,
|
|
getBackendUrl: () => 'http://127.0.0.1:8000',
|
|
fetchImpl: (async (input: RequestInfo | URL) => {
|
|
seen.push(String(input))
|
|
return Response.json({ ok: true })
|
|
}) as typeof fetch,
|
|
})
|
|
|
|
const apiResp = await handler(new Request('planet://earth/api/v1/earth/brand?lang=zh'))
|
|
expect(apiResp.status).toBe(200)
|
|
expect(await apiResp.json()).toEqual({ ok: true })
|
|
|
|
const brandResp = await handler(new Request('planet://earth/earth-brand-assets/custom.png'))
|
|
expect(brandResp.status).toBe(200)
|
|
|
|
expect(seen).toEqual([
|
|
'http://127.0.0.1:8000/api/v1/earth/brand?lang=zh',
|
|
'http://127.0.0.1:8000/earth-brand-assets/custom.png',
|
|
])
|
|
|
|
const blocked = await handler(new Request('planet://other/earth/index.html'))
|
|
expect(blocked.status).toBe(403)
|
|
})
|
|
})
|
|
|
|
describe('external links and runtime config', () => {
|
|
test('routes admin and docs links externally while keeping earth links internal', () => {
|
|
const opts = { earthOrigin: 'planet://earth', adminUrl: 'http://127.0.0.1:3000/' }
|
|
expect(resolveExternalTargetUrl('planet://earth/admin/users?page=1', opts)).toBe(
|
|
'http://127.0.0.1:3000/admin/users?page=1',
|
|
)
|
|
expect(resolveExternalTargetUrl('planet://earth/docs/manual', opts)).toBe(
|
|
'http://127.0.0.1:3000/docs/manual',
|
|
)
|
|
expect(resolveExternalTargetUrl('planet://earth/earth/index.html', opts)).toBeNull()
|
|
expect(resolveExternalTargetUrl('https://example.org/news', opts)).toBe('https://example.org/news')
|
|
})
|
|
|
|
test('resolves config precedence and persists user endpoints', () => {
|
|
const userDataDir = path.join(tempRoot, 'user-data')
|
|
const exeDir = path.join(tempRoot, 'exe-dir')
|
|
const fs = require('node:fs')
|
|
fs.mkdirSync(exeDir, { recursive: true })
|
|
writeFileSync(
|
|
path.join(exeDir, 'planet-earth.json'),
|
|
JSON.stringify({ backendUrl: 'http://10.0.0.5:8000', adminUrl: 'http://10.0.0.5:3000' }),
|
|
)
|
|
|
|
const store = new DesktopConfigStore({
|
|
userDataDir,
|
|
exeDir,
|
|
argv: ['--backend-url=https://planet.example.com', '--fullscreen', '--fps'],
|
|
env: {},
|
|
})
|
|
const cfg = store.get()
|
|
expect(cfg.backendUrl).toBe('https://planet.example.com')
|
|
expect(cfg.wsUrl).toBe('wss://planet.example.com/ws')
|
|
expect(cfg.adminUrl).toBe('http://10.0.0.5:3000')
|
|
expect(cfg.fullscreen).toBe(true)
|
|
expect(cfg.forceShowFps).toBe(true)
|
|
expect(deriveBackendWsUrl('http://127.0.0.1:8000')).toBe('ws://127.0.0.1:8000/ws')
|
|
})
|
|
})
|