Copy a garden (deep duplicate) from the gardens list
Build image / build-and-push (push) Successful in 23s
Gadfly review (reusable) / review (pull_request) Successful in 6m30s
Adversarial Review (Gadfly) / review (pull_request) Successful in 6m30s

Adds POST /gardens/:id/copy: duplicates a garden the actor owns, along
with its objects and their currently-planted plops, into a new garden
owned by the actor. A blank name derives "<source> (copy)".

Deliberately not carried over:
  * public_token — the share link is a capability granted to the
    original; a copy must not silently inherit a live public URL
  * garden_shares — a copy is private to whoever made it
  * removed plantings — the copy is a fresh layout, not a history

Owner-only for now: a copy's plops keep pointing at the SOURCE's
plant_ids, so letting a viewer copy someone else's garden would hand
them plants outside their catalog and pin the original owner's plants
against deletion (plantings.plant_id is ON DELETE RESTRICT). Copying a
shared garden needs a plant-cloning policy first.

The whole copy runs in one transaction, so a failure part-way leaves no
half-populated garden. The object/planting list scans are factored into
queryObjects/queryPlantings so the same code serves a plain read and a
read inside that transaction.

UI: a Copy action on the owner's garden card opens a modal prefilled
with the derived name, then lands in the new garden.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
This commit is contained in:
2026-07-20 23:49:50 -04:00
co-authored by Claude Opus 4.8
parent e74fb308c1
commit 5dd35c3800
14 changed files with 632 additions and 29 deletions
+42
View File
@@ -156,6 +156,48 @@ func (s *Service) UpdateGarden(ctx context.Context, actorID, gardenID int64, in
return updated, err
}
// CopyGarden duplicates a garden into a new one owned by the actor, carrying
// over its dimensions, grid settings, objects and currently-planted plops. A
// blank name defaults to "<source> (copy)".
//
// Owner-only, deliberately: a copy's plops keep pointing at the SOURCE's
// plant_ids, and a custom plant is owned by one user. Letting a viewer copy
// someone else's garden would hand them a garden referencing plants that aren't
// in their catalog — and would block the original owner from ever deleting those
// plants (plantings.plant_id is ON DELETE RESTRICT). Copying a shared garden
// needs a plant-cloning policy first; until then the owner's own copy is the
// case that's unambiguously correct.
func (s *Service) CopyGarden(ctx context.Context, actorID, gardenID int64, name string) (*domain.Garden, error) {
src, err := s.requireGardenRole(ctx, actorID, gardenID, roleOwner)
if err != nil {
return nil, err
}
name = strings.TrimSpace(name)
if name == "" {
name = copyName(src.Name)
}
if len(name) > maxGardenNameLen {
return nil, domain.ErrInvalidInput
}
created, err := s.store.CopyGarden(ctx, gardenID, actorID, name)
if err != nil {
return nil, err
}
created.MyRole = roleOwner.String() // the copier owns the copy
return created, nil
}
// copyName derives the default name for a copy. A name already at the length cap
// is trimmed to make room for the suffix, on a rune boundary so the result stays
// valid UTF-8.
func copyName(src string) string {
const suffix = " (copy)"
if room := maxGardenNameLen - len(suffix); len(src) > room {
src = strings.TrimSpace(strings.ToValidUTF8(src[:room], ""))
}
return src + suffix
}
// DeleteGarden removes a garden; only the owner may.
func (s *Service) DeleteGarden(ctx context.Context, actorID, gardenID int64) error {
if _, err := s.requireGardenRole(ctx, actorID, gardenID, roleOwner); err != nil {
+281
View File
@@ -6,6 +6,7 @@ import (
"math"
"strings"
"testing"
"unicode/utf8"
"gitea.stevedudenhoeffer.com/steve/pansy/internal/domain"
)
@@ -249,3 +250,283 @@ func TestDeleteGarden(t *testing.T) {
t.Errorf("re-delete err = %v, want ErrNotFound", err)
}
}
func TestCopyGardenDuplicatesContents(t *testing.T) {
ctx := context.Background()
s := newTestService(t, openConfig())
owner := seedUser(t, s, "[email protected]")
src, err := s.CreateGarden(ctx, owner, GardenInput{
Name: "Home", WidthCM: 732, HeightCM: 732, UnitPref: domain.UnitImperial,
Notes: "the real one", GridSizeCM: 30, SnapToGrid: true,
})
if err != nil {
t.Fatalf("CreateGarden: %v", err)
}
bed := seedBed(t, s, owner, src.ID)
plant := seedOwnPlant(t, s, owner, 20)
plop, err := s.CreatePlanting(ctx, owner, bed.ID, PlantingInput{PlantID: plant.ID, XCM: 10, YCM: -20, RadiusCM: 15})
if err != nil {
t.Fatalf("CreatePlanting: %v", err)
}
dup, err := s.CopyGarden(ctx, owner, src.ID, "")
if err != nil {
t.Fatalf("CopyGarden: %v", err)
}
if dup.ID == src.ID {
t.Fatal("copy reused the source id")
}
if dup.Name != "Home (copy)" {
t.Errorf("name = %q, want 'Home (copy)'", dup.Name)
}
if dup.OwnerID != owner {
t.Errorf("owner = %d, want %d", dup.OwnerID, owner)
}
if dup.Version != 1 {
t.Errorf("version = %d, want a fresh 1", dup.Version)
}
if dup.WidthCM != src.WidthCM || dup.HeightCM != src.HeightCM ||
dup.UnitPref != src.UnitPref || dup.Notes != src.Notes ||
dup.GridSizeCM != src.GridSizeCM || dup.SnapToGrid != src.SnapToGrid {
t.Errorf("settings not carried over: %+v vs source %+v", dup, src)
}
full, err := s.GardenFull(ctx, owner, dup.ID)
if err != nil {
t.Fatalf("GardenFull(copy): %v", err)
}
if len(full.Objects) != 1 {
t.Fatalf("copy has %d objects, want 1", len(full.Objects))
}
got := full.Objects[0]
if got.ID == bed.ID {
t.Error("copied object reused the source object id")
}
if got.GardenID != dup.ID {
t.Errorf("copied object garden = %d, want the copy %d", got.GardenID, dup.ID)
}
if got.Kind != bed.Kind || got.XCM != bed.XCM || got.YCM != bed.YCM ||
got.WidthCM != bed.WidthCM || got.HeightCM != bed.HeightCM || got.Plantable != bed.Plantable {
t.Errorf("copied object differs: %+v vs source %+v", got, bed)
}
if len(full.Plantings) != 1 {
t.Fatalf("copy has %d plantings, want 1", len(full.Plantings))
}
gotPlop := full.Plantings[0]
if gotPlop.ID == plop.ID {
t.Error("copied planting reused the source planting id")
}
if gotPlop.ObjectID != got.ID {
t.Errorf("copied planting parent = %d, want the copied object %d", gotPlop.ObjectID, got.ID)
}
if gotPlop.PlantID != plop.PlantID || gotPlop.XCM != plop.XCM || gotPlop.YCM != plop.YCM ||
gotPlop.RadiusCM != plop.RadiusCM {
t.Errorf("copied planting differs: %+v vs source %+v", gotPlop, plop)
}
// The source is untouched by the copy.
srcFull, err := s.GardenFull(ctx, owner, src.ID)
if err != nil {
t.Fatalf("GardenFull(source): %v", err)
}
if len(srcFull.Objects) != 1 || len(srcFull.Plantings) != 1 {
t.Errorf("source changed: %d objects, %d plantings", len(srcFull.Objects), len(srcFull.Plantings))
}
}
func TestCopyGardenSkipsRemovedPlantings(t *testing.T) {
ctx := context.Background()
s := newTestService(t, openConfig())
owner := seedUser(t, s, "[email protected]")
src := seedGarden(t, s, owner)
bed := seedBed(t, s, owner, src.ID)
plant := seedOwnPlant(t, s, owner, 20)
kept, err := s.CreatePlanting(ctx, owner, bed.ID, PlantingInput{PlantID: plant.ID, RadiusCM: 10})
if err != nil {
t.Fatalf("CreatePlanting(kept): %v", err)
}
if _, err := s.CreatePlanting(ctx, owner, bed.ID, PlantingInput{PlantID: plant.ID, XCM: 50, RadiusCM: 10}); err != nil {
t.Fatalf("CreatePlanting(cleared): %v", err)
}
// Clear the bed, then re-plant one plop: the copy should carry the active plop
// only, not the soft-removed history.
if _, err := s.ClearObject(ctx, owner, bed.ID); err != nil {
t.Fatalf("ClearObject: %v", err)
}
replanted, err := s.CreatePlanting(ctx, owner, bed.ID, PlantingInput{PlantID: plant.ID, XCM: 25, RadiusCM: 10})
if err != nil {
t.Fatalf("CreatePlanting(replanted): %v", err)
}
_ = kept
dup, err := s.CopyGarden(ctx, owner, src.ID, "")
if err != nil {
t.Fatalf("CopyGarden: %v", err)
}
full, err := s.GardenFull(ctx, owner, dup.ID)
if err != nil {
t.Fatalf("GardenFull: %v", err)
}
if len(full.Plantings) != 1 {
t.Fatalf("copy has %d plantings, want only the 1 active one", len(full.Plantings))
}
if full.Plantings[0].XCM != replanted.XCM {
t.Errorf("copied plop x = %v, want the active %v", full.Plantings[0].XCM, replanted.XCM)
}
}
func TestCopyGardenNaming(t *testing.T) {
ctx := context.Background()
s := newTestService(t, openConfig())
owner := seedUser(t, s, "[email protected]")
g, err := s.CreateGarden(ctx, owner, GardenInput{Name: "Yard"})
if err != nil {
t.Fatalf("CreateGarden: %v", err)
}
// An explicit name wins, and is trimmed like any other garden name.
dup, err := s.CopyGarden(ctx, owner, g.ID, " Experiment ")
if err != nil {
t.Fatalf("CopyGarden: %v", err)
}
if dup.Name != "Experiment" {
t.Errorf("name = %q, want trimmed 'Experiment'", dup.Name)
}
// An over-long explicit name is rejected rather than silently truncated.
if _, err := s.CopyGarden(ctx, owner, g.ID, strings.Repeat("a", maxGardenNameLen+1)); !errors.Is(err, domain.ErrInvalidInput) {
t.Errorf("long name err = %v, want ErrInvalidInput", err)
}
// A source already at the cap still yields a valid derived name.
long, err := s.CreateGarden(ctx, owner, GardenInput{Name: strings.Repeat("b", maxGardenNameLen)})
if err != nil {
t.Fatalf("CreateGarden(long): %v", err)
}
dupLong, err := s.CopyGarden(ctx, owner, long.ID, "")
if err != nil {
t.Fatalf("CopyGarden(long): %v", err)
}
if len(dupLong.Name) > maxGardenNameLen {
t.Errorf("derived name is %d bytes, over the %d cap", len(dupLong.Name), maxGardenNameLen)
}
if !strings.HasSuffix(dupLong.Name, " (copy)") {
t.Errorf("derived name = %q, want a ' (copy)' suffix", dupLong.Name)
}
}
func TestCopyGardenNameRuneBoundary(t *testing.T) {
ctx := context.Background()
s := newTestService(t, openConfig())
owner := seedUser(t, s, "[email protected]")
// A name at the cap made of multi-byte runes: truncating for the suffix must
// not split one and leave invalid UTF-8.
name := strings.Repeat("é", maxGardenNameLen/2) // 2 bytes each == the cap
g, err := s.CreateGarden(ctx, owner, GardenInput{Name: name})
if err != nil {
t.Fatalf("CreateGarden: %v", err)
}
dup, err := s.CopyGarden(ctx, owner, g.ID, "")
if err != nil {
t.Fatalf("CopyGarden: %v", err)
}
if !utf8.ValidString(dup.Name) {
t.Errorf("derived name %q is not valid UTF-8", dup.Name)
}
if len(dup.Name) > maxGardenNameLen {
t.Errorf("derived name is %d bytes, over the %d cap", len(dup.Name), maxGardenNameLen)
}
}
func TestCopyGardenPermissions(t *testing.T) {
ctx := context.Background()
s := newTestService(t, openConfig())
alice := seedUser(t, s, "[email protected]")
bob := seedUser(t, s, "[email protected]")
g := seedGarden(t, s, alice)
// A stranger can't see the garden at all, let alone copy it.
if _, err := s.CopyGarden(ctx, bob, g.ID, ""); !errors.Is(err, domain.ErrNotFound) {
t.Errorf("stranger copy err = %v, want ErrNotFound", err)
}
// An editor may edit contents but not duplicate the garden (see CopyGarden's
// note on cross-owner plant references).
if _, err := s.AddShare(ctx, alice, g.ID, "[email protected]", domain.RoleEditor); err != nil {
t.Fatalf("AddShare: %v", err)
}
if _, err := s.CopyGarden(ctx, bob, g.ID, ""); !errors.Is(err, domain.ErrForbidden) {
t.Errorf("editor copy err = %v, want ErrForbidden", err)
}
// A missing garden is ErrNotFound.
if _, err := s.CopyGarden(ctx, alice, g.ID+9999, ""); !errors.Is(err, domain.ErrNotFound) {
t.Errorf("missing garden err = %v, want ErrNotFound", err)
}
}
func TestCopyGardenDoesNotInheritPublicLink(t *testing.T) {
ctx := context.Background()
s := newTestService(t, openConfig())
owner := seedUser(t, s, "[email protected]")
g := seedGarden(t, s, owner)
link, err := s.EnablePublicShareLink(ctx, owner, g.ID, false)
if err != nil {
t.Fatalf("EnablePublicShareLink: %v", err)
}
if link.Token == "" {
t.Fatal("no token issued")
}
dup, err := s.CopyGarden(ctx, owner, g.ID, "")
if err != nil {
t.Fatalf("CopyGarden: %v", err)
}
// The copy must start private: no token of its own...
dupLink, err := s.GetPublicShareLink(ctx, owner, dup.ID)
if err != nil {
t.Fatalf("GetPublicShareLink(copy): %v", err)
}
if dupLink.Token != "" {
t.Errorf("copy inherited a public token %q", dupLink.Token)
}
// ...and the source's token still resolves to the source, not the copy.
pub, err := s.PublicGarden(ctx, link.Token)
if err != nil {
t.Fatalf("PublicGarden: %v", err)
}
if pub.Garden.ID != g.ID {
t.Errorf("source token resolves to garden %d, want the source %d", pub.Garden.ID, g.ID)
}
}
func TestCopyGardenDoesNotInheritShares(t *testing.T) {
ctx := context.Background()
s := newTestService(t, openConfig())
alice := seedUser(t, s, "[email protected]")
bob := seedUser(t, s, "[email protected]")
g := seedGarden(t, s, alice)
if _, err := s.AddShare(ctx, alice, g.ID, "[email protected]", domain.RoleEditor); err != nil {
t.Fatalf("AddShare: %v", err)
}
dup, err := s.CopyGarden(ctx, alice, g.ID, "")
if err != nil {
t.Fatalf("CopyGarden: %v", err)
}
// Bob keeps his access to the original but gets none to the copy.
if _, err := s.GetGarden(ctx, bob, g.ID); err != nil {
t.Errorf("bob lost access to the source: %v", err)
}
if _, err := s.GetGarden(ctx, bob, dup.ID); !errors.Is(err, domain.ErrNotFound) {
t.Errorf("bob can see the copy: err = %v, want ErrNotFound", err)
}
}