Files
planet/frontend/tests/satellite-refresh-preservation.test.ts
T
rayd1o 30b44ec00d
ci / backend (push) Waiting to run
ci / frontend (push) Waiting to run
release: bump version to 0.84.2
2026-12-05 01:56:03 +08:00

115 lines
3.6 KiB
TypeScript

import { afterEach, describe, expect, mock, test } from 'bun:test'
import { readFileSync } from 'node:fs'
mock.module('satellite.js', () => ({
twoline2satrec: () => ({ satnum: '25544' }),
propagate: () => ({ position: { x: 6800, y: 0, z: 0 } }),
eciToEcf: (position: { x: number; y: number; z: number }) => position,
gstime: () => 0,
}))
const COMPACT_PAYLOAD = {
format: 'satellite_compact_v1',
count: 2,
fields: [
'id',
'norad_cat_id',
'name',
'constellation_group',
'tle_line1',
'tle_line2',
'fallback_elements',
],
satellites: [
[
1,
25544,
'ISS (ZARYA)',
'space_station',
'1 25544U 98067A 26338.50000000 .00016717 00000-0 10270-3 0 9005',
'2 25544 51.6400 200.0000 0005000 100.0000 260.0000 15.50000000123456',
null,
],
[
2,
44713,
'STARLINK-1007',
'starlink',
'1 44713U 19074A 26338.50000000 .00001000 00000-0 10000-3 0 9001',
'2 44713 53.0000 120.0000 0001500 90.0000 270.0000 15.06000000123456',
null,
],
],
}
describe('satellite refresh preservation', () => {
const originalWindow = globalThis.window
const originalFetch = globalThis.fetch
afterEach(async () => {
const { clearSatelliteData } = await import('../public/earth/js/satellites.js')
clearSatelliteData()
globalThis.window = originalWindow
globalThis.fetch = originalFetch
})
test('preserves rendered satellites when forced refresh fails and supports 304 ETag reuse', async () => {
const { getSatelliteCount, loadSatellites } = await import(
'../public/earth/js/satellites.js'
)
globalThis.window = {
location: { origin: 'https://earth.test' },
dispatchEvent: () => true,
addEventListener: () => {},
removeEventListener: () => {},
} as unknown as Window & typeof globalThis
const seenHeaders: Array<Record<string, string>> = []
let mode: 'ok' | 'fail' | '304' = 'ok'
globalThis.fetch = (async (_input: RequestInfo | URL, init?: RequestInit) => {
seenHeaders.push({ ...(init?.headers as Record<string, string> | undefined) })
if (mode === 'fail') {
return new Response('backend unavailable', { status: 502 })
}
if (mode === '304') {
return new Response(null, {
status: 304,
headers: { etag: '"sat-v1"' },
})
}
return new Response(JSON.stringify(COMPACT_PAYLOAD), {
status: 200,
headers: {
'Content-Type': 'application/json',
etag: '"sat-v1"',
},
})
}) as typeof fetch
const initial = await loadSatellites({ skipCache: true })
expect(initial.count).toBe(2)
expect(getSatelliteCount()).toBe(2)
mode = 'fail'
await expect(loadSatellites({ forceNetwork: true, skipCache: true })).rejects.toThrow('HTTP 502')
expect(getSatelliteCount()).toBe(2)
mode = '304'
const revalidated = await loadSatellites({ forceNetwork: true, skipCache: true })
expect(revalidated.notModified).toBe(true)
expect(revalidated.count).toBe(2)
expect(getSatelliteCount()).toBe(2)
expect(seenHeaders[2]?.['If-None-Match']).toBe('"sat-v1"')
})
test('ensureSatellitesEnabled does not clear satellites before loading new data', () => {
const mainSource = readFileSync(new URL('../public/earth/js/main.js', import.meta.url), 'utf8')
const startIdx = mainSource.indexOf('async function ensureSatellitesEnabled(')
expect(startIdx).toBeGreaterThan(-1)
const snippet = mainSource.slice(startIdx, mainSource.indexOf('await loadSatellites(', startIdx))
expect(snippet).not.toContain('clearSatelliteData()')
})
})