Merge branch 'release/v1.45.0' into feat/v145-backup

# Conflicts:
#	src/__tests__/baselines/source-file-lines.json
#	src/__tests__/baselines/source-function-lines.json
#	src/__tests__/baselines/version-stamp-comments.json
This commit is contained in:
Marc-André Bombeck committed 2026-10-22 09:59:37 +02:00
commit 0cefac9d68
94 files changed
+14850 -658

No files matched your search

@@ -0,0 +1,111 @@
/**
* Re-importing an Apple Health export the account already holds, against a
* real Postgres.
*
* Every row the insert skips used to cost one UPDATE writing back what was
* stored. A row whose stored values already equal what the update would
* write is now left out: the per-type counts are the same as before (each
* such row counts as updated), the stored rows are the same, and the import
* sends no UPDATE for them. A row whose value changed is still updated, and
* a key the chunk carries twice is still written in order.
*/
import { mkdtempSync, writeFileSync } from "node:fs";
import { join } from "node:path";
import { tmpdir } from "node:os";
import { beforeEach, describe, expect, it } from "vitest";
import { streamParseExportXml } from "@/lib/measurements/import-apple-health-export";
import { makeUser } from "../kit/factories";
import { countStatements } from "./query-counter";
import { getPrismaClient, truncateAllTables } from "./setup";
const prisma = getPrismaClient();
function writeXml(records: string): string {
const dir = mkdtempSync(join(tmpdir(), "healthlog-reimport-"));
const path = join(dir, "export.xml");
writeFileSync(
path,
`<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE HealthData [<!ELEMENT HealthData (Record)*>]>
<HealthData locale="en_US">
${records}
</HealthData>`,
);
return path;
}
function weights(values: number[]): string {
return values
.map(
(value, day) => ` <Record type="HKQuantityTypeIdentifierBodyMass"
unit="kg" sourceName="Scale"
startDate="2026-05-${String(day + 1).padStart(2, "0")} 08:00:00 +0200"
endDate="2026-05-${String(day + 1).padStart(2, "0")} 08:00:00 +0200"
value="${value}"/>`,
)
.join("\n");
}
async function stored(userId: string) {
return prisma.measurement.findMany({
where: { userId, type: "WEIGHT" },
orderBy: { measuredAt: "asc" },
select: { value: true, measuredAt: true, externalId: true },
});
}
describe("Apple Health re-import of unchanged rows", () => {
let userId: string;
const values = Array.from({ length: 20 }, (_, i) => 70 + i / 10);
beforeEach(async () => {
await truncateAllTables(prisma);
userId = (await makeUser()).id;
const first = await streamParseExportXml({
xmlPath: writeXml(weights(values)),
userId,
userTimezone: "Europe/Berlin",
prisma,
});
expect(first.perType.WEIGHT?.inserted).toBe(values.length);
});
it("counts every row as updated, as before, and sends no UPDATE", async () => {
const before = await stored(userId);
const measured = await countStatements(() =>
streamParseExportXml({
xmlPath: writeXml(weights(values)),
userId,
userTimezone: "Europe/Berlin",
prisma,
}),
);
expect(measured.result.perType.WEIGHT?.inserted).toBe(0);
expect(measured.result.perType.WEIGHT?.updated).toBe(values.length);
expect(await stored(userId)).toEqual(before);
const updates = measured.statements.filter((sql) =>
/^\s*UPDATE "public"\."measurements"/i.test(sql),
);
expect(updates).toEqual([]);
});
it("still updates a row whose value changed", async () => {
const changed = [...values];
changed[3] = 99.9;
const result = await streamParseExportXml({
xmlPath: writeXml(weights(changed)),
userId,
userTimezone: "Europe/Berlin",
prisma,
});
expect(result.perType.WEIGHT?.updated).toBe(values.length);
const rows = await stored(userId);
// The externalId hashes the sample, so a new value is a new sample of
// the same instant: it adopts the stored row through the natural key.
expect(rows).toHaveLength(values.length);
expect(rows[3]!.value).toBe(99.9);
});
});
@@ -0,0 +1,230 @@
/**
* The canonical-source pick, against a real Postgres: the shipped statement
* (group the window to `(type, day, source)` first, then `DISTINCT ON`)
* against the statement it replaced (`DISTINCT ON` over every raw row),
* kept verbatim below as the reference.
*
* Both must return exactly the same rows, for every reader shape the helper
* serves (all types or one type, a trailing window or none), under the
* default ladders and under user ladders that reorder them, over a fixture
* that carries what could tell them apart: overlapping sources on the same
* day, a source off the ladder, a day where the winning source's only row is
* tombstoned, readings either side of UTC midnight and of a DST change in
* the user's zone, empty days, and several readings per source per day.
*/
import { beforeAll, describe, expect, it } from "vitest";
import type {
MeasurementSource,
MeasurementType,
} from "@/generated/prisma/client";
import {
buildSourceRankCase,
canonicalMeasurementsCte,
} from "@/lib/analytics/source-rank-sql";
import { makeUser } from "../kit/factories";
import { getPrismaClient, truncateAllTables } from "./setup";
const USER_ID = "user-canonical-parity";
const DAY_MS = 86_400_000;
/** The pick as it stood before it grouped first. */
function legacyCanonicalMeasurementsCte(
rankUnqualified: string,
sinceInterval?: string,
onlyType?: MeasurementType,
): string {
const sinceInner = sinceInterval
? `AND "measured_at" >= (NOW() AT TIME ZONE 'UTC') - INTERVAL '${sinceInterval}'`
: "";
const sinceOuter = sinceInterval
? `AND mm."measured_at" >= (NOW() AT TIME ZONE 'UTC') - INTERVAL '${sinceInterval}'`
: "";
const typeInner = onlyType
? `AND "type" = '${onlyType}'::"measurement_type"`
: "";
const typeOuter = onlyType
? `AND mm."type" = '${onlyType}'::"measurement_type"`
: "";
return `
SELECT mm.*
FROM measurements mm
JOIN (
SELECT DISTINCT ON ("type", date_trunc('day', "measured_at"))
"type" AS t,
date_trunc('day', "measured_at") AS d,
"source" AS canon
FROM measurements
WHERE "user_id" = $1
AND "deleted_at" IS NULL
${sinceInner}
${typeInner}
ORDER BY "type", date_trunc('day', "measured_at"), (${rankUnqualified}), "source"::text
) c
ON c.t = mm."type"
AND c.d = date_trunc('day', mm."measured_at")
AND c.canon = mm."source"
WHERE mm."user_id" = $1
AND mm."deleted_at" IS NULL
${sinceOuter}
${typeOuter}`;
}
/** Small seeded generator, so the fixture is the same on every run. */
function rng(seed: number) {
let a = seed >>> 0;
return () => {
a = (a + 0x6d2b79f5) >>> 0;
let t = a;
t = Math.imul(t ^ (t >>> 15), t | 1);
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
const SOURCES: MeasurementSource[] = [
"APPLE_HEALTH",
"WITHINGS",
"WHOOP",
"OURA",
"MANUAL",
// Off every ladder below: the name tiebreak decides.
"NIGHTSCOUT",
];
const TYPES: MeasurementType[] = [
"PULSE",
"RESTING_HEART_RATE",
"HEART_RATE_VARIABILITY",
"WEIGHT",
"ACTIVITY_STEPS",
"BLOOD_GLUCOSE",
];
async function seed(): Promise<number> {
const prisma = getPrismaClient();
await makeUser({ id: USER_ID, timezone: "Europe/Berlin" });
const random = rng(42);
const now = Date.now();
const rows: Array<{
userId: string;
type: MeasurementType;
source: MeasurementSource;
value: number;
unit: string;
measuredAt: Date;
deletedAt: Date | null;
}> = [];
// 140 days: across the 90-day window edge, and over the last DST change.
for (let day = 0; day < 140; day++) {
if (day % 11 === 5) continue; // an empty day
const dayStart = Math.floor((now - day * DAY_MS) / DAY_MS) * DAY_MS;
for (const type of TYPES) {
const sourceCount = 1 + Math.floor(random() * 3);
const sources = [...SOURCES]
.sort(() => random() - 0.5)
.slice(0, sourceCount);
for (const source of sources) {
const readings = 1 + Math.floor(random() * 4);
for (let r = 0; r < readings; r++) {
// Mostly mid-day, sometimes a minute either side of UTC midnight.
const offset =
r === 0 && random() < 0.2
? random() < 0.5
? 60_000
: DAY_MS - 60_000
: Math.floor(random() * DAY_MS);
rows.push({
userId: USER_ID,
type,
source,
value: Math.round(random() * 1000) / 10,
unit: "x",
measuredAt: new Date(dayStart + offset),
// A tombstone, sometimes the only row its source had that day.
deletedAt: random() < 0.08 ? new Date() : null,
});
}
}
}
}
// The natural identity is unique; drop accidental collisions.
const seen = new Set<string>();
const unique = rows.filter((row) => {
const key = `${row.type}|${row.source}|${row.measuredAt.getTime()}`;
if (seen.has(key)) return false;
seen.add(key);
return true;
});
await prisma.measurement.createMany({ data: unique });
return unique.length;
}
const LADDERS: Array<[string, unknown]> = [
["default ladders", null],
[
"user ladders that reorder them",
{
pulse: ["APPLE_HEALTH", "WITHINGS"],
restingHeartRate: ["OURA", "APPLE_HEALTH", "WHOOP"],
hrv: ["APPLE_HEALTH"],
weight: ["MANUAL", "WITHINGS"],
steps: ["WITHINGS", "APPLE_HEALTH"],
},
],
];
const SHAPES: Array<[string, string | undefined, MeasurementType | undefined]> =
[
["every type, 90 days", "90 days", undefined],
["every type, no window", undefined, undefined],
["one type, 366 days", "366 days", "PULSE"],
["one unranked type, 30 days", "30 days", "BLOOD_GLUCOSE"],
["one type, 7 days", "7 days", "RESTING_HEART_RATE"],
];
describe("canonical-source pick parity", () => {
let seeded = 0;
beforeAll(async () => {
await truncateAllTables(getPrismaClient());
seeded = await seed();
});
it("seeds a fixture worth comparing", () => {
expect(seeded).toBeGreaterThan(2000);
});
for (const [ladderLabel, ladder] of LADDERS) {
for (const [shapeLabel, since, onlyType] of SHAPES) {
it(`returns the same rows: ${ladderLabel}, ${shapeLabel}`, async () => {
const prisma = getPrismaClient();
const rank = buildSourceRankCase(ladder, '"type"', '"source"');
const select = (cte: string) =>
prisma.$queryRawUnsafe<
Array<{ id: string; type: string; source: string }>
>(
`WITH cm AS (${cte}) SELECT id, type::text AS type, source::text AS source FROM cm ORDER BY id`,
USER_ID,
);
const [now, before] = await Promise.all([
select(canonicalMeasurementsCte(rank, since, onlyType)),
select(legacyCanonicalMeasurementsCte(rank, since, onlyType)),
]);
expect(now.length).toBeGreaterThan(0);
expect(now).toEqual(before);
// The pick dropped something, so the comparison is not trivial.
if (!onlyType || onlyType === "PULSE") {
const live = await prisma.measurement.count({
where: {
userId: USER_ID,
deletedAt: null,
...(onlyType ? { type: onlyType } : {}),
},
});
if (!since) expect(now.length).toBeLessThan(live);
}
});
}
}
});
@@ -0,0 +1,209 @@
/**
* The dense-series read, against a real Postgres: the shipped single
* statement (`readDenseSeries`) against the two statements it replaced,
* kept below as the reference (the bucket SQL and the figures SQL are the
* same builders, so the reference differs only in reading the canonical rows
* twice instead of once).
*
* Buckets and figures must match for every shape the route asks for: day
* and hour grains; an hourly-mean type (pulse, two ranked sources), an
* unranked dense type (glucose) and a ranked non-hourly one (oxygen
* saturation); zones with a DST change and a half-hour offset; a fixture
* with gaps, tombstones and readings around local and UTC midnight.
*/
import { beforeAll, describe, expect, it } from "vitest";
import type {
MeasurementSource,
MeasurementType,
} from "@/generated/prisma/client";
import { usesHourlyMeanDay } from "@/lib/measurements/day-statistic";
import {
denseBucketsSql,
denseFiguresSql,
readDenseSeries,
type DenseBucketRow,
type DenseFigures,
} from "@/lib/measurements/dense-series-read";
import { seriesRowsFrom } from "@/lib/measurements/series-canonical";
import { makeUser } from "../kit/factories";
import { getPrismaClient, truncateAllTables } from "./setup";
const prisma = getPrismaClient();
const DAY_MS = 86_400_000;
/** The two statements as the route sent them before. */
async function legacyDenseSeries(args: {
userId: string;
type: MeasurementType;
grain: "day" | "hour";
userTz: string;
since: Date;
seriesRows: string;
}) {
const hourWeighted = args.grain === "day" && usesHourlyMeanDay(args.type);
const bucketRows = await prisma.$queryRawUnsafe<DenseBucketRow[]>(
`${denseBucketsSql(hourWeighted, `${args.seriesRows} WHERE m."measured_at" >= $4`)}
ORDER BY bucket_start ASC`,
args.userId,
args.grain,
args.userTz,
args.since,
);
// The figures statement took the window start as `$2` and the zone as
// `$3`, the latter only for an hourly-mean type.
const hourly = usesHourlyMeanDay(args.type);
const [rawAgg] = await prisma.$queryRawUnsafe<DenseFigures[]>(
`WITH src AS (SELECT m.* FROM ${args.seriesRows} WHERE m."measured_at" >= $2)
${denseFiguresSql(hourly)}`,
args.userId,
args.since,
...(hourly ? [args.userTz] : []),
);
return { bucketRows, rawAgg };
}
function rng(seed: number) {
let a = seed >>> 0;
return () => {
a = (a + 0x6d2b79f5) >>> 0;
let t = a;
t = Math.imul(t ^ (t >>> 15), t | 1);
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
let userId = "";
async function seed() {
userId = (await makeUser({ timezone: "Europe/Berlin" })).id;
const random = rng(7);
const now = Date.now();
const rows: Array<{
userId: string;
type: MeasurementType;
source: MeasurementSource;
value: number;
valueMin: number | null;
valueMax: number | null;
unit: string;
measuredAt: Date;
deletedAt: Date | null;
}> = [];
const plan: Array<[MeasurementType, MeasurementSource[], number]> = [
["PULSE", ["APPLE_HEALTH", "WITHINGS"], 60],
["BLOOD_GLUCOSE", ["NIGHTSCOUT", "MANUAL"], 70],
["OXYGEN_SATURATION", ["APPLE_HEALTH", "WITHINGS"], 96],
];
// 200 days: across the last two DST changes, with gaps.
for (let day = 0; day < 200; day++) {
if (day % 13 === 4) continue;
const dayStart = Math.floor((now - day * DAY_MS) / DAY_MS) * DAY_MS;
for (const [type, sources, base] of plan) {
for (const source of sources) {
if (random() < 0.3) continue;
const readings = 3 + Math.floor(random() * 12);
for (let r = 0; r < readings; r++) {
const at = dayStart + Math.floor(random() * DAY_MS);
const value = Math.round((base + random() * 30) * 10) / 10;
const banded = type === "PULSE" && random() < 0.3;
rows.push({
userId,
type,
source,
value,
valueMin: banded ? value - 10 : null,
valueMax: banded ? value + 15 : null,
unit: "x",
measuredAt: new Date(at),
deletedAt: random() < 0.05 ? new Date() : null,
});
}
}
}
}
const seen = new Set<string>();
await prisma.measurement.createMany({
data: rows.filter((row) => {
const key = `${row.type}|${row.source}|${row.measuredAt.getTime()}`;
if (seen.has(key)) return false;
seen.add(key);
return true;
}),
});
}
const CASES: Array<[MeasurementType, "day" | "hour", number]> = [
["PULSE", "day", 180],
["PULSE", "hour", 60],
["BLOOD_GLUCOSE", "day", 180],
["BLOOD_GLUCOSE", "hour", 30],
["OXYGEN_SATURATION", "day", 120],
];
const ZONES = ["UTC", "Europe/Berlin", "Asia/Kolkata", "America/New_York"];
function close(a: number | null | undefined, b: number | null | undefined) {
if (a == null || b == null) return expect(a ?? null).toBe(b ?? null);
expect(Math.abs(a - b)).toBeLessThanOrEqual(1e-9 * Math.max(1, Math.abs(b)));
}
describe("dense-series read parity", () => {
beforeAll(async () => {
await truncateAllTables(prisma);
await seed();
});
for (const [type, grain, days] of CASES) {
for (const userTz of ZONES) {
it(`matches the two statements: ${type}, ${grain}, ${days} d, ${userTz}`, async () => {
const args = {
userId,
type,
grain,
userTz,
since: new Date(Date.now() - days * DAY_MS),
seriesRows: seriesRowsFrom(null, type, days),
};
const [now, before] = await Promise.all([
readDenseSeries(args),
legacyDenseSeries(args),
]);
expect(before.bucketRows.length).toBeGreaterThan(10);
expect(now.bucketRows.map((r) => r.bucket_start.getTime())).toEqual(
before.bucketRows.map((r) => r.bucket_start.getTime()),
);
now.bucketRows.forEach((row, i) => {
const ref = before.bucketRows[i]!;
close(row.mean, ref.mean);
close(row.min_value, ref.min_value);
close(row.max_value, ref.max_value);
});
expect(now.rawAgg?.n).toBe(before.rawAgg?.n);
close(now.rawAgg?.mean, before.rawAgg?.mean);
close(now.rawAgg?.min, before.rawAgg?.min);
close(now.rawAgg?.max, before.rawAgg?.max);
close(now.rawAgg?.sd, before.rawAgg?.sd);
});
}
}
it("answers no figures row for an hourly-mean type without readings, as before", async () => {
const empty = await makeUser();
const args = {
userId: empty.id,
type: "PULSE" as const,
grain: "day" as const,
userTz: "UTC",
since: new Date(Date.now() - 365 * DAY_MS),
seriesRows: seriesRowsFrom(null, "PULSE", 365),
};
const [now, before] = await Promise.all([
readDenseSeries(args),
legacyDenseSeries(args),
]);
expect(now).toEqual(before);
expect(now.rawAgg).toBeUndefined();
});
});
@@ -66,16 +66,16 @@ const DAY_MS = 86_400_000;
* the header for how to move one.
*/
const BUDGET = {
authMe: 18,
dashboardSummary: 86,
dashboardSnapshot: 103,
authMe: 13,
dashboardSummary: 84,
dashboardSnapshot: 99,
series30: 6,
series365: 6,
series3650: 6,
dayView: 48,
healthStatus: 60,
correlations: 44,
batchIngest500: 31,
series365: 5,
series3650: 5,
dayView: 47,
healthStatus: 59,
correlations: 43,
batchIngest500: 28,
} as const;
type Handler = (
@@ -0,0 +1,189 @@
/**
* `POST /api/measurements/batch`: a `stats:` row re-posted with nothing
* changed, through the real API kit against the real database.
*
* The phone re-sends the same daily totals on every sync. Such a re-post:
* - answers exactly what it answered before: per entry `updated`, and the
* same `updated` count in the summary;
* - writes nothing (the stored row keeps its `syncVersion` and
* `updatedAt`), and starts nothing: no rollup refold, no cache
* invalidation, no PR detection;
* - goes the old way as soon as one column differs (the value, the unit,
* the device), and for a tombstoned row (a resurrection), so those are
* written and start the tail as before;
* - costs a handful of statements for a batch of them, instead of a
* reconciler transaction each.
*/
import { beforeEach, describe, expect, it, vi } from "vitest";
import { POST } from "@/app/api/measurements/batch/route";
import { callRoute } from "../../kit/call-route";
import {
makeSession,
makeUser,
type SessionFixture,
} from "../../kit/factories";
import { countStatements } from "../query-counter";
import { getPrismaClient, truncateAllTables } from "../setup";
const tail = vi.hoisted(() => ({ rollups: 0, invalidations: 0, prs: 0 }));
vi.mock("@/lib/rollups/after-measurement-mutation", async () => {
const actual = await vi.importActual<
typeof import("@/lib/rollups/after-measurement-mutation")
>("@/lib/rollups/after-measurement-mutation");
return {
...actual,
afterMeasurementMutation: (
...args: Parameters<typeof actual.afterMeasurementMutation>
) => {
tail.rollups += 1;
return actual.afterMeasurementMutation(...args);
},
};
});
vi.mock("@/lib/cache/invalidate", async () => {
const actual = await vi.importActual<typeof import("@/lib/cache/invalidate")>(
"@/lib/cache/invalidate",
);
return {
...actual,
invalidateUserMeasurements: (
...args: Parameters<typeof actual.invalidateUserMeasurements>
) => {
tail.invalidations += 1;
return actual.invalidateUserMeasurements(...args);
},
};
});
vi.mock("@/lib/jobs/pr-detection", async () => {
const actual = await vi.importActual<
typeof import("@/lib/jobs/pr-detection")
>("@/lib/jobs/pr-detection");
return {
...actual,
enqueuePrDetection: async () => {
tail.prs += 1;
},
};
});
const prisma = getPrismaClient();
const ROUTE = "/api/measurements/batch";
const DAYS = ["2026-10-01", "2026-10-02", "2026-10-03", "2026-10-04"];
function stepsEntry(day: string, value: number, deviceType = "watch") {
return {
hkIdentifier: "HKQuantityTypeIdentifierStepCount",
value,
unit: "count",
startDate: `${day}T00:00:00.000Z`,
endDate: `${day}T23:59:59.000Z`,
externalId: `stats:HKQuantityTypeIdentifierStepCount:${day}`,
deviceType,
};
}
let auth: SessionFixture;
let userId: string;
async function post(entries: unknown[]) {
return callRoute<{
updated: number;
inserted: number;
entries: Array<{ status: string }>;
}>(POST, { method: "POST", route: ROUTE, auth, body: { entries } });
}
function resetTail() {
tail.rollups = 0;
tail.invalidations = 0;
tail.prs = 0;
}
beforeEach(async () => {
await truncateAllTables(prisma);
const user = await makeUser({ timezone: "UTC" });
userId = user.id;
auth = await makeSession(user);
const first = await post(DAYS.map((d, i) => stepsEntry(d, 1000 + i)));
expect(first.status).toBe(200);
expect(first.data.inserted).toBe(DAYS.length);
resetTail();
});
async function storedVersions() {
const rows = await prisma.measurement.findMany({
where: { userId, externalId: { startsWith: "stats:" } },
orderBy: { externalId: "asc" },
select: { syncVersion: true, updatedAt: true, value: true },
});
return rows;
}
describe("an unchanged stats: re-post", () => {
it("answers `updated` as before, writes nothing and starts nothing", async () => {
const before = await storedVersions();
const measured = await countStatements(() =>
post(DAYS.map((d, i) => stepsEntry(d, 1000 + i))),
);
const res = measured.result;
expect(res.status).toBe(200);
expect(res.data.updated).toBe(DAYS.length);
expect(res.data.inserted).toBe(0);
expect(res.data.entries.map((e) => e.status)).toEqual(
DAYS.map(() => "updated"),
);
expect(await storedVersions()).toEqual(before);
expect(tail).toEqual({ rollups: 0, invalidations: 0, prs: 0 });
// Four reconciler transactions used to be about forty statements.
expect(measured.count).toBeLessThanOrEqual(12);
});
it("writes and starts the tail when one column differs", async () => {
const before = (await storedVersions()).map((r) => r.syncVersion);
const res = await post([
stepsEntry(DAYS[0]!, 1000), // unchanged
stepsEntry(DAYS[1]!, 5555), // new value
stepsEntry(DAYS[2]!, 1002, "phone"), // new device
]);
expect(res.data.entries.map((e) => e.status)).toEqual([
"updated",
"updated",
"updated",
]);
expect(res.data.updated).toBe(3);
const after = await storedVersions();
expect(after.map((r) => r.syncVersion)).toEqual([
before[0],
before[1]! + 1,
before[2]! + 1,
before[3],
]);
expect(after[1]!.value).toBe(5555);
expect(tail.rollups).toBe(1);
expect(tail.invalidations).toBeGreaterThanOrEqual(1);
expect(tail.prs).toBe(1);
});
it("resurrects a tombstoned row the old way", async () => {
await prisma.measurement.updateMany({
where: {
userId,
externalId: `stats:HKQuantityTypeIdentifierStepCount:${DAYS[3]}`,
},
data: { deletedAt: new Date() },
});
const res = await post([stepsEntry(DAYS[3]!, 1003)]);
expect(res.data.entries[0]!.status).toBe("updated");
const row = await prisma.measurement.findFirst({
where: {
userId,
externalId: `stats:HKQuantityTypeIdentifierStepCount:${DAYS[3]}`,
},
select: { deletedAt: true },
});
expect(row?.deletedAt).toBeNull();
expect(tail.rollups).toBe(1);
});
});
+17 -2
View File
@@ -117,8 +117,21 @@ function countBuildsPerKey(cache: {
return builds;
}
/**
* The slice's pre-fold remainder is a cell of its own in the same bucket (a
* closed block, `pre-fold-remainder.ts`), built when the slice takes the
* rollup path; it is counted apart from the route's two cells.
*/
function preFoldBuildsOf(builds: Map<string, number>, userId: string) {
return [...builds.entries()]
.filter(([k]) => k.startsWith(`${userId}|pre-fold-remainder|`))
.reduce((n, [, c]) => n + c, 0);
}
function buildsOf(builds: Map<string, number>, userId: string) {
const entries = [...builds.entries()].filter(([k]) => k.startsWith(userId));
const entries = [...builds.entries()].filter(
([k]) => k.startsWith(userId) && !k.includes("|pre-fold-remainder|"),
);
return {
defaultBody: entries
.filter(([k]) => k.startsWith(`${userId}|default|`))
@@ -230,7 +243,9 @@ describe("v1.4.34 IW-G — server-cache route integration", () => {
summaries: 2,
keys: 2,
});
expect(caches.analytics.stats().misses).toBe(4);
expect(caches.analytics.stats().misses).toBe(
4 + preFoldBuildsOf(builds, user.id),
);
// Sanity: the body reflects the new write.
const body = (await third.json()) as {