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
113 KB
Raw Blame
import { expect, test } from '@playwright/test';

export const CASE_ID = 'browser-test';
export const PANEL_PATH = 'media/panel.svg';

const PANEL_SVG = `
  <svg xmlns="http://www.w3.org/2000/svg" width="640" height="360">
    <rect width="640" height="360" fill="#304b65"/>
    <path d="M0 300L180 150L300 250L450 90L640 280V360H0Z" fill="#6c8f55"/>
    <circle cx="520" cy="76" r="34" fill="#f4c95d"/>
  </svg>`;

// A real raster tile, not an SVG: the map engine decodes tiles into bitmaps,
// and `createImageBitmap` refuses SVG. 16px of dark ground and a green ridge,
// scaled up over the tile — enough to tell imagery from an empty map in a
// failure screenshot.
const TILE_PNG = Buffer.from(
  'iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAAAQElEQVR42mNUMXdnIAUwMZAI' +
    'hrAGbUtSNEBUQ0jKnAS3F5egtiWSBri9aCqw24AmgdUqCGAhxiUoNuAxbKgkDQDARwagm0jS' +
    'UwAAAABJRU5ErkJggg==',
  'base64'
);

/** A batch of tiles as api/tile_batch.py writes it: each one its place asked, its status, its length, itself. */
function tileBatch(keys, tile) {
  return Buffer.concat(keys.map((_, index) => {
    const head = Buffer.alloc(8);
    head.writeUInt16LE(index, 0);
    head.writeUInt16LE(200, 2);
    head.writeUInt32LE(tile.length, 4);
    return Buffer.concat([head, tile]);
  }));
}

// A reference overlay is transparent but for the lines it draws. The fixture
// answers with nothing drawn at all, so a layer that is on never paints over
// what a spec is looking at underneath it.
const CLEAR_TILE_PNG = Buffer.from(
  'iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAEklEQVR4nGNgGAWjYBSMAggAAAQQAAFVN1rQAAAAAElFTkSuQmCC',
  'base64'
);

/** Where the fixture's TileJSON says OpenFreeMap's tiles are, as the real one does. */
const PLACE_TILES = 'https://tiles.openfreemap.org/planet/fixture-build/{z}/{x}/{y}.pbf';

/**
 * One vector tile holding one village, in OpenFreeMap's `place` layer: the
 * least the Place names style writes a label from. Encoded here, by the Mapbox
 * Vector Tile layout, so the fixture needs no encoder.
 *
 * The village stands at sixteen points of a grid rather than one. The map opens
 * deeper than the tiles go, so one tile spans more than the map is wide, and a
 * single point could fall outside the view.
 */
function placeTile(name) {
  const varint = (n) => {
    const out = [];
    for (; n > 0x7f; n >>>= 7) out.push((n & 0x7f) | 0x80);
    return [...out, n];
  };
  const key = (field, wire) => varint((field << 3) | wire);
  const nested = (field, body) => [...key(field, 2), ...varint(body.length), ...body];
  const text = (value) => [...Buffer.from(value, 'utf8')];
  const zigzag = (n) => (n << 1) ^ (n >> 31);
  // on a 4096 extent; each point is written as a step from the one before
  const grid = [512, 1536, 2560, 3584].flatMap((y) => [512, 1536, 2560, 3584].map((x) => [x, y]));
  const steps = grid.flatMap(([x, y], i) => {
    const [px, py] = i ? grid[i - 1] : [0, 0];
    return [...varint(zigzag(x - px)), ...varint(zigzag(y - py))];
  });
  const feature = [
    ...nested(2, [0, 0, 1, 1]), // tags: class=village, name:latin=<name>
    ...key(3, 0), 1, // points
    ...nested(4, [...varint(1 | (grid.length << 3)), ...steps]), // one MoveTo, sixteen points
  ];
  const layer = [
    ...key(15, 0), 2,
    ...nested(1, text('place')),
    ...nested(2, feature),
    ...nested(3, text('class')),
    ...nested(3, text('name:latin')),
    ...nested(4, nested(1, text('village'))),
    ...nested(4, nested(1, text(name))),
    ...key(5, 0), ...varint(4096),
  ];
  return Buffer.from(nested(3, layer));
}

const media = [{
  path: PANEL_PATH,
  filename: 'panel.svg',
  kind: 'image',
  width: 640,
  height: 360,
  size: 256,
  sha256: '0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef',
  source: { type: 'upload' },
  thumbnail: null,
  thumb_state: 'ready',
  enrich_state: 'ready',
  folder: '',
  notes: '',
}];

// Saved work as the Map panel reads it (GET /satellite/index). Empty by
// default: each spec that needs saved items overrides this route itself.
const savedIndex = [];

const caseOverview = {
  id: CASE_ID,
  name: 'Browser Test',
  scratch: false,
  entities: [],
  links: [],
  folders: [],
};

const settings = {
  coord_format: 'dd',
  units: 'metric',
  home_view: { lat: 48.8584, lon: 2.2945, zoom: 16 },
  post_mention: '@GeoConfirmed',
  post_target: 'x',
  post_template: '',
  signature_handle: '',
  signature: false,
  export_dirs: { notes: '', media: '', proofs: '' },
  api_keys: {},
  providers_enabled: {},
  free_tiers: {},
  eco_max_zooms: {},
  provider_status: {},
  ingest_token: '',
  version: '0.2.8',
  workspace_root: '/tmp/azimut-browser-fixture',
  extension_version: '0.2.5', // tracks the extension, so it lags the app version
  update_check_on_start: false,
  update_dismissed_version: '',
  // the tour taken, this version's What's new and Find's beta notice read, so none
  // covers a spec (e2e/onboarding.spec.js opens them on purpose)
  tour: 'done',
  whats_new_seen: '0.2.8',
  find_beta_seen: true,
  usage: {},
  usage_overrides: {},
  eco_zoom_fallback: true,
  eco_max_zoom: 15,
  free_tier: {},
  month: '2026-07',
};

// The offline gazetteer, cut down to what a spec needs: two cities that share a
// prefix, so ranking is visible, and one street only the geocoder knows about.
const GAZETTEER = [
  { name: 'Kramatorsk', region: 'Donetsk', country: 'ua', country_name: 'Ukraine', lat: 48.7311, lon: 37.5678, population: 147145 },
  { name: 'Kraków', region: 'Lesser Poland', country: 'pl', country_name: 'Poland', lat: 50.0614, lon: 19.9366, population: 755050 },
];

const GEOCODED = [
  { lat: 48.8534, lon: 37.6067, display_name: 'Kramatorska Street, Sloviansk, Donetsk Oblast, Ukraine', kind: 'road' },
];

// A workspace that is present and nobody else's, so the app runs instead of
// showing the stopped screen (GET /settings/workspace), and no folder in it is
// waiting to become a case (GET /workspace/folders).
const workspaceStatus = {
  locked_by: null,
  locked_detail: '',
  root: '/tmp/azimut-browser-fixture',
  default_root: '/tmp/azimut-browser-fixture',
  pointed: false,
  environment: false,
  missing: false,
  moving: false,
  cases: 1,
  move: null,
};
const workspaceFolders = [];

// The graph readings, as `engine/graph.py` resolves them from the two registries: a
// set of verbs from the relation types, and a set of node roles from the entity types.
// Held here as data because a browser spec must not depend on the backend running,
// and kept in step with those registries for the same reason the relation slice is:
// a stale lens would let a spec prove the opposite of the shipped contract.
//
// What every reading of the *case* leaves out: the analyst's own output, which is the
// filing rather than the case. `My work` is the one lens that draws it.
const GRAPH_FILED = ['inspect-session', 'note', 'post'];

export const graphLenses = {
  lenses: [
    { id: 'all', label: 'Everything', hint: 'every connection between the things the case is about', types: ['derived-from', 'depends-on', 'located-at', 'depicts', 'owns', 'part-of', 'member-of', 'posted', 'appears-in', 'sited-at', 'instance-of', 'in-network', 'same-image-as', 'mentions', 'about', 'at', 'cites'], hides: GRAPH_FILED },
    { id: 'subjects', label: 'Subjects', hint: 'who owns, belongs to, posted or appears in what', types: ['owns', 'part-of', 'member-of', 'posted', 'appears-in', 'instance-of', 'in-network', 'same-image-as'], hides: GRAPH_FILED },
    { id: 'ground', label: 'Ground', hint: 'what is tied to a place on the map', types: ['located-at', 'depicts', 'sited-at'], hides: GRAPH_FILED },
    { id: 'claims', label: 'Claims', hint: 'what is asserted, and what it rests on', types: ['about', 'at', 'cites'], hides: GRAPH_FILED },
    { id: 'work', label: 'My work', hint: 'what you wrote, what it was made from, and what it points at', types: ['derived-from', 'depends-on', 'mentions'], hides: [] },
  ],
  orders: [
    { value: 'degree', label: 'Most connected', hint: 'keeps the hubs, so a cut view still shows the shape' },
    { value: 'recent', label: 'Latest work', hint: 'keeps what entered the case last' },
  ],
  max_hops: 3,
};

/** Where a graph node's preview is served from, as `media_thumbs` reports it. */
export const GRAPH_THUMB = 'media/.thumbs/harbour.svg';

/** The default drawn case: one hub, its members, a place and something unconnected. */
const graphNodes = [
  { id: 'org-1', type: 'organization', label: '3rd Battalion', family: 'actor', status: 'confirmed', at: '2026-08-01T09:00:00Z', degree: 3 },
  // The one node with a picture: a card shows a preview, everything else a glyph.
  // `kind` is what the bytes are, which the payload carries for media alone: one
  // type covers image, video and audio, so the node has to say which it holds.
  { id: 'med-1', type: 'media', label: 'harbour frame', family: 'collected', status: 'confirmed', at: '2026-08-01T09:30:00Z', degree: 1, kind: 'image', thumb: GRAPH_THUMB, folder: 'harbour' },
  { id: 'per-1', type: 'person', label: 'Section chief', family: 'actor', status: 'confirmed', at: '2026-08-01T10:00:00Z', degree: 1 },
  { id: 'veh-1', type: 'vehicle', label: 'BTR-82A 4721', family: 'asset', status: 'confirmed', at: '2026-08-01T11:00:00Z', degree: 1, attrs: { plate: 'AX-904-ZT' } },
  { id: 'acc-1', type: 'account', label: '@harbourwatch', family: 'identifier', status: 'suggested', at: '2026-08-02T08:00:00Z', degree: 1 },
  { id: 'plc-1', type: 'place', label: 'checkpoint north', family: 'place', status: 'confirmed', at: '2026-08-02T09:00:00Z', degree: 0 },
  { id: 'dom-1', type: 'domain', label: 'nothing.test', family: 'identifier', status: 'confirmed', at: '2026-08-02T10:00:00Z', degree: 0 },
];

/**
 * The seed edges. Copied per fixture rather than read directly, because a link
 * written from a surface has to be there on the next read: a graph that answered
 * without the edge just filed would make a spec prove the opposite of the shipped
 * behaviour, since the tool says nothing when the edge appears and speaks up when it
 * does not.
 */
const graphLinks = [
  { id: 'l-1', from: 'per-1', to: 'org-1', type: 'member-of', provenance: { by: 'user', at: '2026-08-02T09:00:00Z', status: 'confirmed' } },
  { id: 'l-4', from: 'acc-1', to: 'med-1', type: 'posted', provenance: { by: 'user', at: '2026-08-02T09:20:00Z', status: 'confirmed' } },
  { id: 'l-2', from: 'org-1', to: 'veh-1', type: 'owns', provenance: { by: 'user', at: '2026-08-02T09:05:00Z', status: 'confirmed' } },
  { id: 'l-3', from: 'org-1', to: 'acc-1', type: 'owns', provenance: { by: 'enrich', at: '2026-08-02T09:10:00Z', status: 'suggested' } },
];

function graphView(url, override, pins = new Map(), held = graphLinks) {
  if (override) return override;
  const lens = url.searchParams.get('lens') ?? 'all';
  const verbs = graphLenses.lenses.find((entry) => entry.id === lens)?.types ?? [];
  // One lens draws one set of verbs, as the engine resolves them. Filtering here
  // rather than blanking a single reading is what lets a spec state an edge into a
  // lens that does not hold its verb, which is the one case the tool words.
  const drawn = held.filter((link) => verbs.includes(link.type));
  // The catalog filters the graph now sends, applied here rather than ignored: a
  // spec has to be able to prove the surface asked the server to narrow. `type` is a
  // comma-separated set, because that is what a family switch resolves to on its way
  // out — read as one string, unticking a family narrowed to nothing.
  const wantedTypes = (url.searchParams.get('type') ?? '').split(',').filter(Boolean);
  const wantedStatus = url.searchParams.get('status') ?? '';
  const wantedFolder = url.searchParams.get('folder') ?? '';
  const recursive = url.searchParams.get('recursive') === 'true';
  const unfiled = url.searchParams.get('unfiled') === 'true';
  const query = (url.searchParams.get('q') ?? '').toLocaleLowerCase().split(/\s+/).filter(Boolean);
  const field = url.searchParams.get('attr') ?? '';
  const value = url.searchParams.get('value') ?? '';
  const linked = url.searchParams.get('linked') ?? '';
  const unlinked = url.searchParams.get('unlinked') === 'true';
  const since = url.searchParams.get('since') ?? '';
  const until = url.searchParams.get('until') ?? '';
  const filedBy = (url.searchParams.get('by') ?? '').split(',').filter(Boolean);
  const opened = (url.searchParams.get('expand') ?? '').split(',').filter(Boolean);
  // The drawing is a set the client owns: `keep` draws these and nothing around them,
  // `omit` leaves these out whatever put them there. Honoured here rather than ignored,
  // or a spec would prove a picture the server never sends.
  const byName = (url.searchParams.get('keep') ?? '').split(',').filter(Boolean);
  const out = new Set((url.searchParams.get('omit') ?? '').split(',').filter(Boolean));
  const degrees = new Map(graphNodes.map((node) => [node.id, 0]));
  for (const link of drawn) {
    degrees.set(link.from, degrees.get(link.from) + 1);
    degrees.set(link.to, degrees.get(link.to) + 1);
  }
  // The pins are served back the way the case stores them — keyed by lens as well
  // as by node — so a spec can prove an arrangement survived a reload, and that it
  // did not leak into another reading.
  // What an expansion reaches: the opened nodes and everything one hop from them,
  // kept whatever the filters say — an analyst asking what touches this node is
  // asking about the case, not about the narrowing they set earlier.
  const reached = new Set(opened);
  for (const link of drawn) {
    if (opened.includes(link.from)) reached.add(link.to);
    if (opened.includes(link.to)) reached.add(link.from);
  }
  const typeOf = new Map(graphNodes.map((node) => [node.id, node.type]));
  const reaches = (node, wantedType) => drawn.some(
    (link) =>
      (link.from === node.id && typeOf.get(link.to) === wantedType) ||
      (link.to === node.id && typeOf.get(link.from) === wantedType),
  );
  const hasLink = (node) => drawn.some((link) => link.from === node.id || link.to === node.id);
  const passes = (node) => {
    const searchable = [node.label, node.type, node.folder, node.kind, ...Object.values(node.attrs ?? {})]
      .filter(Boolean).join('\n').toLocaleLowerCase();
    return (
    (!wantedTypes.length || wantedTypes.includes(node.type))
    && (!wantedStatus || node.status === wantedStatus)
    && (!wantedFolder || (recursive
      ? node.folder === wantedFolder || node.folder?.startsWith(`${wantedFolder}/`)
      : node.folder === wantedFolder))
    && (!unfiled || !node.folder)
    && (!query.length || query.every((term) => searchable.includes(term)))
    && (!field || !value || String(node.attrs?.[field] ?? node[field] ?? '') === value)
    && (!linked || reaches(node, linked))
    && (!unlinked || !hasLink(node))
    && (!since || node.at >= since)
    && (!until || node.at <= `${until}T23:59:59Z`)
    && (!filedBy.length || filedBy.includes(node.by ?? 'user'))
    );
  };
  const standing = graphNodes.filter(
    (node) =>
      !out.has(node.id) && (reached.has(node.id) || byName.includes(node.id) || passes(node)),
  );
  // An arrival is held by whatever reaches it, so a removal takes with it the hops no
  // anchor still reaches — or the drawing keeps a handful of dots with no edge to
  // anything. Anchors are the ranked nodes and the ones named, which stand on their
  // own. Mirrors `engine/graph._stranded`, and it is here for the same reason the
  // filters are: a spec must not prove a picture the server never sends.
  const survives = new Set(standing.map((node) => node.id));
  const shown = new Set(
    standing
      .filter((node) => passes(node) || byName.includes(node.id) || opened.includes(node.id))
      .map((node) => node.id),
  );
  for (let growing = true; growing; ) {
    growing = false;
    for (const link of drawn) {
      for (const [near, far] of [[link.from, link.to], [link.to, link.from]]) {
        if (shown.has(near) && survives.has(far) && !shown.has(far)) {
          shown.add(far);
          growing = true;
        }
      }
    }
  }
  const kept = standing.filter((node) => shown.has(node.id));
  // A degree says what a click would bring in, and nothing brings a removal back, so
  // the edges that left with it stop being offered — exactly as the engine corrects it.
  for (const link of drawn) {
    for (const [near, far] of [[link.from, link.to], [link.to, link.from]]) {
      if (out.has(far) && shown.has(near)) degrees.set(near, degrees.get(near) - 1);
    }
  }
  const ranked = new Set(graphNodes.filter(passes).map((node) => node.id));
  const nodes = kept.map((node) => {
    const seat = { ...node, degree: degrees.get(node.id) };
    delete seat.attrs;
    if (query.length) {
      const match = Object.entries(node.attrs ?? {}).find(([, held]) =>
        query.every((term) => String(held).toLocaleLowerCase().includes(term))
      );
      if (match) seat.matches = [{ field: match[0], label: 'Plate', value: String(match[1]) }];
    }
    if (!ranked.has(node.id)) seat.added = true;
    const pin = pins.get(`${lens}|${node.id}`);
    return pin ? { ...seat, pin: [pin.x, pin.y] } : seat;
  });
  return {
    lens,
    order: url.searchParams.get('order') ?? 'degree',
    nodes,
    links: drawn.filter((link) => shown.has(link.from) && shown.has(link.to)),
    total: ranked.size,
    shown: nodes.length,
    truncated: false,
    kept: byName.filter((id) => shown.has(id)),
    expanded: opened.filter((id) => graphNodes.some((node) => node.id === id)),
    isolated: nodes.filter((node) => node.degree === 0).length,
    pinned: [...pins.keys()].filter((key) => key.startsWith(`${lens}|`)).length,
  };
}


/**
 * The shortest route between two entities, as `engine/graph.paths` answers it.
 *
 * A plain breadth-first walk rather than the bidirectional one the server runs: the
 * fixture case is seven nodes, and a spec proves the *tool's* behaviour, never the
 * server's search strategy. Undirected, like the real one.
 */
function graphPaths(url, override, held = graphLinks) {
  // A spec that overrides the drawn case overrides what a route may run through:
  // answering over the default edges would have the route contradict the picture.
  const drawn = override?.nodes ?? graphNodes;
  if (override?.links) held = override.links;
  const from = url.searchParams.get('from');
  const to = url.searchParams.get('to');
  const near = new Map();
  for (const link of held) {
    for (const [a, b] of [[link.from, link.to], [link.to, link.from]]) {
      if (!near.has(a)) near.set(a, []);
      near.get(a).push([b, link]);
    }
  }
  // Every predecessor at the shortest depth, not one of them: the ties are what the
  // tool draws all of, so a fixture that kept one could not prove it.
  const depth = new Map([[from, 0]]);
  const behind = new Map([[from, []]]);
  let edge = [from];
  while (edge.length && !depth.has(to)) {
    const step = depth.get(edge[0]) + 1;
    const next = [];
    for (const id of edge) {
      for (const [far, link] of near.get(id) ?? []) {
        if (depth.has(far) && depth.get(far) < step) continue;
        if (!depth.has(far)) {
          depth.set(far, step);
          behind.set(far, []);
          next.push(far);
        }
        behind.get(far).push([id, link]);
      }
    }
    edge = next;
  }
  const base = { lens: 'all', from, to, searched: 4, truncated: false };
  if (!depth.has(to)) return { ...base, found: false, hops: 0, routes: [], nodes: [], links: [] };
  const back = (at) => {
    if (at === from) return [{ nodes: [from], links: [] }];
    return (behind.get(at) ?? []).flatMap((step) =>
      back(step[0]).map((route) => ({
        nodes: [...route.nodes, at],
        links: [...route.links, step[1].id],
      })),
    );
  };
  const routes = back(to);
  const seen = new Set(routes.flatMap((route) => route.nodes));
  const used = new Set(routes.flatMap((route) => route.links));
  return {
    ...base,
    found: true,
    hops: routes[0].links.length,
    routes,
    nodes: drawn.filter((node) => seen.has(node.id)),
    links: held.filter((link) => used.has(link.id)),
  };
}

function graphHood(url, override, held = graphLinks) {
  if (override) return override;
  const root = url.searchParams.get('root') ?? 'org-1';
  const links = held.filter((link) => link.from === root || link.to === root);
  const reached = new Set([root]);
  for (const link of links) reached.add(link.from === root ? link.to : link.from);
  const nodes = graphNodes
    .filter((node) => reached.has(node.id))
    .map((node) => ({ ...node, hop: node.id === root ? 0 : 1 }));
  return { lens: url.searchParams.get('lens') ?? 'all', root, hops: 1, nodes, links, shown: nodes.length, truncated: false };
}

// The relation vocabulary slice exercised by browser specs. Keep endpoint and
// action metadata exact: a stale action can make an E2E test prove the opposite
// of the production contract.
export const relationTypes = [
  { type: 'located-at', label: 'was recorded at', inverse_label: 'was recorded here', hint: 'where the photo, video or audio was recorded', group: '', action: 'relation', from_types: ['media'], to_types: ['place'], from_media_kinds: ['audio', 'image', 'video'], to_media_kinds: [], manual: true, ratable: true },
  { type: 'depicts', label: 'shows', inverse_label: 'is shown in', hint: 'the place is visible in the image or video', group: '', action: 'relation', from_types: ['capture', 'media'], to_types: ['place'], from_media_kinds: ['image', 'video'], to_media_kinds: [], manual: true, ratable: true },
  { type: 'owns', label: 'owns', inverse_label: 'is owned by', hint: 'ownership rather than use, access or operational control', group: '', action: 'relation', from_types: ['organization', 'person'], to_types: ['account', 'aircraft', 'domain', 'email', 'ip', 'network', 'organization', 'phone', 'structure', 'vehicle', 'vessel'], from_media_kinds: [], to_media_kinds: [], manual: true, ratable: true },
  { type: 'part-of', label: 'is part of', inverse_label: 'contains', hint: 'an internal unit inside its parent organization', group: '', action: 'relation', from_types: ['organization'], to_types: ['organization'], from_media_kinds: [], to_media_kinds: [], manual: true, ratable: true },
  { type: 'member-of', label: 'is a member of', inverse_label: 'has member', hint: 'membership rather than internal containment', group: '', action: 'relation', from_types: ['organization', 'person'], to_types: ['organization'], from_media_kinds: [], to_media_kinds: [], manual: true, ratable: true },
  { type: 'associated-with', label: 'is associated with', inverse_label: 'is associated with', hint: 'a tie that exists, where what kind it is rides on the edge', group: '', action: 'relation', from_types: ['organization', 'person'], to_types: ['organization', 'person'], from_media_kinds: [], to_media_kinds: [], manual: true, ratable: true, qualifier: 'How they are tied' },
  { type: 'posted', label: 'posted', inverse_label: 'was posted by', hint: 'the account published the content or URL', group: '', action: 'relation', from_types: ['account'], to_types: ['bookmark', 'media'], from_media_kinds: [], to_media_kinds: [], manual: true, ratable: true },
  { type: 'appears-in', label: 'appears in', inverse_label: 'shows', hint: 'the entity or a recognizable representation is visible', group: '', action: 'relation', from_types: ['account', 'aircraft', 'domain', 'email', 'ip', 'network', 'organization', 'person', 'phone', 'structure', 'vehicle', 'vessel'], to_types: ['capture', 'media'], from_media_kinds: [], to_media_kinds: ['image', 'video'], manual: true, ratable: true },
  { type: 'sited-at', label: 'is sited at', inverse_label: 'is the site of', hint: 'a permanent site rather than a dated presence', group: '', action: 'relation', from_types: ['structure'], to_types: ['place'], from_media_kinds: [], to_media_kinds: [], manual: true, ratable: true },
  { type: 'in-network', label: 'is in network', inverse_label: 'contains', hint: 'the address or subnet belongs inside this network', group: '', action: 'relation', from_types: ['ip', 'network'], to_types: ['network'], from_media_kinds: [], to_media_kinds: [], manual: true, ratable: true },
  { type: 'same-image-as', label: 'is the same image as', inverse_label: 'is the same image as', hint: 'enrichment matched the perceptual hashes', group: '', action: 'relation', from_types: ['media'], to_types: ['media'], from_media_kinds: ['image'], to_media_kinds: ['image'], manual: false, ratable: false },
  { type: 'mentions', label: 'mentions', inverse_label: 'is mentioned by', hint: 'the document refers to the entity', group: 'Mentions', action: 'mention', from_types: ['bookmark', 'note', 'post', 'proof'], to_types: ['account', 'aircraft', 'bookmark', 'capture', 'claim', 'domain', 'email', 'inspect-session', 'ip', 'media', 'network', 'note', 'organization', 'person', 'phone', 'place', 'post', 'proof', 'structure', 'vehicle', 'vessel'], from_media_kinds: [], to_media_kinds: [], manual: true, ratable: false },
  { type: 'about', label: 'is about', inverse_label: 'has claim', hint: 'what the statement concerns', group: '', action: 'claim', from_types: ['claim'], to_types: ['account', 'aircraft', 'capture', 'domain', 'email', 'equipment-type', 'ip', 'media', 'network', 'organization', 'person', 'phone', 'place', 'structure', 'vehicle', 'vessel'], from_media_kinds: [], to_media_kinds: [], manual: true, ratable: false },
  { type: 'at', label: 'places it at', inverse_label: 'is a claim location', hint: 'where the statement places its subject or event', group: '', action: 'claim', from_types: ['claim'], to_types: ['place'], from_media_kinds: [], to_media_kinds: [], manual: true, ratable: false },
  { type: 'cites', label: 'cites', inverse_label: 'supports claim', hint: 'the evidence the statement relies on', group: '', action: 'claim', from_types: ['claim'], to_types: ['bookmark', 'capture', 'media', 'note', 'proof'], from_media_kinds: [], to_media_kinds: [], manual: true, ratable: false },
  { type: 'contradicts', label: 'contradicts', inverse_label: 'is contradicted by', hint: 'the two statements cannot both hold', group: '', action: 'claim', from_types: ['claim'], to_types: ['claim'], from_media_kinds: [], to_media_kinds: [], manual: true, ratable: false },
];

// How sure an edge may say the analyst is. Served beside the verbs, and absent from
// this list is "not assessed" — the lack of a rating, never a level.
const confidenceLevels = [
  { value: 3, label: 'Certain', hint: 'established and corroborated' },
  { value: 2, label: 'Probable', hint: 'more likely than not, and short of established' },
  { value: 1, label: 'Possible', hint: 'roughly even odds, and it cannot be excluded' },
  { value: -1, label: 'Ruled out', hint: 'checked and eliminated' },
];

// The entity-vocabulary slice exercised by browser specs: what each used type reads
// as, its family, icon and generated fields. Backend contract tests cover the full
// registry; a browser spec adds a row here when it needs to render that type.
const entityTypes = [
  {
    type: 'person', label: 'Person', family: 'actor', icon: 'user', manual: true, group: '', image_gallery: true,
    identity_label: 'Full name', identity_placeholder: 'Name or known alias',
    hint: 'a named individual', family_reads: 'a person or organization that can act or hold ownership',
    attrs: [
      { key: 'aliases', label: 'Other names', kind: 'text', rungs: [], options: [], minimum: null, maximum: null },
      { key: 'role', label: 'Role', kind: 'text', rungs: [], options: [], minimum: null, maximum: null },
      { key: 'nationality', label: 'Nationality', kind: 'text', rungs: [], options: [], minimum: null, maximum: null },
    ],
  },
  {
    type: 'organization', label: 'Organization', family: 'actor', icon: 'layers', manual: true, group: '', image_gallery: true,
    identity_label: 'Organization name', identity_placeholder: 'Name of the organization',
    hint: 'a company, a ministry or a military unit', family_reads: 'a person or organization that can act or hold ownership',
    attrs: [],
  },
  {
    type: 'vehicle', label: 'Vehicle', family: 'asset', icon: 'grip', manual: true, group: '', image_gallery: true,
    identity_label: 'Vehicle name', identity_placeholder: 'How this vehicle is known',
    hint: 'one particular vehicle, not a model', family_reads: 'a thing that is owned or appears in footage',
    attrs: [
      { key: 'plate', label: 'Plate', kind: 'text', rungs: [], options: [], minimum: null, maximum: null },
      { key: 'make', label: 'Make', kind: 'text', rungs: [], options: [], minimum: null, maximum: null },
      { key: 'model', label: 'Model', kind: 'text', rungs: [], options: [], minimum: null, maximum: null },
      { key: 'colour', label: 'Colour', kind: 'text', rungs: [], options: [], minimum: null, maximum: null },
    ],
  },
  {
    type: 'equipment-type', label: 'Equipment type', family: 'class', icon: 'stack', manual: true, group: '',
    identity_label: 'Model', identity_placeholder: 'T-72B3',
    hint: 'a model the case counts with, never one particular object', family_reads: 'a model the case counts with, never one particular object',
    attrs: [],
  },
  {
    type: 'account', label: 'Account', family: 'identifier', icon: 'at', manual: true, group: '',
    identity_label: 'Handle', identity_placeholder: '@handle',
    hint: 'a handle on one platform', family_reads: 'a handle on a system, where the value is the identity',
    attrs: [
      { key: 'platform', label: 'Platform', kind: 'text', rungs: [], options: [], minimum: null, maximum: null },
      { key: 'url', label: 'Profile URL', kind: 'url', rungs: [], options: [], minimum: null, maximum: null },
    ],
  },
  {
    type: 'domain', label: 'Domain', family: 'identifier', icon: 'globe', manual: true, group: '',
    identity_label: 'Hostname', identity_placeholder: 'example.test',
    hint: 'a hostname, which is its own identity', family_reads: 'a handle on a system, where the value is the identity',
    attrs: [
      { key: 'registrar', label: 'Registrar', kind: 'text', rungs: [], options: [], minimum: null, maximum: null },
    ],
  },
  {
    type: 'ip', label: 'IP address', family: 'identifier', icon: 'hash', manual: true, group: '',
    identity_label: 'IP address', identity_placeholder: '203.0.113.42',
    hint: 'an address, which is its own identity', family_reads: 'a handle on a system, where the value is the identity',
    attrs: [
      { key: 'network', label: 'Legacy network', hint: 'older free text, replaced by the In network relation', kind: 'text', editable: false, rungs: [], options: [], minimum: null, maximum: null },
      { key: 'asn', label: 'ASN', kind: 'text', rungs: [], options: [], minimum: null, maximum: null },
      { key: 'provider', label: 'Provider', kind: 'text', rungs: [], options: [], minimum: null, maximum: null },
    ],
  },
  {
    type: 'network', label: 'Network', family: 'identifier', icon: 'globe', manual: true, group: '',
    identity_label: 'Network or CIDR', identity_placeholder: '203.0.113.0/24',
    hint: 'an IP range, named by its network or CIDR', family_reads: 'a handle on a system, where the value is the identity',
    attrs: [
      { key: 'asn', label: 'ASN', kind: 'text', rungs: [], options: [], minimum: null, maximum: null },
      { key: 'provider', label: 'Provider', kind: 'text', rungs: [], options: [], minimum: null, maximum: null },
      { key: 'country', label: 'Country', kind: 'text', rungs: [], options: [], minimum: null, maximum: null },
    ],
  },
  {
    type: 'claim', label: 'Claim', family: 'claim', icon: 'note', manual: true, group: 'Reasoning',
    hint: 'something you are saying about the case',
    identity_label: 'Claim', identity_placeholder: 'What are you asserting?',
    family_reads: 'a statement about the rest of the case, carrying its own reasoning',
    attrs: [
      { key: 'count', label: 'How many', hint: 'how many of the one thing this claim counts', kind: 'number', rungs: [], options: [], minimum: 1, maximum: 100000, whole: true, group: 'What it states' },
      { key: 'condition', label: 'Condition', hint: 'the state at the moment this claim describes', kind: 'choice', rungs: [], options: [{ value: 'intact', label: 'Intact' }, { value: 'damaged', label: 'Damaged' }, { value: 'destroyed', label: 'Destroyed' }, { value: 'abandoned', label: 'Abandoned' }], minimum: null, maximum: null },
      { key: 'confidence', label: 'Confidence', hint: 'how strongly the statement is supported', kind: 'choice', rungs: [], options: [{ value: 'certain', label: 'Certain' }, { value: 'probable', label: 'Probable' }, { value: 'possible', label: 'Possible' }, { value: 'refuted', label: 'Ruled out' }], minimum: null, maximum: null },
      { key: 'method', label: 'How this was worked out', hint: 'the reasoning a reader would need to check this', kind: 'text', rungs: [], options: [], minimum: null, maximum: null },
      { key: 'verbatim', label: 'As the source put it', hint: 'the original wording, quoted rather than paraphrased', kind: 'text', rungs: [], options: [], minimum: null, maximum: null },
    ],
  },
  { type: 'media', label: 'Media', family: 'collected', icon: 'image', manual: false, group: '', hint: 'a file the case collected', family_reads: 'bytes gathered into the case rather than written', attrs: [] },
  { type: 'capture', label: 'Capture', family: 'collected', icon: 'satellite', manual: false, group: '', hint: 'a map screenshot', family_reads: 'bytes gathered into the case rather than written', attrs: [] },
  {
    type: 'place', label: 'Place', family: 'place', icon: 'pin', manual: false, group: '',
    hint: 'a saved point', family_reads: 'a point on the map, never a thing standing on it',
    // the precision fields, as `engine/entities.py` declares them: a place edited from
    // the map opens the same panel as the sidebar, and these are what it is opened for
    attrs: [
      { key: 'radius_m', label: 'Uncertainty radius (m)', kind: 'number', group: 'How precise', rungs: [{ value: 500, label: 'This neighbourhood' }], options: [], minimum: 1, maximum: 5000000 },
      { key: 'footprint', label: 'Footprint', kind: 'geojson', rungs: [], options: [], minimum: null, maximum: null },
      { key: 'verbatim', label: 'As the source put it', kind: 'longtext', rungs: [], options: [], minimum: null, maximum: null },
      { key: 'method', label: 'How this point was found', kind: 'longtext', rungs: [], options: [], minimum: null, maximum: null },
    ],
  },
  { type: 'note', label: 'Note', family: 'document', icon: 'note', manual: false, group: '', hint: 'a Markdown page in the case notebook', family_reads: 'something read rather than gathered', attrs: [] },
  {
    type: 'bookmark', label: 'Bookmark', family: 'document', icon: 'globe', manual: false, group: '',
    hint: 'a page the case points at',
    family_reads: 'something read rather than gathered, made or merely consulted',
    attrs: [
      { key: 'archive_url', label: 'Archived copy', hint: 'a snapshot that survives the page being taken down', kind: 'url', rungs: [], options: [], minimum: null, maximum: null },
      {
        key: 'reliability', label: 'Source reliability', kind: 'choice', rungs: [], minimum: null, maximum: null,
        hint: 'how much this source is worth in general, never how sure one claim is',
        options: [
          { value: 'A', label: 'Completely reliable' },
          { value: 'B', label: 'Usually reliable' },
        ],
      },
    ],
  },
];

const providers = [{
  id: 'esri-world-imagery',
  label: 'Esri World Imagery',
  url: 'https://tiles.invalid/{z}/{x}/{y}.png',
  attribution: 'Browser fixture',
  max_zoom: 19,
  tile_size: 256,
  oversample: 1,
  imagery: true,
  capturable: true,
  // the app keeps its tiles on disk, so a flat map asks them in batches, as for the real Esri
  cacheable: true,
}];

/**
 * The Maps JS basemap, offered only when a spec asks for it (`widget: true`).
 *
 * It is the one basemap that is not our tiles: Google's own map renders under
 * ours. A spec that wants it also wants `fakeGoogleMaps`, so that no key, no
 * network and no billed map load is involved.
 */
const WIDGET_PROVIDER = {
  id: 'google-js',
  label: 'Google Satellite (Maps JS)',
  url: 'https://maps.googleapis.com/maps/api/js?key=fixture',
  attribution: 'Map data © Google',
  max_zoom: 21,
  tile_size: 256,
  oversample: 1,
  imagery: true,
  capturable: false,
  widget: 'google-maps-js',
  meter: 'google_js',
  eco_max_zoom: 0,
};

/**
 * Stand in for the Maps JS API before the app loads.
 *
 * `loadGoogleMaps` resolves at once when `window.google.maps` is already there,
 * so this replaces the script, the key and the billing in one go. The map it
 * builds paints itself magenta, which is how a spec can tell whether it really
 * shows through our own canvas.
 */
export async function fakeGoogleMaps(page) {
  await page.addInitScript(() => {
    window.__googleMaps = [];
    window.google = {
      maps: {
        event: {
          addListenerOnce: (map, name, handler) => map.__once.push([name, handler]),
          trigger: (map, name) => map.__triggered.push(name),
        },
        Map: class {
          constructor(node, options) {
            this.node = node;
            this.options = options;
            this.cameras = [];
            this.__once = [];
            this.__triggered = [];
            node.style.background = 'rgb(255, 0, 255)';
            window.__googleMaps.push(this);
          }
          moveCamera(camera) {
            this.cameras.push(camera);
          }
          addListener() {}
        },
      },
    };
  });
}

function json(route, body, status = 200) {
  return route.fulfill({ status, contentType: 'application/json', body: JSON.stringify(body) });
}

function fixtureIso(milliseconds) {
  return new Date(milliseconds).toISOString().replace('.000Z', 'Z');
}

function fixtureTemporalToken(raw) {
  const clean = raw.replace(/[~?%]$/, '');
  const timestamp = clean.match(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{1,6})?(Z|[+-]\d{2}:\d{2})?$/);
  if (timestamp) {
    if (!timestamp[1]) {
      return {
        earliest: null, latest: null,
        precision: clean.includes('.') ? 'subsecond' : 'second',
        zone: 'local', sortable: false, timestamp: true,
      };
    }
    const earliest = Date.parse(clean);
    if (!Number.isFinite(earliest)) return null;
    return {
      earliest: fixtureIso(earliest),
      latest: fixtureIso(earliest + (clean.includes('.') ? 1 : 1000)),
      precision: clean.includes('.') ? 'subsecond' : 'second',
      zone: timestamp[1] === 'Z' ? 'utc' : 'offset',
      sortable: true,
      timestamp: true,
    };
  }

  const match = clean.match(/^(\d{4})(?:-(\d{2})(?:-(\d{2}))?)?$/);
  if (!match) return null;
  const year = Number(match[1]);
  const month = match[2] ? Number(match[2]) : 1;
  const day = match[3] ? Number(match[3]) : 1;
  const earliest = Date.UTC(year, month - 1, day);
  const latest = match[3]
    ? Date.UTC(year, month - 1, day + 1)
    : match[2] ? Date.UTC(year, month, 1) : Date.UTC(year + 1, 0, 1);
  return {
    earliest: fixtureIso(earliest),
    latest: fixtureIso(latest),
    precision: match[3] ? 'day' : match[2] ? 'month' : 'year',
    zone: 'date-only',
    sortable: true,
    timestamp: false,
  };
}

function fixtureTemporalReading(raw) {
  if (!raw) {
    return {
      earliest: null, latest: null, precision: null, shape: null,
      zone: null, sortable: false, uncertain: false, approximate: false,
    };
  }
  const parts = raw.split('/');
  const start = fixtureTemporalToken(parts[0]);
  const end = parts[1] ? fixtureTemporalToken(parts[1]) : null;
  if (!start || (parts[1] && !end)) {
    return {
      earliest: null, latest: null, precision: null,
      shape: parts[1] ? 'interval' : 'instant', zone: null, sortable: false,
      uncertain: raw.includes('?') || raw.includes('%'),
      approximate: raw.includes('~') || raw.includes('%'),
    };
  }
  return {
    earliest: start.earliest,
    latest: end ? (end.timestamp ? end.earliest : end.latest) : start.latest,
    precision: end && end.precision !== start.precision ? 'mixed' : start.precision,
    shape: end ? 'interval' : 'instant',
    zone: end && end.zone !== start.zone ? 'mixed' : start.zone,
    sortable: start.sortable && (!end || end.sortable),
    uncertain: raw.includes('?') || raw.includes('%'),
    approximate: raw.includes('~') || raw.includes('%'),
  };
}

/**
 * Run the real Svelte, Konva and map code while replacing only Azimut's
 * local API and files with deterministic fixtures. Any unexpected request is
 * recorded so a passing interaction test cannot silently depend on the network.
 */
export async function installAppFixture(page, options = {}) {
  const unexpected = [];
  const railTiles = [];
  const referenceTiles = [];
  const placeNameTiles = [];
  const captures = [];
  const placeWrites = [];
  const proofSaves = [];
  const kinReads = [];
  const proposalRuns = [];
  const fixtureProposals = structuredClone(
    options.proposals ?? {
      pending: { accounts: 0, posted: 0, sites: 0 }, items: [], listed: 0, radius: 300,
      // Read already, as a case is once anything was imported: the toolbar stays as it was.
      through: '2026-07-21T00:00:00Z',
    },
  );
  const fixtureSavedIndex = options.savedIndex ?? savedIndex;
  // Located files as the Media position reads them (GET /satellite/media).
  const fixtureMediaIndex = options.mediaIndex ?? [];
  const fixtureMedia = (options.media ?? media).map((item) => ({ ...item }));
  const fixtureCases = options.cases ?? [caseOverview];
  const fixtureProviders = options.widget ? [...providers, WIDGET_PROVIDER] : providers;
  const widgetLoads = [];
  const fixtureSavedIndexes = options.savedIndexes ?? { [CASE_ID]: fixtureSavedIndex };
  const fixtureMediaIndexes = options.mediaIndexes ?? { [CASE_ID]: fixtureMediaIndex };
  const savedIndexDelays = options.savedIndexDelays ?? {};
  const caseDelays = options.caseDelays ?? {};
  const graphDelays = options.graphDelays ?? {};
  const fixtureDrafts = options.drafts ?? {};
  const fixtureSettings = {
    ...settings,
    ...(options.settings ?? {}),
    export_dirs: { ...settings.export_dirs, ...(options.exportDirs ?? {}) },
  };
  // What the search bar asked for, and in which layer: a spec has to be able to
  // prove the geocoder was left alone while the analyst was still typing.
  const geoQueries = { suggest: [], places: [] };
  const settingsWrites = [];
  const folderWrites = [];
  const exportWrites = [];
  const fixtureFolderRoots = options.folderRoots ?? [];
  const fixtureFolderViews = { ...(options.folderViews ?? {}) };
  const fixtureLookupEntities = options.lookupEntities ?? {};
  const trashGroups = [...(options.trashGroups ?? [])];
  const trashWrites = [];
  const bundleCalls = [];
  // The case's saved search grids, held across requests: a sweep auto-saves, so
  // what was marked is readable from what was written.
  const fixtureGrids = new Map();
  const gridWrites = [];
  // Horizon's bench (api/horizon_work.py), per case, for the page's life: a reload brings it back.
  const horizonWork = new Map(Object.entries(options.horizonWork ?? {}));
  const workWrites = [];
  const skyQueries = [];
  // Where the graph's nodes have been dragged to, held across requests so a spec
  // can reload the view and find the arrangement still there.
  const graphPins = new Map(Object.entries(options.graphPins ?? {}));
  const graphQueries = [];
  const graphPinWrites = [];
  const analysisViews = (options.analysisViews ?? []).map((view) => ({
    ...view,
    spec: structuredClone(view.spec ?? {}),
  }));
  const analysisWrites = [];
  const timelineRows = (options.timelineItems ?? []).map((item) => structuredClone(item));
  const timelineWrites = [];
  const bundlePreview = options.bundlePreview;
  const bundleJob = options.bundleJob ?? { state: 'ready' };
  // Relations, keyed by entity id: what the bounded chain endpoint answers for
  // the row whose relations a surface asked for.
  const fixtureChains = options.chains ?? {};
  const linkWrites = [];
  // The edges this run holds, copied so a write from one spec is not read by the
  // next. What the graph is drawn from, what the catalog's one-hop filter tests, and
  // what a POST, a PATCH or a DELETE moves. A spec that brought its own catalog can
  // bring the edges between those rows too.
  const caseLinks = (options.links ?? graphLinks).map((link) => ({
    ...link,
    provenance: { ...link.provenance },
  }));
  let minted = 0;
  // What the bounded catalog holds, and what the board asked it for. Copied rather
  // than held by reference: a spec's `catalog` is a module constant shared by every
  // test in the file, and confirming or dismissing a row writes to this one.
  const fixtureCatalog = (options.catalog ?? []).map((entity) => ({
    ...entity,
    provenance: { ...entity.provenance },
  }));
  const catalogQueries = [];
  const entityWrites = [];
  const galleryWrites = [];
  const entityImages = new Map(
    Object.entries(options.entityImages ?? {}).map(([id, images]) => [
      id,
      images.map((image) => ({ ...image })),
    ])
  );
  const uploads = [];
  const pastes = [];
  const revealed = [];
  const mediaDownloadWrites = [];

  /**
   * The sheets this case holds, in memory, behaving the way the engine does.
   *
   * Enough of the real thing for the grid to be exercised for real: a stamp handed out
   * by every read and checked by every save, the sidecar written by its own route
   * without touching the table, and the key column filled in on create. `sheetStamp`
   * moves on its own when a test wants to play the spreadsheet that edited the file
   * underneath.
   */
  const fixtureSheets = new Map();
  const sheetWrites = [];
  let sheetTick = 1000;
  const nextStamp = () => `${(sheetTick += 1)}-70`;
  const makeSheet = (title, columns, rows = [], meta = null) => {
    const id = `sheet-${fixtureSheets.size + 1}`;
    const held = {
      id,
      title,
      path: `sheets/${title}.csv`,
      columns: ['id', ...columns],
      rows: rows.map((row, at) => [`r${at + 1}`, ...row]),
      // A sheet may arrive with a sidecar: the roles are what make a column offer a
      // vocabulary, and a spec about that list cannot set one up through the panel.
      meta: { version: 5, ...(meta ?? {}) },
      stamp: nextStamp(),
    };
    fixtureSheets.set(id, held);
    return held;
  };
  const sheetPayload = (held) => ({
    id: held.id,
    title: held.title,
    path: held.path,
    columns: [...held.columns],
    rows: held.rows.map((row) => [...row]),
    meta: structuredClone(held.meta),
    stamp: held.stamp,
    assigned: false,
    dropped_roles: [],
    pieces: {},
  });
  for (const [title, columns, rows, meta] of options.sheets ?? []) {
    makeSheet(title, columns, rows, meta);
  }

  const timelineEntity = (id) => fixtureCatalog.find((entity) => entity.id === id)
    ?? fixtureChains[id]?.entity
    ?? null;
  const timelineReferences = (item, ids, held) => {
    if (Array.isArray(held)) return held.map((entry) => ({ ...entry }));
    return (ids ?? []).map(timelineEntity).filter(Boolean).map((entry) => ({
      id: entry.id, label: entry.label, type: entry.type,
    }));
  };
  const enrichedTimelineItem = (item) => ({
    ...structuredClone(item),
    owner_type: item.owner_type ?? timelineEntity(item.owner_id)?.type ?? '',
    subject_entities: timelineReferences(item, item.subjects, item.subject_entities),
    place_entities: timelineReferences(item, item.places, item.place_entities),
    source_entities: timelineReferences(item, item.sources, item.source_entities),
  });
  /** Where a statement's places sit, read from the place entities themselves. The
   *  projection carries names, and only the map route resolves them to coordinates —
   *  a place the case has not located therefore drops out here, as it does server
   *  side. */
  const positionedPlaces = (item) => (item.places ?? []).map((placeId) => {
    const place = timelineEntity(placeId);
    const lat = Number(place?.attrs?.lat);
    const lon = Number(place?.attrs?.lon);
    if (!place || !Number.isFinite(lat) || !Number.isFinite(lon)) return null;
    return {
      id: place.id,
      label: place.label,
      lat,
      lon,
      radius_m: place.attrs.radius_m ?? null,
      footprint: place.attrs.footprint ?? null,
    };
  }).filter(Boolean);
  /** What a mark carries about the row's own entity, so the card can hand it back to
   *  the tool that owns it and show a photograph rather than a line of text. */
  const mapOwner = (item) => {
    const entity = timelineEntity(item.owner_id);
    return {
      id: item.owner_id,
      type: entity?.type ?? item.owner_type ?? '',
      label: entity?.label ?? item.label ?? '',
      attrs: entity?.attrs ?? {},
      thumb: entity?.thumb ?? entity?.attrs?.thumb ?? null,
    };
  };
  /** The server's file lanes: a source's picture or video, or what the app pictured
   *  from above (a capture, or a file whose `origin` is one of those routes). */
  const fromAbove = (entry) => entry.type === 'capture'
    || ['satellite', 'screenshot', 'compare'].includes(timelineEntity(entry.id)?.origin?.type);
  const laneFile = (item, lane) => [...item.subject_entities, ...item.source_entities]
    .find((entry) => ['media', 'capture'].includes(entry.type) && fromAbove(entry) === (lane === 'imagery'))
    ?? null;
  const timelineMatchesTrack = (item, track = {}) => {
    if ((track.hidden ?? []).includes(item.id)) return false;
    if (['sources', 'imagery'].includes(track.as_files)
      && (item.category !== 'statement' || !laneFile(item, track.as_files))) return false;
    const roles = Array.isArray(track.roles) ? track.roles : [];
    if (roles.length && !roles.includes(item.time_role ?? 'unset')) return false;
    const terms = track.terms && typeof track.terms === 'object' ? track.terms : {};
    const relation = track.relation ?? 'any';
    const related = {
      owner: [timelineEntity(item.owner_id)].filter(Boolean),
      about: timelineReferences(item, item.subjects, item.subject_entities),
      place: timelineReferences(item, item.places, item.place_entities),
      source: timelineReferences(item, item.sources, item.source_entities),
    };
    const candidates = relation === 'any'
      ? [...related.owner, ...related.about, ...related.place, ...related.source]
      : related[relation] ?? [];
    const hasTerms = Object.values(terms).some((value) => value !== '' && value !== null && value !== false);
    if (!hasTerms) return !['about', 'place', 'source'].includes(relation) || candidates.length > 0;
    const types = String(terms.type ?? '').split(',').filter(Boolean);
    const words = String(terms.q ?? '').toLocaleLowerCase().split(/\s+/).filter(Boolean);
    return candidates.some((entity) => {
      const text = [entity.label, entity.type, ...Object.values(entity.attrs ?? {})]
        .filter(Boolean).join('\n').toLocaleLowerCase();
      return (!types.length || types.includes(entity.type))
        && (!terms.status || entity.provenance?.status === terms.status)
        && (!words.length || words.every((word) => text.includes(word)));
    });
  };
  const scopedTimelineRows = ({ categories = [], entity = '', track = {}, from = '', to = '', includeUndated = true }) =>
    timelineRows.map(enrichedTimelineItem).filter((item) =>
      (!categories.length || categories.includes(item.category))
      && (!entity || item.owner_id === entity || (item.subjects ?? []).includes(entity)
        || (item.places ?? []).includes(entity) || (item.sources ?? []).includes(entity))
      && timelineMatchesTrack(item, track)
      // the server's `collected_only`: working files stay off, but the one in focus
      && !(track.collected_only === true && item.produced_here === true && item.owner_id !== entity)
      && (item.earliest
        ? (!from || item.latest > (from.includes('T') ? from : `${from}T00:00:00Z`))
          && (!to || item.earliest < (to.includes('T') ? to : `${to}T23:59:59Z`))
        : includeUndated)
    );
  const analysisSnapshotCount = (spec) => spec?.snapshot?.timeline_items?.length
    ?? spec?.snapshot?.entities?.length
    ?? 0;
  const captureTimelineSnapshot = (spec) => {
    const timeline = spec?.timeline ?? {};
    const visible = new Set(timeline.visible_categories ?? ['statement', 'media']);
    const captured = new Map();
    const timelineTracks = {};
    for (const track of timeline.tracks ?? []) {
      const categories = (track.categories ?? []).filter((category) => visible.has(category));
      const rows = categories.length ? scopedTimelineRows({
        categories,
        entity: timeline.entity?.id ?? '',
        track: { ...(track.query ?? {}), hidden: track.hidden ?? [] },
        from: timeline.from ?? '',
        to: timeline.to ?? '',
      }) : [];
      timelineTracks[track.id] = rows.map((item) => item.id);
      for (const item of rows) captured.set(item.id, item);
    }
    return {
      ...structuredClone(spec),
      snapshot: {
        captured_at: '2026-08-12T12:00:00Z',
        entities: [],
        links: [],
        timeline_items: [...captured.values()],
        timeline_tracks: timelineTracks,
      },
    };
  };

  await page.addInitScript(({ caseId }) => {
    if (caseId) localStorage.setItem('azimut:lastCase', caseId);
    localStorage.setItem('azimut:theme', 'dark');
  }, {
    caseId: options.lastCase === undefined ? CASE_ID : options.lastCase,
  });

  await page.route('**/*', async (route) => {
    const request = route.request();
    const url = new URL(request.url());
    const path = url.pathname;

    // The railways are served straight from their provider — the proxy cannot
    // expand a `{s}` template. Answered here, with the same bytes as a proxied
    // tile, so a spec can prove the overlay was asked for without the run
    // touching the network.
    if (url.hostname.endsWith('.tiles.openrailwaymap.org')) {
      railTiles.push(request.url());
      return route.fulfill({ contentType: 'image/png', body: TILE_PNG });
    }

    // Esri's reference layers are drawn straight from their host too, and
    // Borders is on whenever a map opens — so every map spec asks for these.
    // Answered here, so no run touches the network to draw a border.
    if (url.hostname === 'services.arcgisonline.com') {
      referenceTiles.push(request.url());
      return route.fulfill({ contentType: 'image/png', body: CLEAR_TILE_PNG });
    }

    // The borders and the place names are OpenFreeMap's vector tiles, found
    // through its TileJSON, and Borders is on whenever a map opens. A tile is empty unless the
    // spec names a village (`placeNames`), so no label lands on what another
    // spec is looking at; with one, every tile holds that village.
    if (url.hostname === 'tiles.openfreemap.org') {
      placeNameTiles.push(request.url());
      if (path === '/planet') {
        return json(route, { tilejson: '3.0.0', tiles: [PLACE_TILES], minzoom: 0, maxzoom: 14 });
      }
      const body = options.placeNames ? placeTile(options.placeNames) : Buffer.alloc(0);
      return route.fulfill({ contentType: 'application/x-protobuf', body });
    }

    if (url.hostname !== '127.0.0.1') {
      unexpected.push(`${request.method()} ${request.url()}`);
      return route.abort('blockedbyclient');
    }

    if (path.startsWith('/@') || path.startsWith('/src/') || path.startsWith('/node_modules/') ||
        path === '/' || path === '/index.html' || path === '/favicon.svg') {
      return route.continue();
    }

    if (path === `/files/${CASE_ID}/${PANEL_PATH}`) {
      return route.fulfill({ contentType: 'image/svg+xml', body: PANEL_SVG });
    }
    if (path === `/files/${CASE_ID}/${GRAPH_THUMB}`) {
      return route.fulfill({ contentType: 'image/svg+xml', body: PANEL_SVG });
    }
    if (path.startsWith('/api/tiles/batch/')) {
      const keys = (url.searchParams.get('t') ?? '').split(',').filter(Boolean);
      return route.fulfill({ contentType: 'application/octet-stream', body: tileBatch(keys, TILE_PNG) });
    }
    if (path.startsWith('/api/tiles/')) {
      return route.fulfill({ contentType: 'image/png', body: TILE_PNG });
    }
    if (path === '/api/events') {
      return route.fulfill({ contentType: 'text/event-stream', body: '' });
    }
    if (path === '/api/settings/signature.png') {
      return route.fulfill({ status: 404, body: '' });
    }
    if (path === '/api/settings') return json(route, fixtureSettings);
    if (path === '/api/settings/prefs' && request.method() === 'PUT') {
      const payload = request.postDataJSON();
      const previousExportDirs = fixtureSettings.export_dirs;
      settingsWrites.push(payload);
      Object.assign(fixtureSettings, payload);
      if (payload.export_dirs) {
        fixtureSettings.export_dirs = { ...previousExportDirs, ...payload.export_dirs };
      }
      return json(route, fixtureSettings);
    }
    if (path === '/api/folders/roots') return json(route, { roots: fixtureFolderRoots });
    if (path === '/api/folders') {
      const wanted = url.searchParams.get('path');
      const view = fixtureFolderViews[wanted];
      return view ? json(route, view) : json(route, { detail: 'Folder not found' }, 404);
    }
    if (path === '/api/folders/create' && request.method() === 'POST') {
      const payload = request.postDataJSON();
      const separator = payload.parent.endsWith('/') ? '' : '/';
      const made = `${payload.parent}${separator}${payload.name}`;
      folderWrites.push({ ...payload, path: made });
      fixtureFolderViews[made] = {
        path: made,
        name: payload.name,
        parent: payload.parent,
        crumbs: [{ name: payload.name, path: made }],
        folders: [],
        truncated: false,
        writable: true,
      };
      return json(route, { name: payload.name, path: made });
    }
    if (path === '/api/settings/scrapers') return json(route, { scrapers: [] });
    // Read on mount by `checkForUpdatesOnStart`, before any tool and whatever the
    // update switch says, because it touches disk rather than the network. The
    // fixture has no extension folder, which is the state a fresh install is in.
    if (path === '/api/settings/extension') {
      return json(route, {
        path: '/tmp/fixture/extension',
        bundled: { version: '0.0.0-fixture', payload: 'fixture-payload' },
        folder: null,
        staged: false,
      });
    }
    if (path === '/api/settings/ffmpeg') {
      return json(route, { available: true, version: 'fixture', source: 'bundled', path: '/tmp/ffmpeg' });
    }
    if (path === '/api/settings/diagnostics') {
      return json(route, { kind: 'bug', title: 'Issue', report: 'Fixture report', url: 'https://example.invalid/issue' });
    }
    // Asked on mount, before any tool: the app has to know the workspace is
    // there and whether a folder in it is still waiting to become a case.
    if (path === '/api/settings/workspace') return json(route, workspaceStatus);
    if (path === '/api/workspace/folders') return json(route, workspaceFolders);
    if (path === '/api/templates') return json(route, { proof: [], post: [] });
    if (path === '/api/cases/relation-types') return json(route, relationTypes);
    if (path === '/api/cases/confidence-levels') return json(route, confidenceLevels);
    if (path === '/api/cases/entity-types') return json(route, entityTypes.map((entry) => ({
      ...entry,
      retypable: entry.manual && ['actor', 'asset', 'class', 'identifier'].includes(entry.family),
      mergeable: ['actor', 'asset', 'class', 'identifier', 'place'].includes(entry.family),
    })));
    if (path === '/api/cases/graph-lenses') return json(route, graphLenses);
    if (path === '/api/cases/bundles/inspect' && request.method() === 'POST') {
      bundleCalls.push({ kind: 'inspect' });
      return json(route, bundlePreview ?? { detail: 'No bundle fixture' }, bundlePreview ? 200 : 400);
    }
    if (path === '/api/cases') {
      return json(route, fixtureCases.map((item) => ({ entity_count: 1, ...item })));
    }
    const overview = fixtureCases.find((item) => path === `/api/cases/${item.id}`);
    if (overview) {
      if (caseDelays[overview.id]) {
        await new Promise((resolve) => setTimeout(resolve, caseDelays[overview.id]));
      }
      return json(route, overview);
    }
    const caseId = fixtureCases.find((item) => path.startsWith(`/api/cases/${item.id}/`))?.id;
    if (caseId && path === `/api/cases/${caseId}/map-layers` && request.method() === 'GET') {
      return json(route, []);
    }
    // Detect's top-bar activity reads the case's runs whenever a case opens,
    // whichever tool is showing, so this is part of opening a case rather than
    // something a spec asked for. A spec about Detect routes it itself.
    if (caseId && path === `/api/cases/${caseId}/analysis/runs` && request.method() === 'GET') {
      return json(route, []);
    }
    if (caseId && path === `/api/cases/${caseId}/analysis-views`) {
      if (request.method() === 'GET') {
        return json(route, {
          views: analysisViews.map(({ spec, ...view }) => ({
            ...view,
            snapshot_count: analysisSnapshotCount(spec),
          })),
        });
      }
      if (request.method() === 'POST') {
        const body = request.postDataJSON();
        const spec = body.mode === 'snapshot' && body.surface === 'timeline'
          ? captureTimelineSnapshot(body.spec)
          : structuredClone(body.spec ?? {});
        const saved = {
          id: `v_browser_${analysisViews.length + 1}`,
          ...body,
          spec,
          created_at: '2026-08-10T10:00:00Z',
          updated_at: '2026-08-10T10:00:00Z',
          snapshot_count: analysisSnapshotCount(spec),
        };
        analysisViews.push(saved);
        analysisWrites.push({ method: 'POST', caseId, body });
        return json(route, saved);
      }
    }
    const duplicateMatch = caseId && path.match(
      new RegExp(`^/api/cases/${caseId}/analysis-views/([^/]+)/duplicate$`)
    );
    if (duplicateMatch && request.method() === 'POST') {
      const source = analysisViews.find(
        (view) => view.id === decodeURIComponent(duplicateMatch[1])
      );
      if (!source) return json(route, { detail: 'Analysis view not found' }, 404);
      const body = request.postDataJSON();
      const saved = {
        ...structuredClone(source),
        id: `v_browser_${analysisViews.length + 1}`,
        name: body.name,
        created_at: '2026-08-10T10:00:00Z',
        updated_at: '2026-08-10T10:00:00Z',
      };
      analysisViews.push(saved);
      analysisWrites.push({ method: 'POST', body, duplicate: source.id });
      return json(route, saved);
    }
    const analysisMatch = caseId && path.match(
      new RegExp(`^/api/cases/${caseId}/analysis-views/([^/]+)$`)
    );
    if (analysisMatch) {
      const id = decodeURIComponent(analysisMatch[1]);
      const index = analysisViews.findIndex((view) => view.id === id);
      if (index < 0) return json(route, { detail: 'Analysis view not found' }, 404);
      if (request.method() === 'GET') return json(route, analysisViews[index]);
      if (request.method() === 'PUT') {
        const body = request.postDataJSON();
        analysisViews[index] = {
          ...analysisViews[index],
          ...body,
          id,
          updated_at: '2026-08-10T11:00:00Z',
        };
        analysisWrites.push({ method: 'PUT', caseId, id, body });
        return json(route, analysisViews[index]);
      }
      if (request.method() === 'DELETE') {
        analysisViews.splice(index, 1);
        analysisWrites.push({ method: 'DELETE', id });
        return json(route, { status: 'deleted', deleted: [id], trash: 'trash-view-1' });
      }
    }
    // Pins are held per lens, keyed `lens|entity`, because that is how the case
    // stores them: an arrangement belongs to one reading.
    const pinsIn = (lens) => [...graphPins.keys()].filter((key) => key.startsWith(`${lens}|`));
    if (caseId && path === `/api/cases/${caseId}/graph/pins`) {
      if (request.method() === 'PUT') {
        const body = JSON.parse(request.postData());
        graphPinWrites.push({ caseId, body });
        for (const pin of body.pins) {
          graphPins.set(`${body.lens}|${pin.id}`, { x: pin.x, y: pin.y });
        }
        return json(route, { lens: body.lens, pinned: pinsIn(body.lens).length });
      }
      if (request.method() === 'DELETE') {
        const lens = url.searchParams.get('lens') ?? 'all';
        for (const key of pinsIn(lens)) graphPins.delete(key);
        return json(route, { lens, pinned: 0 });
      }
    }
    const unpinned = caseId
      && request.method() === 'DELETE'
      && path.startsWith(`/api/cases/${caseId}/graph/pins/`);
    if (unpinned) {
      const lens = url.searchParams.get('lens') ?? 'all';
      graphPins.delete(`${lens}|${path.slice(`/api/cases/${caseId}/graph/pins/`.length)}`);
      return json(route, { lens, pinned: pinsIn(lens).length });
    }
    if (caseId && path === `/api/cases/${caseId}/graph`) {
      graphQueries.push({ caseId, search: url.search });
      if (graphDelays[caseId]) {
        await new Promise((resolve) => setTimeout(resolve, graphDelays[caseId]));
      }
      const savedView = analysisViews.find((view) => view.id === url.searchParams.get('view'));
      if (savedView?.mode === 'snapshot') {
        const snapshot = savedView.spec?.snapshot ?? { entities: [], links: [] };
        const ids = new Set(snapshot.entities.map((entity) => entity.id));
        const links = snapshot.links.filter((link) => ids.has(link.from) && ids.has(link.to));
        const degrees = new Map(snapshot.entities.map((entity) => [entity.id, 0]));
        for (const link of links) {
          degrees.set(link.from, degrees.get(link.from) + 1);
          degrees.set(link.to, degrees.get(link.to) + 1);
        }
        return json(route, {
          lens: url.searchParams.get('lens') ?? 'all',
          order: 'snapshot',
          nodes: snapshot.entities.map((entity) => ({
            id: entity.id,
            type: entity.type,
            label: entity.label,
            family: 'actor',
            status: entity.provenance?.status ?? 'confirmed',
            at: entity.provenance?.at ?? '',
            degree: degrees.get(entity.id),
          })),
          links,
          total: snapshot.entities.length,
          shown: snapshot.entities.length,
          truncated: false,
          isolated: [...degrees.values()].filter((degree) => degree === 0).length,
          single_account: 0,
          kept: [],
          expanded: [],
          pinned: 0,
          snapshot: true,
          captured_at: snapshot.captured_at,
        });
      }
      const chosen = options.graphs?.[caseId] ?? options.graph;
      const override = typeof chosen === 'function'
        ? await chosen({ caseId, url, catalog: fixtureCatalog, links: caseLinks })
        : chosen;
      return json(route, graphView(url, override, graphPins, caseLinks));
    }
    if (caseId && path === `/api/cases/${caseId}/graph/neighborhood`) {
      return json(route, graphHood(url, options.graph, caseLinks));
    }

    if (caseId && path === `/api/cases/${caseId}/graph/paths`) {
      return json(route, graphPaths(url, options.graph, caseLinks));
    }

    const timelineClaimMatch = caseId && path.match(
      new RegExp(`^/api/cases/${caseId}/timeline/claims(?:/([^/]+))?$`)
    );
    if (timelineClaimMatch?.[1] && request.method() === 'GET') {
      const ownerId = decodeURIComponent(timelineClaimMatch[1]);
      const held = timelineRows.map(enrichedTimelineItem).find((item) => item.owner_id === ownerId);
      return held ? json(route, { item: held }) : json(route, { detail: 'not a claim' }, 404);
    }
    if (timelineClaimMatch && request.method() !== 'GET') {
      const body = request.postDataJSON();
      const ownerId = timelineClaimMatch[1] ?? `claim-browser-${timelineRows.length + 1}`;
      timelineWrites.push({ method: request.method(), ownerId, body });
      const index = timelineRows.findIndex((item) => item.owner_id === ownerId);
      const raw = body.when ?? (index >= 0 ? timelineRows[index].raw : null);
      const reading = fixtureTemporalReading(raw);
      const temporal = {
        id: `temporal:claim:${ownerId}`,
        owner_id: ownerId,
        category: 'statement',
        kind: 'claim',
        label: body.statement ?? (index >= 0 ? timelineRows[index].label : 'Assessment'),
        raw,
        ...reading,
        time_role: body.time_role ?? null,
        status: 'confirmed',
        confidence: body.confidence ?? null,
        parse_error: null,
        subjects: body.about ?? [],
        places: body.at ?? [],
        sources: body.cites ?? [],
      };
      if (index >= 0) timelineRows[index] = temporal;
      else timelineRows.push(temporal);
      // New subjects named on the entry line are filed with the claim, as the route does.
      const created = (body.create ?? []).map((entry, at) => ({
        id: `${entry.type}-browser-${timelineRows.length}-${at + 1}`, ...entry,
      }));
      for (const entry of created) {
        fixtureCatalog.push({
          id: entry.id, type: entry.type, label: entry.label, attrs: {},
          provenance: { by: 'user', at: '2026-08-12T10:00:00Z', status: 'confirmed' },
        });
        temporal[{ about: 'subjects', at: 'places', cites: 'sources' }[entry.slot]].push(entry.id);
      }
      return json(route, {
        entity: { id: ownerId, type: 'claim', label: temporal.label, attrs: body, provenance: { by: 'user', at: '2026-08-12T10:00:00Z', status: 'confirmed' } },
        links: [],
        created,
        temporal,
      });
    }
    if (caseId && path === `/api/cases/${caseId}/timeline/map`) {
      const from = url.searchParams.get('from');
      const to = url.searchParams.get('to');
      // Everything dated in the window that the case has put somewhere, in whichever
      // categories the caller asked for — `at` is a Claim's connector, and a layer
      // reading it alone answered a window of located photographs with nothing.
      // `matched` counts the window; `mapped` counts the ones a marker can be put on,
      // so the card can say how much it is not showing.
      const wanted = url.searchParams.getAll('category').flatMap((value) => value.split(','));
      const matched = scopedTimelineRows({
        categories: wanted.filter(Boolean).length ? wanted.filter(Boolean) : ['statement', 'media'],
        from, to, includeUndated: false,
      });
      const mapped = matched
        .map((item) => ({
          ...item,
          place_entities: positionedPlaces(item),
          owner: mapOwner(item),
        }))
        .filter((item) => item.place_entities.length);
      return json(route, {
        items: mapped,
        matched: matched.length,
        mapped: mapped.length,
        marks: mapped.reduce((count, item) => count + item.place_entities.length, 0),
        truncated: false,
        window: { from, to },
      });
    }

    if (caseId && path === `/api/cases/${caseId}/timeline`) {
      const wanted = url.searchParams.getAll('category');
      const entity = url.searchParams.get('entity');
      let track = {};
      try {
        track = JSON.parse(url.searchParams.get('track') ?? '{}');
      } catch {
        return json(route, { detail: 'Invalid timeline track' }, 400);
      }
      const base = scopedTimelineRows({ categories: wanted, entity, track });
      const datedBase = base.filter((item) => item.earliest);
      const from = url.searchParams.get('from');
      const to = url.searchParams.get('to');
      const includeUndated = url.searchParams.get('include_undated') !== 'false';
      const visible = scopedTimelineRows({
        categories: wanted, entity, track, from, to, includeUndated,
      });
      const bucket = url.searchParams.get('bucket');
      const width = { year: 4, month: 7, day: 10 }[bucket] ?? 0;
      const bucketCounts = new Map();
      if (width) for (const item of datedBase) {
        const key = item.earliest.slice(0, width);
        const held = bucketCounts.get(key) ?? { count: 0, categories: {} };
        held.count += 1;
        held.categories[item.category] = (held.categories[item.category] ?? 0) + 1;
        bucketCounts.set(key, held);
      }
      const lane = ['sources', 'imagery'].includes(track.as_files) ? track.as_files : null;
      return json(route, {
        items: lane
          ? visible.map((item) => {
            const file = laneFile(item, lane);
            return { ...item, file: { id: file.id, label: file.label, type: file.type } };
          })
          : visible,
        next_cursor: null,
        total: visible.length,
        undated: base.filter((item) => !item.earliest && !item.raw).length,
        unplaced: base.filter((item) => !item.earliest && item.raw).length,
        buckets: [...bucketCounts]
          .sort(([left], [right]) => left.localeCompare(right))
          .map(([start, held]) => ({ start, count: held.count, categories: held.categories })),
        buckets_truncated: false,
        extent: {
          from: datedBase.map((item) => item.earliest).sort()[0] ?? null,
          to: datedBase.map((item) => item.latest).sort().at(-1) ?? null,
        },
        window: { from, to },
      });
    }

    if (caseId && path === `/api/cases/${caseId}/satellite/index`) {
      if (savedIndexDelays[caseId]) {
        await new Promise((resolve) => setTimeout(resolve, savedIndexDelays[caseId]));
      }
      return json(route, fixtureSavedIndexes[caseId] ?? []);
    }
    if (caseId && path === `/api/cases/${caseId}/satellite/media`) {
      return json(route, fixtureMediaIndexes[caseId] ?? []);
    }
    // The case as the catalog answers for it: a spec's own list, or the drawn case so
    // the fixture stands for one case rather than two — the graph's search reads the
    // catalog, and it has to be able to find an entity the drawing left out.
    const catalogRows = () =>
      fixtureCatalog.length
        ? fixtureCatalog
        : graphNodes.map((node) => ({
            id: node.id,
            type: node.type,
            label: node.label,
            attrs: {
              ...(node.folder ? { folder: node.folder } : {}),
              ...(node.kind ? { kind: node.kind } : {}),
              ...(node.attrs ?? {}),
            },
            provenance: { by: 'user', at: node.at, status: node.status },
          }));
    if (caseId && path === `/api/cases/${caseId}/catalog/summary`) {
      if (options.summary) return json(route, options.summary);
      // The count the surface shows when nothing is filtered, so it has to be the
      // catalog's own size rather than a fixed 1 — otherwise "Showing 2 of 1".
      //
      // `by_type` is every type the case holds, which is what the graph resolves its
      // family switches against: a spec that set a catalog up is standing for that
      // case, one that did not is standing for the drawn one. Hard-coded, the graph's
      // legend would offer one family for a case holding five.
      const byType = {};
      const rows = catalogRows();
      for (const entity of rows) {
        byType[entity.type] = (byType[entity.type] ?? 0) + 1;
      }
      const typeOf = new Map(rows.map((entity) => [entity.id, entity.type]));
      const linkedTo = {};
      for (const entity of rows) {
        const reached = new Set();
        for (const link of caseLinks) {
          if (link.from === entity.id && typeOf.get(link.to)) reached.add(typeOf.get(link.to));
          if (link.to === entity.id && typeOf.get(link.from)) reached.add(typeOf.get(link.from));
        }
        for (const type of reached) linkedTo[type] = (linkedTo[type] ?? 0) + 1;
      }
      return json(route, {
        total: fixtureCatalog.length || 1,
        by_type: byType,
        by_status: { confirmed: 1 },
        by_folder: {},
        linked_to: linkedTo,
      });
    }
    // Which stored fields the listed types hold, and their values: the menus a field
    // filter is chosen from. Derived from the same rows the page is, so the fixture
    // cannot offer a term the mocked case could not answer.
    if (caseId && path === `/api/cases/${caseId}/catalog/attributes`) {
      const wanted = (url.searchParams.get('type') ?? '').split(',').filter(Boolean);
      const counts = new Map();
      for (const entity of catalogRows()) {
        if (wanted.length && !wanted.includes(entity.type)) continue;
        for (const [key, value] of Object.entries(entity.attrs ?? {})) {
          if (typeof value !== 'string' || !value) continue;
          const held = counts.get(key) ?? new Map();
          held.set(value, (held.get(value) ?? 0) + 1);
          counts.set(key, held);
        }
      }
      return json(route, {
        attrs: [...counts].map(([key, values]) => ({
          key,
          entities: [...values.values()].reduce((sum, n) => sum + n, 0),
          values: [...values]
            .sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0]))
            .map(([value, count]) => ({ value, count })),
          truncated: false,
        })),
      });
    }
    if (caseId && path === `/api/cases/${caseId}/catalog/entities`) {
      // Filtered here rather than returned whole, so a spec can prove the surface
      // asked the server to narrow instead of hiding rows it had already loaded.
      const wanted = (url.searchParams.get('type') ?? '').split(',').filter(Boolean);
      const term = (url.searchParams.get('q') ?? '').toLowerCase();
      const status = url.searchParams.get('status') ?? '';
      const field = url.searchParams.get('attr') ?? '';
      const value = url.searchParams.get('value') ?? '';
      const linked = url.searchParams.get('linked') ?? '';
      const folder = url.searchParams.get('folder') ?? '';
      const unfiled = url.searchParams.get('unfiled') === 'true';
      const recursive = url.searchParams.get('recursive') === 'true';
      const filedIn = (entity) => {
        const at = entity.attrs?.folder ?? '';
        if (unfiled) return !at;
        if (!folder) return true;
        return at === folder || (recursive && at.startsWith(`${folder}/`));
      };
      const savedView = analysisViews.find(
        (view) => view.id === url.searchParams.get('view')
      );
      const held = savedView?.mode === 'snapshot'
        ? savedView.spec?.snapshot?.entities ?? []
        : catalogRows();
      const typeOf = new Map(held.map((entity) => [entity.id, entity.type]));
      // The one-hop test, over the same edges the graph answers with. Undirected,
      // because "linked to a place" is a question about the pair.
      const reaches = (id, wantedType) =>
        caseLinks.some(
          (link) =>
            (link.from === id && typeOf.get(link.to) === wantedType) ||
            (link.to === id && typeOf.get(link.from) === wantedType)
        );
      // A fact-time window reaches an entity through its own dates and through the
      // statements that name it, which is the join the server makes: a person is in
      // June because something said about them happened in June.
      const temporalFrom = url.searchParams.get('temporal_from') ?? '';
      const temporalTo = url.searchParams.get('temporal_to') ?? '';
      const inPeriod = temporalFrom && temporalTo
        ? new Set(scopedTimelineRows({
            categories: (url.searchParams.get('temporal_category') ?? '').split(',').filter(Boolean),
            from: temporalFrom,
            to: temporalTo,
            includeUndated: false,
          }).flatMap((item) => [
            item.owner_id, ...item.subjects ?? [], ...item.places ?? [], ...item.sources ?? [],
          ]))
        : null;
      const matching = held.filter(
        (entity) =>
          (!wanted.length || wanted.includes(entity.type)) &&
          (!status || entity.provenance?.status === status) &&
          (!term || [entity.label, entity.type, ...Object.values(entity.attrs ?? {})]
            .some((heldValue) => String(heldValue ?? '').toLowerCase().includes(term))) &&
          (!field || !value || String(entity.attrs?.[field] ?? '') === value) &&
          (!linked || reaches(entity.id, linked)) &&
          filedIn(entity) &&
          (!inPeriod || inPeriod.has(entity.id))
      );
      const order = url.searchParams.get('order') ?? '';
      const descending = order.startsWith('-');
      const column = descending ? order.slice(1) : order;
      const direction = descending ? -1 : 1;
      // "Most noted": the Claims naming each row, newest first on a tie, as the store
      // orders it.
      const eventsOf = (id) => new Set(caseLinks
        .filter((link) => link.to === id && ['about', 'at', 'cites'].includes(link.type)
          && typeOf.get(link.from) === 'claim')
        .map((link) => link.from)).size;
      const ordered = column === 'events'
        ? [...matching].map((entity, at) => ({ entity, at, n: eventsOf(entity.id) }))
          .sort((left, right) => (left.n - right.n) * direction || (right.at - left.at))
          .map((entry) => entry.entity)
        : column
          ? [...matching].sort((left, right) => {
              const a = column === 'created' ? left.provenance?.at : left.label;
              const b = column === 'created' ? right.provenance?.at : right.label;
              return String(a ?? '').localeCompare(String(b ?? '')) * direction;
            })
          : matching;
      catalogQueries.push(url.search);
      // `catalogPage` makes the fixture answer in pages, which is the only way to
      // reach the surface's server-search mode: a case that fits one page filters
      // in the browser and never asks a second question. The cursor is the offset
      // of the next page, so a surface that reads every page gets to the end.
      const page = options.catalogPage ?? 0;
      const from = Number(url.searchParams.get('cursor')) || 0;
      const items = page ? ordered.slice(from, from + page) : ordered;
      const byType = {};
      for (const entity of matching) byType[entity.type] = (byType[entity.type] ?? 0) + 1;
      return json(route, {
        items,
        next_cursor: page && matching.length > from + page ? String(from + page) : null,
        total: matching.length,
        ...(url.searchParams.get('counts') === 'type' ? { by_type: byType } : {}),
      });
    }
    // What the Claims say about a page of rows: the count naming each, from the edges
    // this fixture holds. Dates stay empty, so no row draws a density it was not given.
    if (caseId && path === `/api/cases/${caseId}/catalog/events` && request.method() === 'POST') {
      const { ids = [] } = request.postDataJSON() ?? {};
      const claims = new Set(catalogRows().filter((entity) => entity.type === 'claim').map((entity) => entity.id));
      const rows = Object.fromEntries(ids.map((id) => [id, {
        events: new Set(caseLinks
          .filter((link) => link.to === id && claims.has(link.from) && ['about', 'at', 'cites'].includes(link.type))
          .map((link) => link.from)).size,
        first: null, last: null, sources: 0, places: 0, buckets: Array(12).fill(0),
      }]));
      return json(route, { range: null, rows });
    }
    const galleryMatch = caseId && path.match(
      new RegExp(`^/api/cases/${caseId}/entities/([^/]+)/images(?:/([^/]+)(/primary)?)?$`)
    );
    if (galleryMatch) {
      const entityId = decodeURIComponent(galleryMatch[1]);
      const imageId = galleryMatch[2] ? decodeURIComponent(galleryMatch[2]) : null;
      const held = entityImages.get(entityId) ?? [];
      entityImages.set(entityId, held);
      const syncThumb = () => {
        const entity = fixtureCatalog.find((item) => item.id === entityId);
        const primary = held.find((image) => image.primary);
        if (!entity) return;
        if (primary) entity.thumb = primary.thumbnail ?? primary.path;
        else delete entity.thumb;
      };
      if (request.method() === 'GET' && !imageId) return json(route, { images: held });
      if (request.method() === 'POST' && imageId === 'upload') {
        const uploaded = {
          id: `photo-direct-${held.length + 1}`,
          media_id: null,
          direct: true,
          path: PANEL_PATH,
          thumbnail: null,
          title: 'Portrait',
          kind: 'image',
          position: held.length,
          primary: held.length === 0,
          ...(options.entityImageUploadResult ?? {}),
        };
        held.push(uploaded);
        syncThumb();
        galleryWrites.push({ method: 'UPLOAD', entityId, imageId: uploaded.id });
        return json(route, { added: 1, images: held });
      }
      if (request.method() === 'POST' && !imageId) {
        const body = request.postDataJSON();
        let added = 0;
        for (const id of body.media_ids) {
          if (held.some((image) => image.media_id === id)) continue;
          const item = fixtureMedia.find((candidate) => candidate.entity_id === id)
            ?? (options.uploadResult?.entity?.id === id ? options.uploadResult.item : null);
          held.push({
            ...(item ?? { path: PANEL_PATH, title: 'Photo', kind: 'image' }),
            id,
            media_id: id,
            direct: false,
            position: held.length,
            primary: held.length === 0,
          });
          added += 1;
        }
        syncThumb();
        galleryWrites.push({ method: 'POST', entityId, mediaIds: body.media_ids });
        return json(route, { added, images: held });
      }
      if (request.method() === 'PUT' && imageId && galleryMatch[3]) {
        for (const image of held) image.primary = image.id === imageId;
        syncThumb();
        galleryWrites.push({ method: 'PUT', entityId, imageId });
        return json(route, { images: held });
      }
      if (request.method() === 'DELETE' && imageId) {
        const removed = held.findIndex((image) => image.id === imageId);
        if (removed !== -1) held.splice(removed, 1);
        if (held.length && !held.some((image) => image.primary)) held[0].primary = true;
        syncThumb();
        galleryWrites.push({ method: 'DELETE', entityId, imageId });
        return json(route, { images: held });
      }
    }
    // The identifier already in the case under this value, the way `identity_key`
    // compares: case aside, and a handle with or without its sigil.
    if (caseId && path === `/api/cases/${caseId}/entities/twin`) {
      const type = url.searchParams.get('type');
      const key = (value) => String(value ?? '').trim().toLowerCase().replace(/^@/, '');
      const wanted = key(url.searchParams.get('label'));
      const held = fixtureCatalog.find((entity) => entity.type === type && key(entity.label) === wanted);
      return json(route, { entity: held ?? null });
    }
    // A grouped delete, one trash group: what the entry line's Undo sends.
    if (caseId && path === `/api/cases/${caseId}/entities/delete` && request.method() === 'POST') {
      const { ids } = request.postDataJSON();
      entityWrites.push({ method: 'DELETE', ids });
      for (const id of ids) {
        const at = timelineRows.findIndex((item) => item.owner_id === id);
        if (at >= 0) timelineRows.splice(at, 1);
        const index = fixtureCatalog.findIndex((entity) => entity.id === id);
        if (index >= 0) fixtureCatalog.splice(index, 1);
      }
      return json(route, { status: 'deleted', deleted: ids, tombstoned: [], trash: 'trash-1' });
    }
    const entityMatch = caseId && path.match(new RegExp(`^/api/cases/${caseId}/entities/([^/]+)$`));
    if (entityMatch && request.method() !== 'GET') {
      const body = request.method() === 'DELETE' ? null : request.postDataJSON();
      entityWrites.push({ method: request.method(), id: entityMatch[1], body });
      const index = fixtureCatalog.findIndex((entity) => entity.id === entityMatch[1]);
      if (index >= 0) {
        if (request.method() === 'DELETE') fixtureCatalog.splice(index, 1);
        else if (body?.status) fixtureCatalog[index].provenance.status = body.status;
      }
      // the delete route's own shape: what went, and the trash group to put it back
      if (request.method() === 'DELETE') {
        return json(route, {
          status: 'deleted',
          deleted: [entityMatch[1]],
          tombstoned: [],
          trash: 'trash-1',
        });
      }
      return json(route, fixtureCatalog[index] ?? { id: entityMatch[1], ...body });
    }
    if (caseId && path === `/api/cases/${caseId}/entities` && request.method() === 'POST') {
      const payload = request.postDataJSON();
      entityWrites.push(payload);
      const created = {
        id: options.newEntityId ?? 'e_new',
        ...payload,
        provenance: { by: 'user', at: '2026-08-03T10:00:00Z', status: 'confirmed' },
      };
      fixtureCatalog.push(created);
      return json(route, created);
    }
    if (caseId && path === `/api/cases/${caseId}/trash`) {
      if (request.method() === 'DELETE') {
        trashWrites.push({ kind: 'empty' });
        const purged = trashGroups.length;
        trashGroups.splice(0);
        return json(route, { purged });
      }
      return json(route, {
        groups: trashGroups,
        items: trashGroups.reduce((total, group) => total + group.item_count, 0),
        size_bytes: trashGroups.reduce((total, group) => total + group.size_bytes, 0),
      });
    }
    const trashMatch = caseId && path.match(new RegExp(`^/api/cases/${caseId}/trash/([^/]+)(/restore)?$`));
    if (trashMatch) {
      const groupId = trashMatch[1];
      const index = trashGroups.findIndex((group) => group.id === groupId);
      if (index < 0) return json(route, { detail: 'Trash group not found' }, 404);
      const kind = trashMatch[2] ? 'restore' : 'purge';
      trashWrites.push({ kind, groupId });
      trashGroups.splice(index, 1);
      return json(route, kind === 'restore' ? { status: 'restored' } : { status: 'purged' });
    }
    if (caseId && path === `/api/cases/${caseId}/bundle/export` && request.method() === 'POST') {
      bundleCalls.push({ kind: 'export', body: request.postDataJSON() });
      return json(route, { job_id: 'bundle-job' });
    }
    if (caseId && path === `/api/cases/${caseId}/bundle/jobs/bundle-job`) {
      bundleCalls.push({ kind: 'job' });
      return json(route, bundleJob);
    }
    if (caseId && path === `/api/cases/${caseId}/search-grids`) {
      return json(route, [...fixtureGrids.values()].map(({ name, spec, title }) => ({
        name,
        title,
        cells: Object.keys(spec.statuses ?? {}).length,
        cleared: Object.values(spec.statuses ?? {}).filter((s) => s === 'cleared').length,
        flagged: Object.values(spec.statuses ?? {}).filter((s) => s === 'flagged').length,
      })));
    }
    // The sweep auto-saves on every mark, so a spec can read the marks back out
    // of what was written rather than out of the canvas they are painted on.
    const gridMatch = caseId && path.match(new RegExp(`^/api/cases/${caseId}/search-grids/(.+)$`));
    if (gridMatch) {
      const name = gridMatch[1];
      if (request.method() === 'PUT') {
        const body = request.postDataJSON();
        fixtureGrids.set(name, { name, spec: body.spec, title: body.title });
        gridWrites.push({ name, spec: body.spec });
        return json(route, { status: 'saved' });
      }
      if (request.method() === 'DELETE') {
        fixtureGrids.delete(name);
        return json(route, { status: 'deleted' });
      }
      const held = fixtureGrids.get(name);
      return held ? json(route, held.spec) : json(route, { detail: 'not found' }, 404);
    }
    const workMatch = path.match(/^\/api\/cases\/([^/]+)\/horizon\/work$/);
    if (workMatch) {
      const owner = decodeURIComponent(workMatch[1]);
      if (request.method() === 'PUT') {
        const body = request.postDataJSON();
        horizonWork.set(owner, body);
        workWrites.push(body);
        return json(route, { ok: true });
      }
      if (request.method() === 'DELETE') return json(route, { cleared: horizonWork.delete(owner) });
      const held = horizonWork.get(owner) ?? null;
      return json(route, { work: held, photo_here: held?.photo ? true : null });
    }
    const placementMatch = caseId && path.match(
      new RegExp(`^/api/cases/${caseId}/entities/(.+)/placement$`)
    );
    if (placementMatch) {
      return json(
        route,
        options.placements?.[placementMatch[1]] ?? { points: [], truncated: false }
      );
    }
    // What the statements about one entity come to. Details asks for it whenever the
    // Connections tab is on screen, so every spec that opens a panel reaches it. 404 is the
    // ordinary answer — nothing states anything about this row — and `tallies` is how
    // a spec gives one a total.
    const tallyMatch = caseId && path.match(
      new RegExp(`^/api/cases/${caseId}/entities/(.+)/tally$`)
    );
    if (tallyMatch) {
      const row = options.tallies?.[tallyMatch[1]];
      return row ? json(route, row) : route.fulfill({ status: 404, body: '{}' });
    }
    if (caseId && path === `/api/cases/${caseId}/entities/redirects`) return json(route, { redirects: {} });
    if (caseId && path.match(new RegExp(`^/api/cases/${caseId}/entities/[^/]+/merges$`))) return json(route, { merges: [] });
    const chainMatch = caseId && path.match(new RegExp(`^/api/cases/${caseId}/entities/(.+)/chain$`));
    if (chainMatch) {
      const chain = fixtureChains[chainMatch[1]];
      return json(route, chain ?? { entity: null, sources: [], lost: [], dependents: [], relations: [], empty: true });
    }
    const linkMatch = caseId && path.match(new RegExp(`^/api/cases/${caseId}/links(?:/(.+))?$`));
    if (linkMatch && request.method() !== 'GET') {
      const body = request.method() === 'DELETE' ? null : request.postDataJSON();
      linkWrites.push({ method: request.method(), id: linkMatch[1] ?? null, body });
      // The write lands in the case, so the next read answers with it. A stub that
      // recorded the request and forgot it made every surface look like it had
      // written nothing.
      if (request.method() === 'DELETE') {
        const gone = caseLinks.findIndex((link) => link.id === linkMatch[1]);
        if (gone !== -1) caseLinks.splice(gone, 1);
        return json(route, { status: 'deleted' });
      }
      if (linkMatch[1]) {
        // The drawn case is not the only place a fixture holds edges — a `chains`
        // entry carries its own — so an id this list does not know is echoed back
        // rather than refused, which is what the real route would answer for it.
        const held = caseLinks.find((link) => link.id === linkMatch[1]);
        if (held) {
          if (body.type) held.type = body.type;
          if (body.status) held.provenance.status = body.status;
        }
        return json(route, held ?? { id: linkMatch[1], ...body });
      }
      minted += 1;
      const filed = {
        id: `link-new-${minted}`,
        from: body.from_id,
        to: body.to_id,
        type: body.type,
        provenance: { by: 'user', at: '2026-08-03T09:00:00Z', status: 'confirmed' },
      };
      caseLinks.push(filed);
      return json(route, filed);
    }
    if (caseId && path === `/api/cases/${caseId}/media/reveal` && request.method() === 'POST') {
      revealed.push(request.postDataJSON()?.path ?? '');
      return json(route, { path: '/workspace/cases/browser-test/azimut/media' });
    }
    if (caseId && path === `/api/cases/${caseId}/media/paste` && request.method() === 'POST') {
      // The clipboard route answers the shape `upload` does, plus whatever the paste
      // dialog stated: the title that names the file and the origin it typed.
      const body = request.postData() ?? '';
      const field = (name) => body.match(new RegExp(`name="${name}"\\r?\\n\\r?\\n([^\\r]*)`))?.[1] ?? '';
      pastes.push({ title: field('title'), source_url: field('source_url'), length: body.length });
      const result = options.pasteResult ?? {
        duplicate: false,
        entity: {
          id: 'media-pasted',
          type: 'media',
          label: field('title') || 'paste-20260811-120000',
          attrs: { path: 'media/pasted.png', kind: 'image', sha256: 'def' },
          provenance: { by: 'paste', at: '2026-08-11T12:00:00Z', status: 'confirmed' },
        },
        item: { path: 'media/pasted.png', kind: 'image' },
      };
      if (!result.duplicate && result.entity) fixtureCatalog.push(result.entity);
      return json(route, result);
    }
    if (caseId && path === `/api/cases/${caseId}/media/upload` && request.method() === 'POST') {
      // What the importer answers: the entity it filed, with the kind it read off
      // the bytes. `uploadResult` lets a spec make it a duplicate instead.
      uploads.push({ length: request.postData()?.length ?? 0 });
      const result = options.uploadResult ?? {
        duplicate: false,
        entity: {
          id: 'media-imported',
          type: 'media',
          label: 'site plan',
          attrs: { path: 'media/site plan.pdf', kind: 'file', sha256: 'abc' },
          provenance: { by: 'media-library', at: '2026-08-04T09:00:00Z', status: 'confirmed' },
        },
        item: { path: 'media/site plan.pdf', kind: 'file' },
      };
      if (!result.duplicate && result.entity) fixtureCatalog.push(result.entity);
      return json(route, result);
    }
    if (
      options.mediaDownloadJob &&
      caseId &&
      path === `/api/cases/${caseId}/media/download` &&
      request.method() === 'POST'
    ) {
      mediaDownloadWrites.push(request.postDataJSON());
      return json(route, { job_id: 'media-download-job' });
    }
    if (options.mediaDownloadJob && path === '/api/jobs/media-download-job') {
      return json(route, options.mediaDownloadJob);
    }
    if (path === `/api/cases/${CASE_ID}/media/page`) {
      return json(route, {
        items: fixtureMedia,
        next_cursor: null,
        total: fixtureMedia.length,
        facets: {
          category_counts: { image: fixtureMedia.length, upload: fixtureMedia.length },
          folder_counts: {},
          gps_count: 0,
          thumbnail_pending: 0,
        },
      });
    }
    if (path === `/api/cases/${CASE_ID}/media`) return json(route, fixtureMedia);
    // Inspect and Collage open on their pickers: no file worked on and no
    // collage saved unless a spec brings some.
    if (path === '/api/inspect/ops') return json(route, { filters: [], analyses: [] });
    if (path === `/api/cases/${CASE_ID}/inspect/works`) return json(route, options.inspectWorks ?? []);
    if (path === `/api/cases/${CASE_ID}/collages`) return json(route, options.collages ?? []);
    // One media file with everything its sidecar holds. The browse index leaves
    // enrichment's metadata dumps out, so the Details panel reads a file at a time.
    if (path === `/api/cases/${CASE_ID}/media/item`) {
      const wanted = url.searchParams.get('path');
      const found = fixtureMedia.find((item) => item.path === wanted);
      return found ? json(route, found) : route.fulfill({ status: 404, body: '{}' });
    }
    if (path === `/api/cases/${CASE_ID}/media/export` && request.method() === 'POST') {
      const payload = request.postDataJSON();
      exportWrites.push(payload);
      return json(route, {
        file: 'panel.svg',
        folder: fixtureSettings.export_dirs.media || "the case's exports folder",
      });
    }
    // Two different reads: the capture shelf (pickers list it beside media) and
    // the geo index the Map panel groups. Both are needed — a picker whose
    // shelf 404s shows no panels at all.
    if (path === `/api/cases/${CASE_ID}/satellite`) return json(route, options.satCaptures ?? []);
    if (path === `/api/cases/${CASE_ID}/drafts`) {
      return json(route, Object.entries(fixtureDrafts).map(([name, draft]) => ({
        name,
        title: draft.title,
        updated_at: draft.updated_at,
        target: draft.state?.target,
      })));
    }
    if (path.startsWith(`/api/cases/${CASE_ID}/drafts/`)) {
      const name = path.slice(path.lastIndexOf('/') + 1);
      const draft = fixtureDrafts[name];
      return draft ? json(route, draft) : json(route, { detail: 'Draft not found' }, 404);
    }
    if (path === `/api/cases/${CASE_ID}/notes`) return json(route, { text: '' });
    if (path === `/api/cases/${CASE_ID}/entities/lookup`) {
      return json(route, { entity: fixtureLookupEntities[url.searchParams.get('value')] ?? null });
    }
    if (path === '/api/satellite/providers') return json(route, fixtureProviders);
    if (path === '/api/firms/sensors') {
      return json(route, {
        keyed: false,
        sensors: [
          { id: 'viirs', label: 'VIIRS (S-NPP + NOAA-20)' },
          { id: 'viirs_noaa20', label: 'VIIRS NOAA-20' },
          { id: 'modis', label: 'MODIS' },
        ],
        max_zoom: 10,
      });
    }
    // one billed map load, counted where it happens: the tile proxy cannot see
    // the Maps JS widget, so the tool reports it itself
    if (path.startsWith('/api/satellite/usage/') && request.method() === 'POST') {
      widgetLoads.push(path.split('/').at(-1));
      return json(route, { ok: true });
    }
    if (path === '/api/satellite/imagery-date') {
      return json(route, { supported: false, date: null, source: null });
    }
    if (path === `/api/cases/${CASE_ID}/satellite/capture` && request.method() === 'POST') {
      const payload = request.postDataJSON();
      captures.push(payload);
      return json(route, {
        path: `media/capture-${captures.length}.png`,
        title: `Capture ${captures.length}`,
        ...payload,
        provider_label: 'Esri World Imagery',
        attribution: 'Browser fixture',
        fetched_at: '2026-07-21T00:00:00Z',
        tiles_missing: 0,
        tiles_upscaled: 0,
      });
    }
    if (caseId && path === `/api/cases/${caseId}/satellite/place` && request.method() === 'POST') {
      const payload = request.postDataJSON();
      placeWrites.push({ caseId, ...payload });
      return json(route, {
        id: `place-${placeWrites.length}`,
        type: 'place',
        label: 'Saved place',
        attrs: payload,
      });
    }
    if (path === `/api/cases/${CASE_ID}/proofs` && request.method() === 'POST') {
      const payload = request.postDataJSON();
      proofSaves.push(payload);
      // The title comes back the way the backend answers, canonicalised: the
      // composer writes it straight into the header, so dropping it here left
      // the saved document holding a title the composer never gave it.
      return json(route, {
        id: 'browser-proof-entity',
        name: 'browser-proof',
        title: payload.title,
        png: 'proofs/browser-proof.png',
      });
    }

    // What a saved proof shares with the case, and the links the case proposes by
    // itself. Nothing by default, so a spec that never looks sees an empty answer.
    const kinMatch = path.match(new RegExp(`^/api/cases/${caseId}/entities/([^/]+)/kin$`));
    if (kinMatch) {
      kinReads.push(kinMatch[1]);
      return json(route, options.kin ?? { sites: [], places: [], accounts: [], radius: 300 });
    }
    if (path === `/api/cases/${caseId}/proposals`) {
      if (request.method() === 'POST') proposalRuns.push(Date.now());
      return json(route, {
        ...(request.method() === 'POST' ? { filed: { accounts: 0, posted: 0, sites: 0 } } : {}),
        ...fixtureProposals,
      });
    }
    const dropMatch = path.match(new RegExp(`^/api/cases/${caseId}/proposals/([^/]+)$`));
    if (dropMatch && request.method() === 'DELETE') {
      fixtureProposals.items = fixtureProposals.items.filter((item) => item.id !== dropMatch[1]);
      return json(route, { status: 'deleted', dropped: [], ...fixtureProposals });
    }
    if (path === `/api/cases/${caseId}/sheets/from-case/files`) {
      return json(route, options.filesWorklist ?? { total: 0, answered: 0, sheet: null });
    }

    if (path === `/api/cases/${caseId}/sheets` && request.method() === 'GET') {
      return json(route, {
        sheets: [...fixtureSheets.values()].map((held) => ({
          id: held.id,
          title: held.title,
          rows: held.rows.length,
          columns: held.columns.length,
        })),
      });
    }
    if (path === `/api/cases/${caseId}/sheets` && request.method() === 'POST') {
      const body = request.postDataJSON();
      const made = makeSheet(body.title, body.columns ?? ['Subject', 'Status', 'Notes']);
      return json(route, { id: made.id, label: made.title, type: 'sheet', attrs: { path: made.path } });
    }
    const sheetMatch = path.match(new RegExp(`^/api/cases/${caseId}/sheets/([^/]+)(/meta|/stamp)?$`));
    if (sheetMatch && fixtureSheets.has(sheetMatch[1])) {
      const held = fixtureSheets.get(sheetMatch[1]);
      if (sheetMatch[2] === '/stamp') return json(route, { stamp: held.stamp });
      if (request.method() === 'GET') return json(route, sheetPayload(held));
      const body = request.postDataJSON();
      // The sidecar's own route: it never touches the table, so it never moves the stamp.
      if (sheetMatch[2] === '/meta') {
        held.meta = body.meta ?? {};
        sheetWrites.push({ id: held.id, kind: 'meta' });
        return json(route, { status: 'saved', meta: structuredClone(held.meta) });
      }
      if (body.stamp !== undefined && body.stamp !== held.stamp) {
        return json(route, { detail: 'this file changed on disk since it was opened' }, 409);
      }
      held.columns = body.columns;
      held.rows = body.rows;
      held.meta = body.meta ?? {};
      held.stamp = nextStamp();
      sheetWrites.push({ id: held.id, kind: 'table', rows: body.rows.length });
      return json(route, { status: 'saved', ...sheetPayload(held) });
    }

    // The civil zone at a point, from the bundled boundaries: two zones are enough to
    // tell a place's clock from another's.
    if (path === '/api/geo/zone') {
      geoQueries.zone = [...(geoQueries.zone ?? []), url.search];
      const lon = Number(url.searchParams.get('lon'));
      return json(route, {
        name: lon > 20 ? 'Europe/Kyiv' : lon > 0 ? 'Europe/Paris' : lon > -30 ? 'Europe/Lisbon' : 'America/New_York',
      });
    }
    // The search bar's two layers. The offline one answers coordinates and a
    // handful of cities; the geocoder answers streets, and only ever late.
    if (path === '/api/geo/suggest') {
      const q = url.searchParams.get('q') ?? '';
      geoQueries.suggest.push(q);
      const decimal = /^\s*(-?\d{1,3}(?:\.\d+)?)\s*,\s*(-?\d{1,3}(?:\.\d+)?)\s*$/.exec(q);
      const cities = decimal
        ? []
        : GAZETTEER.filter((city) => city.name.toLowerCase().startsWith(q.trim().toLowerCase()));
      return json(route, {
        coords: decimal ? { lat: Number(decimal[1]), lon: Number(decimal[2]) } : null,
        cities,
        attribution: cities.length ? '© GeoNames (CC BY 4.0)' : null,
      });
    }
    // One day of sun and moon over a point: enough samples for the arc, the hour
    // ticks and the slider, with the sun up and the moon down at the chosen hour.
    if (path === '/api/geo/sky') {
      skyQueries.push({
        lat: Number(url.searchParams.get('lat')),
        lon: Number(url.searchParams.get('lon')),
        date: url.searchParams.get('date') ?? '',
      });
      const minutes = Array.from({ length: 25 }, (_, i) => i * 60);
      return json(route, {
        date: url.searchParams.get('date') || '2026-06-21',
        moment: { local: '2026-06-21T12:00:00', utc: '2026-06-21T10:00:00Z' },
        curve: {
          minutes,
          clock: minutes.map((m) => `${String(m / 60).padStart(2, '0')}:00`),
          sun_azimuth: minutes.map((m) => (m / 1440) * 360),
          sun_altitude: minutes.map((m) => Math.round(Math.sin((m / 1440) * Math.PI) * 60 - 5)),
          moon_azimuth: minutes.map((m) => ((m / 1440) * 360 + 180) % 360),
          moon_altitude: minutes.map((m) => Math.round(Math.sin((m / 1440) * Math.PI + 2) * 40)),
          moon_illuminated: minutes.map(() => 0.62),
        },
        sun: { rise: '05:45', set: '21:55', noon: '13:50' },
        moon: { phase: 'waxing gibbous', waxing: true, illuminated: 0.62 },
      });
    }
    if (path === '/api/geo/places') {
      const q = url.searchParams.get('q') ?? '';
      geoQueries.places.push(q);
      const places = GEOCODED.filter((place) =>
        place.display_name.toLowerCase().includes(q.trim().toLowerCase())
      );
      return json(route, {
        places,
        busy: false,
        throttled: false,
        attribution: places.length ? '© OpenStreetMap contributors (Nominatim)' : null,
      });
    }

    // The layers a proof's point can join: the fixture case has none. A spec
    // that needs some routes its own over this one.
    if (/^\/api\/cases\/[^/]+\/layer-targets$/.test(path)) return json(route, []);

    unexpected.push(`${request.method()} ${path}`);
    return json(route, { detail: `Unhandled browser fixture request: ${path}` }, 404);
  });

  return {
    captures,
    workWrites,
    kinReads,
    proposalRuns,
    placeWrites,
    railTiles,
    referenceTiles,
    placeNameTiles,
    widgetLoads,
    gridWrites,
    skyQueries,
    proofSaves,
    linkWrites,
    entityWrites,
    galleryWrites,
    catalogQueries,
    graphQueries,
    graphPinWrites,
    analysisWrites,
    timelineWrites,
    uploads,
    pastes,
    revealed,
    mediaDownloadWrites,
    sheetWrites,
    /** Play the spreadsheet that edited the CSV underneath the grid. */
    moveSheetOnDisk: (id, change = {}) => {
      const held = fixtureSheets.get(id);
      Object.assign(held, change);
      held.stamp = nextStamp();
      return held.stamp;
    },
    sheetOnDisk: (id) => fixtureSheets.get(id),
    trashWrites,
    bundleCalls,
    geoQueries,
    settingsWrites,
    folderWrites,
    exportWrites,
    expectNoUnexpectedRequests: () => expect(unexpected).toEqual([]),
  };
}

/**
 * Put the Saved panel, and the layer drawing from it, on one position.
 *
 * The map panel opens on its Layers tab and Saved opens on **Media**, so a spec
 * about the case's own work asks for both rather than assuming either.
 */
export async function showSaved(page, kind) {
  await page.getByRole('tablist', { name: 'Map panel' }).getByRole('tab', { name: 'Saved' }).click();
  await page
    .getByRole('group', { name: 'What this panel lists' })
    .getByRole('button', { name: kind, exact: true })
    .click();
}

/**
 * Wait for the map to be up.
 *
 * The signal is `lib/map`'s own ready flag, not a class the engine happens to
 * put on its container: a suite that recognises the engine is a suite pinned to
 * it, and swapping the engine (SPEC v3) would then rewrite every map spec.
 *
 * `maps` is how many the screen puts up — two, on Compare's linked pair.
 */
export async function awaitMapReady(page, maps = 1) {
  needsWebGL();
  const ready = page.locator('.map[data-map-ready="true"]');
  await expect(ready).toHaveCount(maps);
  await expect(ready.first()).toBeVisible();
}

/**
 * Skip a spec that needs a map where the browser cannot draw one.
 *
 * The runner's Firefox has no WebGL and cannot be given any: it asks for a
 * native driver, a box with no GPU has none, and the software path a patched
 * build would take is not there — `tryNativeGL()`, then `EXHAUSTED_DRIVERS`,
 * read off its own console with the blocklist off, `webgl.allow-software` set
 * and both halves of mesa installed. So the map is answered by Chromium there.
 *
 * Only there. A developer's Firefox draws the map like any other browser, and
 * the specs run on both engines locally, which is where an engine-specific
 * break would show first. The skip is named in the report rather than hidden,
 * and `awaitMapReady` asks it, so every spec that waits for a map comes through
 * it; a plate's maps are not `.map`, so the plate specs that draw on one ask it
 * themselves.
 */
export function needsWebGL() {
  test.skip(
    !!process.env.CI && test.info().project.name === 'firefox',
    'the runner\'s Firefox has no WebGL; Chromium answers for the map there'
  );
}

/**
 * Two frames gone by: a canvas library lays the map of what a click hits with the frame it
 * draws, so a press sent the moment after a change can meet the map from before it.
 */
export async function canvasSettled(page) {
  await page.evaluate(() => new Promise((resolve) => requestAnimationFrame(() => requestAnimationFrame(resolve))));
}

/**
 * What the map is painting, as bytes.
 *
 * A drawn shape has no element of its own — the engine paints every layer of
 * them into one canvas — so what a spec can hold onto is the picture. Two of
 * these differ when something was drawn, cleared or restyled, and comparing
 * them is how a spec says "that appeared" without naming the engine.
 */
export async function mapPicture(page) {
  await expect(page.locator('.map[data-map-ready="true"]')).toBeVisible();
  return page.locator('.map').screenshot();
}

/** Did the map's picture change? */
export function repainted(before, after) {
  return !before.equals(after);
}

export async function openProofWithPanel(page) {
  await page.goto('/#proof');
  await expect(page.locator('.tool-header').getByRole('button', { name: 'New proof', exact: true })).toBeVisible();
  await page.getByRole('button', { name: 'New proof' }).first().click();
  await expect(page.getByRole('heading', { name: 'Create proof' })).toBeVisible();
  await page.locator('.selectable-pick').click();
  await page.getByRole('button', { name: 'Create proof' }).click();
  await expect(page.locator('.konva canvas')).toHaveCount(2);
  await expect(page.locator('.panel-row')).toHaveCount(1);
  await page.evaluate(() => new Promise((resolve) => {
    requestAnimationFrame(() => requestAnimationFrame(resolve));
  }));
}

/**
 * Where a map is and how close, read as a user would: the point menu's DD row
 * at the map's centre, then 200 px right of it. The first is the place, the gap
 * between the two is the zoom. Read until two readings agree, so a glide or a
 * zoom still easing in is not taken for where the map rests. Tab closes the
 * menu, since Esc would also leave a fullscreen tool.
 */
export async function restingCamera(page, map) {
  const read = async () => {
    const box = await map.boundingBox();
    const at = async (dx) => {
      await page.mouse.click(box.x + box.width / 2 + dx, box.y + box.height / 2, { button: 'right' });
      const menu = page.getByRole('menu', { name: 'This point' });
      const text = await menu.locator('.item.copy')
        .filter({ has: page.locator('.fmt', { hasText: /^DD$/ }) }).locator('.coords').textContent();
      await page.keyboard.press('Tab');
      await expect(menu).toBeHidden();
      return text.split(',').map(Number);
    };
    const [lat, lon] = await at(0);
    const [, east] = await at(200);
    return { lat, lon, span: east - lon };
  };
  let last = null;
  await expect.poll(async () => {
    const now = await read();
    const still = !!last && now.lat === last.lat && now.lon === last.lon && now.span === last.span;
    last = now;
    return still;
  }).toBe(true);
  return last;
}