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'), '
Earth', '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) }) test('forwards Accept-Encoding, If-None-Match, and AbortSignal and preserves 304 null-body responses', async () => { const capturedHeaders: Array