mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-19 00:12:18 +00:00
fix(backups): preserve canonical recovery integrity
This commit is contained in:
1 parent
5f4017d039
commit
f8b0a64d69
11 files changed
+853
-615
No files matched your search
@@ -383,3 +383,135 @@ describe("backupPayloadSchema — v1.28 backup-completeness domains", () => {
|
||||
expect(summary.documents).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe("backupPayloadSchema — database enum boundaries", () => {
|
||||
const enumPayload = {
|
||||
schemaVersion: "1",
|
||||
exportedAt: "2026-07-20T00:00:00.000Z",
|
||||
userId: "u1",
|
||||
measurements: [
|
||||
{
|
||||
id: "measurement-1",
|
||||
type: "WEIGHT",
|
||||
value: 75,
|
||||
unit: "kg",
|
||||
measuredAt: "2026-07-19T07:00:00.000Z",
|
||||
source: "MANUAL",
|
||||
},
|
||||
],
|
||||
intakeEvents: [
|
||||
{
|
||||
medication: "Example",
|
||||
scheduledFor: "2026-07-19T08:00:00.000Z",
|
||||
source: "WEB",
|
||||
},
|
||||
],
|
||||
cycleProfile: {
|
||||
goal: "GENERAL_HEALTH",
|
||||
secondarySymptom: "MUCUS",
|
||||
},
|
||||
cycleDayLogs: [
|
||||
{
|
||||
date: "2026-07-19",
|
||||
flow: "MEDIUM",
|
||||
ovulationTest: "NEGATIVE",
|
||||
cervicalMucus: "CREAMY",
|
||||
cervixPosition: "LOW",
|
||||
cervixFirmness: "FIRM",
|
||||
cervixOpening: "CLOSED",
|
||||
pregnancyTest: "NEGATIVE",
|
||||
progesteroneTest: "INDETERMINATE",
|
||||
contraceptive: "NONE",
|
||||
source: "MANUAL",
|
||||
},
|
||||
],
|
||||
illnessEpisodes: [
|
||||
{
|
||||
id: "illness-1",
|
||||
label: "Cold",
|
||||
type: "INFECTION",
|
||||
lifecycle: "ACUTE",
|
||||
onsetAt: "2026-07-01T00:00:00.000Z",
|
||||
},
|
||||
],
|
||||
allergies: [
|
||||
{
|
||||
id: "allergy-1",
|
||||
substance: "Penicillin",
|
||||
category: "MEDICATION",
|
||||
type: "ALLERGY",
|
||||
severity: "SEVERE",
|
||||
status: "ACTIVE",
|
||||
},
|
||||
],
|
||||
familyHistory: [
|
||||
{
|
||||
id: "family-1",
|
||||
relationship: "MOTHER",
|
||||
condition: "Diabetes",
|
||||
},
|
||||
],
|
||||
workouts: [
|
||||
{
|
||||
id: "workout-1",
|
||||
sportType: "RUNNING",
|
||||
startedAt: "2026-07-19T06:00:00.000Z",
|
||||
endedAt: "2026-07-19T06:30:00.000Z",
|
||||
durationSec: 1800,
|
||||
source: "APPLE_HEALTH",
|
||||
},
|
||||
],
|
||||
documents: [
|
||||
{
|
||||
id: "document-1",
|
||||
kind: "LAB_RESULT",
|
||||
mimeType: "application/pdf",
|
||||
byteSize: 42,
|
||||
status: "STORED",
|
||||
summaryState: "NONE",
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
const enumPaths: Array<[string, Array<string | number>]> = [
|
||||
["measurement type", ["measurements", 0, "type"]],
|
||||
["measurement source", ["measurements", 0, "source"]],
|
||||
["intake source", ["intakeEvents", 0, "source"]],
|
||||
["cycle goal", ["cycleProfile", "goal"]],
|
||||
["cycle secondary symptom", ["cycleProfile", "secondarySymptom"]],
|
||||
["cycle flow", ["cycleDayLogs", 0, "flow"]],
|
||||
["cycle ovulation test", ["cycleDayLogs", 0, "ovulationTest"]],
|
||||
["cycle cervical mucus", ["cycleDayLogs", 0, "cervicalMucus"]],
|
||||
["cycle cervix position", ["cycleDayLogs", 0, "cervixPosition"]],
|
||||
["cycle cervix firmness", ["cycleDayLogs", 0, "cervixFirmness"]],
|
||||
["cycle cervix opening", ["cycleDayLogs", 0, "cervixOpening"]],
|
||||
["cycle pregnancy test", ["cycleDayLogs", 0, "pregnancyTest"]],
|
||||
["cycle progesterone test", ["cycleDayLogs", 0, "progesteroneTest"]],
|
||||
["cycle contraceptive", ["cycleDayLogs", 0, "contraceptive"]],
|
||||
["cycle source", ["cycleDayLogs", 0, "source"]],
|
||||
["illness type", ["illnessEpisodes", 0, "type"]],
|
||||
["illness lifecycle", ["illnessEpisodes", 0, "lifecycle"]],
|
||||
["allergy category", ["allergies", 0, "category"]],
|
||||
["allergy type", ["allergies", 0, "type"]],
|
||||
["allergy severity", ["allergies", 0, "severity"]],
|
||||
["allergy status", ["allergies", 0, "status"]],
|
||||
["family relationship", ["familyHistory", 0, "relationship"]],
|
||||
["workout source", ["workouts", 0, "source"]],
|
||||
["document kind", ["documents", 0, "kind"]],
|
||||
["document status", ["documents", 0, "status"]],
|
||||
["document summary state", ["documents", 0, "summaryState"]],
|
||||
];
|
||||
|
||||
it.each(enumPaths)("rejects an unsupported %s", (_label, path) => {
|
||||
const candidate = structuredClone(enumPayload);
|
||||
let cursor: Record<PropertyKey, unknown> = candidate;
|
||||
for (const segment of path.slice(0, -1)) {
|
||||
cursor = cursor[segment] as Record<PropertyKey, unknown>;
|
||||
}
|
||||
cursor[path.at(-1)!] = "UNSUPPORTED_BACKUP_ENUM";
|
||||
|
||||
const parsed = backupPayloadSchema.safeParse(candidate);
|
||||
|
||||
expect(parsed.success).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -20,6 +20,32 @@
|
||||
* a useful summary for them.
|
||||
*/
|
||||
import { z } from "zod/v4";
|
||||
import {
|
||||
AllergyCategory,
|
||||
AllergySeverity,
|
||||
AllergyStatus,
|
||||
AllergyType,
|
||||
CervicalMucus,
|
||||
CervixFirmness,
|
||||
CervixOpening,
|
||||
CervixPosition,
|
||||
ContraceptiveKind,
|
||||
CycleTrackingGoal,
|
||||
DocumentSummaryState,
|
||||
FamilyRelationship,
|
||||
FlowLevel,
|
||||
HomeTestResult,
|
||||
IllnessLifecycle,
|
||||
IllnessType,
|
||||
InboundDocumentKind,
|
||||
InboundDocumentStatus,
|
||||
IntakeSource,
|
||||
MeasurementSource,
|
||||
MeasurementType,
|
||||
OvulationTest,
|
||||
SecondarySymptom,
|
||||
} from "@/generated/prisma/enums";
|
||||
|
||||
|
||||
export const BACKUP_SCHEMA_VERSION = "1" as const;
|
||||
|
||||
@@ -32,12 +58,14 @@ const isoDateTime = z
|
||||
|
||||
const measurementSchema = z
|
||||
.object({
|
||||
type: z.string().min(1),
|
||||
id: z.string().min(1).optional(),
|
||||
type: z.enum(MeasurementType),
|
||||
value: z.number(),
|
||||
unit: z.string().min(1),
|
||||
measuredAt: isoDateTime,
|
||||
source: z.string().min(1).optional(),
|
||||
source: z.enum(MeasurementSource).optional(),
|
||||
notes: z.string().nullable().optional(),
|
||||
deletedAt: isoDateTime.nullable().optional(),
|
||||
})
|
||||
.passthrough();
|
||||
|
||||
@@ -65,7 +93,7 @@ const intakeEventSchema = z
|
||||
scheduledFor: isoDateTime,
|
||||
takenAt: isoDateTime.nullable().optional(),
|
||||
skipped: z.boolean().optional(),
|
||||
source: z.string().min(1).optional(),
|
||||
source: z.enum(IntakeSource).optional(),
|
||||
})
|
||||
.passthrough();
|
||||
|
||||
@@ -114,6 +142,7 @@ const moodEntrySchema = z
|
||||
source: z.string().min(1).optional(),
|
||||
externalId: z.string().nullable().optional(),
|
||||
loggedAt: isoDateTime,
|
||||
deletedAt: isoDateTime.nullable().optional(),
|
||||
factors: z.array(moodFactorSchema).default([]),
|
||||
})
|
||||
.passthrough();
|
||||
@@ -148,23 +177,23 @@ const cycleSpanSchema = z
|
||||
const cycleDayLogSchema = z
|
||||
.object({
|
||||
date: z.string().min(1),
|
||||
flow: z.string().nullable().optional(),
|
||||
flow: z.enum(FlowLevel).nullable().optional(),
|
||||
intermenstrualBleeding: z.boolean().optional(),
|
||||
basalBodyTempC: z.number().nullable().optional(),
|
||||
temperatureExcluded: z.boolean().optional(),
|
||||
ovulationTest: z.string().nullable().optional(),
|
||||
cervicalMucus: z.string().nullable().optional(),
|
||||
cervixPosition: z.string().nullable().optional(),
|
||||
cervixFirmness: z.string().nullable().optional(),
|
||||
cervixOpening: z.string().nullable().optional(),
|
||||
ovulationTest: z.enum(OvulationTest).nullable().optional(),
|
||||
cervicalMucus: z.enum(CervicalMucus).nullable().optional(),
|
||||
cervixPosition: z.enum(CervixPosition).nullable().optional(),
|
||||
cervixFirmness: z.enum(CervixFirmness).nullable().optional(),
|
||||
cervixOpening: z.enum(CervixOpening).nullable().optional(),
|
||||
sexualActivity: z.boolean().optional(),
|
||||
protectedSex: z.boolean().nullable().optional(),
|
||||
pregnancyTest: z.string().nullable().optional(),
|
||||
progesteroneTest: z.string().nullable().optional(),
|
||||
contraceptive: z.string().nullable().optional(),
|
||||
pregnancyTest: z.enum(HomeTestResult).nullable().optional(),
|
||||
progesteroneTest: z.enum(HomeTestResult).nullable().optional(),
|
||||
contraceptive: z.enum(ContraceptiveKind).nullable().optional(),
|
||||
sensitiveEncrypted: z.string().nullable().optional(),
|
||||
notesEncrypted: z.string().nullable().optional(),
|
||||
source: z.string().min(1).optional(),
|
||||
source: z.enum(MeasurementSource).optional(),
|
||||
externalId: z.string().nullable().optional(),
|
||||
tz: z.string().nullable().optional(),
|
||||
symptomKeys: z.array(z.string()).default([]),
|
||||
@@ -174,12 +203,12 @@ const cycleDayLogSchema = z
|
||||
/** Cycle-tracking preferences (one row per user). */
|
||||
const cycleProfileSchema = z
|
||||
.object({
|
||||
goal: z.string().min(1).optional(),
|
||||
goal: z.enum(CycleTrackingGoal).optional(),
|
||||
cycleTrackingEnabled: z.boolean().nullable().optional(),
|
||||
typicalCycleLength: z.number().int().nullable().optional(),
|
||||
typicalPeriodLength: z.number().int().nullable().optional(),
|
||||
lutealPhaseLength: z.number().int().nullable().optional(),
|
||||
secondarySymptom: z.string().nullable().optional(),
|
||||
secondarySymptom: z.enum(SecondarySymptom).nullable().optional(),
|
||||
predictionEnabled: z.boolean().optional(),
|
||||
rawChartMode: z.boolean().optional(),
|
||||
discreetNotifications: z.boolean().optional(),
|
||||
@@ -254,8 +283,8 @@ const illnessEpisodeBackupSchema = z
|
||||
.object({
|
||||
id: z.string().min(1),
|
||||
label: z.string().min(1),
|
||||
type: z.string().min(1),
|
||||
lifecycle: z.string().min(1),
|
||||
type: z.enum(IllnessType),
|
||||
lifecycle: z.enum(IllnessLifecycle),
|
||||
onsetAt: isoDateTime,
|
||||
resolvedAt: isoDateTime.nullable().optional(),
|
||||
// Self-referencing flare/exacerbation link, carried as the exported
|
||||
@@ -272,10 +301,10 @@ const allergyBackupSchema = z
|
||||
.object({
|
||||
id: z.string().min(1),
|
||||
substance: z.string().min(1),
|
||||
category: z.string().min(1),
|
||||
type: z.string().min(1),
|
||||
severity: z.string().nullable().optional(),
|
||||
status: z.string().min(1),
|
||||
category: z.enum(AllergyCategory),
|
||||
type: z.enum(AllergyType),
|
||||
severity: z.enum(AllergySeverity).nullable().optional(),
|
||||
status: z.enum(AllergyStatus),
|
||||
onsetAt: isoDateTime.nullable().optional(),
|
||||
reaction: z.string().nullable().optional(),
|
||||
note: z.string().nullable().optional(),
|
||||
@@ -287,7 +316,7 @@ const allergyBackupSchema = z
|
||||
const familyHistoryBackupSchema = z
|
||||
.object({
|
||||
id: z.string().min(1),
|
||||
relationship: z.string().min(1),
|
||||
relationship: z.enum(FamilyRelationship),
|
||||
condition: z.string().min(1),
|
||||
ageAtOnset: z.number().int().nullable().optional(),
|
||||
note: z.string().nullable().optional(),
|
||||
@@ -311,7 +340,7 @@ const workoutBackupSchema = z
|
||||
stepCount: z.number().int().nullable().optional(),
|
||||
elevationM: z.number().nullable().optional(),
|
||||
pauseDurationSec: z.number().int().nullable().optional(),
|
||||
source: z.string().min(1),
|
||||
source: z.enum(MeasurementSource),
|
||||
externalId: z.string().nullable().optional(),
|
||||
createdAt: isoDateTime.optional(),
|
||||
updatedAt: isoDateTime.optional(),
|
||||
@@ -330,12 +359,12 @@ const base64BytesSchema = z
|
||||
const documentBackupSchema = z
|
||||
.object({
|
||||
id: z.string().min(1),
|
||||
kind: z.string().min(1),
|
||||
kind: z.enum(InboundDocumentKind),
|
||||
title: z.string().nullable().optional(),
|
||||
filename: z.string().nullable().optional(),
|
||||
mimeType: z.string().min(1),
|
||||
byteSize: z.number().int(),
|
||||
status: z.string().min(1),
|
||||
status: z.enum(InboundDocumentStatus),
|
||||
reportDate: z.string().nullable().optional(),
|
||||
documentDate: z.string().nullable().optional(),
|
||||
contentEncrypted: base64BytesSchema.optional(),
|
||||
@@ -349,7 +378,7 @@ const documentBackupSchema = z
|
||||
errorReason: z.string().nullable().optional(),
|
||||
summaryEncrypted: base64BytesSchema.nullable().optional(),
|
||||
summaryGeneratedAt: isoDateTime.nullable().optional(),
|
||||
summaryState: z.string().min(1).optional(),
|
||||
summaryState: z.enum(DocumentSummaryState).optional(),
|
||||
summary: z.string().nullable().optional(),
|
||||
createdAt: isoDateTime.optional(),
|
||||
updatedAt: isoDateTime.optional(),
|
||||
|
||||
Reference in new issue
Block a user