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.3 KB
Raw Blame
import { describe, expect, it } from 'vitest';
import {
  askable,
  firmsNote,
  keyState,
  DATED,
  lastDayOf,
  MAX_RANGE_DAYS,
  onDay,
  PICTURE,
  summary,
  tileParams,
  today,
  WINDOWS,
} from './firms.js';

const SENSORS = [
  { id: 'viirs', label: 'VIIRS (S-NPP, NOAA-20, NOAA-21)' },
  { id: 'modis', label: 'MODIS (Terra + Aqua)' },
];

describe('what to ask FIRMS for', () => {
  it('asks by how recent, for the rolling windows', () => {
    expect(tileParams({ sensor: 'viirs', window: '24h' })).toEqual({
      sensor: 'viirs',
      window: '24h',
    });
  });

  it('leaves the dates out of a rolling window', () => {
    // they would be two more things to keep in step with a layer that already
    // means "the last 24 hours"
    const params = tileParams({ sensor: 'viirs', window: '7d', first: '2026-01-01' });
    expect(params.first).toBeUndefined();
  });

  it('asks by date for the archive, and reads one day as a range of itself', () => {
    expect(tileParams({ sensor: 'modis', window: DATED, first: '2026-08-01' })).toEqual({
      sensor: 'modis',
      window: DATED,
      first: '2026-08-01',
      last: '2026-08-01',
    });
  });
});

describe('what the service will answer', () => {
  it('accepts every window it offers', () => {
    for (const entry of WINDOWS) expect(askable({ window: entry.id })).toBe(true);
  });

  it('waits rather than asking, while a dated window has no date', () => {
    // switching to Dates is not an error, so the layer holds instead of
    // fetching tiles that come back as an exception report
    expect(askable({ window: DATED })).toBe(false);
    expect(askable({ window: DATED, first: 'soon' })).toBe(false);
  });

  it('accepts a single day and a range inside the limit', () => {
    expect(askable({ window: DATED, first: '2026-08-01' })).toBe(true);
    expect(askable({ window: DATED, first: '2026-08-01', last: '2026-08-09' })).toBe(true);
  });

  it('refuses a range longer than FIRMS allows', () => {
    expect(askable({ window: DATED, first: '2026-01-01', last: '2026-03-01' })).toBe(false);
  });

  it('refuses a range that ends before it starts', () => {
    expect(askable({ window: DATED, first: '2026-08-09', last: '2026-08-01' })).toBe(false);
  });

  it('says where a range has to stop, so the input can say it too', () => {
    expect(lastDayOf('2026-01-01')).toBe('2026-01-31');
    expect(askable({ window: DATED, first: '2026-01-01', last: lastDayOf('2026-01-01') })).toBe(
      true
    );
    expect(lastDayOf('')).toBe('');
  });

  it('counts the first day as one of the days', () => {
    const first = '2026-05-10';
    const days =
      (new Date(`${lastDayOf(first)}T00:00:00Z`) - new Date(`${first}T00:00:00Z`)) / 86400000;
    expect(days).toBe(MAX_RANGE_DAYS - 1);
  });

  it('opens on a day that exists', () => {
    expect(today(new Date('2026-09-12T22:30:00Z'))).toBe('2026-09-12');
  });
});

describe('what the row says it is showing', () => {
  it('names the instrument without the satellites it is made of', () => {
    // "VIIRS (S-NPP, NOAA-20, NOAA-21) · 24 h" does not fit a layer row
    expect(summary({ sensor: 'viirs', window: '24h' }, SENSORS)).toBe('VIIRS · 24 h');
  });

  it('names the day, or the range, when it is showing the archive', () => {
    expect(summary({ sensor: 'modis', window: DATED, first: '2026-08-01' }, SENSORS)).toBe(
      'MODIS · 2026-08-01'
    );
    expect(
      summary({ sensor: 'modis', window: DATED, first: '2026-08-01', last: '2026-08-09' }, SENSORS)
    ).toBe('MODIS · 2026-08-01 → 2026-08-09');
  });

  it('asks for the date it is waiting on', () => {
    expect(summary({ sensor: 'viirs', window: DATED }, SENSORS)).toBe('VIIRS · pick a date');
  });
});

describe('what the row says about the key', () => {
  it('lets a usable key through without a word', () => {
    expect(keyState({ keyed: true, state: 'ready' })).toEqual({ usable: true, detail: '', title: '', action: '' });
  });

  it('tells a missing key from one switched off and one FIRMS refused', () => {
    expect(keyState({ keyed: false, state: 'missing' }).detail).toBe('needs a free key');
    expect(keyState({ keyed: false, state: 'off' })).toMatchObject({ usable: false, detail: 'off in Settings', action: 'Turn it on' });
    expect(keyState({ keyed: false, state: 'refused' })).toMatchObject({ detail: 'key refused', action: 'Check the key' });
  });

  it('reads an answer without a state as no key', () => {
    expect(keyState({}).detail).toBe('needs a free key');
    expect(keyState(undefined).usable).toBe(false);
  });
});

describe('the note under the controls', () => {
  it('puts a paused layer first, whatever else is true', () => {
    expect(firmsNote({ window: DATED }, { until: '2026-09-26T12:00:00+00:00' })).toMatch(/allowance used up/);
  });

  it('asks for a date before anything else', () => {
    expect(firmsNote({ window: DATED })).toBe('Pick a date to draw the detections.');
  });

  it('warns about a long range and says nothing about a short one', () => {
    expect(firmsNote({ window: DATED, first: '2026-08-01', last: '2026-08-31' })).toMatch(/long range/);
    expect(firmsNote({ window: DATED, first: '2026-08-01', last: '2026-08-07' })).toBe('');
    expect(firmsNote({ window: DATED, first: '2026-08-01' })).toBe('');
  });

  it('explains the two colours of a window longer than a day', () => {
    expect(firmsNote({ window: '7d' })).toBe('Red: the last 24 hours. Amber: before them.');
    expect(firmsNote({ window: '48h' })).toMatch(/Red/);
    expect(firmsNote({ window: '24h' })).toBe('');
  });
});

describe('following the picture on screen', () => {
  const choice = { sensor: 'viirs', window: PICTURE, first: '', last: '' };

  it('asks for the day the picture was taken', () => {
    expect(onDay(choice, '2024-03-12')).toEqual({ sensor: 'viirs', window: DATED, first: '2024-03-12', last: '' });
    expect(tileParams(onDay(choice, '2024-03-12'))).toEqual({
      sensor: 'viirs',
      window: DATED,
      first: '2024-03-12',
      last: '2024-03-12',
    });
  });

  it('falls back to the last 24 hours for a picture with no date', () => {
    expect(onDay(choice, '')).toEqual({ sensor: 'viirs', window: '24h', first: '', last: '' });
    expect(onDay(choice, 'not a day').window).toBe('24h');
  });

  it('leaves a picked window alone', () => {
    const picked = { sensor: 'modis', window: '7d', first: '', last: '' };
    expect(onDay(picked, '2024-03-12')).toBe(picked);
  });
});