|
| 1 | +/** |
| 2 | + * Bedtime review shared logic tests |
| 3 | + * B2 (F3): due-card stats + mini-review target selection |
| 4 | + * @ai-context 睡前复习共享查询测试:到期卡过滤、牌组目标选择、失败静默降级; |
| 5 | + * 存储层全 Mock,绝不触碰真实 IndexedDB。 |
| 6 | + */ |
| 7 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 8 | + |
| 9 | +const mocks = vi.hoisted(() => ({ |
| 10 | + flashcardStore: { getTable: vi.fn() }, |
| 11 | + flashcardDeckStore: { getTable: vi.fn() }, |
| 12 | +})); |
| 13 | + |
| 14 | +vi.mock('@/lib/storage', () => ({ |
| 15 | + flashcardStore: mocks.flashcardStore, |
| 16 | + flashcardDeckStore: mocks.flashcardDeckStore, |
| 17 | +})); |
| 18 | + |
| 19 | +import { findDueCards, findTopDueDeck } from './bedtimeReview'; |
| 20 | + |
| 21 | +interface FakeCard { |
| 22 | + id: string; |
| 23 | + deckId: string; |
| 24 | + dueDate: Date; |
| 25 | + repetitions: number; |
| 26 | +} |
| 27 | + |
| 28 | +/** 保留 Dexie where/and/toArray 语义的极简内存表 mock */ |
| 29 | +function makeCardTable(rows: FakeCard[]) { |
| 30 | + return { |
| 31 | + where: (field: string) => ({ |
| 32 | + belowOrEqual: (bound: Date) => ({ |
| 33 | + and: (pred: (r: FakeCard) => boolean) => ({ |
| 34 | + toArray: async () => |
| 35 | + rows.filter((r) => field !== 'dueDate' || new Date(r.dueDate) <= bound).filter(pred), |
| 36 | + }), |
| 37 | + }), |
| 38 | + }), |
| 39 | + toArray: async () => rows, |
| 40 | + }; |
| 41 | +} |
| 42 | + |
| 43 | +const DUE_SOON = new Date('2026-08-03T10:00:00Z'); |
| 44 | +const DUE_LATER = new Date('2026-08-10T10:00:00Z'); |
| 45 | +const NOW = new Date('2026-08-03T12:00:00Z'); |
| 46 | + |
| 47 | +beforeEach(() => { |
| 48 | + vi.clearAllMocks(); |
| 49 | + mocks.flashcardStore.getTable.mockReset(); |
| 50 | + mocks.flashcardDeckStore.getTable.mockReset(); |
| 51 | +}); |
| 52 | + |
| 53 | +describe('findDueCards', () => { |
| 54 | + it('should return due cards (dueDate <= now, repetitions > 0)', async () => { |
| 55 | + const cards: FakeCard[] = [ |
| 56 | + { id: 'c1', deckId: 'd1', dueDate: DUE_SOON, repetitions: 2 }, |
| 57 | + { id: 'c2', deckId: 'd1', dueDate: DUE_LATER, repetitions: 3 }, // 未到期 |
| 58 | + { id: 'c3', deckId: 'd2', dueDate: DUE_SOON, repetitions: 0 }, // 新卡 |
| 59 | + ]; |
| 60 | + mocks.flashcardStore.getTable.mockReturnValue(makeCardTable(cards)); |
| 61 | + |
| 62 | + const result = await findDueCards(NOW); |
| 63 | + expect(result.map((c) => c.id)).toEqual(['c1']); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should return empty array when no due cards', async () => { |
| 67 | + mocks.flashcardStore.getTable.mockReturnValue(makeCardTable([{ id: 'c2', deckId: 'd1', dueDate: DUE_LATER, repetitions: 3 }])); |
| 68 | + expect(await findDueCards(NOW)).toEqual([]); |
| 69 | + }); |
| 70 | +}); |
| 71 | + |
| 72 | +describe('findTopDueDeck', () => { |
| 73 | + it('should return the deck with most due cards', async () => { |
| 74 | + const cards: FakeCard[] = [ |
| 75 | + { id: 'c1', deckId: 'd1', dueDate: DUE_SOON, repetitions: 1 }, |
| 76 | + { id: 'c2', deckId: 'd1', dueDate: DUE_SOON, repetitions: 1 }, |
| 77 | + { id: 'c3', deckId: 'd2', dueDate: DUE_SOON, repetitions: 1 }, |
| 78 | + { id: 'c4', deckId: 'd3', dueDate: DUE_LATER, repetitions: 1 }, |
| 79 | + ]; |
| 80 | + mocks.flashcardStore.getTable.mockReturnValue(makeCardTable(cards)); |
| 81 | + mocks.flashcardDeckStore.getTable.mockReturnValue(makeCardTable([ |
| 82 | + { id: 'd1', deckId: 'd1', dueDate: DUE_LATER, repetitions: 0 }, |
| 83 | + { id: 'd2', deckId: 'd2', dueDate: DUE_LATER, repetitions: 0 }, |
| 84 | + ])); |
| 85 | + |
| 86 | + expect(await findTopDueDeck(NOW)).toBe('d1'); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should return undefined when no decks exist', async () => { |
| 90 | + mocks.flashcardStore.getTable.mockReturnValue(makeCardTable([ |
| 91 | + { id: 'c1', deckId: 'd1', dueDate: DUE_SOON, repetitions: 1 }, |
| 92 | + ])); |
| 93 | + mocks.flashcardDeckStore.getTable.mockReturnValue(makeCardTable([])); |
| 94 | + expect(await findTopDueDeck(NOW)).toBeUndefined(); |
| 95 | + }); |
| 96 | + |
| 97 | + it('should return undefined when no due cards at all', async () => { |
| 98 | + mocks.flashcardStore.getTable.mockReturnValue(makeCardTable([ |
| 99 | + { id: 'c4', deckId: 'd3', dueDate: DUE_LATER, repetitions: 1 }, |
| 100 | + ])); |
| 101 | + mocks.flashcardDeckStore.getTable.mockReturnValue(makeCardTable([ |
| 102 | + { id: 'd3', deckId: 'd3', dueDate: DUE_LATER, repetitions: 0 }, |
| 103 | + ])); |
| 104 | + expect(await findTopDueDeck(NOW)).toBeUndefined(); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should silently return undefined when storage query throws', async () => { |
| 108 | + // deck 表保持正常,确保 Promise.all 正常成立,卡片表拒绝被统一捕获 |
| 109 | + mocks.flashcardDeckStore.getTable.mockReturnValue(makeCardTable([])); |
| 110 | + mocks.flashcardStore.getTable.mockImplementation(() => { |
| 111 | + throw new Error('storage unavailable'); |
| 112 | + }); |
| 113 | + expect(await findTopDueDeck(NOW)).toBeUndefined(); |
| 114 | + }); |
| 115 | +}); |
0 commit comments