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
5.1 KB
Raw Blame
import { describe, expect, it } from 'vitest';
import { readView, readWindowLabel, viewParams } from './view.js';

describe('a view written into the address', () => {
  it('carries where the camera is, at a tenth of a metre', () => {
    expect(viewParams({ lat: 48.85661234, lon: 2.35222987, zoom: 17 })).toEqual({
      ll: '48.856612,2.35223',
      z: '17',
    });
  });

  it('leaves north-up out, because it is what every map opens on', () => {
    expect(viewParams({ lat: 1, lon: 1, zoom: 5, bearing: 0 }).b).toBeUndefined();
    expect(viewParams({ lat: 1, lon: 1, zoom: 5, bearing: 360 }).b).toBeUndefined();
    expect(viewParams({ lat: 1, lon: 1, zoom: 5, bearing: 215 }).b).toBe('215');
  });

  it('names the basemap, which is the surface\'s own and not the tool\'s', () => {
    expect(viewParams({ lat: 1, lon: 1, zoom: 5, provider: 'sentinel2' }).p).toBe('sentinel2');
  });

  it('writes nothing it cannot vouch for', () => {
    expect(viewParams({ lat: NaN, lon: 2, zoom: 4 }).ll).toBeUndefined();
    expect(viewParams({ lat: 91, lon: 2, zoom: 4 }).ll).toBeUndefined();
    expect(viewParams({ lat: 1, lon: 2, zoom: 99 }).z).toBeUndefined();
    expect(viewParams()).toEqual({});
  });
});

describe('a view read back out of it', () => {
  const read = (query) => readView(new URLSearchParams(query));

  it('reads a whole view', () => {
    expect(read('ll=48.8566,2.3522&z=17&b=215&p=esri')).toEqual({
      lat: 48.8566,
      lon: 2.3522,
      zoom: 17,
      bearing: 215,
      provider: 'esri',
    });
  });

  it('round-trips what was written', () => {
    const view = { lat: 48.8566, lon: 2.3522, zoom: 17, bearing: 90, provider: 'sentinel2' };
    expect(read(new URLSearchParams(viewParams(view)).toString())).toEqual(view);
  });

  it('honours a partial address as far as it goes', () => {
    expect(read('p=sentinel2')).toEqual({ provider: 'sentinel2' });
    expect(read('z=12')).toEqual({ zoom: 12 });
  });

  it('refuses what a hand-edited address can say', () => {
    // the map must stay where it is rather than fly to NaN
    expect(read('ll=nowhere')).toBe(null);
    expect(read('ll=91,2')).toBe(null);
    expect(read('ll=48.8,181')).toBe(null);
    expect(read('z=900')).toBe(null);
    expect(read('b=-4')).toBe(null);
    expect(read('')).toBe(null);
  });

  it('drops half a coordinate rather than guessing the other half', () => {
    expect(read('ll=48.8')).toBe(null);
  });
});

describe('which window this is', () => {
  const label = (query) => readWindowLabel(new URLSearchParams(query));

  it('numbers a detached window', () => {
    expect(label('w=2')).toBe(2);
  });

  it('says nothing about the first one', () => {
    expect(label('')).toBe(null);
    expect(label('w=1')).toBe(null);
  });

  it('refuses a number that would reach a title unread', () => {
    expect(label('w=0')).toBe(null);
    expect(label('w=-3')).toBe(null);
    expect(label('w=2.5')).toBe(null);
    expect(label('w=999')).toBe(null);
    expect(label('w=<script>')).toBe(null);
  });
});

describe('a tilted view of the relief', () => {
  it('writes the tilt only while the relief is on, zero included', () => {
    expect(viewParams({ lat: 1, lon: 1, zoom: 5 }).t).toBeUndefined();
    expect(viewParams({ lat: 1, lon: 1, zoom: 5, pitch: null }).t).toBeUndefined();
    expect(viewParams({ lat: 1, lon: 1, zoom: 5, pitch: 0 }).t).toBe('0');
    expect(viewParams({ lat: 1, lon: 1, zoom: 5, pitch: 61.6 }).t).toBe('62');
  });

  it('reads a tilt back, and refuses one the map could not take', () => {
    expect(readView(new URLSearchParams('ll=46,7&z=12&t=55')).pitch).toBe(55);
    expect(readView(new URLSearchParams('ll=46,7&z=12&t=0')).pitch).toBe(0);
    // the globe's steepest, which it writes and must open again
    expect(readView(new URLSearchParams('ll=46,7&z=12&t=88')).pitch).toBe(88);
    expect(readView(new URLSearchParams('ll=46,7&z=12&t=120')).pitch).toBeUndefined();
    expect(readView(new URLSearchParams('ll=46,7&z=12')).pitch).toBeUndefined();
  });

  it('keeps the zoom’s fraction over relief, where it is part of the camera, and only there', () => {
    expect(viewParams({ lat: 1, lon: 1, zoom: 13.6274, pitch: 60 }).z).toBe('13.63');
    expect(viewParams({ lat: 1, lon: 1, zoom: 13.6274, pitch: 0 }).z).toBe('13.63');
    expect(viewParams({ lat: 1, lon: 1, zoom: 13.6274 }).z).toBe('14');
    expect(readView(new URLSearchParams('ll=46,7&z=13.63&t=60')).zoom).toBe(13.63);
    expect(readView(new URLSearchParams('ll=46,7&z=13.63')).zoom).toBe(14);
  });

  it('writes the height the camera holds its centre at, over relief only, and reads it back', () => {
    expect(viewParams({ lat: 1, lon: 1, zoom: 13, pitch: 85, elevation: 711.9 }).e).toBe('712');
    expect(viewParams({ lat: 1, lon: 1, zoom: 13, elevation: 711.9 }).e).toBeUndefined();
    expect(viewParams({ lat: 1, lon: 1, zoom: 13, pitch: 85, elevation: null }).e).toBeUndefined();
    expect(readView(new URLSearchParams('ll=46,7&z=13.78&t=85&e=712')).elevation).toBe(712);
    expect(readView(new URLSearchParams('ll=46,7&z=13&e=712')).elevation).toBeUndefined();
    expect(readView(new URLSearchParams('ll=46,7&z=13&t=85&e=99999')).elevation).toBeUndefined();
  });
});