/** * earth-now.co * Author: Simon-Pierre Boucher * Contact: contact@spboucher.ai * File: apps/web/test/consistency-shared.ts * Purpose: Shared fixture for the client/server consistency tests — models, instants and hand-derivable closed-form expected values */ import { SEASONAL_EPOCH_MS, YEAR_SECONDS, parseIsoUtc, type CounterModel, } from "@earth-now/counter"; export const ANCHOR_ISO = "2026-01-01T00:00:00.000Z"; /** Simple linear model — closed-form value is hand-derivable. */ export const LINEAR_MODEL: CounterModel = { metricId: "consistency_linear", anchorValue: 8_231_613_070, anchorTime: ANCHOR_ISO, rateFn: { kind: "linear", perSecond: 2.3 }, observedAt: "2025-07-01T00:00:00.000Z", sourceId: "un_wpp_2024", modelVersion: "linear-stock-v1", displayHints: { decimals: 0, sigFigs: 7, unit: "people" }, }; /** Seasonal model with one annual harmonic — analytic integral, exact everywhere. */ export const SEASONAL_MODEL: CounterModel = { metricId: "consistency_seasonal", anchorValue: 0, anchorTime: ANCHOR_ISO, rateFn: { kind: "seasonal", base: 4.3, harmonics: [{ period: "year", order: 1, amplitude: 0.5, phase: 0.25 }], }, observedAt: "2025-12-01T00:00:00.000Z", sourceId: "un_wpp_2024", modelVersion: "seasonal-ytd-v1", displayHints: { decimals: 0, unit: "births" }, }; /** Five instants across the year, including the anchor itself. */ export const INSTANTS: readonly number[] = [ Date.UTC(2026, 0, 1, 0, 0, 0), Date.UTC(2026, 1, 15, 12, 0, 0), Date.UTC(2026, 5, 30, 23, 59, 59), Date.UTC(2026, 7, 9, 7, 30, 0), Date.UTC(2026, 11, 31, 23, 59, 59, 999), ]; /** * Closed-form linear value, replicating the exact arithmetic (and operation * order) of counterValue for kind=linear — provably v = a + r·Δt. */ export function linearClosedForm(tMs: number): number { const anchorMs = parseIsoUtc(LINEAR_MODEL.anchorTime); const rateFn = LINEAR_MODEL.rateFn; if (rateFn.kind !== "linear") throw new Error("fixture must be linear"); return LINEAR_MODEL.anchorValue + (rateFn.perSecond * (tMs - anchorMs)) / 1000; } /** * Closed-form seasonal value: v(t) = a + base·(τ₁−τ₀) + (A/ω)·(sin(ωτ₁+φ) − sin(ωτ₀+φ)), * with ω = 2π·order / YEAR_SECONDS and τ measured from the seasonal epoch — * the analytic integral of the declared rate, in the same operation order as the runtime. */ export function seasonalClosedForm(tMs: number): number { const rateFn = SEASONAL_MODEL.rateFn; if (rateFn.kind !== "seasonal") throw new Error("fixture must be seasonal"); const anchorMs = parseIsoUtc(SEASONAL_MODEL.anchorTime); const tau0 = (anchorMs - SEASONAL_EPOCH_MS) / 1000; const tau1 = (tMs - SEASONAL_EPOCH_MS) / 1000; let v = SEASONAL_MODEL.anchorValue + rateFn.base * (tau1 - tau0); for (const h of rateFn.harmonics) { const omega = (2 * Math.PI * h.order) / YEAR_SECONDS; v += (h.amplitude / omega) * (Math.sin(omega * tau1 + h.phase) - Math.sin(omega * tau0 + h.phase)); } return v; }