package http_test import ( "context" "encoding/json" "io" "log/slog" "net/http/httptest" "strings" "testing" "git.jokresner.de/johannes/meilisearch-showcase/internal/app" "git.jokresner.de/johannes/meilisearch-showcase/internal/domain/catalog" httpiface "git.jokresner.de/johannes/meilisearch-showcase/internal/interfaces/http" ) type stubIndex struct { movies map[catalog.MovieID]*catalog.Movie } func (s stubIndex) EnsureSchema(context.Context) error { return nil } func (s stubIndex) Index(_ context.Context, ms []*catalog.Movie) error { for _, m := range ms { s.movies[m.ID()] = m } return nil } func (s stubIndex) Get(_ context.Context, id catalog.MovieID) (*catalog.Movie, error) { m, ok := s.movies[id] if !ok { return nil, catalog.ErrNotFound } return m, nil } func (s stubIndex) Delete(context.Context, catalog.MovieID) error { return nil } func (s stubIndex) Search(context.Context, catalog.Query) (catalog.Results, error) { return catalog.Results{EstimatedTotal: 0}, nil } func newServer() *httptest.Server { svc := app.NewCatalogService(stubIndex{movies: map[catalog.MovieID]*catalog.Movie{}}) log := slog.New(slog.NewTextHandler(io.Discard, nil)) return httptest.NewServer(httpiface.NewHandler(svc, log).Routes()) } func TestImportSearchGetFlow(t *testing.T) { srv := newServer() defer srv.Close() body := `[{"id":"1","title":"Alien","genres":["Sci-Fi"],"year":1979,"rating":8.5}]` res, err := srv.Client().Post(srv.URL+"/movies", "application/json", strings.NewReader(body)) if err != nil || res.StatusCode != 202 { t.Fatalf("import: %v %v", res.StatusCode, err) } res, _ = srv.Client().Get(srv.URL + "/movies/1") if res.StatusCode != 200 { t.Fatalf("get: %d", res.StatusCode) } var m map[string]any json.NewDecoder(res.Body).Decode(&m) if m["title"] != "Alien" { t.Fatalf("payload: %v", m) } res, _ = srv.Client().Get(srv.URL + "/movies/404") if res.StatusCode != 404 { t.Fatalf("missing movie must 404, got %d", res.StatusCode) } res, _ = srv.Client().Post(srv.URL+"/movies", "application/json", strings.NewReader(`[{"id":"2","title":""}]`)) if res.StatusCode != 400 { t.Fatalf("invalid movie must 400, got %d", res.StatusCode) } res, _ = srv.Client().Get(srv.URL + "/search?q=alien&genre=Sci-Fi") if res.StatusCode != 200 { t.Fatalf("search: %d", res.StatusCode) } }