feat(import): carry every field the app's own files write

The JSON import read six measurement fields and six mood fields while
the full backup and the JSON export carry twice that. A sleep night came
back as stageless rows, a range reading as a single value, an ECG alert
without its verdict, and a mood entry without its note, its level-A
values, its zone, its tag and factor links or its day context.

The import now reads valueMin/valueMax, sleepStage,
rhythmClassification and deviceType on a reading, and note, a1 to a5,
tz, factors, structuredTags and context on a mood entry. Each mood entry
is written with its links and context in one transaction, the way the
bulk mood ingest writes one. A link to a tag the importing account does
not have is dropped and named in droppedTagKeys / droppedFactorKeys, and
the import card says which. The schema moves to
src/lib/import/json/schema.ts so the guards can read it.

json-import-field-coverage.test.ts runs the JSON export and both backup
variants on an account with every column filled in: every field they
write is either read by the import or named with the reason it is not
(row ids, source, deletion records, ciphertext, sync bookkeeping), and a
reason for a field no exporter writes fails as stale. The round trip
imports each file into a fresh account and compares the re-export field
for field. The published contract is held to the runtime schema.
This commit is contained in:
Marc-André Bombeck committed 2026-10-23 21:33:06 +02:00
1 parent 43002d37e8
commit a840e30fac
31 files changed
+1814 -508

No files matched your search

@@ -0,0 +1,262 @@
/**
* Every field the app's exporters write for a reading or a mood entry is
* either read by the JSON import or named here with the reason it is not.
*
* The same kind of guard as `measurement-backup-completeness.test.ts` and
* `mood-backup-completeness.test.ts`, one step further down the pipe: those
* prove a column reaches the file, this one proves a field of the file reaches
* the import. The JSON import used to read six measurement fields and six
* mood fields while the files carried twice that, so a sleep night came back
* as stageless rows, a range as a single value, a mood entry without its note,
* its sliders, its links or its day.
*
* The field sets are read from what the exporters actually write, for an
* account with every column filled in (`json-import-seed.ts`), so a field an
* exporter learns to write appears here the release it does. Each file is
* walked in both of its forms: the JSON export, the portable full backup and
* the disaster-recovery backup.
*
* The reasons are checked the other way too: a reason for a field no exporter
* writes any more is stale and fails, so the list cannot grow into a list of
* things that were once true.
*/
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import { GET as exportJson } from "@/app/api/export/route";
import { buildFullBackupPayload } from "@/lib/export/full-backup-payload";
import {
jsonImportMeasurementSchema,
jsonImportMoodContextSchema,
jsonImportMoodEntrySchema,
} from "@/lib/import/json/schema";
import { callRoute } from "../kit/call-route";
import { makeSession } from "../kit/factories";
import {
seedJsonImportCatalogue,
seedJsonImportOwner,
} from "./json-import-seed";
import { getPrismaClient, truncateAllTables } from "./setup";
const prisma = getPrismaClient();
/** Why a field of an exported row is not a field of the imported one. */
const ROW_ID =
"The import writes new rows into the importing account; the file's row id names a row of the account it came from.";
const SOURCE =
"Every imported row is stamped IMPORT. Source priority and the sync paths key on the source, and a row claiming an integration would be taken for that integration's own.";
const DELETED_AT =
"A deletion record, not a reading: the import skips the row and counts it (`normalize.ts`).";
const CIPHERTEXT =
"Ciphertext under the exporting instance's key (disaster-recovery file only). The portable file carries the same note as text, and that is what the import reads.";
const BOOKKEEPING =
"Sync bookkeeping of the account the file came from (disaster-recovery file only). It describes that account's history with its sources, not the reading.";
const ROW_STAMP =
"The row's own write stamp (disaster-recovery file only); an imported row is written now.";
const MEASUREMENT_NOT_IMPORTED: Record<string, string> = {
id: ROW_ID,
source: SOURCE,
deletedAt: DELETED_AT,
notesEncrypted: CIPHERTEXT,
externalSourceVersion: BOOKKEEPING,
aggregationProvenance: BOOKKEEPING,
ingestTokenId:
"The API token that wrote the reading on the exporting account; no token of that account exists in the importing one.",
syncVersion: BOOKKEEPING,
createdAt: ROW_STAMP,
updatedAt: ROW_STAMP,
};
const MOOD_NOT_IMPORTED: Record<string, string> = {
id: ROW_ID,
source: SOURCE,
deletedAt: DELETED_AT,
noteEncrypted: CIPHERTEXT,
syncedAt: BOOKKEEPING,
syncVersion: BOOKKEEPING,
createdAt: ROW_STAMP,
updatedAt: ROW_STAMP,
linkCreatedAt: ROW_STAMP,
};
const CONTEXT_NOT_IMPORTED: Record<string, string> = {
notesEncrypted: CIPHERTEXT,
createdAt: ROW_STAMP,
updatedAt: ROW_STAMP,
};
/**
* The files' other sections. The JSON import carries readings and mood
* entries into an account that has its own record already; an account's
* whole record (medications, labs, documents, settings, …) comes back through
* the backup restore, which replaces it. The mood vocabulary is named apart
* because the entries' links point into it: a link to a tag the importing
* account does not have is dropped and named in the response.
*/
const IMPORTED_SECTIONS = new Set(["measurements", "moodEntries"]);
const VOCABULARY_SECTIONS: Record<string, string> = {
customMoodTags:
"Account vocabulary, restored by the backup restore. An imported link to one of these tags resolves only when the importing account has a tag of that key, and is otherwise dropped and named (`droppedTagKeys`).",
customMoodTagCategories:
"Account vocabulary, restored by the backup restore with the tags it groups.",
hiddenMoodTags:
"An account setting (which tags its editor hides), restored by the backup restore.",
};
type Row = Record<string, unknown>;
function keysOf(rows: readonly Row[]): Set<string> {
const keys = new Set<string>();
for (const row of rows) for (const key of Object.keys(row)) keys.add(key);
return keys;
}
/** Fields of `written` the import neither reads nor names. */
function unaccounted(
written: Set<string>,
read: Set<string>,
named: Record<string, string>,
): string[] {
return [...written].filter((key) => !read.has(key) && !(key in named));
}
interface Files {
jsonExport: { top: Row; measurements: Row[]; moodEntries: Row[] };
portable: Row & { measurements: Row[]; moodEntries: Row[] };
disasterRecovery: Row & { measurements: Row[]; moodEntries: Row[] };
}
let files: Files;
beforeAll(async () => {
await truncateAllTables(prisma);
await seedJsonImportCatalogue(prisma);
const { ownerId } = await seedJsonImportOwner(prisma);
const owner = await prisma.user.findUniqueOrThrow({ where: { id: ownerId } });
const exported = await callRoute<unknown>(exportJson, {
method: "GET",
route: "/api/export",
query: { format: "json", type: "all" },
auth: await makeSession(owner),
});
expect(exported.status).toBe(200);
const data = (exported.body as { data: Row }).data;
const portable = await buildFullBackupPayload(prisma, ownerId);
const disasterRecovery = await buildFullBackupPayload(prisma, ownerId, {
purpose: "disaster-recovery",
});
files = {
jsonExport: {
top: data,
measurements: data.measurements as Row[],
moodEntries: data.moodEntries as Row[],
},
portable: portable.payload as Files["portable"],
disasterRecovery: disasterRecovery.payload as Files["disasterRecovery"],
};
});
afterAll(async () => {
await truncateAllTables(prisma);
});
const MEASUREMENT_READ = new Set(
Object.keys(jsonImportMeasurementSchema.shape),
);
const MOOD_READ = new Set(Object.keys(jsonImportMoodEntrySchema.shape));
const CONTEXT_READ = new Set(Object.keys(jsonImportMoodContextSchema.shape));
const FILE_NAMES = ["jsonExport", "portable", "disasterRecovery"] as const;
function measurementsOf(name: (typeof FILE_NAMES)[number]): Row[] {
return files[name].measurements;
}
function moodOf(name: (typeof FILE_NAMES)[number]): Row[] {
return files[name].moodEntries;
}
function contextsOf(name: (typeof FILE_NAMES)[number]): Row[] {
return moodOf(name)
.map((entry) => entry.context)
.filter((context): context is Row => !!context);
}
describe("the JSON import reads what the exporters write", () => {
it.each(FILE_NAMES)("every measurement field of the %s", (name) => {
const rows = measurementsOf(name);
expect(rows.length).toBeGreaterThan(3);
expect(
unaccounted(keysOf(rows), MEASUREMENT_READ, MEASUREMENT_NOT_IMPORTED),
).toEqual([]);
});
it.each(FILE_NAMES)("every mood-entry field of the %s", (name) => {
const rows = moodOf(name);
expect(rows.length).toBeGreaterThan(0);
expect(unaccounted(keysOf(rows), MOOD_READ, MOOD_NOT_IMPORTED)).toEqual([]);
});
it.each(["portable", "disasterRecovery"] as const)(
"every day-context field of the %s",
(name) => {
const rows = contextsOf(name);
expect(rows.length).toBeGreaterThan(0);
expect(
unaccounted(keysOf(rows), CONTEXT_READ, CONTEXT_NOT_IMPORTED),
).toEqual([]);
},
);
it("names the mood vocabulary the entries' links point into", () => {
for (const name of ["portable", "disasterRecovery"] as const) {
const sections = Object.keys(files[name]);
expect(sections).toEqual(expect.arrayContaining([...IMPORTED_SECTIONS]));
for (const section of Object.keys(VOCABULARY_SECTIONS)) {
expect(sections, `${name} carries ${section}`).toContain(section);
}
}
// The JSON export's other two sections are medications, restored the
// same way as every other section of an account's record.
expect(Object.keys(files.jsonExport.top).sort()).toEqual([
"intakeEvents",
"measurements",
"medications",
"moodEntries",
]);
});
it("gives no reason for a field no exporter writes", () => {
const writtenMeasurement = new Set(
FILE_NAMES.flatMap((name) => [...keysOf(measurementsOf(name))]),
);
const writtenMood = new Set(
FILE_NAMES.flatMap((name) => [...keysOf(moodOf(name))]),
);
const writtenContext = new Set(
FILE_NAMES.flatMap((name) => [...keysOf(contextsOf(name))]),
);
const stale = [
...Object.keys(MEASUREMENT_NOT_IMPORTED)
.filter((key) => !writtenMeasurement.has(key))
.map((key) => `measurement.${key}`),
...Object.keys(MOOD_NOT_IMPORTED)
.filter((key) => !writtenMood.has(key))
.map((key) => `moodEntry.${key}`),
...Object.keys(CONTEXT_NOT_IMPORTED)
.filter((key) => !writtenContext.has(key))
.map((key) => `context.${key}`),
];
expect(stale).toEqual([]);
});
it("reads no field it also names as left out", () => {
const both = [
...Object.keys(MEASUREMENT_NOT_IMPORTED).filter((k) =>
MEASUREMENT_READ.has(k),
),
...Object.keys(MOOD_NOT_IMPORTED).filter((k) => MOOD_READ.has(k)),
...Object.keys(CONTEXT_NOT_IMPORTED).filter((k) => CONTEXT_READ.has(k)),
];
expect(both).toEqual([]);
});
});
+230 -199
View File
@@ -1,18 +1,24 @@
/**
* The JSON import takes the app's own files back.
* The JSON import takes the app's own files back, whole.
*
* An account's full backup (`/api/export/full-backup`, the portable export)
* and its JSON export (`/api/export?format=json`) are written, as they are
* served, and imported into a fresh account. What the import carries (a
* measurement's type, value, unit, instant, glucose context and note; a mood
* entry's day, label, score, tags and instant) must come back as it was.
* An account with every column filled in (`json-import-seed.ts`) is written
* out by the full backup (the portable export, `/api/export/full-backup`) and
* by the JSON export (`/api/export?format=json`), each file is imported into
* a fresh account, and the fresh account is written out again by the same
* exporter. The two files must agree on everything the import carries; what
* it deliberately leaves out is named in `json-import-field-coverage.test.ts`
* and is the only thing stripped before the comparison.
*
* Reading the result back through the exporter rather than through a
* hand-written projection of the tables is the point: a field the import
* drops shows up as a difference in the file a person would get back, which
* is the loss that matters, and a field an exporter learns to write is
* compared from the release it starts.
*
* Both files spell an empty field their own way, `null` in the backup and an
* empty string in the export. Before the import read files in the exporter's
* terms, the first empty note, tag list or glucose context refused the whole
* file with a 422. The portable backup also left a reading's own attributes
* out (a glucose value's fasting context among them), so the backup half
* pins those too.
* file with a 422.
*/
import { afterAll, beforeAll, describe, expect, it } from "vitest";
@@ -21,163 +27,60 @@ import { POST as importCsv } from "@/app/api/import/csv/route";
import { GET as exportMeasurementsCsv } from "@/app/api/export/measurements/route";
import { POST as importJson } from "@/app/api/import/route";
import { encryptNote, readNote } from "@/lib/crypto/note-cipher";
import { buildFullBackupPayload } from "@/lib/export/full-backup-payload";
import { streamFullBackupJson } from "@/lib/export/full-backup-stream";
import { callRoute } from "../kit/call-route";
import { makeSession, makeUser } from "../kit/factories";
import {
JI_OWN_TAG_KEY,
seedJsonImportCatalogue,
seedJsonImportOwner,
} from "./json-import-seed";
import { getPrismaClient, truncateAllTables } from "./setup";
const prisma = getPrismaClient();
const MEASUREMENTS = [
{
type: "WEIGHT",
value: 81.4,
unit: "kg",
at: "2026-09-10T06:30:00.000Z",
note: "after the run",
},
{
type: "WEIGHT",
value: 81.9,
unit: "kg",
at: "2026-09-11T06:30:00.000Z",
note: null,
},
{
type: "PULSE",
value: 62,
unit: "bpm",
at: "2026-09-11T07:00:00.000Z",
note: null,
},
{
type: "BLOOD_GLUCOSE",
value: 104,
unit: "mg/dL",
at: "2026-09-12T08:00:00.000Z",
note: null,
glucoseContext: "FASTING" as const,
},
{
type: "BLOOD_GLUCOSE",
value: 141,
unit: "mg/dL",
at: "2026-09-12T13:00:00.000Z",
note: "after lunch",
glucoseContext: "POSTPRANDIAL" as const,
},
] as const;
type Row = Record<string, unknown>;
const MOOD = [
{
date: "2026-09-10",
mood: "GUT",
score: 4,
tags: null,
at: "2026-09-10T19:00:00.000Z",
},
{
date: "2026-09-11",
mood: "OKAY",
score: 3,
tags: JSON.stringify(["work", "sleep"]),
at: "2026-09-11T20:30:00.000Z",
},
{
date: "2026-09-12",
mood: "SUPER_GUT",
score: 5,
tags: null,
at: "2026-09-12T21:15:00.000Z",
},
] as const;
interface ImportStats {
measurements: number;
moodEntries: number;
skipped: number;
droppedTagKeys?: string[];
droppedFactorKeys?: string[];
}
let ownerId: string;
async function seedOwner(): Promise<void> {
const owner = await makeUser({
timezone: "Europe/Berlin",
glucoseUnit: "mmol/L" /* the JSON export stays in the stored unit */,
async function fullBackupOf(userId: string): Promise<{
measurements: Row[];
moodEntries: Row[];
}> {
let text = "";
await streamFullBackupJson(prisma, userId, (chunk) => {
text += chunk;
});
ownerId = owner.id;
for (const m of MEASUREMENTS) {
await prisma.measurement.create({
data: {
userId: ownerId,
type: m.type,
value: m.value,
unit: m.unit,
measuredAt: new Date(m.at),
source: "MANUAL",
notes: null,
notesEncrypted: encryptNote(m.note),
glucoseContext: "glucoseContext" in m ? m.glucoseContext : null,
},
});
}
// A reading deleted before the backup: its row is the record of a
// deletion, and the import leaves it out.
await prisma.measurement.create({
data: {
userId: ownerId,
type: "WEIGHT",
value: 90,
unit: "kg",
measuredAt: new Date("2026-09-09T06:30:00.000Z"),
source: "MANUAL",
deletedAt: new Date("2026-09-09T07:00:00.000Z"),
},
});
for (const e of MOOD) {
await prisma.moodEntry.create({
data: {
userId: ownerId,
date: e.date,
mood: e.mood,
score: e.score,
tags: e.tags,
source: "MANUAL",
moodLoggedAt: new Date(e.at),
},
});
}
return JSON.parse(text);
}
/** What an import carries, read back from an account. */
async function carried(userId: string) {
const measurements = await prisma.measurement.findMany({
where: { userId, deletedAt: null },
orderBy: [{ measuredAt: "asc" }, { type: "asc" }],
async function jsonExportOf(userId: string): Promise<{
data: { measurements: Row[]; moodEntries: Row[] };
}> {
const user = await prisma.user.findUniqueOrThrow({ where: { id: userId } });
const res = await callRoute<unknown>(exportJson, {
method: "GET",
route: "/api/export",
query: { format: "json", type: "all" },
auth: await makeSession(user),
});
const mood = await prisma.moodEntry.findMany({
where: { userId, deletedAt: null },
orderBy: { moodLoggedAt: "asc" },
});
return {
measurements: measurements.map((m) => ({
type: m.type,
value: m.value,
unit: m.unit,
measuredAt: m.measuredAt.toISOString(),
glucoseContext: m.glucoseContext,
note: readNote(m.notesEncrypted, m.notes),
})),
mood: mood.map((e) => ({
date: e.date,
mood: e.mood,
score: e.score,
tags: e.tags,
loggedAt: e.moodLoggedAt.toISOString(),
})),
};
expect(res.status).toBe(200);
return res.body as { data: { measurements: Row[]; moodEntries: Row[] } };
}
async function importInto(file: unknown) {
const user = await makeUser({ timezone: "Europe/Berlin" });
const res = await callRoute<{
data: { measurements: number; moodEntries: number; skipped: number };
}>(importJson, {
const res = await callRoute<ImportStats>(importJson, {
method: "POST",
route: "/api/import",
body: file,
@@ -186,77 +89,194 @@ async function importInto(file: unknown) {
return { userId: user.id, res };
}
/** A row without the fields the import deliberately does not carry. */
function comparable(row: Row): Row {
const { id: _id, source: _source, ...rest } = row;
return rest;
}
function byInstant(at: string) {
return (a: Row, b: Row) =>
String(a[at]).localeCompare(String(b[at])) ||
String(a.type ?? "").localeCompare(String(b.type ?? ""));
}
beforeAll(async () => {
await truncateAllTables(prisma);
await seedOwner();
await seedJsonImportCatalogue(prisma);
({ ownerId } = await seedJsonImportOwner(prisma));
});
afterAll(async () => {
await truncateAllTables(prisma);
});
describe("the JSON import reads the app's own files", () => {
it("takes a full backup back, nulls and deletion records included", async () => {
let text = "";
await streamFullBackupJson(prisma, ownerId, (chunk) => {
text += chunk;
});
const backup = JSON.parse(text) as {
measurements: Array<Record<string, unknown>>;
moodEntries: Array<Record<string, unknown>>;
};
// The file carries the shapes the import used to refuse.
expect(backup.measurements.some((m) => m.notes === null)).toBe(true);
expect(backup.moodEntries.some((e) => e.tags === null)).toBe(true);
describe("the JSON import takes the app's own files back", () => {
it("takes a full backup back field for field", async () => {
const original = await fullBackupOf(ownerId);
// The file carries the shapes the import used to refuse or drop.
expect(original.measurements.some((m) => m.notes === null)).toBe(true);
expect(original.measurements.some((m) => m.sleepStage === "DEEP")).toBe(
true,
);
expect(original.moodEntries.some((e) => e.context !== null)).toBe(true);
const { userId, res } = await importInto(backup);
const { userId, res } = await importInto(original);
expect(res.status).toBe(200);
const deletedInFile = backup.measurements.filter(
(m) => typeof m.deletedAt === "string",
).length;
expect(res.data).toEqual({
measurements: MEASUREMENTS.length,
moodEntries: MOOD.length,
skipped: deletedInFile,
measurements: original.measurements.length,
moodEntries: original.moodEntries.length,
skipped: 0,
// The account's own tag is not this account's: the entry lands without
// that one link, and the response says which.
droppedTagKeys: [JI_OWN_TAG_KEY],
});
expect(await carried(userId)).toEqual(await carried(ownerId));
const back = await fullBackupOf(userId);
const expectedMeasurements = original.measurements
.map(comparable)
.sort(byInstant("measuredAt"));
expect(
back.measurements.map(comparable).sort(byInstant("measuredAt")),
).toEqual(expectedMeasurements);
const expectedMood = original.moodEntries
.map((entry) => ({
...comparable(entry),
structuredTags: (entry.structuredTags as string[]).filter(
(key) => key !== JI_OWN_TAG_KEY,
),
}))
.sort(byInstant("loggedAt"));
expect(
back.moodEntries.map(comparable).sort(byInstant("loggedAt")),
).toEqual(expectedMood);
});
it("takes the JSON export back, empty strings and its envelope included", async () => {
const owner = await prisma.user.findUniqueOrThrow({
where: { id: ownerId },
});
const exported = await callRoute<unknown>(exportJson, {
method: "GET",
route: "/api/export",
query: { format: "json", type: "all" },
auth: await makeSession(owner),
});
expect(exported.status).toBe(200);
const file = exported.body as {
data: {
measurements: Array<Record<string, unknown>>;
moodEntries: Array<Record<string, unknown>>;
};
};
expect(file.data.measurements.some((m) => m.notes === "")).toBe(true);
expect(file.data.moodEntries.some((e) => e.tags === "")).toBe(true);
const original = await jsonExportOf(ownerId);
expect(original.data.measurements.some((m) => m.notes === "")).toBe(true);
expect(original.data.moodEntries.some((e) => e.note === "")).toBe(true);
const { userId, res } = await importInto(file);
const { userId, res } = await importInto(original);
expect(res.status).toBe(200);
expect(res.data).toMatchObject({
measurements: MEASUREMENTS.length,
moodEntries: MOOD.length,
measurements: original.data.measurements.length,
moodEntries: original.data.moodEntries.length,
skipped: 0,
});
expect(await carried(userId)).toEqual(await carried(ownerId));
// A sleep night is one summary record in this file, and its `notes` is
// the stage breakdown the exporter writes from the stored stages. The
// import stores the night and that text as its note; a re-export
// rebuilds the column from stages the summary does not have. So the
// night's note is compared against what was stored, and the rest of the
// record through the file.
const nights = original.data.measurements.filter(
(m) => m.type === "SLEEP_DURATION",
);
expect(nights).toHaveLength(1);
const storedNight = await prisma.measurement.findFirstOrThrow({
where: { userId, type: "SLEEP_DURATION" },
});
expect(readNote(storedNight.notesEncrypted, storedNight.notes)).toBe(
nights[0]!.notes,
);
const withoutNightNote = (m: Row) =>
m.type === "SLEEP_DURATION"
? { ...comparable(m), notes: null }
: comparable(m);
const back = await jsonExportOf(userId);
expect(
back.data.measurements
.map(withoutNightNote)
.sort(byInstant("measuredAt")),
).toEqual(
original.data.measurements
.map(withoutNightNote)
.sort(byInstant("measuredAt")),
);
expect(
back.data.moodEntries.map(comparable).sort(byInstant("loggedAt")),
).toEqual(
original.data.moodEntries.map(comparable).sort(byInstant("loggedAt")),
);
});
it("takes the measurement CSV back into the CSV import, in display units too", async () => {
const owner = await prisma.user.findUniqueOrThrow({
where: { id: ownerId },
it("leaves out a disaster-recovery file's deletion records, and counts them", async () => {
const { payload } = await buildFullBackupPayload(prisma, ownerId, {
purpose: "disaster-recovery",
});
const rows = (payload as { measurements: Row[] }).measurements;
const deleted = rows.filter((m) => typeof m.deletedAt === "string");
expect(deleted).toHaveLength(1);
const { userId, res } = await importInto(payload);
expect(res.status).toBe(200);
expect(res.data).toMatchObject({
measurements: rows.length - deleted.length,
skipped: deleted.length,
});
expect(
await prisma.measurement.count({ where: { userId, value: 90 } }),
).toBe(0);
});
it("takes the same file twice without changing what the first import wrote", async () => {
const original = await fullBackupOf(ownerId);
const { userId } = await importInto(original);
const first = await fullBackupOf(userId);
const user = await prisma.user.findUniqueOrThrow({ where: { id: userId } });
const again = await callRoute<ImportStats>(importJson, {
method: "POST",
route: "/api/import",
body: original,
auth: await makeSession(user),
});
expect(again.status).toBe(200);
// The mood entries carry their instant, so the second pass finds them;
// the readings carry no source id in a portable file and are written
// again, as the import has always documented for a file without one.
expect(again.data.moodEntries).toBe(0);
const second = await fullBackupOf(userId);
expect(
second.moodEntries.map(comparable).sort(byInstant("loggedAt")),
).toEqual(first.moodEntries.map(comparable).sort(byInstant("loggedAt")));
});
});
describe("the CSV import reads the measurement CSV export", () => {
const READINGS = [
{ type: "WEIGHT", value: 81.4, unit: "kg", at: "2026-09-10T06:30:00.000Z" },
{ type: "PULSE", value: 62, unit: "bpm", at: "2026-09-11T07:00:00.000Z" },
{
type: "BLOOD_GLUCOSE",
value: 104,
unit: "mg/dL",
at: "2026-09-12T08:00:00.000Z",
glucoseContext: "FASTING" as const,
},
] as const;
it("takes it back, in display units too", async () => {
const owner = await makeUser({
timezone: "Europe/Berlin",
glucoseUnit: "mmol/L",
});
for (const m of READINGS) {
await prisma.measurement.create({
data: {
userId: owner.id,
type: m.type,
value: m.value,
unit: m.unit,
measuredAt: new Date(m.at),
source: "MANUAL",
notesEncrypted: encryptNote(null),
glucoseContext: "glucoseContext" in m ? m.glucoseContext : null,
},
});
}
const exported = await callRoute(exportMeasurementsCsv, {
method: "GET",
route: "/api/export/measurements",
@@ -279,13 +299,24 @@ describe("the JSON import reads the app's own files", () => {
},
);
expect(res.status).toBe(200);
expect(res.data).toMatchObject({
inserted: MEASUREMENTS.length,
skipped: 0,
});
expect(res.data).toMatchObject({ inserted: READINGS.length, skipped: 0 });
const back = (await carried(user.id)).measurements;
const original = (await carried(ownerId)).measurements;
const read = async (userId: string) =>
(
await prisma.measurement.findMany({
where: { userId, deletedAt: null },
orderBy: [{ measuredAt: "asc" }, { type: "asc" }],
})
).map((m) => ({
type: m.type,
value: m.value,
unit: m.unit,
measuredAt: m.measuredAt.toISOString(),
glucoseContext: m.glucoseContext,
note: readNote(m.notesEncrypted, m.notes),
}));
const back = await read(user.id);
const original = await read(owner.id);
expect(back).toHaveLength(original.length);
back.forEach((m, i) => {
const o = original[i]!;
+267
View File
@@ -0,0 +1,267 @@
/**
* One account with every column the exporters read filled in, for the JSON
* import's round trip and its field-coverage guard.
*
* Every reading carries what makes it that reading: a range keeps its bounds,
* a sleep night its stages and its writer, a rhythm alert its verdict, a
* glucose value its meal-time context, a note its text. The mood entry
* carries a note, all five level-A values, its zone, a rated factor, a
* catalogue tag, a tag of the account's own and a full day context. A field
* the seed leaves empty is a field neither test can see arrive, so nothing
* here is left at its default.
*/
import type { PrismaClient } from "@/generated/prisma/client";
import { encryptNote } from "@/lib/crypto/note-cipher";
import { encodeKeyList } from "@/lib/mood/context-vocabulary";
import { makeUser } from "../kit/factories";
export const JI_FACTOR_KEY = "ji_sleep_quality";
export const JI_TAG_KEY = "ji_headache";
/** The account's own tag: another account importing the file cannot resolve it. */
export const JI_OWN_TAG_KEY = "custom:ji_long_walk";
export const JI_MEASUREMENTS = [
{
type: "WEIGHT",
value: 81.4,
unit: "kg",
at: "2026-09-10T06:30:00.000Z",
note: "after the run",
},
{
type: "PULSE",
value: 64,
valueMin: 52,
valueMax: 91,
unit: "bpm",
at: "2026-09-11T07:00:00.000Z",
deviceType: "watch",
},
{
type: "BLOOD_GLUCOSE",
value: 104,
unit: "mg/dL",
at: "2026-09-12T08:00:00.000Z",
glucoseContext: "FASTING",
},
{
type: "BLOOD_GLUCOSE",
value: 141,
unit: "mg/dL",
at: "2026-09-12T13:00:00.000Z",
note: "after lunch",
glucoseContext: "POSTPRANDIAL",
},
{
type: "IRREGULAR_RHYTHM_NOTIFICATION",
value: 1,
unit: "count",
at: "2026-09-12T15:00:00.000Z",
rhythmClassification: "IRREGULAR",
deviceType: "watch",
},
// One night, as a watch writes it: a stage per row.
{
type: "SLEEP_DURATION",
value: 70,
unit: "minutes",
at: "2026-09-12T23:40:00.000Z",
sleepStage: "DEEP",
deviceType: "watch",
},
{
type: "SLEEP_DURATION",
value: 240,
unit: "minutes",
at: "2026-09-13T03:40:00.000Z",
sleepStage: "CORE",
deviceType: "watch",
},
{
type: "SLEEP_DURATION",
value: 95,
unit: "minutes",
at: "2026-09-13T05:15:00.000Z",
sleepStage: "REM",
deviceType: "watch",
},
] as const;
export const JI_MOOD = {
date: "2026-09-12",
mood: "GUT",
score: 4,
a1: 8,
a2: 3,
a3: 6,
a4: 7,
a5: 5,
tags: JSON.stringify(["work", "sleep"]),
note: "long day, good evening",
tz: "America/New_York",
at: "2026-09-13T01:30:00.000Z",
} as const;
/** A second entry with nothing but the label: what a one-tap check-in writes. */
export const JI_MOOD_BARE = {
date: "2026-09-11",
mood: "OKAY",
score: 3,
at: "2026-09-11T20:30:00.000Z",
} as const;
export interface SeededOwner {
ownerId: string;
}
/** The catalogue the links resolve against: seeded rows, owned by no account. */
export async function seedJsonImportCatalogue(
prisma: PrismaClient,
): Promise<void> {
const category = await prisma.moodTagCategory.create({
data: { key: "ji_category", labelKey: "mood.tagCategory.ji" },
});
await prisma.moodTag.create({
data: {
categoryId: category.id,
key: JI_TAG_KEY,
labelKey: "mood.tag.jiHeadache",
kind: "BINARY",
},
});
await prisma.moodTag.create({
data: {
categoryId: category.id,
key: JI_FACTOR_KEY,
labelKey: "mood.tag.jiSleepQuality",
kind: "RATED",
scaleMin: 1,
scaleMax: 5,
},
});
}
export async function seedJsonImportOwner(
prisma: PrismaClient,
): Promise<SeededOwner> {
const owner = await makeUser({
timezone: "Europe/Berlin",
glucoseUnit: "mmol/L" /* the JSON export stays in the stored unit */,
});
const ownerId = owner.id;
for (const m of JI_MEASUREMENTS) {
await prisma.measurement.create({
data: {
userId: ownerId,
type: m.type,
value: m.value,
valueMin: "valueMin" in m ? m.valueMin : null,
valueMax: "valueMax" in m ? m.valueMax : null,
unit: m.unit,
measuredAt: new Date(m.at),
source: "APPLE_HEALTH",
notes: null,
notesEncrypted: encryptNote("note" in m ? m.note : null),
glucoseContext: "glucoseContext" in m ? m.glucoseContext : null,
sleepStage: "sleepStage" in m ? m.sleepStage : null,
rhythmClassification:
"rhythmClassification" in m ? m.rhythmClassification : null,
deviceType: "deviceType" in m ? m.deviceType : null,
},
});
}
// A reading deleted before the backup: its row is the record of a
// deletion, and the import leaves it out.
await prisma.measurement.create({
data: {
userId: ownerId,
type: "WEIGHT",
value: 90,
unit: "kg",
measuredAt: new Date("2026-09-09T06:30:00.000Z"),
source: "MANUAL",
deletedAt: new Date("2026-09-09T07:00:00.000Z"),
},
});
const category = await prisma.moodTagCategory.findUniqueOrThrow({
where: { key: "ji_category" },
});
const ownTag = await prisma.moodTag.create({
data: {
userId: ownerId,
categoryId: category.id,
key: JI_OWN_TAG_KEY,
labelKey: "mood.tag.custom",
kind: "BINARY",
},
});
const [tag, factor] = await Promise.all([
prisma.moodTag.findUniqueOrThrow({ where: { key: JI_TAG_KEY } }),
prisma.moodTag.findUniqueOrThrow({ where: { key: JI_FACTOR_KEY } }),
]);
const entry = await prisma.moodEntry.create({
data: {
userId: ownerId,
date: JI_MOOD.date,
mood: JI_MOOD.mood,
score: JI_MOOD.score,
moodA1: JI_MOOD.a1,
stressA2: JI_MOOD.a2,
energyA3: JI_MOOD.a3,
connectionA4: JI_MOOD.a4,
stabilityA5: JI_MOOD.a5,
tags: JI_MOOD.tags,
note: null,
noteEncrypted: encryptNote(JI_MOOD.note),
tz: JI_MOOD.tz,
source: "MANUAL",
moodLoggedAt: new Date(JI_MOOD.at),
},
});
await prisma.moodEntryTagLink.createMany({
data: [
{ moodEntryId: entry.id, moodTagId: tag.id },
{ moodEntryId: entry.id, moodTagId: ownTag.id },
{ moodEntryId: entry.id, moodTagId: factor.id, rating: 4 },
],
});
await prisma.moodContext.create({
data: {
moodEntryId: entry.id,
userId: ownerId,
workStatus: "regular",
workMinutes: 540,
overtimeMinutes: 60,
workLoad: 7,
workSatisfaction: 6,
contactCircles: encodeKeyList(["partner", "family"]),
contactForm: "inPerson",
contactExtent: "extended",
contactQuality: 8,
contactSupport: 7,
leisureCategories: encodeKeyList(["reading"]),
leisureMinutes: 90,
leisureJoy: 8,
leisureRecovery: 7,
eventType: "goodNews",
eventValence: 3,
eventAt: new Date("2026-09-12T18:00:00.000Z"),
notesEncrypted: encryptNote("the call went well"),
},
});
await prisma.moodEntry.create({
data: {
userId: ownerId,
date: JI_MOOD_BARE.date,
mood: JI_MOOD_BARE.mood,
score: JI_MOOD_BARE.score,
moodA1: 5,
source: "MANUAL",
moodLoggedAt: new Date(JI_MOOD_BARE.at),
},
});
return { ownerId };
}
@@ -20,6 +20,7 @@
"moodEntries": 4,
"skipped": 2
},
"import_dropped_link_keys": 0,
"external_id_rejected": 2,
"external_id_shapes": "object_description",
"external_id_surface": "import.json"
@@ -20,6 +20,7 @@
"moodEntries": 1,
"skipped": 6
},
"import_dropped_link_keys": 0,
"external_id_rejected": 2,
"external_id_shapes": "object_description",
"external_id_surface": "import.json"