Fix Wahoo import bugs, add Import All, update spec
Bugs fixed: - FIT parser returns timestamps as Date objects, not strings — convert to ISO 8601 before passing to generateGpx (caused str.replace crash) - FIT parser already converts semicircles to degrees — remove redundant conversion that produced near-zero coordinates - Wahoo CDN URLs are pre-signed S3 URLs — remove Bearer auth header from download requests (caused 400 "Unsupported Authorization Type") - Filter out third-party workouts (fitness_app_id >= 1000) since Wahoo does not share their data via the API UX improvements: - Individual imports use fetcher (no page refresh), button shows "Importing..." inline - "Import all" button imports all unimported workouts on the page sequentially with progress indicator - Add @vitejs/plugin-basic-ssl for local HTTPS dev (opt-in via HTTPS=1) Also adds unit test for FIT-to-GPX conversion with real fixture file, and updates wahoo-import spec to reflect all behaviors. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
01d7620617
commit
20b91ef511
10 changed files with 258 additions and 58 deletions
BIN
apps/journal/app/lib/sync/providers/__fixtures__/wahoo-ride.fit
Normal file
BIN
apps/journal/app/lib/sync/providers/__fixtures__/wahoo-ride.fit
Normal file
Binary file not shown.
63
apps/journal/app/lib/sync/providers/wahoo.test.ts
Normal file
63
apps/journal/app/lib/sync/providers/wahoo.test.ts
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
import { readFileSync } from "node:fs";
|
||||
import { resolve } from "node:path";
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { wahooProvider } from "./wahoo";
|
||||
|
||||
const fixturePath = resolve(__dirname, "__fixtures__/wahoo-ride.fit");
|
||||
const fitBuffer = readFileSync(fixturePath);
|
||||
|
||||
describe("wahooProvider.convertToGpx", () => {
|
||||
it("converts a FIT file to valid GPX with correct coordinates", async () => {
|
||||
const gpx = await wahooProvider.convertToGpx(Buffer.from(fitBuffer));
|
||||
|
||||
expect(gpx).not.toBeNull();
|
||||
expect(gpx).toContain('<?xml version="1.0"');
|
||||
expect(gpx).toContain("<gpx");
|
||||
expect(gpx).toContain("<trkpt");
|
||||
});
|
||||
|
||||
it("produces coordinates in valid lat/lon range (not semicircles)", async () => {
|
||||
const gpx = await wahooProvider.convertToGpx(Buffer.from(fitBuffer));
|
||||
expect(gpx).not.toBeNull();
|
||||
|
||||
const latMatches = gpx!.matchAll(/lat="([^"]+)"/g);
|
||||
const lonMatches = gpx!.matchAll(/lon="([^"]+)"/g);
|
||||
|
||||
const lats = [...latMatches].map((m) => parseFloat(m[1]!));
|
||||
const lons = [...lonMatches].map((m) => parseFloat(m[1]!));
|
||||
|
||||
expect(lats.length).toBeGreaterThan(10);
|
||||
expect(lons.length).toBeGreaterThan(10);
|
||||
|
||||
for (const lat of lats) {
|
||||
expect(lat).toBeGreaterThan(-90);
|
||||
expect(lat).toBeLessThan(90);
|
||||
// Should be real-world coords, not near-zero from double-conversion
|
||||
expect(Math.abs(lat)).toBeGreaterThan(1);
|
||||
}
|
||||
for (const lon of lons) {
|
||||
expect(lon).toBeGreaterThan(-180);
|
||||
expect(lon).toBeLessThan(180);
|
||||
}
|
||||
});
|
||||
|
||||
it("includes ISO 8601 timestamps (not Date objects)", async () => {
|
||||
const gpx = await wahooProvider.convertToGpx(Buffer.from(fitBuffer));
|
||||
expect(gpx).not.toBeNull();
|
||||
|
||||
const timeMatches = gpx!.matchAll(/<time>([^<]+)<\/time>/g);
|
||||
const times = [...timeMatches].map((m) => m[1]!);
|
||||
|
||||
expect(times.length).toBeGreaterThan(10);
|
||||
for (const t of times) {
|
||||
expect(t).toMatch(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/);
|
||||
expect(new Date(t).toString()).not.toBe("Invalid Date");
|
||||
}
|
||||
});
|
||||
|
||||
it("includes elevation data", async () => {
|
||||
const gpx = await wahooProvider.convertToGpx(Buffer.from(fitBuffer));
|
||||
expect(gpx).not.toBeNull();
|
||||
expect(gpx).toContain("<ele>");
|
||||
});
|
||||
});
|
||||
|
|
@ -85,6 +85,7 @@ export const wahooProvider: SyncProvider = {
|
|||
name: string;
|
||||
workout_type: string;
|
||||
starts: string;
|
||||
fitness_app_id?: number;
|
||||
workout_summary?: {
|
||||
duration_active_accum?: number;
|
||||
distance_accum?: number;
|
||||
|
|
@ -96,8 +97,11 @@ export const wahooProvider: SyncProvider = {
|
|||
per_page: number;
|
||||
};
|
||||
|
||||
// Wahoo does not share workout data from third-party apps (fitness_app_id >= 1000)
|
||||
const wahooWorkouts = data.workouts.filter((w) => !w.fitness_app_id || w.fitness_app_id < 1000);
|
||||
|
||||
return {
|
||||
workouts: data.workouts.map((w) => ({
|
||||
workouts: wahooWorkouts.map((w) => ({
|
||||
id: String(w.id),
|
||||
name: w.name || `Workout ${w.id}`,
|
||||
type: w.workout_type ?? "unknown",
|
||||
|
|
@ -118,9 +122,7 @@ export const wahooProvider: SyncProvider = {
|
|||
|
||||
async downloadFile(tokens: TokenSet, workout: Workout): Promise<Buffer> {
|
||||
if (!workout.fileUrl) throw new Error("No file URL for workout");
|
||||
const resp = await fetch(workout.fileUrl, {
|
||||
headers: { Authorization: `Bearer ${tokens.accessToken}` },
|
||||
});
|
||||
const resp = await fetch(workout.fileUrl);
|
||||
if (!resp.ok) throw new Error(`Wahoo file download failed: ${resp.status}`);
|
||||
return Buffer.from(await resp.arrayBuffer());
|
||||
},
|
||||
|
|
@ -139,18 +141,17 @@ export const wahooProvider: SyncProvider = {
|
|||
position_lat?: number;
|
||||
position_long?: number;
|
||||
altitude?: number;
|
||||
timestamp?: string;
|
||||
timestamp?: string | Date;
|
||||
}>;
|
||||
|
||||
// FIT uses semicircles — convert to degrees
|
||||
const SEMICIRCLE_TO_DEG = 180 / Math.pow(2, 31);
|
||||
// fit-file-parser already converts semicircles to degrees
|
||||
const trackPoints = records
|
||||
.filter((r) => r.position_lat != null && r.position_long != null)
|
||||
.map((r) => ({
|
||||
lat: r.position_lat! * SEMICIRCLE_TO_DEG,
|
||||
lon: r.position_long! * SEMICIRCLE_TO_DEG,
|
||||
lat: r.position_lat!,
|
||||
lon: r.position_long!,
|
||||
ele: r.altitude,
|
||||
time: r.timestamp,
|
||||
time: r.timestamp instanceof Date ? r.timestamp.toISOString() : r.timestamp,
|
||||
}));
|
||||
|
||||
if (trackPoints.length < 2) return null;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue