Closes the remaining integration-suite failures (handoff open bug 3),
line by line:
- Fixture family (the upsert equivalence): tags_filter_test
("Science Fiction" found 1 of 3; "Fantasy" 404'd on zero results),
media_item_isbn_test (normalization read back the first case's ISBN
everywhere; edge cases saw a nil ISBN; pagination collapsed 5
creates onto 1 row), search_unified_test (its 3 books were actually
1 row + two no-op creates) — all POSTed a constant file_path into
one library while CreateMediaItem upserts ON CONFLICT
(library_id, file_path). Every fixture now uses unique per-item
paths, the suite's universal convention elsewhere. TestTagsFilter
confirmed same-family: counts came right with no second bug.
- New TestMediaItemCreateDuplicatePath pins the honest-create
contract from da13898: 201 for the first create; 200 + duplicate
flag + the EXISTING row (never a 201 lie), second payload not
overwritten.
- library_browse_test: the admin subtest browsed the container's
/tmp and required it non-empty — env-coupled (empty /tmp → "was
[]"). Now browses a self-created t.TempDir with two subdirs and a
decoy file, asserting ONLY directories are listed.
- series_integration_test: three stale assertions re-anchored to the
shipped contracts, each with a ground-truth comment: /dashboard/
sections key identity on the collection NAME (BuildSections ID =
CollectionName; the web/app reorder + hidden prefs persist those
strings) — the kebab query_type is the storage key and is not
exposed; /api/series/books is name-ONLY (the app client and the
web SSR page both send name only; library_id is ignored, never
validated); /api/series is optional-scope BY DESIGN (GetDistinct-
Series: narg IS NULL OR library_id = narg aggregates across
libraries when omitted). The former 400-on-missing-library_id
expectations asserted validation that never shipped.
- gofmt rider on three untouched-in-this-arc test files
(dashboard_integration, search, test_helpers).
Full suite with -parallel=1: 217 groups pass, zero failures.
112 lines
4.1 KiB
Go
112 lines
4.1 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestUnifiedSearch(t *testing.T) {
|
|
setup := setupDeviceTest(t)
|
|
defer setup.Server.Close()
|
|
|
|
libraryID := setup.CreateLibrary(t, "Test Search Library", "ebooks")
|
|
_ = setup.CreateDevice(t, "Test Search Device", "koreader", "search-test-123")
|
|
|
|
client := &http.Client{}
|
|
|
|
// Add folder to library (required before adding media items)
|
|
folderReq := map[string]interface{}{
|
|
"folder_path": "/app/uploads",
|
|
}
|
|
folderBody, _ := json.Marshal(folderReq)
|
|
folderHTTP, _ := http.NewRequest("POST", setup.Server.URL+"/api/libraries/"+libraryID+"/folders", bytes.NewBuffer(folderBody))
|
|
folderHTTP.Header.Set("Content-Type", "application/json")
|
|
folderHTTP.Header.Set("Authorization", "Bearer "+setup.UserToken)
|
|
folderResp, err := client.Do(folderHTTP)
|
|
require.NoError(t, err)
|
|
_ = folderResp.Body.Close()
|
|
require.Equal(t, http.StatusCreated, folderResp.StatusCode)
|
|
|
|
// Helper to create book with fields. Unique file_path per book: the
|
|
// create endpoint answers 200/duplicate for a repeated
|
|
// (library_id, file_path), and the old shared constant made the 2nd/3rd
|
|
// creates no-ops anyway.
|
|
createBook := func(title, author, genre string, year int, hasCover bool) {
|
|
slug := strings.ReplaceAll(strings.ToLower(title), " ", "-")
|
|
bookReq := map[string]interface{}{
|
|
"library_id": libraryID,
|
|
"title": title,
|
|
"author": author,
|
|
"genre": genre,
|
|
"copyright_year": year,
|
|
"file_path": "/tmp/" + slug + ".epub",
|
|
"file_size": 1024,
|
|
"mime_type": "application/epub+zip",
|
|
}
|
|
if hasCover {
|
|
bookReq["cover_image_path"] = "/tmp/cover.jpg"
|
|
}
|
|
body, _ := json.Marshal(bookReq)
|
|
req, _ := http.NewRequest("POST", setup.Server.URL+"/api/media-items", bytes.NewBuffer(body))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
req.Header.Set("Authorization", "Bearer "+setup.UserToken)
|
|
resp, err := client.Do(req)
|
|
require.NoError(t, err)
|
|
defer func(Body io.ReadCloser) {
|
|
_ = Body.Close()
|
|
}(resp.Body)
|
|
require.Equal(t, http.StatusCreated, resp.StatusCode)
|
|
}
|
|
|
|
// Create test books with various fields for filtering
|
|
createBook("Foundation and Empire", "asimov", "scifi", 1951, true)
|
|
createBook("The Martian", "weir", "scifi", 2010, true)
|
|
createBook("I, Robot", "asimov", "fiction", 1950, false)
|
|
|
|
t.Run("Fuzzy author filter", func(t *testing.T) {
|
|
req := httptest.NewRequest("GET", "/api/media-items/search?library_id="+libraryID+"&q=%22Foundation%20and%20Empire%22", nil)
|
|
req.Header.Set("Authorization", "Bearer "+setup.UserToken)
|
|
rec := httptest.NewRecorder()
|
|
setup.Server.Config.Handler.ServeHTTP(rec, req)
|
|
|
|
assert.Equal(t, http.StatusOK, rec.Code, "Should exact match quoted query")
|
|
})
|
|
|
|
t.Run("Combined search + filters", func(t *testing.T) {
|
|
req := httptest.NewRequest("GET", "/api/media-items/search?library_id="+libraryID+"&q=foundation&author_filter=asimov", nil)
|
|
req.Header.Set("Authorization", "Bearer "+setup.UserToken)
|
|
rec := httptest.NewRecorder()
|
|
setup.Server.Config.Handler.ServeHTTP(rec, req)
|
|
|
|
assert.Equal(t, http.StatusOK, rec.Code, "Should combine search and filters")
|
|
})
|
|
|
|
t.Run("Boolean filter (exact)", func(t *testing.T) {
|
|
req := httptest.NewRequest("GET", "/api/media-items/search?library_id="+libraryID+"&has_cover=true", nil)
|
|
req.Header.Set("Authorization", "Bearer "+setup.UserToken)
|
|
rec := httptest.NewRecorder()
|
|
setup.Server.Config.Handler.ServeHTTP(rec, req)
|
|
|
|
assert.Equal(t, http.StatusOK, rec.Code, "Should filter by has_cover")
|
|
})
|
|
|
|
t.Run("Missing library_id searches all libraries", func(t *testing.T) {
|
|
req := httptest.NewRequest("GET", "/api/media-items/search?q=zzzznonexistent", nil)
|
|
req.Header.Set("Authorization", "Bearer "+setup.UserToken)
|
|
rec := httptest.NewRecorder()
|
|
setup.Server.Config.Handler.ServeHTTP(rec, req)
|
|
|
|
// library_id is optional - searches all libraries when omitted
|
|
// Returns 404 when no results match
|
|
assert.Equal(t, http.StatusNotFound, rec.Code, "Should return 404 when no results match")
|
|
})
|
|
}
|