From 70a5159f815aec75aaabf26dc48efa5070f2fa36 Mon Sep 17 00:00:00 2001 From: zwitschi Date: Thu, 13 Aug 2026 13:40:03 +0200 Subject: [PATCH] fix(backend): serialize SQLite test connections and await async normalize --- backend/chat/src/handlers/handlers_test.go | 33 ++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/backend/chat/src/handlers/handlers_test.go b/backend/chat/src/handlers/handlers_test.go index ee79baa..1e7a813 100644 --- a/backend/chat/src/handlers/handlers_test.go +++ b/backend/chat/src/handlers/handlers_test.go @@ -103,6 +103,13 @@ func setupTestDeps(t *testing.T) (*Dependencies, *gorm.DB, *auth.SessionStore) { if err != nil { t.Fatalf("failed to open test DB: %v", err) } + // A single connection keeps the in-memory SQLite database shared across + // goroutines (each :memory: connection would otherwise be a separate DB). + sqlDB, err := gormDB.DB() + if err != nil { + t.Fatalf("failed to get sql.DB: %v", err) + } + sqlDB.SetMaxOpenConns(1) gormDB.AutoMigrate(&db.Message{}, &db.ModerationLog{}, &db.Media{}, &db.ScheduleOverride{}, &db.User{}) mr := newMockRedis() sessionStore := auth.NewSessionStore() @@ -1361,4 +1368,30 @@ func TestHandleAdminMediaByID_Normalize(t *testing.T) { if w.Code != http.StatusAccepted { t.Errorf("status = %d, want 202", w.Code) } + + // Wait for the asynchronous normalization goroutine to finish before + // t.Cleanup restores media.NormalizeVideo, avoiding a data race on the + // package-level mock variable. + var updated db.Media + deadline := time.Now().Add(5 * time.Second) + for { + gormDB.First(&updated, 1) + if updated.Status != media.StatusNormalizing { + break + } + if time.Now().After(deadline) { + t.Fatal("normalization did not complete within 5s") + } + time.Sleep(10 * time.Millisecond) + } + + if updated.Status != media.StatusReady { + t.Errorf("Status = %q, want ready", updated.Status) + } + if updated.DurationSeconds != 90 { + t.Errorf("DurationSeconds = %d, want 90", updated.DurationSeconds) + } + if !strings.HasSuffix(updated.Path, ".normalized.mp4") { + t.Errorf("Path = %q, want .normalized.mp4 suffix", updated.Path) + } } \ No newline at end of file