import './_planet.mjs';
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { FakeEl, installFakeDom } from './fakedom.mjs';
installFakeDom();
const { SearchBox } = await import('../js/searchbox.js');
const tick = () => new Promise(r => setTimeout(r, 0));
function box(pins = [], locate = async () => ({ lat: 1, lon: 2 })) {
const input = new FakeEl('input'), list = new FakeEl('div'), went = [];
new SearchBox(input, list, { pins: () => pins, locate, go: r => went.push(r) });
return { input, list, went };
}
test('typing suggests; Enter flies to coordinates with a marker', async () => {
const { input, list, went } = box();
input.value = '40N 10W';
input.fire('input');
assert.equal(list.hidden, false);
assert.equal(list.kids.length, 1);
input.fire('keydown', { key: 'Enter' });
await tick();
assert.deepEqual([went[0].lat, went[0].lon, went[0].marker], [40, -10, true]);
assert.equal(list.hidden, true);
});
test('arrows move the selection; pins fly without a marker', async () => {
const pins = [{ name: 'Alpha', lat: 3, lon: 4, lore: null, note: '' }, { name: 'Alpine', lat: 5, lon: 6, lore: null, note: '' }];
const { input, list, went } = box(pins);
input.value = 'alp';
input.fire('input');
assert.equal(list.kids.length, 2);
input.fire('keydown', { key: 'ArrowDown' });
assert.match(list.kids[1].className, /sel/);
input.fire('keydown', { key: 'Enter' });
await tick();
assert.deepEqual([went[0].lat, went[0].lon, went[0].marker], [5, 6, false]);
});
test('cell ids go through the server; failures are reported', async () => {
const ok = box([], async cell => ({ cell, lat: -20, lon: 100 }));
ok.input.value = '8528308ffffffff';
ok.input.fire('input');
ok.input.fire('keydown', { key: 'Enter' });
await tick();
assert.deepEqual([ok.went[0].lat, ok.went[0].lon], [-20, 100]);
const bad = box([], async () => { throw new Error('nope'); });
bad.input.value = '8528308ffffffff';
bad.input.fire('input');
bad.input.fire('keydown', { key: 'Enter' });
await tick();
assert.equal(bad.went[0].error, 'nope');
});
test('Esc closes the list; junk shows nothing', () => {
const { input, list } = box();
input.value = '95 10';
input.fire('input');
assert.equal(list.hidden, true);
input.value = '40N 10W';
input.fire('input');
input.fire('keydown', { key: 'Escape' });
assert.equal(list.hidden, true);
});