import assert from "node:assert/strict"; import {after, before, test} from "node:test"; import {createServer} from "./support/vite-server.mjs"; let server, service, health; const fetchOriginal = globalThis.fetch; before(async () => { server = await createServer({appType: "custom", logLevel: "silent", server: {middlewareMode: true}}); service = await server.ssrLoadModule("/src/core/map/cesiumService.ts"); health = await server.ssrLoadModule("/src/core/map/useMapGatewayHealth.ts"); }); after(async () => {globalThis.fetch = fetchOriginal; await server?.close();}); test("health distinguishes direct provider from an actual NAS cache", () => { const value = health.decodeHealth({ok: true, service: "missioncore-map-direct", route: "direct", cache: null}); assert.equal(value.cache, null); assert.equal(value.ionConfigured, true); assert.throws(() => health.decodeHealth({ok: false, service: "missioncore-map-direct", route: "direct"})); const nas = health.decodeHealth({ok: true, service: "nodedc-map-gateway", ionConfigured: true, assetAllowlist: [1, 2, 96188], cache: {mode: "online", writePolicy: "read-through", entries: 12, bytes: 30, maxBytes: null, atCapacity: false, persistent: true}}); assert.equal(nas.cache.entries, 12); }); test("settings save uses body-only token and revision, then refreshes maps", async () => { const previousWindow = globalThis.window; const events = []; globalThis.window = {dispatchEvent: event => events.push(event.type)}; globalThis.fetch = async (url, options) => { assert.equal(url, "/api/v1/map/services/cesium-ion"); assert.equal(options.method, "PUT"); assert.equal(options.cache, "no-store"); assert.deepEqual(JSON.parse(options.body), {revision: 7, mode: "auto", token: "candidate"}); return new Response(JSON.stringify({revision: 8, mode: "auto", token_configured: true})); }; try { const value = await service.saveCesiumService({revision: 7, mode: "auto", token: "candidate"}); assert.equal(value.revision, 8); assert.deepEqual(events, [service.cesiumServiceChanged]); } finally {globalThis.window = previousWindow;} }); test("failed settings write neither exposes upstream body nor announces success", async () => { globalThis.fetch = async () => new Response(JSON.stringify({code: "cesium_provider_unauthorized", detail: "private-credential"}), {status: 401}); await assert.rejects(service.saveCesiumService({revision: 7, mode: "auto", token: "candidate"}), error => /отклонил токен/.test(error.message) && !error.message.includes("private-credential")); }); test("token lamp distinguishes saved, pending, verified, rejected and new draft", () => { const base = {configured: false, draft: false, checking: false, error: "", verification: null}; assert.equal(service.cesiumTokenIndicator(base).tone, "neutral"); assert.equal(service.cesiumTokenIndicator({...base, configured: true}).tone, "neutral"); assert.deepEqual(service.cesiumTokenIndicator({...base, checking: true}), { tone: "warning", pulse: true, label: "Проверка доступа Cesium ion", }); assert.equal(service.cesiumTokenIndicator({...base, verification: {ok: true, assets: {}}}).tone, "success"); assert.match(service.cesiumTokenIndicator({...base, verification: {ok: false, assets: {"1": {ok: false, code: "cesium_provider_unauthorized"}}}}).label, /отклонил токен/); assert.equal(service.cesiumTokenIndicator({...base, error: "Нет соединения"}).tone, "warning"); assert.match(service.cesiumTokenIndicator({...base, configured: true, draft: true}).label, /ещё не проверен/); });