From c7b18e70ea87c287240243aed867b3a8b51689cb Mon Sep 17 00:00:00 2001 From: Peter Stone Date: Sun, 20 Sep 2026 08:11:38 +0000 Subject: Make shopping items editable: tap to rename, move store/section, delete MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Tapping an item outside shopping mode now opens an edit sheet. User-added items can be renamed, moved between stores and sections, and deleted. PlanToEat items show why they cannot be edited — the client is read-only, so presenting fields that silently would not save is worse than saying so — and offer removal, which marks them checked, the same mechanism shopping mode already uses. This needed a store registry first. Stores previously existed only because some item referenced them by name, with Trello's list names quietly acting as the registry; removing Trello as a source made the gap visible. Without a registry you cannot move an item into an empty store, and moving the last item out of a store makes it vanish mid-edit. Migration 034 adds shopping_stores and shopping_sections, seeded from the stores currently in use, and gives items a section column — sections previously existed only as PlanToEat category strings, so a user item could not be filed under one at all. Registered stores and sections now appear in the list even when empty, which is the whole point of the registry. Two deliberate choices about destructive edges: * Renaming a store renames it and moves its items in one transaction. Items reference the store by name, so doing either alone orphans them. * Deleting a store or section keeps the items, moving them to Unassigned or clearing the section. Losing the list is far worse than re-filing an item. * An UPDATE matching no rows is an error, not a silent success, so a stale id cannot look like a saved edit. Section options are rebuilt per store in the edit sheet rather than shipped as one flat list, so a Costco item cannot be filed under a Safeway aisle. 18 new store tests, 11 handler tests. Co-Authored-By: Claude Opus 5 Claude-Session: https://claude.ai/code/session_018AwnALZL6bD4VBtATDVv3B --- internal/handlers/shopping_edit_test.go | 238 ++++++++++++++++++++++++++++++++ 1 file changed, 238 insertions(+) create mode 100644 internal/handlers/shopping_edit_test.go (limited to 'internal/handlers/shopping_edit_test.go') diff --git a/internal/handlers/shopping_edit_test.go b/internal/handlers/shopping_edit_test.go new file mode 100644 index 0000000..6d862d7 --- /dev/null +++ b/internal/handlers/shopping_edit_test.go @@ -0,0 +1,238 @@ +package handlers + +import ( + "context" + "net/http" + "net/http/httptest" + "net/url" + "strconv" + "strings" + "testing" +) + +func TestParseUserItemID(t *testing.T) { + cases := []struct { + in string + want int64 + valid bool + }{ + {"user-42", 42, true}, + {"user-1", 1, true}, + {"5f2a91bc", 0, false}, // a PlanToEat id + {"user-", 0, false}, // malformed + {"user-abc", 0, false}, // non-numeric + {"", 0, false}, + } + for _, c := range cases { + got, ok := parseUserItemID(c.in) + if ok != c.valid || got != c.want { + t.Errorf("parseUserItemID(%q) = (%d, %v), want (%d, %v)", c.in, got, ok, c.want, c.valid) + } + } +} + +func TestShoppingItemEdit_UserItemIsEditable(t *testing.T) { + h, cleanup := setupTestHandler(t) + defer cleanup() + + _ = h.store.SaveUserShoppingItemInSection("Milk", "Costco", "Dairy") + items, _ := h.store.GetUserShoppingItems() + id := items[0].ID + + req := withURLParam(httptest.NewRequest(http.MethodGet, "/shopping/item/user-1/edit", nil), + "id", "user-"+strconv.FormatInt(id, 10)) + rec := httptest.NewRecorder() + h.HandleShoppingItemEdit(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("status %d: %s", rec.Code, rec.Body.String()) + } +} + +// A PlanToEat item must not be presented as editable — the client is +// read-only, so a save would silently do nothing. +func TestShoppingItemEdit_PlanToEatItemIsReadOnly(t *testing.T) { + h, cleanup := setupTestHandler(t) + defer cleanup() + + req := withURLParam( + httptest.NewRequest(http.MethodGet, "/shopping/item/pte-1/edit?source=plantoeat&name=Kale&store=Safeway", nil), + "id", "pte-1") + rec := httptest.NewRecorder() + h.HandleShoppingItemEdit(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("status %d", rec.Code) + } +} + +func TestShoppingItemUpdate_RenamesAndMoves(t *testing.T) { + h, cleanup := setupTestHandler(t) + defer cleanup() + + _ = h.store.AddShoppingStore("Safeway") + _ = h.store.SaveUserShoppingItemInSection("Mlik", "Costco", "Dairy") + items, _ := h.store.GetUserShoppingItems() + id := items[0].ID + + form := url.Values{"name": {"Milk"}, "store": {"Safeway"}, "section": {""}} + req := withURLParam( + httptest.NewRequest(http.MethodPost, "/shopping/item/x", strings.NewReader(form.Encode())), + "id", "user-"+strconv.FormatInt(id, 10)) + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") + rec := httptest.NewRecorder() + + h.HandleShoppingItemUpdate(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("status %d: %s", rec.Code, rec.Body.String()) + } + got, err := h.store.GetUserShoppingItem(id) + if err != nil { + t.Fatalf("get: %v", err) + } + if got.Name != "Milk" || got.Store != "Safeway" || got.Section != "" { + t.Errorf("got %+v, want renamed and moved to Safeway with no section", got) + } +} + +// Editing is only offered for user items; a request naming anything else must +// be refused rather than quietly doing nothing. +func TestShoppingItemUpdate_RefusesNonUserItems(t *testing.T) { + h, cleanup := setupTestHandler(t) + defer cleanup() + + form := url.Values{"name": {"Kale"}, "store": {"Safeway"}} + req := withURLParam( + httptest.NewRequest(http.MethodPost, "/shopping/item/x", strings.NewReader(form.Encode())), + "id", "pte-123") + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") + rec := httptest.NewRecorder() + + h.HandleShoppingItemUpdate(rec, req) + + if rec.Code != http.StatusBadRequest { + t.Errorf("status %d, want 400", rec.Code) + } +} + +func TestShoppingItemDelete_RemovesUserItem(t *testing.T) { + h, cleanup := setupTestHandler(t) + defer cleanup() + + _ = h.store.SaveUserShoppingItem("Milk", "Costco") + items, _ := h.store.GetUserShoppingItems() + + req := withURLParam(httptest.NewRequest(http.MethodDelete, "/shopping/item/x", nil), + "id", "user-"+strconv.FormatInt(items[0].ID, 10)) + rec := httptest.NewRecorder() + h.HandleShoppingItemDelete(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("status %d: %s", rec.Code, rec.Body.String()) + } + remaining, _ := h.store.GetUserShoppingItems() + if len(remaining) != 0 { + t.Errorf("got %d items, want 0", len(remaining)) + } +} + +// A PlanToEat item cannot be deleted upstream, so removal marks it checked, +// which filters it out of the list. +func TestShoppingItemDelete_HidesPlanToEatItem(t *testing.T) { + h, cleanup := setupTestHandler(t) + defer cleanup() + + req := withURLParam( + httptest.NewRequest(http.MethodDelete, "/shopping/item/x?source=plantoeat", nil), + "id", "pte-99") + rec := httptest.NewRecorder() + h.HandleShoppingItemDelete(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("status %d: %s", rec.Code, rec.Body.String()) + } + checks, _ := h.store.GetShoppingItemChecks("plantoeat") + if !checks["pte-99"] { + t.Error("expected the item to be marked checked so it drops out of the list") + } +} + +// The registry's reason to exist: an empty store is still a destination. +func TestAggregateShoppingLists_IncludesEmptyRegisteredStores(t *testing.T) { + h, cleanup := setupTestHandler(t) + defer cleanup() + + if err := h.store.AddShoppingStore("Hardware Store"); err != nil { + t.Fatalf("add store: %v", err) + } + + var found bool + for _, st := range h.aggregateShoppingLists(context.Background()) { + if st.Name == "Hardware Store" { + found = true + } + } + if !found { + t.Error("a registered store with no items should still appear, or nothing can be moved into it") + } +} + +func TestShoppingStoreCreate(t *testing.T) { + h, cleanup := setupTestHandler(t) + defer cleanup() + + form := url.Values{"name": {"Hardware Store"}} + req := httptest.NewRequest(http.MethodPost, "/shopping/stores", strings.NewReader(form.Encode())) + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") + rec := httptest.NewRecorder() + + h.HandleShoppingStoreCreate(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("status %d: %s", rec.Code, rec.Body.String()) + } + names, _ := h.store.ShoppingStoreNames() + if len(names) != 1 || names[0] != "Hardware Store" { + t.Errorf("got %v", names) + } +} + +func TestShoppingSectionCreate(t *testing.T) { + h, cleanup := setupTestHandler(t) + defer cleanup() + + _ = h.store.AddShoppingStore("Costco") + + form := url.Values{"store": {"Costco"}, "name": {"Produce"}} + req := httptest.NewRequest(http.MethodPost, "/shopping/sections", strings.NewReader(form.Encode())) + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") + rec := httptest.NewRecorder() + + h.HandleShoppingSectionCreate(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("status %d: %s", rec.Code, rec.Body.String()) + } + stores, _ := h.store.GetShoppingStores() + if len(stores) != 1 || len(stores[0].Sections) != 1 || stores[0].Sections[0].Name != "Produce" { + t.Errorf("got %+v", stores) + } +} + +func TestShoppingSectionCreate_UnknownStore(t *testing.T) { + h, cleanup := setupTestHandler(t) + defer cleanup() + + form := url.Values{"store": {"Nowhere"}, "name": {"Produce"}} + req := httptest.NewRequest(http.MethodPost, "/shopping/sections", strings.NewReader(form.Encode())) + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") + rec := httptest.NewRecorder() + + h.HandleShoppingSectionCreate(rec, req) + + if rec.Code != http.StatusNotFound { + t.Errorf("status %d, want 404", rec.Code) + } +} + -- cgit v1.2.3