| 1 | // Package manifest holds tests for mcp/manifest.json. It contains no |
| 2 | // production code - the manifest itself is the artifact, and these tests |
| 3 | // guard structural invariants the bundling pipeline depends on. |
| 4 | package manifest |
| 5 | |
| 6 | import ( |
| 7 | "encoding/json" |
| 8 | "os" |
| 9 | "path/filepath" |
| 10 | "runtime" |
| 11 | "strings" |
| 12 | "testing" |
| 13 | |
| 14 | "gopkg.in/yaml.v3" |
| 15 | ) |
| 16 | |
| 17 | // envBinding is a minimal subset of the MCPB v0.3 manifest just covering |
| 18 | // the fields these tests assert on. We deliberately do not depend on the |
| 19 | // printing-press internal/pipeline types (that's an internal/ package and |
| 20 | // not importable across modules) - the structural invariants below are |
| 21 | // what actually matter for Claude Desktop install correctness. |
| 22 | type manifestShape struct { |
| 23 | ManifestVersion string `json:"manifest_version"` |
| 24 | Name string `json:"name"` |
| 25 | Version string `json:"version"` |
| 26 | Server struct { |
| 27 | Type string `json:"type"` |
| 28 | EntryPoint string `json:"entry_point"` |
| 29 | MCPConfig struct { |
| 30 | Command string `json:"command"` |
| 31 | Env map[string]string `json:"env"` |
| 32 | } `json:"mcp_config"` |
| 33 | } `json:"server"` |
| 34 | UserConfig map[string]struct { |
| 35 | Type string `json:"type"` |
| 36 | Title string `json:"title"` |
| 37 | Description string `json:"description"` |
| 38 | Sensitive bool `json:"sensitive"` |
| 39 | Required bool `json:"required"` |
| 40 | } `json:"user_config"` |
| 41 | Compatibility struct { |
| 42 | ClaudeDesktop string `json:"claude_desktop"` |
| 43 | Platforms []string `json:"platforms"` |
| 44 | } `json:"compatibility"` |
| 45 | } |
| 46 | |
| 47 | // loadManifest reads mcp/manifest.json relative to this test file so the |
| 48 | // test passes regardless of where `go test` is invoked from. |
| 49 | func loadManifest(t *testing.T) manifestShape { |
| 50 | t.Helper() |
| 51 | _, thisFile, _, ok := runtime.Caller(0) |
| 52 | if !ok { |
| 53 | t.Fatal("runtime.Caller failed") |
| 54 | } |
| 55 | // manifest_test.go is at mcp/internal/manifest/; manifest.json at mcp/. |
| 56 | manifestPath := filepath.Join(filepath.Dir(thisFile), "..", "..", "manifest.json") |
| 57 | data, err := os.ReadFile(manifestPath) |
| 58 | if err != nil { |
| 59 | t.Fatalf("read manifest: %v", err) |
| 60 | } |
| 61 | var m manifestShape |
| 62 | if err := json.Unmarshal(data, &m); err != nil { |
| 63 | t.Fatalf("parse manifest: %v", err) |
| 64 | } |
| 65 | return m |
| 66 | } |
| 67 | |
| 68 | func TestManifestRequiredFields(t *testing.T) { |
| 69 | m := loadManifest(t) |
| 70 | if m.ManifestVersion != "0.3" { |
| 71 | t.Errorf("manifest_version = %q, want 0.3", m.ManifestVersion) |
| 72 | } |
| 73 | if m.Name != "last30days-pp-mcp" { |
| 74 | t.Errorf("name = %q, want last30days-pp-mcp", m.Name) |
| 75 | } |
| 76 | if m.Version == "" { |
| 77 | t.Error("version is empty") |
| 78 | } |
| 79 | if m.Server.Type != "binary" { |
| 80 | t.Errorf("server.type = %q, want binary", m.Server.Type) |
| 81 | } |
| 82 | if m.Server.EntryPoint != "bin/last30days-pp-mcp" { |
| 83 | t.Errorf("server.entry_point = %q, want bin/last30days-pp-mcp", m.Server.EntryPoint) |
| 84 | } |
| 85 | if m.Compatibility.ClaudeDesktop == "" { |
| 86 | t.Error("compatibility.claude_desktop is empty") |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | // TestEnvAndUserConfigCrossReference is the key invariant: every |
| 91 | // ${user_config.<key>} substitution in server.mcp_config.env must point |
| 92 | // at a real user_config entry, and every declared user_config must be |
| 93 | // wired to an env var. A typo on either side silently disables a credential |
| 94 | // at install time without the binary or Claude Desktop noticing. |
| 95 | func TestEnvAndUserConfigCrossReference(t *testing.T) { |
| 96 | m := loadManifest(t) |
| 97 | |
| 98 | if len(m.Server.MCPConfig.Env) == 0 { |
| 99 | t.Fatal("server.mcp_config.env is empty; expected user_config substitutions") |
| 100 | } |
| 101 | if len(m.UserConfig) == 0 { |
| 102 | t.Fatal("user_config is empty; expected per-key declarations") |
| 103 | } |
| 104 | |
| 105 | for envName, value := range m.Server.MCPConfig.Env { |
| 106 | key, ok := parseUserConfigRef(value) |
| 107 | if !ok { |
| 108 | t.Errorf("env[%s] = %q is not a ${user_config.<key>} reference", envName, value) |
| 109 | continue |
| 110 | } |
| 111 | if _, declared := m.UserConfig[key]; !declared { |
| 112 | t.Errorf("env[%s] references user_config[%q], which is not declared", envName, key) |
| 113 | } |
| 114 | // The user_config key must be the lowercased env var so Claude |
| 115 | // Desktop's substitution rule matches PP's emitted shape. |
| 116 | if got := strings.ToLower(envName); key != got { |
| 117 | t.Errorf("env[%s] -> user_config[%q]; convention requires user_config[%q]", envName, key, got) |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | envValues := make(map[string]bool, len(m.Server.MCPConfig.Env)) |
| 122 | for _, value := range m.Server.MCPConfig.Env { |
| 123 | if key, ok := parseUserConfigRef(value); ok { |
| 124 | envValues[key] = true |
| 125 | } |
| 126 | } |
| 127 | for key := range m.UserConfig { |
| 128 | if !envValues[key] { |
| 129 | t.Errorf("user_config[%q] is declared but never substituted into env", key) |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | func isCredentialTitle(title string) bool { |
| 135 | return strings.HasSuffix(title, "_KEY") || |
| 136 | strings.HasSuffix(title, "_TOKEN") || |
| 137 | strings.HasSuffix(title, "_PASSWORD") |
| 138 | } |
| 139 | |
| 140 | func TestUserConfigShape(t *testing.T) { |
| 141 | m := loadManifest(t) |
| 142 | for key, slot := range m.UserConfig { |
| 143 | if slot.Type != "string" { |
| 144 | t.Errorf("user_config[%q].type = %q, want string", key, slot.Type) |
| 145 | } |
| 146 | if slot.Title == "" { |
| 147 | t.Errorf("user_config[%q].title is empty", key) |
| 148 | } |
| 149 | if slot.Description == "" { |
| 150 | t.Errorf("user_config[%q].description is empty", key) |
| 151 | } |
| 152 | wantSensitive := isCredentialTitle(slot.Title) |
| 153 | if slot.Sensitive != wantSensitive { |
| 154 | if wantSensitive { |
| 155 | // API keys, tokens, and passwords must be flagged sensitive so |
| 156 | // Claude Desktop masks the input and prefers OS-keychain storage. |
| 157 | t.Errorf("user_config[%q].sensitive = false; want true for API credentials", key) |
| 158 | } else { |
| 159 | t.Errorf("user_config[%q].sensitive = true; want false for non-secret config", key) |
| 160 | } |
| 161 | } |
| 162 | if slot.Required { |
| 163 | // The engine degrades to web-only mode without keys, so no |
| 164 | // key is install-blocking. |
| 165 | t.Errorf("user_config[%q].required = true; engine degrades without keys, so all keys are optional", key) |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | func TestBlueskyHandleAndXBackendDeclared(t *testing.T) { |
| 171 | m := loadManifest(t) |
| 172 | cases := []struct { |
| 173 | key string |
| 174 | envName string |
| 175 | wantRef string |
| 176 | }{ |
| 177 | {"bsky_handle", "BSKY_HANDLE", "${user_config.bsky_handle}"}, |
| 178 | {"last30days_x_backend", "LAST30DAYS_X_BACKEND", "${user_config.last30days_x_backend}"}, |
| 179 | } |
| 180 | for _, tc := range cases { |
| 181 | slot, ok := m.UserConfig[tc.key] |
| 182 | if !ok { |
| 183 | t.Fatalf("user_config missing %q", tc.key) |
| 184 | } |
| 185 | if slot.Title != tc.envName { |
| 186 | t.Errorf("user_config[%q].title = %q, want %q", tc.key, slot.Title, tc.envName) |
| 187 | } |
| 188 | if slot.Type != "string" { |
| 189 | t.Errorf("user_config[%q].type = %q, want string", tc.key, slot.Type) |
| 190 | } |
| 191 | if slot.Sensitive { |
| 192 | t.Errorf("user_config[%q].sensitive = true, want false", tc.key) |
| 193 | } |
| 194 | if slot.Required { |
| 195 | t.Errorf("user_config[%q].required = true, want false", tc.key) |
| 196 | } |
| 197 | if got := m.Server.MCPConfig.Env[tc.envName]; got != tc.wantRef { |
| 198 | t.Errorf("env[%s] = %q, want %q", tc.envName, got, tc.wantRef) |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | func TestPlatformsMatchShippingMatrix(t *testing.T) { |
| 204 | m := loadManifest(t) |
| 205 | _, thisFile, _, ok := runtime.Caller(0) |
| 206 | if !ok { |
| 207 | t.Fatal("runtime.Caller failed") |
| 208 | } |
| 209 | workflowPath := filepath.Join(filepath.Dir(thisFile), "..", "..", "..", ".github", "workflows", "release.yml") |
| 210 | data, err := os.ReadFile(workflowPath) |
| 211 | if err != nil { |
| 212 | t.Fatalf("read release workflow: %v", err) |
| 213 | } |
| 214 | var workflow struct { |
| 215 | Jobs map[string]struct { |
| 216 | Strategy struct { |
| 217 | Matrix struct { |
| 218 | Include []struct { |
| 219 | GOOS string `yaml:"goos"` |
| 220 | } `yaml:"include"` |
| 221 | } `yaml:"matrix"` |
| 222 | } `yaml:"strategy"` |
| 223 | } `yaml:"jobs"` |
| 224 | } |
| 225 | if err := yaml.Unmarshal(data, &workflow); err != nil { |
| 226 | t.Fatalf("parse release workflow: %v", err) |
| 227 | } |
| 228 | entries := workflow.Jobs["build-mcpb"].Strategy.Matrix.Include |
| 229 | if len(entries) == 0 { |
| 230 | t.Fatal("release build-mcpb matrix has no shipping platforms") |
| 231 | } |
| 232 | platformNames := map[string]string{"darwin": "darwin", "linux": "linux", "windows": "win32"} |
| 233 | required := make(map[string]bool) |
| 234 | for _, entry := range entries { |
| 235 | platform, ok := platformNames[entry.GOOS] |
| 236 | if !ok { |
| 237 | t.Fatalf("release matrix GOOS %q has no manifest platform mapping", entry.GOOS) |
| 238 | } |
| 239 | required[platform] = false |
| 240 | } |
| 241 | for _, p := range m.Compatibility.Platforms { |
| 242 | if _, ok := required[p]; !ok { |
| 243 | t.Errorf("compatibility.platforms contains %q but the release matrix does not ship that platform; add it to the matrix or remove from the manifest", p) |
| 244 | continue |
| 245 | } |
| 246 | required[p] = true |
| 247 | } |
| 248 | for p, found := range required { |
| 249 | if !found { |
| 250 | t.Errorf("compatibility.platforms missing %q", p) |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | func parseUserConfigRef(value string) (string, bool) { |
| 256 | const prefix = "${user_config." |
| 257 | const suffix = "}" |
| 258 | if !strings.HasPrefix(value, prefix) || !strings.HasSuffix(value, suffix) { |
| 259 | return "", false |
| 260 | } |
| 261 | return value[len(prefix) : len(value)-len(suffix)], true |
| 262 | } |
| 263 |