Files
HealthLog/src/lib/oura/__tests__/scope.test.ts
T
Marc-André Bombeck 7585994697 feat(oura): import workouts and intraday heart rate
Workouts from /v2/usercollection/workout land as Workout rows keyed on
the Oura id, with the activity mapped onto the canonical sport set and
the raw label, intensity and recording source kept in metadata. Heart
rate from /v2/usercollection/heartrate lands as PULSE, streamed page by
page and written in bounded batches, first write wins per instant. The
hourly window for heart rate is hours, not days: six by default,
reaching back to the newest reading after a gap, at most 72.

Both collections sit behind their own scopes (workout, heartrate). A
grant from before they were requested answers 401 naming the scope; that
is recorded as a scope note on the card, never as a failure or a
reconnect. Collection refusals carry Retry-After on a 429.
2026-10-21 19:35:47 +02:00

113 lines
3.3 KiB
TypeScript

import { describe, expect, it } from "vitest";
import {
OURA_COLLECTIONS,
OURA_OAUTH_SCOPE,
OURA_SCOPE_GATED_COLLECTIONS,
ouraCollectionsMissingFromGrant,
ouraScopeReconnectList,
parseOuraGrantedScopes,
parseOuraMissingScope,
} from "../scope";
describe("Oura scope map", () => {
it("asks for the scope of every collection the sync reads", () => {
const requested = new Set(OURA_OAUTH_SCOPE.split(" "));
for (const c of OURA_COLLECTIONS) expect(requested).toContain(c.scope);
expect(requested).toContain("personal");
});
it("puts resilience behind stress, not heart_health", () => {
const scopeOf = Object.fromEntries(
OURA_COLLECTIONS.map((c) => [c.name, c.scope]),
);
expect(scopeOf.resilience).toBe("stress");
expect(scopeOf.spo2).toBe("spo2");
expect(scopeOf.vo2max).toBe("heart_health");
expect(scopeOf.cardio_age).toBe("heart_health");
expect(OURA_OAUTH_SCOPE.split(" ")).toEqual([
"daily",
"spo2",
"heart_health",
"stress",
"workout",
"heartrate",
"personal",
]);
});
it("derives the gated collections from the map, in display order", () => {
expect([...OURA_SCOPE_GATED_COLLECTIONS]).toEqual([
"spo2",
"vo2max",
"cardio_age",
"resilience",
"workout",
"heartrate",
]);
expect(ouraScopeReconnectList(["resilience", "readiness", "spo2"])).toEqual(
["spo2", "resilience"],
);
});
});
describe("granted scopes", () => {
it("reads Oura's extapi-prefixed grant", () => {
expect(
parseOuraGrantedScopes("extapi:daily extapi:personal extapi:spo2"),
).toEqual(new Set(["daily", "personal", "spo2"]));
});
it("knows nothing when the response names no scopes", () => {
expect(parseOuraGrantedScopes(undefined)).toBeNull();
expect(parseOuraGrantedScopes(" ")).toBeNull();
expect(ouraCollectionsMissingFromGrant(undefined)).toBeNull();
});
it("lists the gated collections a grant leaves out", () => {
expect(
ouraCollectionsMissingFromGrant(
"extapi:daily extapi:personal extapi:spo2 extapi:heart_health",
),
).toEqual(["resilience", "workout", "heartrate"]);
// A grant from before workouts and heart rate were read: those two are
// the scope note, nothing else.
expect(
ouraCollectionsMissingFromGrant(
"daily personal spo2 heart_health stress",
),
).toEqual(["workout", "heartrate"]);
expect(
ouraCollectionsMissingFromGrant(
"daily personal spo2 heart_health stress workout heartrate",
),
).toEqual([]);
});
});
describe("parseOuraMissingScope", () => {
it("reads the scope a refusal names", () => {
expect(
parseOuraMissingScope({
detail: "Token is not authorized access stress scope.",
}),
).toBe("stress");
expect(
parseOuraMissingScope({
detail: "Token is not authorized access extapi:heart_health scope.",
}),
).toBe("heart_health");
});
it("returns null for a dead token or any other body", () => {
expect(
parseOuraMissingScope({
detail:
"The access token provided is expired, revoked, malformed, or invalid for other reasons.",
}),
).toBeNull();
expect(parseOuraMissingScope(null)).toBeNull();
expect(parseOuraMissingScope({ detail: 42 })).toBeNull();
});
});