| 1 | package main |
| 2 | |
| 3 | import "testing" |
| 4 | |
| 5 | func TestLayeringContract(t *testing.T) { |
| 6 | for _, tc := range []struct { |
| 7 | name, pkg, dep string |
| 8 | wantViolation bool |
| 9 | }{ |
| 10 | {"utility package stays a leaf", "internal/fileutil", "internal/config", true}, |
| 11 | {"utility package may use the stdlib only", "internal/textutil", "internal/agent", true}, |
| 12 | {"kernel may not reach the controller", "internal/agent", "internal/control", true}, |
| 13 | {"kernel may not reach a frontend", "internal/agent", "internal/cli", true}, |
| 14 | {"diagnostics may not reach the composition root", "internal/capdiag", "internal/boot", true}, |
| 15 | {"frontend may use the controller", "internal/serve", "internal/control", false}, |
| 16 | {"frontend may use another frontend", "internal/cli", "internal/serve", false}, |
| 17 | {"frontend subpackage may use its parent", "internal/bot/qq", "internal/bot", false}, |
| 18 | {"entrypoint may use a frontend", "cmd/reasonix", "internal/cli", false}, |
| 19 | {"desktop host may use the controller", "desktop", "internal/control", false}, |
| 20 | {"controller may use the kernel", "internal/control", "internal/agent", false}, |
| 21 | {"kernel may use a utility package", "internal/agent", "internal/fileutil", false}, |
| 22 | } { |
| 23 | t.Run(tc.name, func(t *testing.T) { |
| 24 | if got := violates(tc.pkg, tc.dep) != ""; got != tc.wantViolation { |
| 25 | t.Fatalf("violates(%q, %q) = %v, want %v", tc.pkg, tc.dep, got, tc.wantViolation) |
| 26 | } |
| 27 | }) |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | func TestLayeringReadsImportsFromSource(t *testing.T) { |
| 32 | src := "package agent\n\nimport (\n\t\"fmt\"\n\t\"reasonix/internal/cli\"\n)\n\nvar _ = fmt.Sprint\nvar _ = cli.Run\n" |
| 33 | s := parseBytes("internal/agent/a.go", []byte(src)) |
| 34 | found := checkLayering(map[string][]importRef{s.rel: s.importRefs()}) |
| 35 | if len(found) != 1 || found[0].Rule != ruleLayering || found[0].Line != 5 { |
| 36 | t.Fatalf("want one %s on line 5, got %+v", ruleLayering, found) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | func TestLayeringIgnoresTestFiles(t *testing.T) { |
| 41 | src := "package agent\n\nimport \"reasonix/internal/cli\"\n\nvar _ = cli.Run\n" |
| 42 | s := parseBytes("internal/agent/a_test.go", []byte(src)) |
| 43 | if refs := s.importRefs(); len(refs) != 0 { |
| 44 | t.Fatalf("test file imports should not be layered: %v", refs) |
| 45 | } |
| 46 | } |
| 47 |