Skip to content

The OSINT investigator's workbench. One case, one folder, every tool. Local-first: your media and your investigations never leave your machine.

JavaScript
6.9 KB
Raw Blame
// @vitest-environment happy-dom
import { describe, expect, it, vi } from 'vitest';
import { flushSync, mount, unmount } from 'svelte';
import AcquisitionPicker from './AcquisitionPicker.svelte';
import { daysBefore } from '../../lib/sentinel.js';

const passes = [
  { date: '2026-05-11', cloud: 3, granules: 2, coverage: 1 },
  { date: '2026-05-08', cloud: 41, granules: 1, coverage: 0.62 },
];

function render(props = {}) {
  const target = document.createElement('div');
  document.body.append(target);
  const live = mount(AcquisitionPicker, {
    target,
    props: {
      list: passes, lookback: 30, searched: true, areas: 1,
      onlookback: () => {}, onlook: () => {}, onpick: () => {}, ...props,
    },
  });
  flushSync();
  return { target, done: () => { unmount(live); target.remove(); } };
}

const rows = (target) => [...target.querySelectorAll('li')];
const button = (target, name) =>
  [...target.querySelectorAll('button')].find((node) => node.textContent.trim() === name);

describe('the acquisition picker', () => {
  it('has no Dates window unless it is handed a way to pick a day', () => {
    const { target, done } = render();
    expect(button(target, 'Dates')).toBeUndefined();
    done();
  });

  it('shows what it is handed under a Dates window, in place of the list and the lookup', async () => {
    const { createRawSnippet } = await import('svelte');
    const dates = createRawSnippet(() => ({ render: () => '<div class="calendars">pick a day</div>' }));
    const onlookback = vi.fn();
    const { target, done } = render({ dates, onlookback });
    expect(button(target, 'Dates')).toBeDefined();
    expect(target.querySelector('.calendars')).toBe(null);
    button(target, 'Dates').click();
    expect(onlookback).toHaveBeenCalledWith('dates');
    done();
    const shown = render({ dates, lookback: 'dates' });
    expect(shown.target.querySelector('.calendars').textContent).toBe('pick a day');
    expect(button(shown.target, 'Dates').getAttribute('aria-pressed')).toBe('true');
    expect(rows(shown.target)).toHaveLength(0);
    expect(button(shown.target, 'Look again')).toBeUndefined();
    shown.done();
  });

  it('lists what exists rather than every day on the calendar', () => {
    const { target, done } = render();
    expect(rows(target)).toHaveLength(2);
    expect(target.querySelector('input[type="date"]')).toBe(null);
    done();
  });

  it('shows how much of the areas each pass reaches, beside its cloud', () => {
    const { target, done } = render();
    expect(rows(target)[0].textContent).toContain('Full cover');
    expect(rows(target)[0].textContent).toContain('3% cloud');
    // the fact a crosshair lookup cannot give, and a sweep cannot do without
    expect(rows(target)[1].textContent).toContain('62% of the areas');
    done();
  });

  it('asks for an area before it asks Copernicus anything', () => {
    const onlook = vi.fn();
    const { target, done } = render({ areas: 0, list: [], searched: false });
    expect(target.textContent).toContain('Draw an area in step 1');
    expect(button(target, 'Find passes').disabled).toBe(true);
    done();
  });

  it('does not reach the network until it is asked to', () => {
    const onlook = vi.fn();
    const { target, done } = render({ list: [], searched: false, onlook });
    // local-first: mounting the panel must never spend a request
    expect(onlook).not.toHaveBeenCalled();
    expect(target.textContent).toContain('billed as one Copernicus request');
    button(target, 'Find passes').click();
    expect(onlook).toHaveBeenCalledOnce();
    done();
  });

  it('hands a chosen pass back for the slot it was chosen for', () => {
    const onpick = vi.fn();
    const { target, done } = render({ onpick });
    button(target, 'A').click();
    expect(onpick).toHaveBeenCalledWith('a', passes[0]);
    done();
  });

  it('offers one slot for a method that reads a single image', () => {
    const { target, done } = render({ single: true });
    expect(button(target, 'A')).toBeUndefined();
    expect(button(target, 'Use')).toBeDefined();
    done();
  });

  it('hides the compare slot when a rule will resolve it at run time', () => {
    const { target, done } = render({ wantsCompare: false });
    expect(button(target, 'B')).toBeUndefined();
    expect(button(target, 'A')).toBeDefined();
    done();
  });

  it('disables a listed pass that would put A after B', () => {
    const { target, done } = render({ a: { date: '2026-05-11' }, b: { date: '2026-05-08' } });
    const latest = target.querySelector('[aria-label="Use 2026-05-11"]');
    const older = target.querySelector('[aria-label="Use 2026-05-08"]');
    expect(latest.querySelector('button[title="Date A must be before date B"]').disabled).toBe(true);
    expect(older.querySelector('button[title="Date A must be before date B"]').disabled).toBe(true);
    done();
  });

  it('says when the catalogue stopped short instead of implying a full list', () => {
    const { target, done } = render({ truncated: true });
    expect(target.textContent).toContain('stopped short');
    done();
  });

  it('uses a short search window while dates A and B keep their own calendars', () => {
    const onlookback = vi.fn();
    const { target, done } = render({ onlookback });
    expect(button(target, 'Dates…')).toBeUndefined();
    button(target, '1 year').click();
    expect(onlookback).toHaveBeenCalledWith(365);
    done();
  });

  it('offers the older passes a cut list is missing', () => {
    const onolder = vi.fn();
    const { target, done } = render({ truncated: true, lookback: 365, onolder });
    button(target, 'Older passes').click();
    expect(onolder).toHaveBeenCalledOnce();
    done();
  });

  it('offers nothing older once the list reaches the first day asked', () => {
    const { target, done } = render({ truncated: true, lookback: 30,
      list: [{ date: daysBefore(30), cloud: 2, coverage: 1 }], onolder: vi.fn() });
    expect(button(target, 'Older passes')).toBeUndefined();
    done();
  });

  it('suggests a longer window when nothing reaches the areas', () => {
    const { target, done } = render({ list: [] });
    expect(target.textContent).toContain('No pass reaches these areas');
    done();
  });

  it('lists radar passes by time and direction, and holds a pair to one track', () => {
    const radar = [
      { date: '2026-05-14', time: '17:33:02', orbit: 'ascending', cloud: null, coverage: 1 },
      { date: '2026-05-14', time: '05:42:10', orbit: 'descending', cloud: null, coverage: 1 },
    ];
    const { target, done } = render({ list: radar, radar: true, a: { date: '2026-05-02', time: '05:42:40' } });
    expect(rows(target)).toHaveLength(2);
    expect(rows(target)[1].textContent).toContain('05:42 UTC ↓');
    expect(target.textContent).not.toContain('cloud');
    // the evening pass is another track than A's: it cannot be its pair
    const [evening, morning] = rows(target).map((row) => [...row.querySelectorAll('button')].at(-1));
    expect(evening.disabled).toBe(true);
    expect(morning.disabled).toBe(false);
    done();
  });
});