Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion scripts/verify-browser.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ try {
await page.click('#lang-toggle');
await waitForText(page, '#left-title', 'Original');
assert.equal(await page.evaluate(() => document.documentElement.lang), 'en');
assert.equal(await page.title(), 'Text Diff · Text Compare & Word Count');
});

await check('cycles themes', async () => {
Expand Down
35 changes: 3 additions & 32 deletions tests/diff.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,6 @@ test('skips inline highlights when lines are too different', () => {
assert.equal(row.right.segments, null);
});

test('handles astral characters in inline highlights', () => {
const [row] = computeDiff('a😀b', 'a😀c').rows;
assert.deepEqual(row.left.segments, [{ text: 'a😀', changed: false }, { text: 'b', changed: true }]);
assert.deepEqual(row.right.segments, [{ text: 'a😀', changed: false }, { text: 'c', changed: true }]);
});

test('inline segments always rebuild the original emoji sequences', () => {
const [row] = computeDiff('家人 👨‍👩‍👧 都在', '家人 👨‍👩‍👦 都在').rows;
assert.equal(row.type, 'change');
assert.equal(joined(row.left.segments), '家人 👨‍👩‍👧 都在');
assert.equal(joined(row.right.segments), '家人 👨‍👩‍👦 都在');
});

test('ignoreWhitespace treats half-width and full-width spacing changes as equal', () => {
const result = computeDiff('a b\nc', 'ab\u3000\nc', { ignoreWhitespace: true });
assert.equal(result.identical, true);
Expand Down Expand Up @@ -166,35 +153,19 @@ test('collapseRows accepts an empty list', () => {
assert.deepEqual(collapseRows([]), []);
});

function randomHan(seed, length) {
let state = seed;
return Array.from({ length }, () => {
state = (state * 1103515245 + 12345) % 2147483648;
return String.fromCodePoint(0x4e00 + (state % 500));
}).join('');
}

test('caps the total time spent on inline highlights', () => {
const oldText = Array.from({ length: 100 }, (_, i) => randomHan(i + 1, 1500)).join('\n');
const newText = Array.from({ length: 100 }, (_, i) => randomHan(i + 1001, 1500)).join('\n');
const started = performance.now();
const result = computeDiff(oldText, newText);
const elapsed = performance.now() - started;
assert.equal(result.rows.length, 100);
assert.ok(result.rows.every((row) => row.type === 'change'));
assert.ok(elapsed < 1500, `took ${Math.round(elapsed)} ms`);
});

test('inline highlights cover whole grapheme clusters', () => {
const changedTexts = (segments) => segments.filter((s) => s.changed).map((s) => s.text);
const cases = [
['a😀b', 'a😀c', ['b'], ['c']],
['cafe\u0301 au lait', 'cafe au lait', ['e\u0301'], ['e']],
['國旗 🇹🇼 飄揚', '國旗 🇹🇭 飄揚', ['🇹🇼'], ['🇹🇭']],
['讚 👍🏻 喔', '讚 👍🏿 喔', ['👍🏻'], ['👍🏿']],
['家人 👨‍👩‍👧 都在', '家人 👨‍👩‍👦 都在', ['👨‍👩‍👧'], ['👨‍👩‍👦']],
];
for (const [oldLine, newLine, removed, added] of cases) {
const [row] = computeDiff(oldLine, newLine).rows;
assert.equal(joined(row.left.segments), oldLine);
assert.equal(joined(row.right.segments), newLine);
assert.deepEqual(changedTexts(row.left.segments), removed, oldLine);
assert.deepEqual(changedTexts(row.right.segments), added, newLine);
}
Expand Down
5 changes: 0 additions & 5 deletions tests/i18n.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ import { readFile } from 'node:fs/promises';
import { LANGUAGES, messages } from '../js/i18n.js';
import { STAT_GROUPS } from '../js/render.js';

test('supports Traditional Chinese and English', () => {
assert.deepEqual(LANGUAGES, ['zh-TW', 'en']);
});

test('every language defines the same message keys', () => {
const base = Object.keys(messages['zh-TW']).sort();
for (const lang of LANGUAGES) assert.deepEqual(Object.keys(messages[lang]).sort(), base, lang);
Expand All @@ -16,7 +12,6 @@ test('every language defines the same message keys', () => {
test('every key used in index.html has a message', async () => {
const html = await readFile(new URL('../index.html', import.meta.url), 'utf8');
const keys = [...html.matchAll(/data-i18n(?:-placeholder|-aria-label)?="([^"]+)"/g)].map((match) => match[1]);
assert.ok(keys.length > 20);
for (const key of keys) assert.ok(Object.hasOwn(messages['zh-TW'], key), key);
});

Expand Down
10 changes: 1 addition & 9 deletions tests/importmap.test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { access, readFile } from 'node:fs/promises';
import { readFile } from 'node:fs/promises';

const html = await readFile(new URL('../index.html', import.meta.url), 'utf8');

test('the import map loads jsdiff from the installed npm package', async () => {
const match = html.match(/<script type="importmap">([\s\S]*?)<\/script>/);
assert.ok(match, 'index.html has an import map');
const { imports } = JSON.parse(match[1]);
assert.equal(imports.diff, './node_modules/diff/libesm/index.js');
await access(new URL(`../${imports.diff}`, import.meta.url));
});

test('no script is loaded from another host', () => {
const sources = [...html.matchAll(/<script[^>]*\ssrc="([^"]+)"/g)].map((match) => match[1]);
const mapped = [...html.matchAll(/"(https?:\/\/[^"]+\.m?js)"/g)].map((match) => match[1]);
Expand Down
12 changes: 6 additions & 6 deletions tests/stats.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,11 @@ test('categories add up to the total number of characters', () => {
assert.equal(sum, stats.characters);
});

test('a counter reuses the result while the text and options stay the same', () => {
test('a counter updates results when text or counting options change', () => {
const count = createCounter();
const first = count('同一段文字', { includeNewline: false, theme: 'dark' });
assert.equal(count('同一段文字', { includeNewline: false, theme: 'light' }), first);
assert.notEqual(count('同一段文字', { includeNewline: true }), first);
assert.notEqual(count('另一段文字'), first);
assert.deepEqual(count('同一段文字'), countText('同一段文字'));
assert.equal(count('a\nb').characters, 2);
assert.equal(count('a\nb', { includeNewline: true }).characters, 3);
assert.equal(count('a\nb').characters, 2);
assert.equal(count('另一段文字').han, 5);
assert.equal(count('a\nb').han, 0);
});
Loading