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
67.0 KB
Raw Blame
import { describe, expect, it, vi } from 'vitest';
import { readFileSync } from 'node:fs';
import { render } from 'svelte/server';

vi.mock('konva', () => ({ default: {} }));

import ProofComposer from './ProofComposer.svelte';
import {
  bindPanelPointerLifecycle,
  createCanvasRenderGate,
  discardDraft,
} from './proof/canvasLifecycle.js';

const source = readFileSync(new URL('./ProofComposer.svelte', import.meta.url), 'utf8');

/** The body of a top-level function in the component, for the assertions that are
 *  about one of them rather than about the file. */
const bodyOfFn = (name) => {
  const at = source.indexOf(`function ${name}(`);
  return source.slice(at, source.indexOf('\n  }', at));
};

describe('Proof Composer empty state', () => {
  it('hides proof-specific chrome until a proof is started', () => {
    const { body } = render(ProofComposer);

    expect(body).toContain('Compose a proof');
    expect(body).not.toContain('Freehand (d)');
    expect(body).not.toContain('title-input');
    expect(body).not.toContain('<aside');
    expect(body).not.toContain('House style');
    expect(body).not.toContain('Coordinates');
    expect(body).not.toContain('Annotations');
  });
});

describe('Proof Composer header', () => {
  it('uses the same compact controls as Inspect', () => {
    // Up to the body: the line a save writes under the header sits between the two.
    const header = source.slice(source.indexOf('<div class="tool-header">'), source.indexOf('\n  <div class="body">'));

    expect(header).not.toContain('class="btn"');
    expect(header).not.toContain('btn-primary');
    expect(header).toContain('btn-ok');
    expect(header).not.toContain('size={15}');
    expect(header).toContain('Open proof');
    expect(header).toContain("saving ? 'Saving…' : 'Save proof'");
    expect(header).toContain('<Icon name="save" size={14} /> {saving');
    expect(header).not.toContain('<Icon name="check" size={14} /> {saving');
    expect(header).toContain('btn-info');
    expect(source).toContain('align-items: baseline;');
    expect(source).toContain('font-weight: 700;');
  });

  it('exports the current proof and exposes its remembered destination', () => {
    // Up to the body: the line a save writes under the header sits between the two.
    const header = source.slice(source.indexOf('<div class="tool-header">'), source.indexOf('\n  <div class="body">'));

    expect(header).toContain("exporting ? 'Exporting…' : 'Export PNG'");
    expect(header).toContain('onclick={exportProofPng}');
    expect(header).toContain('onclick={toggleExportMenu}');
    expect(source).toContain('exportDir = (await readDestinations()).proofs;');
    expect(source).toContain('kind="proofs"');
    expect(source).toContain('confirmLabel="Export here"');
    expect(source).toContain('onchosen={useExportFolder}');
    expect(source).toContain('api.post(`/api/cases/${cid}/proofs/${encodeURIComponent(savedName)}/export`)');
    expect(source).toContain('/proofs/export/reveal`');
    // a save that stops to check a point resumes the export once it lands
    expect(source).toContain('if (dirty || !savedName) await save({ then: exportProof });');
    expect(source).toContain('if (dirty || !savedName) return;');
  });

  it('uses Copy as the main action and keeps export choices in the compact menu', () => {
    const split = source.slice(source.indexOf('<div class="export-split">'), source.indexOf('</div>\n    <button class="btn btn-ok'));
    const menu = source.slice(source.indexOf('<div class="export-menu card"'), source.indexOf('<div class="export-destination"'));

    expect(split).toContain('onclick={copyPng}');
    expect(split).toContain("copying ? 'Copying…' : 'Copy'");
    expect(menu.indexOf("onclick={exportProofPng}")).toBeLessThan(menu.indexOf('onclick={openExportPicker}'));
    expect(menu.indexOf('onclick={openExportPicker}')).toBeLessThan(menu.indexOf('onclick={revealProofExports}'));
    expect(menu).toContain('<span>{exporting ? \'Exporting…\' : \'Export PNG\'}</span>');
    expect(menu).toContain('<span>Export to another folder…</span>');
    expect(menu).toContain('<span>Show export folder</span>');
    expect(source).toContain('await exportProof();');
    expect(source).toContain('.export-main {');
    expect(source).toContain('.export-toggle {');
  });
});

describe('Proof Composer naming', () => {
  it('adopts the canonical filename stem returned by the backend', () => {
    expect(source).toContain('proof.title = result.title;');
  });

  it('checks the current hidden spec path before claiming a saved proof was deleted', () => {
    // Via the shared helper, never a literal: the path moved once already, and a
    // stale one matches nothing, so every save warns the proof was deleted.
    expect(source).toContain("lookupEntity(id, specAttr('proof'), specPath('proof', name))");
    expect(source).not.toContain('`proofs/${name}.json`');
  });
});

describe('Proof Composer pickers', () => {
  it('searches the panel picker and the saved-proof list only past six entries', () => {
    expect(source).toContain("import SearchInput from '../components/SearchInput.svelte'");
    expect(source).toContain('const PICKER_SEARCH_MIN = 6;');
    expect(source).toContain('{#if panelBrowserOpen || pickerItems.length > PICKER_SEARCH_MIN}');
    expect(source).toContain('{#if proofBrowserOpen || openList.length > PICKER_SEARCH_MIN}');
    expect(source).toContain('bind:value={panelQuery}');
    expect(source).toContain('bind:value={proofQuery}');
    expect(source).toContain('{#each visiblePanelItems as item (item.src)}');
    expect(source).toContain('{#each visibleProofs as entry (entry.name)}');
  });

  it('offers the Inspect folder browser behind the "…" in both pickers', () => {
    expect(source).toContain("import FolderBrowser from '../components/FolderBrowser.svelte'");
    expect(source).toContain('function togglePanelBrowser()');
    expect(source).toContain('function toggleProofBrowser()');
    expect(source).toContain('onconfirm={(item) => selectPanelBrowser(item, true)}');
    expect(source).toContain('onconfirm={(entry) => selectProofBrowser(entry, true)}');
    expect(source).toContain('disabled={!panelBrowseSelection} onclick={confirmPanelBrowser}');
    expect(source).toContain('disabled={!proofBrowseSelection} onclick={confirmProofBrowser}');
    expect(source).not.toContain('>Back</button>');
  });

  it('filters panels by satellite / other images, like the Create proof dialog', () => {
    expect(source).toContain("import PanelCategories from './proof/PanelCategories.svelte'");
    expect(source).toContain('<PanelCategories items={pickerItems} category={panelCategory} onpick={setPanelCategory} />');
    expect(source).toContain("filterProofPanelItems(pickerItems, '', panelCategory)");
  });

  it('calls a capture satellite exactly when the Media Library does', () => {
    expect(source).toContain("import { isSatelliteMedia, sortItems } from '../lib/mediaFilter.js'");
    expect(source).toContain("kind: isSatelliteMedia(s) ? 'satellite' : 'media'");
    // captures are dropped from the media half by path, so a Street View grab
    // is listed once, under Other images, instead of twice
    expect(source).toContain('const captured = new Set(sats.map((s) => s.path));');
    expect(source).toContain('!captured.has(m.path)');
    expect(source).not.toContain('isSatelliteCapture');
  });

  it('lists both halves newest first, rather than every capture above every photo', () => {
    // Each list arrives newest first and concatenating them threw that away. The
    // picker is opened for the frame cut a minute ago, whichever half it came from.
    const fetch = bodyOfFn('fetchPanelItems');
    expect(fetch).toContain("added_at: s.added_at ?? s.fetched_at ?? ''");
    expect(fetch).toContain("added_at: m.added_at ?? ''");
    expect(fetch).toContain("'newest'");
    expect(fetch).toContain('return sortItems(');
  });

  it('searches panels by their title, not by the file name', () => {
    expect(source).toContain('label: m.title || m.filename');
    expect(source).toContain('placeholder="Search titles…"');
  });

  it('files panels and saved proofs by their case folder', () => {
    expect(source).toContain("folder: m.folder ?? ''");
    expect(source).toContain("folder: s.folder ?? ''");
    expect(source).toContain("fetchAllEntities(caseState.current.id, { types: ['proof'] })");
    expect(source).toContain('folders.get(entry.spec_path)');
  });
});

describe('Proof Composer empty canvas', () => {
  it('keeps the drawing rail out until a proof is open', () => {
    const rail = source.slice(source.indexOf('<div class="body">'), source.indexOf('<ProofCanvas'));

    expect(rail).toContain('{#if proofStarted}');
    expect(rail).toContain('<ProofToolbar');
  });
});

describe('Proof Composer drawing tools own the canvas', () => {
  it('mutes the document and its handles while a drawing tool is active', () => {
    const rule = source.slice(
      source.indexOf('function syncCanvasListening'),
      source.indexOf('function docPoint'),
    );

    expect(rule).toContain("const live = tool === 'select' && !spacePan;");
    expect(rule).toContain('docLayer?.listening(live);');
    expect(rule).toContain('uiLayer?.listening(live);');
    // Hold-space panning went through its own switch once. Two rules meant the
    // second could hand the canvas back while a drawing tool still held it.
    expect(source).not.toContain('docLayer.listening(false)');
    expect(source).not.toContain('docLayer?.listening(true)');
  });

  it('leaves the colour, size and keyboard acts to the tool that can show them', () => {
    expect(source).toContain("const editableShapes = $derived(tool === 'select' ? selectedShapes : []);");

    const controls = source.slice(
      source.indexOf('function setColor'),
      source.indexOf('// Arrow-key nudge'),
    );
    expect(controls).not.toContain('selectedShape');
    expect(source).toContain("(e.key === 'Delete' || e.key === 'Backspace') && tool === 'select'");
  });
});

describe('Proof Composer panel pointer lifecycle', () => {
  function fakeGroup() {
    const handlers = new Map();
    const group = { on: vi.fn((events, handler) => handlers.set(events, handler)) };
    return { handlers, group };
  }

  const panelHit = () => ({ target: { name: () => 'panel-hit' } });

  it('does not select and rebuild the panel during pointerdown', () => {
    const { group, handlers } = fakeGroup();
    const onPress = vi.fn();
    const onSelect = vi.fn();

    bindPanelPointerLifecycle(group, { onPress, onSelect, onDragEnd: null });
    handlers.get('pointerdown')(panelHit());

    expect(onPress).toHaveBeenCalledOnce();
    expect(onSelect).not.toHaveBeenCalled();

    handlers.get('click tap')(panelHit());
    expect(onSelect).toHaveBeenCalledOnce();
  });

  it('selects a dragged panel only after the drag has ended', () => {
    const { group, handlers } = fakeGroup();
    const calls = [];

    bindPanelPointerLifecycle(group, {
      onPress: vi.fn(),
      onSelect: () => calls.push('select'),
      onDragEnd: () => calls.push('commit'),
    });
    handlers.get('dragend')({ target: group });

    expect(calls).toEqual(['select', 'commit']);
  });

  it('ignores click and drag events bubbled from an annotation', () => {
    const { group, handlers } = fakeGroup();
    const onPress = vi.fn();
    const onSelect = vi.fn();
    const onDragEnd = vi.fn();
    const annotation = { name: () => '' };

    bindPanelPointerLifecycle(group, { onPress, onSelect, onDragEnd });
    handlers.get('pointerdown')({ target: annotation });
    handlers.get('click tap')({ target: annotation });
    handlers.get('dragend')({ target: annotation });

    expect(onPress).not.toHaveBeenCalled();
    expect(onSelect).not.toHaveBeenCalled();
    expect(onDragEnd).not.toHaveBeenCalled();
  });
});

describe('Proof Composer canvas render gate', () => {
  function setup() {
    const callbacks = new Map();
    let nextId = 1;
    const schedule = vi.fn((callback) => {
      const id = nextId++;
      callbacks.set(id, callback);
      return id;
    });
    const cancel = vi.fn((id) => callbacks.delete(id));
    const rebuild = vi.fn();
    const refreshUi = vi.fn();
    const gate = createCanvasRenderGate(schedule, cancel, { rebuild, refreshUi });
    const runFrame = () => {
      const [id, callback] = callbacks.entries().next().value ?? [];
      if (!callback) return;
      callbacks.delete(id);
      callback();
    };
    return { gate, schedule, cancel, rebuild, refreshUi, callbacks, runFrame };
  }

  it('defers a reactive rebuild until the pointer gesture has ended', () => {
    const { gate, schedule, rebuild, runFrame } = setup();

    gate.beginPointer();
    gate.requestRebuild();

    expect(schedule).not.toHaveBeenCalled();
    expect(rebuild).not.toHaveBeenCalled();

    gate.endPointer();
    expect(schedule).toHaveBeenCalledOnce();
    runFrame();
    expect(rebuild).toHaveBeenCalledOnce();
  });

  it('coalesces repeated state changes into one rebuild', () => {
    const { gate, schedule, rebuild, runFrame } = setup();

    gate.requestRebuild();
    gate.requestRebuild();
    gate.requestRebuild();

    expect(schedule).toHaveBeenCalledOnce();
    runFrame();
    expect(rebuild).toHaveBeenCalledOnce();
  });

  it('does not rebuild when a new pointer starts before the frame runs', () => {
    const { gate, rebuild, runFrame } = setup();

    gate.requestRebuild();
    gate.beginPointer();
    runFrame();
    expect(rebuild).not.toHaveBeenCalled();

    gate.endPointer();
    runFrame();
    expect(rebuild).toHaveBeenCalledOnce();
  });

  it('refreshes selection UI without rebuilding the document', () => {
    const { gate, rebuild, refreshUi, runFrame } = setup();

    gate.requestUi();
    runFrame();

    expect(refreshUi).toHaveBeenCalledOnce();
    expect(rebuild).not.toHaveBeenCalled();
  });

  it('lets a document rebuild absorb a queued selection refresh', () => {
    const { gate, rebuild, refreshUi, runFrame } = setup();

    gate.requestUi();
    gate.requestRebuild();
    runFrame();

    expect(rebuild).toHaveBeenCalledOnce();
    expect(refreshUi).not.toHaveBeenCalled();
  });

  it('cancels a queued rebuild when the canvas is destroyed', () => {
    const { gate, cancel, rebuild, runFrame } = setup();

    gate.requestRebuild();
    gate.destroy();

    expect(cancel).toHaveBeenCalledOnce();
    runFrame();
    expect(rebuild).not.toHaveBeenCalled();
  });
});

describe('Proof Composer panel previews', () => {
  it('builds picker cells from the cached thumbnail, never the full-size image', () => {
    // a satellite capture is a multi-megabyte PNG: rendering it in a 150px cell
    // is what made "Add panel" and "New proof" crawl
    expect(source).toContain("import { pollWhile } from '../lib/poll.js'");
    expect(source).toContain('...panelPreview(s)');
    expect(source).toContain('...panelPreview(m)');
    expect(source).not.toContain('thumb: s.path');
    expect(source).not.toContain('thumb: m.thumbnail ?? m.path');
  });

  it('renders a placeholder instead of an <img> while a thumbnail is missing', () => {
    expect(source).toContain('{#if item.thumb}');
    expect(source).toContain('{:else if item.thumbPending}');
  });

  it('re-lists the picker while the worker is still generating thumbnails', () => {
    expect(source).toContain(
      "const pickerThumbsPending = $derived(pickerItems.some((i) => i.thumbPending));"
    );
    expect(source).toContain(
      'pollWhile(() => pickerThumbsPending, () => refreshPickerItems(), 1500)'
    );
  });
});

describe('Proof Composer saved-proof list', () => {
  it('never blocks the list on the full-resolution export', () => {
    // the saved PNG is the export itself; it is decoded lazily and off the
    // main thread so opening the list stays instant
    const row = source.slice(source.indexOf('<div class="open-list">'), source.indexOf('open-del'));
    expect(row).toContain('loading="lazy"');
    expect(row).toContain('decoding="async"');
  });

  it('shows the export thumbnail, falling back to the export itself', () => {
    // a proof saved before thumbnails existed has none until the listing
    // backfills it, and one that could not be rendered never gets one
    const row = source.slice(source.indexOf('<div class="open-list">'), source.indexOf('open-del'));
    expect(row).toContain('{#if entry.thumb || entry.png}');
    expect(row).toContain('fileUrl(caseState.current.id, entry.thumb ?? entry.png)');
  });
});

describe('Proof naming', () => {
  it('names a fresh proof "Proof N", numbered past the case', () => {
    expect(source).toContain("const freshTitle = () => nextName('proof', savedProofTitles());");
    expect(source).toContain("savedTitles(proofEntities, 'proof')");
    expect(source).not.toContain('Untitled proof');
  });

  it('names the proof in the header and nowhere else', () => {
    expect(source).toContain('<input\n        class="input title-input"\n        aria-label="Proof name"\n        bind:value={proof.title}');
    expect(source).not.toContain('newProofName');
  });

  it('renames in place: saving sends the bound slug so the backend moves the file', () => {
    expect(source).toContain('rename_from: savedName,');
    expect(source).toContain('title: proof.title,');
  });

  it('asks before an unbound proof takes a name another proof holds', () => {
    expect(source).toContain("if (!savedName && savedProofNames().has(slugify(proof.title, 'proof')))");
    expect(source).toContain('title="Overwrite this proof?"');
  });
});

describe('Proof Composer pasted images', () => {
  it('keeps a pasted image out of the case: no panel, no source, no media', () => {
    // it goes into its own array, so nothing that walks `panels` can mistake it
    // for evidence — the save files derived-from edges from panels alone
    expect(source).toContain("title: '', panels: [], pastes: []");
    expect(source).not.toContain('proof.panels.push(paste');
    expect(source).toContain('proof.pastes.unshift(paste)');
    // the picker and the ingest are never involved
    expect(source).toContain('accept="image/png,image/jpeg,image/webp"');
    expect(source).not.toContain('/api/cases/${c.id}/media');
  });

  it('needs a panel before an image has anywhere to land', () => {
    expect(source).toContain('if (!proof.panels.length) {');
    expect(source).toContain('Add a panel before adding an overlay');
  });

  it('lets one paste handler decide between an image and a copied annotation', () => {
    expect(source).toContain('onpaste={onPaste}');
    expect(source).toContain("i.type.startsWith('image/')");
    // the old Ctrl+V keydown branch would have swallowed the paste event
    expect(source).not.toContain("k === 'v' && clipboard");
    // and a paste into the title or a caption stays text
    expect(source).toContain('isTextTarget(e.target)');
  });

  it('decides that chord on which copy came last', () => {
    // Ctrl+C on a rectangle then Ctrl+V pasted whatever screenshot the system
    // clipboard was holding, because the system one was read first.
    expect(source).toContain('if (clipboard && (shapeCopyFresh || !item))');
    expect(source).toContain('shapeCopyFresh = true;');
  });

  it('hands the chord back to the system clipboard once the analyst leaves', () => {
    // going somewhere else is the only way an outside copy could have happened
    expect(source).toContain("window.addEventListener('blur', release)");
    expect(source).toContain('const release = () => (shapeCopyFresh = false)');
  });

  it('offers the file picker and the drop as well as Ctrl+V', () => {
    expect(source).toContain('{pickImageFile}');
    expect(source).toContain("containerEl.addEventListener('drop', onCanvasDrop)");
    // the drawing rail stays about drawing: adding an overlay lives in its own
    // side section, and pasting is the shortcut
    const rail = source.slice(source.indexOf('<ProofToolbar'), source.indexOf('<ProofCanvas'));
    expect(rail).not.toContain('pickImageFile');
    expect(source).not.toContain('navigator.clipboard.read');
  });

  it('sends a pasted image once, with the save that references it', () => {
    expect(source).toContain('.filter(({ entry }) => entry?.pending && entry.data)');
    expect(source).toContain('assets,');
    expect(source).toContain('if (entry) entry.pending = false;');
    // no upload endpoint of its own: an unsaved proof leaves nothing behind
    expect(source).not.toContain('/assets/upload');
  });

  it('reads pasted images back from the proof folder on open', () => {
    // Beside the spec, under `.meta/`, which is where the save put them. This
    // assertion held the folder one level up for as long as the code did, and an
    // overlay nothing had lost reopened as missing. What the two sides agree on is
    // checked against `layout.proof_assets_rel` itself, in the backend suite.
    expect(source).toContain('proofs/.meta/${entry.name}.assets/${p.asset}');
    expect(source).toContain("pasteAssets.set(p.asset, { img, data: null, pending: false })");
    // annotations bound to a paste survive the reload with it
    expect(source).toContain('const validSurfaces = new Set([...proof.panels, ...proof.pastes]');
  });

  it('never lets a pasted image change the size of the document', () => {
    // docSize is measured from panels and shapes only (the free marks among
    // them reach the page's edge), and a drag is clamped
    expect(source).toContain('proof.panels, proof.shapes, proof.notes, textOpts()');
    expect(source).toContain('freeExtent(proof.shapes, inkHelpers)');
    expect(source).toContain('Object.assign(paste, clampPaste(paste, width, height))');
  });

  it('draws pasted images over the panels and the legend', () => {
    const rebuild = source.slice(source.indexOf('function rebuild()'), source.indexOf("const st = proof.signatureText"));
    expect(rebuild.indexOf('for (let i = proof.panels.length - 1')).toBeLessThan(
      rebuild.indexOf('for (let i = proof.pastes.length - 1')
    );
    // and each one hosts its own annotations, like a panel does
    expect(rebuild).toContain('for (const s of proof.shapes.filter((x) => x.panel === paste.id && !isPageMark(x)))');
  });

  it('resizes a pasted image from its corners with the aspect locked', () => {
    // the list has grown a symbol since; what this test guards is that a paste
    // is still on it, not who else joined
    expect(source).toMatch(/transformer\.keepRatio\([^)]*!!pasteNode/);
    expect(source).toContain('implied < PASTE_SCALE_MIN || implied > PASTE_SCALE_MAX');
    expect(source).toContain('|| panelNode || pasteNode || (sigNode && !footerPicked)');
  });

  it('drops a pasted image with its annotations, and only one thing is selected', () => {
    expect(source).toContain('proof.shapes.filter((s) => s.panel !== paste.id)');
    expect(source).toContain('selectedPasteId = null');
    expect(source).toContain('removePaste(proof.pastes.findIndex((p) => p.id === selectedPasteId))');
  });
});

describe('Proof Composer frames', () => {
  const layers = readFileSync(new URL('./proof/ProofLayersPanel.svelte', import.meta.url), 'utf8');

  it('offers the same border control on a picked panel and a picked overlay, on the rail', () => {
    // one place for it: the toolbar, beside the canvas, not a button in a row
    expect(layers).not.toContain('frameControl');
    expect(source).toContain(': selectedPanelId ? proof.panels.find((p) => p.id === selectedPanelId)');
    expect(source).toContain(': selectedPasteId ? proof.pastes.find((p) => p.id === selectedPasteId) : null,');
    expect(source).toContain('setSurfaceBorder={(patch) => pickedSurface && setFrame(pickedSurface, patch)}');
  });

  it('is decoration: a frame never reaches the legend', () => {
    // the legend is built from shape colours; frames are not shapes
    expect(source).toContain('orderedFeatureColors(proof.shapes');
    expect(source).not.toContain('orderedFeatureColors([...proof.shapes');
    // …and it frames the image, not the upright box a turned image sits in
    expect(source).toContain('if (panel.frame) group.add(frameNode(panel.frame, image, box.scale))');
    expect(source).toContain('if (paste.frame) group.add(frameNode(paste.frame, image, box.scale))');
  });

  it('draws the border inset so it cannot move the layout', () => {
    const fn = source.slice(source.indexOf('function frameNode('), source.indexOf('// ---- rebuild canvas'));
    expect(fn).toContain('x: w / 2, y: w / 2');
    expect(fn).toContain('natural[0] - w');
    // in page pixels, like any other ink: the same border on every panel
    expect(fn).toContain('const w = frame.width / scale;');
  });
});

describe('Proof Composer panel and overlay transforms', () => {
  it('hydrates a spec surface into its crop, its turn and its upright box', () => {
    const body = bodyOfFn('hydratedSurface');
    expect(body).toContain('crop: normalizeSourceCrop(surface.crop, sourceNatural)');
    expect(body).toContain('rotation: normalizeSurfaceAngle(surface.rotation)');
    expect(bodyOfFn('refreshSurface'))
      .toContain('surfaceBoxSize(surface.sourceNatural, surface.crop, surface.rotation)');
    expect(source).toContain("proof.panels.push(hydratedSurface({");
    expect(source).toContain("proof.pastes.push(hydratedSurface({ ...p, id: p.id ?? newId('x') }, img))");
  });

  it('turns nothing but the group, so no pixel and no annotation is rewritten', () => {
    // The image, its border, its marks and its notes are one group that turns.
    // Baking the angle into pixels is what put the selection frame and the crop
    // marks on a transparent box bigger than the picture.
    const body = bodyOfFn('applySurfaceRotation');
    expect(body).toContain('item.rotation = turned');
    expect(body).not.toContain('mapSurfaceShapes');
    expect(source).toContain('rotation: panel.rotation ?? 0');
    expect(source).toContain('rotation: paste.rotation ?? 0');
    expect(source).not.toContain('renderSurfaceImage');
  });

  it('crops in source pixels and walks annotations by the corner they moved', () => {
    const body = bodyOfFn('applySurfaceCrop');
    expect(body).toContain('normalizeSourceCrop(selection, item.sourceNatural)');
    expect(body).toContain('shiftProofShape(s, dx, dy)');
    expect(body).toContain('item.crop = kept');
  });

  it('shows the whole source under the marks, so a crop can be taken back', () => {
    const body = bodyOfFn('surfaceImageNode');
    expect(body).toContain("name: 'crop-ghost'");
    expect(body).toContain('width: sw, height: sh'); // the source, not the crop
    expect(bodyOfFn('beginSurfaceCrop')).toContain('cropDraft = item.crop');
    // …and the marks clamp to the source, which is what lets them pull back out
    expect(bodyOfFn('updateCropFromHandle')).toContain('frame.item.sourceNatural');
  });

  it('enters crop mode on the canvas by double-click and draws eight marks', () => {
    expect(source).toContain('openCrop={beginSurfaceCrop}');
    expect(source.match(/beginSurfaceCrop\((panel|paste)\)/g)).toHaveLength(2);
    expect(source).toContain("cropHandles = new Konva.Group({ id: 'proof-crop-handles' })");
    expect(source).toContain("group.on('dblclick dbltap'");
    expect(source).toContain('for (const [key, mark] of Object.entries(points))');
    expect(source).not.toContain('ProofCropDialog');
  });

  it('holds the crop box as a draft until crop mode is left', () => {
    // Committing on each handle released would bury the pixels the next pull
    // needs, and would write one operation per nudge into the saved spec.
    expect(bodyOfFn('endSurfaceCrop')).toContain('applySurfaceCrop(item, selection)');
    expect(bodyOfFn('applySurfaceCrop')).not.toContain('cropDraft =');
    expect(bodyOfFn('discardSurfaceCrop')).not.toContain('applySurfaceCrop');
    // saving or copying the picture keeps a box still under its marks…
    expect(bodyOfFn('exportPng')).toContain('endSurfaceCrop();');
    // …and Escape drops it, like every other draft in this tool
    expect(source).toMatch(/Escape' && cropSurfaceId\) \{\s*\n\s*discardSurfaceCrop\(\);/);
    // a surface on its way out of the proof takes its draft with it
    expect(bodyOfFn('removePanel')).toContain('discardSurfaceCrop()');
    expect(bodyOfFn('removePaste')).toContain('discardSurfaceCrop()');
  });

  it('dresses the turn knob the way Collage dresses its own', () => {
    expect(source).toContain('rotateAnchorOffset: 28');
    expect(source).toContain('!!panelNode || !!pasteNode');
    // A disc on a stem, and nothing inside it: a glyph at anchor size is a
    // smudge rather than a symbol.
    const knob = bodyOfFn('drawRotateKnob');
    expect(knob).toContain('stroke: ACCENT');
    expect(knob).not.toContain('Konva.Path');
    expect(source).toContain("anchor.fill('rgba(22, 22, 22, 0.92)')");
    expect(source).toContain('anchor.stroke(ACCENT)');
    expect(source).not.toContain("anchor.fill('#1473e6')");
  });

  it('puts the selection frame away while the crop marks are up', () => {
    expect(source).toContain('!handles || cropSurfaceId');
  });
});

describe('POV', () => {
  it('asks what the point means, since the composition cannot say', () => {
    // recorded-at and shows are independent: a rooftop shot is recorded
    // somewhere it never shows
    expect(source).toContain('title="Point of view"');
    expect(source).toContain('onclick={() => togglePov(i)}');
  });

  it('lights one point at most, because a camera stood in one place', () => {
    expect(source).toContain('proof.points.map((one, at) => ({ ...one, pov: on && at === i }))');
  });
});

describe('the points a proof states', () => {
  it('keeps a single-point proof looking exactly as it did', () => {
    // its menu offers the layers only: there is nothing to remove and nothing to move
    expect(source).toContain('onraise={i > 0 ? () => raisePoint(i) : null}');
    expect(source).toContain('onremove={i > 0 ? () => removePoint(i) : null}');
    expect(source).toContain('placeholder="lat, lon"');
    expect(source).toContain('placeholder="label"');
  });

  it('materialises the auto point before a second one sits under it', () => {
    // an empty field means "whatever the imagery says", and that answer has to
    // become the conclusion in writing before the list can grow
    const state = bodyOfFn('statePoint0');
    expect(state).toContain('if (proof.points[0].coords.trim()) return;');
    expect(state).toContain('proof.points[0].coords = displayedCoords;');
    // …and still as unchecked as it was: naming a point is not checking where it is
    expect(state).toContain('if (pointProposed(proof.points, 0, proof.panels)) proof.points[0].proposed = true;');
    expect(bodyOfFn('addPoint')).toContain('statePoint0();');
  });

  it('writes the coordinates down with the name typed onto them', () => {
    // The save states only what the proof holds: a row with no coordinates is
    // dropped, and the name typed onto the imagery's answer went with it — no
    // point in the request, no place filed, and nothing said so.
    expect(source).toContain('oninput={(e) => namePoint(i, e.target.value)}');
    expect(bodyOfFn('namePoint')).toContain('if (i === 0) statePoint0();');
  });

  it('writes them down for POV too, which is the same claim about the same row', () => {
    expect(bodyOfFn('togglePov')).toContain('if (on) statePoint0();');
  });

  it('chooses the conclusion by order, never by POV', () => {
    expect(source).toContain('function raisePoint(i)');
    expect(source).toContain('onraise={i > 0 ? () => raisePoint(i) : null}');
  });

  it('saves the proof before a point joins a layer, since the marker follows the proof as saved', () => {
    expect(bodyOfFn('addPointToLayer')).toContain('if (dirty || !savedName) save({ then: go }); else go();');
  });

  it('caps the list where the engine caps it', () => {
    expect(source).toContain('if (proof.points.length >= MAX_POINTS) return;');
  });

  it('grows the list from the head, the way the sources do', () => {
    const head = source.slice(source.indexOf('<span>Coordinates</span>'), source.indexOf('placeholder="lat, lon"'));
    expect(head).toContain('{#if proof.points.length < MAX_POINTS}');
    expect(head).toContain('title="Add a point"');
    expect(head).toContain('onclick={addPoint}');
    expect(source).not.toContain('class="point-add"');
  });

  it('states a panel’s place under the answer already on screen', () => {
    // In free layout a new panel lands in *front*, so the auto answer used to
    // change under the analyst as soon as a second capture was dropped on the
    // canvas; in grid layout the second place was simply never heard.
    const body = bodyOfFn('addPanel');
    expect(body).toContain('const answered = displayedCoords;');
    expect(body).toContain('const answeredProposed = pointProposed(proof.points, 0, proof.panels);');
    expect(body.indexOf('const answered')).toBeLessThan(body.indexOf('proof.panels.unshift(panel)'));
    expect(body.indexOf('const answeredProposed')).toBeLessThan(body.indexOf('proof.panels.unshift(panel)'));
    expect(body).toContain(
      'proof.points = statePanelPoint(proof.points, panel, answered, prefs.coordFormat, answeredProposed);'
    );
  });

  it('opens a row on the ground it names, or on the point above it', () => {
    // six decimals is a tenth of a metre: the pin is the instrument, not the digits
    expect(source).toContain(": 'Move this point on the map'}");
    expect(source).toContain('onclick={() => (pointMap = { row: i, view: pointMapView(i) })}');
    const view = bodyOfFn('pointMapView');
    expect(view).toContain('proof.points.slice(0, i).map((one) => one.coords).reverse()');
    expect(view).toContain('autoCoords(proof.panels)');
    expect(view).toContain('{ ...prefs.homeView }');
    expect(bodyOfFn('movePoint')).toContain(
      'proof.points[i].coords = formatCoords(point, prefs.coordFormat);'
    );
  });

  it('greys a point the imagery proposed until it is typed or placed', () => {
    expect(source).toContain('class:proposed={pointProposed(proof.points, i, proof.panels)}');
    expect(source).toContain('class:check={pointProposed(proof.points, i, proof.panels)}');
    expect(source).toContain("'Not checked yet. Check it on the map'");
    const typed = source.slice(source.indexOf('placeholder="lat, lon"'), source.indexOf('class="field-act"'));
    expect(typed).toContain('proof.points[i].proposed = false;');
    expect(bodyOfFn('movePoint')).toContain('proof.points[i].proposed = false;');
  });

  it('shows every unchecked point on the map before a save or a post', () => {
    const body = bodyOfFn('save');
    expect(body).toContain('const rows = checked ? [] : uncheckedPoints(proof.points, proof.panels);');
    expect(body).toContain('checking = { rows, at: 0, andPost, then };');
    // the check comes before anything is written, the overwrite question included
    expect(body.indexOf('uncheckedPoints')).toBeLessThan(body.indexOf('overwritePrompt'));
    expect(body.indexOf('uncheckedPoints')).toBeLessThan(body.indexOf('performSave'));
    // one point at a time, then the save it was holding
    const pick = bodyOfFn('checkPick');
    expect(pick).toContain('movePoint(rows[at], point);');
    expect(pick).toContain('checking = { ...checking, at: at + 1 };');
    expect(bodyOfFn('finishCheck')).toContain('await save({ andPost, checked: true, then });');
    // a post publishes the point, so only a plain save can leave it for later
    expect(source).toContain('later: !checking.andPost,');
    expect(source).toContain('onlater={checkStep?.later ? () => finishCheck(true) : null}');
    expect(source).toContain('{#key checking?.at}');
    expect(source).toContain('|| checking !== null');
  });

  it('redraws the plate when a point or the footer switch changes', () => {
    // both feed the footer, and the footer decides the picture's height: a
    // rebuild that does not read them leaves the export a line behind
    const rebuild = source.slice(source.indexOf('Rebuild only when published document'));
    expect(rebuild.slice(0, rebuild.indexOf('requestRebuild')))
      .toContain('proof.footerCoords, proof.footerText, proof.points,');
  });

  it('measures the footer at the number of lines it will print', () => {
    expect(source).toContain('footerLines: footerLines(proof, prefs.coordFormat).length');
    expect(source).toContain(
      "const printed = proof.footerEnabled !== false ? footerLines(proof, prefs.coordFormat) : [];"
    );
    expect(source).toContain('if (proof.panels.length && printed.length) {');
  });
});

describe("the point a proof concludes on", () => {
  it('files what it can and asks about the rest, never for a point already saved', () => {
    // the server answers with nothing when the case holds those points already
    expect(source).toContain('if (result.place?.filed?.length)');
    expect(source).toContain('if (result.place?.asking?.length)');
  });

  it('asks with the coordinates, since the map is where the point will be read', () => {
    expect(source).toContain('Save this point as a place?');
    expect(source).toContain("'Save these points as places?'");
    expect(source).toContain('this proof and the files it composes will say they show them');
    expect(source).toContain('was recorded at the marked one');
  });

  it('answers through the proof, so the edge is filed with the point', () => {
    expect(source).toContain('/proofs/${encodeURIComponent(offer.name)}/place');
    expect(source).toContain('await reloadCase()');
  });

  it('asks before dropping the point a correction moved it off', () => {
    // the place stays on the map until the analyst says otherwise: a save
    // withdraws the claim, it does not clean up after them
    expect(source).toContain('orphanOffer = result.orphans?.length ? result.orphans : null');
    expect(source).toContain('Delete the old place?');
    expect(source).toContain('This proof no longer points at');
    expect(source).toContain('Nothing else in the case points there.');
  });

  it('queues that question behind the other one, since a save can raise both', () => {
    const dialogs = source.slice(source.indexOf('{#if placeOffer}'));
    expect(dialogs.indexOf('{:else if orphanOffer}')).toBeGreaterThan(-1);
    expect(dialogs.indexOf('{:else if orphanOffer}')).toBeLessThan(dialogs.indexOf('{#if deleteEntry}'));
  });

  it('drops it through the entity route, so it lands in the trash like any delete', () => {
    expect(source).toContain('await api.del(`/api/cases/${caseState.current.id}/entities/${place.id}`)');
    expect(source).toContain('restorable={RESTORABLE}');
  });
});

describe('Proof Composer — one document at a time', () => {
  const composer = readFileSync(new URL('./ProofComposer.svelte', import.meta.url), 'utf8');

  it('stops an earlier open from streaming panels into the proof that replaced it', () => {
    // The open list stays up while a proof loads, so clicking a second entry is
    // one gesture away. Without a guard, A's panels kept arriving into the
    // document now bound to B's name, and Save wrote the hybrid over B.
    expect(composer).toContain('let openRun = 0;');
    expect(composer).toContain('const run = ++openRun;');
    expect(composer.match(/if \(run !== openRun\) return;/g)?.length).toBeGreaterThanOrEqual(4);
  });

  it('cancels a pending history capture before an undo restores the document', () => {
    // The clear has to come before the histBusy guard, or a timer armed in the
    // 350 ms before the undo survives it and pushes the pre-undo state back.
    expect(composer).toMatch(
      /const json = docSnapshot\(\);[\s\S]*?clearTimeout\(histTimer\);\s*\n\s*if \(histBusy\) return;/
    );
  });
});


describe('a draft left in hand', () => {
  it('destroys the node and hands back nothing', () => {
    const destroy = vi.fn();
    expect(discardDraft({ node: { destroy } })).toBeNull();
    expect(destroy).toHaveBeenCalledOnce();
  });

  it('survives a draft that never got a node', () => {
    expect(discardDraft(null)).toBeNull();
    expect(discardDraft({})).toBeNull();
  });

  it('abandons the shape being drawn when Escape lands mid-drag', () => {
    // Escape used to switch the tool to Select and leave `drawing` armed, so
    // the release below took the Select branch and the draft never settled.
    const escape = source.slice(source.indexOf('function onKeydown'), source.indexOf('function onKeyup'));

    expect(escape).toContain("if (e.key === 'Escape' && drawing)");
    expect(escape).toContain('drawing = discardDraft(drawing)');
  });

  it('never lets Select swallow the release that settles a draft', () => {
    const up = source.slice(source.indexOf('function onPointerUp'), source.indexOf('function finishPath'));
    const select = up.indexOf("if (tool === 'select')");

    expect(select).toBeGreaterThan(-1);
    // the Select branch holds no nested block, so the next brace closes it:
    // whatever return it makes has to be guarded on there being no draft
    const branch = up.slice(select, up.indexOf('}', select));
    expect(branch).toContain('if (!drawing) return;');
    expect(branch).not.toMatch(/\n\s*return;/);
  });
});

describe('stamping a symbol', () => {
  const down = source.slice(source.indexOf('function onPointerDown'), source.indexOf('function onPointerMove'));

  it('places one on a single click and keeps the tool in hand', () => {
    // text switches to Select after placing because the next act is editing it;
    // marking six vehicles is one act, so the stamp stays armed
    expect(down).toContain("if (tool === 'icon')");
    expect(down).toContain("kind: 'icon', name: entry.name");
    const stamp = down.slice(down.indexOf("if (tool === 'icon')"), down.indexOf("// curve:"));
    expect(stamp).toContain('selectedIds = [s.id]');
    expect(stamp).not.toContain("tool = 'select'");
  });

  it('sizes it on the page, so it reads the same on any resolution', () => {
    expect(down).toContain('size: ICON_SIZE_DEFAULT,');
  });

  it('gives a solid silhouette no stroke width to carry', () => {
    expect(down).toContain('...(isSolidIcon(entry.name) ? {} : { strokeWidth: strokeW })');
    const setStroke = source.slice(source.indexOf('function setStroke'), source.indexOf('/** Fill opacity'));
    expect(setStroke).toContain("if (s.kind === 'icon' && isSolidIcon(s.name)) continue;");
  });

  it('scales but never deforms: corners only, ratio locked', () => {
    const tr = source.slice(source.indexOf('transformer.rotateEnabled('), source.indexOf('if (handles) {'));
    const anchors = tr.slice(tr.indexOf('enabledAnchors'));

    expect(anchors).toContain("selKind === 'text' || selKind === 'icon'");
    // the middle handles are the ones that would let it be squashed
    expect(anchors).not.toMatch(/selKind === 'icon'[^?]*'middle-left'/);
    expect(source).toMatch(/transformer\.keepRatio\([^)]*selKind === 'icon'/);
    // and it reads selKind only after selKind exists: a const referenced above
    // its declaration throws, which takes the whole effect down with it —
    // panel handles and move arrows included
    expect(source.indexOf('const selKind = selectedShape?.kind;'))
      .toBeLessThan(source.indexOf('transformer.keepRatio('));
  });

  it('hangs the shape on the point it names rather than a corner', () => {
    const node = source.slice(source.indexOf('function makeIconNode'), source.indexOf('// ---- inline text editing'));

    expect(node).toContain('const origin = iconOrigin(s.name, size)');
    expect(node).toContain('id: s.id, x: s.x, y: s.y');
    // and it rebinds by that same point when dropped onto another surface:
    // everything that is not a box and not a polyline answers for its origin
    expect(bodyOfFn('anchorOf')).toContain('if (!Array.isArray(s.points)) return { x: s.x, y: s.y };');
  });

  it('turns the fill into a badge disc behind the glyph', () => {
    const node = source.slice(source.indexOf('function makeIconNode'), source.indexOf('// ---- inline text editing'));

    expect(node).toContain('const disc = s.fillOpacity ?? 0');
    expect(node).toContain('fill: fillPaint(s.color, disc)');
    expect(node).toContain('glyphInk(s.color, disc)');
    // a stroked glyph is mostly holes, so the press lands on the box
    expect(node).toContain("fill: '#000', opacity: 0");
  });

  it('divides the scale back out of the stroke, so a big symbol is not a fat one', () => {
    const node = source.slice(source.indexOf('function makeIconNode'), source.indexOf('// ---- inline text editing'));

    expect(node).toContain('strokeWidth: (s.strokeWidth ?? 4) / scale / k');
  });

  it('reaches the stamp with a key nothing else claims', () => {
    expect(source).toContain("else if (e.key === 's') tool = 'icon';");
  });
});

// ---- gestures the composer runs itself --------------------------------------
// The three below — drawing, marquee, pan — are handled in this file rather than
// by Konva, so the stranded-gesture net that covers the transformer and node
// drags (konvaGesture.js) never reached them.

const canvasSource = readFileSync(new URL('./proof/ProofCanvas.svelte', import.meta.url), 'utf8');
const down = source.slice(source.indexOf('function onPointerDown'), source.indexOf('function onPointerMove'));
const keys = source.slice(source.indexOf('function onKeydown'), source.indexOf('function onKeyup'));

describe('a release the canvas never hears', () => {
  const mount = source.slice(
    source.indexOf('onMount(() => {'),
    source.indexOf('// reset the document when the case changes'),
  );
  const settle = source.slice(
    source.indexOf('function settleStrandedGesture'),
    source.indexOf('function finishPath'),
  );
  const marquee = source.slice(
    source.indexOf('function applyMarquee'),
    source.indexOf('/** Fold the draft in hand'),
  );

  it('settles the gestures this file runs itself', () => {
    // Letting go over the side column — a hand's width from the canvas edge —
    // left a draft stretching under a bare pointer and a marquee standing with
    // no selection to show for it.
    expect(settle).toContain('if (marquee) applyMarquee();');
    expect(settle).toContain('if (drawing) commitDrawing();');
  });

  it('ends the pan on the spot, since it follows the live pointer', () => {
    // A frame's wait is invisible for a draft — it only moves on a pointermove
    // over the canvas — but a pan reads every move, so one arriving before the
    // frame slid the view after the button was let go.
    expect(mount).toContain('if (panDrag) endPan();');
    expect(mount.indexOf('if (panDrag) endPan();')).toBeLessThan(mount.indexOf('const seq = gestureSeq;'));
    expect(settle).not.toContain('endPan');
  });

  it('hangs on the same window release that closes the gestures Konva owns', () => {
    expect(mount).toContain('settleStrandedGesture();');
    expect(mount).toContain("window.addEventListener('pointerup', settlePointer, true)");
    expect(mount).toContain("window.addEventListener('pointercancel', settlePointer, true)");
    expect(mount).toContain("window.addEventListener('blur', settlePointer)");
  });

  it('never settles a gesture a fresh press has already replaced', () => {
    // The settle waits a frame so a normal release can close itself first. A
    // press landing inside that frame opens a new draft, and committing that
    // one would end a stroke still being drawn.
    expect(mount).toContain('gestureSeq += 1;');
    expect(mount).toContain('const seq = gestureSeq;');
    expect(mount).toContain('if (seq === gestureSeq) settleStrandedGesture();');
  });

  it('reads the marquee off its node rather than a pointer that has left', () => {
    expect(marquee).toContain('width: node.width(), height: node.height()');
    expect(marquee).not.toContain('docPoint()');
    expect(marquee).toContain('node?.destroy();');
    expect(marquee).toContain('marquee = null;');
  });

  it('commits the draft through the one path a normal release uses', () => {
    // Two commit paths would be two answers to "what did that stroke become".
    const up = source.slice(source.indexOf('function onPointerUp'), source.indexOf('function settleStrandedGesture'));
    expect(up).toContain('commitDrawing();');
    expect(up).toContain('applyMarquee();');
    expect(up).toContain('endPan();');
  });
});

describe('the button that draws', () => {
  it('is the primary one; the middle one pans', () => {
    expect(down).toContain('if (spacePan || e.evt.button === 1)');
    expect(down).toContain('if ((e.evt.button ?? 0) > 0) return;');
  });

  it('turns the right button away before either tool can answer it', () => {
    // A right-drag drew a whole annotation under the context menu opening over
    // it; in Select it cleared the selection and opened a marquee.
    expect(down.indexOf('(e.evt.button ?? 0) > 0'))
      .toBeLessThan(down.indexOf("if (tool === 'select')"));
  });
});

describe('Escape', () => {
  it('unwinds one level per press instead of meaning two opposite things', () => {
    expect(keys).toContain("if (e.key === 'Escape' && drawing)");
    expect(keys).toMatch(/if \(visiblePick\) \{[\s\S]*?\} else \{\s*tool = 'select';/);
  });

  it('never clears a pick and puts the pen down in the same press', () => {
    const rung = keys.slice(keys.indexOf('if (visiblePick) {'), keys.indexOf("} else if (e.key === 'v')"));
    const cleared = rung.slice(0, rung.indexOf('} else {'));

    expect(cleared).toContain('selectedIds = [];');
    expect(cleared).toContain('selectedPanelId = null;');
    expect(cleared).not.toContain("tool = 'select'");
  });

  it('counts only a pick that shows, so one held for later is not a rung', () => {
    const derived = source.slice(
      source.indexOf('const visiblePick = $derived('),
      source.indexOf('// Every dialog that can sit over'),
    );

    expect(derived).toContain('selectionLive');
    expect(derived).toContain('selectedIds.length > 0');
    expect(derived).toContain('!!selectedSig');
  });
});

describe('a dialog over the composer', () => {
  it('keeps the one-letter tool keys off the canvas behind it', () => {
    expect(keys).toContain('if (modalOpen) return;');
    expect(keys.indexOf('if (modalOpen) return;')).toBeLessThan(keys.indexOf("e.key === 'r'"));
    // and Escape belongs to the dialog closing, not to the tool underneath
    expect(keys.indexOf('if (modalOpen) return;')).toBeLessThan(keys.indexOf('if (visiblePick) {'));
  });

  it('still lets the export menu answer its own Escape first', () => {
    expect(keys.indexOf('exportMenuOpen = false;')).toBeLessThan(keys.indexOf('if (modalOpen) return;'));
  });

  it('names every dialog that can be up', () => {
    const derived = source.slice(
      source.indexOf('// Every dialog that can sit over'),
      source.indexOf('const editableShape ='),
    );

    for (const flag of [
      'picker', 'newProofOpen', 'importOpen', 'openList', 'discardConfirm',
      'replaceWithNewConfirm', 'exportPicker', 'overwritePrompt', 'placeOffer',
      'orphanOffer', 'deleteEntry', 'sourcePick',
    ]) {
      expect(derived).toContain(flag);
    }
  });
});

describe('picking from the side column', () => {
  const bodyOf = (name) => {
    const at = source.indexOf(`function ${name}`);
    return source.slice(at, source.indexOf('\n  }', at));
  };

  it('takes the hand back to Select, since a row cannot mean "draw here"', () => {
    // The canvas refuses instead: there a press means draw. A row that lit up
    // orange while Delete, the handles and the colour all ignored it was a
    // selection visible in one column and absent from the other.
    for (const name of ['pickShapeRow', 'pickPanelRow', 'pickPasteRow']) {
      expect(bodyOf(name)).toContain("tool = 'select';");
    }
  });

  it('wires the rows to those rather than to the raw pick the canvas guards', () => {
    expect(source).toContain('selectShape={pickShapeRow}');
    expect(source).toContain('selectPanelRow={pickPanelRow}');
    expect(source).toContain('selectPasteRow={pickPasteRow}');
    expect(source).not.toContain('bind:selectedPanelId');
    expect(source).not.toContain('bind:selectedPasteId');
  });

  it('lights a row only while the pick can be acted on', () => {
    expect(source).toContain("const selectionLive = $derived(tool === 'select');");
    expect(source).toContain('{selectionLive}');
  });

  it('clears a marquee that would otherwise swallow the row click', () => {
    expect(bodyOf('pickPanelRow')).toContain('marqueeEnded = false;');
  });
});

describe('the curve tool', () => {
  const finish = source.slice(
    source.indexOf('function finishPath'),
    source.indexOf('function commitPasteNode'),
  );

  it('puts the pen down like the box, the line and the arrow beside it', () => {
    expect(finish).toContain("kind: 'curve'");
    expect(finish).toContain('selectedIds = [s.id];');
    expect(finish).toContain("if (!keepTool) tool = 'select';");
  });

  it('ends where it was and opens the next one on a vertex dropped elsewhere', () => {
    // The click used to be dropped in silence, which reads as a dead canvas.
    // The tool has to survive that close, or the curve it opens is stranded.
    expect(down).toContain(
      'if (pathDraft && pathDraft.panel.id !== panel.id) finishPath(true, { keepTool: true });'
    );
  });
});

describe('finishing a shape', () => {
  const commit = source.slice(
    source.indexOf('function commitDrawing'),
    source.indexOf('function onPointerUp'),
  );

  it('picks it and puts the pen down, so the toolbar edits what was just drawn', () => {
    // What follows a stroke is almost always a word about it — its colour, its
    // width, the note it carries — and each of those used to draw a second box.
    expect(commit).toContain('selectedIds = [s.id];');
    expect(commit).toContain("tool = 'select';");
  });
});

describe('placing a label', () => {
  it('opens the editor on the click that places it', () => {
    const text = down.slice(down.indexOf("if (tool === 'text')"), down.indexOf("if (tool === 'icon')"));

    // A frame later, or the press that placed it takes the focus straight back
    // out of the editor: the browser moves focus off whatever holds it when a
    // click lands on something that cannot, so an editor opened inside the
    // pointerdown was blurred, committed and closed before a key could reach it.
    expect(text).toContain('requestAnimationFrame(() => startTextEdit(s));');
    expect(text).toContain("tool = 'select';"); // the next act is the typing
  });

  it('places the editor off the surface box, since there is no node yet', () => {
    const start = source.slice(
      source.indexOf('function startTextEdit'),
      source.indexOf('function commitTextEdit'),
    );

    expect(start).toContain('const frame = frameOf(s.panel);');
    expect(start).toContain('surfaceToPage({ x: s.x, y: s.y }, frame)');
    expect(start).not.toContain('getAbsolutePosition');
    // and over the words of a boxed label, not over its corner
    expect(start).toContain('textBoxPad(s.fontSize)');
    expect(source).toContain('const pad = textBoxPad(fontSize) / k;');
  });

  it('holds more than one line, the way Konva draws it', () => {
    expect(canvasSource).toContain('<textarea');
    expect(canvasSource).not.toContain('<input\n      class="text-edit"');
    expect(canvasSource).toContain("if (event.key === 'Enter' && !event.shiftKey)");
    expect(canvasSource).toContain('white-space: pre;');
    expect(canvasSource).toContain("rows={textEdit.value.split('\\n').length}");
  });
});

describe('the room around the page', () => {
  it('pans under a drawing tool, the way it does under Select', () => {
    // Same room, same drag: it answered only to Select, so with a box in hand
    // the press was lost and the crosshair promised a stroke it never drew.
    expect(down).toContain('if (!hit) {\n      startPan();');
  });

  it('says so with the cursor while the view slides', () => {
    expect(source).toContain("containerEl.style.cursor = 'grabbing';");
    expect(source).toContain("containerEl.style.cursor = spacePan ? 'grab' : '';");
  });
});

describe('a freehand stroke', () => {
  it('resizes and turns like the shapes beside it', () => {
    // It is the one kind with neither vertex handles nor a shape to speak of,
    // so a dashed frame with nothing to pull read as a broken selection.
    const ui = source.slice(source.indexOf('transformer.rotateEnabled('), source.indexOf('if (handles) {'));

    expect(ui).toMatch(/rotateEnabled\([\s\S]*?selKind === 'freehand'/);
    expect(ui).toMatch(/enabledAnchors\([\s\S]*?selKind === 'freehand'[\s\S]*?'middle-left'/);
  });

  it('keeps its samples off the handles, which would bury the stroke', () => {
    expect(source).toContain("const POINT_KINDS = new Set(['line', 'arrow', 'curve']);");
  });

  it('folds the whole transform into its samples, having nowhere else to put it', () => {
    const at = source.indexOf("} else if (s.kind === 'freehand') {");
    const fold = source.slice(at, source.indexOf('s.rotation = node.rotation();', at));

    expect(fold).toContain('pointsWithTransform(s.points, node.getTransform().getMatrix())');
    expect(fold).toContain('node.position({ x: 0, y: 0 });');
    expect(fold).toContain('node.rotation(0);'); // nothing left on the node to rotate around
  });
});

describe('the unsaved badge', () => {
  it('goes away when an undo walks back to what is on disk', () => {
    const apply = source.slice(source.indexOf('function applySnapshot'), source.indexOf('function undo()'));

    expect(apply).toContain('dirty = docSnapshot() !== savedSnapshot;');
    expect(apply).not.toContain('dirty = true;');
  });

  it('remembers the document every save and every open leaves there', () => {
    const save = source.slice(
      source.indexOf('async function performSave'),
      source.indexOf('/** Open the list fresh'),
    );
    const open = source.slice(
      source.indexOf('async function openProof('),
      source.indexOf('const selectedShapes'),
    );
    const reset = source.slice(source.indexOf('function resetDoc'), source.indexOf('function discardProof'));

    expect(save).toContain('savedSnapshot = docSnapshot();');
    expect(open).toContain('savedSnapshot = docSnapshot();');
    expect(reset).toContain('savedSnapshot = null;');
  });
});

describe('Proof Composer — what the proof says about itself', () => {
  const side = source.slice(source.indexOf('<span>Source</span>'), source.indexOf('<ProofLayersPanel'));

  it('puts the description and the date under Source, the sentence first', () => {
    expect(side.indexOf('<span>Description</span>')).toBeGreaterThan(-1);
    expect(side.indexOf('<span>Date</span>')).toBeGreaterThan(side.indexOf('<span>Description</span>'));
  });

  it('says both are optional, rather than leaving the `!` above to imply it', () => {
    expect(side.match(/class="meta-optional">optional</g)).toHaveLength(2);
  });

  it('edits the date in one field that writes the case\'s temporal profile', () => {
    expect(side).toContain('<DateField');
    expect(side).not.toContain('<TemporalInput');
    expect(side).toContain('placeholder="dd/mm/yyyy · Oct 2025 · ~2025"');
    expect(side).toContain('onchange={(value) => { proof.when = value; dirty = true; }}');
  });

  it('never marks an empty date as missing, the way a coordinate is', () => {
    expect(side.slice(side.indexOf('<span>Date</span>'))).not.toContain('meta-warn');
  });

  it('never fills the date in, from a panel or from anything else', () => {
    // Coordinates and sources are answered by the panels; a date is not. The
    // composer must state it because somebody decided it, not because a file
    // carried a number.
    expect(source).not.toContain('offeredWhen');
    expect(source).not.toContain('autoWhen');
  });

  it('empties it in one press, rather than handing it back to the panels', () => {
    expect(side).toContain('title="Clear the date"');
    expect(side).toContain("onclick={() => { proof.when = ''; dirty = true; }}");
    expect(side).toContain('<Icon name="trash" size={12} />');
  });

  it('opens on both, and blanks both on a fresh proof', () => {
    expect(source).toContain("proof.when = typeof spec.when === 'string' ? spec.when : '';");
    expect(source).toContain(
      "proof.description = typeof spec.description === 'string' ? spec.description : '';",
    );
    expect(bodyOfFn('resetDoc')).toContain("proof.when = '';");
    expect(bodyOfFn('resetDoc')).toContain("proof.description = '';");
  });

  it('hands the description to the post it opens', () => {
    expect(source).toContain('description: proof.description,');
  });

  it('names the clock a date is read on, the first point\'s unless another is picked', () => {
    const clock = side.slice(side.indexOf('<DateField'));
    expect(clock).toContain('{#if proof.when.trim()}');
    expect(clock).toContain("clock={{ zone: proof.whenZone || pointZone || 'UTC', fixed: '' }}");
    expect(clock).toContain("here={pointZone ? [{ zone: pointZone, place: 'the point' }] : []}");
    expect(clock).toContain('zone={proof.whenZone || pointZone || null}');
    expect(clock).toContain('No point yet, so the day is read in UTC.');
    // the point's own row goes back to following the point
    expect(bodyOfFn('pickClock')).toContain('proof.whenZone = here ? null : zone;');
    expect(source).toContain("await zoneAt({ lat: Number(where.lat), lon: Number(where.lon) })");
    expect(bodyOfFn('resetDoc')).toContain('proof.whenZone = null;');
  });

  it('hands the date and its clock to the post it opens', () => {
    expect(source).toContain('when: proof.when,');
    expect(source).toContain('whenZone: result.when_zone ?? null,');
  });
});

describe('Proof Composer — dating the material', () => {
  it('says what the save stated, without asking first', () => {
    expect(source).toContain("if (result.dated?.length) toast(datedLabel(result.dated), 'ok', 2600);");
  });

  it('names one file and counts past one, like the places beside it', () => {
    const body = bodyOfFn('datedLabel');
    expect(body).toContain('`Dated the material: ${dated[0]}`');
    expect(body).toContain('`Dated ${dated.length} files this proof rests on`');
  });
});

describe('Proof Composer — numbered markers and blur boxes', () => {
  it('offers both on the rail, with a key each', () => {
    expect(source).toContain("{ id: 'number', icon: 'numbered', label: 'Numbered marker', shortcut: 'n' },");
    expect(source).toContain("{ id: 'blur', icon: 'blurBox', label: 'Blur box', shortcut: 'b' },");
    expect(source).toContain("else if (e.key === 'n') tool = 'number';");
    expect(source).toContain("else if (e.key === 'b') tool = 'blur';");
  });

  it('stamps a marker and keeps the tool, the way the symbol stamp does', () => {
    const body = bodyOfFn('onPointerDown');
    expect(body).toContain("if (tool === 'number') {");
    expect(body).toContain('n: nextMarkerNumber(color)');
    // no `tool = 'select'` in that branch: numbering four things is one act
    const branch = body.slice(body.indexOf("if (tool === 'number') {"));
    expect(branch.slice(0, branch.indexOf('return;'))).not.toContain("tool = 'select'");
  });

  it('counts each colour as its own series, starting back at 1', () => {
    const body = bodyOfFn('nextMarkerNumber');
    // colour is what this composer says "same feature" with, so a second colour
    // is a second series rather than a continuation of the first
    expect(body).toContain("x.kind === 'number' && x.color === ink");
    expect(body).toContain('while (taken.has(n)) n += 1;');
  });

  it('renumbers a marker recoloured into another series', () => {
    const body = bodyOfFn('setColor');
    expect(body).toContain("if (s.kind === 'number' && s.color !== c) s.n = nextMarkerNumber(c);");
  });

  it('gives both a marker and a blur box handles to resize and turn', () => {
    const ui = source.slice(source.indexOf('transformer.keepRatio('), source.indexOf('if (handles) {'));
    // a disc stays a disc
    expect(ui).toMatch(/keepRatio\([\s\S]*?selKind === 'number'/);
    expect(ui).toMatch(/rotateEnabled\([\s\S]*?selKind === 'number' \|\| selKind === 'blur'/);
    // a box takes the full set, a marker the corners
    expect(ui).toMatch(/enabledAnchors\([\s\S]*?selKind === 'blur'[\s\S]*?'middle-left'/);
    expect(ui).toMatch(/selKind === 'text' \|\| selKind === 'icon' \|\| selKind === 'number'/);
  });

  it('writes down where either one was dropped, panel and all', () => {
    // both used to fall through to the polyline branch, where reading the points
    // they have none of threw inside dragend: the drop was never written and the
    // mark sprang back to where it had been picked up
    const body = bodyOfFn('anchorOf');
    expect(body).toContain("if (s.kind === 'rect' || s.kind === 'blur' || s.kind === 'zoom') {");
    expect(body).toContain('if (!Array.isArray(s.points)) return { x: s.x, y: s.y };');
    expect(bodyOfFn('landShape')).toContain('surfaceToPage(anchorOf(s), from)');
  });

  it('keeps a colour away from the one mark drawn in no ink', () => {
    expect(source).toContain("? editableShapes.every((s) => s.kind === 'blur')");
    expect(source).toContain('showColor={!colorless}');
    // and never writes one on it either, at the draw or from the palette
    expect(source).toContain("...(kind === 'blur' ? {} : { color, strokeWidth: strokeW }),");
    expect(bodyOfFn('setColor')).toContain(
      "const inked = editableShapes.filter((s) => s.kind !== 'blur');"
    );
  });

  it('draws a blur box by dragging, and files it as a rectangle', () => {
    expect(source).toContain("} else if (tool === 'blur') {");
    expect(source).toContain("if ((kind === 'rect' || kind === 'blur') && node.width() > minSize");
    // the draft is resized as a box, not as a polyline: a Rect has no points()
    expect(source).toContain("} else if (kind === 'rect' || kind === 'blur' || kind === 'zoom') {");
  });

  it('blurs the picture itself, through the surface the box sits on', () => {
    const body = bodyOfFn('makeBlurNode');
    expect(body).toContain('filters: [Konva.Filters.Blur]');
    expect(body).toContain('const radius = blurRadiusFor(s);');
    expect(body).toContain('blurRadius: radius');
    expect(body).toContain('surface.sourceNatural');
    expect(body).toContain('surface.crop');
    // a filter only draws once the node is cached
    expect(body).toContain('if (patch) node.cache();');
    // the margin is read, not drawn: the clip keeps the box the size it was drawn
    expect(body).toContain('clip: { x: 0, y: 0, width: s.w, height: s.h }');
    // and it follows the box as it is dragged and as it is turned
    expect(body).toContain("group.on('dragmove', paint);");
    expect(body).toContain("group.on('transform', paint);");
    // a turned box hides the ground under it, so the patch is turned back level
    expect(body).toContain('w: s.w, h: s.h, rotation: group.rotation()');
    expect(body).toContain('rotation: patch.turn');
    // Konva measures a group by its children and ignores the clip, so the frame
    // stood a blur radius off the box until this answered with the box itself
    expect(body).toContain('group.getClientRect = ({ skipTransform, relativeTo } = {}) => {');
  });

  it('hands each shape the surface it stands on, panel or pasted image', () => {
    expect(source).toContain('addSurfaceShape(group, s, box, panel);');
    expect(source).toContain('addSurfaceShape(group, s, box, paste);');
    expect(bodyOfFn('addSurfaceShape')).toContain('const node = makeShapeNode(s, box, surface);');
  });

  it('keeps a line width away from the two marks that have no line', () => {
    expect(source).toContain("editableShapes.every((s) => s.kind === 'number' || s.kind === 'blur')");
    expect(source).toContain('showStroke={!solidIconOnly && !strokeless}');
  });
});

describe('ProofComposer offers what is open to the Add event bar', () => {
  it('publishes under its own tool id and withdraws on unmount', () => {
    expect(source).toMatch(/offerNote\(\s*'proof',/);
    expect(source).toContain("onDestroy(() => withdrawNote('proof'))");
  });
});