Files
pansy/internal/api/plantings_test.go
T
steveandClaude Opus 4.8 8f736048b9
Build image / build-and-push (push) Successful in 5s
Address Gadfly review on #14: bounds trap, date order, count cap
- UpdatePlanting: only re-check the object-bounds when the position is actually
  being moved. A plop orphaned outside its object by a later resize stays
  editable/removable instead of becoming a row you can't fix or delete.
- finalizePlanting: reject removed_at before planted_at.
- derivedCount: guard Inf (not just NaN) and cap the result at maxExplicitCount
  (1e6) — the same ceiling a manual override honors — so a huge radius / tiny
  spacing can't overflow a 32-bit int or return an absurd value.
- Refresh stale docs that referenced #14 as not-yet-landed (FullGarden,
  ListActivePlantingsForGarden, ListReferencedPlants) and note the date-only
  layout beside timeLayout.

Deliberately did NOT add a plantable re-check to Update/Delete: existing plops
must stay editable/removable even if their object was later marked non-plantable
(same trap as the bounds case).

Tests: derived-count cap, removed-before-planted rejection, and edit/move of a
plop orphaned by an object shrink.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01JdQpdYYsTgtkJBxbcpAszi
2026-07-18 22:39:48 -04:00

100 lines
3.7 KiB
Go

package api
import (
"encoding/json"
"net/http"
"strconv"
"testing"
)
func plantingPath(id int64) string { return "/api/v1/plantings/" + strconv.FormatInt(id, 10) }
func objectPlantingsPath(id int64) string { return objectPath(id) + "/plantings" }
func TestPlantingCreatePatchFullDelete(t *testing.T) {
r := authEngine(t, localCfg())
cookie := registerAndCookie(t, r, "[email protected]")
gid := createGardenAPI(t, r, cookie, "Yard")
// A plantable bed centered in the garden.
w := doJSON(t, r, http.MethodPost, objectsPath(gid),
map[string]any{"kind": "bed", "xCm": 500, "yCm": 500, "widthCm": 200, "heightCm": 200}, cookie)
oid := int64(decodeMap(t, w.Body.Bytes())["id"].(float64))
// A seeded built-in plant to place.
w = doJSON(t, r, http.MethodGet, "/api/v1/plants", nil, cookie)
plantID, _ := firstBuiltinID(t, w.Body.Bytes())
// Place a plop → 201 with a derivedCount.
w = doJSON(t, r, http.MethodPost, objectPlantingsPath(oid),
map[string]any{"plantId": plantID, "xCm": 0, "yCm": 0, "radiusCm": 20}, cookie)
if w.Code != http.StatusCreated {
t.Fatalf("create planting: status %d, body %s", w.Code, w.Body.String())
}
pl := decodeMap(t, w.Body.Bytes())
pid := int64(pl["id"].(float64))
dc, ok := pl["derivedCount"].(float64)
if !ok || dc < 1 {
t.Errorf("response missing a positive derivedCount: %+v", pl)
}
// /full includes the plop and its referenced plant.
w = doJSON(t, r, http.MethodGet, fullPath(gid), nil, cookie)
var full struct {
Plantings []map[string]any `json:"plantings"`
Plants []map[string]any `json:"plants"`
}
if err := json.Unmarshal(w.Body.Bytes(), &full); err != nil {
t.Fatalf("decode full: %v", err)
}
if len(full.Plantings) != 1 || len(full.Plants) != 1 {
t.Fatalf("full = %d plantings / %d plants, want 1 / 1", len(full.Plantings), len(full.Plants))
}
// Soft-remove (clear-bed style) → it leaves /full.
w = doJSON(t, r, http.MethodPatch, plantingPath(pid), map[string]any{"removedAt": "2030-06-01", "version": 1}, cookie)
if w.Code != http.StatusOK {
t.Fatalf("soft-remove: status %d, body %s", w.Code, w.Body.String())
}
w = doJSON(t, r, http.MethodGet, fullPath(gid), nil, cookie)
full.Plantings = nil
json.Unmarshal(w.Body.Bytes(), &full)
if len(full.Plantings) != 0 {
t.Errorf("removed plop still in /full: %d", len(full.Plantings))
}
// Hard delete → 204.
if w := doJSON(t, r, http.MethodDelete, plantingPath(pid), nil, cookie); w.Code != http.StatusNoContent {
t.Errorf("delete planting = %d, want 204", w.Code)
}
}
func TestPlantingIntoNonPlantableIsRejected(t *testing.T) {
r := authEngine(t, localCfg())
cookie := registerAndCookie(t, r, "[email protected]")
gid := createGardenAPI(t, r, cookie, "Yard")
// A tree is not plantable.
w := doJSON(t, r, http.MethodPost, objectsPath(gid),
map[string]any{"kind": "tree", "xCm": 500, "yCm": 500, "widthCm": 100, "heightCm": 100}, cookie)
oid := int64(decodeMap(t, w.Body.Bytes())["id"].(float64))
w = doJSON(t, r, http.MethodGet, "/api/v1/plants", nil, cookie)
plantID, _ := firstBuiltinID(t, w.Body.Bytes())
w = doJSON(t, r, http.MethodPost, objectPlantingsPath(oid),
map[string]any{"plantId": plantID, "xCm": 0, "yCm": 0, "radiusCm": 10}, cookie)
if w.Code != http.StatusBadRequest {
t.Errorf("plant into tree = %d, want 400", w.Code)
}
}
func TestPlantingsRequireAuth(t *testing.T) {
r := authEngine(t, localCfg())
if w := doJSON(t, r, http.MethodPost, "/api/v1/objects/1/plantings", map[string]any{"plantId": 1}, nil); w.Code != http.StatusUnauthorized {
t.Errorf("unauthenticated create = %d, want 401", w.Code)
}
if w := doJSON(t, r, http.MethodDelete, "/api/v1/plantings/1", nil, nil); w.Code != http.StatusUnauthorized {
t.Errorf("unauthenticated delete = %d, want 401", w.Code)
}
}