package journal_test import ( "io" "net/http" "net/http/httptest" "net/url" "sync/atomic" "testing" "time" "git.golem15.com/golem15/sm-journal-plugin/models" ) // TestSearchGateOff is D-12 / T-15-11. A published save with Typesense // configured records zero outbound HTTP because search_use_typesense // defaults false. Unpublished ShouldBeSearchable stays false even if the // in-memory gate is flipped on. func TestSearchGateOff(t *testing.T) { var hits atomic.Int64 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { hits.Add(1) w.WriteHeader(http.StatusOK) _, _ = io.WriteString(w, `{}`) })) t.Cleanup(srv.Close) u, err := url.Parse(srv.URL) if err != nil { t.Fatal(err) } env := newPostsEnv(t, "SUMMER_SEARCH__DRIVER=typesense", "SUMMER_SEARCH__TYPESENSE__API_KEY=test-only-typesense-key", "SUMMER_SEARCH__TYPESENSE__HOST="+u.Hostname(), "SUMMER_SEARCH__TYPESENSE__PORT="+u.Port(), "SUMMER_SEARCH__TYPESENSE__PROTOCOL="+u.Scheme, ) past := time.Now().UTC().Add(-time.Hour) post := models.Post{ Title: "Published searchable", Slug: "published-searchable", Content: "hello search gate", Published: true, PublishedAt: &past, } if err := env.gdb.Create(&post).Error; err != nil { t.Fatal(err) } if hits.Load() != 0 { t.Fatalf("Typesense HTTP hits = %d, want 0", hits.Load()) } if post.ShouldBeSearchable() { t.Fatal("ShouldBeSearchable true with default search_use_typesense") } draft := models.Post{Title: "Draft", Slug: "draft-search", Content: "x", Published: false} models.SetSearchGate(true) t.Cleanup(func() { models.SetSearchGate(false) }) if draft.ShouldBeSearchable() { t.Fatal("unpublished ShouldBeSearchable must be false even if the in-memory gate is on") } if hits.Load() != 0 { t.Fatalf("Typesense HTTP hits after in-memory flip = %d, want 0", hits.Load()) } }