webtools/site/queued-pastes/queue.test.mjs
Leon Mika de63c48da2
Some checks failed
/ publish (push) Failing after 1m21s
Added Queued Pastes
2026-09-16 21:52:03 +10:00

100 lines
3.4 KiB
JavaScript

import { test } from 'node:test';
import assert from 'node:assert/strict';
import { firstLineRange, currentLine, advance, copyText, adjustCopiedLine, trimTrailing } from './queue.mjs';
test('currentLine returns the first line without its newline', () => {
assert.equal(currentLine('abc\ndef'), 'abc');
});
test('currentLine returns the whole string when there is no newline', () => {
assert.equal(currentLine('abc'), 'abc');
});
test('currentLine of an empty string is empty', () => {
assert.equal(currentLine(''), '');
});
test('currentLine with a leading newline is an empty string', () => {
assert.equal(currentLine('\ndef'), '');
});
test('firstLineRange covers the first line up to but not including the newline', () => {
assert.deepEqual(firstLineRange('abc\ndef'), { start: 0, end: 3 });
});
test('firstLineRange covers the whole string when there is no newline', () => {
assert.deepEqual(firstLineRange('abc'), { start: 0, end: 3 });
});
test('firstLineRange of an empty string is a zero-width range', () => {
assert.deepEqual(firstLineRange(''), { start: 0, end: 0 });
});
test('advance removes the first line and its newline', () => {
assert.equal(advance('abc\ndef'), 'def');
});
test('advance on a single line without a trailing newline empties the text', () => {
assert.equal(advance('abc'), '');
});
test('advance on text ending with a newline leaves no phantom line', () => {
assert.equal(advance('abc\n'), '');
});
test('advance removes a leading empty line', () => {
assert.equal(advance('\n\n'), '\n');
});
test('advance on an empty string stays empty', () => {
assert.equal(advance(''), '');
});
test('copyText returns the first line when new-line is off', () => {
assert.equal(copyText('abc\ndef', false), 'abc');
});
test('copyText appends a newline when new-line is on', () => {
assert.equal(copyText('abc\ndef', true), 'abc\n');
});
test('copyText appends a newline even without a trailing newline in the text', () => {
assert.equal(copyText('abc', true), 'abc\n');
});
test('adjustCopiedLine appends a newline when enabling and the clipboard matches the line', () => {
assert.equal(adjustCopiedLine('abc', 'abc', true), 'abc\n');
});
test('adjustCopiedLine removes the newline when disabling and the clipboard matches line plus newline', () => {
assert.equal(adjustCopiedLine('abc\n', 'abc', false), 'abc');
});
test('adjustCopiedLine is a no-op result when the clipboard already has the desired form', () => {
assert.equal(adjustCopiedLine('abc\n', 'abc', true), 'abc\n');
assert.equal(adjustCopiedLine('abc', 'abc', false), 'abc');
});
test('adjustCopiedLine returns null when the clipboard holds unrelated text', () => {
assert.equal(adjustCopiedLine('xyz', 'abc', true), null);
});
test('adjustCopiedLine returns null when the clipboard has the line plus more than one newline', () => {
assert.equal(adjustCopiedLine('abc\n\n', 'abc', true), null);
});
test('trimTrailing removes trailing spaces, tabs, and newlines', () => {
assert.equal(trimTrailing('line1\nline2\n\n \t'), 'line1\nline2');
});
test('trimTrailing leaves per-line trailing spaces intact', () => {
assert.equal(trimTrailing('line1 \nline2'), 'line1 \nline2');
});
test('trimTrailing on text without trailing whitespace is unchanged', () => {
assert.equal(trimTrailing('abc'), 'abc');
});
test('trimTrailing on whitespace-only text is empty', () => {
assert.equal(trimTrailing(' \n \n'), '');
});