|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +func anyContains(ss []string, sub string) bool { |
| 10 | + for _, s := range ss { |
| 11 | + if strings.Contains(s, sub) { |
| 12 | + return true |
| 13 | + } |
| 14 | + } |
| 15 | + return false |
| 16 | +} |
| 17 | + |
| 18 | +// TestValidateProfiles covers the data-model.md validation table (Spec 057): |
| 19 | +// fatal slug/reserved/duplicate rules and warn-skip for unknown/empty servers. |
| 20 | +func TestValidateProfiles(t *testing.T) { |
| 21 | + mk := func(profiles []ProfileConfig, servers ...string) *Config { |
| 22 | + var sc []*ServerConfig |
| 23 | + for _, s := range servers { |
| 24 | + sc = append(sc, &ServerConfig{Name: s}) |
| 25 | + } |
| 26 | + return &Config{Servers: sc, Profiles: profiles} |
| 27 | + } |
| 28 | + |
| 29 | + t.Run("valid profile passes with no error or warning", func(t *testing.T) { |
| 30 | + warnings, err := ValidateProfiles(mk([]ProfileConfig{{Name: "research", Servers: []string{"fs", "web"}}}, "fs", "web")) |
| 31 | + if err != nil { |
| 32 | + t.Fatalf("unexpected error: %v", err) |
| 33 | + } |
| 34 | + if len(warnings) != 0 { |
| 35 | + t.Errorf("expected no warnings, got %v", warnings) |
| 36 | + } |
| 37 | + }) |
| 38 | + |
| 39 | + fatal := []struct { |
| 40 | + name string |
| 41 | + profiles []ProfileConfig |
| 42 | + }{ |
| 43 | + {"uppercase slug", []ProfileConfig{{Name: "Research", Servers: []string{"fs"}}}}, |
| 44 | + {"leading dash", []ProfileConfig{{Name: "-x", Servers: []string{"fs"}}}}, |
| 45 | + {"contains space", []ProfileConfig{{Name: "a b", Servers: []string{"fs"}}}}, |
| 46 | + {"too long (64)", []ProfileConfig{{Name: strings.Repeat("a", 64), Servers: []string{"fs"}}}}, |
| 47 | + {"reserved all", []ProfileConfig{{Name: "all", Servers: []string{"fs"}}}}, |
| 48 | + {"reserved code", []ProfileConfig{{Name: "code", Servers: []string{"fs"}}}}, |
| 49 | + {"reserved call", []ProfileConfig{{Name: "call", Servers: []string{"fs"}}}}, |
| 50 | + {"reserved p", []ProfileConfig{{Name: "p", Servers: []string{"fs"}}}}, |
| 51 | + } |
| 52 | + for _, tc := range fatal { |
| 53 | + t.Run("fatal: "+tc.name, func(t *testing.T) { |
| 54 | + _, err := ValidateProfiles(mk(tc.profiles, "fs")) |
| 55 | + if err == nil { |
| 56 | + t.Errorf("expected fatal error for %s", tc.name) |
| 57 | + } |
| 58 | + }) |
| 59 | + } |
| 60 | + |
| 61 | + t.Run("boundary: 63-char slug is valid", func(t *testing.T) { |
| 62 | + _, err := ValidateProfiles(mk([]ProfileConfig{{Name: strings.Repeat("a", 63), Servers: []string{"fs"}}}, "fs")) |
| 63 | + if err != nil { |
| 64 | + t.Errorf("63-char slug must be valid, got %v", err) |
| 65 | + } |
| 66 | + }) |
| 67 | + |
| 68 | + t.Run("fatal: duplicate name names the slug", func(t *testing.T) { |
| 69 | + _, err := ValidateProfiles(mk([]ProfileConfig{ |
| 70 | + {Name: "dup", Servers: []string{"fs"}}, |
| 71 | + {Name: "dup", Servers: []string{"web"}}, |
| 72 | + }, "fs", "web")) |
| 73 | + if err == nil || !strings.Contains(err.Error(), "dup") { |
| 74 | + t.Errorf("expected duplicate-name error mentioning 'dup', got %v", err) |
| 75 | + } |
| 76 | + }) |
| 77 | + |
| 78 | + t.Run("warn-skip: unknown server warns, does not fail", func(t *testing.T) { |
| 79 | + warnings, err := ValidateProfiles(mk([]ProfileConfig{{Name: "x", Servers: []string{"fs", "ghost"}}}, "fs")) |
| 80 | + if err != nil { |
| 81 | + t.Fatalf("unknown server must warn-and-skip, not fail: %v", err) |
| 82 | + } |
| 83 | + if !anyContains(warnings, "ghost") { |
| 84 | + t.Errorf("expected a warning naming the unknown server 'ghost', got %v", warnings) |
| 85 | + } |
| 86 | + }) |
| 87 | + |
| 88 | + t.Run("warn: empty servers is a legal deny-all", func(t *testing.T) { |
| 89 | + warnings, err := ValidateProfiles(mk([]ProfileConfig{{Name: "locked", Servers: nil}})) |
| 90 | + if err != nil { |
| 91 | + t.Fatalf("empty servers is legal (deny-all): %v", err) |
| 92 | + } |
| 93 | + if !anyContains(warnings, "locked") { |
| 94 | + t.Errorf("expected a warning naming the empty profile 'locked', got %v", warnings) |
| 95 | + } |
| 96 | + }) |
| 97 | +} |
| 98 | + |
| 99 | +// TestProfilesRoundTrip covers SC-004: absent profiles serialize away (omitempty) |
| 100 | +// so existing configs are byte-identical; present profiles round-trip losslessly. |
| 101 | +func TestProfilesRoundTrip(t *testing.T) { |
| 102 | + noProfiles := &Config{Listen: "127.0.0.1:8080"} |
| 103 | + b, err := json.Marshal(noProfiles) |
| 104 | + if err != nil { |
| 105 | + t.Fatal(err) |
| 106 | + } |
| 107 | + if strings.Contains(string(b), "profiles") { |
| 108 | + t.Errorf("absent profiles must not appear in JSON (omitempty): %s", b) |
| 109 | + } |
| 110 | + |
| 111 | + withProfiles := &Config{Profiles: []ProfileConfig{{Name: "research", Servers: []string{"fs", "web"}}}} |
| 112 | + b2, err := json.Marshal(withProfiles) |
| 113 | + if err != nil { |
| 114 | + t.Fatal(err) |
| 115 | + } |
| 116 | + var back Config |
| 117 | + if err := json.Unmarshal(b2, &back); err != nil { |
| 118 | + t.Fatal(err) |
| 119 | + } |
| 120 | + if len(back.Profiles) != 1 || back.Profiles[0].Name != "research" || len(back.Profiles[0].Servers) != 2 { |
| 121 | + t.Errorf("profiles did not round-trip losslessly: %+v", back.Profiles) |
| 122 | + } |
| 123 | +} |
0 commit comments