Files
zen-kanji/server/tests/utils.test.js
T
Crylia c1651550d1
Release Build / build-docker (push) Successful in 52s
Release Build / build-android-and-release (push) Failing after 1m1s
chore: sync all remaining changes
- Remove android build directory (moved to CI)
- Update package dependencies
- Misc server and client improvements
2026-04-18 22:40:25 +02:00

59 lines
2.0 KiB
JavaScript

import { describe, it, expect, vi, beforeAll, afterAll } from 'vitest';
import { getDaysDiff, getSRSDate } from '../src/utils/dateUtils.js';
describe('Date Utils', () => {
describe('getDaysDiff', () => {
it('should calculate difference between two dates correctly', () => {
const d1 = '2023-01-01';
const d2 = '2023-01-03';
expect(getDaysDiff(d1, d2)).toBe(2);
});
it('should return 0 for same day', () => {
expect(getDaysDiff('2023-01-01', '2023-01-01')).toBe(0);
});
});
describe('getSRSDate', () => {
beforeAll(() => {
vi.useFakeTimers();
vi.setSystemTime(new Date('2023-01-01T12:00:00Z'));
});
afterAll(() => {
vi.useRealTimers();
});
// Helper: getSRSDate adds hours then zeroes out minutes/seconds
const expectedDate = (hoursToAdd) => {
const d = new Date('2023-01-01T12:00:00Z');
d.setUTCHours(d.getUTCHours() + hoursToAdd);
d.setUTCMinutes(0, 0, 0);
return d.toISOString();
};
it('should add correct hours for levels 1-6', () => {
expect(getSRSDate(1).toISOString()).toBe(expectedDate(4)); // 4 hours
expect(getSRSDate(2).toISOString()).toBe(expectedDate(8)); // 8 hours
expect(getSRSDate(3).toISOString()).toBe(expectedDate(24)); // 1 day
expect(getSRSDate(4).toISOString()).toBe(expectedDate(48)); // 2 days
expect(getSRSDate(5).toISOString()).toBe(expectedDate(7 * 24)); // 1 week
expect(getSRSDate(6).toISOString()).toBe(expectedDate(14 * 24)); // 2 weeks
});
it('should add correct hours for levels 7-9', () => {
expect(getSRSDate(7).toISOString()).toBe(expectedDate(30 * 24)); // 30 days
expect(getSRSDate(8).toISOString()).toBe(expectedDate(90 * 24)); // 90 days
expect(getSRSDate(9).toISOString()).toBe(expectedDate(180 * 24)); // 180 days
});
it('should return null for level 10 (burned)', () => {
expect(getSRSDate(10)).toBeNull();
});
it('should default to 4 hours for unknown levels', () => {
expect(getSRSDate(99).toISOString()).toBe(expectedDate(4));
});
});
});