Files
archie/internal/infrastructure/staterpc/alerts_test.go
T
sam a971947c39 refactor(state)!: split StateStoreService into per-domain services
One gRPC service per domain in state.v1 (tasks, executions, accounts, identities, packages, captures, bindings, access, orgs, alerts and so on), each with its own server type in staterpc. RPC and message names and numbers are unchanged.
2026-10-25 19:54:27 +11:00

210 lines
6.5 KiB
Go

package staterpc
import (
"context"
"fmt"
"slices"
"strings"
"testing"
"time"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
pb "github.com/samcharles93/archie-core/internal/contracts/state/v1"
"github.com/samcharles93/archie-core/internal/domain/alert"
"github.com/samcharles93/archie-core/internal/domain/org"
)
// fakeAlerts holds alerts in memory and applies the same transition rules the
// State Store does, so the test drives the RPC handlers' org handling and the
// domain's dedup, resolve and dismiss rules without a database.
type fakeAlerts struct {
alert.Store
next int64
alerts map[alert.Key]*alert.Alert
}
func (f *fakeAlerts) RaiseAlert(_ context.Context, in alert.Input) (alert.Alert, error) {
if err := in.Validate(); err != nil {
return alert.Alert{}, err
}
prev := f.alerts[in.Key]
next, _ := alert.Apply(prev, in, time.Now())
if prev == nil {
f.next++
next.ID = f.next
}
f.alerts[in.Key] = &next
return next, nil
}
func (f *fakeAlerts) ResolveAlert(_ context.Context, key alert.Key, by string) error {
if prev := f.alerts[key]; prev != nil {
next, _ := alert.Resolve(*prev, by, time.Now())
f.alerts[key] = &next
}
return nil
}
func (f *fakeAlerts) DismissAlert(_ context.Context, orgID org.OrgID, id int64, by string) error {
for key, a := range f.alerts {
if a.ID == id && key.Org == orgID {
next, _ := alert.Dismiss(*a, by, time.Now())
f.alerts[key] = &next
return nil
}
}
return alert.ErrNotFound
}
func (f *fakeAlerts) ListAlerts(_ context.Context, orgID org.OrgID, filter alert.Filter) ([]alert.Alert, error) {
states := filter.States
if len(states) == 0 {
states = []alert.State{alert.StateOpen}
}
var out []alert.Alert
for key, a := range f.alerts {
if key.Org == orgID && slices.Contains(states, a.State) {
out = append(out, *a)
}
}
slices.SortFunc(out, func(a, b alert.Alert) int { return strings.Compare(a.Subject, b.Subject) })
return out, nil
}
type alertStep struct {
as org.OrgID // the caller's scope; empty is a service acting for no one
op string // raise, event, resolve, dismiss or list
org string // the org the request names
subject string
states []string
want []string // list: "org subject state xcount"
code codes.Code
}
func TestAlerts(t *testing.T) {
const a, b = "org-a", "org-b"
cases := []struct {
name string
steps []alertStep
}{
{"raising again bumps one row", []alertStep{
{op: "raise", org: a, subject: "t1"},
{op: "raise", org: a, subject: "t1"},
{op: "raise", org: a, subject: "t1"},
{op: "raise", org: a, subject: "t2"},
{op: "list", org: a, want: []string{"org-a t1 open x3", "org-a t2 open x1"}},
}},
{"a condition alert resolves itself", []alertStep{
{op: "raise", org: a, subject: "t1"},
{op: "resolve", org: a, subject: "t1"},
{op: "resolve", org: a, subject: "t1"},
{op: "list", org: a},
{op: "list", org: a, states: []string{"resolved"}, want: []string{"org-a t1 resolved x1"}},
}},
{"an event alert is not resolved by a check", []alertStep{
{op: "event", org: a, subject: "t1"},
{op: "resolve", org: a, subject: "t1"},
{op: "list", org: a, want: []string{"org-a t1 open x1"}},
}},
{"a dismissed condition stays snoozed until it clears and recurs", []alertStep{
{op: "raise", org: a, subject: "t1"},
{op: "dismiss", as: a, subject: "t1"},
{op: "raise", org: a, subject: "t1"},
{op: "list", org: a},
{op: "resolve", org: a, subject: "t1"},
{op: "raise", org: a, subject: "t1"},
{op: "list", org: a, want: []string{"org-a t1 open x1"}},
}},
{"a dismissed event comes back when it happens again", []alertStep{
{op: "event", org: a, subject: "t1"},
{op: "dismiss", as: a, subject: "t1"},
{op: "list", org: a},
{op: "event", org: a, subject: "t1"},
{op: "list", org: a, want: []string{"org-a t1 open x1"}},
}},
{"orgs never see each other's alerts", []alertStep{
{op: "raise", org: a, subject: "t1"},
{op: "raise", org: b, subject: "t1"},
{op: "raise", org: b, subject: "t2"},
{op: "list", as: a, want: []string{"org-a t1 open x1"}},
{op: "list", as: b, org: a, want: []string{"org-b t1 open x1", "org-b t2 open x1"}},
}},
{"a scoped caller cannot reach another org", []alertStep{
{op: "raise", org: a, subject: "t1"},
{op: "dismiss", as: b, org: a, subject: "t1", code: codes.NotFound},
{op: "raise", as: b, org: a, subject: "t9"},
{op: "list", as: a, want: []string{"org-a t1 open x1"}},
{op: "list", as: b, want: []string{"org-b t9 open x1"}},
}},
{"a service must name the org", []alertStep{
{op: "raise", subject: "t1", code: codes.InvalidArgument},
}},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
s := &alertServer{server: &server{deps: Deps{Alerts: &fakeAlerts{alerts: map[alert.Key]*alert.Alert{}}}}}
for i, step := range tc.steps {
ctx := context.Background()
if step.as != "" {
ctx = org.WithScope(ctx, step.as)
}
got, err := runAlertStep(ctx, s, step)
if status.Code(err) != step.code {
t.Fatalf("step %d (%s): code %v, want %v (%v)", i, step.op, status.Code(err), step.code, err)
}
if step.op == "list" && !slices.Equal(got, step.want) {
t.Fatalf("step %d: listed %q, want %q", i, got, step.want)
}
}
})
}
}
func runAlertStep(ctx context.Context, s *alertServer, step alertStep) ([]string, error) {
switch step.op {
case "raise", "event":
_, err := s.RaiseAlert(ctx, &pb.RaiseAlertRequest{
OrgId: step.org, Kind: "k", Subject: step.subject, Severity: "action", Title: "t",
Condition: step.op == "raise",
})
return nil, err
case "resolve":
_, err := s.ResolveAlert(ctx, &pb.ResolveAlertRequest{OrgId: step.org, Kind: "k", Subject: step.subject})
return nil, err
case "dismiss":
// The alert's id is looked up under the org the step names, or the
// caller's own.
owner := cmpOr(step.org, string(step.as))
listed, err := s.deps.Alerts.ListAlerts(ctx, org.OrgID(owner), alert.Filter{})
if err != nil {
return nil, err
}
id := int64(-1)
for _, a := range listed {
if a.Subject == step.subject {
id = a.ID
}
}
_, err = s.DismissAlert(ctx, &pb.DismissAlertRequest{OrgId: step.org, Id: id})
return nil, err
default:
r, err := s.ListAlerts(ctx, &pb.ListAlertsRequest{OrgId: step.org, States: step.states})
var out []string
for _, a := range r.GetAlerts() {
out = append(out, fmt.Sprintf("%s %s %s x%d", a.GetOrgId(), a.GetSubject(), a.GetState(), a.GetCount()))
}
return out, err
}
}
func cmpOr(values ...string) string {
for _, v := range values {
if v != "" {
return v
}
}
return ""
}