Commit Graph
679 Commits
Author SHA1 Message Date
john-okeefe 778d611cab test: update collections bulk tests for 'added' field rename
Update test assertions in TestCollectionsBulkOperations to expect
'added' instead of 'success' in the response, matching the handler
change made in the bulk operations rename.

Fixes:
- BulkAddBooks_InvalidCollectionID: assert 'added' field exists
- BulkAddBooks_SingleOperation: assert 'added' field exists
2026-02-10 20:51:29 -05:00
john-okeefe bddb411a3d fix: add validation for empty media_item_id in bulk update
Add strict validation to return 400 Bad Request when any media_item_id
is empty in the bulk-update request, rather than treating it as a
partial failure with 200 OK.

This aligns the handler behavior with test expectations for the
BulkUpdateBooks_EmptyBookIDs test case.
2026-02-10 20:51:23 -05:00
john-okeefe 0022f75a73 docs: remove old books API documentation
Endpoints moved to /api/media-items/, so delete the books/ directory.
Includes bulk_delete_books.md, bulk_update_books.md, and download_book.md.
2026-02-10 19:59:19 -05:00
john-okeefe 5f268238c8 docs: update cross-references from /api/books/ to /api/media-items/
- Update api-reference.md with new endpoint paths
- Update api-reference.md Books API section → Media Items API section
- Update index.md Books API link → Media Items API
- Update get_shelf.md cover_url reference from /api/books/ to /api/media-items/
2026-02-10 19:59:12 -05:00
john-okeefe d4903feb71 docs: add media-items bulk operation and download documentation
- Add comprehensive documentation for bulk delete endpoint
- Add comprehensive documentation for bulk update endpoint
- Add comprehensive documentation for download endpoint
- Document all request/response fields with correct names
- Include examples and error codes
- Add notes on tag normalization and partial success
2026-02-10 19:58:47 -05:00
john-okeefe b364aee631 test(bruno): update API tests for /api/media-items/ endpoints
- Update bulk-delete test: endpoint URL, request field (book_ids → media_item_ids), response field (success → deleted), documentation
- Update bulk-update test: endpoint URL, request structure (updates → media_item_updates), response field (success → updated), documentation
- Update download test: endpoint URL (/api/books/ → /api/media-items/), documentation
2026-02-10 19:58:41 -05:00
john-okeefe bb4b85afcc test: update bulk operations tests for /api/media-items/ endpoints
- Update all test URLs from /api/books/ to /api/media-items/
- Update request structures: book_ids → media_item_ids
- Update bulk-update request format to array of operations
- Update response assertions: success → deleted/updated
- Update result assertions: book_id → media_item_id
2026-02-10 19:57:28 -05:00
john-okeefe 03225cf6e2 refactor: rename bulk operations from /api/books/ to /api/media-items/
- Rename routes: /api/books/bulk-{delete,update} → /api/media-items/bulk-{delete,update}
- Rename route: /api/books/:uuid/download → /api/media-items/:uuid/download
- Update request fields: book_ids → media_item_ids
- Update response fields: success → deleted/updated
- Update result fields: book_id → media_item_id
- Update collection handler: success → added

This change improves API semantic correctness as the system handles
multiple media types (ebooks, comics, manga), not just books.

BREAKING CHANGE: All bulk operation endpoints and field names renamed
2026-02-10 19:56:59 -05:00
john-okeefe f72c552241 fix: correct TestKoboAnalyticsGettests request format to match handler
The test was sending a single object with a "meta" field, but the handler
expects an array of KoboAnalyticsTest objects (matching other Kobo endpoints).

Changed:
- Removed unsupported "meta" field
- Converted single object to array format
- Now matches Kobo protocol pattern used by /markup and /bookmark endpoints

All Kobo tests now pass:
- TestKoboInitialization 
- TestKoboLibrarySync 
- TestKoboMarkupSync 
- TestKoboBookmarkSync 
- TestKoboAnalyticsGettests 
- TestKoboDeviceHeaderParsing 
2026-02-10 18:30:36 -05:00
john-okeefe 7dcc70b448 fix: remove redundant getTestUserID call in TestKoboInitialization
The TestKoboInitialization test was calling getTestUserID() explicitly
on line 25, but loginTestUser() already calls this internally. This caused
the test user to be deleted and recreated after login, leading to
inconsistent state and HTTP 500 errors when creating libraries.

After removing the redundant call:
- TestKoboInitialization now passes
- All Kobo sync tests pass successfully
2026-02-10 18:15:28 -05:00
john-okeefe 7e7945fbb7 fix: make trigger creation idempotent
- Add DROP TRIGGER IF EXISTS before CREATE TRIGGER
- Fixes 'trigger already exists' error during schema initialization
- Allows schema to run multiple times safely
2026-02-10 16:54:01 -05:00
john-okeefe 1f65933653 fix: handle schema.sql file path in containerized environment
- Try multiple locations for schema.sql file
- Support both local dev and containerized deployment paths
- Add informative logging when schema is loaded
- Prevent runtime.Caller issues in containers

Locations checked:
- database/schema/schema.sql (working directory)
- /app/database/schema/schema.sql (container)
- ../database/schema/schema.sql (relative)
- ../../database/schema/schema.sql (relative)

This fixes the 'no such file or directory' error in production containers.
2026-02-10 16:52:12 -05:00
john-okeefe a289353b0b feat: integrate schema initialization into server startup (Phase 3)
- Add schema initialization call after database connection
- Initialize schema before handler creation
- Fatal on failure (schema is critical for app to function)
- Clear log messages show initialization progress

Server startup flow:
1. Load config
2. Connect to database
3. Initialize schema (NEW - ensures all tables/functions exist)
4. Create handlers and services
5. Start server
2026-02-10 16:48:48 -05:00
john-okeefe 9d1a1e0228 feat: add schema initialization logic (Phase 2)
- Create internal/database/schema.go with full initialization logic
- Parse table names from schema.sql using regex (handles both formats)
- Execute schema in atomic transaction
- Verify all expected tables exist
- Verify all critical functions exist (6 functions)
- PostgreSQL advisory locking with 30-second timeout
- Self-healing from partial/corrupted state
- Load schema.sql from filesystem at runtime

Features:
- Defensive regex handles IF NOT EXISTS and legacy CREATE TABLE
- Lock timeout prevents indefinite hangs
- Function verification ensures sync operations work
- Clear error messages with debug hints
2026-02-10 16:48:28 -05:00
john-okeefe 6ffb2ef6bc schema: make all schema statements idempotent (Phase 1)
- Convert 18 CREATE TABLE → CREATE TABLE IF NOT EXISTS (27 total)
- Convert 62 CREATE INDEX → CREATE INDEX IF NOT EXISTS (82 total)
- Add ON CONFLICT to 2 INSERT statements (3 total)
- Verify 8 ALTER TABLE already have IF NOT EXISTS
- Verify 6 CREATE FUNCTION use OR REPLACE

Schema is now fully idempotent and safe for automatic initialization on every startup.
2026-02-10 16:47:18 -05:00
john-okeefe 5b85f61125 docs: add future schema changes guidance to initialization plan
- Document safe vs breaking changes distinction
- List additive changes handled automatically by initialization
- List breaking changes requiring manual migration
- Provide 6-step migration strategy for breaking changes
- Recommend preferring additive changes for automatic initialization
2026-02-10 16:45:29 -05:00
john-okeefe 8689dc0847 docs: add testing strategy and version requirements to schema plan
- Add PostgreSQL version requirements section (min 13, rec 15)
- Add unit testing section for schema.go functions
- Add concurrent startup manual test with 5 instances
- Add debug hint in executeSchema error messages
- Add PostgreSQL troubleshooting section
- Update documentation requirements to include testing
2026-02-10 16:40:35 -05:00
john-okeefe ffa8de3661 docs: improve schema initialization plan with verification
- Fix regex pattern to handle both IF NOT EXISTS and legacy CREATE TABLE formats
- Add 30-second lock timeout to prevent indefinite hangs
- Add function verification (6 critical functions checked)
- Document 8 ALTER TABLE statements already idempotent
- Document 6 CREATE FUNCTION statements use OR REPLACE
- Add time import for timeout support
- Update verification checklist with new requirements
- Update log messages to show table and function counts
2026-02-10 16:39:28 -05:00
john-okeefe 27f62e9ae8 docs(schema): Consolidate schema initialization plans with corrections
Remove obsolete planning documents:
- KOBO_IMPLEMENTATION_PLAN.md (replaced by refined plan)
- SCHEMA_INITIALIZATION_PLAN.md (replaced by refined plan)

Update REFINED_SCHEMA_PLAN.md with critical corrections:
- Fix FNV-1a hash constant (7804706162000639061, was 582394759234)
- Correct table/index counts (27 tables, 82 indexes, not 36/102)
- Change approach: embed existing schema.sql (no duplication)
- Add local database update step after schema changes
- Add documentation requirements section

This consolidates three planning documents into one accurate, actionable plan
for implementing automatic schema initialization with idempotent migrations.
2026-02-10 16:23:56 -05:00
john-okeefe b329187669 created schema plans to keep app from starting before init scripts are complete. 2026-02-10 15:46:18 -05:00
john-okeefe 6e52e49169 fix(tests): Remove duplicate analytics test with stale field expectations
The first GetReadingStats_WithAuth_DefaultDates test was expecting
'total_books' and 'total_reading_time' fields that don't exist in the
API response. The second duplicate test correctly expects
'total_books_read' and 'total_reading_time_minutes'.

This resolves the TestAnalyticsReadingStats failure.
2026-02-10 13:30:14 -05:00
john-okeefe b2a3955c1e 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)
2026-02-10 13:24:08 -05:00
john-okeefe 509291b584 docs(tests): Add TestServerSetup cleanup pattern documentation 2026-02-10 13:09:09 -05:00
john-okeefe bb2ba14976 fix(tests): Fix all remaining test compilation errors 2026-02-10 13:08:10 -05:00
john-okeefe 568e16eab4 fix(tests): Fix helper function variable scope 2026-02-10 13:07:01 -05:00
john-okeefe fd4aa5cb59 fix(tests): Fix t.Run block variable scope issues 2026-02-10 13:06:32 -05:00
john-okeefe f15bf213ee fix(tests): Fix all remaining test compilation errors 2026-02-10 13:06:12 -05:00
john-okeefe 5b32b59781 fix(tests): Fix edge cases in test file migration 2026-02-10 13:03:32 -05:00
john-okeefe 6c610465eb refactor(tests): Update all test files to use TestServerSetup pattern 2026-02-10 13:01:12 -05:00
john-okeefe f3141f18ef refactor(tests): Create TestServerSetup struct with proper resource cleanup
BREAKING CHANGE: setupTestServer() now returns *TestServerSetup instead of (*httptest.Server, *database.Queries, *config.Config)

This fixes the database connection and goroutine leak issues where:
- Each test created a new pgxpool (default max_conns = 4)
- connManager.StartCleanupTask() goroutine was never stopped
- queueProcessor.Start() goroutine was never stopped
- ~160 tests = potential 640+ leaked connections

New TestServerSetup struct provides:
- Automatic cleanup via t.Cleanup()
- Proper goroutine cancellation
- Database pool closing
- Thread-safe close() method with mutex

Phase 1 of test cleanup refactor.
2026-02-10 12:58:51 -05:00
john-okeefe 57cb58bcbf test(kobo): Fix Kobo integration tests with proper device authentication
- Fix TestKoboInitialization: use setupDeviceTest() for device creation
- Fix TestKoboLibrarySync: remove /test-token/ route path, add device auth
- Fix TestKoboMarkupSync: add device auth and last-read-place test case
- Fix TestKoboBookmarkSync: add device auth and last-read-place test case
- Fix TestKoboAnalyticsGettests: add device authentication
- Add debug logging to all test functions
- Remove unused imports (config, database, middleware, router, services, sync)

Phase 3 of KOBO_IMPLEMENTATION_PLAN.md completed (Steps 7-12).

All tests now use proper device authentication (Bearer tokens + x-kobo-device headers)
and include test cases for the new last-read-place bookmark feature.
2026-02-10 12:41:09 -05:00
john-okeefe 80ad45e7f9 feat(handlers): Add Kobo sync enhancements and last-read-place support
- Add nil UUID checks after mapContentIdToBookhoardUUID in all handlers
- Add ContentType detection for Kobo EPUB/PDF sync (EPUB=6, PDF=5)
- Add "last-read-place" bookmark type support with EPUB CFI position tracking
- Restore broken mapContentIdToBookhoardUUID function with UUID parsing
- Restore mapBookhoardUUIDToKoboContentId helper function
- Restore getCollectionMetadataForBook helper function

This fixes the catastrophic file corruption from commit 2200720 which
deleted 414 lines and inserted code in the wrong location.

Phase 1-3 of KOBO_IMPLEMENTATION_PLAN.md completed:
- Step 4: Nil UUID checks in Markup, Bookmark, AnalyticsGettests, SyncFromServer
- Step 5: ContentType field added to KoboReadingSync struct
- Step 6: last-read-place case added to Markup handler switch statement

Testing: Code compiles successfully, all handlers properly structured
2026-02-10 12:41:04 -05:00
john-okeefe 82a5cf70f2 fix(middleware): Correct rate limit header type conversion
- Fix string(rune(remaining)) to strconv.Itoa(remaining) in device_auth.go
- Prevents garbage characters in X-RateLimit-Remaining header
- No functionality changes, only fixes broken headers

Testing: Verified with code inspection that headers return proper integers
2026-02-10 12:40:52 -05:00
john-okeefe 2200720537 fix(middleware): Correct rate limit header type conversion 2026-02-10 12:06:12 -05:00
john-okeefe 2b24dd9dd3 fix: Fix failing integration tests and update documentation
- Fix TestUpdateDevice: Use correct JSON field name and handle float64 type
- Fix TestRejectDeviceRegistration: Expect message response instead of boolean
- Update approve device docs: Add missing response fields
- Update reject device docs: Correct message text and format
- Update Bruno API: Fix example response for reject endpoint

Both integration tests now pass while maintaining API consistency.
2026-02-10 10:22:19 -05:00
john-okeefe 1413c75b26 fix: Fix device response fields and add missing approved confirmation
feat: Improve device test infrastructure with setupDeviceTest helper

refactor: Standardize pending registrations API response field names
2026-02-10 09:31:35 -05:00
john-okeefe 36e03f89b6 test(bruno): add system settings API tests and update list users test
System Settings Bruno Tests (new):
- bruno/system/get-scan-settings.bru
- bruno/system/update-scan-settings.bru
- Test GET endpoint for retrieving system scan settings
- Test PUT endpoint for updating system scan settings
- Include admin authentication requirements
- Document response structures

List Users Bruno Test (update):
- bruno/user/admin/List Users.bru
- Add max_devices to response documentation
- Add device_count to response documentation
- Update feature descriptions

These Bruno tests provide API contract verification for the new
system settings endpoints and document the enhanced user list response.
2026-02-09 20:12:21 -05:00
john-okeefe 90603b7a48 docs: add system settings API documentation and update user list docs
System Settings API Documentation (new):
- docs/developer/api/system/settings.md
- Document GET /api/libraries/scan-settings endpoint
- Document PUT /api/libraries/scan-settings endpoint
- Include request/response examples
- Document validation rules and error codes
- Include migration notes from per-user to system-wide

User List API Documentation (update):
- docs/developer/api/admin/list_users.md
- Add max_devices field to response
- Add device_count field to response
- Include complete response field descriptions table
- Update example to show new fields

Documentation covers both the new system-wide scan settings feature
and the enhanced user list with device monitoring capabilities.
2026-02-09 20:11:56 -05:00
john-okeefe fefb1bd33b test: update tests for system settings and user list enhancements
System Settings Tests (new file):
- Create system_settings_test.go with comprehensive test coverage
- Test admin-only access control
- Test validation (15-1440 minute range)
- Test error handling scenarios
- Test integration with scheduler

User Tests Cleanup:
- Remove old TestScanSettings from user_test.go
- Scan settings moved to system-wide (no longer per-user)

Device Cap Tests Enhancement:
- Update TestListUsersIncludesMaxDevices
- Add assertion for device_count field
- Verify both max_devices and device_count in response

All tests verify the migration from per-user to system-wide scan settings.
2026-02-09 20:11:14 -05:00
john-okeefe bee6d588d6 chore(main): initialize and register SystemSettingsHandler
Add SystemSettingsHandler initialization in main.go:
- Create systemSettingsHandler instance with queries
- Add to router.Config for route registration
- Properly wired with existing dependencies

This enables the system settings endpoints to be registered and functional.
2026-02-09 20:10:53 -05:00
john-okeefe 0551f17f0e refactor(scheduler): migrate from per-user to system-wide scan settings
Update scheduler to use system-wide settings instead of per-user:
- Change Database interface to use GetSystemSetting
- Remove GetScanSettings (per-user method)
- Update checkAndScheduleScans to read system settings
- Apply system-wide scan frequency to all libraries

Scheduler now respects global scan settings for all library scanning,
enabling consistent system-wide scan behavior.
2026-02-09 20:10:40 -05:00
john-okeefe 938e5eed53 feat(router): add system settings routes and handler wiring
Router configuration updates:
- Add SystemSettingsHandler to Config struct
- Register GET /api/libraries/scan-settings (admin-only)
- Register PUT /api/libraries/scan-settings (admin-only)
- Wire SystemSettingsHandler in main.go

These routes replace per-user scan settings endpoints with
system-wide admin-only endpoints.
2026-02-09 20:10:30 -05:00
john-okeefe 4488ed5d16 refactor(handlers): remove per-user scan settings handlers
Clean up auth.go after migrating to system-wide settings:
- Remove UpdateScanSettings handler (moved to system_settings.go)
- Remove GetScanSettings handler (moved to system_settings.go)
- Remove UpdateScanSettingsRequest type (now in system_settings.go)

These handlers are now in SystemSettingsHandler with system-wide scope
instead of per-user functionality.
2026-02-09 20:10:18 -05:00
john-okeefe 3266093248 feat(handlers): add system-wide scan settings handler
Create new SystemSettingsHandler for managing system-wide scan settings:
- GetScanSettings: retrieve scan frequency and auto-scan status
- UpdateScanSettings: update scan settings (15-1440 minutes range)
- Admin-only access (no user-specific data)
- Key-value based storage instead of per-user settings

Replaces per-user scan settings with centralized system configuration.
This handler is used by /api/libraries/scan-settings endpoints.
2026-02-09 20:10:06 -05:00
john-okeefe b506046be0 chore(db): regenerate database code from sqlc
Auto-generated changes from running 'sqlc generate' after query updates:
- models.go: updated with SystemSettings struct, removed scan fields from Users
- querier.go: updated interface with new system settings methods
- queries.sql.go: regenerated with new query methods

Generated via: cd internal/database && sqlc generate
2026-02-09 20:09:58 -05:00
john-okeefe 363e747cb3 feat(db): add system settings queries and enhance user queries
System Settings Migration:
- Add GetSystemSetting query for single setting retrieval
- Add UpdateSystemSetting query for updating settings
- Add GetAllSystemSettings query for all settings
- Remove UpdateScanSettings and GetScanSettings (per-user queries)

User Query Enhancement:
- Add max_devices field to GetUser query
- Add device_count computed field to GetUser query
- Add max_devices field to ListUsers query
- Add device_count computed field to ListUsers query

These changes support:
1. System-wide scan settings instead of per-user settings
2. Users can now see their device count and limits
3. Admins can monitor device usage across all users
2026-02-09 20:09:48 -05:00
john-okeefe e360ec2c30 feat(db): add system_settings table and remove per-user scan settings
- Add system_settings table with id, setting_key, setting_value, description, updated_at
- Insert default settings: scan_frequency_minutes=60, auto_scan_enabled=true
- Remove scan_frequency_minutes and auto_scan_enabled from users table
- Migrate from per-user scan settings to system-wide settings

This change enables centralized scan configuration for all libraries
while removing individual user scan preferences.

Database schema changes require container recreation:
  podman compose down -v
  podman compose up -d
2026-02-09 20:09:29 -05:00
john-okeefe b1a7d0a581 refactor: finalize media cleanup and prepare scan settings plan
- Complete media scanner cleanup (ebook → media terminology)
- Update remaining comments for consistency
- Add comprehensive scan settings migration plan
- Comment updates in book_matching.go and main.go
- Remove COMPLETE_MEDIA_CLEANUP_PLAN.md (completed)
- Add SCAN_SETTINGS_MIGRATION_PLAN.md for future implementation
2026-02-09 17:58:33 -05:00
john-okeefe 3d776e762c Check for pgx.ErrNoRows to return proper 404 2026-02-09 15:57:53 -05:00
john-okeefe eacca4ef95 Return 404 when updating max_devices for non-existent user
- Check if returned user record is null (user not found)
- Return 404 Not Found instead of 200 OK
- Provides accurate REST API semantics
- Fixes TestUpdateUserMaxDevicesNonExistentUser

Related: Database query change commit
2026-02-09 15:53:14 -05:00