Files
pansy/internal/service/ops_test.go
T
steveandClaude Opus 4.8 f4c5db587f Season view: filter the editor to a year (#54)
"Change the garlic bed to cucumbers this year" has a year in it, and pansy had
no notion of one — the editor showed whatever is currently planted, full stop.

The data has always supported seasons: plantings carry planted_at and
removed_at, and "clear bed" soft-removes rather than deleting. A season is a
date range over data that already exists. No schema change, and specifically no
seasons table — it would duplicate what the dates already say and create a
second source of truth about when something was in the ground.

?year=YYYY on /full returns every plop whose [planted_at, removed_at] interval
overlapped that calendar year, so garlic planted in October and pulled the
following July appears in BOTH years, which is what actually happened. Undated
plantings appear in every year: everything that predates this feature has a null
planted_at, and a rule that excluded them would empty every existing garden the
moment a year was selected. Without the param /full behaves exactly as before —
the existing tests pass unchanged, which was the point of doing it this way.

Widening to past plops means widening the referenced-plant lookup with it.
A plant pulled last July isn't active, but its plops still have to render with
the right icon and colour, so ListReferencedPlants takes an includeRemoved flag
that tracks the same switch.

A past season is READ-ONLY, gated at the single canEdit the palette, inspector,
nudging, placement and drag handles all key off. Editing the past by accident is
the failure mode this feature introduces, so the state is stated in a banner
rather than implied by a dropdown you set a while ago, with the way back to the
live garden next to it.

The season is a separate query under its own key. The optimistic mutations all
patch gardenFullKey(gardenId); folding a year into that key would let them write
into whichever season happened to be on screen. Read-only views never need that
machinery, and keeping them out of it means they can't accidentally join it.

The year selector offers only years the garden holds data for, plus the current
one. A free numeric field invites a typo, and a typo'd year produces a
confidently empty garden that reads as data loss rather than a mistake — the
server bounds the year for the same reason.

Closes #54

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01H3zbym8Doka2d7D48maSgZ
2026-07-21 01:40:26 -04:00

277 lines
9.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package service
import (
"context"
"errors"
"testing"
"gitea.stevedudenhoeffer.com/steve/pansy/internal/domain"
)
func TestNamedRegion(t *testing.T) {
o := &domain.GardenObject{WidthCM: 200, HeightCM: 100} // hw=100, hh=50
cases := []struct {
name string
minX, minY, maxX, maxY float64
}{
{"all", -100, -50, 100, 50},
{"north", -100, -50, 100, 0},
{"top half", -100, -50, 100, 0},
{"south", -100, 0, 100, 50},
{"bottom", -100, 0, 100, 50},
{"east", 0, -50, 100, 50},
{"right half", 0, -50, 100, 50},
{"west", -100, -50, 0, 50},
{"nw", -100, -50, 0, 0},
{"NE corner", 0, -50, 100, 0},
{"northeast", 0, -50, 100, 0},
{"sw", -100, 0, 0, 50},
{"se corner", 0, 0, 100, 50},
}
for _, c := range cases {
r, err := NamedRegion(o, c.name)
if err != nil {
t.Errorf("%q: unexpected error %v", c.name, err)
continue
}
if r.MinX != c.minX || r.MinY != c.minY || r.MaxX != c.maxX || r.MaxY != c.maxY {
t.Errorf("%q = [%v,%v,%v,%v], want [%v,%v,%v,%v]", c.name, r.MinX, r.MinY, r.MaxX, r.MaxY, c.minX, c.minY, c.maxX, c.maxY)
}
}
if _, err := NamedRegion(o, "middle-ish"); !errors.Is(err, domain.ErrInvalidInput) {
t.Errorf("unknown region err = %v, want ErrInvalidInput", err)
}
if _, err := NamedRegion(o, ""); !errors.Is(err, domain.ErrInvalidInput) {
t.Errorf("empty region err = %v, want ErrInvalidInput", err)
}
if _, err := NamedRegion(nil, "all"); !errors.Is(err, domain.ErrInvalidInput) {
t.Errorf("nil object err = %v, want ErrInvalidInput", err)
}
}
func TestFillRegionCappedForHugeArea(t *testing.T) {
ctx := context.Background()
s := newTestService(t, openConfig())
owner := seedUser(t, s, "[email protected]")
g, _ := s.CreateGarden(ctx, owner, GardenInput{Name: "Huge", WidthCM: 8000, HeightCM: 8000})
bed := seedFillBed(t, s, owner, g.ID, 6000, 6000) // ~46k lattice points at radius 15 → over the cap
plant := seedOwnPlant(t, s, owner, 10)
region, _ := NamedRegion(bed, "all")
if _, err := s.FillRegion(ctx, owner, bed.ID, region, plant.ID, nil); !errors.Is(err, domain.ErrInvalidInput) {
t.Errorf("oversized fill err = %v, want ErrInvalidInput (over maxFillPlops)", err)
}
}
func TestDefaultPlopRadius(t *testing.T) {
if got := defaultPlopRadius(4); got != 15 { // 1.5*4=6 → floored to 15
t.Errorf("radius(4) = %v, want 15", got)
}
if got := defaultPlopRadius(20); got != 30 { // 1.5*20
t.Errorf("radius(20) = %v, want 30", got)
}
}
// seedFillBed makes a plantable bed of the given size centered in a big garden.
func seedFillBed(t *testing.T, s *Service, owner, gardenID int64, w, h float64) *domain.GardenObject {
t.Helper()
o, err := s.CreateObject(context.Background(), owner, gardenID, ObjectInput{
Kind: domain.KindBed, XCM: 1000, YCM: 1000, WidthCM: w, HeightCM: h,
})
if err != nil {
t.Fatalf("seed bed %vx%v: %v", w, h, err)
}
return o
}
func TestFillRegionDeterministicPacking(t *testing.T) {
ctx := context.Background()
s := newTestService(t, openConfig())
owner := seedUser(t, s, "[email protected]")
g, err := s.CreateGarden(ctx, owner, GardenInput{Name: "Big", WidthCM: 2000, HeightCM: 2000})
if err != nil {
t.Fatalf("garden: %v", err)
}
bed := seedFillBed(t, s, owner, g.ID, 60, 60) // hw=hh=30
plant := seedOwnPlant(t, s, owner, 10) // radius = max(15,15) = 15
region, _ := NamedRegion(bed, "all")
created, err := s.FillRegion(ctx, owner, bed.ID, region, plant.ID, nil)
if err != nil {
t.Fatalf("FillRegion: %v", err)
}
// Hex lattice on [-30,30]² at pitch 30, rows ~26 apart → 4 plops (2 rows × 2).
if len(created) != 4 {
t.Fatalf("filled %d plops, want 4 (60×60 bed, radius 15)", len(created))
}
for _, p := range created {
if p.RadiusCM != 15 || p.PlantedAt == nil || p.DerivedCount < 1 {
t.Errorf("unexpected created plop: %+v", p)
}
if p.XCM < -30 || p.XCM > 30 || p.YCM < -30 || p.YCM > 30 {
t.Errorf("plop center out of bed bounds: %+v", p)
}
}
// Re-filling the same region skips everything (each candidate sits exactly on
// an existing plop → entirely inside it).
again, err := s.FillRegion(ctx, owner, bed.ID, region, plant.ID, nil)
if err != nil {
t.Fatalf("second FillRegion: %v", err)
}
if len(again) != 0 {
t.Errorf("re-fill created %d plops, want 0 (all covered)", len(again))
}
}
func TestFillRegionRotatedBedUsesLocalFrame(t *testing.T) {
ctx := context.Background()
s := newTestService(t, openConfig())
owner := seedUser(t, s, "[email protected]")
g, _ := s.CreateGarden(ctx, owner, GardenInput{Name: "Big", WidthCM: 2000, HeightCM: 2000})
bed := seedFillBed(t, s, owner, g.ID, 400, 400)
// Rotate the bed 45°; the fill must still target the LOCAL NE corner.
rot := 45.0
bed, _ = s.UpdateObject(ctx, owner, bed.ID, ObjectPatch{RotationDeg: &rot}, bed.Version)
plant := seedOwnPlant(t, s, owner, 20)
region, _ := NamedRegion(bed, "ne")
created, err := s.FillRegion(ctx, owner, bed.ID, region, plant.ID, nil)
if err != nil {
t.Fatalf("FillRegion: %v", err)
}
if len(created) == 0 {
t.Fatal("expected some plops in the NE corner")
}
for _, p := range created {
// NE corner in local coords: x ≥ 0 (east), y ≤ 0 (north).
if p.XCM < 0 || p.YCM > 0 {
t.Errorf("plop not in local NE corner: %+v", p)
}
}
}
func TestClearObject(t *testing.T) {
ctx := context.Background()
s := newTestService(t, openConfig())
owner := seedUser(t, s, "[email protected]")
g := seedGarden(t, s, owner)
bed := seedBed(t, s, owner, g.ID)
plant := seedOwnPlant(t, s, owner, 10)
region, _ := NamedRegion(bed, "all")
if _, err := s.FillRegion(ctx, owner, bed.ID, region, plant.ID, nil); err != nil {
t.Fatalf("fill: %v", err)
}
n, err := s.ClearObject(ctx, owner, bed.ID)
if err != nil {
t.Fatalf("ClearObject: %v", err)
}
if n < 1 {
t.Fatalf("cleared %d, want ≥ 1", n)
}
full, _ := s.GardenFull(ctx, owner, g.ID, nil)
if len(full.Plantings) != 0 {
t.Errorf("plantings after clear = %d, want 0", len(full.Plantings))
}
// Clearing an already-empty object clears 0.
if again, _ := s.ClearObject(ctx, owner, bed.ID); again != 0 {
t.Errorf("second clear = %d, want 0", again)
}
}
func TestOpsForbiddenForViewer(t *testing.T) {
ctx := context.Background()
s := newTestService(t, openConfig())
owner := seedUser(t, s, "[email protected]")
viewer := seedUser(t, s, "[email protected]")
g := seedGarden(t, s, owner)
bed := seedBed(t, s, owner, g.ID)
plant := seedOwnPlant(t, s, owner, 10)
if _, err := s.AddShare(ctx, owner, g.ID, "[email protected]", domain.RoleViewer); err != nil {
t.Fatalf("share: %v", err)
}
region, _ := NamedRegion(bed, "all")
if _, err := s.FillRegion(ctx, viewer, bed.ID, region, plant.ID, nil); !errors.Is(err, domain.ErrForbidden) {
t.Errorf("viewer fill = %v, want ErrForbidden", err)
}
if _, err := s.ClearObject(ctx, viewer, bed.ID); !errors.Is(err, domain.ErrForbidden) {
t.Errorf("viewer clear = %v, want ErrForbidden", err)
}
// But a viewer can DescribeGarden (read).
if _, err := s.DescribeGarden(ctx, viewer, g.ID); err != nil {
t.Errorf("viewer describe = %v, want ok", err)
}
}
// TestFillScenario is the DESIGN scenario: garlic in the NE corner, basil in the
// NW, beans across the south — three distinct groups in the right places.
func TestFillScenario(t *testing.T) {
ctx := context.Background()
s := newTestService(t, openConfig())
owner := seedUser(t, s, "[email protected]")
g, _ := s.CreateGarden(ctx, owner, GardenInput{Name: "Plot", WidthCM: 2000, HeightCM: 2000})
bed := seedFillBed(t, s, owner, g.ID, 400, 400)
garlic := seedNamedPlant(t, s, owner, "Garlic", 15)
basil := seedNamedPlant(t, s, owner, "Basil", 25)
beans := seedNamedPlant(t, s, owner, "Beans", 10)
fill := func(name string, plantID int64) {
t.Helper()
region, err := NamedRegion(bed, name)
if err != nil {
t.Fatalf("region %q: %v", name, err)
}
if _, err := s.FillRegion(ctx, owner, bed.ID, region, plantID, nil); err != nil {
t.Fatalf("fill %q: %v", name, err)
}
}
fill("ne", garlic.ID)
fill("nw", basil.ID)
fill("south", beans.ID)
desc, err := s.DescribeGarden(ctx, owner, g.ID)
if err != nil {
t.Fatalf("DescribeGarden: %v", err)
}
if len(desc.Objects) != 1 {
t.Fatalf("objects = %d, want 1", len(desc.Objects))
}
// Tally plant → the set of rough locations it appears in.
locs := map[string]map[string]bool{}
for _, p := range desc.Objects[0].Plantings {
if locs[p.Plant] == nil {
locs[p.Plant] = map[string]bool{}
}
locs[p.Plant][p.Location] = true
}
if len(locs["Garlic"]) == 0 || !locs["Garlic"]["NE corner"] {
t.Errorf("garlic locations = %v, want NE corner", locs["Garlic"])
}
if !locs["Basil"]["NW corner"] {
t.Errorf("basil locations = %v, want NW corner", locs["Basil"])
}
// Beans fill the south half → their plops read as "south" (and possibly the
// SE/SW corners at the edges), never north.
for loc := range locs["Beans"] {
if loc == "north" || loc == "NE corner" || loc == "NW corner" || loc == "center" {
t.Errorf("beans appeared in %q, want only southern locations", loc)
}
}
if len(locs["Beans"]) == 0 {
t.Error("beans produced no plops")
}
}
// seedNamedPlant creates a custom plant with a specific name + spacing.
func seedNamedPlant(t *testing.T, s *Service, owner int64, name string, spacingCM float64) *domain.Plant {
t.Helper()
p, err := s.CreatePlant(context.Background(), owner, PlantInput{
Name: name, Category: domain.CategoryVegetable, SpacingCM: spacingCM, Color: "#4a7c3f", Icon: "🌱",
})
if err != nil {
t.Fatalf("seed plant %s: %v", name, err)
}
return p
}