Files
hyprcrd/public/plugins/tests/state-functions.test.js
T
2026-04-10 07:57:53 +02:00

115 lines
4.1 KiB
JavaScript

import { describe, it, expect } from './runner.js';
import { state, parseTags, sortedCards, getChildren, getGroupCards, getRecent, markRecent, getFavorites, toggleFavorite, isFavorite } from '../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);
});
});