mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-16 15:23:29 +00:00
Close five honesty gaps on the integration surface
Each of these made the server report something that was not so, and each now
has a test that fails without the fix.
The Withings credential DELETE wrote no audit row and left the ledger at
whatever state it last held. Fitbit, Google Health and WHOOP all park theirs,
and three of them say in their own comments that they do it for parity with a
list that never included Withings. A client reading `state` after the teardown
was told `connected` about a connection that no longer existed.
Oura's and Strava's credential DELETEs audited only when an access token
happened to be present, so removing a stored pair that had never completed
OAuth left no trail at all. Polar's twin has always done both unconditionally;
they match it now. The old Oura test asserted the skip, which is why the gap
survived a rewrite — that expectation is inverted here, with the reason next to
it.
The Withings and WHOOP manual syncs had no rate limiter, while `fullSync` walks
full history against a per-user provider budget. The Fitbit route's own comment
records that it was the outlier until it got one; these two were never brought
along. Both now carry the same 5/60s baseline and 1/hour full-sync buckets as
their siblings.
Every sync route parsed its body inside a catch commented "no body provided",
which was true for one of the three ways to reach it. An absent body still
means an incremental run — that is the documented shape and the web cards send
it. A body that is present and unparseable is now 400, and one that fails the
schema is 422: `{"fullSync": "true"}` used to be read as false and answered 200,
so a client that asked for full history and mistyped the value was told the run
it never requested had succeeded. Unknown keys are still ignored. The four
copies collapse into one helper.
The consolidated integration status swallowed a failed freshness query into an
empty array, which is exactly what a record with nothing in it also produces —
so the signal built to say "this metric has gone quiet" answered "nothing has
gone quiet" when it had failed to look. `metricFreshnessDegraded` separates the
two, as one flag rather than one per entry, because it is a single query for all
eight providers.
The contract follows the behaviour in the same commit.
This commit is contained in:
1 parent
7f67c24ecc
commit
b6e34d1264
18 files changed
+826
-108
No files matched your search
@@ -70,10 +70,19 @@ type Entry = {
|
||||
metricFreshness?: Array<{ type: string; lastSeenAt: string; stale: boolean }>;
|
||||
};
|
||||
|
||||
async function fetchEnvelope(): Promise<{
|
||||
integrations: Entry[];
|
||||
metricFreshnessDegraded: boolean;
|
||||
}> {
|
||||
return (await (GET as unknown as () => Promise<{ data: unknown }>)())
|
||||
.data as {
|
||||
integrations: Entry[];
|
||||
metricFreshnessDegraded: boolean;
|
||||
};
|
||||
}
|
||||
|
||||
async function fetchEntries(): Promise<Entry[]> {
|
||||
const res = (await (GET as unknown as () => Promise<{ data: unknown }>)())
|
||||
.data as { integrations: Entry[] };
|
||||
return res.integrations;
|
||||
return (await fetchEnvelope()).integrations;
|
||||
}
|
||||
|
||||
describe("/api/integrations/status — Polar/Oura fold (04-M2)", () => {
|
||||
@@ -277,3 +286,61 @@ describe("/api/integrations/status — a provider that has delivered nothing for
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* The freshness read is fail-soft, and it used to be fail-silent with it.
|
||||
*
|
||||
* `getSourceMetricFreshness` is one query for all eight providers, wrapped in a
|
||||
* `.catch(() => ({}))` so a groupBy hiccup never 500s the whole settings
|
||||
* surface. The fallback makes every entry's `metricFreshness` an empty array —
|
||||
* which is byte-identical to what a user with nothing recorded sees. So the
|
||||
* honesty signal built to say "this metric has gone quiet" answered "nothing
|
||||
* has gone quiet" when it had in fact failed to look.
|
||||
*
|
||||
* `metricFreshnessDegraded` separates the two. It stays a flag rather than a
|
||||
* nullable array per entry because the query fails for all eight providers at
|
||||
* once or for none of them.
|
||||
*/
|
||||
describe("/api/integrations/status — the freshness read failed", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
for (const key of Object.keys(perIntegrationLedger)) {
|
||||
delete perIntegrationLedger[key];
|
||||
}
|
||||
userFind.mockResolvedValue({});
|
||||
whoopFind.mockResolvedValue(null);
|
||||
polarAvailable.mockResolvedValue(true);
|
||||
ouraAvailable.mockResolvedValue(false);
|
||||
});
|
||||
|
||||
it("reports the degradation instead of passing an empty list off as an answer", async () => {
|
||||
(prisma.measurement.groupBy as ReturnType<typeof vi.fn>).mockRejectedValue(
|
||||
new Error("statement timeout"),
|
||||
);
|
||||
|
||||
const envelope = await fetchEnvelope();
|
||||
|
||||
expect(envelope.metricFreshnessDegraded).toBe(true);
|
||||
// Still fail-soft: the rest of the envelope survives, all eight entries
|
||||
// present, because a freshness hiccup is never worth the settings surface.
|
||||
expect(envelope.integrations).toHaveLength(8);
|
||||
for (const entry of envelope.integrations) {
|
||||
expect(entry.metricFreshness).toEqual([]);
|
||||
}
|
||||
});
|
||||
|
||||
it("reports no degradation when the read simply found nothing", async () => {
|
||||
(prisma.measurement.groupBy as ReturnType<typeof vi.fn>).mockResolvedValue(
|
||||
[],
|
||||
);
|
||||
(prisma.workout.groupBy as ReturnType<typeof vi.fn>).mockResolvedValue([]);
|
||||
|
||||
const envelope = await fetchEnvelope();
|
||||
|
||||
// The same empty lists as above, and this is the case they honestly mean.
|
||||
expect(envelope.metricFreshnessDegraded).toBe(false);
|
||||
for (const entry of envelope.integrations) {
|
||||
expect(entry.metricFreshness).toEqual([]);
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -149,9 +149,20 @@ export const GET = apiHandler(async () => {
|
||||
// single metric type has gone silent even while the integration reads green
|
||||
// ("connected · 5 min ago"). Fail-soft: this is an honesty signal, never worth
|
||||
// 500-ing the whole Settings card, so a groupBy hiccup degrades to no data.
|
||||
const metricFreshness = await getSourceMetricFreshness(user.id).catch(
|
||||
() => ({}) as Partial<Record<IntegrationKey, MetricFreshnessSample[]>>,
|
||||
);
|
||||
//
|
||||
// The degradation is REPORTED rather than swallowed. Falling back to `{}`
|
||||
// makes every entry's `metricFreshness` an empty array, which is exactly what
|
||||
// a user with nothing recorded also sees — so "no metric has gone quiet" and
|
||||
// "we could not tell whether a metric has gone quiet" were the same answer on
|
||||
// the wire, and the honesty signal quietly became the thing it was built to
|
||||
// prevent. `metricFreshnessDegraded` separates them. One flag rather than a
|
||||
// nullable array per entry, because this is ONE query for all eight providers
|
||||
// — it fails for all of them or for none.
|
||||
let metricFreshnessDegraded = false;
|
||||
const metricFreshness = await getSourceMetricFreshness(user.id).catch(() => {
|
||||
metricFreshnessDegraded = true;
|
||||
return {} as Partial<Record<IntegrationKey, MetricFreshnessSample[]>>;
|
||||
});
|
||||
|
||||
const now = Date.now();
|
||||
const whoopConnected = !!whoopConn?.whoopUserId;
|
||||
@@ -243,8 +254,13 @@ export const GET = apiHandler(async () => {
|
||||
const stravaConnected = !!dbUser?.stravaAccessTokenEncrypted;
|
||||
const nightscoutConfigured = !!dbUser?.nightscoutUrlEncrypted;
|
||||
|
||||
if (metricFreshnessDegraded) {
|
||||
annotate({ meta: { integrations_metric_freshness_degraded: true } });
|
||||
}
|
||||
|
||||
return apiSuccess({
|
||||
threshold: getPersistentFailureThreshold(),
|
||||
metricFreshnessDegraded,
|
||||
integrations: [
|
||||
{
|
||||
...publish(withingsStatus),
|
||||
|
||||
Reference in new issue
Block a user