Files
supermarket/internal/approvals/approvals_test.go
T
davidandClaude Opus 5.5 311d2e815b Phase 11, part 3: approvals for what agents send out
Agents can now ask to publish or schedule a post, send an email, add
someone to a mailing list, or apply an SEO change. Each waits on the
Approvals page (with a count in the sidebar) for an admin or a member with
edit on its module, who can edit it first, approve or refuse it, and let
the agent take that class of action on its own with a cap per day. A
rotation after a leak revokes the old keys and withdraws everything
allowed. All of it is in the audit log.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 10:54:53 -07:00

66 lines
1.9 KiB
Go

package approvals
import (
"errors"
"path/filepath"
"testing"
"time"
"supermarket/internal/db"
)
func TestRequestsAndGrants(t *testing.T) {
d, err := db.Open(filepath.Join(t.TempDir(), "app.db"))
if err != nil {
t.Fatal(err)
}
defer d.Close()
now := time.Now()
r := &Request{Agent: 5, Class: "post:2", Module: "content", Target: 11, Params: map[string]string{"at": "x"}, Summary: "Publish draft 11"}
if err := Add(d, r, now); err != nil {
t.Fatal(err)
}
if p, _ := Pending(d, "post:2", 11); p == nil || p.ID != r.ID {
t.Error("not pending")
}
if err := Claim(d, r.ID, 1, Done, now); err != nil {
t.Fatal(err)
}
if err := Claim(d, r.ID, 2, Refused, now); !errors.Is(err, ErrDecided) {
t.Errorf("decided twice: %v", err)
}
Finish(d, r.ID, errors.New("X said no"))
got, _ := Get(d, r.ID)
if got.Status != Failed || got.Result != "X said no" || got.DecidedBy != 1 || got.Params["at"] != "x" || got.OnItsOwn() {
t.Errorf("%+v", got)
}
if list, _ := List(d, Waiting, 0, 10); len(list) != 0 {
t.Error("still waiting")
}
// On its own, with a cap of 2 a day.
if ok, _ := Allowed(d, 5, "email-first", now); ok {
t.Error("allowed without a grant")
}
Allow(d, Grant{Agent: 5, Class: "email-first", Label: "send a first email", Module: "prospects", DailyCap: 2, GrantedBy: 1}, now)
for i := 0; i < 2; i++ {
if ok, _ := Allowed(d, 5, "email-first", now); !ok {
t.Fatalf("call %d refused", i)
}
Add(d, &Request{Agent: 5, Class: "email-first", Module: "prospects", Target: int64(i), Summary: "x", Status: Done}, now)
}
if ok, _ := Allowed(d, 5, "email-first", now); ok {
t.Error("over the cap")
}
if ok, _ := Allowed(d, 5, "email-first", now.Add(25*time.Hour)); !ok {
t.Error("the cap is per day")
}
if gs, _ := Grants(d, 5); len(gs) != 1 || gs[0].DailyCap != 2 {
t.Errorf("grants %+v", gs)
}
WithdrawAll(d, 5)
if ok, _ := Allowed(d, 5, "email-first", now.Add(25*time.Hour)); ok {
t.Error("withdrawn")
}
}