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")) } }