Agent tools: plant lookup, plant creation, journal entries (#55) (#69)
Build image / build-and-push (push) Successful in 11s
Build image / build-and-push (push) Successful in 11s
Co-authored-by: Steve Dudenhoeffer <[email protected]>
This commit was merged in pull request #69.
This commit is contained in:
@@ -237,3 +237,138 @@ func TestDeletePlantInUseIsBlocked(t *testing.T) {
|
||||
t.Errorf("delete freed plant: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestFindPlantsRanksAndDisambiguates — FindPlants deliberately returns several
|
||||
// candidates rather than one guess, because "garlic" against a catalog holding
|
||||
// both "Garlic" and "German Red Garlic" is genuinely ambiguous and a caller with
|
||||
// the surrounding context is better placed to choose than a heuristic here.
|
||||
func TestFindPlantsRanksAndDisambiguates(t *testing.T) {
|
||||
s := newTestService(t, openConfig())
|
||||
owner := seedUser(t, s, "[email protected]")
|
||||
ctx := context.Background()
|
||||
|
||||
custom, err := s.CreatePlant(ctx, owner, PlantInput{
|
||||
Name: "German Red Garlic", Category: domain.CategoryVegetable, SpacingCM: 15,
|
||||
Color: "#8a5a8a", Icon: "🧄",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("CreatePlant: %v", err)
|
||||
}
|
||||
|
||||
got, err := s.FindPlants(ctx, owner, "garlic")
|
||||
if err != nil {
|
||||
t.Fatalf("FindPlants: %v", err)
|
||||
}
|
||||
names := map[string]bool{}
|
||||
for _, m := range got {
|
||||
names[m.Name] = true
|
||||
}
|
||||
if !names["Garlic"] || !names[custom.Name] {
|
||||
t.Errorf("got %v, want both the built-in and the custom variety", names)
|
||||
}
|
||||
// Exact match first, so a caller taking [0] gets the least surprising one.
|
||||
if got[0].Name != "Garlic" {
|
||||
t.Errorf("first match = %q, want the exact name", got[0].Name)
|
||||
}
|
||||
|
||||
// Prefix beats substring: "german" should surface the custom one first.
|
||||
pre, err := s.FindPlants(ctx, owner, "german")
|
||||
if err != nil {
|
||||
t.Fatalf("FindPlants: %v", err)
|
||||
}
|
||||
if len(pre) == 0 || pre[0].Name != custom.Name {
|
||||
t.Errorf("FindPlants(german) = %+v, want the custom variety first", pre)
|
||||
}
|
||||
|
||||
// Category search works, and a query matching nothing says so plainly.
|
||||
if herbs, _ := s.FindPlants(ctx, owner, "herb"); len(herbs) == 0 {
|
||||
t.Error("category search found nothing")
|
||||
}
|
||||
if none, _ := s.FindPlants(ctx, owner, "zzzzz"); len(none) != 0 {
|
||||
t.Errorf("FindPlants(zzzzz) = %+v, want nothing", none)
|
||||
}
|
||||
}
|
||||
|
||||
// TestFindPlantsReportsSeedRemaining — so a caller can say "you only have enough
|
||||
// for half that bed" instead of confidently planting seed that doesn't exist.
|
||||
func TestFindPlantsReportsSeedRemaining(t *testing.T) {
|
||||
s := newTestService(t, openConfig())
|
||||
owner := seedUser(t, s, "[email protected]")
|
||||
other := seedUser(t, s, "[email protected]")
|
||||
ctx := context.Background()
|
||||
|
||||
plant := seedOwnPlant(t, s, owner, 15)
|
||||
if _, err := s.CreateSeedLot(ctx, owner, SeedLotInput{
|
||||
PlantID: plant.ID, Quantity: 100, Unit: domain.UnitSeeds,
|
||||
}); err != nil {
|
||||
t.Fatalf("CreateSeedLot: %v", err)
|
||||
}
|
||||
|
||||
got, err := s.FindPlants(ctx, owner, plant.Name)
|
||||
if err != nil {
|
||||
t.Fatalf("FindPlants: %v", err)
|
||||
}
|
||||
if len(got) == 0 || got[0].SeedRemaining == nil || *got[0].SeedRemaining != 100 {
|
||||
t.Fatalf("seedRemaining = %+v, want 100", got[0].SeedRemaining)
|
||||
}
|
||||
if got[0].SeedUnit != domain.UnitSeeds {
|
||||
t.Errorf("seedUnit = %q", got[0].SeedUnit)
|
||||
}
|
||||
|
||||
// A plant with no lots reports nothing rather than a misleading zero.
|
||||
builtin, err := s.FindPlants(ctx, owner, "Garlic")
|
||||
if err != nil {
|
||||
t.Fatalf("FindPlants: %v", err)
|
||||
}
|
||||
if len(builtin) > 0 && builtin[0].SeedRemaining != nil {
|
||||
t.Errorf("a plant with no lots reported %v remaining", *builtin[0].SeedRemaining)
|
||||
}
|
||||
|
||||
// And lots are private: another user searching the same plant sees no seed.
|
||||
// (They can't see the custom plant at all, so search a built-in instead.)
|
||||
if _, err := s.CreateSeedLot(ctx, other, SeedLotInput{
|
||||
PlantID: builtinPlantID(t, s, other), Quantity: 50, Unit: domain.UnitSeeds,
|
||||
}); err != nil {
|
||||
t.Fatalf("CreateSeedLot(other): %v", err)
|
||||
}
|
||||
mine, _ := s.FindPlants(ctx, owner, "Garlic")
|
||||
for _, m := range mine {
|
||||
if m.SeedRemaining != nil {
|
||||
t.Errorf("saw another user's seed count on %q", m.Name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestFindPlantsOmitsATotalItCannotHonestlyGive — lots in different units can't
|
||||
// be summed. Dropping only the unit LABEL would leave a number that reads as a
|
||||
// quantity and isn't one; the count of lots still says there is seed here.
|
||||
func TestFindPlantsOmitsATotalItCannotHonestlyGive(t *testing.T) {
|
||||
s := newTestService(t, openConfig())
|
||||
owner := seedUser(t, s, "[email protected]")
|
||||
plant := seedOwnPlant(t, s, owner, 15)
|
||||
ctx := context.Background()
|
||||
|
||||
for _, u := range []string{domain.UnitSeeds, domain.UnitGrams} {
|
||||
if _, err := s.CreateSeedLot(ctx, owner, SeedLotInput{PlantID: plant.ID, Quantity: 50, Unit: u}); err != nil {
|
||||
t.Fatalf("CreateSeedLot(%s): %v", u, err)
|
||||
}
|
||||
}
|
||||
|
||||
got, err := s.FindPlants(ctx, owner, plant.Name)
|
||||
if err != nil {
|
||||
t.Fatalf("FindPlants: %v", err)
|
||||
}
|
||||
if len(got) == 0 {
|
||||
t.Fatal("plant not found")
|
||||
}
|
||||
if got[0].SeedRemaining != nil {
|
||||
t.Errorf("reported %v across mismatched units; that number means nothing", *got[0].SeedRemaining)
|
||||
}
|
||||
if got[0].SeedUnit != "" {
|
||||
t.Errorf("seedUnit = %q, want empty", got[0].SeedUnit)
|
||||
}
|
||||
// But "several lots, no single total" must stay distinguishable from "none".
|
||||
if got[0].SeedLots != 2 {
|
||||
t.Errorf("seedLots = %d, want 2", got[0].SeedLots)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user