mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-16 15:23:29 +00:00
feat(integrations): tell the Apple Health card how the last sync arrived
The batch route has accepted a `syncTrigger` since v1.32.8 and put it on the wide event, where no human reads it. Half of what was promised got built: the value is written and appears in three places in the codebase, none of which is a screen anyone looks at. Persist it on the account, alongside the instant of the most recent batch that arrived without the app being opened, and publish both from `GET /api/integrations/healthkit`. The card now names how the last sync arrived and when data last came in on its own. `healthKitLastSyncedAt` alone reads identically whether the phone delivers by itself or only while someone has the app open, which is why "is background delivery working" could not be settled from either side. A batch that declares no trigger writes null rather than leaving the previous one standing — the field describes this sync, and an older client's silence is not evidence about it. A foreground batch leaves the background timestamp alone; overwriting it would erase the only answer the card has. Also here: WHOOP, Fitbit and Google Health join the workout-freshness table. All three write workouts, and without an entry their cards could report every measurement type fresh while the workout leg had been silent for weeks. And the status pill drops a generic `aria-label` that replaced its own accessible name, so a screen reader announced the same words for a healthy connection as for a broken one.
This commit is contained in:
1 parent
06935256b0
commit
4e95df9e8d
7 files changed
+507
-16
No files matched your search
@@ -130,6 +130,36 @@ describe("getSourceMetricFreshness", () => {
|
||||
{ type: "WORKOUTS", lastSeenAt: "2026-07-19T17:00:00.000Z" },
|
||||
]);
|
||||
});
|
||||
|
||||
it.each(["whoop", "fitbit", "google-health"] as const)(
|
||||
"carries the workout leg for %s, which writes workouts too",
|
||||
async (integration) => {
|
||||
const source = {
|
||||
whoop: "WHOOP",
|
||||
fitbit: "FITBIT",
|
||||
"google-health": "GOOGLE_HEALTH",
|
||||
}[integration];
|
||||
|
||||
// These three were missing from the workout table, so a card could
|
||||
// report every measurement type fresh while the workout leg had been
|
||||
// silent for weeks — the disclosure had no row that could go quiet.
|
||||
groupByMock.mockResolvedValue([
|
||||
{
|
||||
source,
|
||||
type: "SLEEP_DURATION",
|
||||
_max: { measuredAt: new Date("2026-07-24T04:00:00.000Z") },
|
||||
},
|
||||
]);
|
||||
workoutGroupByMock.mockResolvedValue([
|
||||
{ source, _max: { startedAt: new Date("2026-06-01T09:00:00.000Z") } },
|
||||
]);
|
||||
|
||||
expect((await getSourceMetricFreshness("u1"))[integration]).toEqual([
|
||||
{ type: "SLEEP_DURATION", lastSeenAt: "2026-07-24T04:00:00.000Z" },
|
||||
{ type: "WORKOUTS", lastSeenAt: "2026-06-01T09:00:00.000Z" },
|
||||
]);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
describe("getSourceFreshness — one source, for Apple Health", () => {
|
||||
|
||||
@@ -109,7 +109,7 @@ describe("recordSyncSuccess", () => {
|
||||
},
|
||||
persistentFailureStartedAt: null,
|
||||
failingSinceAt: null,
|
||||
failingLeg: null,
|
||||
failingLegs: [],
|
||||
alertedAt: null,
|
||||
});
|
||||
});
|
||||
@@ -496,7 +496,7 @@ describe("markReauthRequired / markDisconnected / markReconnected", () => {
|
||||
},
|
||||
persistentFailureStartedAt: null,
|
||||
failingSinceAt: null,
|
||||
failingLeg: null,
|
||||
failingLegs: [],
|
||||
alertedAt: null,
|
||||
});
|
||||
});
|
||||
@@ -521,7 +521,7 @@ describe("markReauthRequired / markDisconnected / markReconnected", () => {
|
||||
},
|
||||
persistentFailureStartedAt: null,
|
||||
failingSinceAt: null,
|
||||
failingLeg: null,
|
||||
failingLegs: [],
|
||||
alertedAt: null,
|
||||
});
|
||||
});
|
||||
@@ -840,7 +840,7 @@ describe("resumeIntegrationFromPark", () => {
|
||||
},
|
||||
persistentFailureStartedAt: null,
|
||||
failingSinceAt: null,
|
||||
failingLeg: null,
|
||||
failingLegs: [],
|
||||
alertedAt: null,
|
||||
});
|
||||
|
||||
@@ -1032,7 +1032,7 @@ describe("recordSyncSuccess — leg scoping", () => {
|
||||
it("leaves a sibling leg's error standing and only records the attempt", async () => {
|
||||
vi.mocked(prisma.integrationStatus.findUnique).mockResolvedValueOnce({
|
||||
state: "error_transient",
|
||||
failingLeg: "sleep",
|
||||
failingLegs: ["sleep"],
|
||||
} as never);
|
||||
vi.mocked(prisma.integrationStatus.update).mockResolvedValueOnce(
|
||||
{} as never,
|
||||
@@ -1044,13 +1044,59 @@ describe("recordSyncSuccess — leg scoping", () => {
|
||||
const args = vi.mocked(prisma.integrationStatus.update).mock.calls[0]![0]!;
|
||||
// `lastSuccessAt` deliberately absent: it is what the card's
|
||||
// "connected · X ago" reads, and the sleep pipe has delivered nothing.
|
||||
expect(args.data).toEqual({ lastAttemptAt: expect.any(Date) });
|
||||
// `lastError` is absent too — sleep still owns the message on the row.
|
||||
expect(args.data).toEqual({
|
||||
lastAttemptAt: expect.any(Date),
|
||||
failingLegs: ["sleep"],
|
||||
});
|
||||
});
|
||||
|
||||
it("drops only its own leg when several are failing at once", async () => {
|
||||
vi.mocked(prisma.integrationStatus.findUnique).mockResolvedValueOnce({
|
||||
state: "error_transient",
|
||||
failingLegs: ["sleep", "activity", "ecg"],
|
||||
} as never);
|
||||
vi.mocked(prisma.integrationStatus.update).mockResolvedValueOnce(
|
||||
{} as never,
|
||||
);
|
||||
|
||||
await recordSyncSuccess("u1", "withings", { leg: "activity" });
|
||||
|
||||
expect(prisma.integrationStatus.upsert).not.toHaveBeenCalled();
|
||||
const args = vi.mocked(prisma.integrationStatus.update).mock.calls[0]![0]!;
|
||||
// The single slot could hold one of these three. Two legs are still
|
||||
// failing, and both are still on the row.
|
||||
expect(args.data).toMatchObject({ failingLegs: ["sleep", "ecg"] });
|
||||
// `activity` did not write the message the row holds — `ecg` did, and it
|
||||
// is still failing — so the message stays.
|
||||
expect(args.data).not.toHaveProperty("lastError");
|
||||
});
|
||||
|
||||
it("clears the error message when the leg that wrote it recovers first", async () => {
|
||||
vi.mocked(prisma.integrationStatus.findUnique).mockResolvedValueOnce({
|
||||
state: "error_transient",
|
||||
failingLegs: ["sleep", "ecg"],
|
||||
} as never);
|
||||
vi.mocked(prisma.integrationStatus.update).mockResolvedValueOnce(
|
||||
{} as never,
|
||||
);
|
||||
|
||||
// `ecg` is last, so it failed most recently and owns `lastError`.
|
||||
await recordSyncSuccess("u1", "withings", { leg: "ecg" });
|
||||
|
||||
const args = vi.mocked(prisma.integrationStatus.update).mock.calls[0]![0]!;
|
||||
// Keeping the message would leave the row describing a leg that is now
|
||||
// healthy while `sleep` is the one actually failing.
|
||||
expect(args.data).toMatchObject({
|
||||
failingLegs: ["sleep"],
|
||||
lastError: null,
|
||||
});
|
||||
});
|
||||
|
||||
it("clears the row whole when the failing leg is the one that succeeded", async () => {
|
||||
vi.mocked(prisma.integrationStatus.findUnique).mockResolvedValueOnce({
|
||||
state: "error_transient",
|
||||
failingLeg: "sleep",
|
||||
failingLegs: ["sleep"],
|
||||
} as never);
|
||||
vi.mocked(prisma.integrationStatus.upsert).mockResolvedValueOnce(
|
||||
{} as never,
|
||||
@@ -1064,14 +1110,14 @@ describe("recordSyncSuccess — leg scoping", () => {
|
||||
state: "connected",
|
||||
lastError: null,
|
||||
failingSinceAt: null,
|
||||
failingLeg: null,
|
||||
failingLegs: [],
|
||||
});
|
||||
});
|
||||
|
||||
it("clears the row whole when the failure was never attributed to a leg", async () => {
|
||||
vi.mocked(prisma.integrationStatus.findUnique).mockResolvedValueOnce({
|
||||
state: "error_transient",
|
||||
failingLeg: null,
|
||||
failingLegs: [],
|
||||
} as never);
|
||||
vi.mocked(prisma.integrationStatus.upsert).mockResolvedValueOnce(
|
||||
{} as never,
|
||||
@@ -1097,7 +1143,7 @@ describe("recordSyncSuccess — leg scoping", () => {
|
||||
it("does not treat a healthy row's stale leg marker as a held error", async () => {
|
||||
vi.mocked(prisma.integrationStatus.findUnique).mockResolvedValueOnce({
|
||||
state: "connected",
|
||||
failingLeg: "sleep",
|
||||
failingLegs: ["sleep"],
|
||||
} as never);
|
||||
vi.mocked(prisma.integrationStatus.upsert).mockResolvedValueOnce(
|
||||
{} as never,
|
||||
@@ -1107,4 +1153,102 @@ describe("recordSyncSuccess — leg scoping", () => {
|
||||
|
||||
expect(prisma.integrationStatus.upsert).toHaveBeenCalledOnce();
|
||||
});
|
||||
|
||||
it("reads a malformed leg column as unattributed rather than guessing", async () => {
|
||||
vi.mocked(prisma.integrationStatus.findUnique).mockResolvedValueOnce({
|
||||
state: "error_transient",
|
||||
// Not an array of strings — a legacy shape, or a hand-edited row.
|
||||
failingLegs: { sleep: true },
|
||||
} as never);
|
||||
vi.mocked(prisma.integrationStatus.upsert).mockResolvedValueOnce(
|
||||
{} as never,
|
||||
);
|
||||
|
||||
await recordSyncSuccess("u1", "withings", { leg: "ecg" });
|
||||
|
||||
// Unattributed means any success clears — the pre-existing semantics for
|
||||
// a failure that named no leg, not an invented leg name.
|
||||
expect(prisma.integrationStatus.upsert).toHaveBeenCalledOnce();
|
||||
});
|
||||
});
|
||||
|
||||
describe("recordSyncFailure — leg set", () => {
|
||||
function writtenUpdate() {
|
||||
return vi.mocked(prisma.integrationStatus.upsert).mock.calls[0]![0]!
|
||||
.update as Record<string, unknown>;
|
||||
}
|
||||
|
||||
it("adds a second failing leg instead of overwriting the first", async () => {
|
||||
vi.mocked(prisma.integrationStatus.findUnique).mockResolvedValueOnce({
|
||||
consecutiveFailuresByKind: {
|
||||
transient: 1,
|
||||
reauth_required: 0,
|
||||
persistent: 0,
|
||||
},
|
||||
persistentFailureStartedAt: null,
|
||||
failingSinceAt: new Date("2026-01-01T00:00:00Z"),
|
||||
failingLegs: ["sleep"],
|
||||
alertedAt: null,
|
||||
} as never);
|
||||
|
||||
await recordSyncFailure({
|
||||
userId: "u1",
|
||||
integration: "withings",
|
||||
kind: "transient",
|
||||
message: "Withings activity error: 503 - upstream",
|
||||
leg: "activity",
|
||||
});
|
||||
|
||||
expect(writtenUpdate().failingLegs).toEqual(["sleep", "activity"]);
|
||||
});
|
||||
|
||||
it("moves a re-failing leg to the end so it owns the message it just wrote", async () => {
|
||||
vi.mocked(prisma.integrationStatus.findUnique).mockResolvedValueOnce({
|
||||
consecutiveFailuresByKind: {
|
||||
transient: 2,
|
||||
reauth_required: 0,
|
||||
persistent: 0,
|
||||
},
|
||||
persistentFailureStartedAt: null,
|
||||
failingSinceAt: new Date("2026-01-01T00:00:00Z"),
|
||||
failingLegs: ["sleep", "activity"],
|
||||
alertedAt: null,
|
||||
} as never);
|
||||
|
||||
await recordSyncFailure({
|
||||
userId: "u1",
|
||||
integration: "withings",
|
||||
kind: "transient",
|
||||
message: "Withings sleep error: 503 - upstream",
|
||||
leg: "sleep",
|
||||
});
|
||||
|
||||
// No duplicate, and `sleep` is last because it wrote `lastError`.
|
||||
expect(writtenUpdate().failingLegs).toEqual(["activity", "sleep"]);
|
||||
});
|
||||
|
||||
it("leaves the set alone when the failure names no leg", async () => {
|
||||
vi.mocked(prisma.integrationStatus.findUnique).mockResolvedValueOnce({
|
||||
consecutiveFailuresByKind: {
|
||||
transient: 1,
|
||||
reauth_required: 0,
|
||||
persistent: 0,
|
||||
},
|
||||
persistentFailureStartedAt: null,
|
||||
failingSinceAt: new Date("2026-01-01T00:00:00Z"),
|
||||
failingLegs: ["sleep"],
|
||||
alertedAt: null,
|
||||
} as never);
|
||||
|
||||
await recordSyncFailure({
|
||||
userId: "u1",
|
||||
integration: "withings",
|
||||
kind: "transient",
|
||||
message: "Withings error: 503 - upstream",
|
||||
});
|
||||
|
||||
// An unattributed failure carries nothing to record; erasing `sleep` would
|
||||
// let the sleep leg's next success clear an error it did not fix.
|
||||
expect(writtenUpdate().failingLegs).toEqual(["sleep"]);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user