test: add comprehensive tests for date and Berlin clock conversions

This commit is contained in:
Loic Coenen
2026-06-24 16:29:02 +02:00
committed by Loic Coenen (aider)
parent 0f0709b90d
commit 88ce663b44
2 changed files with 112 additions and 18 deletions

View File

@@ -1,10 +0,0 @@
import { LampState, BerlinClock } from '../types';
describe('Types', () => {
it('LampState has correct values', () => {
expect(LampState.Y).toBe('Y');
expect(LampState.R).toBe('R');
expect(LampState.O).toBe('O');
expect(LampState.N).toBe('N');
});
});

View File

@@ -1,16 +1,120 @@
import { dateToDigital, digitalToDate, dateToBerlin, berlinToDate } from '../utils';
import { LampState } from '../types';
describe('Utils', () => {
it('dateToDigital returns correct format', () => {
// TODO
describe('dateToDigital', () => {
it('returns "HH:mm:ss" for a given date', () => {
const date = new Date(2025, 0, 1, 14, 5, 9);
expect(dateToDigital(date)).toBe('14:05:09');
});
it('pads single-digit hours, minutes, seconds', () => {
const date = new Date(2025, 0, 1, 2, 3, 4);
expect(dateToDigital(date)).toBe('02:03:04');
});
});
describe('digitalToDate', () => {
it('parses a valid digital string', () => {
const result = digitalToDate('12:34:56');
expect(result).toBeInstanceOf(Date);
expect(result.getHours()).toBe(12);
expect(result.getMinutes()).toBe(34);
expect(result.getSeconds()).toBe(56);
});
it('returns an invalid date for invalid input', () => {
const result = digitalToDate('abc');
expect(isNaN(result.getTime())).toBe(true);
});
});
describe('dateToBerlin', () => {
const makeTime = (h: number, m: number, s: number) => {
const d = new Date(2025, 0, 1, h, m, s);
return d;
};
it('returns correct clock for 00:00:00', () => {
const clock = dateToBerlin(makeTime(0, 0, 0));
expect(clock.secondsRow).toEqual([LampState.Y]);
expect(clock.fiveHours).toEqual([LampState.O, LampState.O, LampState.O, LampState.O]);
expect(clock.oneHour).toEqual([LampState.O, LampState.O, LampState.O, LampState.O]);
expect(clock.fiveMinutes).toEqual([
LampState.O, LampState.O, LampState.O,
LampState.O, LampState.O, LampState.O,
LampState.O, LampState.O, LampState.O,
LampState.O, LampState.O,
]);
expect(clock.oneMinute).toEqual([LampState.O, LampState.O, LampState.O, LampState.O]);
});
it('returns correct clock for 23:59:59', () => {
const clock = dateToBerlin(makeTime(23, 59, 59));
expect(clock.secondsRow).toEqual([LampState.O]);
expect(clock.fiveHours).toEqual([LampState.R, LampState.R, LampState.R, LampState.R]);
expect(clock.oneHour).toEqual([LampState.R, LampState.R, LampState.R, LampState.O]);
expect(clock.fiveMinutes).toEqual([
LampState.Y, LampState.Y, LampState.R,
LampState.Y, LampState.Y, LampState.R,
LampState.Y, LampState.Y, LampState.R,
LampState.Y, LampState.Y,
]);
expect(clock.oneMinute).toEqual([LampState.Y, LampState.Y, LampState.Y, LampState.Y]);
});
it('returns correct clock for 12:34:00', () => {
const clock = dateToBerlin(makeTime(12, 34, 0));
expect(clock.secondsRow).toEqual([LampState.Y]);
expect(clock.fiveHours).toEqual([LampState.R, LampState.R, LampState.O, LampState.O]);
expect(clock.oneHour).toEqual([LampState.R, LampState.R, LampState.O, LampState.O]);
const expectedFiveMinutes = [
LampState.Y, LampState.Y, LampState.R,
LampState.Y, LampState.Y, LampState.R,
LampState.O, LampState.O, LampState.O,
LampState.O, LampState.O,
];
expect(clock.fiveMinutes).toEqual(expectedFiveMinutes);
expect(clock.oneMinute).toEqual([LampState.Y, LampState.Y, LampState.Y, LampState.Y]);
});
});
describe('berlinToDate', () => {
it('roundtrips correctly: dateToBerlin then berlinToDate returns the original date (seconds floor to even)', () => {
const original = new Date(2025, 0, 1, 14, 30, 0);
const clock = dateToBerlin(original);
const result = berlinToDate(clock);
expect(result.getHours()).toBe(14);
expect(result.getMinutes()).toBe(30);
expect(result.getSeconds()).toBe(0);
});
it('roundtrips with odd seconds: result seconds will be zero (only parity stored)', () => {
const original = new Date(2025, 0, 1, 23, 59, 59);
const clock = dateToBerlin(original);
const result = berlinToDate(clock);
expect(result.getHours()).toBe(23);
expect(result.getMinutes()).toBe(59);
expect(result.getSeconds()).toBe(0);
});
it('converts a known Berlin clock to the correct time', () => {
const clock = {
secondsRow: [LampState.Y],
fiveHours: [LampState.R, LampState.R, LampState.O, LampState.O],
oneHour: [LampState.R, LampState.R, LampState.O, LampState.O],
fiveMinutes: [
LampState.Y, LampState.Y, LampState.R,
LampState.Y, LampState.Y, LampState.R,
LampState.Y, LampState.Y, LampState.O,
LampState.O, LampState.O,
],
oneMinute: [LampState.Y, LampState.Y, LampState.Y, LampState.Y],
};
const result = berlinToDate(clock);
expect(result.getHours()).toBe(12);
expect(result.getMinutes()).toBe(34);
expect(result.getSeconds()).toBe(0);
});
it('digitalToDate returns a Date', () => {
// TODO
});
it('dateToBerlin returns a BerlinClock', () => {
// TODO
});
it('berlinToDate returns a Date', () => {
// TODO
});
});