package mapatest import ( "go/ast" "go/parser" "go/token" "io/fs" "os/exec" "path/filepath" "strconv" "strings" "testing" ) // I8, egress (Notion import spec §5, D2). Mapa's own tree has no egress // exemption at all: no client, transport, request constructor, dialler // or resolver anywhere in internal/mapa or cmd. Only the notion module // dials; only internal/mapa's import_*.go files import it; notionmd, // the pure mapper, never does; and no production file imports the // fake. rastrillo's sign-in mail lives in rastrillo. // // Mutations (make mutate, first red 2026-11-17): an http.Get in a // handler; the module imported from notionmd. const ( notionModule = "amadan.net/oficina/notion.git" notionFake = notionModule + "/notiontest" ) // dials is, by package path, every name that dials, resolves, or builds // what dials; "*" is the whole package. Selectors are matched through // each file's own import names, so an alias does not hide one, and a // dot import of any of these packages is refused outright (Fable L12). var dials = map[string]map[string]bool{ "net/http": {"Client": true, "Transport": true, "Get": true, "Post": true, "Head": true, "PostForm": true, "NewRequest": true, "NewRequestWithContext": true, "DefaultClient": true, "DefaultTransport": true}, "net": {"Dial": true, "DialTimeout": true, "DialTCP": true, "DialUDP": true, "DialIP": true, "DialUnix": true, "Dialer": true, "Resolver": true, "DefaultResolver": true, "Lookup*": true}, "crypto/tls": {"Dial": true, "DialWithDialer": true, "Dialer": true}, "net/smtp": {"*": true}, "os/exec": {"*": true}, } // dialling is whether name from the package at path dials. func dialling(path, name string) bool { names, ok := dials[path] if !ok { return false } return names["*"] || names[name] || (names["Lookup*"] && strings.HasPrefix(name, "Lookup")) } // productionFiles is every non-test Go file under the repository's // internal/mapa and cmd, relative to the repository. func productionFiles(t *testing.T) []string { t.Helper() var files []string for _, root := range []string{"../mapa", "../../cmd"} { err := filepath.WalkDir(root, func(path string, entry fs.DirEntry, err error) error { if err == nil && !entry.IsDir() && strings.HasSuffix(path, ".go") && !strings.HasSuffix(path, "_test.go") { files = append(files, path) } return err }) if err != nil { t.Fatal(err) } } return files } // platformEgress is Mapa's one other egress, by file and by name: Mapa // images' writes to CARLOS's own bucket, the platform's object store, // over a presigned URL (images spec §1). Images landed after the Notion // spec said no directory is exempt; these two files may build an // http.Client and a request, and nothing else that dials, and the // import rules hold in them as everywhere (Fable round 3, L6). var platformEgress = map[string]map[string]bool{ "config_images.go": {"net/http.Client": true}, "images_bucket.go": {"net/http.Client": true, "net/http.NewRequestWithContext": true}, } func TestI8MapaHasNoEgressOfItsOwn(t *testing.T) { for _, path := range productionFiles(t) { var allowed map[string]bool if filepath.Dir(path) == "../mapa" { allowed = platformEgress[filepath.Base(path)] } file, err := parser.ParseFile(token.NewFileSet(), path, nil, 0) if err != nil { t.Fatal(err) } names := importNames(t, path, file) ast.Inspect(file, func(node ast.Node) bool { selector, ok := node.(*ast.SelectorExpr) if !ok { return true } ident, ok := selector.X.(*ast.Ident) name := "" if ok { name = names[ident.Name] + "." + selector.Sel.Name } if ok && !allowed[name] && dialling(names[ident.Name], selector.Sel.Name) { t.Errorf("%s uses %s.%s: Mapa dials only through the "+ "notion module", path, names[ident.Name], selector.Sel.Name) } return true }) checkImports(t, path, file) } } // importNames maps each name a file uses for an import to its path, // and refuses a dot import of a package that dials. func importNames(t *testing.T, path string, file *ast.File) map[string]string { names := map[string]string{} for _, spec := range file.Imports { imported, _ := strconv.Unquote(spec.Path.Value) name := imported[strings.LastIndex(imported, "/")+1:] if spec.Name != nil { name = spec.Name.Name } if name == "." && dials[imported] != nil { t.Errorf("%s dot-imports %s", path, imported) } names[name] = imported } return names } // checkImports holds one file's imports to the rule. func checkImports(t *testing.T, path string, file *ast.File) { name := filepath.Base(path) importer := filepath.Base(filepath.Dir(path)) == "mapa" && strings.HasPrefix(name, "import_") for _, spec := range file.Imports { imported, _ := strconv.Unquote(spec.Path.Value) switch { case strings.HasPrefix(imported, notionFake): t.Errorf("%s imports the fake; only tests may", path) case strings.HasPrefix(imported, notionModule) && !importer: t.Errorf("%s imports the notion module; only "+ "internal/mapa/import_*.go may", path) } } } // The package half: of every package the binary links, only // internal/mapa imports the module. func TestI8OnlyTheImportLinksTheModule(t *testing.T) { command := exec.Command("go", "list", "-deps", "-f", `{{.ImportPath}} {{join .Imports " "}}`, "../../cmd/...") output, err := command.CombinedOutput() if err != nil { t.Fatalf("go list: %v\n%s", err, output) } for _, line := range strings.Split(string(output), "\n") { fields := strings.Fields(line) if len(fields) < 2 || strings.HasPrefix(fields[0], notionModule) { continue } for _, imported := range fields[1:] { if strings.HasPrefix(imported, notionModule) && fields[0] != "amadan.net/oficina/mapa/internal/mapa" { t.Errorf("%s imports %s", fields[0], imported) } } } }