From f95a113fce96a755adb0c8eac04c04548717334d Mon Sep 17 00:00:00 2001 From: Willie Date: Wed, 8 Apr 2026 17:43:46 +0800 Subject: [PATCH] test: migrate all test metadata to centralized store format Replace .skillshare-meta.json sidecar writes with centralized .metadata.json store writes across all test files. Use install.MetadataFileName constant instead of hardcoded strings. Updated: audit_test, doctor_test, gitignore_project_test, install_online_test, list_project_test, sync_symlinked_dir_test, uninstall_project_test, uninstall_test, uninstall_trash_test, audit_output_online_test, audit_scan_skill_test, analyzer_metadata_test --- internal/audit/analyzer_metadata_test.go | 30 +++++++++----- internal/audit/audit_scan_skill_test.go | 26 ++++++++---- tests/integration/audit_output_online_test.go | 41 ++++++------------- tests/integration/audit_test.go | 34 ++++++++++++--- tests/integration/doctor_test.go | 16 ++++++-- tests/integration/gitignore_project_test.go | 12 +++--- tests/integration/install_online_test.go | 23 ++++++----- tests/integration/list_project_test.go | 15 +++---- tests/integration/sync_symlinked_dir_test.go | 11 ++--- tests/integration/uninstall_project_test.go | 12 +++--- tests/integration/uninstall_test.go | 13 +++--- tests/integration/uninstall_trash_test.go | 19 ++++----- 12 files changed, 142 insertions(+), 110 deletions(-) diff --git a/internal/audit/analyzer_metadata_test.go b/internal/audit/analyzer_metadata_test.go index 53ec1146c..30c057297 100644 --- a/internal/audit/analyzer_metadata_test.go +++ b/internal/audit/analyzer_metadata_test.go @@ -246,19 +246,23 @@ func TestIsWellKnownOrg(t *testing.T) { } func TestMetadataAnalyzer_Integration(t *testing.T) { - // Create a temp skill directory with SKILL.md and .skillshare-meta.json - dir := t.TempDir() + // Create a nested skill directory: root/evil-skill/SKILL.md + // with centralized metadata at root/.metadata.json + root := t.TempDir() + dir := filepath.Join(root, "evil-skill") + os.MkdirAll(dir, 0755) - // Write SKILL.md claiming "from Acme Corp" skillContent := "---\nname: evil-skill\ndescription: Official formatter from Acme Corp\n---\n# Evil\n" if err := os.WriteFile(filepath.Join(dir, "SKILL.md"), []byte(skillContent), 0644); err != nil { t.Fatal(err) } - // Write meta pointing to a different org - meta := metaJSON{RepoURL: "https://github.com/evil-fork/skills.git"} - metaData, _ := json.Marshal(meta) - if err := os.WriteFile(filepath.Join(dir, ".skillshare-meta.json"), metaData, 0644); err != nil { + // Write centralized metadata in parent directory + store := metadataStoreJSON{Entries: map[string]metaJSON{ + "evil-skill": {RepoURL: "https://github.com/evil-fork/skills.git"}, + }} + storeData, _ := json.Marshal(store) + if err := os.WriteFile(filepath.Join(root, metadataFileName), storeData, 0644); err != nil { t.Fatal(err) } @@ -320,14 +324,18 @@ func TestMetadataAnalyzer_NoMeta(t *testing.T) { } func TestMetadataAnalyzer_DisabledRules(t *testing.T) { - dir := t.TempDir() + root := t.TempDir() + dir := filepath.Join(root, "test-skill") + os.MkdirAll(dir, 0755) if err := os.WriteFile(filepath.Join(dir, "SKILL.md"), []byte("---\nname: test\ndescription: Official tool from Acme Corp\n---\n"), 0644); err != nil { t.Fatal(err) } - meta := metaJSON{RepoURL: "https://github.com/evil-fork/skills.git"} - metaData, _ := json.Marshal(meta) - if err := os.WriteFile(filepath.Join(dir, ".skillshare-meta.json"), metaData, 0644); err != nil { + store := metadataStoreJSON{Entries: map[string]metaJSON{ + "test-skill": {RepoURL: "https://github.com/evil-fork/skills.git"}, + }} + storeData, _ := json.Marshal(store) + if err := os.WriteFile(filepath.Join(root, metadataFileName), storeData, 0644); err != nil { t.Fatal(err) } diff --git a/internal/audit/audit_scan_skill_test.go b/internal/audit/audit_scan_skill_test.go index cf565b4b4..9d2295297 100644 --- a/internal/audit/audit_scan_skill_test.go +++ b/internal/audit/audit_scan_skill_test.go @@ -731,19 +731,29 @@ func sha256hex(data []byte) string { } // helper: write meta with file_hashes -func writeMetaWithHashes(t *testing.T, dir string, hashes map[string]string) { +// writeMetaWithHashes writes file hashes into the centralized .metadata.json +// store in the parent directory of skillDir (matching the production layout). +func writeMetaWithHashes(t *testing.T, skillDir string, hashes map[string]string) { t.Helper() - meta := struct { + skillName := filepath.Base(skillDir) + parentDir := filepath.Dir(skillDir) + + type entry struct { Source string `json:"source"` Type string `json:"type"` FileHashes map[string]string `json:"file_hashes"` - }{ - Source: "test", - Type: "local", - FileHashes: hashes, } - data, _ := json.Marshal(meta) - os.WriteFile(filepath.Join(dir, ".skillshare-meta.json"), data, 0644) + store := struct { + Version int `json:"version"` + Entries map[string]entry `json:"entries"` + }{ + Version: 1, + Entries: map[string]entry{ + skillName: {Source: "test", Type: "local", FileHashes: hashes}, + }, + } + data, _ := json.Marshal(store) + os.WriteFile(filepath.Join(parentDir, metadataFileName), data, 0644) } func TestScanSkill_ContentTampered(t *testing.T) { diff --git a/tests/integration/audit_output_online_test.go b/tests/integration/audit_output_online_test.go index f8585a1e5..328a8078f 100644 --- a/tests/integration/audit_output_online_test.go +++ b/tests/integration/audit_output_online_test.go @@ -3,12 +3,11 @@ package integration import ( - "encoding/json" - "os" "path/filepath" "strings" "testing" + "skillshare/internal/install" "skillshare/internal/testutil" ) @@ -83,39 +82,23 @@ func TestUpdateAll_AuditOutputParity_Antigravity(t *testing.T) { updateResult.AssertOutputNotContains(t, "Blocked / Rolled Back") } -// invalidateOneSkillMeta finds the first skill with a .skillshare-meta.json file -// and sets its "version" to a stale value, forcing the next update to re-install it. +// invalidateOneSkillMeta finds the first skill with metadata in the centralized +// store and sets its "version" to a stale value, forcing the next update to re-install it. func invalidateOneSkillMeta(t *testing.T, skillsDir string) { t.Helper() - entries, err := os.ReadDir(skillsDir) - if err != nil { - t.Fatalf("cannot read skills dir: %v", err) - } - - for _, e := range entries { - if !e.IsDir() { + store := install.LoadMetadataOrNew(skillsDir) + for _, name := range store.List() { + entry := store.Get(name) + if entry == nil || entry.Source == "" { continue } - metaPath := filepath.Join(skillsDir, e.Name(), ".skillshare-meta.json") - data, err := os.ReadFile(metaPath) - if err != nil { - continue + entry.Version = "stale" + entry.TreeHash = "" + if err := store.Save(skillsDir); err != nil { + t.Fatalf("save store: %v", err) } - var meta map[string]any - if err := json.Unmarshal(data, &meta); err != nil { - continue - } - meta["version"] = "stale" - meta["tree_hash"] = "" // also clear tree hash so subdir fallback won't match - out, err := json.MarshalIndent(meta, "", " ") - if err != nil { - t.Fatalf("marshal meta: %v", err) - } - if err := os.WriteFile(metaPath, out, 0644); err != nil { - t.Fatalf("write meta: %v", err) - } - t.Logf("invalidated metadata for skill %q to force re-install", e.Name()) + t.Logf("invalidated metadata for skill %q to force re-install", name) return } diff --git a/tests/integration/audit_test.go b/tests/integration/audit_test.go index c0332460d..f344847e7 100644 --- a/tests/integration/audit_test.go +++ b/tests/integration/audit_test.go @@ -13,6 +13,7 @@ import ( "strings" "testing" + "skillshare/internal/install" "skillshare/internal/testutil" ) @@ -875,19 +876,40 @@ func sha256Hex(data []byte) string { return hex.EncodeToString(h[:]) } -// writeMetaJSON writes a .skillshare-meta.json with the given file_hashes into dir. -func writeMetaJSON(t *testing.T, dir string, hashes map[string]string) { +// writeMetaJSON writes file_hashes for a skill into the centralized .metadata.json +// store in the parent directory of skillDir. +func writeMetaJSON(t *testing.T, skillDir string, hashes map[string]string) { t.Helper() - meta := map[string]any{ + skillName := filepath.Base(skillDir) + parentDir := filepath.Dir(skillDir) + + entry := map[string]any{ "source": "test", "type": "local", "installed_at": "2026-01-01T00:00:00Z", } if hashes != nil { - meta["file_hashes"] = hashes + entry["file_hashes"] = hashes } - data, _ := json.Marshal(meta) - if err := os.WriteFile(filepath.Join(dir, ".skillshare-meta.json"), data, 0644); err != nil { + store := map[string]any{ + "version": 1, + "entries": map[string]any{skillName: entry}, + } + + // Merge with existing store if present + existingData, err := os.ReadFile(filepath.Join(parentDir, install.MetadataFileName)) + if err == nil { + var existing map[string]any + if json.Unmarshal(existingData, &existing) == nil { + if entries, ok := existing["entries"].(map[string]any); ok { + entries[skillName] = entry + store = existing + } + } + } + + data, _ := json.Marshal(store) + if err := os.WriteFile(filepath.Join(parentDir, install.MetadataFileName), data, 0644); err != nil { t.Fatalf("writeMetaJSON: %v", err) } } diff --git a/tests/integration/doctor_test.go b/tests/integration/doctor_test.go index 42c2326f9..85cbdfd25 100644 --- a/tests/integration/doctor_test.go +++ b/tests/integration/doctor_test.go @@ -10,6 +10,7 @@ import ( "strings" "testing" + "skillshare/internal/install" "skillshare/internal/testutil" ) @@ -19,9 +20,12 @@ func TestDoctor_AllGood_PassesAll(t *testing.T) { sb.CreateSkill("skill1", map[string]string{ "SKILL.md": "# Skill 1", - // Include meta with correct file hash so integrity check passes - ".skillshare-meta.json": `{"source":"test","type":"local","installed_at":"2026-01-01T00:00:00Z","file_hashes":{"SKILL.md":"sha256:c90671f17f3b99f87d8fe1a542ee2d6829d2b2cfb7684d298e44c7591d8b0712"}}`, }) + + // Write metadata to centralized store with correct file hash so integrity check passes + metaStore := `{"version":1,"entries":{"skill1":{"source":"test","type":"local","installed_at":"2026-01-01T00:00:00Z","file_hashes":{"SKILL.md":"sha256:c90671f17f3b99f87d8fe1a542ee2d6829d2b2cfb7684d298e44c7591d8b0712"}}}}` + os.WriteFile(filepath.Join(sb.SourcePath, install.MetadataFileName), []byte(metaStore), 0644) + targetPath := sb.CreateTarget("claude") // Initialize git and commit to avoid warnings @@ -551,9 +555,13 @@ func TestDoctor_JSON_AllGood(t *testing.T) { defer sb.Cleanup() sb.CreateSkill("skill1", map[string]string{ - "SKILL.md": "# Skill 1", - ".skillshare-meta.json": `{"source":"test","type":"local","installed_at":"2026-01-01T00:00:00Z","file_hashes":{"SKILL.md":"sha256:c90671f17f3b99f87d8fe1a542ee2d6829d2b2cfb7684d298e44c7591d8b0712"}}`, + "SKILL.md": "# Skill 1", }) + + // Write metadata to centralized store with correct file hash so integrity check passes + metaStore := `{"version":1,"entries":{"skill1":{"source":"test","type":"local","installed_at":"2026-01-01T00:00:00Z","file_hashes":{"SKILL.md":"sha256:c90671f17f3b99f87d8fe1a542ee2d6829d2b2cfb7684d298e44c7591d8b0712"}}}}` + os.WriteFile(filepath.Join(sb.SourcePath, install.MetadataFileName), []byte(metaStore), 0644) + targetPath := sb.CreateTarget("claude") // Initialize git and commit to avoid warnings diff --git a/tests/integration/gitignore_project_test.go b/tests/integration/gitignore_project_test.go index ab7dd0d4c..fa7d382f5 100644 --- a/tests/integration/gitignore_project_test.go +++ b/tests/integration/gitignore_project_test.go @@ -3,12 +3,12 @@ package integration import ( - "encoding/json" "os" "path/filepath" "strings" "testing" + "skillshare/internal/install" "skillshare/internal/testutil" ) @@ -37,13 +37,13 @@ func TestGitignoreProject_UninstallRemovesEntry(t *testing.T) { defer sb.Cleanup() projectRoot := sb.SetupProjectDir("claude") - // Create remote skill with meta - skillDir := sb.CreateProjectSkill(projectRoot, "removable", map[string]string{ + // Create remote skill with meta in centralized store + sb.CreateProjectSkill(projectRoot, "removable", map[string]string{ "SKILL.md": "# Removable", }) - meta := map[string]interface{}{"source": "org/removable", "type": "github"} - metaJSON, _ := json.Marshal(meta) - os.WriteFile(filepath.Join(skillDir, ".skillshare-meta.json"), metaJSON, 0644) + skillsDir := filepath.Join(projectRoot, ".skillshare", "skills") + metaStore := `{"version":1,"entries":{"removable":{"source":"org/removable","type":"github"}}}` + os.WriteFile(filepath.Join(skillsDir, install.MetadataFileName), []byte(metaStore), 0644) // Write gitignore with the entry sb.WriteFile(filepath.Join(projectRoot, ".skillshare", ".gitignore"), diff --git a/tests/integration/install_online_test.go b/tests/integration/install_online_test.go index 72a50ab25..a1afec190 100644 --- a/tests/integration/install_online_test.go +++ b/tests/integration/install_online_test.go @@ -5,9 +5,9 @@ package integration import ( "os" "path/filepath" - "strings" "testing" + "skillshare/internal/install" "skillshare/internal/testutil" ) @@ -102,16 +102,19 @@ targets: {} t.Fatalf("did not expect .git directory for subdir API install") } - metaPath := filepath.Join(skillDir, ".skillshare-meta.json") - metaRaw, err := os.ReadFile(metaPath) - if err != nil { - t.Fatalf("failed to read metadata: %v", err) + // Verify metadata in centralized .metadata.json store + store, storeErr := install.LoadMetadata(sb.SourcePath) + if storeErr != nil { + t.Fatalf("failed to load metadata store: %v", storeErr) } - meta := string(metaRaw) - if !strings.Contains(meta, "\"source\": \"github.com/majiayu000/claude-skill-registry/skills/documents/atlassian-search\"") { - t.Fatalf("expected metadata source to preserve subdir source, got: %s", meta) + entry := store.Get("atlassian-search") + if entry == nil { + t.Fatal("expected metadata entry for atlassian-search in centralized store") } - if !strings.Contains(meta, "\"subdir\": \"skills/documents/atlassian-search\"") { - t.Fatalf("expected metadata subdir to match install path, got: %s", meta) + if entry.Source != "github.com/majiayu000/claude-skill-registry/skills/documents/atlassian-search" { + t.Fatalf("expected source to preserve subdir, got: %s", entry.Source) + } + if entry.Subdir != "skills/documents/atlassian-search" { + t.Fatalf("expected subdir to match install path, got: %s", entry.Subdir) } } diff --git a/tests/integration/list_project_test.go b/tests/integration/list_project_test.go index 59a842dcb..eb89092dd 100644 --- a/tests/integration/list_project_test.go +++ b/tests/integration/list_project_test.go @@ -3,12 +3,12 @@ package integration import ( - "encoding/json" "os" "path/filepath" "strings" "testing" + "skillshare/internal/install" "skillshare/internal/testutil" ) @@ -22,16 +22,13 @@ func TestListProject_ShowsLocalAndRemote(t *testing.T) { "SKILL.md": "# Local", }) - // Remote skill (with meta) - skillDir := sb.CreateProjectSkill(projectRoot, "remote-skill", map[string]string{ + // Remote skill (with meta in centralized store) + sb.CreateProjectSkill(projectRoot, "remote-skill", map[string]string{ "SKILL.md": "# Remote", }) - meta := map[string]interface{}{ - "source": "someone/skills/remote-skill", - "type": "github", - } - metaJSON, _ := json.Marshal(meta) - os.WriteFile(filepath.Join(skillDir, ".skillshare-meta.json"), metaJSON, 0644) + skillsDir := filepath.Join(projectRoot, ".skillshare", "skills") + metaStore := `{"version":1,"entries":{"remote-skill":{"source":"someone/skills/remote-skill","type":"github"}}}` + os.WriteFile(filepath.Join(skillsDir, install.MetadataFileName), []byte(metaStore), 0644) result := sb.RunCLIInDir(projectRoot, "list", "-p") result.AssertSuccess(t) diff --git a/tests/integration/sync_symlinked_dir_test.go b/tests/integration/sync_symlinked_dir_test.go index 3d41057af..898283deb 100644 --- a/tests/integration/sync_symlinked_dir_test.go +++ b/tests/integration/sync_symlinked_dir_test.go @@ -7,6 +7,7 @@ import ( "path/filepath" "testing" + "skillshare/internal/install" "skillshare/internal/testutil" ) @@ -334,8 +335,8 @@ func TestUpdateGroup_ExternalSymlinkRejected(t *testing.T) { os.MkdirAll(filepath.Join(externalDir, "victim"), 0755) os.WriteFile(filepath.Join(externalDir, "victim", "SKILL.md"), []byte("---\nname: victim\n---\n# Victim"), 0644) - os.WriteFile(filepath.Join(externalDir, "victim", ".skillshare-meta.json"), - []byte(`{"source":"github.com/example/victim","installed_at":"2025-01-01T00:00:00Z"}`), 0644) + os.WriteFile(filepath.Join(externalDir, install.MetadataFileName), + []byte(`{"version":1,"entries":{"victim":{"source":"github.com/example/victim","installed_at":"2025-01-01T00:00:00Z"}}}`), 0644) // Symlink a group inside source to the external location os.Symlink(externalDir, filepath.Join(sb.SourcePath, "evil-group")) @@ -356,13 +357,13 @@ func TestUpdateAll_SymlinkedSource(t *testing.T) { realSource := filepath.Join(sb.Root, "dotfiles", "skills") os.MkdirAll(realSource, 0755) - // Create a skill with metadata + // Create a skill with metadata in centralized store skillDir := filepath.Join(realSource, "remote-skill") os.MkdirAll(skillDir, 0755) os.WriteFile(filepath.Join(skillDir, "SKILL.md"), []byte("---\nname: remote-skill\n---\n# Remote"), 0644) - os.WriteFile(filepath.Join(skillDir, ".skillshare-meta.json"), - []byte(`{"source":"github.com/example/remote","installed_at":"2025-01-01T00:00:00Z"}`), 0644) + os.WriteFile(filepath.Join(realSource, install.MetadataFileName), + []byte(`{"version":1,"entries":{"remote-skill":{"source":"github.com/example/remote","installed_at":"2025-01-01T00:00:00Z"}}}`), 0644) os.RemoveAll(sb.SourcePath) if err := os.Symlink(realSource, sb.SourcePath); err != nil { diff --git a/tests/integration/uninstall_project_test.go b/tests/integration/uninstall_project_test.go index 0a805eb5b..f6240cdb0 100644 --- a/tests/integration/uninstall_project_test.go +++ b/tests/integration/uninstall_project_test.go @@ -3,7 +3,6 @@ package integration import ( - "encoding/json" "os" "path/filepath" "testing" @@ -47,13 +46,14 @@ func TestUninstallProject_UpdatesConfig(t *testing.T) { defer sb.Cleanup() projectRoot := sb.SetupProjectDir("claude") - // Create remote skill with meta - skillDir := sb.CreateProjectSkill(projectRoot, "remote", map[string]string{ + // Create remote skill with meta in centralized store + sb.CreateProjectSkill(projectRoot, "remote", map[string]string{ "SKILL.md": "# Remote", }) - meta := map[string]interface{}{"source": "org/skills/remote", "type": "github"} - metaJSON, _ := json.Marshal(meta) - os.WriteFile(filepath.Join(skillDir, ".skillshare-meta.json"), metaJSON, 0644) + skillsDir := filepath.Join(projectRoot, ".skillshare", "skills") + metaStore := install.NewMetadataStore() + metaStore.Set("remote", &install.MetadataEntry{Source: "org/skills/remote", Type: "github"}) + metaStore.Save(skillsDir) // Write config and registry with the skill sb.WriteProjectConfig(projectRoot, `targets: diff --git a/tests/integration/uninstall_test.go b/tests/integration/uninstall_test.go index ffb39d4b5..2714b525f 100644 --- a/tests/integration/uninstall_test.go +++ b/tests/integration/uninstall_test.go @@ -172,15 +172,16 @@ func TestUninstall_ShowsMetadata(t *testing.T) { sb := testutil.NewSandbox(t) defer sb.Cleanup() - // Create skill with metadata (simulating installed skill) + // Create skill with metadata in centralized store (simulating installed skill) sb.CreateSkill("meta-skill", map[string]string{ "SKILL.md": "# Meta Skill", - ".skillshare-meta.json": `{ - "source": "github.com/user/repo", - "type": "github", - "installed_at": "2024-01-15T10:30:00Z" -}`, }) + metaStore := install.NewMetadataStore() + metaStore.Set("meta-skill", &install.MetadataEntry{ + Source: "github.com/user/repo", + Type: "github", + }) + metaStore.Save(sb.SourcePath) sb.WriteConfig(`source: ` + sb.SourcePath + ` targets: {} diff --git a/tests/integration/uninstall_trash_test.go b/tests/integration/uninstall_trash_test.go index ab1ba6397..d60861ae8 100644 --- a/tests/integration/uninstall_trash_test.go +++ b/tests/integration/uninstall_trash_test.go @@ -8,6 +8,7 @@ import ( "strings" "testing" + "skillshare/internal/install" "skillshare/internal/testutil" ) @@ -52,12 +53,11 @@ func TestUninstall_WithMeta_PrintsReinstallHint(t *testing.T) { sb.CreateSkill("remote-skill", map[string]string{ "SKILL.md": "# Remote Skill", - ".skillshare-meta.json": `{ - "source": "github.com/user/skills/remote-skill", - "type": "github", - "installed_at": "2026-01-15T10:30:00Z" -}`, }) + // Write metadata to centralized store + metaStore := `{"version":1,"entries":{"remote-skill":{"source":"github.com/user/skills/remote-skill","type":"github","installed_at":"2026-01-15T10:30:00Z"}}}` + os.WriteFile(filepath.Join(sb.SourcePath, install.MetadataFileName), []byte(metaStore), 0644) + sb.WriteConfig(`source: ` + sb.SourcePath + ` targets: {} `) @@ -94,12 +94,11 @@ func TestUninstall_DryRun_ShowsTrashPreview(t *testing.T) { sb.CreateSkill("preview-skill", map[string]string{ "SKILL.md": "# Preview", - ".skillshare-meta.json": `{ - "source": "github.com/org/repo/preview-skill", - "type": "github", - "installed_at": "2026-01-15T10:30:00Z" -}`, }) + // Write metadata to centralized store + metaStore := `{"version":1,"entries":{"preview-skill":{"source":"github.com/org/repo/preview-skill","type":"github","installed_at":"2026-01-15T10:30:00Z"}}}` + os.WriteFile(filepath.Join(sb.SourcePath, install.MetadataFileName), []byte(metaStore), 0644) + sb.WriteConfig(`source: ` + sb.SourcePath + ` targets: {} `)