Files
hyprcrd/public/plugins/tests/state-functions.test.js
T
thandClaude Opus 4.6 fe3399a44b tests: in-run progress strip + cache-bust runner.js across files
* runner.js: shows a 30px fixed-top progress strip during runs (label
  "test x/total:", current suite/test name with ellipsis on the left,
  fixed 50%-wide progress fill on the right that turns danger-red on
  the first failure). The DOM node is created in loader.js so it appears
  immediately on shortcut press; runner.js only updates it. The fill
  width is fixed at flex:0 0 50% so it doesn't jiggle as the test name
  changes length. Suites now live on window._testSuites as a defensive
  fallback so two runner instances stay in sync.

* Cache invalidation: every test file's `from './runner.js'` is now
  `from './runner.js?v=2'`, and loader.js imports the runner with the
  same RUNNER_V suffix. Same URL → one module instance → suite
  registration and runAll see the same array. Bumping RUNNER_V (in
  loader.js + every test file in lock-step) is the way to force a
  re-fetch when runner.js changes.

* loader.js: ensureProgressBar() draws the strip via setProperty(...,
  'important') with z-index 2147483647 attached to documentElement so
  nothing in user CSS, scanline overlay (z 9999) or modals can occlude
  it. Auto-run path (?test=1) also pre-renders the strip.

chore: bump version to 0.9.1

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-11 20:19:22 +02:00

115 lines
4.1 KiB
JavaScript

import { describe, it, expect } from './runner.js?v=2';
import { state, parseTags, sortedCards, getChildren, getGroupCards, getRecent, markRecent, getFavorites, toggleFavorite, isFavorite } from '../../js/modules/state.js';
describe('State: parseTags', () => {
it('splits comma-separated tags', () => {
expect(parseTags({ tags: 'alpha,beta,gamma' })).toEqual(['alpha', 'beta', 'gamma']);
});
it('handles empty tags', () => {
expect(parseTags({ tags: '' })).toEqual([]);
expect(parseTags({ tags: null })).toEqual([]);
expect(parseTags({})).toEqual([]);
});
it('handles single tag', () => {
expect(parseTags({ tags: 'solo' })).toEqual(['solo']);
});
it('handles hierarchical tags', () => {
expect(parseTags({ tags: 'parent/child,other' })).toEqual(['parent/child', 'other']);
});
});
describe('State: getChildren / getGroupCards', () => {
it('getChildren returns child cards', () => {
const parent = state.cards.find(c => {
return state.cards.some(ch => ch.parent_id == c.id);
});
if (!parent) return; // no groups
const children = getChildren(parent.id);
expect(children.length).toBeGreaterThan(0);
for (const ch of children) expect(ch.parent_id).toBe(parent.id);
});
it('getChildren returns empty for non-parent', () => {
const orphan = state.cards.find(c => !c.parent_id && !state.cards.some(ch => ch.parent_id == c.id));
if (!orphan) return;
expect(getChildren(orphan.id).length).toBe(0);
});
it('getGroupCards returns parent + children', () => {
const parent = state.cards.find(c => state.cards.some(ch => ch.parent_id == c.id));
if (!parent) return;
const group = getGroupCards(parent);
expect(group.length).toBeGreaterThan(1);
expect(group[0].id).toBe(parent.id);
});
it('getGroupCards returns null for solo card', () => {
const solo = state.cards.find(c => !c.parent_id && !state.cards.some(ch => ch.parent_id == c.id));
if (!solo) return;
expect(getGroupCards(solo)).toBe(null);
});
});
describe('State: Recent Cards', () => {
it('markRecent adds card to recent list', () => {
const id = 99999;
markRecent(id);
expect(getRecent()).toContain(String(id));
// Clean up
const r = getRecent().filter(i => i != id);
localStorage.setItem('hyprcrd-recent', JSON.stringify(r));
});
it('markRecent puts card at front', () => {
markRecent(88881);
markRecent(88882);
const r = getRecent();
expect(r[0]).toBe('88882');
expect(r[1]).toBe('88881');
// Clean up
const cleaned = r.filter(i => i != '88881' && i != '88882');
localStorage.setItem('hyprcrd-recent', JSON.stringify(cleaned));
});
it('markRecent deduplicates', () => {
markRecent(77771);
markRecent(77772);
markRecent(77771);
const r = getRecent();
const count = r.filter(i => i == '77771').length;
expect(count).toBe(1);
expect(r[0]).toBe('77771');
const cleaned = r.filter(i => i != '77771' && i != '77772');
localStorage.setItem('hyprcrd-recent', JSON.stringify(cleaned));
});
it('markRecent caps at 10', () => {
for (let i = 0; i < 15; i++) markRecent(60000 + i);
expect(getRecent().length).toBeLessThan(12);
const cleaned = getRecent().filter(i => parseInt(i) < 60000 || parseInt(i) > 60020);
localStorage.setItem('hyprcrd-recent', JSON.stringify(cleaned));
});
});
describe('State: Favorites', () => {
it('toggleFavorite adds and removes', () => {
const id = 55555;
const wasFav = isFavorite(id);
toggleFavorite(id);
expect(isFavorite(id)).toBe(!wasFav);
toggleFavorite(id);
expect(isFavorite(id)).toBe(wasFav);
});
it('isFavorite returns boolean', () => {
expect(typeof isFavorite(1)).toBe('boolean');
});
it('getFavorites returns array', () => {
expect(Array.isArray(getFavorites())).toBe(true);
});
});