import test from 'node:test'; import assert from 'node:assert/strict'; // Normalization function matching cardManagerService.ts function normalizeName(name) { return name.toLowerCase().replace(/\s*-\s*method.*$/i, '').replace(/[^a-z0-9]+/g, ''); } // Logic for computing card status matching CardManager UI function getCardStatus(card) { if (!card.is_enrolled || card.fsrs_state === null) return 'unenrolled'; if (card.is_due) return 'due'; if (card.fsrs_state === 1) return 'learning'; if (card.fsrs_state === 2) return 'reviewing'; if (card.fsrs_state === 3) return 'relearning'; return 'new'; } function mergeCatalogAndExplorer(catalog, explorerProblems, now = new Date()) { const explorerByNum = new Map(); const explorerByName = new Map(); const matchedExplorerIds = new Set(); for (const p of explorerProblems) { if (p.problem_number !== undefined && p.problem_number !== null) { explorerByNum.set(p.problem_number, p); } explorerByName.set(normalizeName(p.name), p); } const managedCards = []; // 1. Process curated catalog for (const cat of catalog) { const matched = explorerByNum.get(cat.problem_number) || explorerByName.get(normalizeName(cat.name)); if (matched) { matchedExplorerIds.add(matched.id); const isEnrolled = (matched.next_review !== null && matched.next_review !== undefined) || (matched.fsrs_state !== null && matched.fsrs_state !== undefined); const nextReviewDate = matched.next_review ? new Date(matched.next_review) : null; const isDue = isEnrolled && nextReviewDate !== null ? nextReviewDate <= now : false; managedCards.push({ id: matched.id, problem_number: cat.problem_number, name: cat.name, pattern: cat.pattern, difficulty: cat.difficulty, is_enrolled: isEnrolled, is_custom: false, fsrs_state: matched.fsrs_state ?? (isEnrolled ? 0 : null), fsrs_step: matched.fsrs_step ?? 0, fsrs_stability: matched.fsrs_stability ?? 0.0, fsrs_difficulty: matched.fsrs_difficulty ?? 0.0, fsrs_elapsed_days: matched.fsrs_elapsed_days ?? 0, fsrs_scheduled_days: matched.fsrs_scheduled_days ?? 0, fsrs_reps: matched.fsrs_reps ?? 0, fsrs_lapses: matched.fsrs_lapses ?? 0, next_review: matched.next_review ?? null, times_correct: matched.times_correct ?? 0, total_attempts: matched.total_attempts ?? 0, is_due: isDue }); } else { managedCards.push({ id: cat.id, problem_number: cat.problem_number, name: cat.name, pattern: cat.pattern, difficulty: cat.difficulty, is_enrolled: false, is_custom: false, fsrs_state: null, fsrs_step: 0, fsrs_stability: 0.0, fsrs_difficulty: 0.0, fsrs_elapsed_days: 0, fsrs_scheduled_days: 0, fsrs_reps: 0, fsrs_lapses: 0, next_review: null, times_correct: 0, total_attempts: 0, is_due: false }); } } // 2. Process custom problems for (const exp of explorerProblems) { if (!matchedExplorerIds.has(exp.id)) { const isEnrolled = (exp.next_review !== null && exp.next_review !== undefined) || (exp.fsrs_state !== null && exp.fsrs_state !== undefined); const nextReviewDate = exp.next_review ? new Date(exp.next_review) : null; const isDue = isEnrolled && nextReviewDate !== null ? nextReviewDate <= now : false; managedCards.push({ id: exp.id, problem_number: exp.problem_number, name: exp.name, pattern: exp.pattern || 'General', difficulty: exp.difficulty || 'Medium', is_enrolled: isEnrolled, is_custom: true, fsrs_state: exp.fsrs_state ?? (isEnrolled ? 0 : null), fsrs_step: exp.fsrs_step ?? 0, fsrs_stability: exp.fsrs_stability ?? 0.0, fsrs_difficulty: exp.fsrs_difficulty ?? 0.0, fsrs_elapsed_days: exp.fsrs_elapsed_days ?? 0, fsrs_scheduled_days: exp.fsrs_scheduled_days ?? 0, fsrs_reps: exp.fsrs_reps ?? 0, fsrs_lapses: exp.fsrs_lapses ?? 0, next_review: exp.next_review ?? null, times_correct: exp.times_correct ?? 0, total_attempts: exp.total_attempts ?? 0, is_due: isDue }); } } return managedCards; } test('1. normalizeName handles variations, suffixes, and punctuation consistently', () => { assert.strictEqual(normalizeName('Two Sum'), 'twosum'); assert.strictEqual(normalizeName('Two Sum - Method 1'), 'twosum'); assert.strictEqual(normalizeName('Valid Palindrome II'), 'validpalindromeii'); assert.strictEqual(normalizeName('Course Schedule III - Method B'), 'coursescheduleiii'); assert.strictEqual(normalizeName('3Sum Closest'), '3sumclosest'); }); test('2. Unenrolled catalog cards are correctly populated with null fsrs_state and false due state', () => { const catalog = [ { id: 'cat-1', problem_number: 1, name: 'Two Sum', pattern: 'Hash Map', difficulty: 'Easy' }, { id: 'cat-2', problem_number: 2, name: 'Add Two Numbers', pattern: 'Linked List', difficulty: 'Medium' } ]; const explorerProblems = []; const cards = mergeCatalogAndExplorer(catalog, explorerProblems); assert.strictEqual(cards.length, 2); assert.strictEqual(cards[0].is_enrolled, false); assert.strictEqual(cards[0].fsrs_state, null); assert.strictEqual(cards[0].fsrs_stability, 0.0); assert.strictEqual(cards[0].fsrs_difficulty, 0.0); assert.strictEqual(cards[0].is_due, false); assert.strictEqual(cards[0].is_custom, false); assert.strictEqual(getCardStatus(cards[0]), 'unenrolled'); }); test('3. Enrolled catalog cards match by number or name and correctly calculate due state and FSRS decks', () => { const catalog = [ { id: 'cat-1', problem_number: 1, name: 'Two Sum', pattern: 'Hash Map', difficulty: 'Easy' }, { id: 'cat-2', problem_number: 2, name: 'Add Two Numbers', pattern: 'Linked List', difficulty: 'Medium' }, { id: 'cat-3', problem_number: 3, name: 'LRU Cache', pattern: 'Linked List', difficulty: 'Medium' } ]; const now = new Date('2026-10-13T12:00:00Z'); const past = new Date('2026-10-12T12:00:00Z').toISOString(); const future = new Date('2026-10-20T12:00:00Z').toISOString(); const explorerProblems = [ { id: 'exp-1', problem_number: 1, name: 'Two Sum', fsrs_state: 1, // Learning fsrs_stability: 1.5, fsrs_difficulty: 5.0, next_review: past, times_correct: 3, total_attempts: 4 }, { id: 'exp-2', name: 'Add Two Numbers - Method 1', fsrs_state: 2, // Review fsrs_stability: 4.0, fsrs_difficulty: 4.5, next_review: future, times_correct: 7, total_attempts: 8 }, { id: 'exp-3', problem_number: 3, name: 'LRU Cache', fsrs_state: 3, // Relearning fsrs_stability: 0.8, fsrs_difficulty: 7.0, next_review: future, times_correct: 10, total_attempts: 10 } ]; const cards = mergeCatalogAndExplorer(catalog, explorerProblems, now); assert.strictEqual(cards.length, 3); // Card 1: Due in Learning state (state 1) assert.strictEqual(cards[0].is_enrolled, true); assert.strictEqual(cards[0].is_due, true); assert.strictEqual(cards[0].fsrs_state, 1); assert.strictEqual(getCardStatus(cards[0]), 'due'); // Card 2: Future review in Review state (state 2) -> 'reviewing' assert.strictEqual(cards[1].is_enrolled, true); assert.strictEqual(cards[1].is_due, false); assert.strictEqual(cards[1].fsrs_state, 2); assert.strictEqual(getCardStatus(cards[1]), 'reviewing'); // Card 3: Future review in Relearning state (state 3) -> 'relearning' assert.strictEqual(cards[2].is_enrolled, true); assert.strictEqual(cards[2].is_due, false); assert.strictEqual(cards[2].fsrs_state, 3); assert.strictEqual(getCardStatus(cards[2]), 'relearning'); // Card in Learning state not due -> 'learning' const learningNotDueCard = { ...cards[0], is_due: false }; assert.strictEqual(getCardStatus(learningNotDueCard), 'learning'); // Card in New state (state 0) enrolled not due -> 'new' const newEnrolledCard = { ...cards[0], is_due: false, fsrs_state: 0 }; assert.strictEqual(getCardStatus(newEnrolledCard), 'new'); }); test('4. Custom problems created by users are preserved with is_custom: true', () => { const catalog = [ { id: 'cat-1', problem_number: 1, name: 'Two Sum', pattern: 'Hash Map', difficulty: 'Easy' } ]; const explorerProblems = [ { id: 'custom-99', name: 'Custom Graph Shortest Path', pattern: 'Graphs', difficulty: 'Hard', fsrs_state: 0, fsrs_stability: 0.0, fsrs_difficulty: 0.0, next_review: new Date('2026-10-13T10:00:00Z').toISOString(), times_correct: 1, total_attempts: 1 } ]; const cards = mergeCatalogAndExplorer(catalog, explorerProblems, new Date('2026-10-13T12:00:00Z')); assert.strictEqual(cards.length, 2); const customCard = cards.find(c => c.id === 'custom-99'); assert.ok(customCard); assert.strictEqual(customCard.is_custom, true); assert.strictEqual(customCard.pattern, 'Graphs'); assert.strictEqual(customCard.difficulty, 'Hard'); assert.strictEqual(customCard.fsrs_state, 0); assert.strictEqual(customCard.is_due, true); }); test('5. Offline resilience: empty explorer problems list results in zero throw and all catalog available', () => { const catalog = Array.from({ length: 150 }, (_, i) => ({ id: `cat-${i + 1}`, problem_number: i + 1, name: `Problem ${i + 1}`, pattern: 'Dynamic Programming', difficulty: 'Medium' })); assert.doesNotThrow(() => { const cards = mergeCatalogAndExplorer(catalog, []); assert.strictEqual(cards.length, 150); assert.strictEqual(cards.every(c => !c.is_enrolled), true); }); }); test('6. parseJsonSafe correctly parses stringified JSON structures without crashing', () => { function parseJsonSafe(val) { if (typeof val === 'string') { try { return JSON.parse(val); } catch { return undefined; } } return val ?? undefined; } assert.deepStrictEqual(parseJsonSafe('["1 <= nums.length <= 10^5"]'), ["1 <= nums.length <= 10^5"]); assert.deepStrictEqual(parseJsonSafe('[{"input":"[1,2]","output":"true"}]'), [{"input":"[1,2]","output":"true"}]); assert.strictEqual(parseJsonSafe('invalid json {['), undefined); assert.deepStrictEqual(parseJsonSafe(['already an array']), ['already an array']); assert.strictEqual(parseJsonSafe(null), undefined); }); test('7. Filtered multi-select logic avoids size-equality false positives', () => { const sortedCards = [{ id: 'card-b' }]; const selectedCardIds = new Set(['card-a']); // Buggy check was: selectedCardIds.size === sortedCards.length (1 === 1 -> true!) const isBuggySelected = selectedCardIds.size === sortedCards.length && sortedCards.length > 0; assert.strictEqual(isBuggySelected, true); // Demonstrates the prior bug! // Correct check: const isAllFilteredSelected = sortedCards.length > 0 && sortedCards.every(c => selectedCardIds.has(c.id)); assert.strictEqual(isAllFilteredSelected, false); // Correct behavior! }); test('8. Filter interaction between is_due and fsrs_state decks', () => { const testCards = [ { id: 'c1', is_enrolled: true, is_due: true, fsrs_state: 1 }, // Due in Learning { id: 'c2', is_enrolled: true, is_due: false, fsrs_state: 1 }, // Not due in Learning { id: 'c3', is_enrolled: true, is_due: true, fsrs_state: 2 }, // Due in Review { id: 'c4', is_enrolled: true, is_due: false, fsrs_state: 2 }, // Not due in Review { id: 'c5', is_enrolled: true, is_due: true, fsrs_state: 3 }, // Due in Relearning { id: 'c6', is_enrolled: false, is_due: false, fsrs_state: null } // Unenrolled ]; function filterByDeck(cards, selectedDeck) { return cards.filter(card => { if (selectedDeck === 'due' && !card.is_due) return false; if (selectedDeck === 'enrolled' && !card.is_enrolled) return false; if (selectedDeck === 'new' && card.is_enrolled) return false; if (selectedDeck === 'learning' && (!card.is_enrolled || card.fsrs_state !== 1)) return false; if (selectedDeck === 'reviewing' && (!card.is_enrolled || card.fsrs_state !== 2)) return false; if (selectedDeck === 'relearning' && (!card.is_enrolled || card.fsrs_state !== 3)) return false; return true; }); } // 'due' deck returns all due enrolled cards regardless of state const dueDeck = filterByDeck(testCards, 'due'); assert.deepStrictEqual(dueDeck.map(c => c.id), ['c1', 'c3', 'c5']); // 'learning' deck returns both due and non-due learning cards const learningDeck = filterByDeck(testCards, 'learning'); assert.deepStrictEqual(learningDeck.map(c => c.id), ['c1', 'c2']); // 'reviewing' deck returns both due and non-due review cards const reviewDeck = filterByDeck(testCards, 'reviewing'); assert.deepStrictEqual(reviewDeck.map(c => c.id), ['c3', 'c4']); // 'relearning' deck returns relearning cards const relearningDeck = filterByDeck(testCards, 'relearning'); assert.deepStrictEqual(relearningDeck.map(c => c.id), ['c5']); // 'new' deck returns only unenrolled cards const newDeck = filterByDeck(testCards, 'new'); assert.deepStrictEqual(newDeck.map(c => c.id), ['c6']); });