Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
refactor(frontend): close the last way past the reading hook
zchpeter3ccda2b8d6cb
diff --git a/frontend/src/hooks/useTimeReading.test.tsx b/frontend/src/hooks/useTimeReading.test.tsxindex 7a851ce153..06ea45c00f 100644--- a/frontend/src/hooks/useTimeReading.test.tsx+++ b/frontend/src/hooks/useTimeReading.test.tsx@@ -9,8 +9,13 @@ import { useTimeReading } from "./useTimeReading";const DAY_MS = 86_400_000;+// A boundary that names one instant and never changes again.+const onceAt = (changesAtMs: number) => () =>+ Date.now() < changesAtMs ? changesAtMs : Number.POSITIVE_INFINITY;+type ProbeSpec = {changesAtMs: () => number;+ read?: (input: number) => string;// An absent input has no reading.input?: number;};@@ -23,7 +28,10 @@ function Probe({onRender: (value: string | undefined) => void;}) {const value = useTimeReading(- { read: (input: number) => `read:${input}`, nextChangeAt: spec.changesAtMs },+ {+ read: spec.read ?? ((input: number) => `read:${input}`),+ nextChangeAt: spec.changesAtMs,+ },"input" in spec ? spec.input : 0);onRender(value);@@ -229,24 +237,20 @@ describe("useTimeReading", () => {expect(calls).toEqual(["boundary", "read"]);});- test("returns the reading's current value, and keeps it current", () => {- let value = "before";+ test("returns the value the reading gives now, and keeps it current", () => {const deadlineMs = Date.now() + 1_000;const { values } = mount([{- changesAtMs: () => {- value = Date.now() < deadlineMs ? "before" : "after";- return Date.now() < deadlineMs ? deadlineMs : Number.POSITIVE_INFINITY;- },+ read: () => (Date.now() < deadlineMs ? "before" : "after"),+ changesAtMs: onceAt(deadlineMs),},]);- expect(values).toEqual(["read:0"]);- expect(value).toBe("before");+ expect(values).toEqual(["before"]);act(() => {vi.advanceTimersByTime(1_000);});- expect(value).toBe("after");+ expect(values).toEqual(["after"]);});test("wakes every display when the wall clock steps backward", () => {@@ -264,7 +268,7 @@ describe("useTimeReading", () => {test("never stretches the wake gap past its bound after a clock step", () => {const firstMs = Date.now() + 1_000;- mount([{ changesAtMs: () => (Date.now() < firstMs ? firstMs : Number.POSITIVE_INFINITY) }]);+ mount([{ changesAtMs: onceAt(firstMs) }]);act(() => {vi.advanceTimersByTime(1_000);});@@ -272,7 +276,7 @@ describe("useTimeReading", () => {vi.setSystemTime(Date.now() - 3_600_000);const secondMs = Date.now() + 100;const { renders } = mount([- { changesAtMs: () => (Date.now() < secondMs ? secondMs : Number.POSITIVE_INFINITY) },+ { changesAtMs: onceAt(secondMs) },]);act(() => {vi.advanceTimersByTime(350);@@ -289,7 +293,7 @@ describe("useTimeReading", () => {const soonMs = Date.now() + 10;const { renders } = mount([- { changesAtMs: () => (Date.now() < soonMs ? soonMs : Number.POSITIVE_INFINITY) },+ { changesAtMs: onceAt(soonMs) },]);act(() => {vi.advanceTimersByTime(20);diff --git a/frontend/src/routes/project/ProjectReleaseDashboardPage.tsx b/frontend/src/routes/project/ProjectReleaseDashboardPage.tsxindex da1f7005d4..cd0154f471 100644--- a/frontend/src/routes/project/ProjectReleaseDashboardPage.tsx+++ b/frontend/src/routes/project/ProjectReleaseDashboardPage.tsx@@ -245,9 +245,7 @@ function ReleaseRow({ release }: { release: Release }) {const showFiles = release.files.slice(0, MAX_SHOW_FILES_COUNT);- const createTimeTs = release.createTime- ? getTimeForPbTimestampProtoEs(release.createTime) / 1000- : undefined;+ const createTimeMs = getTimeForPbTimestampProtoEs(release.createTime);const url = `/${release.name}`;@@ -295,7 +293,11 @@ function ReleaseRow({ release }: { release: Release }) {</div></TableCell><TableCell>- {createTimeTs === undefined ? "-" : <HumanizeTs ts={createTimeTs} />}+ {createTimeMs === undefined ? (+ "-"+ ) : (+ <HumanizeTs ts={createTimeMs / 1000} />+ )}</TableCell></TableRow>);diff --git a/frontend/src/routes/project/database-detail/panels/DatabaseOverviewPanel.test.tsx b/frontend/src/routes/project/database-detail/panels/DatabaseOverviewPanel.test.tsxindex 4eff7433d5..5782c121ab 100644--- a/frontend/src/routes/project/database-detail/panels/DatabaseOverviewPanel.test.tsx+++ b/frontend/src/routes/project/database-detail/panels/DatabaseOverviewPanel.test.tsx@@ -162,7 +162,6 @@ vi.mock("@/utils", () => ({getDatabaseEngine: mocks.getDatabaseEngine,getInstanceResource: mocks.getInstanceResource,getDatabaseProject: mocks.getDatabaseProject,- formatRelativeTime: mocks.formatRelativeTime,formatAbsoluteDateTime: mocks.formatAbsoluteDateTime,hasIndexSizeProperty: mocks.hasIndexSizeProperty,isDev: mocks.isDev,diff --git a/frontend/src/utils/datetime.test.ts b/frontend/src/utils/datetime.test.tsindex d32d31ccd3..cafdc5c301 100644--- a/frontend/src/utils/datetime.test.ts+++ b/frontend/src/utils/datetime.test.ts@@ -22,12 +22,9 @@ const withLocale = (language: string, run: () => void) => {import {countdownReading,daysLeftReading,- formatAbsoluteDate,formatAbsoluteDateTime,formatCompactDateTime,formatOperationalDateTime,- formatQueueTime,- formatRelativeTime,passedReading,queueTimeReading,RELATIVE_THRESHOLD_MS,@@ -40,7 +37,7 @@ const MINUTE_MS = 60_000;const HOUR_MS = 3_600_000;const DAY_MS = 86_400_000;-describe("formatRelativeTime", () => {+describe("the relative reading", () => {beforeEach(() => {vi.useFakeTimers();vi.setSystemTime(new Date("2026-03-02T12:00:00Z"));@@ -51,32 +48,32 @@ describe("formatRelativeTime", () => {});test("returns 'now' for timestamps less than 10 seconds ago", () => {- const result = formatRelativeTime(Date.now() - 5000);+ const result = relativeTimeReading.read(Date.now() - 5000);expect(result).toBe("now");});test("returns 'X seconds ago' for 10-59 seconds", () => {- const result = formatRelativeTime(Date.now() - 30_000);+ const result = relativeTimeReading.read(Date.now() - 30_000);expect(result).toContain("seconds ago");});test("returns 'X minutes ago' for 1-59 minutes", () => {- const result = formatRelativeTime(Date.now() - 5 * 60_000);+ const result = relativeTimeReading.read(Date.now() - 5 * 60_000);expect(result).toMatch(/minutes? ago/);});test("returns 'X hours ago' for 1-23 hours", () => {- const result = formatRelativeTime(Date.now() - 3 * 3_600_000);+ const result = relativeTimeReading.read(Date.now() - 3 * 3_600_000);expect(result).toMatch(/hours? ago/);});test("returns 'yesterday' for ~24 hours ago", () => {- const result = formatRelativeTime(Date.now() - 24 * 3_600_000);+ const result = relativeTimeReading.read(Date.now() - 24 * 3_600_000);expect(result).toBe("yesterday");});test("returns 'X days ago' for 2-30 days", () => {- const result = formatRelativeTime(Date.now() - 10 * 86_400_000);+ const result = relativeTimeReading.read(Date.now() - 10 * 86_400_000);expect(result).toContain("days ago");});});@@ -92,31 +89,7 @@ describe("formatAbsoluteDateTime", () => {});});-describe("formatAbsoluteDate", () => {- beforeEach(() => {- vi.useFakeTimers();- vi.setSystemTime(new Date("2026-03-02T12:00:00Z"));- });-- afterEach(() => {- vi.useRealTimers();- });-- test("omits year for same-year dates", () => {- const ts = new Date("2026-06-15T00:00:00Z").getTime();- const result = formatAbsoluteDate(ts);- expect(result).not.toContain("2026");- expect(result).toContain("Jun");- });-- test("includes year for different-year dates", () => {- const ts = new Date("2025-01-15T00:00:00Z").getTime();- const result = formatAbsoluteDate(ts);- expect(result).toContain("2025");- });-});--describe("formatQueueTime", () => {+describe("the work-queue reading", () => {beforeEach(() => {vi.useFakeTimers();vi.setSystemTime(new Date("2026-03-02T12:00:00Z"));@@ -127,30 +100,40 @@ describe("formatQueueTime", () => {});test("reads as relative age inside the 30-day window", () => {- expect(formatQueueTime(Date.now() - 6 * 86_400_000)).toBe("6 days ago");+ expect(queueTimeReading.read(Date.now() - 6 * 86_400_000)).toBe(+ "6 days ago"+ );});test("still reads as relative age one day short of the threshold", () => {- expect(formatQueueTime(Date.now() - 29 * 86_400_000)).toBe("29 days ago");+ expect(queueTimeReading.read(Date.now() - 29 * 86_400_000)).toBe(+ "29 days ago"+ );});test("switches to an absolute date at the threshold", () => {- expect(formatQueueTime(Date.now() - RELATIVE_THRESHOLD_MS)).toBe("Jan 31");+ expect(queueTimeReading.read(Date.now() - RELATIVE_THRESHOLD_MS)).toBe(+ "Jan 31"+ );});test("keeps the year on a date from another year", () => {- expect(formatQueueTime(Date.now() - 90 * 86_400_000)).toBe("Dec 2, 2025");+ expect(queueTimeReading.read(Date.now() - 90 * 86_400_000)).toBe(+ "Dec 2, 2025"+ );});test("measures a future timestamp by the same threshold", () => {- expect(formatQueueTime(Date.now() + 2 * 86_400_000)).toBe("in 2 days");- expect(formatQueueTime(Date.now() + 40 * 86_400_000)).toBe("Apr 11");+ expect(queueTimeReading.read(Date.now() + 2 * 86_400_000)).toBe(+ "in 2 days"+ );+ expect(queueTimeReading.read(Date.now() + 40 * 86_400_000)).toBe("Apr 11");});test("renders both branches in the active locale", () => {withLocale("zh-CN", () => {- expect(formatQueueTime(Date.now() - 6 * 86_400_000)).toBe("6天前");- expect(formatQueueTime(Date.now() - RELATIVE_THRESHOLD_MS)).toBe(+ expect(queueTimeReading.read(Date.now() - 6 * 86_400_000)).toBe("6天前");+ expect(queueTimeReading.read(Date.now() - RELATIVE_THRESHOLD_MS)).toBe("1月31日");});@@ -255,12 +238,12 @@ const expectBoundaryMatchesReading = <Value>(const { nextChangeAt } = reading;for (let link = 0; link < CHAIN_LINKS; link++) {const startMs = Date.now();- const reading = read(tsMs);+ const value = read(tsMs);const changesAtMs = nextChangeAt(tsMs);if (changesAtMs === Number.POSITIVE_INFINITY) {for (const laterMs of FINAL_PROBES_MS) {vi.setSystemTime(startMs + laterMs);- expect(read(tsMs)).toBe(reading);+ expect(read(tsMs)).toBe(value);}return;}@@ -271,11 +254,11 @@ const expectBoundaryMatchesReading = <Value>(Math.ceil(changesAtMs) - 1,]) {vi.setSystemTime(probeMs);- expect(read(tsMs)).toBe(reading);+ expect(read(tsMs)).toBe(value);expect(nextChangeAt(tsMs)).toBe(changesAtMs);}vi.setSystemTime(changesAtMs);- expect(read(tsMs)).not.toBe(reading);+ expect(read(tsMs)).not.toBe(value);}};@@ -316,27 +299,18 @@ describe("reading boundaries", () => {vi.useRealTimers();});- test.each(boundaryCases(SAMPLED_AGES_MS))(- "names the instant the work-queue reading changes (age $offset ms from $start, +$fractionMs ms)",- ({ start, offset, fractionMs }) => {- const startMs = startAt(start);- expectBoundaryMatchesReading(- queueTimeReading,- startMs - offset + fractionMs- );- }- );-- test.each(boundaryCases(SAMPLED_AGES_MS))(- "names the instant the relative reading changes (age $offset ms from $start, +$fractionMs ms)",- ({ start, offset, fractionMs }) => {- const startMs = startAt(start);- expectBoundaryMatchesReading(- relativeTimeReading,- startMs - offset + fractionMs- );- }- );+ describe.each([+ ["work-queue", queueTimeReading],+ ["relative", relativeTimeReading],+ ])("%s", (_name, reading) => {+ test.each(boundaryCases(SAMPLED_AGES_MS))(+ "names the instant the reading changes (age $offset ms from $start, +$fractionMs ms)",+ ({ start, offset, fractionMs }) => {+ const startMs = startAt(start);+ expectBoundaryMatchesReading(reading, startMs - offset + fractionMs);+ }+ );+ });});describe("deadline readings", () => {diff --git a/frontend/src/utils/datetime.ts b/frontend/src/utils/datetime.tsindex f6ce81cb0a..f7b4850401 100644--- a/frontend/src/utils/datetime.ts+++ b/frontend/src/utils/datetime.ts@@ -64,7 +64,7 @@ export type TimeReading<Input, Value> = {nextChangeAt: (input: Input) => number;};-export function formatRelativeTime(timestampMs: number): string {+function formatRelativeTime(timestampMs: number): string {const diffMs = Date.now() - timestampMs;const ageMs = Math.abs(diffMs);const rtf = cachedFormatter(@@ -93,7 +93,7 @@ export function formatAbsoluteDateTime(timestampMs: number): string {}).format(timestampMs);}-export function formatAbsoluteDate(timestampMs: number): string {+function formatAbsoluteDate(timestampMs: number): string {if (new Date(timestampMs).getFullYear() === new Date().getFullYear()) {return dateTimeFormatter("date", {month: "short",@@ -107,7 +107,7 @@ export function formatAbsoluteDate(timestampMs: number): string {}).format(timestampMs);}-export function formatQueueTime(timestampMs: number): string {+function formatQueueTime(timestampMs: number): string {if (Math.abs(Date.now() - timestampMs) >= RELATIVE_THRESHOLD_MS) {return formatAbsoluteDate(timestampMs);}