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
3.0 KB
Raw Blame
import { afterEach, describe, expect, it, vi } from 'vitest';
import { ApiError, api } from './api.js';

function answers(status, body) {
  vi.stubGlobal(
    'fetch',
    vi.fn().mockResolvedValue({
      ok: status < 400,
      status,
      json: async () => body,
    })
  );
}

afterEach(() => {
  vi.unstubAllGlobals();
});

describe('API errors', () => {
  it('reads a refusal the backend wrote as a sentence', async () => {
    answers(409, { detail: 'a case named “Rooftop” already exists' });
    await expect(api.get('/api/cases')).rejects.toThrow('a case named “Rooftop” already exists');
  });

  it('reads a schema rejection, which arrives as the validator’s list of objects', async () => {
    // FastAPI answers 422 with `detail` as an array. Handed straight to Error,
    // every validation failure in the app surfaced as "[object Object]".
    answers(422, {
      detail: [
        { loc: ['body', 'lon'], msg: 'Input should be less than or equal to 180', type: 'less_than_equal' },
      ],
    });
    await expect(api.post('/api/satellite/capture', {})).rejects.toThrow(
      'lon: Input should be less than or equal to 180'
    );
  });

  it('joins several rejected fields into one line', async () => {
    answers(422, {
      detail: [
        { loc: ['body', 'lat'], msg: 'field required' },
        { loc: ['body', 'zoom'], msg: 'must be an integer' },
      ],
    });
    await expect(api.post('/api/x', {})).rejects.toThrow('lat: field required; zoom: must be an integer');
  });

  it('falls back to the status when the body says nothing usable', async () => {
    answers(404, { detail: [] });
    await expect(api.get('/api/x')).rejects.toThrow('HTTP 404');
  });

  it('says a server crash is one, rather than a bare status', async () => {
    answers(500, { detail: [] });
    await expect(api.get('/api/x')).rejects.toThrow('Azimut hit an unexpected error (HTTP 500). Settings → System → Report an issue has its log.');
  });

  it('carries the status for callers that branch on it', async () => {
    answers(404, { detail: 'case not found' });
    await expect(api.get('/api/cases/nope')).rejects.toMatchObject({
      status: 404,
      constructor: ApiError,
    });
  });
});

describe('a request that reaches no server', () => {
  it('says the app is not running, not the browser’s “Failed to fetch”', async () => {
    vi.stubGlobal('fetch', vi.fn().mockRejectedValue(new TypeError('Failed to fetch')));
    const failure = await api.put('/api/cases/c/notes/n', { text: 'x' }).catch((error) => error);
    expect(failure).toBeInstanceOf(ApiError);
    expect(failure.status).toBe(0);
    expect(failure.message).toBe('Azimut is not running. Start it again, then reload this tab.');
  });

  it('lets a cancelled request stay a cancellation', async () => {
    const aborted = Object.assign(new Error('The operation was aborted.'), { name: 'AbortError' });
    vi.stubGlobal('fetch', vi.fn().mockRejectedValue(aborted));
    await expect(api.get('/api/cases')).rejects.toBe(aborted);
  });
});