package web
import (
"net/http"
"net/url"
"strconv"
"strings"
"testing"
"supermarket/internal/inventory"
)
func TestInventoryPrices(t *testing.T) {
s, h := newTestServer(t, nil)
form := url.Values{"name": {"Zendesk"}, "relationship": {"competitor"}, "mention_policy": {"name"},
"price_plan": {"Suite Team", ""}, "price_amount": {"$55", ""}, "price_per": {"per agent per month", ""},
"price_checked": {"2026-11-20", "2026-11-24"}, "price_source": {"https://zendesk.com/pricing", ""}}
if rec := postForm(h, "/market/save", form); rec.Code != http.StatusSeeOther {
t.Fatalf("save: %d %s", rec.Code, rec.Body.String())
}
es, _ := inventory.List(s.DB, "")
if len(es) != 1 || len(es[0].Prices) != 1 || es[0].Prices[0].Amount != "$55" {
t.Fatalf("saved: %+v", es)
}
body := do(h, http.MethodGet, "/market/"+strconv.FormatInt(es[0].ID, 10), nil, nil).Body.String()
if !strings.Contains(body, `name="price_amount" value="$55"`) || !strings.Contains(body, "Their prices") {
t.Errorf("edit page: %s", body)
}
form.Set("price_checked", "")
form["price_checked"] = []string{"", ""}
if rec := postForm(h, "/market/save", form); !strings.Contains(rec.Body.String(), "say when you checked the price $55") && !strings.Contains(rec.Body.String(), "Say when you checked the price $55") {
t.Errorf("a price without its date was saved: %d", rec.Code)
}
}
// The Market was the Inventory: its old addresses lead to the new ones,
// keeping the query and a form's POST.
func TestMarketMoved(t *testing.T) {
_, h := newTestServer(t, nil)
for old, want := range map[string]string{"/inventory": "/market", "/inventory/3?x=1": "/market/3?x=1", "/inventory/import": "/market/import"} {
rec := do(h, http.MethodGet, old, nil, nil)
if rec.Code != http.StatusPermanentRedirect || rec.Header().Get("Location") != want {
t.Errorf("%s: %d %s", old, rec.Code, rec.Header().Get("Location"))
}
}
if rec := postForm(h, "/inventory/save", url.Values{"name": {"X"}}); rec.Code != http.StatusPermanentRedirect || rec.Header().Get("Location") != "/market/save" {
t.Errorf("post: %d %s", rec.Code, rec.Header().Get("Location"))
}
}
func TestMarketImportFill(t *testing.T) {
s, h := newTestServer(t, nil)
list := "- url: https://rival.example\n relationship: competitor\n"
// Without AI: the box can't be ticked, and says why if it is.
body := do(h, http.MethodGet, "/market/import", nil, nil).Body.String()
if !strings.Contains(body, `name="fill" disabled`) || !strings.Contains(body, `Set up the AI`) {
t.Errorf("import page without AI:\n%s", body)
}
body = postForm(h, "/market/import", url.Values{"yaml": {list}, "fill": {"on"}}).Body.String()
if !strings.Contains(body, "set up the AI to fill entries in") || !strings.Contains(body, "rival.example") {
t.Errorf("fill without AI:\n%s", body)
}
// Websites only, without the box ticked: says to tick it.
body = postForm(h, "/market/import", url.Values{"yaml": {list}}).Body.String()
if !strings.Contains(body, "Some entries have only a website") {
t.Errorf("websites only, not filled:\n%s", body)
}
// The last list's progress, from the setting.
st := marketFillState{Running: false, Total: 2, Lines: []inventory.FillLine{
{Site: "https://rival.example", Name: "Rival", ID: 7, Outcome: inventory.FillAdded, Filled: []string{"X handle", "description"}},
{Site: "https://gone.example", Outcome: inventory.FillFailed, Note: "no such host"},
}}
if err := s.DB.PutJSON(marketFillKey, st); err != nil {
t.Fatal(err)
}
body = do(h, http.MethodGet, "/market/import", nil, nil).Body.String()
for _, want := range []string{"1 added, 0 updated, 0 skipped, 1 failed", `Rival`, "filled: X handle, description", "no such host"} {
if !strings.Contains(body, want) {
t.Errorf("progress lacks %q", want)
}
}
// One left running with nothing doing it was stopped with the app;
// the card stops polling.
st.Running = true
_ = s.DB.PutJSON(marketFillKey, st)
body = do(h, http.MethodGet, "/market/import/status", nil, nil).Body.String()
if !strings.Contains(body, "Stopped with the app after 2 of 2") || strings.Contains(body, "hx-trigger") {
t.Errorf("status:\n%s", body)
}
}