package main import ( "go/ast" "go/parser" "go/token" "testing" ) func TestTicket42ProductionCompositionContainsOnlyRSSAdapters(t *testing.T) { file, err := parser.ParseFile(token.NewFileSet(), "main.go", nil, 0) if err != nil { t.Fatal(err) } found := false ast.Inspect(file, func(n ast.Node) bool { literal, ok := n.(*ast.CompositeLit) if !ok { return true } typ, ok := literal.Type.(*ast.MapType) if !ok { return true } value, ok := typ.Value.(*ast.SelectorExpr) if !ok || value.Sel.Name != "Scraper" { return true } found = true if len(literal.Elts) != 2 { t.Error("production must compose exactly two source adapters") } keys := map[string]bool{} for _, element := range literal.Elts { pair, ok := element.(*ast.KeyValueExpr) if !ok { t.Error("unexpected source composition") continue } key, ok := pair.Key.(*ast.SelectorExpr) if !ok { t.Error("source must have canonical identity") continue } keys[key.Sel.Name] = true call, ok := pair.Value.(*ast.CallExpr) if !ok { t.Error("source must be RSS adapter") continue } ctor, ok := call.Fun.(*ast.SelectorExpr) if !ok { t.Error("source must be RSS adapter") continue } pkg, ok := ctor.X.(*ast.Ident) if !ok || pkg.Name != "quanwenrss" || ctor.Sel.Name != "New" { t.Error("non-RSS adapter in production composition") } } if !keys["SourceBloomberg"] || !keys["SourceInvesting"] || keys["SourceYahooFinance"] || keys["SourceCNBCBusiness"] { t.Error("production source keys do not match RSS-only policy") } return true }) if !found { t.Fatal("production source composition not found") } }