Files
archie/tools/contractaudit/audit_test.go
T
sam e537447333 feat(tools): add contractaudit for standing contract-conformance reporting
Reachability (tools/reachaudit, epic archie-core-06ag) answers "is this code
in a shipped binary?". It cannot answer the complementary question: "does
every declared contract surface have a consumer, or a declaration that it
has none?". ratelimit was in the binary closure and called by nothing, which
reachability could not see.

tools/contractaudit classifies each subject of each declared surface as
CONSUMED, DECLARED-UNCONSUMED, UNDECLARED-UNCONSUMED, or STALE-DECLARATION,
and only the last two are findings. The declaration allowlist must match
reality exactly: a missing entry fails and so does an entry that outlived
its subject, because a stale allowlist is how a gate quietly stops checking
anything.

Surfaces: the two gRPC services, whose consumption is derived by scanning
adapter bodies for calls through the generated client, and the dashboard
HTTP API, matched between the registerXRoutes call sites and the path
literals the bundled client calls.

Findings on the current tree, all deliberate and now declared:
ListCaptures and ListUndispatchedCaptures are superseded by their streaming
variants -- staterpc's client calls StreamCaptures -- so no production
client reaches the unary RPCs.

Three bugs this tool found in itself, each recorded because each is the
class of error that makes a conformance check worse than none:

- Joining RPCs to adapter methods by name silently reported live RPCs as
  unconsumed, because adapters do not map one-to-one: StoreClient.Get calls
  GetSession, List calls ListSessions.
- Reflecting over the client value rather than the pointer reported zero
  methods, hence the whole surface unconsumed.
- Treating the asset mount and the health probes as dashboard API routes
  reported four healthy routes as undeclared; they have different consumers
  and are out of scope for this surface.

The consumer extractor fails loudly on a call shape it cannot parse rather
than under-reporting, and every check is negative-tested: a route, an RPC,
and a removed consumer are each injected, confirmed to fail for the right
reason, and reverted. The declaration staleness rule is tested too.

Advisory when run directly; task check runs it -strict. See
docs/prds/contract-conformance-audit.md.
2026-09-15 09:41:20 +10:00

110 lines
4.1 KiB
Go

package main
import (
"os"
"path/filepath"
"testing"
)
// TestClassify covers the three-state rule plus the staleness rule that runs
// in the opposite direction. The staleness cases are the important ones: an
// allowlist that permits something which is no longer true is how a gate
// quietly stops checking anything.
func TestClassify(t *testing.T) {
subjects := []Subject{
{Name: "Svc/Consumed", Evidence: "proto:x", Consumed: true, Consumer: "internal/infrastructure/staterpc/client.go:41"},
{Name: "Svc/Declared", Evidence: "proto:y", Consumed: false},
{Name: "Svc/Bare", Evidence: "proto:z", Consumed: false},
{Name: "Svc/FreshlyConsumed", Evidence: "proto:w", Consumed: true, Consumer: "internal/foo/bar.go:9"},
}
declarations := Declarations{Surfaces: map[string]map[string]Declaration{
"proto/state/v1": {
"Svc/Declared": {Reason: "no consumer yet; extraction is planned", Tracker: "archie-core-8cda.4"},
"Svc/FreshlyConsumed": {Reason: "was unconsumed at audit time", Tracker: "archie-core-8cda.6"},
"Svc/Gone": {Reason: "surface removed it", Tracker: "archie-core-8cda.7"},
},
}}
findings := classify("proto/state/v1", subjects, declarations)
got := map[string]Class{}
for _, finding := range findings {
got[finding.Subject] = finding.Class
}
want := map[string]Class{
"Svc/Consumed": Consumed,
"Svc/Declared": DeclaredUnconsumed,
"Svc/Bare": Undeclared,
"Svc/FreshlyConsumed": Stale,
"Svc/Gone": Stale,
}
for subject, class := range want {
if got[subject] != class {
t.Errorf("classify(%q) = %q, want %q", subject, got[subject], class)
}
}
if len(findings) != len(want) {
t.Errorf("classify produced %d findings, want %d", len(findings), len(want))
}
}
// TestClassifyDeclaredEntryKeepsProvenance asserts the reason and tracker
// survive classification, because the declaration file is the only place the
// debt is legible.
func TestClassifyDeclaredEntryKeepsProvenance(t *testing.T) {
subjects := []Subject{{Name: "Svc/Declared"}}
declarations := Declarations{Surfaces: map[string]map[string]Declaration{
"s": {"Svc/Declared": {Reason: "the consequence, stated", Tracker: "archie-core-abc.1"}},
}}
findings := classify("s", subjects, declarations)
if len(findings) != 1 {
t.Fatalf("got %d findings, want 1", len(findings))
}
if findings[0].Evidence != "the consequence, stated" {
t.Errorf("evidence = %q, want the reason", findings[0].Evidence)
}
if findings[0].Tracker != "archie-core-abc.1" {
t.Errorf("tracker = %q, want the bead id", findings[0].Tracker)
}
}
// TestUndeclaredCountCountsOnlyFindings asserts a healthy surface contributes
// zero, so a strict run over a fully-declared surface passes.
func TestUndeclaredCountCountsOnlyFindings(t *testing.T) {
findings := []Finding{
{Subject: "a", Class: Consumed},
{Subject: "b", Class: DeclaredUnconsumed},
{Subject: "c", Class: Undeclared},
{Subject: "d", Class: Stale},
}
if got := UndeclaredCount(findings); got != 2 {
t.Errorf("UndeclaredCount = %d, want 2 (undeclared + stale)", got)
}
}
// TestLoadDeclarationsMissingFileIsEmpty asserts an absent allowlist is the
// correct starting state: every unconsumed subject becomes undeclared, which
// is the honest first report rather than a silent pass.
func TestLoadDeclarationsMissingFileIsEmpty(t *testing.T) {
declarations, err := loadDeclarations(filepath.Join(t.TempDir(), "absent.json"))
if err != nil {
t.Fatalf("loadDeclarations(absent) error = %v, want nil", err)
}
if len(declarations.Surfaces) != 0 {
t.Errorf("surfaces = %v, want empty", declarations.Surfaces)
}
}
// TestLoadDeclarationsRejectsMalformed asserts a broken allowlist fails loudly
// rather than degrading to "no declarations", which would silently turn every
// declared entry into a finding nobody could clear.
func TestLoadDeclarationsRejectsMalformed(t *testing.T) {
path := filepath.Join(t.TempDir(), "bad.json")
if err := os.WriteFile(path, []byte("{not json"), 0o600); err != nil {
t.Fatal(err)
}
if _, err := loadDeclarations(path); err == nil {
t.Error("loadDeclarations(malformed) error = nil, want a parse failure")
}
}