Files
archie/internal/infrastructure/staterpc/mapping_match_test.go
T
sam fec0ea1f4e feat(eda): mappings per event type, match counts, several bindings per source
A mapping belongs to an event type and is checked against its inferred
schema on save. Each event a mapping resolves is counted once in
mapping_matches (migration 0008), read back as match_count and
last_matched_at. Bindings carry an optional CEL filter over the mapping's
parameters, checked on save. The per-source unique indexes are dropped:
every armed binding whose mapping matches the capture's event type and
whose filter admits it dispatches once, claimed in the per-binding ledger
before its task is enqueued. The State Store gains RecordMappingMatch and
the new mapping and binding fields.
2026-09-24 14:42:58 +10:00

59 lines
2.1 KiB
Go

package staterpc
import (
"context"
"net"
"reflect"
"testing"
"time"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/test/bufconn"
"github.com/samcharles93/archie-core/internal/domain/binding"
"github.com/samcharles93/archie-core/internal/domain/mapping"
)
type fakeMappingMatches struct{ mappingID, captureID string }
func (f *fakeMappingMatches) RecordMappingMatch(_ context.Context, mappingID, captureID string) error {
f.mappingID, f.captureID = mappingID, captureID
return nil
}
func TestRecordMappingMatchCrossesTheWire(t *testing.T) {
fake := &fakeMappingMatches{}
listener := bufconn.Listen(1 << 20)
server := grpc.NewServer()
RegisterServer(server, Deps{MappingMatches: fake})
go func() { _ = server.Serve(listener) }()
t.Cleanup(func() { server.Stop(); _ = listener.Close() })
conn, err := grpc.NewClient("passthrough:///state",
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithContextDialer(func(ctx context.Context, _ string) (net.Conn, error) { return listener.DialContext(ctx) }))
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = conn.Close() })
if err := NewClient(conn).RecordMappingMatch(t.Context(), "m1", "c1"); err != nil {
t.Fatalf("RecordMappingMatch() error = %v", err)
}
if fake.mappingID != "m1" || fake.captureID != "c1" {
t.Fatalf("server received (%q, %q), want (m1, c1)", fake.mappingID, fake.captureID)
}
}
func TestMappingAndBindingCarryEventTypeCountsAndFilter(t *testing.T) {
stamp := time.Date(2026, 9, 24, 9, 0, 0, 0, time.UTC)
m := mapping.Mapping{ID: "m", Name: "n", Fields: []mapping.Field{{Name: "a", Path: "a", Type: mapping.TypeString}}, EventTypeID: "et-1", MatchCount: 7, LastMatchedAt: stamp, CreatedAt: stamp, UpdatedAt: stamp}
if got := mappingValue(mappingProto(m)); !reflect.DeepEqual(got, m) {
t.Fatalf("mapping after round trip = %+v, want %+v", got, m)
}
b := binding.Binding{ID: "b", Filter: `severity == "high"`, CreatedAt: stamp, UpdatedAt: stamp}
if got := bindingValue(bindingProto(b)); got.Filter != b.Filter {
t.Fatalf("binding filter after round trip = %q, want %q", got.Filter, b.Filter)
}
}