Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
fix(frontend): ask the formatters what an instant is
zchpeter453780ca4cae
diff --git a/frontend/src/components/HumanizeTs.test.tsx b/frontend/src/components/HumanizeTs.test.tsxindex 09f5e3d006..e95613a2ba 100644--- a/frontend/src/components/HumanizeTs.test.tsx+++ b/frontend/src/components/HumanizeTs.test.tsx@@ -18,12 +18,20 @@ const formatters = vi.hoisted(() => {? ms + boundaryOffsetMs: Number.POSITIVE_INFINITY,});+ // A rendering that never changes on its own.+ const fixedReading = (prefix: string) => ({+ read: (ms: number) => `${prefix}:${ms}`,+ nextChangeAt: () => Number.POSITIVE_INFINITY,+ });return {queueTimeReading: ageReading("queue", 60_000),relativeTimeReading: ageReading("relative", 45_000),- formatCompactDateTime: (ms: number) => `compact:${ms}`,- formatOperationalDateTime: (ms: number) => `operational:${ms}`,formatAbsoluteDateTime: vi.fn((ms: number) => `absolute:${ms}`),+ compactTimeReading: fixedReading("compact"),+ operationalTimeReading: fixedReading("operational"),+ absoluteTimeReading: fixedReading("absolute"),+ displayableInstantMs: (ms: number) =>+ Number.isFinite(ms) && Math.abs(ms) <= 8.64e15 ? ms : undefined,};});@@ -141,6 +149,10 @@ describe("HumanizeTs", () => {const { container, root } = mount();act(() => root.render(<HumanizeTs ts={Number.NaN} />));expect(container.textContent).toBe("");++ // Finite, and still no instant it can name: Intl throws on this too.+ act(() => root.render(<HumanizeTs ts={1e15} />));+ expect(container.textContent).toBe("");});test("omits the tooltip when tooltip is false", async () => {diff --git a/frontend/src/components/HumanizeTs.tsx b/frontend/src/components/HumanizeTs.tsxindex af9866822d..f46ce60e21 100644--- a/frontend/src/components/HumanizeTs.tsx+++ b/frontend/src/components/HumanizeTs.tsx@@ -3,9 +3,11 @@ import { useTranslation } from "react-i18next";import { Tooltip } from "@/components/ui/tooltip";import { useTimeReading } from "@/hooks/useTimeReading";import {+ absoluteTimeReading,+ compactTimeReading,+ displayableInstantMs,formatAbsoluteDateTime,- formatCompactDateTime,- formatOperationalDateTime,+ operationalTimeReading,queueTimeReading,relativeTimeReading,type TimeReading,@@ -48,13 +50,6 @@ function RelativeAge({ tsMs }: { tsMs: number }) {return <>{useTimeReading(relativeTimeReading, tsMs)}</>;}-const fixed = (- format: (tsMs: number) => string-): TimeReading<number, string> => ({- read: format,- nextChangeAt: () => Number.POSITIVE_INFINITY,-});-// Each mode's label reading, and the reading its tooltip restores: the full// date-time for every reduced form, the age for the full one.const MODES: Record<@@ -65,12 +60,9 @@ const MODES: Record<}> = {queue: { label: queueTimeReading, Hidden: FullDateTime },- compact: { label: fixed(formatCompactDateTime), Hidden: FullDateTime },- operational: {- label: fixed(formatOperationalDateTime),- Hidden: FullDateTime,- },- datetime: { label: fixed(formatAbsoluteDateTime), Hidden: RelativeAge },+ compact: { label: compactTimeReading, Hidden: FullDateTime },+ operational: { label: operationalTimeReading, Hidden: FullDateTime },+ datetime: { label: absoluteTimeReading, Hidden: RelativeAge },};/**@@ -86,10 +78,9 @@ export function HumanizeTs({}: HumanizeTsProps) {// Subscribe to locale changes so the rendered strings update on a language switch.useTranslation();- // An unvalidated string reaching `new Date(...)` gives NaN, which `Intl`- // throws on; a row loses its timestamp rather than the page its subtree.- const instantMs = ts * 1000;- const tsMs = Number.isFinite(instantMs) ? instantMs : undefined;+ // A row loses its timestamp rather than the page its subtree: what is not an+ // instant has no rendering, and the formatters throw on it.+ const tsMs = displayableInstantMs(ts * 1000);const { label: labelReading, Hidden } = MODES[mode];const label = useTimeReading(labelReading, tsMs);if (tsMs === undefined) {diff --git a/frontend/src/routes/project/DatabaseChangelogDetailPage.test.tsx b/frontend/src/routes/project/DatabaseChangelogDetailPage.test.tsxindex c6e8cb0396..c9df236668 100644--- a/frontend/src/routes/project/DatabaseChangelogDetailPage.test.tsx+++ b/frontend/src/routes/project/DatabaseChangelogDetailPage.test.tsx@@ -214,6 +214,11 @@ vi.mock("@/utils/v1/project", () => ({vi.mock("@/utils/datetime", async (importOriginal) => ({...(await importOriginal<typeof import("@/utils/datetime")>()),formatAbsoluteDateTime: () => "formatted time",+ // A label comes from its reading, not from the formatter the reading holds.+ absoluteTimeReading: {+ read: () => "formatted time",+ nextChangeAt: () => Number.POSITIVE_INFINITY,+ },}));vi.mock("@/utils", () => ({diff --git a/frontend/src/routes/project/database-detail/panels/DatabaseOverviewPanel.test.tsx b/frontend/src/routes/project/database-detail/panels/DatabaseOverviewPanel.test.tsxindex 5782c121ab..62b19a4ccf 100644--- a/frontend/src/routes/project/database-detail/panels/DatabaseOverviewPanel.test.tsx+++ b/frontend/src/routes/project/database-detail/panels/DatabaseOverviewPanel.test.tsx@@ -152,9 +152,21 @@ vi.mock("@/utils/datetime", () => ({read: mocks.formatRelativeTime,nextChangeAt: () => Number.POSITIVE_INFINITY,},- formatCompactDateTime: mocks.formatAbsoluteDateTime,- formatOperationalDateTime: mocks.formatAbsoluteDateTime,formatAbsoluteDateTime: mocks.formatAbsoluteDateTime,+ compactTimeReading: {+ read: mocks.formatAbsoluteDateTime,+ nextChangeAt: () => Number.POSITIVE_INFINITY,+ },+ operationalTimeReading: {+ read: mocks.formatAbsoluteDateTime,+ nextChangeAt: () => Number.POSITIVE_INFINITY,+ },+ absoluteTimeReading: {+ read: mocks.formatAbsoluteDateTime,+ nextChangeAt: () => Number.POSITIVE_INFINITY,+ },+ displayableInstantMs: (ms: number) =>+ Number.isFinite(ms) && Math.abs(ms) <= 8.64e15 ? ms : undefined,}));vi.mock("@/utils", () => ({diff --git a/frontend/src/utils/datetime.test.ts b/frontend/src/utils/datetime.test.tsindex cafdc5c301..70b733af27 100644--- a/frontend/src/utils/datetime.test.ts+++ b/frontend/src/utils/datetime.test.ts@@ -22,6 +22,7 @@ const withLocale = (language: string, run: () => void) => {import {countdownReading,daysLeftReading,+ displayableInstantMs,formatAbsoluteDateTime,formatCompactDateTime,formatOperationalDateTime,@@ -399,3 +400,45 @@ describe("deadline readings", () => {expect(daysLeftReading.read(baseMs)).toEqual({ kind: "passed" });});});++describe("displayableInstantMs", () => {+ // Every value a display can be handed, at and just past the edge of the+ // range a time value can occupy.+ const candidates = [+ 0,+ -1,+ 1_772_452_800_123,+ 1_772_452_800_123.5,+ 8.64e15,+ -8.64e15,+ 8.64e15 + 1,+ -8.64e15 - 1,+ 1e18,+ -1e18,+ Number.NaN,+ Number.POSITIVE_INFINITY,+ Number.NEGATIVE_INFINITY,+ ];++ test.each(candidates)(+ "accepts %p exactly when it is a time at all",+ (value) => {+ const isATime = !Number.isNaN(new Date(value).getTime());+ expect(displayableInstantMs(value)).toBe(isATime ? value : undefined);+ }+ );++ test.each(candidates)(+ "never yields %p to a formatter that throws",+ (value) => {+ if (displayableInstantMs(value) === undefined) {+ return;+ }+ // A value this admits reaches Intl, which throws on one outside the range.+ expect(() => formatAbsoluteDateTime(value)).not.toThrow();+ expect(() => formatCompactDateTime(value)).not.toThrow();+ expect(() => formatOperationalDateTime(value)).not.toThrow();+ expect(() => queueTimeReading.read(value)).not.toThrow();+ }+ );+});diff --git a/frontend/src/utils/datetime.ts b/frontend/src/utils/datetime.tsindex c373ef0c3b..763450c528 100644--- a/frontend/src/utils/datetime.ts+++ b/frontend/src/utils/datetime.ts@@ -8,6 +8,24 @@ const DAY_MS = 24 * HOUR_MS;export const RELATIVE_THRESHOLD_MS = 30 * DAY_MS;const NOW_THRESHOLD_MS = 10 * SECOND_MS;+// The range a time value can occupy. Beyond it `Date` holds no time and the+// date formatters below throw, so this is their domain.+const MAX_TIME_VALUE_MS = 8.64e15;++/**+ * The instant a display can render, or nothing for a number that is not a+ * time: an unvalidated string through `new Date(...)` arrives as NaN, and a+ * timestamp can carry a value no instant occupies. The container then shows+ * its empty form, rather than the page losing a subtree to a formatter that+ * throws. A display asks here rather than testing a value itself, which is how+ * a guard drifts from the domain it guards.+ */+export function displayableInstantMs(ms: number): number | undefined {+ return Number.isFinite(ms) && Math.abs(ms) <= MAX_TIME_VALUE_MS+ ? ms+ : undefined;+}+export function getActiveLocale(): string {return i18n.language;}@@ -202,6 +220,19 @@ export const queueTimeReading: TimeReading<number, string> = {nextChangeAt: nextQueueTimeChangeAt,};+// A rendering that says the same thing however long it is on screen: a reading+// whose boundary never arrives, so a display of one holds no subscription.+const fixedReading = (+ format: (timestampMs: number) => string+): TimeReading<number, string> => ({+ read: format,+ nextChangeAt: () => Number.POSITIVE_INFINITY,+});++export const absoluteTimeReading = fixedReading(formatAbsoluteDateTime);+export const compactTimeReading = fixedReading(formatCompactDateTime);+export const operationalTimeReading = fixedReading(formatOperationalDateTime);+// Readings of a deadline.function hasPassed(targetMs: number): boolean {