test(workouts): pin sport provenance across the provider ingests

Behavioural cases first: the Google mapper keeps the raw `exerciseType`
when the sport falls back to "other", reports a missing one as null
rather than an empty string, and the ingest writes it under
`metadata.googleExerciseType` on both the insert and the update arm — the
update is what a full re-sync goes through, so a row written by an older
build repairs in place. The key is omitted rather than nulled when a
session carries no type, so a re-sync cannot wipe what an earlier one
recorded.

Then the structural guard, because the behavioural ones only cover the
provider that was reported. It freezes the set of sport-label mappers and
the set of files creating Workout rows, and asserts every mapping ingest
carries its raw label into metadata. Fitbit has the same gap and is
excluded on purpose — it ships on its own, small and isolated, per the
standing rule for that provider. The exception is an assertion, not an
omission: when Fitbit gains the write, the guard fails and forces the
entry to move. An exception nobody has to retire is how this gap survived
in the first place.
This commit is contained in:
Marc-André Bombeck committed 2026-07-27 22:45:54 +02:00
1 parent 3db4c33f49
commit 39aaef27b1
3 files changed
+348

No files matched your search

@@ -533,6 +533,8 @@ describe("mapWorkout — exercise session", () => {
});
expect(w!.startedAt.toISOString()).toBe("2026-06-03T07:00:00.000Z");
expect(w!.endedAt.toISOString()).toBe("2026-06-03T07:45:00.000Z");
// Provenance rides along even when the mapping succeeded.
expect(w!.sportTypeRaw).toBe("RUNNING");
});
it("falls back to a start-anchored externalId when the resource name is absent", () => {
@@ -563,12 +565,56 @@ describe("mapWorkout — exercise session", () => {
).toBeNull();
});
it("keeps the raw exerciseType when the sport falls back to 'other'", () => {
// The whole point of the provenance field: `sportType` alone cannot say
// whether Google reported a sport the map has never heard of or a genuine
// OTHER_WORKOUT, and the answer is not recoverable from anywhere else.
const w = mapWorkout({
name: "users/me/dataTypes/exercise/dataPoints/kite1",
exercise: {
exerciseType: " KITESURFING ",
interval: {
startTime: "2026-06-04T10:00:00.000Z",
endTime: "2026-06-04T11:00:00.000Z",
},
},
});
expect(w!.sportType).toBe("other");
expect(w!.sportTypeRaw).toBe("KITESURFING");
});
it("reports a missing or blank exerciseType as null, not an empty string", () => {
const noType = mapWorkout({
exercise: {
interval: {
startTime: "2026-06-04T12:00:00.000Z",
endTime: "2026-06-04T12:30:00.000Z",
},
},
});
expect(noType!.sportType).toBe("other");
expect(noType!.sportTypeRaw).toBeNull();
const blankType = mapWorkout({
exercise: {
exerciseType: " ",
interval: {
startTime: "2026-06-04T13:00:00.000Z",
endTime: "2026-06-04T13:30:00.000Z",
},
},
});
expect(blankType!.sportTypeRaw).toBeNull();
});
it("resolves the UPPERCASE ExerciseType enum, with a generic fallback", () => {
expect(mapGoogleHealthSportType("STRENGTH_TRAINING")).toBe("strength");
expect(mapGoogleHealthSportType("HIGH_INTENSITY_INTERVAL_TRAINING")).toBe(
"hiit",
);
expect(mapGoogleHealthSportType("CYCLING")).toBe("cycling");
// Lossy by design — `Workout.metadata.googleExerciseType` is what carries
// the original (see the mapWorkout provenance cases above).
expect(mapGoogleHealthSportType("KITESURFING")).toBe("other");
expect(mapGoogleHealthSportType("")).toBe("other");
});
@@ -47,6 +47,7 @@ const startedAt = new Date("2026-07-19T08:00:00.000Z");
const mapped = {
externalId: "google-1",
sportType: "running",
sportTypeRaw: "RUNNING",
startedAt,
endedAt: new Date("2026-07-19T09:00:00.000Z"),
durationSec: 3600,
@@ -124,3 +125,52 @@ describe("syncUserWorkout — exact inserted identity", () => {
await expect(syncUserWorkout("user-1")).resolves.toBe(1);
});
});
describe("syncUserWorkout — raw activity-type provenance", () => {
it("writes the raw exerciseType to metadata on insert", async () => {
createManyAndReturn.mockResolvedValue([{ id: "new-workout", startedAt }]);
mapWorkout.mockReturnValue({ ...mapped, sportTypeRaw: "KITESURFING" });
await expect(syncUserWorkout("user-1")).resolves.toBe(1);
expect(createManyAndReturn).toHaveBeenCalledWith(
expect.objectContaining({
data: expect.objectContaining({
metadata: { googleExerciseType: "KITESURFING" },
}),
}),
);
});
it("repairs an existing row in place, because the update reuses the payload", async () => {
// A row synced before the provenance write existed has no metadata; the
// update arm is what a full re-sync goes through, so it has to carry the
// key too or the repair path advertised to users does nothing.
createManyAndReturn.mockResolvedValue([]);
mapWorkout.mockReturnValue({ ...mapped, sportTypeRaw: "KITESURFING" });
await expect(syncUserWorkout("user-1")).resolves.toBe(1);
expect(update).toHaveBeenCalledWith(
expect.objectContaining({
data: expect.objectContaining({
metadata: { googleExerciseType: "KITESURFING" },
}),
}),
);
});
it("omits the key entirely when the session carried no type", async () => {
// Omitted, not null: an update writing `metadata: null` would wipe what an
// earlier sync recorded.
createManyAndReturn.mockResolvedValue([]);
mapWorkout.mockReturnValue({ ...mapped, sportTypeRaw: null });
await expect(syncUserWorkout("user-1")).resolves.toBe(1);
const insertData = createManyAndReturn.mock.calls[0][0].data;
const updateData = update.mock.calls[0][0].data;
expect("metadata" in insertData).toBe(false);
expect("metadata" in updateData).toBe(false);
});
});