package repository import ( "testing" "vidarchive/internal/models" ) // MarkStarted must claim a download exactly once: the worker pool's Submit and // its 2s queue checker can both enqueue the same queued download, and only one // worker may actually process it. func TestMarkStartedClaimsOnce(t *testing.T) { db := setupTestDB(t) defer db.Close() repo := NewDownloadRepository(db) d := &models.Download{URL: "https://example.com/v", Status: "queued"} if err := repo.Create(d); err != nil { t.Fatalf("create: %v", err) } claimed, err := repo.MarkStarted(d.ID) if err != nil { t.Fatalf("first MarkStarted: %v", err) } if !claimed { t.Fatal("first MarkStarted should claim the queued download") } claimed, err = repo.MarkStarted(d.ID) if err != nil { t.Fatalf("second MarkStarted: %v", err) } if claimed { t.Error("second MarkStarted should not re-claim a download that is already downloading") } }