repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

quickshell/nuguland/launch.test.js

Raw
File outline
const assert = require('assert');
const L = require('./launch.js');

// --- prefixIntent: each prefix routes + strips, returns {intent, query} ---
assert.deepStrictEqual(L.prefixIntent('=2+2'), { intent: 'calculator', query: '2+2' });
assert.deepStrictEqual(L.prefixIntent('>lock'), { intent: 'action', query: 'lock' });
assert.deepStrictEqual(L.prefixIntent('clip foo'), { intent: 'clipboard', query: 'foo' });
assert.deepStrictEqual(L.prefixIntent('firefox'), { intent: 'all', query: 'firefox' });
// the key is `intent`, NOT `provider` (contract rename)
assert.strictEqual(L.prefixIntent('=2+2').intent, 'calculator');
assert.strictEqual(L.prefixIntent('=2+2').provider, undefined);
assert.strictEqual(L.prefixIntent('CLIP bar').intent, 'clipboard'); // prefix is case-insensitive

// --- fuzzyScore: exact > prefix > word-boundary subsequence > loose subsequence > -1 ---
const exact = L.fuzzyScore('firefox', 'firefox', 0);          // 1000
const prefix = L.fuzzyScore('fire', 'firefox', 0);            // 800 - 7
const wbSub = L.fuzzyScore('fb', 'firefox browser', 0);       // acronym, high band
const loose = L.fuzzyScore('frfx', 'firefox', 0);             // scattered, low band
assert.ok(exact > prefix, 'exact > prefix');
assert.ok(prefix > wbSub, 'prefix > word-boundary subsequence');
assert.ok(wbSub > loose, 'word-boundary subsequence > loose subsequence');
assert.ok(loose >= 0, 'loose subsequence is a match (>= 0)');
// "frfx" finds "firefox"; "zzz" does not
assert.ok(L.fuzzyScore('frfx', 'firefox', 0) > 0, 'frfx matches firefox');
assert.strictEqual(L.fuzzyScore('zzz', 'firefox', 0), -1, 'zzz does not match firefox');
// empty query returns the bare boost (so every row lists)
assert.strictEqual(L.fuzzyScore('', 'anything', 900), 900);
// exact concrete value + provider boost adds in
assert.strictEqual(L.fuzzyScore('firefox', 'firefox', 900), 1900);

// --- calculatorValue: evaluates, rejects non-arithmetic, null on fail/non-finite ---
assert.strictEqual(L.calculatorValue('2+2'), '4');
assert.strictEqual(L.calculatorValue('2*21'), '42');
assert.strictEqual(L.calculatorValue(' 1 + 1 '), '2');   // whitespace stripped
assert.strictEqual(L.calculatorValue('1/2'), '0.5');     // non-integer formatting
assert.strictEqual(L.calculatorValue('a()'), null);      // letters rejected by guard
assert.strictEqual(L.calculatorValue('x=1'), null);      // assignment rejected by guard
assert.strictEqual(L.calculatorValue('1/0'), null);      // Infinity -> non-finite
assert.strictEqual(L.calculatorValue(''), null);         // empty -> null

// --- buildApps: sorts by title + carries the desktop icon name ---
const apps = L.buildApps([
    { id: 'gimp', name: 'GIMP', icon: 'gimp', comment: 'Image editor' },
    { id: 'firefox', name: 'Firefox', icon: 'firefox-icon', comment: 'Web browser' }
]);
assert.deepStrictEqual(apps.map(a => a.title), ['Firefox', 'GIMP']); // alpha sorted
assert.strictEqual(apps[0].icon, 'firefox-icon');                    // icon carried
assert.strictEqual(apps[1].icon, 'gimp');
assert.strictEqual(apps[0].id, 'app:firefox');
assert.strictEqual(apps[0].desktopPath, 'firefox.desktop');
assert.ok(L.buildApps([{ id: 'noname' }]).length === 0); // entries without name skipped

// --- rankResults: exact app title ranks to selectedIndex 0, icon preserved ---
const allRank = L.rankResults('Firefox', { apps: apps });
assert.strictEqual(allRank.providerIntent, 'all');
assert.strictEqual(allRank.selectedIndex, 0);
assert.strictEqual(allRank.results[0].title, 'Firefox');
assert.strictEqual(allRank.results[0].provider, 'app');
assert.strictEqual(allRank.results[0].icon, 'firefox-icon'); // icon survives shaping

// --- rankResults: `>` honors action intent ---
const actions = [
    { id: 'lock', title: 'Lock', subtitle: 'Lock this session', route: 'service' },
    { id: 'overview', title: 'Overview', subtitle: 'Open niri overview', route: 'service' }
];
const actionRank = L.rankResults('>', { apps: apps, actions: actions });
assert.strictEqual(actionRank.providerIntent, 'action');
assert.ok(actionRank.results.length >= 1);
assert.ok(actionRank.results.every(r => r.provider === 'action')); // no apps leak in
assert.strictEqual(actionRank.selectedIndex, 0);

// --- rankResults: `clip ` honors clipboard intent ---
const clipRows = [{ id: 'c1', title: 'hello clip', body: 'hello clip' }];
const clipRank = L.rankResults('clip hello', { apps: apps, clipRows: clipRows });
assert.strictEqual(clipRank.providerIntent, 'clipboard');
assert.ok(clipRank.results.length >= 1);
assert.ok(clipRank.results.every(r => r.provider === 'clipboard'));

// --- rankResults: `=2*21` emits a calc row with value 42 (string) ---
const calcRank = L.rankResults('=2*21', {});
assert.strictEqual(calcRank.providerIntent, 'calculator');
assert.ok(calcRank.calc, 'calc row present');
assert.strictEqual(calcRank.calc.value, '42');
assert.strictEqual(calcRank.results[0].provider, 'calculator');
assert.strictEqual(calcRank.results[0].title, '42');

// --- rankResults: empty query lists apps sorted, selectedIndex 0 ---
const emptyRank = L.rankResults('', { apps: apps });
assert.strictEqual(emptyRank.selectedIndex, 0);
assert.deepStrictEqual(emptyRank.results.map(r => r.title), ['Firefox', 'GIMP']);

console.log('launch.test.js: ALL PASS');