2026-07-17 18:33:06 +02:00
|
|
|
package store
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func openTestDB(t *testing.T) *DB {
|
|
|
|
|
t.Helper()
|
2026-07-24 21:08:07 +02:00
|
|
|
path := filepath.Join(t.TempDir(), "geniusrun_test.db")
|
2026-07-17 18:33:06 +02:00
|
|
|
db, err := Open(path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("Open: %v", err)
|
|
|
|
|
}
|
|
|
|
|
t.Cleanup(func() { db.Close() })
|
|
|
|
|
return db
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func f(v float64) *float64 { return &v }
|
|
|
|
|
|
|
|
|
|
func TestMigrateIsIdempotent(t *testing.T) {
|
2026-07-24 21:08:07 +02:00
|
|
|
path := filepath.Join(t.TempDir(), "geniusrun_test.db")
|
2026-07-17 18:33:06 +02:00
|
|
|
db1, err := Open(path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("first Open: %v", err)
|
|
|
|
|
}
|
|
|
|
|
db1.Close()
|
|
|
|
|
|
|
|
|
|
db2, err := Open(path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("second Open (re-applying migrations): %v", err)
|
|
|
|
|
}
|
|
|
|
|
db2.Close()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestUpsertActivity_InsertThenUpdateIsIdempotent(t *testing.T) {
|
|
|
|
|
db := openTestDB(t)
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
|
|
a := Activity{
|
|
|
|
|
GarminActivityID: 23554066504,
|
|
|
|
|
StartTimeUTC: "2026-07-11 05:02:35",
|
|
|
|
|
DurationSeconds: 1800,
|
|
|
|
|
DistanceMeters: 6858,
|
|
|
|
|
AvgHR: f(148),
|
Dedup Garmin data storage, configurable chart colors, taxonomy fixes, sync/progression fixes
- Remove duplicated Garmin fields from storage; decode display-only fields
(activity name/type, lap duration/HR, structured workout raw JSON) from
RawJSON at API-response time instead of storing redundant columns
- Add a fully configurable chart color system (pace/HR main-line colors, 4
effort-kind colors, tint/darken/brighten intensity knobs) under Profile >
Chart colors
- Rename training types and fix their display order (Easy, Long, 60'/30'
Threshold, Tempo, Intervals, MAS Test, Race) everywhere they're listed
- Add an Efficiency Factor progression metric; fix Progression chart axes to
use tight non-zero-based domains, m:ss/km pace formatting, and rounded
ticks instead of raw floating-point labels
- Expose the raw get_workout_by_id() payload in the raw-data viewer
alongside activity/lap/detail JSON; enlarge the modal and shrink array
indentation for readability
- Fix "last sync" reporting a meaningless activity count: record one
combined sync run per manual "Sync now" and count genuinely new
activities instead of re-listing whatever Garmin returned for the queried
window
- Let a Review Queue activity be manually cleared back to Unclassified, and
make "Reset all" available even while disconnected from Garmin
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-20 06:33:47 +02:00
|
|
|
RawJSON: `{"activityId":23554066504,"activityName":"Auriol - W2-5-Base Endurance","activityType":{"typeKey":"running"}}`,
|
2026-07-17 18:33:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
id, err := db.UpsertActivity(ctx, a)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("UpsertActivity (insert): %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
a.AvgHR = f(150) // simulate a re-sync with a corrected value
|
|
|
|
|
id2, err := db.UpsertActivity(ctx, a)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("UpsertActivity (update): %v", err)
|
|
|
|
|
}
|
|
|
|
|
if id != id2 {
|
|
|
|
|
t.Fatalf("expected same internal id across upserts, got %d then %d", id, id2)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
got, ok, err := db.GetActivity(ctx, id)
|
|
|
|
|
if err != nil || !ok {
|
|
|
|
|
t.Fatalf("GetActivity: ok=%v err=%v", ok, err)
|
|
|
|
|
}
|
|
|
|
|
if *got.AvgHR != 150 {
|
|
|
|
|
t.Errorf("AvgHR = %v, want 150 (update should have applied)", *got.AvgHR)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
all, err := db.ListActivities(ctx, ActivityFilter{})
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("ListActivities: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if len(all) != 1 {
|
|
|
|
|
t.Fatalf("expected exactly 1 activity after upsert-update, got %d", len(all))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestKindAssignment_AppendOnlyHistoryAndCurrentView(t *testing.T) {
|
|
|
|
|
db := openTestDB(t)
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
|
|
activityID, err := db.UpsertActivity(ctx, Activity{
|
|
|
|
|
GarminActivityID: 1,
|
|
|
|
|
StartTimeUTC: "2026-07-11 05:00:00",
|
|
|
|
|
RawJSON: "{}",
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("UpsertActivity: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
kindID, err := db.CreateWorkoutKind(ctx, WorkoutKind{
|
2026-07-17 18:48:04 +02:00
|
|
|
Name: "Test Assignment Custom",
|
2026-07-17 18:33:06 +02:00
|
|
|
RuleJSON: `{"match":"all","conditions":[]}`,
|
|
|
|
|
IsActive: true,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("CreateWorkoutKind: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// First: rule engine says needs_review (ambiguous).
|
|
|
|
|
if _, err := db.InsertKindAssignment(ctx, KindAssignment{
|
|
|
|
|
ActivityID: activityID,
|
|
|
|
|
AssignmentSource: AssignmentSourceRuleEngine,
|
|
|
|
|
Status: AssignmentStatusNeedsReview,
|
|
|
|
|
CandidateKindsJSON: `[{"kind_id":1,"score":0.5}]`,
|
|
|
|
|
}); err != nil {
|
|
|
|
|
t.Fatalf("InsertKindAssignment (rule engine): %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
queue, err := db.ReviewQueue(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("ReviewQueue: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if len(queue) != 1 {
|
|
|
|
|
t.Fatalf("expected 1 item in review queue, got %d", len(queue))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Then: user manually resolves it.
|
|
|
|
|
if _, err := db.InsertKindAssignment(ctx, KindAssignment{
|
|
|
|
|
ActivityID: activityID,
|
|
|
|
|
WorkoutKindID: &kindID,
|
|
|
|
|
AssignmentSource: AssignmentSourceManual,
|
|
|
|
|
Status: AssignmentStatusAssigned,
|
|
|
|
|
}); err != nil {
|
|
|
|
|
t.Fatalf("InsertKindAssignment (manual): %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
queue, err = db.ReviewQueue(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("ReviewQueue after resolve: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if len(queue) != 0 {
|
|
|
|
|
t.Fatalf("expected 0 items in review queue after manual resolve, got %d", len(queue))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
current, ok, err := db.CurrentAssignment(ctx, activityID)
|
|
|
|
|
if err != nil || !ok {
|
|
|
|
|
t.Fatalf("CurrentAssignment: ok=%v err=%v", ok, err)
|
|
|
|
|
}
|
|
|
|
|
if current.AssignmentSource != AssignmentSourceManual || current.Status != AssignmentStatusAssigned {
|
|
|
|
|
t.Errorf("current assignment = %+v, want manual/assigned", current)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The original rule-engine assignment must still exist (append-only history).
|
|
|
|
|
var historyCount int
|
|
|
|
|
if err := db.QueryRowContext(ctx, `SELECT COUNT(*) FROM kind_assignments WHERE activity_id = ?`, activityID).Scan(&historyCount); err != nil {
|
|
|
|
|
t.Fatalf("count history: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if historyCount != 2 {
|
|
|
|
|
t.Errorf("expected 2 historical assignment rows (rule engine + manual), got %d", historyCount)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
forKind, err := db.AssignmentsForKind(ctx, kindID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("AssignmentsForKind: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if len(forKind) != 1 {
|
|
|
|
|
t.Fatalf("expected 1 assigned activity for kind %d, got %d", kindID, len(forKind))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestReplaceLapsIsIdempotent(t *testing.T) {
|
|
|
|
|
db := openTestDB(t)
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
|
|
activityID, err := db.UpsertActivity(ctx, Activity{
|
|
|
|
|
GarminActivityID: 2,
|
|
|
|
|
StartTimeUTC: "2026-07-11 05:00:00",
|
|
|
|
|
RawJSON: "{}",
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("UpsertActivity: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
laps := []Lap{
|
Dedup Garmin data storage, configurable chart colors, taxonomy fixes, sync/progression fixes
- Remove duplicated Garmin fields from storage; decode display-only fields
(activity name/type, lap duration/HR, structured workout raw JSON) from
RawJSON at API-response time instead of storing redundant columns
- Add a fully configurable chart color system (pace/HR main-line colors, 4
effort-kind colors, tint/darken/brighten intensity knobs) under Profile >
Chart colors
- Rename training types and fix their display order (Easy, Long, 60'/30'
Threshold, Tempo, Intervals, MAS Test, Race) everywhere they're listed
- Add an Efficiency Factor progression metric; fix Progression chart axes to
use tight non-zero-based domains, m:ss/km pace formatting, and rounded
ticks instead of raw floating-point labels
- Expose the raw get_workout_by_id() payload in the raw-data viewer
alongside activity/lap/detail JSON; enlarge the modal and shrink array
indentation for readability
- Fix "last sync" reporting a meaningless activity count: record one
combined sync run per manual "Sync now" and count genuinely new
activities instead of re-listing whatever Garmin returned for the queried
window
- Let a Review Queue activity be manually cleared back to Unclassified, and
make "Reset all" available even while disconnected from Garmin
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-20 06:33:47 +02:00
|
|
|
{LapIndex: 1, RawJSON: "{}"},
|
|
|
|
|
{LapIndex: 2, RawJSON: "{}"},
|
2026-07-17 18:33:06 +02:00
|
|
|
}
|
|
|
|
|
if err := db.ReplaceLaps(ctx, activityID, laps); err != nil {
|
|
|
|
|
t.Fatalf("ReplaceLaps (first): %v", err)
|
|
|
|
|
}
|
|
|
|
|
// Re-sync with a different set (e.g. corrected data) should fully replace, not append.
|
|
|
|
|
if err := db.ReplaceLaps(ctx, activityID, laps[:1]); err != nil {
|
|
|
|
|
t.Fatalf("ReplaceLaps (second): %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
got, err := db.LapsForActivity(ctx, activityID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("LapsForActivity: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if len(got) != 1 {
|
|
|
|
|
t.Fatalf("expected 1 lap after replace, got %d", len(got))
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-07-25 10:20:59 +02:00
|
|
|
|
|
|
|
|
func TestUsersAndOwnershipSchema_AppliesCleanly(t *testing.T) {
|
|
|
|
|
db := openTestDB(t)
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
|
|
// A fresh DB has no legacy singleton rows, so every user_id column
|
|
|
|
|
// should already be backfilled to nothing (fresh install, no rows at
|
|
|
|
|
// all yet in these tables besides the migration-seeded workout_kinds --
|
|
|
|
|
// which do have NULL user_id until a real user is provisioned).
|
|
|
|
|
var nullableCount int
|
|
|
|
|
if err := db.QueryRowContext(ctx, `SELECT COUNT(*) FROM workout_kinds WHERE user_id IS NULL`).Scan(&nullableCount); err != nil {
|
|
|
|
|
t.Fatalf("count workout_kinds: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if nullableCount != 8 {
|
|
|
|
|
t.Fatalf("expected 8 migration-seeded workout_kinds with NULL user_id on a fresh DB, got %d", nullableCount)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// UNIQUE(user_id, name) allows the same name across two different users.
|
|
|
|
|
if _, err := db.ExecContext(ctx, `INSERT INTO users (oidc_sub, display_name) VALUES ('sub-a', 'A'), ('sub-b', 'B')`); err != nil {
|
|
|
|
|
t.Fatalf("insert users: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if _, err := db.ExecContext(ctx, `
|
|
|
|
|
INSERT INTO workout_kinds (user_id, name, rule_json)
|
|
|
|
|
VALUES ((SELECT id FROM users WHERE oidc_sub='sub-a'), 'Custom Kind', '{}'),
|
|
|
|
|
((SELECT id FROM users WHERE oidc_sub='sub-b'), 'Custom Kind', '{}')`); err != nil {
|
|
|
|
|
t.Fatalf("expected same workout_kind name to be allowed across two different users, got: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// profile/sync_state UNIQUE(user_id) still rejects a genuine duplicate.
|
|
|
|
|
if _, err := db.ExecContext(ctx, `INSERT INTO profile (user_id) VALUES ((SELECT id FROM users WHERE oidc_sub='sub-a'))`); err != nil {
|
|
|
|
|
t.Fatalf("insert profile for sub-a: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if _, err := db.ExecContext(ctx, `INSERT INTO profile (user_id) VALUES ((SELECT id FROM users WHERE oidc_sub='sub-a'))`); err == nil {
|
|
|
|
|
t.Fatal("expected a second profile row for the same user_id to violate UNIQUE(user_id)")
|
|
|
|
|
}
|
|
|
|
|
}
|