import './_planet.mjs';
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { samplesFor, squareOutline, exportSizeMB, exportStatusText } from '../js/exports.js';
test('samples: the smallest 2^n + 1 covering the size (as the server)', () => {
assert.equal(samplesFor(40, 10), 4097);
assert.equal(samplesFor(100, 2000), 65);
assert.equal(samplesFor(5, 20), 257);
});
test('the square outline: corners size/√2 from the centre, closed', () => {
const R = 12742, pts = squareOutline(0, 0, 100);
assert.deepEqual(pts[0], pts[pts.length - 1]);
const d = p => R * Math.acos(Math.cos(p.lat * Math.PI / 180) * Math.cos(p.lon * Math.PI / 180));
assert.ok(Math.abs(d(pts[0]) - 50 * Math.SQRT2) < 0.05);
const north = pts.reduce((a, b) => (b.lat > a.lat ? b : a));
assert.ok(Math.abs(north.lat * Math.PI / 180 * R - 50) < 0.1, 'the north edge 50 km up');
});
test('size estimate and status', () => {
assert.ok(exportSizeMB(4097) > 100 && exportSizeMB(4097) < 200, "the island export was 128 MB");
assert.equal(exportStatusText({ state: 'running', name: 'x', fraction: 0.5 }), 'Exporting x… 50%');
assert.equal(exportStatusText({ state: 'failed', name: 'x', error: 'boom' }), 'Export x failed: boom');
assert.equal(exportStatusText({ state: 'idle' }), '');
});
test('polling while an export runs keeps what is being typed', async () => {
const { installFakeDom, FakeEl } = await import('./fakedom.mjs');
installFakeDom();
let calls = 0;
globalThis.fetch = async () => ({ ok: true, status: 200, headers: { get: () => 'application/json' },
json: async () => ({ exports: [], status: { state: 'running', name: 'x', fraction: 0.1 * ++calls } }) });
const { ExportPanel } = await import('../js/exportpanel.js');
const box = new FakeEl('section');
const p = new ExportPanel(box, { getCenter: () => ({ lat: 1, lon: 2 }), onSquare: () => {} });
p.visible = true;
await p.load();
const input = box.find(e => e.tag === 'input');
input.value = 'my-area';
await p.load(); // a poll
clearTimeout(p.timer);
assert.equal(box.find(e => e.tag === 'input'), input, 'the same input, not rebuilt');
assert.equal(input.value, 'my-area');
assert.match(box.textContent, /Exporting x… 20%/);
});