The OSINT investigator's workbench. One case, one folder, every tool. Local-first: your media and your investigations never leave your machine.
JavaScript
// @vitest-environment happy-dom
/**
* Tests for the capture extension's popup (extension/popup.js).
*
* Like extension.test.js next door, the popup has no exports: the suite loads
* popup.html into the document, stubs `chrome` and `fetch`, evaluates the source
* and reads the resulting DOM.
*
* What is worth pinning here is the unreachable case. The popup asks the app to
* read the URL before it draws anything, so a stopped app is caught upstream and
* these buttons are never wired at all. The window this covers is the narrow one:
* the app answered the first request and was gone by the second.
*/
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { readFileSync } from 'node:fs';
import { fileURLToPath } from 'node:url';
import { dirname, join } from 'node:path';
const here = dirname(fileURLToPath(import.meta.url));
const ext = join(here, '../../../extension');
const source = readFileSync(join(ext, 'popup.js'), 'utf8');
const html = readFileSync(join(ext, 'popup.html'), 'utf8');
const $ = (id) => document.getElementById(id);
/** Run the popup against a tab, with `fetch` answering per URL fragment. */
async function runPopup({ tabUrl, answers, height = 940, stored = {} }) {
// Body only, without the <script src> happy-dom refuses to fetch: the source is
// evaluated below by hand, with the globals a popup really has.
document.body.innerHTML = html
.replace(/[\s\S]*<body[^>]*>/, '')
.replace(/<\/body>[\s\S]*/, '')
.replace(/<script[\s\S]*?<\/script>/g, '');
// happy-dom ships no `Option` constructor; the popup fills its case pickers with it.
globalThis.Option = function Option(text, value) {
const option = document.createElement('option');
option.textContent = text;
option.value = value ?? text;
return option;
};
globalThis.chrome = {
tabs: { query: vi.fn(async () => [{ id: 7, url: tabUrl }]), sendMessage: vi.fn(async () => {}) },
storage: {
local: {
get: vi.fn(async (defaults) => ({
...defaults,
backendUrl: 'http://127.0.0.1:8477',
token: 'tok-123',
...stored,
})),
set: vi.fn(),
},
},
runtime: { getManifest: () => ({ version: '0.2.9' }), sendMessage: vi.fn(async () => ({ ok: true })) },
scripting: {
executeScript: vi.fn(async () =>
height === null ? Promise.reject(new Error('cannot access')) : [{ result: height }]
),
},
};
globalThis.fetch = vi.fn(async (url) => {
for (const [fragment, answer] of Object.entries(answers)) {
if (String(url).includes(fragment)) return answer();
}
throw new Error('unexpected fetch');
});
const run = new Function('browser', `${source}\n//# sourceURL=popup.js`);
run(undefined);
// init() is async and awaits two fetches before it wires the buttons
await new Promise((resolve) => setTimeout(resolve, 0));
await new Promise((resolve) => setTimeout(resolve, 0));
}
const ok = (body) => () => ({ ok: true, status: 200, json: async () => body });
const down = () => {
throw new Error('network');
};
beforeEach(() => {
document.body.innerHTML = '';
vi.restoreAllMocks();
});
describe('a map page whose app goes away mid-load', () => {
const mapPage = {
tabUrl: 'https://www.google.fr/maps/@48.85,2.29,17z',
answers: {
'/parse': ok({ site: 'google-maps', label: 'Google Maps', lat: 48.85, lon: 2.29, zoom: 17 }),
'/cases': down,
},
};
it('greys out every button, so the error message is the only story', async () => {
await runPopup(mapPage);
expect($('save-place').disabled).toBe(true);
expect($('capture-area').disabled).toBe(true);
expect($('status').textContent).toContain('Cannot reach Azimut');
});
it('leaves nothing that answers a click with silence', async () => {
await runPopup(mapPage);
const live = ['save-place', 'capture-area'].filter((id) => !$(id).disabled);
expect(live).toEqual([]);
});
});
describe('a page that is not a map, whose app goes away mid-load', () => {
it('greys out the bookmark button too', async () => {
await runPopup({
tabUrl: 'https://example.com/article',
answers: { '/parse': ok({ site: null }), '/cases': down },
});
expect($('save-bookmark').disabled).toBe(true);
expect($('status').textContent).toContain('Cannot reach Azimut');
});
});
describe('a map page with the app answering', () => {
it('leaves capture reachable, and holds the place button for its coordinates', async () => {
await runPopup({
tabUrl: 'https://www.google.fr/maps/@48.85,2.29,17z',
answers: {
'/parse': ok({ site: 'google-maps', label: 'Google Maps', lat: 48.85, lon: 2.29, zoom: 17 }),
'/cases': ok([{ id: 'c1', name: 'Case one' }]),
},
});
expect($('capture-area').disabled).toBe(false);
expect($('save-place').disabled).toBe(false);
});
});
describe('how tall the map is drawn', () => {
/**
* Apple states the span its view covers and Google's satellite mode the metres
* its viewport is high. Either is a scale only next to the number of pixels it
* was drawn in, and that number is the one thing the app cannot see — so the
* popup reads it off the tab and sends it with the URL.
*/
const asked = () => globalThis.fetch.mock.calls.map(([url]) => String(url));
it('goes with the URL to the app', async () => {
await runPopup({
tabUrl: 'https://maps.apple.com/frame?center=48.85,2.29&span=0.02,0.03',
answers: {
'/parse': ok({ site: 'apple-maps', label: 'Apple Maps', lat: 48.85, lon: 2.29, zoom: 17 }),
'/cases': ok([{ id: 'c1', name: 'Case one' }]),
},
});
expect(asked().some((url) => url.includes('/parse') && url.includes('height=940'))).toBe(true);
});
it('is left out rather than guessed when the tab refuses to be read', async () => {
await runPopup({
tabUrl: 'https://maps.apple.com/frame?center=48.85,2.29&span=0.02,0.03',
height: null,
answers: {
'/parse': ok({ site: 'apple-maps', label: 'Apple Maps', lat: 48.85, lon: 2.29, zoom: null }),
'/cases': ok([{ id: 'c1', name: 'Case one' }]),
},
});
const parse = asked().find((url) => url.includes('/parse'));
expect(parse).toBeTruthy();
expect(parse).not.toContain('height=');
});
});
describe('the scale bar and north arrow tick', () => {
const googleMaps = (stored) => ({
tabUrl: 'https://www.google.fr/maps/@48.85,2.29,17z',
stored,
answers: {
'/parse': ok({ site: 'google-maps', label: 'Google Maps', lat: 48.85, lon: 2.29, zoom: 17 }),
'/cases': ok([{ id: 'c1', name: 'Case one' }]),
},
});
it('starts off, and a capture taken without it asks for no marks', async () => {
await runPopup(googleMaps());
expect($('marks').checked).toBe(false);
$('capture-area').click();
const [[msg]] = globalThis.chrome.runtime.sendMessage.mock.calls;
expect(msg.meta.marks).toBe(false);
});
it('is remembered the moment it is ticked, and rides with the capture', async () => {
await runPopup(googleMaps());
$('marks').checked = true;
$('marks').dispatchEvent(new Event('change'));
expect(globalThis.chrome.storage.local.set).toHaveBeenCalledWith({ captureMarks: true });
$('capture-area').click();
const [[msg]] = globalThis.chrome.runtime.sendMessage.mock.calls;
expect(msg.meta.marks).toBe(true);
});
it('comes back ticked next time it is opened', async () => {
await runPopup(googleMaps({ captureMarks: true }));
expect($('marks').checked).toBe(true);
});
it('points at the bearing field, since this URL states no rotation', async () => {
await runPopup(googleMaps({ captureMarks: true }));
expect($('marks-note').hidden).toBe(false);
$('bearing').value = '42';
$('bearing').dispatchEvent(new Event('input'));
expect($('marks-note').hidden).toBe(true);
});
it('says nothing about a heading when the marks are not wanted', async () => {
await runPopup(googleMaps());
expect($('marks-note').hidden).toBe(true);
});
});
describe('a place the app would refuse', () => {
const mapPage = (extra = {}) => ({
tabUrl: 'https://www.google.fr/maps/@48.85,2.29,17z',
answers: {
'/parse': ok({ site: 'google-maps', label: 'Google Maps', lat: 48.85, lon: 2.29, zoom: 17 }),
'/cases': ok([{ id: 'c1', name: 'Case one' }]),
...extra,
},
});
const posted = () => globalThis.fetch.mock.calls.filter(([url]) => String(url).includes('/ingest/place'));
it('says which field is out of range, before sending anything', async () => {
await runPopup(mapPage());
$('lat').value = '95';
$('lat').dispatchEvent(new Event('input'));
$('save-place').click();
await new Promise((resolve) => setTimeout(resolve, 0));
expect($('status').textContent).toBe('Latitude runs from -90 to 90.');
expect(posted()).toEqual([]);
});
it('reads a refusal the app sent as a list, never as [object Object]', async () => {
await runPopup(mapPage({
'/ingest/place': () => ({
ok: false,
status: 422,
json: async () => ({ detail: [{ loc: ['body', 'title'], msg: 'String should have at most 300 characters' }] }),
}),
}));
$('save-place').click();
await new Promise((resolve) => setTimeout(resolve, 0));
await new Promise((resolve) => setTimeout(resolve, 0));
expect($('status').textContent).toBe('Could not save: String should have at most 300 characters');
});
});