// The word-level diff behind replace: only what changes, nothing when nothing does. import { test } from 'node:test'; import assert from 'node:assert/strict'; import { diffRuns } from '../src/suggestions/diff.ts'; const show = (a: string, b: string) => diffRuns(a, b).map((r) => [a.slice(r.a0, r.a1), b.slice(r.b0, r.b1)]); test('the same text, or text that differs only in quotes and spacing, is no change', () => { assert.deepEqual(diffRuns('Lanterns are cheap.', 'Lanterns are cheap.'), []); assert.deepEqual(diffRuns("It's a “record”, kept\nsafe.", 'It’s a "record", kept safe.'), []); assert.deepEqual(diffRuns('', ''), []); }); test('a rewrite shows as the words that go and the words that come', () => { assert.deepEqual(show('a very big dog', 'a tiny dog'), [['very big', 'tiny']]); assert.deepEqual(show('The quick brown fox jumps.', 'A quick red fox jumps.'), [ ['The', 'A'], ['brown', 'red'], ]); assert.deepEqual(show('Lanterns are cheap to make.', 'Lanterns are cheap.'), [[' to make', '']]); assert.deepEqual(show('abc', 'abc def'), [['', ' def']]); assert.deepEqual(show('wich', 'which'), [['wich', 'which']]); }); test('a whole different sentence is one run', () => { assert.deepEqual(show('a great many', 'several'), [['a great many', 'several']]); });