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

@@ -1,7 +1,7 @@
{
"$comment": "Production functions over 200 lines, keyed \"file :: name\", and their length. Maintained by src/__tests__/source-size-ratchet.test.ts.",
"count": 355,
"total": 114640,
"count": 354,
"total": 114417,
"entries": {
"app/achievements/page.tsx :: AchievementsPage": 206,
"app/api/admin/backups/upload/route.ts :: apiHandler(arg)": 314,
@@ -208,7 +208,6 @@
"components/settings/import-panel/csv-import-card.tsx :: CsvImportCard": 212,
"components/settings/import-panel/dose-history-import-card.tsx :: DoseHistoryImportCard": 225,
"components/settings/import-panel/health-connect-import-card.tsx :: HealthConnectImportCard": 293,
"components/settings/import-panel/json-import-card.tsx :: JsonImportCard": 223,
"components/settings/import-panel/record-csv-import-card.tsx :: RecordCsvImportSheet": 460,
"components/settings/insights-pill-order-section.tsx :: InsightsPillOrderSection": 255,
"components/settings/integrations/connections-panel.tsx :: ConnectionsPanel": 230,
@@ -1,7 +1,7 @@
{
"$comment": "Comment lines that open with a release version, per file under src/. Maintained by src/__tests__/version-stamp-ratchet.test.ts.",
"count": 2898,
"total": 9294,
"count": 2897,
"total": 9290,
"entries": {
"__tests__/account-payload-consumer-guard.test.ts": 2,
"__tests__/acting-account-boundary-guard.test.ts": 7,
@@ -284,7 +284,6 @@
"app/api/illness/episodes/route.ts": 1,
"app/api/import/__tests__/route.test.ts": 3,
"app/api/import/csv/__tests__/route.test.ts": 2,
"app/api/import/route.ts": 4,
"app/api/ingest/medication/__tests__/route.test.ts": 1,
"app/api/ingest/medication/route.ts": 7,
"app/api/insights/biomarker-assessment/route.ts": 1,
@@ -0,0 +1,75 @@
/**
* The published JSON import contract names the fields the import reads.
*
* `src/lib/openapi/routes/import.ts` restates `jsonImportSchema` by hand,
* because the runtime schema carries transforms the document describes in
* prose. A restatement drifts: a field the import learns to read stays
* undocumented, and one it stops reading stays promised. So the two field
* sets are compared here, per row kind, both ways.
*/
import { describe, expect, it } from "vitest";
import {
jsonImportMeasurementSchema,
jsonImportMoodContextSchema,
jsonImportMoodEntrySchema,
} from "@/lib/import/json/schema";
import { buildOpenApiDocument } from "@/lib/openapi/registry";
type SchemaObject = {
properties?: Record<string, SchemaObject>;
$ref?: string;
allOf?: SchemaObject[];
};
const doc = buildOpenApiDocument() as {
components: { schemas: Record<string, SchemaObject> };
};
function resolve(schema: SchemaObject): SchemaObject {
if (schema.$ref) {
const name = schema.$ref.split("/").pop()!;
return resolve(doc.components.schemas[name]!);
}
if (schema.allOf) {
return {
properties: Object.assign(
{},
...schema.allOf.map((part) => resolve(part).properties ?? {}),
),
};
}
return schema;
}
function documented(name: string): string[] {
const schema = doc.components.schemas[name];
expect(schema, `${name} is published`).toBeDefined();
return Object.keys(resolve(schema!).properties ?? {}).sort();
}
describe("the published JSON import contract", () => {
it("names every measurement field the import reads, and no other", () => {
const fields = documented("JsonImportMeasurement");
expect(fields.length).toBeGreaterThan(5);
expect(fields).toEqual(
Object.keys(jsonImportMeasurementSchema.shape).sort(),
);
});
it("names every mood-entry field the import reads, and no other", () => {
const fields = documented("JsonImportMoodEntry");
expect(fields.length).toBeGreaterThan(5);
expect(fields).toEqual(Object.keys(jsonImportMoodEntrySchema.shape).sort());
});
it("names every day-context field the import reads, and no other", () => {
const entry = resolve(doc.components.schemas.JsonImportMoodEntry!);
const context = resolve(entry.properties!.context!);
const fields = Object.keys(context.properties ?? {}).sort();
expect(fields.length).toBeGreaterThan(10);
expect(fields).toEqual(
Object.keys(jsonImportMoodContextSchema.shape).sort(),
);
});
});
@@ -75,7 +75,7 @@ const WRITERS: Record<string, Disposition> = {
},
"lib/import/json/measurements.ts": {
kind: "upstream",
validatedIn: "app/api/import/route.ts",
validatedIn: "lib/import/json/schema.ts",
},
// The batch ingest's set-based insert writes rows that already passed
// `validateMeasurementRange` per entry in its first pass; everything else