fix(backup): carry deleted family-history entries in the disaster-recovery file

Every record with a tombstone (allergies, labs, conditions, vaccinations,
life events, measurements and the rest) rides the disaster-recovery file
deleted rows included, so a restore gives back the account as it was. The
family history read only live entries for both files, so a restore lost
its deletions, and it carried the note as readable text that the restore
sealed again under the active key.

The disaster-recovery slice now carries every entry with its deletion and
the note as the ciphertext at rest, and the restore writes both back
verbatim; the portable file keeps the readable live entries. The parity
golden copies change for exactly that: the deleted entry, its note's key
and the shifted ids after it.
This commit is contained in:
Marc-André Bombeck committed 2026-10-22 18:59:07 +02:00
1 parent faedcbba93
commit 3b2c2c45f3
8 files changed
+576 -423

No files matched your search

File diff suppressed because it is too large. Load diff
@@ -22,7 +22,7 @@
"illnessEpisodes": 4,
"illnessDayLogs": 2,
"allergies": 2,
"familyHistory": 1,
"familyHistory": 2,
"workouts": 1,
"documents": 1,
"documentConditionLinks": 1,
@@ -78,7 +78,7 @@
"nutrientDays": 1,
"illnessEpisodes": 4,
"allergies": 2,
"familyHistory": 1,
"familyHistory": 2,
"workouts": 1,
"documents": 1,
"documentConditionLinks": 0,
@@ -294,7 +294,7 @@
"nutrientDays": 1,
"illnessEpisodes": 4,
"allergies": 2,
"familyHistory": 1,
"familyHistory": 2,
"workouts": 1,
"documents": 1,
"documentConditionLinks": 0,
@@ -350,7 +350,7 @@
"illnessEpisodes": 4,
"illnessDayLogs": 2,
"allergies": 2,
"familyHistory": 1,
"familyHistory": 2,
"workouts": 1,
"documents": 1,
"healthProfile": 1,
File diff suppressed because it is too large. Load diff
@@ -1707,9 +1707,11 @@
"relationship": "FATHER",
"condition": "Diabetes",
"ageAtOnset": 55,
"note": "type 2",
"note": null,
"notesEncrypted": "<sealed:v2:type 2>",
"createdAt": "<now>",
"updatedAt": "<now>"
"updatedAt": "<now>",
"deletedAt": null
}
],
"workouts": [
@@ -0,0 +1,53 @@
/**
* The disaster-recovery file carries deleted family-history entries, as it
* carries every other record's tombstones, and carries their note as the
* ciphertext at rest. The portable file carries the live entries only, with
* the note readable. The full round trip, the restored tombstone included, is
* pinned by the backup parity golden copies.
*/
import { beforeEach, describe, expect, it } from "vitest";
import { encryptToBytes } from "@/lib/ai/coach/bytes-codec";
import { buildFamilyHistoryBackupSlice } from "@/lib/export/family-history-backup";
import { makeUser } from "../kit/factories";
import { getPrismaClient, truncateAllTables } from "./setup";
const prisma = getPrismaClient();
beforeEach(() => truncateAllTables(prisma));
describe("family history in the backup", () => {
it("keeps a deleted entry in the disaster-recovery file and out of the portable one", async () => {
const user = await makeUser();
const notes = encryptToBytes("diagnosed at fifty");
await prisma.familyHistoryEntry.create({
data: { userId: user.id, relationship: "MOTHER", condition: "Asthma" },
});
const deleted = await prisma.familyHistoryEntry.create({
data: {
userId: user.id,
relationship: "FATHER",
condition: "Hypertension",
notesEncrypted: notes,
deletedAt: new Date("2026-09-30T10:00:00.000Z"),
},
});
const dr = await buildFamilyHistoryBackupSlice(prisma, user.id, true);
expect(dr.familyHistory).toHaveLength(2);
const tombstone = dr.familyHistory.find((e) => e.id === deleted.id);
expect(tombstone?.deletedAt).toBe("2026-09-30T10:00:00.000Z");
expect(tombstone?.note).toBeNull();
expect(tombstone?.notesEncrypted).toBe(
Buffer.from(notes).toString("base64"),
);
const portable = await buildFamilyHistoryBackupSlice(
prisma,
user.id,
false,
);
expect(portable.familyHistory.map((e) => e.condition)).toEqual(["Asthma"]);
});
});