mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-18 08:02:12 +00:00
fix(correlations): date the earlier-engine note from the instance's first new run
The note for records whose findings predate the seasonally adjusted engine compared against a fixed calendar date, so findings an instance kept writing under the earlier engine until its deploy did not count. The first discovery sync of the new engine now stamps AppSettings.correlationSeasonalEngineSince before it writes; without a stamp every stored discovery pattern counts as an earlier finding. A restore from a portable export reset each pattern's createdAt to the restore day and dropped the note for good. createdAt now rides every export, and a file without it falls back to lastComputedAt.
This commit is contained in:
1 parent
587ea35d34
commit
0ea8d929d2
5 files changed
+250
-31
No files matched your search
@@ -3,25 +3,33 @@
|
||||
*
|
||||
* The flag decides who sees the one-time note on the correlation surface, so
|
||||
* it must hold only for a record that kept a discovery pattern from before the
|
||||
* engine change: not for a pattern written since, not for another account's
|
||||
* old pattern, and not for a non-discovery family.
|
||||
* engine change. The change is located by the instance's own marker
|
||||
* (`AppSettings.correlationSeasonalEngineSince`), which the first discovery
|
||||
* sync of the new engine stamps before it writes, not by a calendar date.
|
||||
*/
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
import { getPrismaClient, truncateAllTables } from "./setup";
|
||||
import {
|
||||
canonicalPatternKey,
|
||||
hadFindingsBeforeSeasonalAdjustment,
|
||||
PATTERN_FAMILIES,
|
||||
SEASONAL_ADJUSTMENT_SINCE,
|
||||
resetSeasonalEngineStampForTests,
|
||||
syncAcceptedPatterns,
|
||||
} from "@/lib/insights/correlation-patterns";
|
||||
import {
|
||||
buildProfileBackupSection,
|
||||
restoreProfileData,
|
||||
} from "@/lib/export/profile-backup";
|
||||
|
||||
vi.mock("@/lib/db-compat", () => ({
|
||||
ensureDbCompatibility: vi.fn().mockResolvedValue(undefined),
|
||||
}));
|
||||
|
||||
const DAY_MS = 24 * 60 * 60 * 1000;
|
||||
const BEFORE = new Date(SEASONAL_ADJUSTMENT_SINCE.getTime() - 30 * DAY_MS);
|
||||
const AFTER = new Date(SEASONAL_ADJUSTMENT_SINCE.getTime() + DAY_MS);
|
||||
const MARKER = new Date("2026-10-08T00:00:00.000Z");
|
||||
const BEFORE = new Date(MARKER.getTime() - 30 * DAY_MS);
|
||||
const AFTER = new Date(MARKER.getTime() + DAY_MS);
|
||||
|
||||
let seq = 0;
|
||||
async function seedUser() {
|
||||
@@ -40,40 +48,60 @@ async function seedPattern(userId: string, family: string, createdAt: Date) {
|
||||
await getPrismaClient().correlationPattern.create({
|
||||
data: {
|
||||
userId,
|
||||
canonicalKey: `p1:test-${seq}`,
|
||||
canonicalKey: canonicalPatternKey(`FACTOR_${seq}`, "MOOD", 1),
|
||||
family,
|
||||
factorKey: "ACTIVITY_STEPS",
|
||||
factorKey: `FACTOR_${seq}`,
|
||||
outcomeKey: "MOOD",
|
||||
lagDays: 1,
|
||||
sampleSize: 60,
|
||||
effectSize: 0.4,
|
||||
pValue: 0.001,
|
||||
qValue: 0.01,
|
||||
evidenceHash: `hash-${seq}`,
|
||||
evidenceHash: "a".repeat(64),
|
||||
lastComputedAt: createdAt,
|
||||
createdAt,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async function setMarker(at: Date | null) {
|
||||
await getPrismaClient().appSettings.upsert({
|
||||
where: { id: "singleton" },
|
||||
create: { id: "singleton", correlationSeasonalEngineSince: at },
|
||||
update: { correlationSeasonalEngineSince: at },
|
||||
});
|
||||
}
|
||||
|
||||
beforeEach(async () => {
|
||||
await truncateAllTables(getPrismaClient());
|
||||
resetSeasonalEngineStampForTests();
|
||||
});
|
||||
|
||||
describe("hadFindingsBeforeSeasonalAdjustment", () => {
|
||||
it("holds for a record with a discovery pattern stored before the change", async () => {
|
||||
it("counts every existing discovery pattern while the instance has no marker", async () => {
|
||||
const user = await seedUser();
|
||||
// Written by the earlier engine right up to the deploy: no fixed date
|
||||
// may exclude it.
|
||||
await seedPattern(user.id, PATTERN_FAMILIES.discoveryRetrospective, AFTER);
|
||||
expect(await hadFindingsBeforeSeasonalAdjustment(user.id)).toBe(true);
|
||||
});
|
||||
|
||||
it("holds for a record with a discovery pattern stored before the marker", async () => {
|
||||
await setMarker(MARKER);
|
||||
const user = await seedUser();
|
||||
await seedPattern(user.id, PATTERN_FAMILIES.discoveryRetrospective, BEFORE);
|
||||
expect(await hadFindingsBeforeSeasonalAdjustment(user.id)).toBe(true);
|
||||
});
|
||||
|
||||
it("holds for an early-detection pattern stored before the change", async () => {
|
||||
it("holds for an early-detection pattern stored before the marker", async () => {
|
||||
await setMarker(MARKER);
|
||||
const user = await seedUser();
|
||||
await seedPattern(user.id, PATTERN_FAMILIES.discoveryRecent, BEFORE);
|
||||
expect(await hadFindingsBeforeSeasonalAdjustment(user.id)).toBe(true);
|
||||
});
|
||||
|
||||
it("does not hold for a pattern first stored after the change", async () => {
|
||||
it("does not hold for a pattern first stored after the marker", async () => {
|
||||
await setMarker(MARKER);
|
||||
const user = await seedUser();
|
||||
await seedPattern(user.id, PATTERN_FAMILIES.discoveryRetrospective, AFTER);
|
||||
expect(await hadFindingsBeforeSeasonalAdjustment(user.id)).toBe(false);
|
||||
@@ -96,3 +124,123 @@ describe("hadFindingsBeforeSeasonalAdjustment", () => {
|
||||
expect(await hadFindingsBeforeSeasonalAdjustment(user.id)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("the first discovery sync of the new engine", () => {
|
||||
const evidence = {
|
||||
factorKey: "ACTIVITY_STEPS",
|
||||
outcomeKey: "SLEEP_DURATION",
|
||||
lagDays: 0,
|
||||
sampleSize: 90,
|
||||
effectSize: 0.35,
|
||||
pValue: 0.002,
|
||||
qValue: 0.02,
|
||||
};
|
||||
|
||||
it("stamps the marker before it writes, once", async () => {
|
||||
const earlier = await seedUser();
|
||||
await seedPattern(
|
||||
earlier.id,
|
||||
PATTERN_FAMILIES.discoveryRetrospective,
|
||||
BEFORE,
|
||||
);
|
||||
const fresh = await seedUser();
|
||||
|
||||
await syncAcceptedPatterns({
|
||||
userId: fresh.id,
|
||||
family: PATTERN_FAMILIES.discoveryRetrospective,
|
||||
accepted: [evidence],
|
||||
});
|
||||
|
||||
const settings = await getPrismaClient().appSettings.findUnique({
|
||||
where: { id: "singleton" },
|
||||
});
|
||||
const stamp = settings?.correlationSeasonalEngineSince;
|
||||
expect(stamp).toBeInstanceOf(Date);
|
||||
// The new engine's own finding is not an earlier one; the old one is.
|
||||
expect(await hadFindingsBeforeSeasonalAdjustment(fresh.id)).toBe(false);
|
||||
expect(await hadFindingsBeforeSeasonalAdjustment(earlier.id)).toBe(true);
|
||||
|
||||
// A later run (another process) leaves the first stamp standing.
|
||||
resetSeasonalEngineStampForTests();
|
||||
await syncAcceptedPatterns({
|
||||
userId: fresh.id,
|
||||
family: PATTERN_FAMILIES.discoveryRecent,
|
||||
accepted: [evidence],
|
||||
});
|
||||
const again = await getPrismaClient().appSettings.findUnique({
|
||||
where: { id: "singleton" },
|
||||
});
|
||||
expect(again?.correlationSeasonalEngineSince?.toISOString()).toBe(
|
||||
stamp?.toISOString(),
|
||||
);
|
||||
});
|
||||
|
||||
it("is not triggered by a non-discovery family", async () => {
|
||||
const user = await seedUser();
|
||||
await syncAcceptedPatterns({
|
||||
userId: user.id,
|
||||
family: PATTERN_FAMILIES.fixed,
|
||||
accepted: [evidence],
|
||||
});
|
||||
const settings = await getPrismaClient().appSettings.findUnique({
|
||||
where: { id: "singleton" },
|
||||
});
|
||||
expect(settings?.correlationSeasonalEngineSince ?? null).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("a restore", () => {
|
||||
it("keeps when a finding was first made, so the note survives it", async () => {
|
||||
await setMarker(MARKER);
|
||||
const user = await seedUser();
|
||||
await seedPattern(user.id, PATTERN_FAMILIES.discoveryRetrospective, BEFORE);
|
||||
const prisma = getPrismaClient();
|
||||
|
||||
// The portable export is the one that used to drop `createdAt`.
|
||||
const section = await buildProfileBackupSection(prisma, user.id, {
|
||||
purpose: "portable-export",
|
||||
});
|
||||
expect(section.correlationPatterns[0]?.createdAt).toBe(
|
||||
BEFORE.toISOString(),
|
||||
);
|
||||
|
||||
await prisma.$transaction((tx) =>
|
||||
restoreProfileData(tx, user.id, {
|
||||
healthProfile: section.healthProfile,
|
||||
healthProfileFacts: section.healthProfileFacts,
|
||||
customMetrics: section.customMetrics,
|
||||
correlationPatterns: section.correlationPatterns,
|
||||
}),
|
||||
);
|
||||
|
||||
const restored = await prisma.correlationPattern.findFirstOrThrow({
|
||||
where: { userId: user.id },
|
||||
});
|
||||
expect(restored.createdAt.toISOString()).toBe(BEFORE.toISOString());
|
||||
expect(await hadFindingsBeforeSeasonalAdjustment(user.id)).toBe(true);
|
||||
});
|
||||
|
||||
it("falls back to the last computation for a file without createdAt", async () => {
|
||||
await setMarker(MARKER);
|
||||
const user = await seedUser();
|
||||
await seedPattern(user.id, PATTERN_FAMILIES.discoveryRetrospective, BEFORE);
|
||||
const prisma = getPrismaClient();
|
||||
const section = await buildProfileBackupSection(prisma, user.id);
|
||||
const legacy = section.correlationPatterns.map((pattern) => {
|
||||
const copy = { ...pattern };
|
||||
delete copy.createdAt;
|
||||
return copy;
|
||||
});
|
||||
|
||||
await prisma.$transaction((tx) =>
|
||||
restoreProfileData(tx, user.id, {
|
||||
healthProfile: section.healthProfile,
|
||||
healthProfileFacts: section.healthProfileFacts,
|
||||
customMetrics: section.customMetrics,
|
||||
correlationPatterns: legacy,
|
||||
}),
|
||||
);
|
||||
|
||||
expect(await hadFindingsBeforeSeasonalAdjustment(user.id)).toBe(true);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user