🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.
test(timeSeriesChartUtils): add unit tests for getChartTimeBounds function
Bill Yangff45fb4cf5b9
diff --git a/client/src/components/charts/timeSeriesChartUtils.test.ts b/client/src/components/charts/timeSeriesChartUtils.test.tsnew file mode 100644index 000000000..5e66981ca--- /dev/null+++ b/client/src/components/charts/timeSeriesChartUtils.test.ts@@ -0,0 +1,75 @@+import { DateTime } from "luxon";+import { describe, expect, it } from "vitest";+import { Time } from "../DateSelector/types";+import { getChartTimeBounds } from "./timeSeriesChartUtils";++const ZONE = "America/New_York";++const boundsIso = (time: Time, bucket: Parameters<typeof getChartTimeBounds>[1]) => {+ const { min, max } = getChartTimeBounds(time, bucket, ZONE);+ const iso = (d: Date | undefined) => (d ? DateTime.fromJSDate(d).setZone(ZONE).toISO() : undefined);+ return { min: iso(min), max: iso(max) };+};++describe("getChartTimeBounds", () => {+ it("week: ends on the last day bucket, not the last millisecond", () => {+ expect(boundsIso({ mode: "week", week: "2026-08-17" }, "day")).toEqual({+ min: "2026-08-17T00:00:00.000-04:00",+ max: "2026-08-23T00:00:00.000-04:00",+ });+ });++ it("week: ends on the last hour bucket", () => {+ expect(boundsIso({ mode: "week", week: "2026-08-17" }, "hour").max).toBe("2026-08-23T23:00:00.000-04:00");+ });++ it("month: ends on the last day bucket", () => {+ expect(boundsIso({ mode: "month", month: "2026-08-01" }, "day")).toEqual({+ min: "2026-08-01T00:00:00.000-04:00",+ max: "2026-08-31T00:00:00.000-04:00",+ });+ });++ it("month: weekly buckets land on Sunday, including a month ending on one", () => {+ expect(boundsIso({ mode: "month", month: "2026-08-01" }, "week").max).toBe("2026-08-30T00:00:00.000-04:00");+ // January 2027 ends on a Sunday: flooring to Monday would drop that bucket.+ expect(boundsIso({ mode: "month", month: "2027-01-01" }, "week").max).toBe("2027-01-31T00:00:00.000-05:00");+ });++ it("year: ends on the last month bucket", () => {+ expect(boundsIso({ mode: "year", year: "2026-01-01" }, "month").max).toBe("2026-12-01T00:00:00.000-05:00");+ });++ it("day: ends on the last bucket of the day, across a DST change", () => {+ expect(boundsIso({ mode: "day", day: "2026-08-19" }, "five_minutes").max).toBe("2026-08-19T23:55:00.000-04:00");+ expect(boundsIso({ mode: "day", day: "2026-11-01" }, "hour").max).toBe("2026-11-01T23:00:00.000-05:00");+ });++ it("range: ends on the end date's bucket", () => {+ expect(boundsIso({ mode: "range", startDate: "2026-08-13", endDate: "2026-08-19" }, "day")).toEqual({+ min: "2026-08-13T00:00:00.000-04:00",+ max: "2026-08-19T00:00:00.000-04:00",+ });+ });++ it("range: a single-bucket period keeps a non-empty domain", () => {+ expect(boundsIso({ mode: "range", startDate: "2026-08-19", endDate: "2026-08-19" }, "day")).toEqual({+ min: "2026-08-19T00:00:00.000-04:00",+ max: "2026-08-19T23:59:59.999-04:00",+ });+ });++ it("exact range: ends on the last bucket inside the exclusive end", () => {+ const exact = {+ mode: "range",+ startDate: "2026-08-19",+ endDate: "2026-08-19",+ startTime: "09:15:00",+ endTime: "17:45:00",+ } as const;+ expect(boundsIso(exact, "hour").max).toBe("2026-08-19T17:00:00.000-04:00");+ expect(boundsIso({ ...exact, startTime: "10:00:00", endTime: "12:00:00" }, "hour").max).toBe(+ "2026-08-19T11:00:00.000-04:00"+ );+ });+});diff --git a/client/src/components/charts/timeSeriesChartUtils.ts b/client/src/components/charts/timeSeriesChartUtils.tsindex cc9c9e54b..f21af5969 100644--- a/client/src/components/charts/timeSeriesChartUtils.ts+++ b/client/src/components/charts/timeSeriesChartUtils.ts@@ -3,21 +3,6 @@ import { DateTime } from "luxon";import type { Time } from "@/components/DateSelector/types";-const bucketEndOffsetMinutes = (bucket: TimeBucket): number => {- switch (bucket) {- case "hour":- return 59;- case "fifteen_minutes":- return 14;- case "ten_minutes":- return 9;- case "five_minutes":- return 4;- default:- return 0;- }-};-export const stepBucket = (dt: DateTime, bucket: TimeBucket, direction: 1 | -1): DateTime => {const n = direction;switch (bucket) {@@ -157,10 +142,27 @@ export type ChartTimeBounds = {max: Date | undefined;};+// ClickHouse's `toStartOfWeek` floors to Sunday where Luxon's `startOf("week")`+// floors to Monday, so weekly buckets land on Sundays. Flooring with Luxon would+// put the axis max *before* the final bucket of a period that ends on a Sunday,+// and consumers drop points past the max.+const floorToBucketStart = (dt: DateTime, bucket: TimeBucket): DateTime =>+ bucket === "week" ? dt.startOf("day").minus({ days: dt.weekday % 7 }) : floorToBucket(dt, bucket);++/**+ * The start of the final bucket in `[start, endExclusive)`. Bounds end there+ * rather than at the period's last millisecond so the closing tick sits on the+ * right edge instead of a whole bucket short of it.+ */+const lastBucketStart = (start: DateTime, endExclusive: DateTime, bucket: TimeBucket): Date => {+ const lastInstant = endExclusive.minus({ milliseconds: 1 });+ const last = floorToBucketStart(lastInstant, bucket);+ // A period holding a single bucket would otherwise collapse the x domain.+ return (last > start ? last : lastInstant).toJSDate();+};+// Returns full-period x-scale bounds so related charts share a congruent scale.export const getChartTimeBounds = (time: Time, bucket: TimeBucket, timezone: string): ChartTimeBounds => {- const offset = bucketEndOffsetMinutes(bucket);-if (time.mode === "past-minutes") {const startUnit = time.pastMinutesStart < 360 ? "minute" : "hour";const min = DateTime.now()@@ -176,10 +178,10 @@ export const getChartTimeBounds = (time: Time, bucket: TimeBucket, timezone: str}if (time.mode === "day") {- const day = DateTime.fromISO(time.day, { zone: timezone });+ const day = DateTime.fromISO(time.day, { zone: timezone }).startOf("day");return {- min: day.startOf("day").toJSDate(),- max: day.endOf("day").minus({ minutes: offset }).toJSDate(),+ min: day.toJSDate(),+ max: lastBucketStart(day, day.plus({ days: 1 }), bucket),};}@@ -187,7 +189,7 @@ export const getChartTimeBounds = (time: Time, bucket: TimeBucket, timezone: strconst week = DateTime.fromISO(time.week, { zone: timezone }).startOf("week");return {min: week.toJSDate(),- max: week.endOf("week").minus({ minutes: offset }).toJSDate(),+ max: lastBucketStart(week, week.plus({ weeks: 1 }), bucket),};}@@ -197,7 +199,7 @@ export const getChartTimeBounds = (time: Time, bucket: TimeBucket, timezone: str}).startOf("month");return {min: month.toJSDate(),- max: month.endOf("month").minus({ minutes: offset }).toJSDate(),+ max: lastBucketStart(month, month.plus({ months: 1 }), bucket),};}@@ -205,7 +207,7 @@ export const getChartTimeBounds = (time: Time, bucket: TimeBucket, timezone: strconst year = DateTime.fromISO(time.year, { zone: timezone }).startOf("year");return {min: year.toJSDate(),- max: year.endOf("year").minus({ minutes: offset }).toJSDate(),+ max: lastBucketStart(year, year.plus({ years: 1 }), bucket),};}@@ -215,16 +217,17 @@ export const getChartTimeBounds = (time: Time, bucket: TimeBucket, timezone: strzone: timezone,});const endExclusive = DateTime.fromISO(`${time.endDate}T${time.endTime}`, { zone: timezone });- const displayEnd = stepBucket(endExclusive, bucket, -1);return {min: start.toJSDate(),- max: (displayEnd > start ? displayEnd : endExclusive).toJSDate(),+ max: lastBucketStart(start, endExclusive, bucket),};}+ const start = DateTime.fromISO(time.startDate, { zone: timezone }).startOf("day");+ const endExclusive = DateTime.fromISO(time.endDate, { zone: timezone }).startOf("day").plus({ days: 1 });return {- min: DateTime.fromISO(time.startDate, { zone: timezone }).startOf("day").toJSDate(),- max: DateTime.fromISO(time.endDate, { zone: timezone }).endOf("day").minus({ minutes: offset }).toJSDate(),+ min: start.toJSDate(),+ max: lastBucketStart(start, endExclusive, bucket),};}