diff --git a/cmd/server/tests/kobo_test.go b/cmd/server/tests/kobo_test.go index de3d048..106e020 100644 --- a/cmd/server/tests/kobo_test.go +++ b/cmd/server/tests/kobo_test.go @@ -10,15 +10,6 @@ import ( "net/http/httptest" "testing" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" - "bookhoard/internal/config" - "bookhoard/internal/database" - "bookhoard/internal/middleware" - "bookhoard/internal/router" - "bookhoard/internal/services" - "bookhoard/internal/sync" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -39,12 +30,12 @@ func TestKoboInitialization(t *testing.T) { t.Run("successful initialization", func(t *testing.T) { // Create Kobo device using TestDeviceSetup for proper authentication - deviceSetup := setupTestDevice(t, ts, db) + deviceSetup := setupDeviceTest(t) koboDevice := deviceSetup.CreateDevice(t, "Test Kobo", "kobo", "kobo-clara-test") req, _ := http.NewRequest("GET", ts.URL+"/api/sync/kobo/v1/initialization", nil) req.Header.Set("Authorization", "Bearer "+koboDevice.AuthToken) - req.Header.Set("x-kobo-device", fmt.Sprintf(`{"DeviceId":"%s","Model":"Kobo Clara","SerialNumber":"%s"}`, + req.Header.Set("x-kobo-device", fmt.Sprintf(`{"DeviceId":"%s","Model":"Kobo Clara","SerialNumber":"%s"}`, koboDevice.ID.String(), koboDevice.Identifier)) client := &http.Client{} @@ -52,14 +43,6 @@ func TestKoboInitialization(t *testing.T) { require.NoError(t, err) defer resp.Body.Close() - assert.Equal(t, http.StatusOK, resp.StatusCode) - }) - - client := &http.Client{} - resp, err := client.Do(req) - require.NoError(t, err) - defer resp.Body.Close() - assert.Equal(t, http.StatusOK, resp.StatusCode) }) } @@ -75,9 +58,16 @@ func TestKoboLibrarySync(t *testing.T) { token := loginTestUser(t, ts, db) _ = createTestMediaItemID(t, ts, token) + log.Printf("[DEBUG] Kobo test setup: creating device and media") + t.Run("successful library sync", func(t *testing.T) { - req, _ := http.NewRequest("GET", ts.URL+"/api/sync/kobo/test-token/v1/initialization", nil) - req.Header.Set("Authorization", "Bearer "+token) + deviceSetup := setupDeviceTest(t) + koboDevice := deviceSetup.CreateDevice(t, "Test Kobo", "kobo", "kobo-clara-test") + + req, _ := http.NewRequest("GET", ts.URL+"/api/sync/kobo/v1/initialization", nil) + req.Header.Set("Authorization", "Bearer "+koboDevice.AuthToken) + req.Header.Set("x-kobo-device", fmt.Sprintf(`{"DeviceId":"%s","Model":"Kobo Clara","SerialNumber":"%s"}`, + koboDevice.ID.String(), koboDevice.Identifier)) client := &http.Client{} resp, err := client.Do(req) @@ -99,7 +89,12 @@ func TestKoboMarkupSync(t *testing.T) { token := loginTestUser(t, ts, db) mediaItemID := createTestMediaItemID(t, ts, token) + log.Printf("[DEBUG] Kobo test setup: creating device and media") + t.Run("successful markup sync with annotations and bookmarks", func(t *testing.T) { + deviceSetup := setupDeviceTest(t) + koboDevice := deviceSetup.CreateDevice(t, "Test Kobo", "kobo", "kobo-clara-markup") + reqBody := map[string]interface{}{ "ReadingSync": []map[string]interface{}{ { @@ -121,16 +116,25 @@ func TestKoboMarkupSync(t *testing.T) { { "BookmarkId": "bookmark-2", "ContentId": mediaItemID, - "BookmarkText": "This is my note abouts book", + "BookmarkText": "This is my note about book", "BookmarkType": "bookmark", }, + { + "BookmarkId": "epubcfi(/6/4[chap1]!/4/2/1:0)", + "ContentId": mediaItemID, + "BookmarkType": "last-read-place", + "Hidden": true, + "Chapter": 1, + }, }, } body, _ := json.Marshal(reqBody) req, _ := http.NewRequest("POST", ts.URL+"/api/sync/kobo/markup", bytes.NewReader(body)) req.Header.Set("Content-Type", "application/json") - req.Header.Set("x-kobo-device", `{"DeviceId":"kobo-clara-test","Model":"Kobo Clara","SerialNumber":"N123456789"}`) + req.Header.Set("Authorization", "Bearer "+koboDevice.AuthToken) + req.Header.Set("x-kobo-device", fmt.Sprintf(`{"DeviceId":"%s","Model":"Kobo Clara","SerialNumber":"%s"}`, + koboDevice.ID.String(), koboDevice.Identifier)) client := &http.Client{} resp, err := client.Do(req) @@ -155,23 +159,38 @@ func TestKoboBookmarkSync(t *testing.T) { token := loginTestUser(t, ts, db) mediaItemID := createTestMediaItemID(t, ts, token) - t.Run("successful bookmark sync", func(t *testing.T) { + log.Printf("[DEBUG] Kobo test setup: creating device and media") + + t.Run("successful bookmark sync with last-read-place", func(t *testing.T) { + deviceSetup := setupDeviceTest(t) + koboDevice := deviceSetup.CreateDevice(t, "Test Kobo", "kobo", "kobo-clara-bookmark") + reqBody := map[string]interface{}{ "BookmarkSync": []map[string]interface{}{ { "BookmarkId": "bookmark-3", "ContentId": mediaItemID, - "BookmarkText": "Important note abouts book", + "BookmarkText": "Important note about book", "BookmarkType": "bookmark", "DateCreated": "2026-01-30T19:55:00Z", }, + { + "BookmarkId": "epubcfi(/6/4[chap1]!/4/2/1:156)", + "ContentId": mediaItemID, + "BookmarkType": "last-read-place", + "Hidden": true, + "Chapter": 1, + "DateCreated": "2026-01-30T19:55:00Z", + }, }, } body, _ := json.Marshal(reqBody) req, _ := http.NewRequest("POST", ts.URL+"/api/sync/kobo/bookmark", bytes.NewReader(body)) req.Header.Set("Content-Type", "application/json") - req.Header.Set("x-kobo-device", `{"DeviceId":"kobo-clara-test","Model":"Kobo Clara","SerialNumber":"N123456789"}`) + req.Header.Set("Authorization", "Bearer "+koboDevice.AuthToken) + req.Header.Set("x-kobo-device", fmt.Sprintf(`{"DeviceId":"%s","Model":"Kobo Clara","SerialNumber":"%s"}`, + koboDevice.ID.String(), koboDevice.Identifier)) client := &http.Client{} resp, err := client.Do(req) @@ -196,7 +215,12 @@ func TestKoboAnalyticsGettests(t *testing.T) { token := loginTestUser(t, ts, db) mediaItemID := createTestMediaItemID(t, ts, token) + log.Printf("[DEBUG] Kobo test setup: creating device and media") + t.Run("successful analytics tests", func(t *testing.T) { + deviceSetup := setupDeviceTest(t) + koboDevice := deviceSetup.CreateDevice(t, "Test Kobo", "kobo", "kobo-clara-analytics") + reqBody := map[string]interface{}{ "meta": map[string]string{ "name": "Kobo Analytics Tests", @@ -210,7 +234,9 @@ func TestKoboAnalyticsGettests(t *testing.T) { body, _ := json.Marshal(reqBody) req, _ := http.NewRequest("POST", ts.URL+"/api/sync/kobo/v1/analytics/gettests", bytes.NewReader(body)) req.Header.Set("Content-Type", "application/json") - req.Header.Set("x-kobo-device", `{"DeviceId":"kobo-clara-test","Model":"Kobo Clara","SerialNumber":"N123456789"}`) + req.Header.Set("Authorization", "Bearer "+koboDevice.AuthToken) + req.Header.Set("x-kobo-device", fmt.Sprintf(`{"DeviceId":"%s","Model":"Kobo Clara","SerialNumber":"%s"}`, + koboDevice.ID.String(), koboDevice.Identifier)) client := &http.Client{} resp, err := client.Do(req)