An open source design system that's fully customizable and agent ready
TypeScript
test(Timer): complete hardening audit layers 1-2 (#6483) (#6609)
Manohar Paturi86809383e3e7
diff --git a/.changeset/timer-hardening-audit.md b/.changeset/timer-hardening-audit.mdnew file mode 100644index 0000000000..fef775a004--- /dev/null+++ b/.changeset/timer-hardening-audit.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[chore] Timer hardening pass (Layers 1–2 of the Component Hardening Protocol, #6483). No behavior defect found — the audit confirmed the tick pipeline, format ladder, cadence switch, and resource cleanup match the `spec:AST-037` contract. Two untested guards are now pinned: the wait for a distant future `startTime` is capped at the 32-bit `setTimeout` ceiling, and an unknown `format` value coerces to `elapsed`. The Default story now asserts in a real browser that the display advances on its own. (#6483)++@ManoharPaturi @cixzhangdiff --git a/.github/scripts/story-play-guard.js b/.github/scripts/story-play-guard.jsindex e63bafa6ed..c284c84f1d 100644--- a/.github/scripts/story-play-guard.js+++ b/.github/scripts/story-play-guard.js@@ -420,6 +420,13 @@ const TARGETS = [guards:'rows appended under a resting pointer do not take the highlight; Enter selects the highlighted row (spec:AST-092 FR6)',},+ {+ component: 'Timer',+ story: 'core-timer--default',+ guards:+ 'the display ticks live from the runtime clock, so a regression that ' ++ 'freezes it at 0s fails CI (#6483 audit layer 2)',+ },];const CONTENT_TYPES = {diff --git a/apps/storybook/stories/Timer.stories.tsx b/apps/storybook/stories/Timer.stories.tsxindex 8ba371069c..aef8b2bd29 100644--- a/apps/storybook/stories/Timer.stories.tsx+++ b/apps/storybook/stories/Timer.stories.tsx@@ -2,6 +2,7 @@import {useState} from 'react';import type {Meta, StoryObj} from '@storybook/react';+import {expect, waitFor} from 'storybook/test';import {Timer} from '@astryxdesign/core/Timer';import {Stack} from '@astryxdesign/core/Layout';import {Text} from '@astryxdesign/core/Text';@@ -69,7 +70,22 @@ function TypographyStory() {);}-export const Default: Story = {};+export const Default: Story = {+ play: async ({canvasElement}) => {+ // The tick is DOM-owned: the zero-duration first paint must advance on+ // its own, with no interaction and no React render. This play runs in the+ // storybook test runner, so a regression that freezes the display fails+ // in a real browser instead of only in jsdom.+ const timer = canvasElement.querySelector('time');+ expect(timer).not.toBeNull();+ await waitFor(+ () => {+ expect(timer?.textContent).not.toBe('0s');+ },+ {timeout: 2500},+ );+ },+};export const Formats: Story = {render: () => <FormatsStory />,diff --git a/packages/core/src/Timer/Timer.test.tsx b/packages/core/src/Timer/Timer.test.tsxindex cc9783e222..72e61dabb7 100644--- a/packages/core/src/Timer/Timer.test.tsx+++ b/packages/core/src/Timer/Timer.test.tsx@@ -300,3 +300,57 @@ describe('Timer', () => {expect(timer).not.toHaveAttribute('role');});});++describe('Timer hardening guards', () => {+ beforeEach(() => {+ vi.useFakeTimers();+ vi.setSystemTime(new Date('2026-09-22T00:00:00Z'));+ });++ afterEach(() => {+ vi.useRealTimers();+ vi.restoreAllMocks();+ });++ it('caps the wait for a distant future origin at the 32-bit timeout ceiling', () => {+ const setTimeoutSpy = vi.spyOn(globalThis, 'setTimeout');+ render(+ <Timer+ startTime={Date.now() + 30 * 24 * 60 * 60 * 1000}+ data-testid="timer"+ />,+ );++ // A raw startTime - now delay would exceed the 32-bit setTimeout range and+ // fire immediately in some browsers; the ceiling keeps the wake scheduled.+ expect(setTimeoutSpy).toHaveBeenLastCalledWith(+ expect.any(Function),+ 2_147_483_647,+ );+ });++ it('coerces an unknown format to elapsed', () => {+ const setTimeoutSpy = vi.spyOn(globalThis, 'setTimeout');+ const now = Date.now();+ const {rerender} = render(+ <Timer+ format={'stopwatch' as never}+ startTime={now - 3_723_000}+ data-testid="timer"+ />,+ );++ // Past one hour, only the elapsed format moves to minute cadence: the+ // next wake is the 57 s left in the minute, not one second.+ expect(screen.getByTestId('timer')).toHaveTextContent('1h 02m');+ expect(setTimeoutSpy).toHaveBeenLastCalledWith(+ expect.any(Function),+ 57_000,+ );++ rerender(+ <Timer format="clock" startTime={now - 3_723_000} data-testid="timer" />,+ );+ expect(screen.getByTestId('timer')).toHaveTextContent('1:02:03');+ });+});