repositories / dotfiles
dotfiles
bugabingas dorkfiles
owned by admin
quickshell/nuguland/features/clipboard/clip.test.js
Rawconst assert = require('assert');
const C = require('./clip.js');
// --- detectKind: every branch, precedence-correct ---
assert.strictEqual(C.detectKind('https://example.com/page?x=1').kind, 'url');
assert.strictEqual(C.detectKind('#ff8800').kind, 'color');
assert.strictEqual(C.detectKind('#f80').kind, 'color'); // shorthand still a color
assert.strictEqual(C.detectKind('/tmp/screenshot-12.png').kind, 'image'); // image beats path
assert.strictEqual(C.detectKind('/home/u/file').kind, 'path'); // path, no image ext
assert.strictEqual(C.detectKind('just some prose here').kind, 'text');
// icons match the detected kind
assert.strictEqual(C.detectKind('#f80').icon, '\uF043');
assert.strictEqual(C.detectKind('https://x.io').icon, '\uF0C1');
assert.strictEqual(C.detectKind('/tmp/a.png').icon, '\uF03E');
// --- parseColor: shorthand expansion + alpha drop + rejection ---
assert.strictEqual(C.parseColor('#f80'), '#ff8800'); // #rgb -> #rrggbb
assert.strictEqual(C.parseColor('#ff8800'), '#ff8800'); // #rrggbb passthrough
assert.strictEqual(C.parseColor('#f808'), '#ff8800'); // #rgba -> drop alpha
assert.strictEqual(C.parseColor('#ff8800cc'), '#ff8800');// #rrggbbaa -> drop alpha
assert.strictEqual(C.parseColor('rgb(255, 136, 0)'), '#ff8800');
assert.strictEqual(C.parseColor('rgba(255,136,0,0.5)'), '#ff8800');
assert.strictEqual(C.parseColor('#f808'), '#ff8800'); // (4-digit body is #rgba, valid)
assert.strictEqual(C.parseColor('#ff888'), null); // 5-digit body -> invalid
assert.strictEqual(C.parseColor('#fffffff'), null); // length 7 -> invalid
assert.strictEqual(C.parseColor('hello'), null);
assert.strictEqual(C.parseColor('orange'), null);
// --- isImagePath ---
assert.strictEqual(C.isImagePath('/tmp/x.png'), true);
assert.strictEqual(C.isImagePath('/home/u/pic.JPEG'), true);
assert.strictEqual(C.isImagePath('/home/u/notes.txt'), false);
assert.strictEqual(C.isImagePath('not a path.png'), false); // has whitespace
// --- sortRows: pinned floats above a NEWER unpinned; newest-first within a group ---
const unsorted = [
{ id: 'a', pinned: false, createdAt: 100 },
{ id: 'b', pinned: true, createdAt: 50 }, // pinned but OLD
{ id: 'c', pinned: false, createdAt: 300 }, // newest unpinned
{ id: 'd', pinned: true, createdAt: 70 } // pinned, newer than b
];
const sorted = C.sortRows(unsorted);
assert.deepStrictEqual(sorted.map(r => r.id), ['d', 'b', 'c', 'a']);
// pinned ('d','b') precede the newer unpinned 'c'; within each group newest first
assert.strictEqual(sorted[0].id, 'd');
assert.strictEqual(sorted[1].id, 'b');
assert.strictEqual(C.sortRows(unsorted).length, 4); // does not drop rows
// --- filterRows: substring on title+body, ordered by sortRows afterwards ---
const pool = [
{ id: 'k1', title: 'hello world', body: '', pinned: false, createdAt: 10 },
{ id: 'k2', title: 'unrelated', body: 'say hello', pinned: false, createdAt: 40 }, // matches via body
{ id: 'k3', title: 'HELLO again', body: '', pinned: false, createdAt: 20 }, // case-insensitive
{ id: 'k4', title: 'nope', body: 'nothing', pinned: false, createdAt: 99 }
];
const hits = C.filterRows(pool, 'hello');
assert.deepStrictEqual(hits.map(r => r.id), ['k2', 'k3', 'k1']); // newest-first, k4 excluded
assert.strictEqual(C.filterRows(pool, '').length, 4); // empty query = all
// --- ageLabel: bucket boundaries ---
const now = 1000000000000;
assert.strictEqual(C.ageLabel(now, now), 'just now'); // 0s
assert.strictEqual(C.ageLabel(now - 90 * 1000, now), '1m'); // 90s -> 1m
assert.strictEqual(C.ageLabel(now - 2 * 3600 * 1000, now), '2h'); // 2h
assert.strictEqual(C.ageLabel(now - 3 * 86400 * 1000, now), '3d');// 3d
// --- isReadableText: rejects a binary string, accepts prose ---
let binary = '';
for (let i = 0; i < 64; i++) binary += String.fromCharCode(i % 7); // control bytes 0..6
assert.strictEqual(C.isReadableText(binary), false);
assert.strictEqual(C.isReadableText('a normal clipboard line'), true);
assert.strictEqual(C.isReadableText(''), false);
// --- cloneRow: IPC row shape + enriched view fields ---
const cr = C.cloneRow({ id: 'x', body: '#f80', pinned: true, createdAt: now }, now);
['id', 'title', 'body', 'pinned', 'provider', 'route'].forEach(function (k) {
assert.ok(cr.hasOwnProperty(k), 'cloneRow missing IPC key: ' + k);
});
assert.strictEqual(cr.provider, 'clipboard');
assert.strictEqual(cr.route, 'clipboard');
assert.strictEqual(cr.kind, 'color');
assert.strictEqual(cr.swatch, '#ff8800');
assert.strictEqual(cr.isImage, false);
assert.strictEqual(cr.imagePath, null);
assert.strictEqual(cr.age, 'just now');
// image row: title forced to "image", imagePath populated, swatch null
const ci = C.cloneRow({ id: 'y', body: '/tmp/a.png', createdAt: now }, now);
assert.strictEqual(ci.title, 'image');
assert.strictEqual(ci.isImage, true);
assert.strictEqual(ci.imagePath, '/tmp/a.png');
assert.strictEqual(ci.swatch, null);
// --- persistedRow: durable storage shape ---
const pr = C.persistedRow({ id: 'z', title: 't', body: 'b', pinned: true, createdAt: 5 });
assert.deepStrictEqual(pr, { id: 'z', title: 't', body: 'b', pinned: true, createdAt: 5 });
// --- titles the panel shows (human.js over cloneRow): never a machine identifier ---
const H = require('../../human.js');
const shown = body => {
const row = C.cloneRow({ id: 'h', body: body, createdAt: now }, now);
return H.text(row.title, 'copied ' + row.kind);
};
const piImage = '/tmp/pi-clipboard-23965d8a-47d6-45ce-9d1a-f83c6b805220.png';
assert.strictEqual(shown(piImage), 'image'); // the temp file may be long gone
assert.strictEqual(shown('file://' + piImage), 'image');
assert.strictEqual(shown(piImage + '\nwhat is in this picture?'), 'image'); // first line is the path
assert.strictEqual(shown(piImage + ' what is in this picture?'), 'image what is in this picture?');
assert.strictEqual(shown('file:///tmp/a.png\r\nfile:///tmp/b.png\r\n'), 'a.png'); // uri-list: its first file
assert.strictEqual(shown('file://' + piImage + '\r\nfile:///tmp/b.png\r\n'), 'image');
assert.strictEqual(shown('file:///home/u/Documents/report.pdf'), 'report.pdf');
assert.strictEqual(shown('23965d8a-47d6-45ce-9d1a-f83c6b805220'), 'copied text');
assert.strictEqual(shown('/home/u/notes.txt'), '/home/u/notes.txt'); // a user path stays
assert.strictEqual(shown('just some prose'), 'just some prose');
assert.strictEqual(shown(''), 'clipboard');
// Text must never be passed in process arguments, including other copy surfaces.
const fs = require('fs');
const path = require('path');
for (const file of ['Clipboard.qml', '../notifications/Notifications.qml', '../noko/Noko.qml', '../noko/NokoPanel.qml']) {
const source = fs.readFileSync(path.join(__dirname, file), 'utf8');
assert.doesNotMatch(source, /execDetached\s*\(\s*\[\s*["'](?:\/usr\/bin\/)?(?:wl-copy|wtype)["']/);
for (const match of source.matchAll(/\[\s*["'](?:\/usr\/bin\/)?(?:wl-copy|wtype)["']([^\]]*)\]/g))
assert.match(match[1], /^\s*(?:,\s*["']-["']\s*)?$/, file);
}
const clipboardSource = fs.readFileSync(path.join(__dirname, 'Clipboard.qml'), 'utf8');
assert.doesNotMatch(clipboardSource, /["']focused-window["']/);
console.log('clip.test.js: ALL PASS');