fix(tests): complete TestServerSetup migration for remaining test files

Finish migrating all test files to the new TestServerSetup pattern
introduced by the goroutine cleanup refactoring. This resolves all
remaining compilation errors in the test suite.

Changes:
- device_cap_test.go: Fix undefined ts references (7 instances)
  * Replace ts.URL with setup.Server.URL in all test functions
  * Fix URL references in t.Run subtest closures

- queue_test.go: Fix undefined db and helper function issues (5 instances)
  * Replace db.CreateDevice with setup.DB.CreateDevice
  * Fix loginAdminUser() to use ts/db parameters instead of setup
  * Fix loginUserWithID() to use ts parameter instead of setup

- websocket_test.go: Convert 5 tests to new TestServerSetup pattern
  * Replace old pattern (ts, queries, _) with new pattern (setup)
  * Update all resource references to use setup.Server and setup.DB
  * Fix getTestUserID calls to include t parameter

Build Impact:
- All compilation errors resolved
- Integration tests now compile successfully
- No functional changes to test logic

Related: TestServerSetup cleanup pattern (TEST_CLEANUP_PATTERN.md)
This commit is contained in:
2026-02-10 13:24:08 -05:00
parent 509291b584
commit b2a3955c1e
3 changed files with 27 additions and 27 deletions
+7 -7
View File
@@ -65,7 +65,7 @@ func TestUpdateUserMaxDevices(t *testing.T) {
} }
jsonData, _ := json.Marshal(payload) jsonData, _ := json.Marshal(payload)
req, _ := http.NewRequest("PUT", ts.URL+"/api/auth/users/"+userID.String()+"/max-devices", bytes.NewBuffer(jsonData)) req, _ := http.NewRequest("PUT", setup.Server.URL+"/api/auth/users/"+userID.String()+"/max-devices", bytes.NewBuffer(jsonData))
req.Header.Set("Content-Type", "application/json") req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+adminToken) req.Header.Set("Authorization", "Bearer "+adminToken)
@@ -131,7 +131,7 @@ func TestUpdateUserMaxDevicesValidation(t *testing.T) {
} }
jsonData, _ := json.Marshal(payload) jsonData, _ := json.Marshal(payload)
req, _ := http.NewRequest("PUT", ts.URL+"/api/auth/users/"+userID.String()+"/max-devices", bytes.NewBuffer(jsonData)) req, _ := http.NewRequest("PUT", setup.Server.URL+"/api/auth/users/"+userID.String()+"/max-devices", bytes.NewBuffer(jsonData))
req.Header.Set("Content-Type", "application/json") req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+adminToken) req.Header.Set("Authorization", "Bearer "+adminToken)
@@ -164,7 +164,7 @@ func TestUpdateUserMaxDevicesAuth(t *testing.T) {
} }
jsonData, _ := json.Marshal(payload) jsonData, _ := json.Marshal(payload)
req, _ := http.NewRequest("PUT", ts.URL+"/api/auth/users/"+userID.String()+"/max-devices", bytes.NewBuffer(jsonData)) req, _ := http.NewRequest("PUT", setup.Server.URL+"/api/auth/users/"+userID.String()+"/max-devices", bytes.NewBuffer(jsonData))
req.Header.Set("Content-Type", "application/json") req.Header.Set("Content-Type", "application/json")
client := &http.Client{} client := &http.Client{}
@@ -187,7 +187,7 @@ func TestUpdateUserMaxDevicesAuth(t *testing.T) {
} }
jsonData, _ := json.Marshal(payload) jsonData, _ := json.Marshal(payload)
req, _ := http.NewRequest("PUT", ts.URL+"/api/auth/users/"+userID.String()+"/max-devices", bytes.NewBuffer(jsonData)) req, _ := http.NewRequest("PUT", setup.Server.URL+"/api/auth/users/"+userID.String()+"/max-devices", bytes.NewBuffer(jsonData))
req.Header.Set("Content-Type", "application/json") req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+regularToken) req.Header.Set("Authorization", "Bearer "+regularToken)
@@ -218,7 +218,7 @@ func TestUpdateUserMaxDevicesNonExistentUser(t *testing.T) {
} }
jsonData, _ := json.Marshal(payload) jsonData, _ := json.Marshal(payload)
req, _ := http.NewRequest("PUT", ts.URL+"/api/auth/users/"+nonExistentUserID.String()+"/max-devices", bytes.NewBuffer(jsonData)) req, _ := http.NewRequest("PUT", setup.Server.URL+"/api/auth/users/"+nonExistentUserID.String()+"/max-devices", bytes.NewBuffer(jsonData))
req.Header.Set("Content-Type", "application/json") req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+adminToken) req.Header.Set("Authorization", "Bearer "+adminToken)
@@ -247,7 +247,7 @@ func TestUpdateUserMaxDevicesMissingUserID(t *testing.T) {
jsonData, _ := json.Marshal(payload) jsonData, _ := json.Marshal(payload)
// Missing user ID in URL // Missing user ID in URL
req, _ := http.NewRequest("PUT", ts.URL+"/api/auth/users//max-devices", bytes.NewBuffer(jsonData)) req, _ := http.NewRequest("PUT", setup.Server.URL+"/api/auth/users//max-devices", bytes.NewBuffer(jsonData))
req.Header.Set("Content-Type", "application/json") req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+adminToken) req.Header.Set("Authorization", "Bearer "+adminToken)
@@ -269,7 +269,7 @@ func TestListUsersIncludesMaxDevices(t *testing.T) {
adminUserID := getTestUserID(t, setup.DB) adminUserID := getTestUserID(t, setup.DB)
adminToken = getAdminToken(t, setup.Server, adminUserID) adminToken = getAdminToken(t, setup.Server, adminUserID)
req, _ := http.NewRequest("GET", ts.URL+"/api/auth/users", nil) req, _ := http.NewRequest("GET", setup.Server.URL+"/api/auth/users", nil)
req.Header.Set("Authorization", "Bearer "+adminToken) req.Header.Set("Authorization", "Bearer "+adminToken)
client := &http.Client{} client := &http.Client{}
+6 -6
View File
@@ -41,7 +41,7 @@ func TestGetDeviceQueueStats(t *testing.T) {
userID := getTestUserID(t, setup.DB) userID := getTestUserID(t, setup.DB)
deviceToken := fmt.Sprintf("dev_%s", uuid.New().String()) deviceToken := fmt.Sprintf("dev_%s", uuid.New().String())
device, err := db.CreateDevice(context.Background(), database.CreateDeviceParams{ device, err := setup.DB.CreateDevice(context.Background(), database.CreateDeviceParams{
UserID: pgtype.UUID{Bytes: [16]byte(userID), Valid: true}, UserID: pgtype.UUID{Bytes: [16]byte(userID), Valid: true},
DeviceName: "Test Device", DeviceName: "Test Device",
DeviceType: "koreader", DeviceType: "koreader",
@@ -77,7 +77,7 @@ func TestListDeviceQueueItems(t *testing.T) {
userID := getTestUserID(t, setup.DB) userID := getTestUserID(t, setup.DB)
deviceToken := fmt.Sprintf("dev_%s", uuid.New().String()) deviceToken := fmt.Sprintf("dev_%s", uuid.New().String())
device, err := db.CreateDevice(context.Background(), database.CreateDeviceParams{ device, err := setup.DB.CreateDevice(context.Background(), database.CreateDeviceParams{
UserID: pgtype.UUID{Bytes: [16]byte(userID), Valid: true}, UserID: pgtype.UUID{Bytes: [16]byte(userID), Valid: true},
DeviceName: "Test Device", DeviceName: "Test Device",
DeviceType: "koreader", DeviceType: "koreader",
@@ -143,7 +143,7 @@ func TestClearDeviceQueue(t *testing.T) {
userID := getTestUserID(t, setup.DB) userID := getTestUserID(t, setup.DB)
deviceToken := fmt.Sprintf("dev_%s", uuid.New().String()) deviceToken := fmt.Sprintf("dev_%s", uuid.New().String())
device, err := db.CreateDevice(context.Background(), database.CreateDeviceParams{ device, err := setup.DB.CreateDevice(context.Background(), database.CreateDeviceParams{
UserID: pgtype.UUID{Bytes: [16]byte(userID), Valid: true}, UserID: pgtype.UUID{Bytes: [16]byte(userID), Valid: true},
DeviceName: "Test Device", DeviceName: "Test Device",
DeviceType: "koreader", DeviceType: "koreader",
@@ -223,7 +223,7 @@ func loginAdminUser(t *testing.T, ts *httptest.Server, db *database.Queries) str
_, err = db.ListUsers(context.Background()) _, err = db.ListUsers(context.Background())
if err == nil { if err == nil {
return loginTestUser(t, setup.Server, setup.DB) return loginTestUser(t, ts, db)
} }
loginRequest := map[string]interface{}{ loginRequest := map[string]interface{}{
@@ -232,7 +232,7 @@ func loginAdminUser(t *testing.T, ts *httptest.Server, db *database.Queries) str
} }
body, _ := json.Marshal(loginRequest) body, _ := json.Marshal(loginRequest)
req, _ := http.NewRequest("POST", setup.Server.URL+"/api/auth/login", bytes.NewBuffer(body)) req, _ := http.NewRequest("POST", ts.URL+"/api/auth/login", bytes.NewBuffer(body))
req.Header.Set("Content-Type", "application/json") req.Header.Set("Content-Type", "application/json")
client := &http.Client{} client := &http.Client{}
@@ -255,7 +255,7 @@ func loginUserWithID(t *testing.T, ts *httptest.Server, db *database.Queries, us
} }
body, _ := json.Marshal(loginRequest) body, _ := json.Marshal(loginRequest)
req, _ := http.NewRequest("POST", setup.Server.URL+"/api/auth/login", bytes.NewBuffer(body)) req, _ := http.NewRequest("POST", ts.URL+"/api/auth/login", bytes.NewBuffer(body))
req.Header.Set("Content-Type", "application/json") req.Header.Set("Content-Type", "application/json")
client := &http.Client{} client := &http.Client{}
+14 -14
View File
@@ -20,10 +20,10 @@ import (
// TestWebSocketConnection tests basic WebSocket connection and authentication // TestWebSocketConnection tests basic WebSocket connection and authentication
func TestWebSocketConnection(t *testing.T) { func TestWebSocketConnection(t *testing.T) {
// Setup test server with WebSocket // Setup test server with WebSocket
ts, queries, _ := setupTestServer(t) setup := setupTestServer(t)
// Get JWT token for a test user // Get JWT token for a test user
token := loginTestUser(t, ts, queries) token := loginTestUser(t, setup.Server, setup.DB)
// Connect to WebSocket endpoint // Connect to WebSocket endpoint
wsURL := strings.Replace(setup.Server.URL, "http", "ws", 1) + "/ws/sync?token=" + token wsURL := strings.Replace(setup.Server.URL, "http", "ws", 1) + "/ws/sync?token=" + token
@@ -50,13 +50,13 @@ func TestWebSocketConnection(t *testing.T) {
// TestWebSocketDeviceAuth tests device authentication via WebSocket // TestWebSocketDeviceAuth tests device authentication via WebSocket
func TestWebSocketDeviceAuth(t *testing.T) { func TestWebSocketDeviceAuth(t *testing.T) {
ts, queries, _ := setupTestServer(t) setup := setupTestServer(t)
// Create a test device // Create a test device
userID := getTestUserID(t, queries) userID := getTestUserID(t, setup.DB)
deviceID := uuid.New() deviceID := uuid.New()
_, err := queries.CreateDevice(context.Background(), database.CreateDeviceParams{ _, err := setup.DB.CreateDevice(context.Background(), database.CreateDeviceParams{
UserID: pgtype.UUID{Bytes: [16]byte(userID), Valid: true}, UserID: pgtype.UUID{Bytes: [16]byte(userID), Valid: true},
DeviceName: "Test KOReader", DeviceName: "Test KOReader",
DeviceType: "koreader", DeviceType: "koreader",
@@ -76,21 +76,21 @@ func TestWebSocketDeviceAuth(t *testing.T) {
// We can't easily test WebSocket with custom headers using gorilla/websocket // We can't easily test WebSocket with custom headers using gorilla/websocket
// So this test just verifies the device exists // So this test just verifies the device exists
device, err := queries.GetDeviceByAuthToken(context.Background(), "test-device-token-"+deviceID.String()) device, err := setup.DB.GetDeviceByAuthToken(context.Background(), "test-device-token-"+deviceID.String())
require.NoError(t, err) require.NoError(t, err)
assert.Equal(t, "Test KOReader", device.DeviceName) assert.Equal(t, "Test KOReader", device.DeviceName)
} }
// TestWebSocketProgressBroadcast tests that progress updates are broadcast to connected clients // TestWebSocketProgressBroadcast tests that progress updates are broadcast to connected clients
func TestWebSocketProgressBroadcast(t *testing.T) { func TestWebSocketProgressBroadcast(t *testing.T) {
ts, queries, _ := setupTestServer(t) setup := setupTestServer(t)
// Get JWT token // Get JWT token
token := loginTestUser(t, ts, queries) token := loginTestUser(t, setup.Server, setup.DB)
// Create a test media item // Create a test media item
userID := getTestUserID(t, queries) userID := getTestUserID(t, setup.DB)
mediaID := createTestMediaItem(t, queries, userID) mediaID := createTestMediaItem(t, setup.DB, userID)
// Connect WebSocket client // Connect WebSocket client
wsURL := strings.Replace(setup.Server.URL, "http", "ws", 1) + "/ws/sync?token=" + token wsURL := strings.Replace(setup.Server.URL, "http", "ws", 1) + "/ws/sync?token=" + token
@@ -145,9 +145,9 @@ func TestWebSocketProgressBroadcast(t *testing.T) {
// TestWebSocketPingPong tests that ping/pong messages work correctly // TestWebSocketPingPong tests that ping/pong messages work correctly
func TestWebSocketPingPong(t *testing.T) { func TestWebSocketPingPong(t *testing.T) {
ts, queries, _ := setupTestServer(t) setup := setupTestServer(t)
token := loginTestUser(t, ts, queries) token := loginTestUser(t, setup.Server, setup.DB)
wsURL := strings.Replace(setup.Server.URL, "http", "ws", 1) + "/ws/sync?token=" + token wsURL := strings.Replace(setup.Server.URL, "http", "ws", 1) + "/ws/sync?token=" + token
ws, _, err := websocket.DefaultDialer.Dial(wsURL, nil) ws, _, err := websocket.DefaultDialer.Dial(wsURL, nil)
@@ -177,9 +177,9 @@ func TestWebSocketPingPong(t *testing.T) {
// TestWebSocketConnectionLimit tests that the server handles multiple connections // TestWebSocketConnectionLimit tests that the server handles multiple connections
func TestWebSocketConnectionLimit(t *testing.T) { func TestWebSocketConnectionLimit(t *testing.T) {
ts, queries, _ := setupTestServer(t) setup := setupTestServer(t)
token := loginTestUser(t, ts, queries) token := loginTestUser(t, setup.Server, setup.DB)
// Create multiple connections // Create multiple connections
connections := make([]*websocket.Conn, 5) connections := make([]*websocket.Conn, 5)