Luigit
repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by admin

quickshell/nuguland/human.test.js

Raw
const assert = require('assert');
const H = require('./human.js');

for (const [text, short] of Object.entries({ notify: 'ntf', code: 'cod', media: 'med', system: 'sys', audio: 'aud', power: 'pwr', idle: 'idl', tray: 'try', '': '', a: 'a', ae: 'ae', AEIOU: 'AEI', NOTIFY: 'NTF' }))
    assert.equal(H.abbreviate(text), short, text);

// Machine identifiers: each is recognised whole and found inside a sentence.
const machine = {
    '/tmp/pi-clipboard-23965d8a-47d6-45ce-9d1a-f83c6b805220.png': 'temp path',
    '/var/tmp/report.pdf': 'temp path',
    '/run/user/1000/wayland-1': 'temp path',
    '/dev/shm/pulse-shm-1234': 'temp path',
    'file:///home/me/Pictures/holiday.jpg': 'file uri',
    'FILE:///tmp/x.png': 'file uri',
    '/dev/input/event3': 'device path',
    '/sys/class/backlight/intel_backlight': 'device path',
    '/org/freedesktop/NetworkManager/Devices/2': 'object path',
    '/org/ayatana/NotificationItem/nm_applet': 'object path',
    ':1.234': 'bus name',
    'org.mpris.MediaPlayer2.firefox.instance_1234': 'bus name',
    'org.gnome.Nautilus': 'bus name',
    'com.mitchellh.ghostty': 'bus name',
    'firefox.desktop': 'bus name',
    'org.kde.StatusNotifierItem-1234-1': 'bus name',
    'StatusNotifierItem': 'bus name',
    'alsa_output.pci-0000_00_1f.3.analog-stereo': 'audio node',
    'alsa_input.usb-Generic_USB_Audio-00.mono-fallback': 'audio node',
    'bluez_output.AA_BB_CC_DD_EE_FF.1': 'audio node',
    'AA:BB:CC:DD:EE:FF': 'hardware address',
    '23965d8a-47d6-45ce-9d1a-f83c6b805220': 'uuid',
    'pi-clipboard-23965d8a-47d6-45ce-9d1a-f83c6b805220': 'uuid',
    'd41d8cd98f00b204e9800998ecf8427e': 'hash',
    '3f2a9c1b7e4d': 'hash',
    'aGVsbG8gd29ybGQgMTIzNDU2Nzg5MA==': 'hash',
    'screenshot-1a2b3c4d.png': 'generated name',
    'nuguland-ui-test-AbC12x': 'generated name',
    'tmp.Xy7Qa2bLm0': 'generated name',
    'chrome_status_icon_1': 'generated name',
    'instance_42': 'generated name',
};
for (const [value, kind] of Object.entries(machine)) {
    assert.strictEqual(H.machineKind(value), kind, value);
    assert.strictEqual(H.isMachineIdentifier(value), true, value);
    assert.strictEqual(H.findMachineIdentifier(`opened ${value}, then left`), value, `inside a sentence: ${value}`);
}

// Human text stays human: user files, words, names, numbers and urls.
for (const value of [
    'report.pdf', '/home/me/Documents/report.pdf', '/etc/hosts', '~/notes.md', 'Screenshot from 2026-11-14 10-11-12.png',
    'firefox', 'wezterm', 'nm-applet', 'Spotify', 'fixture headphones', 'Oliver\'s iPhone15', 'Galaxy Buds2 Pro',
    'Built-in Audio Analog Stereo', 'HDMI / DisplayPort 2 Output', 'github.com', 'www.example.com', 'https://example.com/a?b=1',
    '#ff8800', '72%', '12:30', '6.12.0', '192.168.1.10', '2026-11-14', '1731577200000', 'deadbeef', 'x86_64', 'v2.0.1-rc1',
    'cafe1234', 'MediaPlayer', 'needle document', 'NetworkManager.service', 'systemd-coredump@0-1234-5678.service', 'screenshot not taken · no save path given', 'ü 日本語 emoji 🎉',
    '', '   ',
]) {
    assert.strictEqual(H.machineKind(value.trim()), '', value);
    assert.strictEqual(H.findMachineIdentifier(value), '', value);
    assert.strictEqual(H.label(value, 'item'), value.trim() || 'item', value);
    assert.strictEqual(H.text(value), value, value);
}

// Labels: what an identifier stands for, else the caller's kind.
const labels = [
    ['/tmp/pi-clipboard-23965d8a-47d6-45ce-9d1a-f83c6b805220.png', 'clipboard item', 'image'],
    ['file:///tmp/pi-clipboard-23965d8a-47d6-45ce-9d1a-f83c6b805220.png', 'clipboard item', 'image'],
    ['/tmp/report.pdf', 'file', 'report.pdf'],
    ['file:///home/me/My%20Report.pdf', 'file', 'My Report.pdf'],
    ['file:///home/me/bad%E0%A4%A.pdf', 'file', 'bad%E0%A4%A.pdf'],
    ['/tmp/Screenshot from 2026-11-14 10-11-12.png', 'file', 'Screenshot from 2026-11-14 10-11-12.png'],
    ['/run/user/1000/screenshot-1a2b3c4d.png', 'file', 'screenshot'],
    ['/tmp/tmp.Xy7Qa2bLm0', 'file', 'file'],
    ['/tmp/d41d8cd98f00b204e9800998ecf8427e.mp4', 'file', 'video'],
    ['/tmp/', 'file', 'file'],
    ['/tmp', 'file', 'file'],
    ['/dev/input/event3', 'device', 'device'],
    ['/org/freedesktop/NetworkManager/Devices/2', 'device', 'device'],
    ['org.mpris.MediaPlayer2.firefox.instance_1234', 'player', 'firefox'],
    ['org.mpris.MediaPlayer2.spotify', 'player', 'spotify'],
    ['org.gnome.Nautilus', 'app', 'nautilus'],
    ['org.kde.kdeconnect-indicator', 'app', 'kdeconnect indicator'],
    ['org.wezfurlong.wezterm.desktop', 'app', 'wezterm'],
    ['firefox.desktop', 'app', 'firefox'],
    ['org.kde.StatusNotifierItem-1234-1', 'app', 'app'],
    [':1.234', 'app', 'app'],
    ['alsa_output.pci-0000_00_1f.3.analog-stereo', 'output device', 'output device'],
    ['AA:BB:CC:DD:EE:FF', 'device', 'device'],
    ['chrome_status_icon_1', 'app', 'app'],
    ['23965d8a-47d6-45ce-9d1a-f83c6b805220', 'item', 'item'],
    ['app-org.wezfurlong.wezterm@e7f0a3c1b2d44f2a9c8b7d6e5f4a3b2c.service', 'app', 'wezterm'],
    ['app-firefox@e7f0a3c1b2d44f2a9c8b7d6e5f4a3b2c.service', 'app', 'firefox'],
    ['app-niri-firefox-a1b2c3d4.scope', 'app', 'niri-firefox'],
    ['/user.slice/user-1000.slice/user@1000.service/app.slice/app-org.gnome.Nautilus@9f86d081884c7d659a2feaa0c55ad015.service', 'app', 'nautilus'],
    [null, 'app', 'app'],
    [undefined, undefined, 'item'],
];
for (const [value, fallback, expected] of labels) {
    const result = H.label(value, fallback);
    assert.strictEqual(result, expected, `${value} -> ${result}`);
    assert.strictEqual(H.findMachineIdentifier(result), '', `label stays human: ${value}`);
}

// Pick: the first human candidate, else what an identifier stands for, else the kind.
assert.strictEqual(H.pick(['', 'alsa_output.pci-0000_00_1f.3.analog-stereo', 'Speakers'], 'output'), 'Speakers');
assert.strictEqual(H.pick(['org.mpris.MediaPlayer2.firefox.instance_1234', ''], 'player'), 'firefox');
assert.strictEqual(H.pick(['alsa_output.pci-0000_00_1f.3.analog-stereo', null], 'output device'), 'output device');
assert.strictEqual(H.pick([], 'app'), 'app');
assert.strictEqual(H.pick(['  fixture  ']), 'fixture');

// Text: each identifier inside an authored or derived line becomes its label.
assert.strictEqual(H.text('/tmp/pi-clipboard-23965d8a-47d6-45ce-9d1a-f83c6b805220.png what is this?', 'text'), 'image what is this?');
assert.strictEqual(H.text('select org.mpris.MediaPlayer2.firefox.instance_1234'), 'select firefox');
assert.strictEqual(H.text('pasting into (org.wezfurlong.wezterm).'), 'pasting into (wezterm).');
assert.strictEqual(H.text('saved to "/tmp/report.pdf"'), 'saved to "report.pdf"');
assert.strictEqual(H.text('check its folder\n/home/me/Pictures/shot.png'), 'check its folder\n/home/me/Pictures/shot.png');
assert.strictEqual(H.text('check its folder\n/tmp/9f86d081884c7d659a2feaa0c55ad015.png'), 'check its folder\nimage');
assert.strictEqual(H.text('file:///tmp/a%20b.png'), 'a b.png');
assert.strictEqual(H.text(null), '');
for (const [value] of labels) assert.strictEqual(H.findMachineIdentifier(H.text(`about ${value} now`, 'item')), '', `text stays human: ${value}`);

// File kinds by name.
assert.deepStrictEqual(['a.PNG', 'b.webm', 'c.flac', 'd.pdf', 'Screen_Shot.png', 'e.bin', ''].map(H.fileKind), ['image', 'video', 'audio', 'document', 'screenshot', 'file', 'file']);

console.log('human: machine identifiers never become labels');