Commit Graph
680 Commits
Author SHA1 Message Date
john-okeefe ba3d9e4f37 refactor(login): remove duplicate theme code from template
- Remove inline theme functions (lines 62-88)
  - applyTheme, loadTheme, changeTheme functions
- Add script tag for /static/theme.js
- Theme logic now uses shared theme.ts module
- Eliminates code duplication with web/src/theme.ts
- Part of TypeScript conversion plan Phase 6.1.1
2026-02-18 16:42:13 -05:00
john-okeefe 9d2bed92f2 build(typescript): update compiled search.js from TypeScript source
- Compiled from web/src/search.ts
- Added proper type annotations
- Fixed null checks for DOM elements
- Added escapeHtml for library name display
- Updated onclick to use window.selectLibraryAndBook
2026-02-18 16:42:05 -05:00
john-okeefe dfd9cbcde7 feat(typescript): add feature modules for template conversion
- Add search.ts - header search with keyboard navigation
  - Debounced search with 300ms delay
  - Arrow key navigation through results
  - Escape to close, Enter to select
  - Library type icons and highlighting

- Add collections.ts - collection and rule management
  - Rule CRUD operations (create, update, delete)
  - Rule testing functionality
  - Bulk collection operations

- Add bookshelf.ts - book display and navigation
  - Library selection state management
  - Book viewing interactions
  - Pagination logic

- Add linking.ts - book matching and manual linking
  - Search and match functionality
  - Manual link modal
  - Bulk auto-link and suggestions

- Add api-explorer.ts - API testing interface
  - Request/response display
  - cURL command generation
  - History tracking

- Add admin.ts - admin dashboard actions
  - Library scan triggers
  - System statistics display
  - Profile management

- Add analytics.ts - analytics data loading
  - Chart.js integration
  - Daily reading minutes chart
  - Device usage and popular books display

- Add queue.ts - sync queue management
  - Process pending items
  - Clear failed/all items
  - Filter by status, type, device

- Add conflicts.ts - conflict resolution
  - Individual and bulk resolve operations
  - Winner device selection
  - Manual override inputs

- Add docs.ts - documentation search
  - Lunr.js search integration
  - Sidebar toggle for mobile
2026-02-18 16:40:51 -05:00
john-okeefe 60c5a093b5 feat(typescript): add core infrastructure modules
- Add centralized API type definitions (types/api.d.ts)
  - Interfaces for all API responses matching Go handler JSON
  - Snake_case field names matching actual API responses
  - Source file references in comments for verification

- Add API client module (api.ts)
  - Procedural get/post/put/delete functions
  - Automatic auth header injection
  - Exported to window for cross-module access

- Add DOM utilities (dom.ts)
  - escapeHtml for safe HTML rendering
  - querySelector wrappers with null checks
  - Element creation helpers

- Add event delegation helpers (events.ts)
  - Reusable event delegation pattern
  - Data attribute selectors for dynamic content

- Add localStorage wrapper (storage.ts)
  - Type-safe token management
  - Theme persistence helpers
2026-02-18 16:40:29 -05:00
john-okeefe 364de1ee93 docs(typescript): add comprehensive verification checklist for conversion plan 2026-02-18 16:38:13 -05:00
john-okeefe d709510a28 docs(typescript): update conversion plan with accurate line counts and template analysis 2026-02-18 16:38:12 -05:00
john-okeefe 0e386ca87f docs(dashboard): use existing test helpers and document automatic cleanup
Fixed integration tests to use existing helpers from test_helpers.go:

Changes:
- Replace getUserUUIDFromToken() with getTestUserID(t, db) helper 
- Replace parseUUID() with uuid.MustParse() 
- Add explicit comments about automatic cleanup via t.Cleanup() 

Test Helpers Used (all from test_helpers.go):
- setupTestServer(t) - creates test server with automatic cleanup
- loginTestUser(t, ts, db) - logs in admin user
- loginRegularUser(t, ts, db) - logs in regular user
- setupDeviceTest(t) - creates server + user + device + library
- getTestUserID(t, db) - gets/creates admin test user UUID
- uuid.MustParse() - parses UUID strings

Cleanup Pattern:
- Automatic via t.Cleanup() inside setupTestServer()
- Registered automatically when setupTestServer() is called
- No manual defer setup.Close() needed
- Runs even if test fails or panics
- Cleanup order: queue → connections → server → database

Dashboard-Specific Helper:
- updateDashboardPreferences() - only for dashboard testing
- Saves dashboard preferences for test scenarios

Benefits:
- Uses proven, existing helpers (no reinventing the wheel)
- Automatic cleanup prevents resource leaks
- Follows project testing patterns exactly
- Less custom code = fewer bugs
2026-02-17 22:31:38 -05:00
john-okeefe ac88031855 docs(dashboard): document test helpers and cleanup patterns
Added comprehensive documentation of available test helpers:

Available Helpers (from test_helpers.go):
- setupTestServer(t) - Creates test server with auto cleanup via t.Cleanup()
- loginTestUser(t, ts, db) - Logs in admin user, returns JWT token
- loginRegularUser(t, ts, db) - Logs in regular user, returns JWT token
- setupDeviceTest(t) - Creates server + user + device + library
- getTestUserID(t, db) - Gets/creates admin test user UUID
- getRegularUserID(t, db) - Gets/creates regular test user UUID

TestServerSetup Structure:
- Server *httptest.Server
- DB *database.Queries
- DBPool *pgxpool.Pool
- Config *config.Config
- ConnManager, QueueProcessor
- Auto cleanup via t.Cleanup()

Cleanup Pattern:
- Automatic cleanup registered in setupTestServer()
- Runs even if test fails or panics
- Order: queue processor → connection manager → HTTP server → database pool
- No manual defer setup.Close() needed

Updated Integration Tests:
- Added proper imports (database, uuid, pgtype)
- Documented available helpers
- Removed custom helpers that don't exist
- Uses existing project patterns

This ensures developers know what helpers are available and how to use them correctly.
2026-02-17 22:27:51 -05:00
john-okeefe c775ed0a8e docs(dashboard): add comprehensive unit and integration test phases
Phase 14: Unit Tests (2-3 hours)
- Service layer tests (dashboard_service_test.go)
  - filterHiddenSections() - tests no filters, one hidden, multiple hidden
  - reorderSections() - tests default order, custom order, partial order
- Handler helper tests (dashboard_test.go)
  - getSectionType() - smart vs collection sections
  - getSectionTitle() - all smart sections and collections
  - getSectionIcon() - icons for all sections
  - getSectionViewAllURL() - URLs for all sections
- Table-driven tests for multiple scenarios
- Uses testify/assert
- Skips database-dependent tests (use integration tests instead)

Phase 15: Integration Tests (2-3 hours)
- File: cmd/server/tests/dashboard_test.go
- Uses setupTestServer(t) helper from test_helpers.go
- Tests /api/dashboard/sections JSON endpoint:
  - Three-context testing (no auth, user, admin)
  - Missing library_id → 400
  - Invalid library_id → 400
  - With limit parameter
- Tests user preferences:
  - Hidden sections filtered correctly
  - Custom order applied correctly
- Tests SSR /dashboard page:
  - Returns HTML with dashboard elements
  - Requires auth
- Helper functions:
  - updateDashboardPreferences()
  - getUserUUIDFromToken()
  - parseUUID()

Testing Strategy:
- Unit tests alongside source files (project convention)
- Integration tests in cmd/server/tests/ (project convention)
- setupTestServer() helper creates full test environment
- Uses loginTestUser(), loginRegularUser(), setupDeviceTest()
- Follows existing patterns from auth_test.go, collections_bulk_test.go

Updated Timeline: 23-31 days total (added 4-6 hours for testing)

Benefits:
- Comprehensive test coverage before production
- Catches regressions in user preferences logic
- Validates API endpoint behavior across contexts
- Ensures SSR and JSON return consistent data
- Follows project testing conventions
2026-02-17 22:25:48 -05:00
john-okeefe d981fdf517 docs(dashboard): update Carousel plan with API endpoint and user preferences
Major architectural improvements:

1. Add generic /api/dashboard/sections JSON endpoint
   - Created internal/handlers/dashboard.go (new file)
   - Created internal/router/dashboard.go (new file)
   - Single source of truth for web UI, mobile apps, plugins
   - Follows existing handler/router pattern

2. Update DashboardService to apply user preferences
   - GetSectionItems() now accepts sectionOrder and hiddenSections
   - filterHiddenSections() removes user's hidden sections
   - reorderSections() applies user's custom order
   - Ensures consistent behavior across all clients

3. Separate concerns properly
   - API handlers in internal/handlers/dashboard.go
   - SSR routes remain in internal/router/frontend.go
   - Both use same DashboardService (single source of truth)

4. Reorganize implementation phases
   - Phase 1-3: Database, service, queries
   - Phase 4-6: Handler, router, frontend routes
   - Phase 7-9: Templates and settings
   - Phase 10-11: TypeScript modules
   - Phase 12-13: Documentation and testing

5. Add documentation
   - docs/developer/api/dashboard.md (API reference)
   - docs/user/dashboard.md (user guide)

6. Bruno tests already exist
   - bruno/dashboard/ has 5 comprehensive test files
   - Three-context testing (no user, user, admin)
   - No additional tests needed

Benefits:
- Uniform dashboard across web, mobile, plugins
- Single source of truth (no duplicate logic)
- User preferences respected by all clients
- Follows established project patterns
- Comprehensive test coverage

Timeline: Updated to reflect 13 phases (19-25 days total with TypeScript)
2026-02-17 22:20:54 -05:00
john-okeefe 3758532d31 docs(dashboard): update plan to support post-TypeScript conversion patterns
- Add prerequisites section (TypeScript conversion must be completed first)
- Update execution order: TypeScript (16-21 days) then dashboard (3-4 days)
- Revise Phase 7 TypeScript implementation:
  - Change file locations from web/src/ to web/ts/features/dashboard/
  - Replace inline onclick with data-action attributes
  - Use shared apiClient instead of raw fetch()
  - Add event delegation with on() utility
  - Import showToast from core instead of global
  - Add type definitions matching Go handlers
- Update templates to load new TypeScript module paths
- Add summary of key changes from original plan
- Ensure consistency with TypeScript Conversion Plan patterns
2026-02-17 21:58:37 -05:00
john-okeefe 4d756acff4 docs(typescript): add TypeScript conversion plan for inline JavaScript
- Create comprehensive plan to convert ~5,500 lines of inline JS to TypeScript
- Hybrid SSR + TypeScript CRUD approach (keeps existing JSON API)
- Event delegation pattern (no inline onclick handlers)
- Shared infrastructure: apiClient, toast, event utilities
- Procedural/imperative style (no OOP, classes, inheritance)
- 6 phases, 16-21 day timeline
- Preserves single API for all clients (web, mobile, plugins)
- No new backend routes needed
2026-02-17 21:58:33 -05:00
john-okeefe 17ce558522 docs(dashboard): refactor Carousel dashboard plan to align with guidelines
Major restructuring of the dashboard implementation plan to better match project guidelines:

- Change from handler types to template types (SectionData, BookCardData)
- Update from TypeScript to inline JavaScript matching existing pattern
- Change from separate handlers to inline routes in frontend.go
- Refactor service to return raw data (handler formats for templates)
- Add Settings template implementation
- Update TypeScript files to use IIFE pattern with window exports
- Change from .bru files to OpenCollection YAML .yml files
- Add Bruno tests note indicating tests already exist in bruno/dashboard/

This aligns the plan with actual project patterns and reduces architectural divergence.
2026-02-17 20:24:29 -05:00
john-okeefe 420ab930ae test(bruno): add token handling scripts to auth endpoints
Add after-response scripts to automatically save access and refresh tokens to Bruno environment variables after successful authentication. This eliminates manual token copying during testing.

Changes:
- Refresh Token.yml: Add script to save tokens from refresh response
- Register User.yml: Add script to save tokens from registration response
2026-02-17 20:24:24 -05:00
john-okeefe 105c427544 docs: update Bruno terminology to OpenCollection YAML format
Update all references from "Bruno DSL .bru files" to "Bruno OpenCollection YAML .yml files" to reflect the current Bruno format. This includes:

- PROJECT_GUIDELINES.md: Update API testing requirements
- README.md: Update command examples
- TEST_DATA.md: Update test data references
- docs/contributing/development.md: Update API testing section
- docs/developer/api-reference.md: Update Bruno testing documentation
- docs/developer/collections-api.md: Update test file references
- scripts/README.md: Update validation script documentation
- scripts/verify-guidelines.sh: Update file extension check (.bru → .yml)
- bruno/opencollection.yml: Rename collection from "Untitled Collection" to "Bookhoard"
2026-02-17 20:24:20 -05:00
john-okeefe f859b2714d refactor(bruno): reorganize file structure from bruno-yaml to flat bruno directory
- Move all files from bruno-yaml/* to bruno/*
- Maintains existing directory structure within categories
- Updates bruno/user/auth files with OAuth2 refresh token flow
- Updates bruno/user/profile files for user profile management
- Adds bruno/dashboard/ directory with dashboard API tests
- Preserves all existing test scenarios and OpenCollection YAML format
- No functional changes - file reorganization only
2026-02-17 20:22:21 -05:00
john-okeefe 96730d9475 docs: add Carousel dashboard implementation plan 2026-02-17 17:00:46 -05:00
john-okeefe fce16b53f7 fix(auth): return JSON for HTMX login failures instead of HTML 2026-02-16 21:08:15 -05:00
john-okeefe e25dc106fa chore: remove obsolete REFACTORING_PLAN.md 2026-02-16 16:51:26 -05:00
john-okeefe 81c72e706a chore(templates): regenerate template files after templ update
Regenerate all _templ.go files with latest templ generator.
Changes are minimal formatting updates (error message file paths).
2026-02-16 16:51:10 -05:00
john-okeefe 8a6ea39ed2 docs(auth): document 7-day session authentication with constants
- Add comprehensive authentication overview.md explaining:
  - 7-day session duration for JWT and refresh tokens
  - Constants-based implementation (no hardcoded values)
  - Complete authentication flow (register/login/refresh/logout)
  - Session expiration handling (HTML redirect vs JSON error)
  - Security features (HTTP-only cookies, token rotation)
  - Token storage recommendations
- Update login.md with 7-day expires_in field and cookie MaxAge
- Update register.md with 7-day session duration details
- Update refresh_token.md with expires_in: 604800

Documentation provides complete reference for authentication
endpoints with examples and security considerations.
2026-02-16 16:50:43 -05:00
john-okeefe 586f293e09 test(auth): add comprehensive 7-day session tests
- Add seven_day_session_test.go with comprehensive test coverage:
  - Test login returns 7-day session (expires_in: 604800)
  - Test cookie MaxAge is 7 days (604800 seconds)
  - Test refresh token returns 7-day access token
  - Test JWT token has 7-day expiration claim
  - Test 401 error handler redirects HTML requests
  - Test 401 error handler returns JSON for API requests
  - Test register/login do not set document.cookie
- Tests use getTestUserID() and setupTestServer() helpers
- Update security_test.go JWT expiration comment to reflect 7 days

Tests verify all aspects of the 7-day session implementation
including constants usage, cookie values, API responses, and
smart 401 error handling.
2026-02-16 16:50:31 -05:00
john-okeefe 53aad2701b feat(frontend): enhance 401 handling to clear tokens and redirect
- Update fetch interceptor to special-case 401 responses
- Clear invalid tokens from localStorage on 401 (token, refreshToken, user)
- Distinguish between page navigation and API calls:
  - Page navigation: throw error to prevent further processing
  - API calls: show toast error with session expired message
- Suppress network error toast for redirect errors
- Compile TypeScript to JavaScript

This ensures frontend properly handles expired sessions by clearing
stale credentials and showing appropriate error messages.
2026-02-16 16:50:19 -05:00
john-okeefe 7b07645ee2 feat(auth): show session expired message on login page
- Update Login template to accept sessionExpired boolean parameter
- Add conditional message box when session=expired query param present
- Update /login route handler to parse session query param
- Pass sessionExpired flag to Login template
- Regenerate login_templ.go with new signature

Displays friendly message: "Your session has expired. Please log in
again to continue." when users are redirected due to expired sessions.
2026-02-16 16:50:10 -05:00
john-okeefe 7952bc7f6a feat(router): add smart 401 error handler for HTML vs API requests
- Add strings import for Accept header parsing
- Add wantsHTML() helper function to detect HTML vs API requests
  - Checks Accept header for text/html
  - Checks HX-Request header for HTMX requests
  - Checks X-Requested-With for AJAX (should return JSON)
  - Defaults to JSON for API routes
- Update JWT middleware ErrorHandler to:
  - Redirect HTML requests to /login?session=expired
  - Return JSON error for API requests with session_expired message
- Enables browser navigation to redirect gracefully while API calls
  return proper error responses

This fixes the issue where protected routes returned JSON 401
for browser navigation instead of redirecting to login.
2026-02-16 16:49:54 -05:00
john-okeefe 2e1af8d20b feat(auth): extend session duration to 7 days using constants
- Add SessionDuration constant (7 days) and SessionDurationSec computed value
- Update JWT token expiration to use SessionDuration instead of 1 hour
- Update register/login cookie MaxAge to use SessionDurationSec (604800)
- Update register/login API response ExpiresIn to use SessionDurationSec
- Update refresh token endpoint ExpiresIn to use SessionDurationSec
- Remove redundant client-side document.cookie lines from login/register
- Add TODO comment for HTTPS cookie Secure flag

This provides Google-like persistent sessions with a single source of truth
for session duration, eliminating hardcoded values throughout the codebase.
2026-02-16 16:49:43 -05:00
john-okeefe c9ebc5b11a feat: add authorization header to device token regeneration
- Add Bearer token from localStorage to regenerate-token API request
- Update code formatting for consistency (double quotes, indentation)

This ensures the device token regeneration endpoint receives proper
authentication via the Authorization header.
2026-02-16 09:18:02 -05:00
john-okeefe a0e9a2b6e6 refactor: remove auth inherit and token handling from login request
- Remove 'auth: inherit' from POST request configuration
- Remove post-response script that set token environment variable
- Clean up documentation formatting

This simplifies the login request configuration as authentication
will now be handled via HTTP-only cookies instead of bearer tokens.
2026-02-16 09:17:56 -05:00
john-okeefe b5156bbe16 feat: add HTTP-only cookie for browser authentication
- Set HTTP-only cookie in login handler for SSR authentication
- Set HTTP-only cookie in registration handler
- Change default redirect from /bookshelf to /dashboard
- Cookie enables browser page navigation without JavaScript
2026-02-15 21:36:42 -05:00
john-okeefe 6b3ccdfc55 feat: add protected frontend SSR routes
- Add frontendProtected group for authenticated pages
- Add /dashboard, /collections, /progress, /devices, /conflicts, /analytics routes
- Add /admin, /admin/, /admin/profile, /admin/library routes
- Keep legacy /api/devices-page and /api/conflicts-page for backward compatibility
- All routes use JWT middleware for authentication
2026-02-15 21:36:36 -05:00
john-okeefe 1803ac2ee7 feat: add ScannerHandler to router Config
- Add ScannerHandler field to Config struct for frontend route access
- Move scannerHandler creation before registerFrontendRoutes call
- Enables /progress page to access scanner data
2026-02-15 21:36:30 -05:00
john-okeefe 2df2b2d026 refactor: remove bookshelf route, consolidate to dashboard
- Update header navigation to link to /dashboard instead of /bookshelf
- Update index page auto-redirect to use /dashboard
- Remove duplicate route, keeping full-featured dashboard with filters
2026-02-15 21:36:25 -05:00
john-okeefe b682f09fbc feat: add static file serving and theme safelist
- Serve static files from web/static directory
- Add theme class safelist to Tailwind config for dynamic theming support
- Regenerate CSS with updated configuration
2026-02-15 16:53:34 -05:00
john-okeefe b46bace1b6 test: rewrite system_settings tests to use real handlers and add regular user support 2026-02-15 00:29:29 -05:00
john-okeefe fd1194f830 test(sync): fix integration tests - use config for db, fix helper IDs, correct route paths 2026-02-15 00:29:20 -05:00
john-okeefe a9eb8aa9fd test(auth): remove invalid registration test case that fails mock validation 2026-02-15 00:29:01 -05:00
john-okeefe eeb6c69063 test(auth): remove redundant RefreshToken_TokenTampering test case 2026-02-15 00:28:52 -05:00
john-okeefe 6346e9bc27 test: add new handler test files for analytics, auth, kobo, library, progress, and sidecar 2026-02-14 21:38:08 -05:00
john-okeefe acd194c217 test: add device token validation and text utility tests 2026-02-14 21:38:00 -05:00
john-okeefe 4d15dba555 test(auth): fix refresh token invalid token test to expect BadRequest 2026-02-14 21:37:52 -05:00
john-okeefe 28310cc6b2 test(queue): add createTestQueueItem helper and improve queue test assertions 2026-02-14 21:37:45 -05:00
john-okeefe 557f057621 fix(db): cast status to varchar in sync queue update for proper enum comparison 2026-02-14 21:37:38 -05:00
john-okeefe d7ab22c399 fix(auth): add jti claim to JWT tokens for unique token identification 2026-02-14 21:37:29 -05:00
john-okeefe aaa367cb33 chore: add -short flag to test command in Dockerfile 2026-02-14 00:12:56 -05:00
john-okeefe 8e054bd149 fix: update test files for token handling and response parsing
- Update callers of createTestMediaItemID to not pass token
- Fix loginAdminUser to delete/recreate admin user for consistent state
- Fix TestListAllQueueItems_Admin to parse response as map with 'items' key
- Remove unused token variables from tests
- Update device_test.go with admin password hash constant
2026-02-14 00:12:28 -05:00
john-okeefe 962bab1df0 fix: improve test helpers with fresh tokens and cleanup
- createTestMediaItemID now gets fresh auth token to avoid stale tokens
- Use unique library names with timestamps to avoid conflicts
- Add t.Cleanup to delete libraries after tests
- Remove token parameter from function signature (not needed)
2026-02-14 00:12:22 -05:00
john-okeefe 02ff078adf fix: validate UUIDs in OPDS middleware before authentication
- Add UUID validation in device_auth middleware for OPDS routes
- Return 400 Bad Request for invalid device/book IDs instead of 401
- Remove redundant UUID validation from OPDS handlers (middleware handles it)
2026-02-14 00:12:15 -05:00
john-okeefe 030e8c87e3 fix: normalize negative offset to zero in media filter 2026-02-14 00:12:08 -05:00
john-okeefe b3934c2c27 chore: remove old phase1_example_test.go file
Remove the old phase1_example_test.go file that was renamed to
device_test_patterns_test.go. This file should have been removed
in the previous commit but was missed.
2026-02-13 21:50:52 -05:00
john-okeefe b44e4e4709 docs: remove Phase X placeholders from API documentation
Clean up API documentation files by removing Phase X references:

Remove 'API Explorer will be inserted here in Phase X' placeholders from:
- 70+ API endpoint documentation files
- Authentication endpoints (login, logout, register, refresh)
- User endpoints (profile, settings, password)
- Device endpoints (registration, sync, shelves)
- Library endpoints (CRUD, folders, visibility)
- Media endpoints (items, progress, highlights, notes)
- Admin endpoints (users, analytics)
- Sync endpoints (Kobo, KOReader)
- OPDS endpoints
- Scanner endpoints
- Queue endpoints

These placeholders were from planning documents and have no meaning
to API consumers. The documentation is now clean and ready for use.
2026-02-13 21:50:44 -05:00