mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-17 23:52:24 +00:00
Account for every dropped Apple Health sample the result used to hide
Three silent gaps in the import accounting, one commit: The reproductive samples of an export land in recordsRead but were discarded without a counter when cycle tracking is off — the result now names them (samplesSkippedModuleDisabled). A cycle flush that failed partway zeroed its stats AFTER day-by-day writes had landed; the flush is now fault-isolated per day and reports daysFailed plus the first failure reason instead of claiming nothing happened. And the import card hardcoded skipped: 0 while the worker has computed, persisted and served per-class skip counters since v1.15 — it now renders them, splitting deliberate exclusions (unsupported and deferred types, a disabled cycle module) from genuine refusals (unparseable, out of range, failed writes), with a per-type breakdown. Only refusals tip the outcome tone.
This commit is contained in:
1 parent
a45dde2039
commit
db6465c17d
12 files changed
+530
-31
No files matched your search
@@ -0,0 +1,85 @@
|
||||
/**
|
||||
* Cycle-import accumulator — flush fault isolation (A6).
|
||||
*
|
||||
* The pre-fix flush let one colliding day throw out of the loop; the
|
||||
* importer's catch then zeroed the stats, so a partial write reported
|
||||
* `samplesConsumed: 0` over rows already in the table. The flush is now
|
||||
* fault-isolated per day and its stats stay honest: what landed counts,
|
||||
* what failed is named.
|
||||
*
|
||||
* Watched red: with the per-day try/catch removed from `flush()` the
|
||||
* partial-failure test throws instead of reporting
|
||||
* `{ daysUpserted: 1, daysFailed: 1 }`.
|
||||
*/
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
upsert: vi.fn(),
|
||||
findManyCycles: vi.fn(async () => []),
|
||||
findUniqueProfile: vi.fn(async () => null),
|
||||
updateManyProfile: vi.fn(async () => ({ count: 0 })),
|
||||
}));
|
||||
|
||||
vi.mock("@/lib/db", () => ({
|
||||
prisma: {
|
||||
menstrualCycle: { findMany: mocks.findManyCycles },
|
||||
cycleProfile: {
|
||||
findUnique: mocks.findUniqueProfile,
|
||||
updateMany: mocks.updateManyProfile,
|
||||
},
|
||||
},
|
||||
}));
|
||||
vi.mock("@/lib/cycle/day-log-write", () => ({
|
||||
upsertCycleDayLog: (...args: unknown[]) => mocks.upsert(...args),
|
||||
}));
|
||||
vi.mock("@/lib/cycle/gate", () => ({
|
||||
isCycleAvailableForUser: vi.fn(async () => true),
|
||||
}));
|
||||
vi.mock("@/lib/auth/audit", () => ({ auditLog: vi.fn() }));
|
||||
|
||||
import { CycleImportAccumulator } from "../import-accumulator";
|
||||
|
||||
const FLOW = "HKCategoryTypeIdentifierMenstrualFlow";
|
||||
|
||||
beforeEach(() => {
|
||||
mocks.upsert.mockReset();
|
||||
});
|
||||
|
||||
describe("CycleImportAccumulator.flush — per-day fault isolation", () => {
|
||||
it("keeps counting after a failed day and names the failure", async () => {
|
||||
const acc = new CycleImportAccumulator("u1", "Europe/Berlin");
|
||||
expect(
|
||||
acc.consume(FLOW, "2026-05-10", "HKCategoryValueMenstrualFlowMedium"),
|
||||
).toBe(true);
|
||||
expect(
|
||||
acc.consume(FLOW, "2026-05-11", "HKCategoryValueMenstrualFlowLight"),
|
||||
).toBe(true);
|
||||
|
||||
mocks.upsert
|
||||
.mockRejectedValueOnce(new Error("colliding day"))
|
||||
.mockResolvedValueOnce({ existed: false });
|
||||
|
||||
const stats = await acc.flush();
|
||||
|
||||
expect(stats.samplesConsumed).toBe(2);
|
||||
expect(stats.daysUpserted).toBe(1);
|
||||
expect(stats.daysInserted).toBe(1);
|
||||
expect(stats.daysFailed).toBe(1);
|
||||
expect(stats.firstFailureReason).toBe("colliding day");
|
||||
});
|
||||
|
||||
it("reports clean stats when every day lands", async () => {
|
||||
const acc = new CycleImportAccumulator("u1", "Europe/Berlin");
|
||||
acc.consume(FLOW, "2026-05-10", "3");
|
||||
mocks.upsert.mockResolvedValue({ existed: false });
|
||||
|
||||
const stats = await acc.flush();
|
||||
expect(stats).toMatchObject({
|
||||
samplesConsumed: 1,
|
||||
daysUpserted: 1,
|
||||
daysFailed: 0,
|
||||
firstFailureReason: null,
|
||||
samplesSkippedModuleDisabled: 0,
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -37,8 +37,11 @@ export const HK_CYCLE_DAY_EXTERNAL_PREFIX = "hkcycle:";
|
||||
/** A zeroed cycle-import result — the no-cycle / aborted-flush fallback. */
|
||||
export const EMPTY_CYCLE_IMPORT_STATS: CycleImportStats = {
|
||||
samplesConsumed: 0,
|
||||
samplesSkippedModuleDisabled: 0,
|
||||
daysUpserted: 0,
|
||||
daysInserted: 0,
|
||||
daysFailed: 0,
|
||||
firstFailureReason: null,
|
||||
goalNudged: false,
|
||||
};
|
||||
|
||||
@@ -54,10 +57,24 @@ interface DayBucket {
|
||||
export interface CycleImportStats {
|
||||
/** Reproductive samples consumed into a day-bucket. */
|
||||
samplesConsumed: number;
|
||||
/**
|
||||
* Reproductive samples the export carried but the fold dropped because
|
||||
* cycle tracking is disabled for the account. Named so the "read 900k /
|
||||
* imported 880k" gap stops being unexplained.
|
||||
*/
|
||||
samplesSkippedModuleDisabled: number;
|
||||
/** Day-logs upserted at flush. */
|
||||
daysUpserted: number;
|
||||
/** Day-logs that the write created (vs refreshed). */
|
||||
daysInserted: number;
|
||||
/**
|
||||
* Day-logs whose write FAILED at flush. The flush is fault-isolated per
|
||||
* day, so `daysUpserted` counts what actually landed rather than being
|
||||
* zeroed by one colliding day.
|
||||
*/
|
||||
daysFailed: number;
|
||||
/** The first per-day failure's message, for the operator-facing line. */
|
||||
firstFailureReason: string | null;
|
||||
/** Whether a CycleProfile goal nudge fired. */
|
||||
goalNudged: boolean;
|
||||
}
|
||||
@@ -87,6 +104,16 @@ export class CycleImportAccumulator {
|
||||
return this.byDay.size > 0;
|
||||
}
|
||||
|
||||
/** Samples consumed so far — for the module-disabled skip accounting. */
|
||||
sampleCount(): number {
|
||||
return this.samplesConsumed;
|
||||
}
|
||||
|
||||
/** Accumulated day-buckets — for the whole-flush-failed accounting. */
|
||||
dayCount(): number {
|
||||
return this.byDay.size;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fold one reproductive HK sample into its day-bucket. `dayKey` is the
|
||||
* already-resolved user-local `YYYY-MM-DD`. `rawValue` is the sample's
|
||||
@@ -177,8 +204,11 @@ export class CycleImportAccumulator {
|
||||
async flush(): Promise<CycleImportStats> {
|
||||
const stats: CycleImportStats = {
|
||||
samplesConsumed: this.samplesConsumed,
|
||||
samplesSkippedModuleDisabled: 0,
|
||||
daysUpserted: 0,
|
||||
daysInserted: 0,
|
||||
daysFailed: 0,
|
||||
firstFailureReason: null,
|
||||
goalNudged: false,
|
||||
};
|
||||
if (this.byDay.size === 0) return stats;
|
||||
@@ -230,15 +260,26 @@ export class CycleImportAccumulator {
|
||||
};
|
||||
|
||||
const cycleId = owningCycleId(dayKey);
|
||||
const result = await upsertCycleDayLog(
|
||||
this.userId,
|
||||
entry,
|
||||
this.userTimezone,
|
||||
cycleId,
|
||||
encryptSensitive,
|
||||
);
|
||||
stats.daysUpserted += 1;
|
||||
if (!result.existed) stats.daysInserted += 1;
|
||||
// Fault-isolated per day: one colliding day must not zero the stats
|
||||
// for the days that DID land (the pre-fix shape reported
|
||||
// `samplesConsumed: 0` after partial writes — a false "nothing
|
||||
// happened" over rows already in the table).
|
||||
try {
|
||||
const result = await upsertCycleDayLog(
|
||||
this.userId,
|
||||
entry,
|
||||
this.userTimezone,
|
||||
cycleId,
|
||||
encryptSensitive,
|
||||
);
|
||||
stats.daysUpserted += 1;
|
||||
if (!result.existed) stats.daysInserted += 1;
|
||||
} catch (err: unknown) {
|
||||
stats.daysFailed += 1;
|
||||
stats.firstFailureReason ??=
|
||||
err instanceof Error ? err.message : String(err);
|
||||
continue;
|
||||
}
|
||||
|
||||
// The most-recent contraceptive method across the import drives the
|
||||
// single goal nudge (applied once after the day loop).
|
||||
|
||||
Reference in new issue
Block a user