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
15.6 KB
Raw Blame
import { beforeEach, describe, expect, it, vi } from 'vitest';

const post = vi.fn().mockResolvedValue({ path: '/cases/c1/azimut/media' });
const get = vi.fn();
vi.mock('./api.js', () => ({ api: { post: (...a) => post(...a), get: (...a) => get(...a) } }));

const { caseState, uiState } = await import('./state.svelte.js');
const {
  gotoCapture,
  gotoPoint,
  lookFrom,
  openComparison,
  openEntity,
  openGuide,
  openInHorizon,
  openInReverseSearch,
  openInTimeline,
  openMapAt,
  opensInFileManager,
} = await import('./navigate.js');

beforeEach(() => {
  uiState.tool = 'media';
  uiState.gotoCoords = null;
  uiState.lookAt = null;
  uiState.focusCapture = null;
  uiState.openBoardEntity = null;
  uiState.openCompare = null;
  uiState.reverseTarget = null;
  post.mockClear();
  caseState.current = { id: 'c1', name: 'Case', entities: [], links: [], folders: [] };
  vi.stubGlobal('window', { open: vi.fn() });
});

describe('openInTimeline', () => {
  it('scopes the Timeline to the entity and selects the entry it was pressed on', () => {
    const entity = { id: 'm1', label: 'Frame', type: 'media', attrs: { path: 'media/f.png' } };
    openInTimeline(entity, { id: 'event-a', produced_here: true });
    expect(uiState.tool).toBe('timeline');
    expect(uiState.timelineFocus).toEqual({
      entityId: 'm1',
      entityLabel: 'Frame',
      entityType: 'media',
      entityAttrs: { path: 'media/f.png' },
      itemId: 'event-a',
      // a working file says so, or a Media track holding them back would hide the row
      producedHere: true,
    });
  });

  it('opens on the whole entity when no entry is named', () => {
    openInTimeline({ id: 'p1', label: 'Pier', type: 'place' });
    expect(uiState.timelineFocus).toMatchObject({ entityId: 'p1', itemId: null, producedHere: false, entityAttrs: {} });
  });
});

describe('openGuide', () => {
  it('opens the guide over the tool on whatever asked, and leaves the tool alone', () => {
    uiState.tool = 'satellite';
    openGuide('satellite');
    expect(uiState.guide).toEqual({ at: 'satellite' });
    expect(uiState.tool).toBe('satellite');
  });

  it('opens on a page by its id, as Home asks for the start', () => {
    openGuide('start');
    expect(uiState.guide).toEqual({ at: 'start' });
  });
});

describe('openInReverseSearch', () => {
  it('opens Reverse Search on the picture it was pressed from', () => {
    expect(openInReverseSearch({ path: 'media/clip.mp4', kind: 'video', label: 'Clip', time: 3 })).toBe(true);
    expect(uiState.tool).toBe('reverse');
    expect(uiState.reverseTarget).toEqual({
      path: 'media/clip.mp4',
      blob: null,
      kind: 'video',
      label: 'Clip',
      time: 3,
    });
  });

  it('stays put, and queues nothing, for a picture it cannot hand over', () => {
    expect(openInReverseSearch({ path: 'media/report.pdf', kind: 'file' })).toBe(false);
    expect(uiState.tool).toBe('media');
    expect(uiState.reverseTarget).toBeNull();
  });
});

describe('openEntity', () => {
  it('reopens a sheet by its entity id in Sheet', () => {
    openEntity({ id: 'sheet-1', type: 'sheet', label: 'Index', attrs: { path: 'sheets/index.csv' } });
    expect(uiState.tool).toBe('sheet');
    expect(uiState.openSheet).toBe('sheet-1');
  });
  it('opens a merged place at the survivor coordinates instead of frozen coordinates', async () => {
    get.mockResolvedValue({ entity: { id: 'kept', type: 'place', label: 'Kept place', attrs: { lat: 3, lon: 4 } }, merged_from: { id: 'old', label: 'Old place' } });
    openEntity({ id: 'old', type: 'place', attrs: { lat: 1, lon: 2 } });
    for (let i = 0; i < 5; i++) await Promise.resolve();
    expect(get).toHaveBeenCalledWith('/api/cases/c1/entities/old/chain');
    expect(uiState.gotoCoords.lat).toBe(3);
    expect(uiState.gotoCoords.lon).toBe(4);
  });
  it('opens a search the case keeps in Horizon, which shows it in Find', () => {
    openEntity({ id: 'f1', type: 'horizon-find', label: 'Ridge', attrs: { spec: '.horizon/finds/Ridge.json' } });
    expect(uiState.tool).toBe('horizon');
    expect(uiState.openHorizonFind).toBe('Ridge');
  });
  it('opens bookmarks and external captures in a new browser tab', () => {
    openEntity({ type: 'bookmark', attrs: { url: 'https://example.test/bookmark' } });
    openEntity({ type: 'capture', attrs: { source_url: 'https://maps.example.test/view' } });

    expect(window.open).toHaveBeenNthCalledWith(1, 'https://example.test/bookmark', '_blank', 'noopener,noreferrer');
    expect(window.open).toHaveBeenNthCalledWith(2, 'https://maps.example.test/view', '_blank', 'noopener,noreferrer');
  });

  it('returns an internal capture to its recorded Satellite view', () => {
    openEntity({ type: 'capture', attrs: {
      path: 'media/crop.png', lat: 48.8584, lon: 2.2945, zoom: 17, bearing: 30,
      provider: 'esri-world-imagery',
    } });

    expect(uiState.tool).toBe('satellite');
    expect(uiState.focusCapture).toBe('media/crop.png');
    expect(uiState.gotoCoords).toEqual({
      lat: 48.8584, lon: 2.2945, zoom: 17, bearing: 30, provider: 'esri-world-imagery',
    });
  });

  it('leaves the zoom to the map when a place records none', () => {
    // enrichment mints a place from a photo's EXIF: coordinates, no zoom. A
    // zoom of 0 is the whole globe, so an absent one must stay absent (NaN)
    // and let the Satellite tool pick its own close-in default.
    openEntity({ type: 'place', attrs: { lat: 48.8584, lon: 2.2945, zoom: null, bearing: null } });

    expect(uiState.tool).toBe('satellite');
    expect(uiState.gotoCoords.lat).toBe(48.8584);
    expect(uiState.gotoCoords.zoom).toBeNaN();
    expect(uiState.gotoCoords.bearing).toBeNaN();
  });

  it('keeps the zoom a place did record', () => {
    openEntity({ type: 'place', attrs: { lat: 48.8584, lon: 2.2945, zoom: 19, bearing: 0 } });

    expect(uiState.gotoCoords).toEqual({ lat: 48.8584, lon: 2.2945, zoom: 19, bearing: 0 });
  });
});

describe('a type with no tool of its own', () => {
  it('opens on the board, which is where the graph types are read', () => {
    // before this the call fell through doing nothing: a relation row on the map
    // said "Open …" and swallowed the click
    openEntity({ id: 'e-person', type: 'person', label: 'A. Analyst', attrs: {} });

    expect(uiState.tool).toBe('board');
    expect(uiState.openBoardEntity).toBe('e-person');
  });

  it('leaves the workspace alone for a row it cannot identify', () => {
    openEntity({ type: 'person', attrs: {} });

    expect(uiState.tool).toBe('media');
    expect(uiState.openBoardEntity).toBeNull();
  });
});

describe('a Claim', () => {
  beforeEach(() => {
    uiState.timelineRange = null;
    uiState.timelineQueue = null;
    get.mockReset();
  });
  const claim = { id: 'e-claim', type: 'claim', label: 'Column seen', attrs: {} };

  it('opens on the Timeline at its entry, with its neighbours in view', async () => {
    const item = {
      id: 'temporal:claim:e-claim', raw: '2026-03-12', sortable: true,
      earliest: '2026-03-12T00:00:00.000000Z', latest: '2026-03-13T00:00:00.000000Z',
    };
    get.mockResolvedValueOnce({ item });
    await openEntity(claim);

    expect(get).toHaveBeenCalledWith('/api/cases/c1/timeline/claims/e-claim');
    expect(uiState.tool).toBe('timeline');
    expect(uiState.timelineRange).toEqual({
      from: '2026-03-10T00:00:00.000Z',
      to: '2026-03-15T00:00:00.000Z',
      itemId: 'temporal:claim:e-claim',
      item,
    });
    expect(uiState.openBoardEntity).toBeNull();
  });

  it('opens the Undated queue on an entry with no date', async () => {
    const item = { id: 'temporal:claim:e-claim', raw: null, earliest: null, latest: null, sortable: false };
    get.mockResolvedValueOnce({ item });
    await openEntity(claim);

    expect(uiState.tool).toBe('timeline');
    expect(uiState.timelineQueue).toEqual({ queue: 'undated', itemId: item.id, item });
  });

  it('opens the list of entries off the UTC axis on one it cannot place', async () => {
    const item = { id: 'temporal:claim:e-claim', raw: '2026-03-12T14:00', earliest: null, latest: null, sortable: false };
    get.mockResolvedValueOnce({ item });
    await openEntity(claim);

    expect(uiState.timelineQueue).toEqual({ queue: 'unplaced', itemId: item.id, item });
  });

  it('falls back to its row on the Board when the Timeline cannot say', async () => {
    get.mockRejectedValueOnce(new Error('gone'));
    await openEntity(claim);

    expect(uiState.tool).toBe('board');
    expect(uiState.openBoardEntity).toBe('e-claim');
  });
});

describe('gotoCapture', () => {
  it('leaves the zoom to the map when the capture records none', () => {
    gotoCapture({ type: 'capture', attrs: { lat: 48.8584, lon: 2.2945, zoom: null } });

    expect(uiState.tool).toBe('satellite');
    expect(uiState.gotoCoords.zoom).toBeNaN();
  });
});

describe('gotoPoint', () => {
  it('flies the map to a position a file states about itself', () => {
    // no entity to open: the point comes off a sidecar, and the map is where
    // "is this plausible?" gets answered
    gotoPoint(48.8583, 2.2945);
    expect(uiState.tool).toBe('satellite');
    expect(uiState.gotoCoords).toEqual({ lat: 48.8583, lon: 2.2945 });
  });

  it('ignores a point it could not place', () => {
    uiState.gotoCoords = null;
    gotoPoint(Number('north'), 2.2945);
    expect(uiState.gotoCoords).toBeNull();
  });
});

describe('openMapAt', () => {
  it('flies Satellite to the point, at the zoom it was looked at', () => {
    openMapAt('satellite', { lat: 12.76, lon: 43.65, zoom: 15 });
    expect(uiState.tool).toBe('satellite');
    expect(uiState.gotoCoords).toEqual({ lat: 12.76, lon: 43.65, zoom: 15 });
    expect(uiState.lookAt).toBeNull();
  });

  it('moves Compare and Detect there, and leaves their own pictures to them', () => {
    openMapAt('compare', { lat: 12.76, lon: 43.65, zoom: 15 });
    expect(uiState.tool).toBe('compare');
    expect(uiState.lookAt).toEqual({ tool: 'compare', lat: 12.76, lon: 43.65, zoom: 15 });
    expect(uiState.compareAt ?? null).toBeNull();
    openMapAt('detect', { lat: 12.76, lon: 43.65 });
    expect(uiState.tool).toBe('detect');
    expect(uiState.lookAt).toEqual({ tool: 'detect', lat: 12.76, lon: 43.65 });
  });

  it('goes nowhere for a tab that is not a map, or a point it could not place', () => {
    openMapAt('board', { lat: 12.76, lon: 43.65 });
    openMapAt('detect', { lat: Number('north'), lon: 43.65 });
    expect(uiState.tool).toBe('media');
    expect(uiState.lookAt).toBeNull();
  });
});

describe('lookFrom', () => {
  it('stands the Horizon eye on the point, facing the way the map faced', () => {
    lookFrom({ lat: 35.62, lon: 71.33, heading: 342 });
    expect(uiState.tool).toBe('horizon');
    expect(uiState.horizonAt).toEqual({ lat: 35.62, lon: 71.33, heading: 342 });
    lookFrom({ lat: 1, lon: 2 });
    expect(uiState.horizonAt).toEqual({ lat: 1, lon: 2 });
  });

  it('carries a profile\'s question: the eye\'s height and the point to look for', () => {
    lookFrom({ lat: 1, lon: 2, heading: 45, eyeHeight: 10, mark: { lat: 1.1, lon: 2.1, height: 30 } });
    expect(uiState.horizonAt).toEqual({ lat: 1, lon: 2, heading: 45, eyeHeight: 10, mark: { lat: 1.1, lon: 2.1, height: 30 } });
  });

  it('goes nowhere for a point it could not place', () => {
    uiState.horizonAt = null;
    lookFrom({ lat: Number('north'), lon: 2 });
    expect(uiState.tool).toBe('media');
    expect(uiState.horizonAt).toBeNull();
  });
});

describe('openInHorizon', () => {
  it('hands a photo to Horizon to lay over its view', () => {
    expect(openInHorizon({ path: 'media/summit.jpg', kind: 'image', title: 'Summit', time: 3 })).toBe(true);
    expect(uiState.tool).toBe('horizon');
    expect(uiState.horizonPhoto).toEqual({ path: 'media/summit.jpg', kind: 'image', title: 'Summit' });
  });

  it('opens a video at the moment the analyst was looking at', () => {
    openInHorizon({ path: 'media/clip.mp4', kind: 'video', time: 14.2 });
    expect(uiState.horizonPhoto).toEqual({ path: 'media/clip.mp4', kind: 'video', time: 14.2 });
  });

  it('goes nowhere with what is not a photo or a video', () => {
    uiState.horizonPhoto = null;
    expect(openInHorizon({ path: 'media/plan.pdf', kind: 'file' })).toBe(false);
    expect(uiState.tool).toBe('media');
    expect(uiState.horizonPhoto).toBeNull();
  });
});

describe('a file the app cannot display', () => {
  const plan = { id: 'm1', type: 'media', attrs: { path: 'media/site plan.pdf', kind: 'file' } };

  it('is handed back to the desktop, in the folder it lives in', () => {
    openEntity(plan);

    expect(post).toHaveBeenCalledWith('/api/cases/c1/media/reveal', {
      path: 'media/site plan.pdf',
    });
    // and the workspace does not move: nothing here could show it anyway
    expect(uiState.tool).toBe('media');
    expect(uiState.focusMedia).not.toBe('media/site plan.pdf');
  });

  it('leaves the images, video and audio the app does display alone', () => {
    openEntity({ id: 'm2', type: 'media', attrs: { path: 'media/quay.jpg', kind: 'image' } });

    expect(post).not.toHaveBeenCalled();
    expect(uiState.focusMedia).toBe('media/quay.jpg');
  });

  it('says which entities need the file manager', () => {
    expect(opensInFileManager(plan)).toBe(true);
    expect(opensInFileManager({ type: 'media', attrs: { path: 'a.mp4' } })).toBe(false);
    expect(opensInFileManager({ type: 'capture', attrs: { path: 'media/crop.png' } })).toBe(false);
    expect(opensInFileManager(null)).toBe(false);
  });
});

describe('reopening an artifact in its tool', () => {
  // The tools take the bare stem the save route slugified. These paths are the
  // ones the backend records today (`layout.proof_spec_rel` and friends); the
  // folder prefixes moved once already, and stripping them by name meant every
  // one of these handed over a name with a directory still glued to it.
  it('names a proof by its spec file, wherever the spec folder sits', () => {
    openEntity({ type: 'proof', attrs: { spec: 'proofs/.meta/Rooftop angle.json' } });

    expect(uiState.openProof).toBe('Rooftop angle');
    expect(uiState.tool).toBe('proof');
  });

  it('names a post draft by its draft file', () => {
    openEntity({ type: 'post', attrs: { draft: '.drafts/Quay thread.json' } });

    expect(uiState.openDraft).toBe('Quay thread');
    expect(uiState.tool).toBe('post');
  });

  it('names the Inspect work on a file by its spec file', () => {
    openEntity({ type: 'inspect-session', attrs: { spec: '.inspect/Bridge pass.json' } });

    expect(uiState.openInspect).toBe('Bridge pass');
    expect(uiState.tool).toBe('inspect');
  });

  it('names a collage by its spec file', () => {
    openEntity({ type: 'collage', attrs: { spec: '.collages/Harbour strip.json' } });

    expect(uiState.openCollage).toBe('Harbour strip');
    expect(uiState.tool).toBe('collage');
  });

  it('names a comparison session by its spec file', () => {
    openEntity({ type: 'compare-session', attrs: { spec: 'comparisons/.meta/Harbour change.json' } });

    expect(uiState.openCompare).toBe('Harbour change');
    expect(uiState.tool).toBe('compare');
  });

  it('opens the tool empty rather than on a name it could not read', () => {
    uiState.openProof = null;
    openEntity({ type: 'proof', attrs: {} });

    expect(uiState.openProof).toBeNull();
    expect(uiState.tool).toBe('proof');
  });
});

describe('openComparison', () => {
  it('reopens a saved comparison from its Saved row, by the name Compare knows it by', () => {
    openComparison({ kind: 'comparison', session: 'Harbour reading' });
    expect(uiState.openCompare).toBe('Harbour reading');
    expect(uiState.tool).toBe('compare');
  });

  it('goes nowhere for a row that names no comparison', () => {
    openComparison({ kind: 'capture' });
    expect(uiState.tool).toBe('media');
  });
});