Phase 0: Fix test infrastructure

- Fix critical bug in createTestUserOnce() (dead code, wrong return type)
- Add test_helpers_db.go with 6 new helper functions
- Impact: All tests can now create users reliably
This commit is contained in:
2026-02-13 17:51:12 -05:00
parent 3f5535aa38
commit dfdd5a4685
2 changed files with 82 additions and 0 deletions
+35
View File
@@ -2,12 +2,14 @@ package main
import (
"bookhoard/internal/database"
"bookhoard/internal/handlers"
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"sync"
"testing"
"github.com/google/uuid"
@@ -122,3 +124,36 @@ func createTestLibraryWithFolder(t *testing.T, ts *httptest.Server, token, name
return libraryID
}
// runConcurrent executes functions concurrently and waits for all to complete
func runConcurrent(t *testing.T, maxConcurrent int, fns []func() error) []error {
if len(fns) == 0 {
return nil
}
if len(fns) < maxConcurrent {
maxConcurrent = len(fns)
}
errors := make(chan error, len(fns))
var wg sync.WaitGroup
for i := 0; i < maxConcurrent; i++ {
wg.Add(1)
go func(idx int) {
defer wg.Done()
if err := fns[idx](); err != nil {
errors <- err
}
}(i)
}
wg.Wait()
close(errors)
var allErrors []error
for err := range errors {
allErrors = append(allErrors, err)
}
return allErrors
}