fix: update seconds lamp every second when using system time

This commit is contained in:
Loic Coenen
2026-06-25 09:58:27 +02:00
committed by Loic Coenen (aider)
parent 0b3a1d4d66
commit abf8b13fc7
2 changed files with 39 additions and 24 deletions

View File

@@ -211,33 +211,40 @@ describe("Berlin Clock", () => {
});
describe("Use System Time", () => {
it("should show system time when checkbox is checked", () => {
// Start by entering a fixed time
cy.get('[data-testid="time-input"]')
.clear()
.type("12:00:00")
.type("{enter}");
cy.get('[data-testid="digital-clock"]').should("contain", "12:00:00");
it("should update every second and the seconds lamp should blink", () => {
// Freeze the clock at a known time (even seconds e.g. 0, so lamp is Y)
const now = new Date();
now.setSeconds(0, 0); // seconds = 0 → even → lamp = Y
cy.clock(now);
// Visit the page (clock is frozen from this point)
cy.visit("/");
// Check the "Use system time" checkbox
cy.get('[data-testid="use-system-time"]').check();
// Now the digital clock should reflect the real system time
cy.get('[data-testid="digital-clock"]')
.invoke("text")
.then((displayedTime) => {
// displayedTime is "HH:mm:ss"
const parts = displayedTime.split(":");
const displayedSeconds =
parseInt(parts[0]) * 3600 +
parseInt(parts[1]) * 60 +
parseInt(parts[2]);
const now = new Date();
const realSeconds =
now.getHours() * 3600 + now.getMinutes() * 60 + now.getSeconds();
// Allow a 5-second tolerance
expect(Math.abs(displayedSeconds - realSeconds)).to.be.lessThan(5);
});
// Verify initial seconds lamp is Y (seconds=0)
cy.get('[data-testid="seconds-lamp"]').should(
"have.attr",
"aria-label",
"Y",
);
// Advance time by 1 second → seconds=1 (odd) → lamp toggles to O
cy.tick(1000);
cy.get('[data-testid="seconds-lamp"]').should(
"have.attr",
"aria-label",
"O",
);
// Advance another second → seconds=2 (even) → lamp back to Y
cy.tick(1000);
cy.get('[data-testid="seconds-lamp"]').should(
"have.attr",
"aria-label",
"Y",
);
});
});
});

View File

@@ -1,4 +1,4 @@
import { useState } from "react";
import { useState, useEffect } from "react";
import { BerlinClock } from "./BerlinClock";
import { DigitalClock } from "./DigitalClock";
import { useCurrentTime } from "../hooks/useCurrentTime";
@@ -9,6 +9,14 @@ export function TimeConverter() {
const [useSystemTime, setUseSystemTime] = useState(false);
const berlinClock = dateToBerlin(currentTime);
useEffect(() => {
if (!useSystemTime) return;
const id = setInterval(() => {
setCurrentTime(new Date());
}, 1000);
return () => clearInterval(id);
}, [useSystemTime, setCurrentTime]);
return (
<div>
<label>