Skip to content

OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.

ralph(M35-001): Anchor the mcp/chart fixtures to a fixed date, removing the midnight flake at source

ralphca9245606eef
diff --git a/packages/core/src/modules/chart/chart.service.test.ts b/packages/core/src/modules/chart/chart.service.test.ts
index 781e26294..b4b3399d6 100644
--- a/packages/core/src/modules/chart/chart.service.test.ts
+++ b/packages/core/src/modules/chart/chart.service.test.ts
@@ -5,13 +5,18 @@
* all-cohorts paths run against seeded `cohort_members` + a Postgres cohort.
*
* Fixture (test/fixtures.ts, seeded per suite under its own project id so it
- * can run concurrently with V1's suites): Alice — 3 events 2 days ago
+ * can run concurrently with V1's suites, with the clock pinned to
+ * test/fixture-clock.ts's 12:00 UTC anchor): Alice — 3 events 2 days ago
* (Chrome, page_view /home); Charlie — 5 events 5 days ago (Firefox,
* screen_view + page_view /shop, purchase 9900); Bob — no events. This suite
* adds one static cohort ("Firefox users") holding Charlie.
*/
import { afterAll, beforeAll, describe, expect, it, mock } from 'bun:test';
+import {
+ pinFixtureClock,
+ releaseFixtureClock,
+} from '../../../test/fixture-clock';
import type { IChartEventItem, IReportInput } from '../report/report.constants';
const TEST_PROJECT_ID = 'chart-integration-test';
@@ -30,6 +35,7 @@ mock.module('@openpanel/redis', () => ({
}));
let FIXTURE: typeof import('../../../../../test/fixtures').FIXTURE;
+let fixtureNow: Date;
let service: ReturnType<typeof import('./chart.service').createChartService>;
let ch: typeof import('@openpanel/db/src/clickhouse/client').ch;
@@ -73,6 +79,7 @@ beforeAll(async () => {
);
await bootstrapTestDatabases();
await fixtures.setupPostgresFixtures(TEST_PROJECT_ID, TEST_ORG_ID);
+ fixtureNow = pinFixtureClock();
await fixtures.setupFixtures(TEST_PROJECT_ID);
const { db } = await import('@openpanel/db/src/prisma-client');
@@ -113,6 +120,7 @@ afterAll(async () => {
await db.cohort.deleteMany({ where: { id: COHORT_ID } });
await fixtures.teardownFixtures(TEST_PROJECT_ID);
await fixtures.teardownPostgresFixtures(TEST_PROJECT_ID, TEST_ORG_ID);
+ releaseFixtureClock();
});
function seriesNamed<TSerie extends { names: string[] }>(
@@ -335,7 +343,9 @@ describe('getProjectCard', () => {
describe('getChartBucketProfiles', () => {
it('returns the profiles behind one data point, honoring breakdowns', async () => {
- const twoDaysAgo = new Date(Date.now() - 2 * DAY_MS).toISOString();
+ const twoDaysAgo = new Date(
+ fixtureNow.getTime() - 2 * DAY_MS
+ ).toISOString();
const profiles = await service.bucketProfiles({
projectId: TEST_PROJECT_ID,
date: twoDaysAgo,
@@ -358,7 +368,9 @@ describe('getChartBucketProfiles', () => {
});
it('selects only whitelisted profile columns for profile.* references', async () => {
- const twoDaysAgo = new Date(Date.now() - 2 * DAY_MS).toISOString();
+ const twoDaysAgo = new Date(
+ fixtureNow.getTime() - 2 * DAY_MS
+ ).toISOString();
const profiles = await service.bucketProfiles({
projectId: TEST_PROJECT_ID,
date: twoDaysAgo,
diff --git a/packages/core/src/modules/mcp/src/integration/tools.test.ts b/packages/core/src/modules/mcp/src/integration/tools.test.ts
index ac0f96664..03c3eb00f 100644
--- a/packages/core/src/modules/mcp/src/integration/tools.test.ts
+++ b/packages/core/src/modules/mcp/src/integration/tools.test.ts
@@ -8,7 +8,9 @@
* apps/api/src/routes/insights.router.test.ts uses) in beforeAll/afterAll,
* under its own project/org id so it can run concurrently with that suite.
*
- * Fixture data (test/fixtures.ts's FIXTURE):
+ * Fixture data (test/fixtures.ts's FIXTURE), seeded and queried with the clock
+ * pinned to test/fixture-clock.ts's 12:00 UTC anchor so "5 days ago" is one
+ * calendar day whatever time the suite runs:
* Alice — 3 events: session_start, page_view(/home), session_end — 2 days ago — country: US, browser: Chrome
* Bob — 0 events (inactive) — profile created 90 days ago — country: SE
* Charlie — 5 events: session_start, screen_view, page_view(/shop), purchase, session_end — 5 days ago — browser: Firefox
@@ -27,6 +29,10 @@
*/
import { afterAll, beforeAll, describe, expect, it, mock } from 'bun:test';
+import {
+ pinFixtureClock,
+ releaseFixtureClock,
+} from '../../../../../test/fixture-clock';
import type { McpToolDeps } from '../tools/shared';
const TEST_PROJECT_ID = 'mcp-integration-test';
@@ -97,6 +103,7 @@ beforeAll(async () => {
);
await bootstrapTestDatabases();
await fixtures.setupPostgresFixtures(TEST_PROJECT_ID, TEST_ORG_ID);
+ pinFixtureClock();
await fixtures.setupFixtures(TEST_PROJECT_ID);
// M15-003: the tools take `deps`/`services` as arguments, so the suite
@@ -117,6 +124,7 @@ afterAll(async () => {
const fixtures = await import('../../../../../../../test/fixtures');
await fixtures.teardownFixtures(TEST_PROJECT_ID);
await fixtures.teardownPostgresFixtures(TEST_PROJECT_ID, TEST_ORG_ID);
+ releaseFixtureClock();
});
const CTX = {
diff --git a/packages/core/test/MIDNIGHT_FLAKE_PROOF.md b/packages/core/test/MIDNIGHT_FLAKE_PROOF.md
new file mode 100644
index 000000000..ed42ed0b3
--- /dev/null
+++ b/packages/core/test/MIDNIGHT_FLAKE_PROOF.md
@@ -0,0 +1,191 @@
+# Midnight fixture flake — proof (M35-001)
+
+## The flake
+
+Around 00:00–00:25 UTC, three tests failed because `test/fixtures.ts` seeds at
+`Date.now() - N days - M minutes` while the assertions bucket by calendar day:
+
+- `get_profile_metrics > returns exact metrics for charlie` (`uniqueDaysActive` 1 vs 2)
+- `getChartBucketProfiles > returns the profiles behind one data point, honoring breakdowns`
+- `getChartBucketProfiles > selects only whitelisted profile columns for profile.* references`
+
+## The fix
+
+- `test/fixture-clock.ts`: `pinFixtureClock()` freezes `Date` (bun's
+ `setSystemTime`) at the latest 12:00 UTC not after now. `releaseFixtureClock()`
+ unfreezes it. Every seeded offset then lands inside one calendar day.
+- `chart.service.test.ts` and `mcp/src/integration/tools.test.ts` pin the clock
+ before `setupFixtures` and release it in `afterAll`. The two
+ `getChartBucketProfiles` cases compute "two days ago" from `fixtureNow`, not
+ `Date.now()`.
+- `test/fixture-clock.test.ts` unit-tests the anchor. Its last case shows that
+ charlie's seeded span covers two calendar days at 00:10 under the raw clock
+ and one day under the anchor.
+- `test/midnight-window.test.ts` re-runs both suites in child processes with the
+ wall clock frozen at 00:01, 00:10 and 00:19 UTC, via
+ `test/wall-clock-preload.ts`. It runs in every `bun run test`, so this proof
+ is repeated on every run, not just recorded once.
+
+The anchor is a time of day, not a fixed calendar date. Some services compare
+against ClickHouse's own `now()` (the 3-month project card, `inactiveDays`,
+last-seen buckets), and bun cannot fake that clock. The anchor is never in the
+future and never more than 24 h old, so every `now`-relative window still
+contains the fixtures.
+
+**Limitation:** the in-window runs below fake only the JS clock. ClickHouse
+`now()` was the real time (≈11:11 UTC on 2026-09-16), so the JS-to-ClickHouse
+clock gap in these runs is ≈23 h. In a real 00:10 run the gap is ≈12 h. Both
+gaps are inside the 24 h bound above.
+
+All runs below were executed on 2026-09-16 on this box, from
+`packages/core`. `test/preload.ts` (bunfig `[test] preload`, also imported by
+`wall-clock-preload.ts`) pins the infra URLs to the isolated
+`openpanel_test` Postgres/ClickHouse databases. Output is filtered with
+`grep -E "wall clock pinned|\(fail\)|^ *[0-9]+ (pass|fail)|expect\(\) calls|^Ran "`.
+
+## 1. Clock faked INTO the 00:00–00:25 UTC window — passing
+
+Command (run 2026-09-16T11:11:01Z–11:11:30Z):
+
+```bash
+cd packages/core
+for t in 2026-09-16T00:01:00.000Z 2026-09-16T00:10:00.000Z 2026-09-16T00:19:00.000Z; do
+ FIXTURE_WALL_CLOCK_ISO=$t \
+ BUN_OPTIONS="--preload=$PWD/test/wall-clock-preload.ts" \
+ bun test --isolate src/modules/chart/chart.service.test.ts \
+ src/modules/mcp/src/integration/tools.test.ts
+done
+```
+
+Output:
+
+```text
+=== FIXTURE_WALL_CLOCK_ISO=2026-09-16T00:01:00.000Z
+wall clock pinned at 2026-09-16T00:01:00.000Z
+wall clock pinned at 2026-09-16T00:01:00.000Z
+ 54 pass
+ 0 fail
+ 160 expect() calls
+Ran 54 tests across 2 files. [10.26s]
+exit=0
+=== FIXTURE_WALL_CLOCK_ISO=2026-09-16T00:10:00.000Z
+wall clock pinned at 2026-09-16T00:10:00.000Z
+wall clock pinned at 2026-09-16T00:10:00.000Z
+ 54 pass
+ 0 fail
+ 160 expect() calls
+Ran 54 tests across 2 files. [10.05s]
+exit=0
+=== FIXTURE_WALL_CLOCK_ISO=2026-09-16T00:19:00.000Z
+wall clock pinned at 2026-09-16T00:19:00.000Z
+wall clock pinned at 2026-09-16T00:19:00.000Z
+ 54 pass
+ 0 fail
+ 160 expect() calls
+Ran 54 tests across 2 files. [9.14s]
+exit=0
+```
+
+The `wall clock pinned at` line is printed once per test file (`--isolate`).
+It shows that each suite ran with the faked time.
+
+## 2. The same suites at a normal hour — passing
+
+Command (real clock, run 2026-09-16T11:11:35Z–11:11:45Z):
+
+```bash
+cd packages/core
+bun test --isolate src/modules/chart/chart.service.test.ts \
+ src/modules/mcp/src/integration/tools.test.ts
+```
+
+Output:
+
+```text
+ 54 pass
+ 0 fail
+ 160 expect() calls
+Ran 54 tests across 2 files. [9.87s]
+exit=0
+```
+
+## 3. Negative control — the same in-window runs WITHOUT the fix
+
+The two suite files were temporarily replaced with their `HEAD` versions
+(`git show HEAD:<file> > <file>`) and restored afterwards. The rest of the
+command matches section 1 (run 2026-09-16T11:11:51Z–11:12:24Z). Each run fails
+on exactly the documented flake tests and nothing else:
+
+```text
+=== FIXTURE_WALL_CLOCK_ISO=2026-09-16T00:01:00.000Z (fix reverted)
+(fail) getChartBucketProfiles > returns the profiles behind one data point, honoring breakdowns [11.33ms]
+(fail) getChartBucketProfiles > selects only whitelisted profile columns for profile.* references [9.43ms]
+(fail) get_profile_metrics > returns exact metrics for charlie [17.57ms]
+ 51 pass
+ 3 fail
+Ran 54 tests across 2 files. [11.09s]
+exit=1
+=== FIXTURE_WALL_CLOCK_ISO=2026-09-16T00:10:00.000Z (fix reverted)
+(fail) get_profile_metrics > returns exact metrics for charlie [26.26ms]
+ 53 pass
+ 1 fail
+Ran 54 tests across 2 files. [10.98s]
+exit=1
+=== FIXTURE_WALL_CLOCK_ISO=2026-09-16T00:19:00.000Z (fix reverted)
+(fail) get_profile_metrics > returns exact metrics for charlie [15.67ms]
+ 53 pass
+ 1 fail
+Ran 54 tests across 2 files. [10.79s]
+exit=1
+```
+
+So the fake clock does reproduce the flake, and the passes in section 1 come
+from the fix.
+
+## 4. The committed guard tests
+
+Command (run 2026-09-16T11:12:32Z–11:13:04Z):
+
+```bash
+cd packages/core
+bun test --isolate test/fixture-clock.test.ts test/midnight-window.test.ts
+```
+
+Output:
+
+```text
+ 6 pass
+ 0 fail
+ 18 expect() calls
+Ran 6 tests across 2 files. [32.45s]
+exit=0
+```
+
+## 5. Full gate on this tree
+
+`verification/full.sh` was run from the controller, 2026-09-16T11:13:41Z to
+11:19:12Z, with exit 0. Excerpt:
+
+```text
+OK: bun run test
+GOLDEN: the only differing case(s) are known non-deterministic holes: insights-event-property-values
+GOLDEN: every other case matched byte-exact. Passing.
+OK: golden/compare
+OK: golden/openapi v2-diff.sh
+FULL: green
+```
+
+`openpanel.events` on the prod copy read 321,191,947 before this run and
+321,191,957 after. The e2e ingestion added rows, and none were lost.
+
+## What this does not cover
+
+- The fix covers only the two suites that seed `test/fixtures.ts` from
+ packages/core: `chart.service.test.ts` and `mcp/src/integration/tools.test.ts`.
+ `test/fixtures.ts` is unchanged, and no other consumer of it (V1's root
+ suites, apps/api) was touched.
+- `verification/midnight-flake` (the M34-001 guard) is unchanged. It is still
+ the safety net.
+- The children in `midnight-window.test.ts` use the same fixture project ids
+ as the suites they re-run. This is safe only because bun runs test files one
+ at a time.
diff --git a/packages/core/test/fixture-clock.test.ts b/packages/core/test/fixture-clock.test.ts
new file mode 100644
index 000000000..4a7187682
--- /dev/null
+++ b/packages/core/test/fixture-clock.test.ts
@@ -0,0 +1,46 @@
+import { describe, expect, it } from 'bun:test';
+import { fixtureAnchor } from './fixture-clock';
+
+const MINUTE_MS = 60_000;
+const DAY_MS = 24 * 60 * MINUTE_MS;
+// test/fixtures.ts seeds charlie's events at `now - 5d - {20..0} min`.
+const CHARLIE_DAYS_AGO = 5;
+const CHARLIE_SPREAD_MINUTES = 20;
+
+function utcDay(epochMs: number): string {
+ return new Date(epochMs).toISOString().slice(0, 10);
+}
+
+function charlieSeedDays(now: Date): Set<string> {
+ const newest = now.getTime() - CHARLIE_DAYS_AGO * DAY_MS;
+ return new Set([
+ utcDay(newest - CHARLIE_SPREAD_MINUTES * MINUTE_MS),
+ utcDay(newest),
+ ]);
+}
+
+describe('fixtureAnchor', () => {
+ it('is today 12:00 UTC from noon onward', () => {
+ expect(fixtureAnchor(new Date('2026-09-16T12:00:00Z')).toISOString()).toBe(
+ '2026-09-16T12:00:00.000Z'
+ );
+ expect(fixtureAnchor(new Date('2026-09-16T23:59:59Z')).toISOString()).toBe(
+ '2026-09-16T12:00:00.000Z'
+ );
+ });
+
+ it('is yesterday 12:00 UTC before noon, so it never lies in the future', () => {
+ expect(fixtureAnchor(new Date('2026-09-16T00:10:00Z')).toISOString()).toBe(
+ '2026-09-15T12:00:00.000Z'
+ );
+ expect(fixtureAnchor(new Date('2026-09-16T11:59:59Z')).toISOString()).toBe(
+ '2026-09-15T12:00:00.000Z'
+ );
+ });
+
+ it('keeps a seeded day in one calendar day where the raw clock splits it', () => {
+ const insideMidnightWindow = new Date('2026-09-16T00:10:00Z');
+ expect(charlieSeedDays(insideMidnightWindow).size).toBe(2);
+ expect(charlieSeedDays(fixtureAnchor(insideMidnightWindow)).size).toBe(1);
+ });
+});
diff --git a/packages/core/test/fixture-clock.ts b/packages/core/test/fixture-clock.ts
new file mode 100644
index 000000000..0a62ef0df
--- /dev/null
+++ b/packages/core/test/fixture-clock.ts
@@ -0,0 +1,34 @@
+// The clock that seeds and queries the shared test/fixtures.ts dataset.
+//
+// The seeder places events at `now - N days - M minutes` and several tests
+// bucket by calendar day, so a `now` within minutes of UTC midnight splits one
+// seeded day across two buckets. Pinning `now` to a fixed time of day keeps
+// every seeded offset inside its own day, whatever time the suite runs.
+//
+// The anchor is a time of day rather than a fixed calendar date because some
+// services under test compare against ClickHouse's own `now()` (the 3-month
+// project card, `inactiveDays`, last-seen buckets), which this cannot fake.
+import { setSystemTime } from 'bun:test';
+
+const FIXTURE_ANCHOR_HOUR_UTC = 12;
+
+/** The latest 12:00 UTC not after `now`, so no fixture lands in ClickHouse's future. */
+export function fixtureAnchor(now: Date = new Date()): Date {
+ const anchor = new Date(now);
+ anchor.setUTCHours(FIXTURE_ANCHOR_HOUR_UTC, 0, 0, 0);
+ if (anchor > now) {
+ anchor.setUTCDate(anchor.getUTCDate() - 1);
+ }
+ return anchor;
+}
+
+/** Freezes `Date` at the fixture anchor; call before seeding. */
+export function pinFixtureClock(): Date {
+ const anchor = fixtureAnchor();
+ setSystemTime(anchor);
+ return anchor;
+}
+
+export function releaseFixtureClock(): void {
+ setSystemTime();
+}
diff --git a/packages/core/test/midnight-window.test.ts b/packages/core/test/midnight-window.test.ts
new file mode 100644
index 000000000..b2f943f27
--- /dev/null
+++ b/packages/core/test/midnight-window.test.ts
@@ -0,0 +1,59 @@
+// Proof that the midnight fixture flake is gone: runs the two suites it hit
+// (see test/fixture-clock.ts) in child processes whose wall clock is frozen
+// inside the 00:00-00:25 UTC window where they used to fail.
+//
+// The children share the suites' fixture project ids, so they run one at a
+// time; bun runs test files sequentially, so no sibling file overlaps them.
+import { describe, expect, it } from 'bun:test';
+import { spawnSync } from 'bun';
+import { join } from 'node:path';
+import { WALL_CLOCK_PINNED_MARKER } from './wall-clock.constants';
+
+const CORE_DIR = join(import.meta.dir, '..');
+const WALL_CLOCK_PRELOAD = join(import.meta.dir, 'wall-clock-preload.ts');
+const MIDNIGHT_FLAKE_SUITES = [
+ 'src/modules/chart/chart.service.test.ts',
+ 'src/modules/mcp/src/integration/tools.test.ts',
+];
+const MIDNIGHT_FLAKE_SUITE_TEST_COUNT = 54;
+// 00:01 is inside the chart pair's window, 00:10 and 00:19 inside charlie's.
+const IN_WINDOW_WALL_CLOCKS = [
+ '2026-09-16T00:01:00.000Z',
+ '2026-09-16T00:10:00.000Z',
+ '2026-09-16T00:19:00.000Z',
+];
+const CHILD_RUN_TIMEOUT_MS = 120_000;
+
+function runSuitesAt(wallClockIso: string) {
+ const child = spawnSync(
+ ['bun', 'test', '--isolate', ...MIDNIGHT_FLAKE_SUITES],
+ {
+ cwd: CORE_DIR,
+ env: {
+ ...process.env,
+ BUN_OPTIONS: `--preload=${WALL_CLOCK_PRELOAD}`,
+ FIXTURE_WALL_CLOCK_ISO: wallClockIso,
+ },
+ }
+ );
+ return {
+ exitCode: child.exitCode,
+ output: `${child.stdout.toString()}${child.stderr.toString()}`,
+ };
+}
+
+describe('mcp/chart fixture suites inside the midnight window', () => {
+ for (const wallClockIso of IN_WINDOW_WALL_CLOCKS) {
+ it(
+ `pass with the wall clock at ${wallClockIso}`,
+ () => {
+ const { exitCode, output } = runSuitesAt(wallClockIso);
+ expect(output).toContain(`${WALL_CLOCK_PINNED_MARKER} ${wallClockIso}`);
+ expect(output).toContain(` ${MIDNIGHT_FLAKE_SUITE_TEST_COUNT} pass`);
+ expect(output).toContain(' 0 fail');
+ expect(exitCode).toBe(0);
+ },
+ CHILD_RUN_TIMEOUT_MS
+ );
+ }
+});
diff --git a/packages/core/test/wall-clock-preload.ts b/packages/core/test/wall-clock-preload.ts
new file mode 100644
index 000000000..85f4588b7
--- /dev/null
+++ b/packages/core/test/wall-clock-preload.ts
@@ -0,0 +1,18 @@
+// Preloaded (BUN_OPTIONS=--preload) only by midnight-window.test.ts's child
+// runs: freezes the wall clock at FIXTURE_WALL_CLOCK_ISO before any suite
+// code loads, so the fixtures see the time of day the flake needs.
+//
+// Core's own preload is imported first because a CLI preload may replace
+// bunfig's, and that one pins every test at the isolated openpanel_test databases.
+import { setSystemTime } from 'bun:test';
+import './preload';
+import { WALL_CLOCK_PINNED_MARKER } from './wall-clock.constants';
+
+const wallClockIso = process.env.FIXTURE_WALL_CLOCK_ISO;
+if (!wallClockIso) {
+ throw new Error('FIXTURE_WALL_CLOCK_ISO is required');
+}
+setSystemTime(new Date(wallClockIso));
+process.stderr.write(
+ `${WALL_CLOCK_PINNED_MARKER} ${new Date().toISOString()}\n`
+);
diff --git a/packages/core/test/wall-clock.constants.ts b/packages/core/test/wall-clock.constants.ts
new file mode 100644
index 000000000..ed3ba5d85
--- /dev/null
+++ b/packages/core/test/wall-clock.constants.ts
@@ -0,0 +1,2 @@
+/** Printed by wall-clock-preload.ts so a child run proves its clock was faked. */
+export const WALL_CLOCK_PINNED_MARKER = 'wall clock pinned at';