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
29.8 KB
Raw Blame
import { describe, it, expect } from 'vitest';
import {
  TWEET_TOKENS, DEFAULT_TWEET_BODY, tweetFields, buildTweet1,
  extraPostTweetText, groupPostMedia, mediaTweetText, newPostMediaTweet,
  applyPostTemplateStructure, normalizePostMediaPickerTarget, normPostTemplate, postMediaForType,
  handoffThread, postCharacterCount, postComposeUrl, postHandoffUrl, postReportMarkdown, postTarget,
  proofSourceMediaPaths, renumberMediaTweetText, retargetMediaTweetText,
  sourceSection, sourceUrls,
  templateFromPost, templateUsesPostField, togglePostMedia,
  pointLines, postCoordinates, splitPointLine, postDate,
  POST_TEMPLATE_STARTERS, bodyTokens, fixMisspelledToken, misspelledTokens,
} from './post.js';

const full = {
  place: 'Bakhmut, Donetsk, Ukraine',
  plusCode: '8FPGXXXX+XX',
  description: 'Rooftop match',
  lat: 48.85, lon: 2.35,
  mention: '@GeoConfirmed',
  source: 'https://x.com/a/1',
};

describe('tweetFields — resolve a draft into token values', () => {
  it('formats coordinates to six decimals and trims text', () => {
    const f = tweetFields({ ...full, place: '  Bakhmut  ' });
    expect(f.place).toBe('Bakhmut');
    expect(f.coordinates).toBe('48.850000, 2.350000');
  });

  it('leaves absent tokens empty (no coords without a point)', () => {
    const f = tweetFields({ mention: '' });
    expect(f.coordinates).toBe('');
    expect(f.mention).toBe('');
  });
});

describe('buildTweet1 — the default body', () => {
  it('renders the classic full thread', () => {
    expect(buildTweet1(DEFAULT_TWEET_BODY, full)).toBe(
      'Bakhmut, Donetsk, Ukraine - 8FPGXXXX+XX\n\n' +
      'Rooftop match\n\n' +
      '48.850000, 2.350000\n\n' +
      '@GeoConfirmed\n\n' +
      'Source: https://x.com/a/1'
    );
  });
});

describe('buildTweet1 — token ordering + line control', () => {
  it('honours a custom order', () => {
    const body = '#coordinates\n#mention\n#place';
    expect(buildTweet1(body, full)).toBe('48.850000, 2.350000\n@GeoConfirmed\nBakhmut, Donetsk, Ukraine');
  });

  it('keeps the analyst blank lines between tokens', () => {
    expect(buildTweet1('#coordinates\n\n#source', full)).toBe('48.850000, 2.350000\n\nhttps://x.com/a/1');
  });

  it('drops a line whose tokens are all empty, label and all', () => {
    // no source → the whole "Source: #source" line vanishes
    const noSource = { ...full, source: '' };
    expect(buildTweet1(DEFAULT_TWEET_BODY, noSource)).toBe(
      'Bakhmut, Donetsk, Ukraine - 8FPGXXXX+XX\n\nRooftop match\n\n48.850000, 2.350000\n\n@GeoConfirmed'
    );
  });

  it('trims a dangling separator when one side of a combined line is empty', () => {
    expect(buildTweet1('#place - #pluscode', { ...full, plusCode: '' })).toBe('Bakhmut, Donetsk, Ukraine');
    expect(buildTweet1('#place - #pluscode', { ...full, place: '' })).toBe('8FPGXXXX+XX');
  });

  it('collapses blank-line runs and trims the ends', () => {
    expect(buildTweet1('\n\n#coordinates\n\n\n\n#mention\n\n', full))
      .toBe('48.850000, 2.350000\n\n@GeoConfirmed');
  });

  it('a coords-only body posts barebones', () => {
    expect(buildTweet1('#coordinates', full)).toBe('48.850000, 2.350000');
  });

  it('keeps literal text on lines that also carry a filled token', () => {
    expect(buildTweet1('Filmed at #place today', full)).toBe('Filmed at Bakhmut, Donetsk, Ukraine today');
  });

  it('exposes the insertable token palette', () => {
    expect(TWEET_TOKENS.map((t) => t.tag)).toEqual([
      '#place', '#pluscode', '#coordinates', '#description', '#mention', '#source', '#date',
    ]);
    for (const t of TWEET_TOKENS) expect(t.sample).toBeTruthy();
  });
});

describe('editable media tweets', () => {
  it('puts the media prefix in the tweet text', () => {
    expect(mediaTweetText('video', '', 2)).toBe('2/ Video:');
    expect(mediaTweetText('images', 'Caption', 2)).toBe('2/ Image:\nCaption');
    expect(mediaTweetText('images', '', 3)).toBe('3/ Image:');
  });

  it('updates an untouched prefix when the selected media type changes', () => {
    expect(retargetMediaTweetText('2/ Video:\nCaption', 'video', 'images', 2))
      .toBe('2/ Image:\nCaption');
  });

  it('preserves a prefix the analyst edited', () => {
    expect(retargetMediaTweetText('2/ Footage:\nCaption', 'video', 'images', 2))
      .toBe('2/ Footage:\nCaption');
  });

  it('creates an empty third media tweet without choosing a type', () => {
    expect(newPostMediaTweet(7, 3)).toEqual({
      id: 7,
      text: '',
      mediaPaths: [],
      mediaType: 'none',
      isMediaTweet: true,
      mediaTextIncludesPrefix: true,
    });
  });

  it('adds the numbered prefix once a media type is known', () => {
    expect(newPostMediaTweet(8, 3, 'video')).toEqual({
      id: 8,
      text: '3/ Video:',
      mediaPaths: [],
      mediaType: 'video',
      isMediaTweet: true,
      mediaTextIncludesPrefix: true,
    });
  });

  it('renumbers an untouched prefix after an earlier tweet is removed', () => {
    expect(renumberMediaTweetText('4/ Video:\nCaption', 'video', 4, 3))
      .toBe('3/ Video:\nCaption');
    expect(renumberMediaTweetText('Footage:\nCaption', 'video', 4, 3))
      .toBe('Footage:\nCaption');
  });

  it('copies new prefixes verbatim and upgrades legacy media tweets', () => {
    expect(extraPostTweetText({
      text: '3/ Image:\nCaption', mediaPaths: ['media/a.jpg'],
      mediaType: 'images', mediaTextIncludesPrefix: true,
    }, 3)).toBe('3/ Image:\nCaption');
    expect(extraPostTweetText({
      text: 'Caption', mediaPaths: ['media/a.jpg'], mediaType: 'images',
    }, 3)).toBe('3/ Image:\nCaption');
  });
});

describe('togglePostMedia — manual media override', () => {
  it('adds and removes an image', () => {
    const image = { path: 'media/a.jpg', kind: 'image' };
    const added = togglePostMedia({}, image);
    expect(added).toEqual({ mediaPaths: ['media/a.jpg'], mediaType: 'images', outcome: 'added' });
    expect(togglePostMedia(added, image)).toEqual({
      mediaPaths: [], mediaType: 'images', outcome: 'removed',
    });
  });

  it('replaces proof-filled video when an image is selected', () => {
    expect(togglePostMedia(
      { mediaPaths: ['media/source.mp4'], mediaType: 'video' },
      { path: 'media/override.jpg', kind: 'image' },
    )).toEqual({
      mediaPaths: ['media/override.jpg'], mediaType: 'images', outcome: 'replaced',
    });
  });

  it('keeps the current selection when the limit is reached', () => {
    const mediaPaths = ['media/1.jpg', 'media/2.jpg', 'media/3.jpg', 'media/4.jpg'];
    expect(togglePostMedia(
      { mediaPaths, mediaType: 'images' },
      { path: 'media/5.jpg', kind: 'image' },
    )).toEqual({ mediaPaths, mediaType: 'images', outcome: 'limit' });
  });
});

describe('postMediaForType — picker filtering', () => {
  const items = [
    { path: 'media/a.jpg', kind: 'image' },
    { path: 'media/b.mp4', kind: 'video' },
    { path: 'media/c.mp3', kind: 'audio' },
  ];

  it('shows only images in image mode', () => {
    expect(postMediaForType(items, 'images')).toEqual([items[0]]);
  });

  it('shows only videos in video mode', () => {
    expect(postMediaForType(items, 'video')).toEqual([items[1]]);
  });
});

describe('normalizePostMediaPickerTarget — tweet 2 regression', () => {
  it('treats a click event as tweet 2 instead of an extra-tweet id', () => {
    expect(normalizePostMediaPickerTarget({ type: 'click' })).toBeNull();
  });

  it('keeps an extra tweet id', () => {
    expect(normalizePostMediaPickerTarget(3)).toBe(3);
  });
});

describe('groupPostMedia — proof media tweets', () => {
  const kind = (path) => path.endsWith('.mp4') ? 'video' : 'image';

  it('packs up to four files of the same type into one tweet', () => {
    expect(groupPostMedia(
      ['media/1.mp4', 'media/2.mp4', 'media/3.mp4', 'media/4.mp4'], kind,
    )).toEqual([{
      mediaType: 'video',
      mediaPaths: ['media/1.mp4', 'media/2.mp4', 'media/3.mp4', 'media/4.mp4'],
    }]);
  });

  it('separates images and videos', () => {
    expect(groupPostMedia(
      ['media/1.mp4', 'media/1.jpg', 'media/2.mp4', 'media/2.jpg'], kind,
    )).toEqual([
      { mediaType: 'video', mediaPaths: ['media/1.mp4', 'media/2.mp4'] },
      { mediaType: 'images', mediaPaths: ['media/1.jpg', 'media/2.jpg'] },
    ]);
  });

  it('starts another same-type tweet after four files', () => {
    expect(groupPostMedia(
      ['media/1.jpg', 'media/2.jpg', 'media/3.jpg', 'media/4.jpg', 'media/5.jpg'], kind,
    )).toEqual([
      { mediaType: 'images', mediaPaths: ['media/1.jpg', 'media/2.jpg', 'media/3.jpg', 'media/4.jpg'] },
      { mediaType: 'images', mediaPaths: ['media/5.jpg'] },
    ]);
  });
});

describe('templateFromPost / normPostTemplate — round-trip', () => {
  it('captures mention, body, media and extra-tweet skeleton, no content', () => {
    const t = templateFromPost({
      mention: '@Desk',
      body: '#coordinates\n#source',
      mediaEnabled: false,
      extraTweets: [{ id: 1, text: 'Boilerplate close' }],
    });
    expect(t).toEqual({
      mention: '@Desk',
      body: '#coordinates\n#source',
      mediaEnabled: false,
      extraTweets: [{ text: 'Boilerplate close' }],
    });
  });

  it('falls back to the default body when none is given', () => {
    expect(templateFromPost({ mention: '@x' }).body).toBe(DEFAULT_TWEET_BODY);
    expect(templateFromPost({ body: '   ' }).body).toBe(DEFAULT_TWEET_BODY);
  });

  it('fills shipped defaults for a bare blob', () => {
    expect(normPostTemplate({})).toEqual({
      mention: '@GeoConfirmed', body: DEFAULT_TWEET_BODY, mediaEnabled: true, extraTweets: [],
    });
  });

  it('migrates a legacy toggle template to a token body', () => {
    const legacy = { toggles: { showHeader: false, showDescription: false, showCoords: true, showMention: false, showSource: true } };
    expect(normPostTemplate(legacy).body).toBe('#coordinates\n\nSource: #source');
  });

  it('a token template survives a save → normalise round-trip unchanged', () => {
    const stored = templateFromPost({
      mention: '@Desk', body: '#place\n#coordinates', mediaEnabled: false,
      extraTweets: [{ text: 'a' }, { text: 'b' }],
    });
    expect(normPostTemplate(stored)).toEqual(stored);
  });

  it('preserves an intentionally blank mention', () => {
    const stored = templateFromPost({ mention: '', body: '#place' });
    expect(normPostTemplate(stored).mention).toBe('');
  });

  it('caps malformed hand-edited arrays and strings', () => {
    const normalized = normPostTemplate({
      mention: 'm'.repeat(400),
      body: 'b'.repeat(20_000),
      extraTweets: Array.from({ length: 25 }, () => ({ text: 'x'.repeat(5_000) })),
    });
    expect(normalized.mention).toHaveLength(280);
    expect(normalized.body).toHaveLength(16_000);
    expect(normalized.extraTweets).toHaveLength(20);
    expect(normalized.extraTweets[0].text).toHaveLength(4_000);
  });
});

describe('applyPostTemplateStructure — replace the whole skeleton', () => {
  it('clears existing extra posts when the template has none', () => {
    const current = {
      mention: '@Old', body: '#source', mediaEnabled: true,
      extraTweets: [{ text: 'Old', mediaPaths: ['media/a.png'] }],
    };
    const applied = applyPostTemplateStructure(current, {
      mention: '', body: '#place', mediaEnabled: false, extraTweets: [],
    });
    expect(applied).toMatchObject({
      mention: '', body: '#place', mediaEnabled: false, extraTweets: [],
    });
  });

  it('drops attachments when creating template extra posts', () => {
    const applied = applyPostTemplateStructure({}, { extraTweets: [{ text: 'Close' }] });
    expect(applied.extraTweets).toEqual([{
      text: 'Close', mediaPaths: [], mediaType: 'none',
      isMediaTweet: false, mediaTextIncludesPrefix: false,
    }]);
  });
});

describe('templateUsesPostField — compose fields controlled by a template', () => {
  it('disables fields whose tokens are absent', () => {
    const body = '#coordinates\n#source';
    expect(templateUsesPostField(body, 'coordinates')).toBe(true);
    expect(templateUsesPostField(body, 'source')).toBe(true);
    expect(templateUsesPostField(body, 'description')).toBe(false);
    expect(templateUsesPostField(body, 'place')).toBe(false);
    expect(templateUsesPostField(body, 'mention')).toBe(false);
  });

  it('keeps coordinates available when they can resolve a place or plus code', () => {
    expect(templateUsesPostField('#place', 'coordinates')).toBe(true);
    expect(templateUsesPostField('#pluscode', 'coordinates')).toBe(true);
  });
});

describe('bodyTokens and misspelledTokens — what a layout will and will not fill', () => {
  it('lists the tokens a body holds, in order, and leaves a longer word alone', () => {
    expect(bodyTokens('#Place - #pluscode\n#placement #dates')).toEqual(['#place', '#pluscode']);
  });

  it('names a word that is one edit from a token, with the token it meant', () => {
    expect(misspelledTokens('#coordinate\n#Sources and #plus_code')).toEqual([
      { written: '#coordinate', meant: '#coordinates' },
      { written: '#Sources', meant: '#source' },
      { written: '#plus_code', meant: '#pluscode' },
    ]);
  });

  it('says nothing about real tokens, real hashtags, or a word named twice', () => {
    expect(misspelledTokens('#place #mention #osint #geolocation')).toEqual([]);
    expect(misspelledTokens('#coordinate and #coordinate')).toHaveLength(1);
  });
});

describe('fixMisspelledToken — the one-click correction', () => {
  it('swaps every whole-word occurrence and leaves the longer token next to it alone', () => {
    const body = '#coordinate\n#coordinates\nagain #coordinate, and #coordinate-x';
    expect(fixMisspelledToken(body, '#coordinate', '#coordinates')).toBe(
      '#coordinates\n#coordinates\nagain #coordinates, and #coordinate-x',
    );
  });

  it('matches the case it was written in, and takes a separator as part of the word', () => {
    expect(fixMisspelledToken('#Sources #sources', '#Sources', '#source')).toBe('#source #sources');
    expect(fixMisspelledToken('#plus_code and #plus_codes', '#plus_code', '#pluscode')).toBe(
      '#pluscode and #plus_codes',
    );
  });

  it('leaves a body with no such word exactly as it was, and a fixed one clean', () => {
    expect(fixMisspelledToken('#place', '#coordinate', '#coordinates')).toBe('#place');
    const fixed = fixMisspelledToken('#coordinate\n#Sources', '#coordinate', '#coordinates');
    expect(misspelledTokens(fixed)).toEqual([{ written: '#Sources', meant: '#source' }]);
  });
});

describe('POST_TEMPLATE_STARTERS — where a new template starts', () => {
  it('starts with the classic thread, unchanged', () => {
    expect(POST_TEMPLATE_STARTERS[0]).toMatchObject({ id: 'classic', body: DEFAULT_TWEET_BODY });
  });

  it('gives every starter its own id and a name to show', () => {
    const ids = POST_TEMPLATE_STARTERS.map((starter) => starter.id);
    expect(new Set(ids).size).toBe(ids.length);
    for (const starter of POST_TEMPLATE_STARTERS) expect(starter.label.trim()).not.toBe('');
  });

  it('writes only real tokens, so no starter goes out with text that looks like one', () => {
    for (const starter of POST_TEMPLATE_STARTERS) {
      expect(misspelledTokens(starter.body), starter.id).toEqual([]);
      expect(bodyTokens(starter.body).length, starter.id).toBeGreaterThan(0);
    }
  });

  it('is read-only, so one editor cannot change what the next one offers', () => {
    expect(Object.isFrozen(POST_TEMPLATE_STARTERS)).toBe(true);
    for (const starter of POST_TEMPLATE_STARTERS) expect(Object.isFrozen(starter)).toBe(true);
  });

  it('offers a dated layout that shows the date, and one that leaves it out', () => {
    const byId = Object.fromEntries(POST_TEMPLATE_STARTERS.map((s) => [s.id, s]));
    const fields = { ...full, date: '2025-10-24 14:30 UTC+3' };
    expect(buildTweet1(byId.dated.body, fields)).toContain('Filmed: 2025-10-24 14:30 UTC+3');
    expect(buildTweet1(byId.classic.body, fields)).not.toContain('2025-10-24');
    expect(buildTweet1(byId.dated.body, full)).not.toContain('Filmed');
  });

  it('builds the short and coordinates-only layouts the way their names promise', () => {
    const byId = Object.fromEntries(POST_TEMPLATE_STARTERS.map((s) => [s.id, s]));
    expect(buildTweet1(byId.coordinates.body, full)).toBe('48.850000, 2.350000\n\n@GeoConfirmed');
    expect(buildTweet1(byId.short.body, full)).toBe(
      'Bakhmut, Donetsk, Ukraine\n48.850000, 2.350000\n\nSource: https://x.com/a/1',
    );
  });
});

describe('proofSourceMediaPaths — source media selected from a proof', () => {
  it('uses original source media behind a derived frame', () => {
    const data = {
      entities: [
        { id: 'proof', type: 'proof', attrs: { path: 'proofs/check.png' } },
        { id: 'frame', type: 'media', attrs: { path: 'media/frame.jpg', kind: 'image' } },
        { id: 'video', type: 'media', attrs: { path: 'media/clip.mp4', kind: 'video' } },
        { id: 'photo', type: 'media', attrs: { path: 'media/still.jpg', kind: 'image' } },
        { id: 'capture', type: 'capture', attrs: { path: 'media/satellite.png', kind: 'image' } },
        { id: 'collage', type: 'media', attrs: { path: 'media/collage.png', kind: 'image' } },
      ],
      links: [
        { from: 'proof', to: 'frame', type: 'derived-from' },
        { from: 'proof', to: 'photo', type: 'derived-from' },
        { from: 'proof', to: 'capture', type: 'derived-from' },
        { from: 'proof', to: 'collage', type: 'derived-from' },
        { from: 'frame', to: 'video', type: 'derived-from' },
      ],
    };
    const media = [
      { path: 'media/frame.jpg', source: { type: 'inspect', from: 'media/clip.mp4' } },
      { path: 'media/clip.mp4', source: { type: 'download', url: 'https://source.test/post', webpage_url: 'https://source.test/post' } },
      { path: 'media/still.jpg', source: { type: 'download', url: 'https://other.test/post' } },
      { path: 'media/collage.png', source: { op: 'collage', sources: ['media/still.jpg'] } },
    ];
    expect(proofSourceMediaPaths(data, 'proofs/check.png', 'https://source.test/post', media))
      .toEqual(['media/clip.mp4']);
  });

  it('does not select media without a matching proof source', () => {
    expect(proofSourceMediaPaths({ entities: [], links: [] }, 'proofs/missing.png', 'https://source.test/post')).toEqual([]);
  });
});

describe('post targets', () => {
  it('keeps legacy and unknown draft targets on X', () => {
    expect(postTarget()).toMatchObject({ id: 'x', limit: 280 });
    expect(postTarget('unknown')).toMatchObject({ id: 'x' });
  });

  it('counts links for X, but graphemes for Bluesky', () => {
    expect(postCharacterCount('x', 'A https://example.test/very-long-url')).toBe(25);
    expect(postCharacterCount('bluesky', 'A https://example.test/very-long-url')).toBe(36);
    expect(postCharacterCount('bluesky', '👍🏽')).toBe(1);
  });

  it('builds the official compose handoff for each social target', () => {
    expect(postComposeUrl('x', 'A post')).toBe('https://x.com/intent/post?text=A%20post');
    expect(postComposeUrl('bluesky', 'A post')).toBe('https://bsky.app/intent/compose?text=A%20post');
  });

  it('opens the full composer where the extension takes over, since that is the one that threads', () => {
    // The intent page posts one message. The run of posts under the first is the
    // whole point of handing over, so it needs the composer that builds a thread.
    expect(postHandoffUrl('x', 'A post')).toBe('https://x.com/compose/post?text=A%20post');
    expect(postHandoffUrl('bluesky', 'A post')).toBe('https://bsky.app/intent/compose?text=A%20post');
  });

  it('opens a dropped or hand-edited target on X, like every other reader of one', () => {
    // Mastodon was a target and is not: its share link is a redirector that asks
    // which server first, so nothing could say where the thread was going.
    expect(postHandoffUrl('mastodon', 'A post')).toBe('https://x.com/compose/post?text=A%20post');
    expect(postComposeUrl('mastodon', 'A post')).toBe('https://x.com/intent/post?text=A%20post');
    expect(postTarget('mastodon').id).toBe('x');
  });
});

describe('handoffThread — the thread as the extension receives it', () => {
  it('pairs each post with the case files it carries', () => {
    const { posts, dropped } = handoffThread([
      { text: 'Geolocated.', files: ['proofs/strike.png'] },
      { text: 'Video:', files: ['media/clip.mp4'] },
    ]);

    expect(posts).toEqual([
      { text: 'Geolocated.', files: ['proofs/strike.png'] },
      { text: 'Video:', files: ['media/clip.mp4'] },
    ]);
    expect(dropped).toBe(0);
  });

  it('keeps a post that is only a picture, and drops one that is nothing', () => {
    const { posts } = handoffThread([
      { text: '   ', files: ['media/a.jpg'] },
      { text: '', files: [] },
      { text: 'Context.' },
    ]);

    expect(posts).toEqual([
      { text: '   ', files: ['media/a.jpg'] },
      { text: 'Context.', files: [] },
    ]);
  });

  it('counts what a post cannot carry rather than losing it quietly', () => {
    const files = ['a.jpg', 'b.jpg', 'c.jpg', 'd.jpg', 'e.jpg', 'f.jpg'];
    const { posts, dropped } = handoffThread([{ text: 'Five angles', files }]);

    expect(posts[0].files).toEqual(['a.jpg', 'b.jpg', 'c.jpg', 'd.jpg']);
    expect(dropped).toBe(2);
  });

  it('survives an absent list', () => {
    expect(handoffThread()).toEqual({ posts: [], dropped: 0 });
  });
});

describe('sourceSection — a proof resting on more than one post', () => {
  it('links every address when the line is nothing but addresses', () => {
    // The composer hands over one address per line, so this is the ordinary shape of a
    // proof built from two posts. A run separated by dots is what proofs saved before
    // that carried, and it reads the same.
    const listed = [
      '- [https://x.com/a/1](https://x.com/a/1)',
      '- [https://t.me/b/2](https://t.me/b/2)',
    ].join('\n');
    expect(sourceSection('https://x.com/a/1\nhttps://t.me/b/2')).toBe(listed);
    expect(sourceSection('https://x.com/a/1  ·  https://t.me/b/2')).toBe(listed);
    // One address keeps the reading it always had.
    expect(sourceSection('https://x.com/a/1')).toBe('[Open original source](https://x.com/a/1)');
  });

  it('prints prose instead of linking it', () => {
    expect(sourceSection('handed to me on a stick')).toBe('handed to me on a stick');
    // A line that starts with an address and goes on saying something used to be
    // wrapped whole in one href, which is a dead link either way.
    expect(sourceSection('https://x.com/a/1 reposted from elsewhere')).toBe(
      'https://x.com/a/1 reposted from elsewhere',
    );
    expect(sourceSection('')).toBe('');
  });

  it('keeps the report and the media matcher reading the same line', () => {
    const line = 'https://x.com/a/1\nhttps://t.me/b/2';
    const report = postReportMarkdown({ title: 'Two posts', source: line });
    expect(report).toContain('- [https://x.com/a/1](https://x.com/a/1)');
    expect(report).toContain('- [https://t.me/b/2](https://t.me/b/2)');
    expect(report).not.toContain(`(${line})`); // the one broken href this replaces
    // And the matcher reads the same block the report just rendered.
    expect(sourceUrls(line)).toEqual(['https://x.com/a/1', 'https://t.me/b/2']);
  });
});

describe('postReportMarkdown', () => {
  it('renders a structured report with links, maps and embedded case media', () => {
    const report = postReportMarkdown({
      title: 'Rooftop match',
      place: 'Bakhmut, Ukraine',
      plusCode: '8FPGXXXX+XX',
      coordinates: '48.850000, 2.350000',
      dms: '48°51′00.0″N 2°21′00.0″E',
      mapLinks: {
        Google: 'https://maps.example.test',
        OSM: 'https://osm.example.test',
      },
      description: 'Match against reference imagery.',
      source: 'https://example.test/source',
      attachments: ['proofs/match.png', 'media/a.jpg', 'media/a.jpg'],
      proofEntity: { id: 'e_proof', label: 'Rooftop proof', attrs: { path: 'proofs/match.png' } },
      mediaEntities: [{ id: 'e_frame', label: 'Frame', attrs: { path: 'media/a.jpg', kind: 'image' } }],
    });

    expect(report).toContain('| Coordinates | `48.850000, 2.350000` |');
    expect(report).toContain('| Maps | [Google](https://maps.example.test) · [OSM](https://osm.example.test) |');
    expect(report).toContain('[Open original source](https://example.test/source)');
    expect(report).toContain('## Assessment\n\nMatch against reference imagery.');
    expect(report).not.toContain('## Prepared thread');
    expect(report).toContain('[[entity:e_proof|Rooftop proof]]');
    expect(report).toContain('[[media:e_proof|Rooftop proof]]{width=100% align=center}');
    expect(report).toContain('[[entity:e_frame|Frame]]');
    expect(report).toContain('[[media:e_frame|Frame]]{width=100% align=center}');
    expect(report).not.toContain('- proofs/match.png');
  });

  it('always gives an otherwise empty export a title', () => {
    expect(postReportMarkdown()).toBe('# Untitled report\n');
  });
});


describe('the points a post carries', () => {
  it('reads a line with a name and a line without', () => {
    expect(splitPointLine('impact 2 — 64.148100, -21.940100')).toEqual({
      label: 'impact 2', coords: '64.148100, -21.940100',
    });
    expect(splitPointLine('  64.148100, -21.940100 ')).toEqual({
      label: '', coords: '64.148100, -21.940100',
    });
  });

  it('drops the blank lines a pasted block leaves behind', () => {
    expect(pointLines('64.1466, -21.9426\n\n  \nb — 1, 2')).toEqual([
      { label: '', coords: '64.1466, -21.9426' },
      { label: 'b', coords: '1, 2' },
    ]);
  });

  it('reads the resolved point in decimal and the rest as written', () => {
    // the first line is the one the post looked up, so it reads as it always has
    expect(postCoordinates('64°08\'48"N 21°56\'33"W\nimpact 2 — 64.1481, -21.9401', {
      lat: 64.1466, lon: -21.9426,
    })).toBe('64.146600, -21.942600\nimpact 2 — 64.1481, -21.9401');
  });

  it('keeps the name of the point it resolved', () => {
    expect(postCoordinates('caméra — 64.1466, -21.9426', { lat: 64.1466, lon: -21.9426 }))
      .toBe('caméra — 64.146600, -21.942600');
  });

  it('carries one point exactly as it did before the list', () => {
    expect(tweetFields({ lat: 48.85, lon: 2.35 }).coordinates).toBe('48.850000, 2.350000');
  });

  it('carries every point into the tweet, one per line', () => {
    const fields = tweetFields({
      lat: 64.1466, lon: -21.9426,
      coordsText: 'impact 1 — 64.1466, -21.9426\nimpact 2 — 64.1481, -21.9401',
    });
    expect(fields.coordinates).toBe(
      'impact 1 — 64.146600, -21.942600\nimpact 2 — 64.1481, -21.9401'
    );
  });

  it('tables the points in the report, once there is more than one', () => {
    const report = postReportMarkdown({
      title: 'Harbour strike',
      coordinates: '64.146600, -21.942600',
      points: [
        { label: 'impact 1', coords: '64.1466, -21.9426' },
        { label: '', coords: '64.1481, -21.9401' },
      ],
    });
    expect(report).toContain('## Points');
    expect(report).toContain('| impact 1 | `64.1466, -21.9426` |');
    expect(report).toContain('| Point 2 | `64.1481, -21.9401` |'); // unnamed takes its rank
  });

  it('leaves a single-point report the shape it had', () => {
    const report = postReportMarkdown({
      title: 'One point',
      coordinates: '64.146600, -21.942600',
      points: [{ label: '', coords: '64.1466, -21.9426' }],
    });
    expect(report).not.toContain('## Points');
  });
});

describe('#date — the date a proof states, only where a template asks for it', () => {
  it('fills its own line and drops it when there is no date', () => {
    const body = '#place\n\nFilmed: #date\n\n#coordinates';
    expect(buildTweet1(body, { ...full, date: '2024-03-12' }))
      .toBe('Bakhmut, Donetsk, Ukraine\n\nFilmed: 2024-03-12\n\n48.850000, 2.350000');
    expect(buildTweet1(body, { ...full, date: '' }))
      .toBe('Bakhmut, Donetsk, Ukraine\n\n48.850000, 2.350000');
  });

  it('never enters the classic thread on its own', () => {
    expect(DEFAULT_TWEET_BODY).not.toContain('#date');
    expect(templateUsesPostField(DEFAULT_TWEET_BODY, 'date')).toBe(false);
    expect(templateUsesPostField('Filmed: #Date', 'date')).toBe(true);
  });

  it('leaves a longer hashtag a template already carried alone', () => {
    expect(templateUsesPostField('#dates #placement', 'date')).toBe(false);
    expect(templateUsesPostField('#dates #placement', 'place')).toBe(false);
    expect(buildTweet1('#placement #dates', { ...full, date: '2024-03-12' })).toBe('#placement #dates');
  });
});

describe('postDate — the stored value, in no language, with its clock', () => {
  it('keeps a date or a range of dates exactly as the case holds it', () => {
    expect(postDate('2024-03-12', 'Europe/Kyiv')).toBe('2024-03-12');
    expect(postDate('2024-03~')).toBe('2024-03~');
    expect(postDate('2024-03-12/2024-03-15?')).toBe('2024-03-12/2024-03-15?');
    expect(postDate('')).toBe('');
  });

  it('says which clock a time is on, as an offset from UTC on that day', () => {
    expect(postDate('2024-07-12T14:30:00', 'Europe/Kyiv')).toBe('2024-07-12 14:30 UTC+3');
    expect(postDate('2024-01-12T14:30:00', 'Europe/Kyiv')).toBe('2024-01-12 14:30 UTC+2');
    expect(postDate('2024-01-12T14:30:00Z')).toBe('2024-01-12 14:30 UTC');
    expect(postDate('2024-01-12T14:30:15+05:30')).toBe('2024-01-12 14:30:15 UTC+5:30');
    // Across a clock change the offset is the one in force at that hour.
    expect(postDate('2024-03-31T01:30:00', 'Europe/Paris')).toBe('2024-03-31 01:30 UTC+1');
    expect(postDate('2024-03-31T03:30:00', 'Europe/Paris')).toBe('2024-03-31 03:30 UTC+2');
  });

  it('names no clock it does not know', () => {
    expect(postDate('2024-01-12T14:30:00')).toBe('2024-01-12 14:30');
    expect(postDate('2024-01-12T14:30:00', 'Not/AZone')).toBe('2024-01-12 14:30');
  });

  it('writes a span of times once, with its clock at the end', () => {
    expect(postDate('2024-01-12T14:30:00/2024-01-12T15:00:00', 'Asia/Tokyo')).toBe('2024-01-12 14:30/15:00 UTC+9');
    expect(postDate('2024-01-12T23:30:00Z/2024-01-13T01:00:00Z')).toBe('2024-01-12 23:30/2024-01-13 01:00 UTC');
  });
});