package host import ( "go/ast" "go/parser" "go/token" "os" "strings" "testing" ) func TestPlan14ProductionProofConstructors(t *testing.T) { t.Parallel() entries, err := os.ReadDir(".") if err != nil { t.Fatal(err) } found := map[string]int{} healthyWriter := 0 settledWriter := 0 for _, entry := range entries { name := entry.Name() if !strings.HasSuffix(name, ".go") || strings.HasSuffix(name, "_test.go") { continue } file, err := parser.ParseFile(token.NewFileSet(), name, nil, 0) if err != nil { t.Fatal(err) } ast.Inspect(file, func(n ast.Node) bool { if lit, ok := n.(*ast.CompositeLit); ok { if typ, ok := lit.Type.(*ast.Ident); ok && typ.Name == "proofRoot" { for _, elt := range lit.Elts { kv, ok := elt.(*ast.KeyValueExpr) if !ok { continue } key, ok := kv.Key.(*ast.Ident) if !ok || key.Name != "healthyDir" { continue } if isRootHealthy(kv.Value) { healthyWriter++ } } } } if call, ok := n.(*ast.CallExpr); ok { if fn, ok := call.Fun.(*ast.SelectorExpr); ok && fn.Sel.Name == "recordSettled" && len(call.Args) == 1 && isRootHealthy(call.Args[0]) { settledWriter++ } } sel, ok := n.(*ast.SelectorExpr) if !ok { return true } pkg, ok := sel.X.(*ast.Ident) if !ok || pkg.Name != "release" { return true } if sel.Sel.Name == "NewProofGate" { t.Errorf("%s retains writable healthy authority", name) } if sel.Sel.Name == "NewRootProofGate" { found[name]++ } return true }) } for _, name := range []string{"agent.go", "proofask.go", "heldstage.go", "movespass.go"} { if found[name] != 1 { t.Errorf("%s: trusted constructors = %d", name, found[name]) } } if healthyWriter != 1 || settledWriter != 1 { t.Fatalf("root healthy writers: observation=%d settle=%d", healthyWriter, settledWriter) } if _, err := os.Stat("movesheldrelease_production.go"); err == nil && found["movesheldrelease_production.go"] != 1 { t.Fatal("moves root constructor not wired") } } func isRootHealthy(expr ast.Expr) bool { sel, ok := expr.(*ast.SelectorExpr) if !ok || sel.Sel.Name != "RootHealthyDir" { return false } pkg, ok := sel.X.(*ast.Ident) return ok && pkg.Name == "release" }