@clipin/convex-wearables 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/client/index.d.ts +9 -4
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js.map +1 -1
- package/dist/component/_generated/component.d.ts +50 -0
- package/dist/component/_generated/component.d.ts.map +1 -0
- package/dist/component/_generated/component.js +11 -0
- package/dist/component/_generated/component.js.map +1 -0
- package/dist/component/backfillJobs.d.ts +11 -11
- package/dist/component/connections.d.ts +9 -9
- package/dist/component/connections.d.ts.map +1 -1
- package/dist/component/connections.js +2 -0
- package/dist/component/connections.js.map +1 -1
- package/dist/component/dataPoints.d.ts +5 -5
- package/dist/component/events.d.ts +13 -13
- package/dist/component/garminBackfill.d.ts +2 -2
- package/dist/component/garminWebhooks.d.ts +2 -2
- package/dist/component/garminWebhooks.d.ts.map +1 -1
- package/dist/component/garminWebhooks.js +2 -0
- package/dist/component/garminWebhooks.js.map +1 -1
- package/dist/component/lifecycle.d.ts +1 -1
- package/dist/component/lifecycle.d.ts.map +1 -1
- package/dist/component/lifecycle.js +2 -0
- package/dist/component/lifecycle.js.map +1 -1
- package/dist/component/oauthStates.d.ts +3 -3
- package/dist/component/schema.d.ts +26 -26
- package/dist/component/sdkPush.d.ts +11 -11
- package/dist/component/summaries.d.ts +4 -4
- package/dist/component/syncJobs.d.ts +23 -23
- package/dist/component/syncWorkflow.d.ts +2 -2
- package/dist/test.d.ts +421 -0
- package/dist/test.d.ts.map +1 -0
- package/dist/test.js +17 -0
- package/dist/test.js.map +1 -0
- package/package.json +12 -2
- package/src/client/_generated/_ignore.ts +2 -0
- package/src/client/index.test.ts +52 -0
- package/src/client/index.ts +784 -0
- package/src/client/types.ts +533 -0
- package/src/component/_generated/_ignore.ts +2 -0
- package/src/component/_generated/api.ts +16 -0
- package/src/component/_generated/component.ts +74 -0
- package/src/component/_generated/dataModel.ts +40 -0
- package/src/component/_generated/server.ts +48 -0
- package/src/component/backfillJobs.test.ts +47 -0
- package/src/component/backfillJobs.ts +245 -0
- package/src/component/connections.test.ts +297 -0
- package/src/component/connections.ts +329 -0
- package/src/component/convex.config.ts +7 -0
- package/src/component/dataPoints.test.ts +282 -0
- package/src/component/dataPoints.ts +305 -0
- package/src/component/dataSources.test.ts +247 -0
- package/src/component/dataSources.ts +109 -0
- package/src/component/events.test.ts +380 -0
- package/src/component/events.ts +288 -0
- package/src/component/garminBackfill.ts +343 -0
- package/src/component/garminWebhooks.test.ts +609 -0
- package/src/component/garminWebhooks.ts +656 -0
- package/src/component/httpHandlers.ts +153 -0
- package/src/component/lifecycle.test.ts +179 -0
- package/src/component/lifecycle.ts +87 -0
- package/src/component/menstrualCycles.ts +124 -0
- package/src/component/oauthActions.ts +261 -0
- package/src/component/oauthStates.test.ts +170 -0
- package/src/component/oauthStates.ts +85 -0
- package/src/component/providerSettings.ts +66 -0
- package/src/component/providers/additionalProviders.test.ts +401 -0
- package/src/component/providers/garmin.ts +1169 -0
- package/src/component/providers/oauth.test.ts +174 -0
- package/src/component/providers/oauth.ts +246 -0
- package/src/component/providers/polar.ts +220 -0
- package/src/component/providers/registry.ts +37 -0
- package/src/component/providers/strava.test.ts +195 -0
- package/src/component/providers/strava.ts +253 -0
- package/src/component/providers/suunto.ts +592 -0
- package/src/component/providers/types.ts +189 -0
- package/src/component/providers/whoop.ts +600 -0
- package/src/component/schema.ts +339 -0
- package/src/component/sdkPush.test.ts +367 -0
- package/src/component/sdkPush.ts +440 -0
- package/src/component/summaries.test.ts +201 -0
- package/src/component/summaries.ts +143 -0
- package/src/component/syncJobs.test.ts +254 -0
- package/src/component/syncJobs.ts +140 -0
- package/src/component/syncWorkflow.test.ts +87 -0
- package/src/component/syncWorkflow.ts +739 -0
- package/src/component/test.setup.ts +6 -0
- package/src/component/workflowManager.ts +19 -0
- package/src/test.ts +25 -0
|
@@ -0,0 +1,609 @@
|
|
|
1
|
+
import { convexTest } from "convex-test";
|
|
2
|
+
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
3
|
+
import { api } from "./_generated/api";
|
|
4
|
+
import { GARMIN_BACKFILL_TYPES } from "./garminBackfill";
|
|
5
|
+
import { triggerBackfill } from "./providers/garmin";
|
|
6
|
+
import schema from "./schema";
|
|
7
|
+
import { modules } from "./test.setup";
|
|
8
|
+
|
|
9
|
+
function createTest() {
|
|
10
|
+
return convexTest(schema, modules);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
afterEach(() => {
|
|
14
|
+
vi.restoreAllMocks();
|
|
15
|
+
vi.unstubAllGlobals();
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
describe("garminWebhooks", () => {
|
|
19
|
+
it("ingests Garmin wellness feeds into events, data points, and summaries", async () => {
|
|
20
|
+
const t = createTest();
|
|
21
|
+
|
|
22
|
+
await t.run(async (ctx) => {
|
|
23
|
+
await ctx.db.insert("connections", {
|
|
24
|
+
userId: "user-1",
|
|
25
|
+
provider: "garmin",
|
|
26
|
+
providerUserId: "garmin-user-1",
|
|
27
|
+
accessToken: "garmin-token",
|
|
28
|
+
scope: "OLD_SCOPE",
|
|
29
|
+
status: "active",
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
const dayStart = Math.floor(Date.parse("2026-03-16T00:00:00Z") / 1000);
|
|
34
|
+
const midday = Math.floor(Date.parse("2026-03-16T12:00:00Z") / 1000);
|
|
35
|
+
|
|
36
|
+
await t.action(api.garminWebhooks.processPushPayload, {
|
|
37
|
+
garminClientId: "garmin-client",
|
|
38
|
+
payload: {
|
|
39
|
+
activities: [
|
|
40
|
+
{
|
|
41
|
+
userId: "garmin-user-1",
|
|
42
|
+
activityId: 101,
|
|
43
|
+
activityType: "RUNNING",
|
|
44
|
+
startTimeInSeconds: dayStart + 3600,
|
|
45
|
+
durationInSeconds: 1800,
|
|
46
|
+
deviceName: "Forerunner 965",
|
|
47
|
+
averageHeartRateInBeatsPerMinute: 145,
|
|
48
|
+
maxHeartRateInBeatsPerMinute: 176,
|
|
49
|
+
distanceInMeters: 5000,
|
|
50
|
+
},
|
|
51
|
+
],
|
|
52
|
+
activityDetails: [
|
|
53
|
+
{
|
|
54
|
+
userId: "garmin-user-1",
|
|
55
|
+
activityId: 202,
|
|
56
|
+
summaryId: "202-detail",
|
|
57
|
+
summary: {
|
|
58
|
+
activityType: "CYCLING",
|
|
59
|
+
startTimeInSeconds: dayStart + 7200,
|
|
60
|
+
durationInSeconds: 2700,
|
|
61
|
+
deviceName: "Edge 1040",
|
|
62
|
+
averageHeartRateInBeatsPerMinute: 138,
|
|
63
|
+
maxHeartRateInBeatsPerMinute: 170,
|
|
64
|
+
distanceInMeters: 18000,
|
|
65
|
+
},
|
|
66
|
+
},
|
|
67
|
+
],
|
|
68
|
+
sleeps: [
|
|
69
|
+
{
|
|
70
|
+
userId: "garmin-user-1",
|
|
71
|
+
summaryId: "sleep-1",
|
|
72
|
+
startTimeInSeconds: dayStart,
|
|
73
|
+
durationInSeconds: 8 * 60 * 60,
|
|
74
|
+
deepSleepDurationInSeconds: 90 * 60,
|
|
75
|
+
lightSleepDurationInSeconds: 240 * 60,
|
|
76
|
+
remSleepInSeconds: 90 * 60,
|
|
77
|
+
awakeDurationInSeconds: 60 * 60,
|
|
78
|
+
averageHeartRate: 52,
|
|
79
|
+
lowestHeartRate: 45,
|
|
80
|
+
avgOxygenSaturation: 95,
|
|
81
|
+
respirationAvg: 14,
|
|
82
|
+
overallSleepScore: { value: 88 },
|
|
83
|
+
},
|
|
84
|
+
],
|
|
85
|
+
dailies: [
|
|
86
|
+
{
|
|
87
|
+
userId: "garmin-user-1",
|
|
88
|
+
summaryId: "daily-1",
|
|
89
|
+
startTimeInSeconds: dayStart,
|
|
90
|
+
durationInSeconds: 24 * 60 * 60,
|
|
91
|
+
calendarDate: "2026-03-16",
|
|
92
|
+
steps: 12345,
|
|
93
|
+
distanceInMeters: 9600,
|
|
94
|
+
activeKilocalories: 640,
|
|
95
|
+
bmrKilocalories: 1550,
|
|
96
|
+
floorsClimbed: 12,
|
|
97
|
+
minHeartRateInBeatsPerMinute: 46,
|
|
98
|
+
maxHeartRateInBeatsPerMinute: 176,
|
|
99
|
+
averageHeartRateInBeatsPerMinute: 72,
|
|
100
|
+
restingHeartRateInBeatsPerMinute: 48,
|
|
101
|
+
averageStressLevel: 30,
|
|
102
|
+
bodyBatteryChargedValue: 80,
|
|
103
|
+
bodyBatteryDrainedValue: 35,
|
|
104
|
+
moderateIntensityDurationInSeconds: 1800,
|
|
105
|
+
vigorousIntensityDurationInSeconds: 900,
|
|
106
|
+
timeOffsetHeartRateSamples: {
|
|
107
|
+
"0": 50,
|
|
108
|
+
"300": 58,
|
|
109
|
+
},
|
|
110
|
+
},
|
|
111
|
+
],
|
|
112
|
+
epochs: [
|
|
113
|
+
{
|
|
114
|
+
userId: "garmin-user-1",
|
|
115
|
+
summaryId: "epoch-1",
|
|
116
|
+
startTimeInSeconds: dayStart + 900,
|
|
117
|
+
durationInSeconds: 900,
|
|
118
|
+
steps: 120,
|
|
119
|
+
activeKilocalories: 10,
|
|
120
|
+
meanHeartRateInBeatsPerMinute: 92,
|
|
121
|
+
},
|
|
122
|
+
],
|
|
123
|
+
bodyComps: [
|
|
124
|
+
{
|
|
125
|
+
userId: "garmin-user-1",
|
|
126
|
+
summaryId: "body-1",
|
|
127
|
+
measurementTimeInSeconds: midday,
|
|
128
|
+
weightInGrams: 70000,
|
|
129
|
+
bodyFatInPercent: 18.5,
|
|
130
|
+
bodyMassIndex: 22.5,
|
|
131
|
+
muscleMassInGrams: 32000,
|
|
132
|
+
},
|
|
133
|
+
],
|
|
134
|
+
hrv: [
|
|
135
|
+
{
|
|
136
|
+
userId: "garmin-user-1",
|
|
137
|
+
summaryId: "hrv-1",
|
|
138
|
+
startTimeInSeconds: dayStart,
|
|
139
|
+
calendarDate: "2026-03-16",
|
|
140
|
+
lastNightAvg: 55,
|
|
141
|
+
hrvValues: {
|
|
142
|
+
"0": 50,
|
|
143
|
+
"300": 60,
|
|
144
|
+
},
|
|
145
|
+
},
|
|
146
|
+
],
|
|
147
|
+
stressDetails: [
|
|
148
|
+
{
|
|
149
|
+
userId: "garmin-user-1",
|
|
150
|
+
summaryId: "stress-1",
|
|
151
|
+
startTimeInSeconds: midday,
|
|
152
|
+
stressLevelValues: {
|
|
153
|
+
"0": 20,
|
|
154
|
+
"180": 25,
|
|
155
|
+
},
|
|
156
|
+
bodyBatteryValues: {
|
|
157
|
+
"0": 75,
|
|
158
|
+
},
|
|
159
|
+
},
|
|
160
|
+
],
|
|
161
|
+
respiration: [
|
|
162
|
+
{
|
|
163
|
+
userId: "garmin-user-1",
|
|
164
|
+
summaryId: "resp-1",
|
|
165
|
+
startTimeInSeconds: midday,
|
|
166
|
+
avgWakingRespirationValue: 14.5,
|
|
167
|
+
timeOffsetRespirationRateValues: {
|
|
168
|
+
"0": 14.5,
|
|
169
|
+
"300": 15.1,
|
|
170
|
+
},
|
|
171
|
+
},
|
|
172
|
+
],
|
|
173
|
+
pulseOx: [
|
|
174
|
+
{
|
|
175
|
+
userId: "garmin-user-1",
|
|
176
|
+
summaryId: "spo2-1",
|
|
177
|
+
startTimeInSeconds: midday,
|
|
178
|
+
calendarDate: "2026-03-16",
|
|
179
|
+
avgSpo2: 97,
|
|
180
|
+
timeOffsetSpo2Values: {
|
|
181
|
+
"0": 97,
|
|
182
|
+
"300": 96,
|
|
183
|
+
},
|
|
184
|
+
},
|
|
185
|
+
],
|
|
186
|
+
bloodPressures: [
|
|
187
|
+
{
|
|
188
|
+
userId: "garmin-user-1",
|
|
189
|
+
summaryId: "bp-1",
|
|
190
|
+
measurementTimestampGMT: midday,
|
|
191
|
+
systolic: 120,
|
|
192
|
+
diastolic: 80,
|
|
193
|
+
},
|
|
194
|
+
],
|
|
195
|
+
userMetrics: [
|
|
196
|
+
{
|
|
197
|
+
userId: "garmin-user-1",
|
|
198
|
+
summaryId: "metrics-1",
|
|
199
|
+
calendarDate: "2026-03-16",
|
|
200
|
+
vo2Max: 52,
|
|
201
|
+
fitnessAge: 30,
|
|
202
|
+
},
|
|
203
|
+
],
|
|
204
|
+
skinTemp: [
|
|
205
|
+
{
|
|
206
|
+
userId: "garmin-user-1",
|
|
207
|
+
summaryId: "skin-1",
|
|
208
|
+
startTimeInSeconds: midday,
|
|
209
|
+
skinTemperature: 33.2,
|
|
210
|
+
},
|
|
211
|
+
],
|
|
212
|
+
healthSnapshot: [
|
|
213
|
+
{
|
|
214
|
+
userId: "garmin-user-1",
|
|
215
|
+
summaryId: "snapshot-1",
|
|
216
|
+
startTimeInSeconds: midday + 60,
|
|
217
|
+
heartRate: 54,
|
|
218
|
+
hrv: 58,
|
|
219
|
+
stress: 12,
|
|
220
|
+
spo2: 98,
|
|
221
|
+
respiration: 13.8,
|
|
222
|
+
},
|
|
223
|
+
],
|
|
224
|
+
moveiq: [
|
|
225
|
+
{
|
|
226
|
+
userId: "garmin-user-1",
|
|
227
|
+
summaryId: "moveiq-1",
|
|
228
|
+
startTimeInSeconds: dayStart + 10_800,
|
|
229
|
+
durationInSeconds: 600,
|
|
230
|
+
activityType: "WALKING",
|
|
231
|
+
},
|
|
232
|
+
],
|
|
233
|
+
mct: [
|
|
234
|
+
{
|
|
235
|
+
userId: "garmin-user-1",
|
|
236
|
+
summaryId: "mct-1",
|
|
237
|
+
periodStartDateStr: "2026-03-14",
|
|
238
|
+
dayInCycle: 3,
|
|
239
|
+
cycleLength: 28,
|
|
240
|
+
},
|
|
241
|
+
],
|
|
242
|
+
userPermissionsChange: [
|
|
243
|
+
{
|
|
244
|
+
userId: "garmin-user-1",
|
|
245
|
+
permissions: ["HEALTH_EXPORT", "ACTIVITY_EXPORT"],
|
|
246
|
+
},
|
|
247
|
+
],
|
|
248
|
+
},
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
const result = await t.run(async (ctx) => {
|
|
252
|
+
const events = await ctx.db.query("events").collect();
|
|
253
|
+
const summaries = await ctx.db.query("dailySummaries").collect();
|
|
254
|
+
const dataPoints = await ctx.db.query("dataPoints").collect();
|
|
255
|
+
const menstrualCycles = await ctx.db.query("menstrualCycles").collect();
|
|
256
|
+
const connection = await ctx.db
|
|
257
|
+
.query("connections")
|
|
258
|
+
.withIndex("by_provider_user", (idx) =>
|
|
259
|
+
idx.eq("provider", "garmin").eq("providerUserId", "garmin-user-1"),
|
|
260
|
+
)
|
|
261
|
+
.first();
|
|
262
|
+
|
|
263
|
+
return {
|
|
264
|
+
connection,
|
|
265
|
+
events,
|
|
266
|
+
summaries,
|
|
267
|
+
dataPointTypes: Array.from(new Set(dataPoints.map((point) => point.seriesType))).sort(),
|
|
268
|
+
menstrualCycles,
|
|
269
|
+
};
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
expect(result.connection?.scope).toBe("ACTIVITY_EXPORT HEALTH_EXPORT");
|
|
273
|
+
expect(result.events).toHaveLength(4);
|
|
274
|
+
expect(result.events.map((event) => event.type)).toEqual(
|
|
275
|
+
expect.arrayContaining(["running", "cycling", "sleep_session", "moveiq_walking"]),
|
|
276
|
+
);
|
|
277
|
+
|
|
278
|
+
const activitySummary = result.summaries.find(
|
|
279
|
+
(summary) => summary.category === "activity" && summary.date === "2026-03-16",
|
|
280
|
+
);
|
|
281
|
+
const recoverySummary = result.summaries.find(
|
|
282
|
+
(summary) => summary.category === "recovery" && summary.date === "2026-03-16",
|
|
283
|
+
);
|
|
284
|
+
const bodySummary = result.summaries.find(
|
|
285
|
+
(summary) => summary.category === "body" && summary.date === "2026-03-16",
|
|
286
|
+
);
|
|
287
|
+
const sleepSummary = result.summaries.find(
|
|
288
|
+
(summary) => summary.category === "sleep" && summary.date === "2026-03-16",
|
|
289
|
+
);
|
|
290
|
+
|
|
291
|
+
expect(activitySummary).toMatchObject({
|
|
292
|
+
totalSteps: 12345,
|
|
293
|
+
totalCalories: 2190,
|
|
294
|
+
activeCalories: 640,
|
|
295
|
+
totalDistance: 9600,
|
|
296
|
+
floorsClimbed: 12,
|
|
297
|
+
avgHeartRate: 72,
|
|
298
|
+
maxHeartRate: 176,
|
|
299
|
+
minHeartRate: 46,
|
|
300
|
+
activeMinutes: 45,
|
|
301
|
+
});
|
|
302
|
+
expect(recoverySummary).toMatchObject({
|
|
303
|
+
restingHeartRate: 48,
|
|
304
|
+
avgStressLevel: 30,
|
|
305
|
+
bodyBattery: 45,
|
|
306
|
+
hrvAvg: 55,
|
|
307
|
+
spo2Avg: 97,
|
|
308
|
+
});
|
|
309
|
+
expect(bodySummary).toMatchObject({
|
|
310
|
+
weight: 70,
|
|
311
|
+
bodyFatPercentage: 18.5,
|
|
312
|
+
bodyMassIndex: 22.5,
|
|
313
|
+
});
|
|
314
|
+
expect(sleepSummary).toMatchObject({
|
|
315
|
+
sleepDurationMinutes: 480,
|
|
316
|
+
sleepEfficiency: 88,
|
|
317
|
+
deepSleepMinutes: 90,
|
|
318
|
+
lightSleepMinutes: 240,
|
|
319
|
+
remSleepMinutes: 90,
|
|
320
|
+
awakeDuringMinutes: 60,
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
expect(result.dataPointTypes).toEqual(
|
|
324
|
+
expect.arrayContaining([
|
|
325
|
+
"blood_pressure_diastolic",
|
|
326
|
+
"blood_pressure_systolic",
|
|
327
|
+
"body_fat_percentage",
|
|
328
|
+
"body_mass_index",
|
|
329
|
+
"energy",
|
|
330
|
+
"garmin_body_battery",
|
|
331
|
+
"garmin_fitness_age",
|
|
332
|
+
"garmin_stress_level",
|
|
333
|
+
"heart_rate",
|
|
334
|
+
"heart_rate_variability_sdnn",
|
|
335
|
+
"oxygen_saturation",
|
|
336
|
+
"respiratory_rate",
|
|
337
|
+
"resting_heart_rate",
|
|
338
|
+
"skeletal_muscle_mass",
|
|
339
|
+
"skin_temperature",
|
|
340
|
+
"steps",
|
|
341
|
+
"vo2_max",
|
|
342
|
+
"weight",
|
|
343
|
+
]),
|
|
344
|
+
);
|
|
345
|
+
expect(result.menstrualCycles).toHaveLength(1);
|
|
346
|
+
});
|
|
347
|
+
|
|
348
|
+
it("keeps existing activity timestamps intact when Garmin sends nested activityDetails", async () => {
|
|
349
|
+
const t = createTest();
|
|
350
|
+
|
|
351
|
+
await t.run(async (ctx) => {
|
|
352
|
+
await ctx.db.insert("connections", {
|
|
353
|
+
userId: "user-activity-details",
|
|
354
|
+
provider: "garmin",
|
|
355
|
+
providerUserId: "garmin-user-activity-details",
|
|
356
|
+
accessToken: "garmin-token",
|
|
357
|
+
status: "active",
|
|
358
|
+
});
|
|
359
|
+
});
|
|
360
|
+
|
|
361
|
+
const startTimeInSeconds = Math.floor(Date.parse("2026-03-20T09:00:00Z") / 1000);
|
|
362
|
+
const durationInSeconds = 3421;
|
|
363
|
+
|
|
364
|
+
await t.action(api.garminWebhooks.processPushPayload, {
|
|
365
|
+
garminClientId: "garmin-client",
|
|
366
|
+
payload: {
|
|
367
|
+
activities: [
|
|
368
|
+
{
|
|
369
|
+
userId: "garmin-user-activity-details",
|
|
370
|
+
activityId: 22258974253,
|
|
371
|
+
activityType: "RUNNING",
|
|
372
|
+
startTimeInSeconds,
|
|
373
|
+
durationInSeconds,
|
|
374
|
+
averageHeartRateInBeatsPerMinute: 156,
|
|
375
|
+
maxHeartRateInBeatsPerMinute: 173,
|
|
376
|
+
activeKilocalories: 753,
|
|
377
|
+
distanceInMeters: 10016.89,
|
|
378
|
+
steps: 9290,
|
|
379
|
+
averageSpeedInMetersPerSecond: 2.928,
|
|
380
|
+
maxSpeedInMetersPerSecond: 3.835,
|
|
381
|
+
totalElevationGainInMeters: 52.2,
|
|
382
|
+
},
|
|
383
|
+
],
|
|
384
|
+
},
|
|
385
|
+
});
|
|
386
|
+
|
|
387
|
+
await t.action(api.garminWebhooks.processPushPayload, {
|
|
388
|
+
garminClientId: "garmin-client",
|
|
389
|
+
payload: {
|
|
390
|
+
activityDetails: [
|
|
391
|
+
{
|
|
392
|
+
userId: "garmin-user-activity-details",
|
|
393
|
+
activityId: 22258974253,
|
|
394
|
+
summaryId: "22258974253-detail",
|
|
395
|
+
summary: {
|
|
396
|
+
activityType: "RUNNING",
|
|
397
|
+
startTimeInSeconds,
|
|
398
|
+
durationInSeconds,
|
|
399
|
+
averageHeartRateInBeatsPerMinute: 156,
|
|
400
|
+
maxHeartRateInBeatsPerMinute: 173,
|
|
401
|
+
activeKilocalories: 753,
|
|
402
|
+
distanceInMeters: 10016.89,
|
|
403
|
+
steps: 9290,
|
|
404
|
+
averageSpeedInMetersPerSecond: 2.928,
|
|
405
|
+
maxSpeedInMetersPerSecond: 3.835,
|
|
406
|
+
totalElevationGainInMeters: 52.2,
|
|
407
|
+
},
|
|
408
|
+
},
|
|
409
|
+
],
|
|
410
|
+
},
|
|
411
|
+
});
|
|
412
|
+
|
|
413
|
+
const events = await t.run(async (ctx) => {
|
|
414
|
+
return await ctx.db.query("events").collect();
|
|
415
|
+
});
|
|
416
|
+
|
|
417
|
+
expect(events).toHaveLength(1);
|
|
418
|
+
expect(events[0]).toMatchObject({
|
|
419
|
+
externalId: "garmin-22258974253",
|
|
420
|
+
type: "running",
|
|
421
|
+
sourceName: "Garmin",
|
|
422
|
+
durationSeconds: durationInSeconds,
|
|
423
|
+
startDatetime: startTimeInSeconds * 1000,
|
|
424
|
+
endDatetime: (startTimeInSeconds + durationInSeconds) * 1000,
|
|
425
|
+
energyBurned: 753,
|
|
426
|
+
distance: 10016.89,
|
|
427
|
+
averageSpeed: 2.928,
|
|
428
|
+
maxSpeed: 3.835,
|
|
429
|
+
totalElevationGain: 52.2,
|
|
430
|
+
});
|
|
431
|
+
expect(Number.isNaN(events[0]?.startDatetime)).toBe(false);
|
|
432
|
+
expect(Number.isNaN(events[0]?.endDatetime)).toBe(false);
|
|
433
|
+
});
|
|
434
|
+
|
|
435
|
+
it("skips Garmin activities whose timing cannot be parsed", async () => {
|
|
436
|
+
const t = createTest();
|
|
437
|
+
|
|
438
|
+
await t.run(async (ctx) => {
|
|
439
|
+
await ctx.db.insert("connections", {
|
|
440
|
+
userId: "user-invalid-activity",
|
|
441
|
+
provider: "garmin",
|
|
442
|
+
providerUserId: "garmin-user-invalid-activity",
|
|
443
|
+
accessToken: "garmin-token",
|
|
444
|
+
status: "active",
|
|
445
|
+
});
|
|
446
|
+
});
|
|
447
|
+
|
|
448
|
+
await t.action(api.garminWebhooks.processPushPayload, {
|
|
449
|
+
garminClientId: "garmin-client",
|
|
450
|
+
payload: {
|
|
451
|
+
activities: [
|
|
452
|
+
{
|
|
453
|
+
userId: "garmin-user-invalid-activity",
|
|
454
|
+
activityId: 303,
|
|
455
|
+
activityType: "RUNNING",
|
|
456
|
+
startTimeInSeconds: "not-a-timestamp",
|
|
457
|
+
durationInSeconds: 1800,
|
|
458
|
+
},
|
|
459
|
+
],
|
|
460
|
+
},
|
|
461
|
+
});
|
|
462
|
+
|
|
463
|
+
const events = await t.run(async (ctx) => {
|
|
464
|
+
return await ctx.db.query("events").collect();
|
|
465
|
+
});
|
|
466
|
+
|
|
467
|
+
expect(events).toHaveLength(0);
|
|
468
|
+
});
|
|
469
|
+
|
|
470
|
+
it("updates Garmin scopes and revokes connections on deregistration", async () => {
|
|
471
|
+
const t = createTest();
|
|
472
|
+
|
|
473
|
+
await t.run(async (ctx) => {
|
|
474
|
+
await ctx.db.insert("connections", {
|
|
475
|
+
userId: "user-2",
|
|
476
|
+
provider: "garmin",
|
|
477
|
+
providerUserId: "garmin-user-2",
|
|
478
|
+
accessToken: "garmin-token",
|
|
479
|
+
scope: "OLD_SCOPE",
|
|
480
|
+
status: "active",
|
|
481
|
+
});
|
|
482
|
+
});
|
|
483
|
+
|
|
484
|
+
await t.action(api.garminWebhooks.processPushPayload, {
|
|
485
|
+
garminClientId: "garmin-client",
|
|
486
|
+
payload: {
|
|
487
|
+
userPermissionsChange: [
|
|
488
|
+
{
|
|
489
|
+
userId: "garmin-user-2",
|
|
490
|
+
permissions: ["HEALTH_EXPORT"],
|
|
491
|
+
},
|
|
492
|
+
],
|
|
493
|
+
deregistrations: [
|
|
494
|
+
{
|
|
495
|
+
userId: "garmin-user-2",
|
|
496
|
+
},
|
|
497
|
+
],
|
|
498
|
+
},
|
|
499
|
+
});
|
|
500
|
+
|
|
501
|
+
const connection = await t.run(async (ctx) => {
|
|
502
|
+
return await ctx.db
|
|
503
|
+
.query("connections")
|
|
504
|
+
.withIndex("by_provider_user", (idx) =>
|
|
505
|
+
idx.eq("provider", "garmin").eq("providerUserId", "garmin-user-2"),
|
|
506
|
+
)
|
|
507
|
+
.first();
|
|
508
|
+
});
|
|
509
|
+
|
|
510
|
+
expect(connection?.scope).toBe("HEALTH_EXPORT");
|
|
511
|
+
expect(connection?.status).toBe("revoked");
|
|
512
|
+
});
|
|
513
|
+
|
|
514
|
+
it("accepts stringified daily payloads with oversized heart-rate sample maps", async () => {
|
|
515
|
+
const t = createTest();
|
|
516
|
+
|
|
517
|
+
await t.run(async (ctx) => {
|
|
518
|
+
await ctx.db.insert("connections", {
|
|
519
|
+
userId: "user-3",
|
|
520
|
+
provider: "garmin",
|
|
521
|
+
providerUserId: "garmin-user-3",
|
|
522
|
+
accessToken: "garmin-token",
|
|
523
|
+
status: "active",
|
|
524
|
+
});
|
|
525
|
+
});
|
|
526
|
+
|
|
527
|
+
const dayStart = Math.floor(Date.parse("2026-03-17T00:00:00Z") / 1000);
|
|
528
|
+
const timeOffsetHeartRateSamples = Object.fromEntries(
|
|
529
|
+
Array.from({ length: 1100 }, (_, index) => [String(index * 15), 60 + (index % 40)]),
|
|
530
|
+
);
|
|
531
|
+
|
|
532
|
+
await t.action(api.garminWebhooks.processPushPayload, {
|
|
533
|
+
garminClientId: "garmin-client",
|
|
534
|
+
payloadJson: JSON.stringify({
|
|
535
|
+
dailies: [
|
|
536
|
+
{
|
|
537
|
+
userId: "garmin-user-3",
|
|
538
|
+
summaryId: "daily-large-1",
|
|
539
|
+
startTimeInSeconds: dayStart,
|
|
540
|
+
durationInSeconds: 24 * 60 * 60,
|
|
541
|
+
calendarDate: "2026-03-17",
|
|
542
|
+
restingHeartRateInBeatsPerMinute: 47,
|
|
543
|
+
timeOffsetHeartRateSamples,
|
|
544
|
+
},
|
|
545
|
+
],
|
|
546
|
+
}),
|
|
547
|
+
});
|
|
548
|
+
|
|
549
|
+
const result = await t.run(async (ctx) => {
|
|
550
|
+
const dataPoints = await ctx.db.query("dataPoints").collect();
|
|
551
|
+
const summaries = await ctx.db.query("dailySummaries").collect();
|
|
552
|
+
return { dataPoints, summaries };
|
|
553
|
+
});
|
|
554
|
+
|
|
555
|
+
expect(result.dataPoints.filter((point) => point.seriesType === "heart_rate")).toHaveLength(
|
|
556
|
+
1100,
|
|
557
|
+
);
|
|
558
|
+
expect(
|
|
559
|
+
result.dataPoints.filter((point) => point.seriesType === "resting_heart_rate"),
|
|
560
|
+
).toHaveLength(1);
|
|
561
|
+
expect(
|
|
562
|
+
result.summaries.find(
|
|
563
|
+
(summary) => summary.category === "recovery" && summary.date === "2026-03-17",
|
|
564
|
+
),
|
|
565
|
+
).toMatchObject({
|
|
566
|
+
restingHeartRate: 47,
|
|
567
|
+
});
|
|
568
|
+
});
|
|
569
|
+
});
|
|
570
|
+
|
|
571
|
+
describe("garminBackfill", () => {
|
|
572
|
+
it("includes the extended Garmin data types in the backfill workflow", () => {
|
|
573
|
+
expect(GARMIN_BACKFILL_TYPES).toEqual(
|
|
574
|
+
expect.arrayContaining([
|
|
575
|
+
"activityDetails",
|
|
576
|
+
"epochs",
|
|
577
|
+
"bodyComps",
|
|
578
|
+
"hrv",
|
|
579
|
+
"stressDetails",
|
|
580
|
+
"respiration",
|
|
581
|
+
"pulseOx",
|
|
582
|
+
"bloodPressures",
|
|
583
|
+
"userMetrics",
|
|
584
|
+
"skinTemp",
|
|
585
|
+
"healthSnapshot",
|
|
586
|
+
"moveiq",
|
|
587
|
+
"mct",
|
|
588
|
+
]),
|
|
589
|
+
);
|
|
590
|
+
});
|
|
591
|
+
|
|
592
|
+
it("triggers extended Garmin backfill endpoints even when Garmin returns an empty 202 body", async () => {
|
|
593
|
+
const fetchMock = vi.fn().mockResolvedValue(new Response(null, { status: 202 }));
|
|
594
|
+
vi.stubGlobal("fetch", fetchMock as unknown as typeof fetch);
|
|
595
|
+
|
|
596
|
+
await expect(
|
|
597
|
+
triggerBackfill("garmin-token", "healthSnapshot", 100, 200),
|
|
598
|
+
).resolves.toBeUndefined();
|
|
599
|
+
|
|
600
|
+
const [url, init] = fetchMock.mock.calls[0] ?? [];
|
|
601
|
+
expect(String(url)).toBe(
|
|
602
|
+
"https://apis.garmin.com/wellness-api/rest/backfill/healthSnapshot?summaryStartTimeInSeconds=100&summaryEndTimeInSeconds=200",
|
|
603
|
+
);
|
|
604
|
+
expect(init?.headers).toMatchObject({
|
|
605
|
+
Authorization: "Bearer garmin-token",
|
|
606
|
+
Accept: "application/json",
|
|
607
|
+
});
|
|
608
|
+
});
|
|
609
|
+
});
|