package layer_test import ( "os" "path/filepath" "testing" "github.com/netkingdom/flex-auth/internal/layer" ) func writeRepo(t *testing.T, root, name, intent, declFile string) { t.Helper() dir := filepath.Join(root, name) if err := os.MkdirAll(dir, 0o755); err != nil { t.Fatal(err) } if intent != "" { body := "---\nlayer: " + intent + "\nrole: PDP\n---\n\n# x\n" if err := os.WriteFile(filepath.Join(dir, "INTENT.md"), []byte(body), 0o644); err != nil { t.Fatal(err) } } if declFile != "" { if err := os.WriteFile(filepath.Join(dir, "layer.yaml"), []byte("layer: "+declFile+"\n"), 0o644); err != nil { t.Fatal(err) } } } // The finding FLEX-WP-0030 B1 rests on: §11 accepts either form and does not // say which governs when a repository carries both and they disagree. func TestSurveyDetectsFormsDisagreeingWithinOneRepo(t *testing.T) { root := t.TempDir() writeRepo(t, root, "peer", "Engine", "engine") rows, err := layer.SurveyDeclarations(root, []string{"peer"}) if err != nil { t.Fatal(err) } if len(rows) != 1 { t.Fatalf("rows = %d; want 1", len(rows)) } if !rows[0].SelfDisagrees() { t.Fatal("Engine vs engine across the two §11 forms was not reported as disagreement") } if got := len(layer.SelfDisagreeing(rows)); got != 1 { t.Fatalf("SelfDisagreeing = %d; want 1", got) } } // Case is not folded: whether §3 is case-insensitive is the open question, and // folding here would hide the finding rather than resolve it. func TestSurveyDoesNotFoldCase(t *testing.T) { root := t.TempDir() writeRepo(t, root, "peer", "", "engine") rows, _ := layer.SurveyDeclarations(root, []string{"peer"}) if rows[0].File.InVocabulary { t.Fatal(`"engine" was accepted into the §3 vocabulary; the survey must not fold case`) } } func TestSurveyReportsMissingDeclaration(t *testing.T) { root := t.TempDir() writeRepo(t, root, "silent", "", "") rows, _ := layer.SurveyDeclarations(root, []string{"silent"}) if rows[0].Declared() { t.Fatal("a repository with neither form was reported as declared") } if got := layer.Undeclared(rows); len(got) != 1 || got[0] != "silent" { t.Fatalf("Undeclared = %v; want [silent]", got) } } // A single well-formed declaration must not be reported as disagreeing with // itself — flex-auth is exactly this shape. func TestSurveySingleFormIsNotDisagreement(t *testing.T) { root := t.TempDir() writeRepo(t, root, "solo", "Engine", "") rows, _ := layer.SurveyDeclarations(root, []string{"solo"}) if rows[0].SelfDisagrees() { t.Fatal("a repository with only INTENT.md was reported as self-disagreeing") } if !rows[0].Intent.InVocabulary { t.Fatal(`"Engine" was rejected from the §3 vocabulary`) } }