Deadcode-verified removals (RTA reachability from main, cross-checked with -test and textual xrefs): Go server: - whole files: middleware/transaction.go, middleware/error_handler.go, middleware/password_validator.go (live registration machinery kept), handlers/sync.go, sync/offline.go - unrouted endpoints: auth UpdateUsername/UpdateEmail, timezone settings handler, OPDS RegisterOPDS, Kobo LibrarySync handler, ReaderHandler.ShowReader, JSON-data twins on collections/library - duplicate book-matching handlers (book_matching.go Handler twins, matching.go MatchingHandler twins), superseded helpers (router.parseUUID, handlers.ValidateDeviceToken, normalizePath, userExists, generateLockID) Templ/client: - never-routed pages: api_explorer.templ, queue.templ and their client modules api-explorer.ts, api-explorer-docs.ts, queue.ts - fully orphaned wrapper modules events.ts, dom.ts - broken clearSyncQueue (called a nonexistent endpoint) and the hidden, never-populated sync-queue section on the devices page - 166+ unused exports demoted to private (knip-verified); ~85 truly dead declarations removed via tsc noUnusedLocals fixpoint - stale 'interactive API Explorer' claims dropped from docs The docs drift-gate package (internal/docs), templ-bound modules (dashboard, stats, header, index, login, profile*, bookshelf, collections, toast-error), sqlc-generated code, and web/static are intentionally untouched.
80 lines
2.8 KiB
Go
80 lines
2.8 KiB
Go
package main
|
|
|
|
import (
|
|
"bookhoard/internal/handlers"
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// TestWithStructs demonstrates struct-based testing patterns
|
|
// BEFORE: map[string]interface{} -> AFTER: handlers.* structs
|
|
// BEFORE: No DB verification -> AFTER: Database verification
|
|
|
|
func TestWithStructs(t *testing.T) {
|
|
deviceSetup := setupDeviceTest(t)
|
|
token := deviceSetup.UserToken
|
|
|
|
// Create a device to test listing
|
|
device := deviceSetup.CreateDevice(t, "Test Device", "koreader", "test-device-struct-123")
|
|
|
|
t.Run("ListDevices with struct", func(t *testing.T) {
|
|
req := httptest.NewRequest("GET", "/api/devices", nil)
|
|
req.Header.Set("Authorization", "Bearer "+token)
|
|
rr := httptest.NewRecorder()
|
|
deviceSetup.Server.Config.Handler.ServeHTTP(rr, req)
|
|
|
|
assert.Equal(t, http.StatusOK, rr.Code, "Should list devices")
|
|
|
|
var response handlers.DeviceListResponse
|
|
err := json.Unmarshal(rr.Body.Bytes(), &response)
|
|
require.NoError(t, err, "Response should match DeviceListResponse schema")
|
|
assert.GreaterOrEqual(t, len(response.Devices), 1, "Should have at least one device")
|
|
|
|
firstDevice := response.Devices[0]
|
|
assert.Equal(t, device.Name, firstDevice.DeviceName, "Should match device name")
|
|
assert.Equal(t, device.Type, firstDevice.DeviceType, "Should match device type")
|
|
})
|
|
|
|
t.Run("UpdateDevice with struct and DB verification", func(t *testing.T) {
|
|
syncEnabled := false
|
|
syncFreq := int32(15)
|
|
|
|
updateRequest := map[string]interface{}{
|
|
"sync_enabled": &syncEnabled,
|
|
"sync_frequency_minutes": &syncFreq,
|
|
}
|
|
updateBody, _ := json.Marshal(updateRequest)
|
|
|
|
req := httptest.NewRequest("PUT", fmt.Sprintf("/api/devices/%s", device.ID), bytes.NewBuffer(updateBody))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
req.Header.Set("Authorization", "Bearer "+token)
|
|
rr := httptest.NewRecorder()
|
|
deviceSetup.Server.Config.Handler.ServeHTTP(rr, req)
|
|
|
|
assert.Equal(t, http.StatusOK, rr.Code, "Should update device")
|
|
|
|
var response map[string]interface{}
|
|
err := json.Unmarshal(rr.Body.Bytes(), &response)
|
|
require.NoError(t, err, "Response should unmarshal")
|
|
|
|
assert.True(t, response["device_updated"].(bool), "Device should be updated")
|
|
|
|
// NEW: Database verification
|
|
pgDeviceID := pgtype.UUID{Bytes: [16]byte(device.ID), Valid: true}
|
|
updatedDevice, err := deviceSetup.DB.GetDevice(context.Background(), pgDeviceID)
|
|
require.NoError(t, err, "Device should exist in database after update")
|
|
|
|
assert.Equal(t, syncEnabled, updatedDevice.SyncEnabled.Bool, "DB: Sync should be disabled")
|
|
assert.Equal(t, syncFreq, updatedDevice.SyncFrequencyMinutes.Int32, "DB: Sync frequency should be updated")
|
|
})
|
|
}
|