package calls import ( "context" "encoding/json" "strconv" "sync/atomic" "testing" "time" access "amadan.net/oficina/agent-access.git" ) func TestDelegatedActorNamespaceAndSingleRateCharge(t *testing.T) { f, p := linkedDelegated(t) var checks atomic.Int64 if e := f.app.mountDelegated(access.VerifyFunc(func(context.Context, string) (access.Principal, error) { checks.Add(1); return *p, nil }), f.app.agentIssuer, "acme"); e != nil { t.Fatal(e) } now := time.Now() f.app.store.clk.set(func() time.Time { return now }) if rec := delegatedCall(f.app, "calls_create_meeting", delegatedInputForTest()); rec.Code != 200 { t.Fatal(rec.Code, rec.Body.String()) } if checks.Load() < 3 { t.Fatal("write did not reverify delegation") } person, e := f.app.delegatedPerson(t.Context(), *p, "acme") if e != nil { t.Fatal(e) } raw, _ := json.Marshal([]string{f.app.cfg.Home.Issuer, f.app.agentIssuer, p.Subject, p.Client, "acme", strconv.FormatInt(person.ID, 10)}) want, e := f.app.sealer.Fingerprint("connection.actor.v1", string(raw)) if e != nil { t.Fatal(e) } var got string if e = f.app.db.Writer().QueryRow("SELECT coordinate FROM calls_connections").Scan(&got); e != nil || got != want { t.Fatal("actor namespace mismatch", e) } // One creation plus 59 reads exhaust exactly the 60-request allowance. for i := 0; i < 59; i++ { if rec := delegatedCall(f.app, "calls_list_meetings", map[string]any{"team": "acme"}); rec.Code != 200 { t.Fatal("one request charged repeatedly", i, rec.Code) } } if rec := delegatedCall(f.app, "calls_list_meetings", map[string]any{"team": "acme"}); rec.Code != 429 { t.Fatal("request not charged", rec.Code) } }