/** * earth-now.co * Author: Simon-Pierre Boucher * Contact: contact@spboucher.ai * File: packages/counter/test/consistency.test.ts * Purpose: Client/server consistency — jsdom (browser-like) and a spawned Node process must produce bit-identical values * * @vitest-environment jsdom */ import { execFileSync } from "node:child_process"; import { fileURLToPath } from "node:url"; import { dirname, join } from "node:path"; import { describe, expect, it } from "vitest"; import { type CounterModel, counterValue } from "../src/index"; const MODELS: CounterModel[] = [ { metricId: "consistency_linear", anchorValue: 12345.678, anchorTime: "2026-01-01T00:00:00.000Z", rateFn: { kind: "linear", perSecond: 1.2345 }, observedAt: "2026-01-01T00:00:00.000Z", sourceId: "test", modelVersion: "test-v1", displayHints: { decimals: 0, unit: "u" }, }, { metricId: "consistency_seasonal", anchorValue: 1e9, anchorTime: "2026-01-01T00:00:00.000Z", rateFn: { kind: "seasonal", base: 4.17, harmonics: [ { period: "year", order: 1, amplitude: 0.146, phase: 1.8449 }, { period: "day", order: 1, amplitude: 0.02, phase: 2.88 }, ], }, observedAt: "2025-07-01T00:00:00.000Z", sourceId: "test", modelVersion: "test-v1", displayHints: { decimals: 0, unit: "u" }, }, { metricId: "consistency_spline", anchorValue: 8231000000, anchorTime: "2025-07-01T00:00:00.000Z", rateFn: { kind: "spline", knots: [ ["2023-07-01T00:00:00.000Z", 8091000000], ["2024-07-01T00:00:00.000Z", 8161000000], ["2025-07-01T00:00:00.000Z", 8231000000], ["2026-07-01T00:00:00.000Z", 8299000000], ], }, observedAt: "2025-07-01T00:00:00.000Z", sourceId: "test", modelVersion: "test-v1", displayHints: { decimals: 0, unit: "u" }, }, ]; const INSTANTS = [ "2026-01-01T00:00:00.000Z", "2026-03-14T15:09:26.535Z", "2026-08-09T12:00:00.000Z", "2026-12-31T23:59:59.999Z", "2027-06-15T06:30:00.000Z", ].map((iso) => Date.parse(iso)); describe("client/server consistency (bit-identical values)", () => { it("runs under jsdom", () => { expect(typeof document).toBe("object"); }); it("jsdom and a separate Node process agree exactly on every model × instant", () => { const here = counterValue !== undefined; expect(here).toBe(true); const inJsdom = MODELS.map((m) => INSTANTS.map((t) => counterValue(m, t))); // Evaluate the SAME models in a plain Node child process against the built dist. const distIndex = join(dirname(fileURLToPath(import.meta.url)), "..", "dist", "index.js"); const script = ` const { counterValue } = await import(${JSON.stringify(distIndex)}); const models = ${JSON.stringify(MODELS)}; const instants = ${JSON.stringify(INSTANTS)}; const out = models.map((m) => instants.map((t) => counterValue(m, t))); console.log(JSON.stringify(out)); `; const stdout = execFileSync(process.execPath, ["--input-type=module", "-e", script], { encoding: "utf8", }); const inNode = JSON.parse(stdout) as number[][]; // JSON round-trips finite doubles exactly (shortest round-trip repr), so // toEqual here really is bit-identity. expect(inNode).toEqual(inJsdom); for (let i = 0; i < MODELS.length; i++) { for (let j = 0; j < INSTANTS.length; j++) { expect(Object.is(inNode[i]![j]!, inJsdom[i]![j]!)).toBe(true); } } }); });