Files
skillshare/internal/config/schema_test.go
T
Rusty ShacklefordandWillie 43059a1428 fix(opencode): preserve v2 hook and plugin directory packages (#915)
* fix(opencode): preserve v2 directory packages

Stage the reviewed issue 913 source for isolated fork validation. Preserve
package directories and manifests without executing managed packages or
installing their dependencies, and include receipt and localized UI fixes.

Reviewed local source: 15ac88e2f266571b8a6dc8d777302db0f4dcae44
Exact source tree: 45ebabfe2b

Static checks and independent inspection completed. Go, integration,
dashboard, rendering, and hosted runtime validation remain pending.
This fork staging commit is not an upstream publication or merge.

* test(opencode): align schema and snapshot assertions

Check the version-aware binding schema and verify native unregister retains the unchanged managed snapshot.

* fix(opencode): resolve Windows npm native launchers

Probe recognized native Opencode executables without invoking a batch shell; exercise the default version probe on hosted Windows.

* test(opencode): normalize Windows fixture line endings

Avoid doubling carriage returns when Git checks out the npm fixture with CRLF.

* fix(opencode): keep an undetected OpenCode version from blocking other agents

Review of the v2 directory package change found that one failed
`opencode --version` probe failed the whole hooks plan, so a machine
without the binary (a second machine sharing the config, CI, a
devcontainer) could no longer sync hooks to any agent, and doctor and
the dashboard preview failed with it.

Hooks:
- A probe that fails or reports an unknown major now skips only the
  OpenCode bindings with one warning per target. Outputs already owned
  there are parked, so they are neither rewritten nor removed while the
  layout is unknown. The probe runs once per plan, list and import.
- A `package` binding on OpenCode v1 is skipped with a warning instead
  of failing the plan.
- The v2 dependency warning is reported once, and only when a package
  file is written, so a synced hook no longer keeps doctor yellow.
- `hooks import` reports an unreadable file such as a symlink as that
  package's problem; other packages and flat files stay importable.
  The package validation problem is reported once instead of twice.
- Directories under `plugins/` are listed as unmanaged only on v2.

Plugins:
- The inventory leaves `plugins/skillshare-NAME` registrations to
  hooks, so a synced v2 hook is not offered as an importable plugin.

Dashboard: the new warnings are translated in every locale, a
`hook NAME:` prefix no longer defeats translation, the native preview
translates its per-file error, and the YAML editor documents `package`.

Docs: the English pages and the built-in skill describe the behavior
above; the ja, ko, zh-Hans and zh-Hant pages now carry the same
content as the English ones. The runbook drives the CLI with mdproof
assertions instead of prose expectations.

Refs #913.

---------

Co-authored-by: Willie <egg358358@gmail.com>
2026-11-16 01:50:14 +08:00

482 lines
15 KiB
Go

package config
import (
"encoding/json"
"os"
"path/filepath"
"reflect"
"slices"
"strings"
"testing"
)
func TestSave_IncludesSchemaComment(t *testing.T) {
tmp := t.TempDir()
cfgPath := filepath.Join(tmp, "config.yaml")
t.Setenv("SKILLSHARE_CONFIG", cfgPath)
cfg := &Config{
Source: "/tmp/skills",
Targets: map[string]TargetConfig{},
}
if err := cfg.Save(); err != nil {
t.Fatalf("Save failed: %v", err)
}
data, err := os.ReadFile(cfgPath)
if err != nil {
t.Fatalf("read config: %v", err)
}
firstLine := strings.SplitN(string(data), "\n", 2)[0]
want := "# yaml-language-server: $schema=" + GlobalSchemaURL
if firstLine != want {
t.Errorf("first line = %q, want %q", firstLine, want)
}
}
func TestProjectSave_IncludesSchemaComment(t *testing.T) {
root := t.TempDir()
cfg := &ProjectConfig{
Targets: []ProjectTargetEntry{{Name: "claude"}},
}
if err := cfg.Save(root); err != nil {
t.Fatalf("Save failed: %v", err)
}
path := ProjectConfigPath(root)
data, err := os.ReadFile(path)
if err != nil {
t.Fatalf("read project config: %v", err)
}
firstLine := strings.SplitN(string(data), "\n", 2)[0]
want := "# yaml-language-server: $schema=" + ProjectSchemaURL
if firstLine != want {
t.Errorf("first line = %q, want %q", firstLine, want)
}
}
func TestLoad_WithSchemaComment(t *testing.T) {
tmp := t.TempDir()
cfgPath := filepath.Join(tmp, "config.yaml")
t.Setenv("SKILLSHARE_CONFIG", cfgPath)
raw := "# yaml-language-server: $schema=" + GlobalSchemaURL + "\nsource: /tmp/skills\ntargets: {}\n"
if err := os.WriteFile(cfgPath, []byte(raw), 0644); err != nil {
t.Fatalf("write config: %v", err)
}
cfg, err := Load()
if err != nil {
t.Fatalf("Load failed: %v", err)
}
if cfg.Source != "/tmp/skills" {
t.Errorf("Source = %q, want /tmp/skills", cfg.Source)
}
}
func TestLoad_WithTargetNaming(t *testing.T) {
tmp := t.TempDir()
cfgPath := filepath.Join(tmp, "config.yaml")
t.Setenv("SKILLSHARE_CONFIG", cfgPath)
raw := "# yaml-language-server: $schema=" + GlobalSchemaURL + "\nsource: /tmp/skills\ntarget_naming: standard\ntargets:\n claude:\n skills:\n target_naming: flat\n"
if err := os.WriteFile(cfgPath, []byte(raw), 0644); err != nil {
t.Fatalf("write config: %v", err)
}
cfg, err := Load()
if err != nil {
t.Fatalf("Load failed: %v", err)
}
if cfg.TargetNaming != "standard" {
t.Fatalf("TargetNaming = %q, want standard", cfg.TargetNaming)
}
claude := cfg.Targets["claude"]
if got := claude.SkillsConfig().TargetNaming; got != "flat" {
t.Fatalf("target target_naming = %q, want flat", got)
}
}
func TestLoadProject_WithSchemaComment(t *testing.T) {
root := t.TempDir()
cfgPath := filepath.Join(root, ".skillshare", "config.yaml")
if err := os.MkdirAll(filepath.Dir(cfgPath), 0755); err != nil {
t.Fatalf("mkdir: %v", err)
}
raw := "# yaml-language-server: $schema=" + ProjectSchemaURL + "\ntargets:\n - claude\n"
if err := os.WriteFile(cfgPath, []byte(raw), 0644); err != nil {
t.Fatalf("write config: %v", err)
}
cfg, err := LoadProject(root)
if err != nil {
t.Fatalf("LoadProject failed: %v", err)
}
if len(cfg.Targets) != 1 || cfg.Targets[0].Name != "claude" {
t.Errorf("unexpected targets: %+v", cfg.Targets)
}
}
func TestLoadProject_WithTargetNaming(t *testing.T) {
root := t.TempDir()
cfgPath := filepath.Join(root, ".skillshare", "config.yaml")
if err := os.MkdirAll(filepath.Dir(cfgPath), 0755); err != nil {
t.Fatalf("mkdir: %v", err)
}
raw := "# yaml-language-server: $schema=" + ProjectSchemaURL + "\ntarget_naming: standard\ntargets:\n - name: claude\n skills:\n target_naming: flat\n"
if err := os.WriteFile(cfgPath, []byte(raw), 0644); err != nil {
t.Fatalf("write config: %v", err)
}
cfg, err := LoadProject(root)
if err != nil {
t.Fatalf("LoadProject failed: %v", err)
}
if cfg.TargetNaming != "standard" {
t.Fatalf("TargetNaming = %q, want standard", cfg.TargetNaming)
}
if got := cfg.Targets[0].SkillsConfig().TargetNaming; got != "flat" {
t.Fatalf("target target_naming = %q, want flat", got)
}
}
func TestSchemaFiles_ValidJSON(t *testing.T) {
// Find schema files relative to this test file's package.
// Schema files are at the repo root: schemas/*.json
root := findRepoRoot(t)
tests := []struct {
file string
wantTitle string
}{
{"schemas/config.schema.json", "Skillshare Global Configuration"},
{"schemas/project-config.schema.json", "Skillshare Project Configuration"},
{"schemas/mcp.schema.json", "Skillshare MCP Source"},
{"schemas/hooks.schema.json", "Skillshare Hooks"},
}
for _, tt := range tests {
t.Run(tt.file, func(t *testing.T) {
path := filepath.Join(root, tt.file)
data, err := os.ReadFile(path)
if err != nil {
t.Fatalf("read schema file: %v", err)
}
var schema map[string]any
if err := json.Unmarshal(data, &schema); err != nil {
t.Fatalf("invalid JSON: %v", err)
}
if title, ok := schema["title"].(string); !ok || title != tt.wantTitle {
t.Errorf("title = %q, want %q", title, tt.wantTitle)
}
if _, ok := schema["$defs"]; !ok {
t.Error("schema should have $defs")
}
})
}
}
// Both schemas reject unknown keys, so every yaml tag the structs accept must
// be listed or editors flag valid configs.
func TestSchema_PropertiesCoverConfigStructs(t *testing.T) {
root := findRepoRoot(t)
for file, typ := range map[string]reflect.Type{
"schemas/config.schema.json": reflect.TypeOf(Config{}),
"schemas/project-config.schema.json": reflect.TypeOf(ProjectConfig{}),
} {
data, err := os.ReadFile(filepath.Join(root, file))
if err != nil {
t.Fatalf("read %s: %v", file, err)
}
var schema struct {
Properties map[string]any `json:"properties"`
}
if err := json.Unmarshal(data, &schema); err != nil {
t.Fatalf("%s: invalid JSON: %v", file, err)
}
for i := 0; i < typ.NumField(); i++ {
key, _, _ := strings.Cut(typ.Field(i).Tag.Get("yaml"), ",")
if key == "" || key == "-" {
continue
}
if _, ok := schema.Properties[key]; !ok {
t.Errorf("%s: missing property %q for %s.%s", file, key, typ.Name(), typ.Field(i).Name)
}
}
}
}
func TestSchema_ExtrasAndAgentsAllowExtension(t *testing.T) {
root := findRepoRoot(t)
for _, file := range []string{"schemas/config.schema.json", "schemas/project-config.schema.json"} {
data, err := os.ReadFile(filepath.Join(root, file))
if err != nil {
t.Fatalf("read %s: %v", file, err)
}
var schema map[string]any
if err := json.Unmarshal(data, &schema); err != nil {
t.Fatalf("%s: invalid JSON: %v", file, err)
}
owners := map[string]bool{}
collectExtensionOwners(schema, owners)
if !owners["extraTargetConfig"] {
t.Errorf("%s: extraTargetConfig missing 'extension' property", file)
}
if !owners["agents"] {
t.Errorf("%s: agents block missing 'extension' property", file)
}
}
}
func TestSchema_ExtrasAllowSingleFile(t *testing.T) {
root := findRepoRoot(t)
for _, file := range []string{"schemas/config.schema.json", "schemas/project-config.schema.json"} {
data, err := os.ReadFile(filepath.Join(root, file))
if err != nil {
t.Fatalf("read %s: %v", file, err)
}
var schema struct {
Defs map[string]struct {
Properties map[string]struct {
Enum []string `json:"enum"`
} `json:"properties"`
} `json:"$defs"`
}
if err := json.Unmarshal(data, &schema); err != nil {
t.Fatalf("%s: invalid JSON: %v", file, err)
}
if _, ok := schema.Defs["extraConfig"].Properties["file"]; !ok {
t.Errorf("%s: extraConfig missing 'file' property", file)
}
target := schema.Defs["extraTargetConfig"].Properties
if _, ok := target["as"]; !ok {
t.Errorf("%s: extraTargetConfig missing 'as' property", file)
}
if !slices.Contains(target["mode"].Enum, "import") {
t.Errorf("%s: extraTargetConfig mode enum missing 'import': %v", file, target["mode"].Enum)
}
}
}
func TestSchema_TargetsAllowInstructions(t *testing.T) {
root := findRepoRoot(t)
cases := map[string]func(defs map[string]any) any{
"schemas/config.schema.json": func(defs map[string]any) any {
return defs["targetConfig"]
},
"schemas/project-config.schema.json": func(defs map[string]any) any {
return defs["projectTargetEntry"].(map[string]any)["oneOf"].([]any)[1]
},
}
for file, target := range cases {
data, err := os.ReadFile(filepath.Join(root, file))
if err != nil {
t.Fatalf("read %s: %v", file, err)
}
var schema map[string]any
if err := json.Unmarshal(data, &schema); err != nil {
t.Fatalf("%s: invalid JSON: %v", file, err)
}
props := target(schema["$defs"].(map[string]any)).(map[string]any)["properties"].(map[string]any)
in, ok := props["instructions"].(map[string]any)
if !ok {
t.Fatalf("%s: target missing 'instructions' property", file)
}
inProps := in["properties"].(map[string]any)
for _, key := range []string{"path", "import"} {
if _, ok := inProps[key]; !ok {
t.Errorf("%s: instructions missing %q", file, key)
}
}
}
}
func TestSchema_TargetSkillsAllowEnabled(t *testing.T) {
root := findRepoRoot(t)
cases := map[string]func(defs map[string]any) any{
"schemas/config.schema.json": func(defs map[string]any) any {
return defs["targetConfig"]
},
"schemas/project-config.schema.json": func(defs map[string]any) any {
return defs["projectTargetEntry"].(map[string]any)["oneOf"].([]any)[1]
},
}
for file, target := range cases {
data, err := os.ReadFile(filepath.Join(root, file))
if err != nil {
t.Fatalf("read %s: %v", file, err)
}
var schema map[string]any
if err := json.Unmarshal(data, &schema); err != nil {
t.Fatalf("%s: invalid JSON: %v", file, err)
}
props := target(schema["$defs"].(map[string]any)).(map[string]any)["properties"].(map[string]any)
skills := props["skills"].(map[string]any)["properties"].(map[string]any)
if _, ok := skills["enabled"]; !ok {
t.Errorf("%s: skills missing 'enabled'", file)
}
agents := props["agents"].(map[string]any)["properties"].(map[string]any)
if _, ok := agents["enabled"]; ok {
t.Errorf("%s: agents must not accept 'enabled'", file)
}
}
}
// collectExtensionOwners records the key of every object whose "properties"
// includes an "extension" field, so schema tests can assert which blocks accept
// an extension transform.
func collectExtensionOwners(node any, owners map[string]bool) {
switch n := node.(type) {
case map[string]any:
for k, v := range n {
if vm, ok := v.(map[string]any); ok {
if props, ok := vm["properties"].(map[string]any); ok {
if _, has := props["extension"]; has {
owners[k] = true
}
}
}
collectExtensionOwners(v, owners)
}
case []any:
for _, item := range n {
collectExtensionOwners(item, owners)
}
}
}
// findRepoRoot walks up from the current working directory to find go.mod.
func findRepoRoot(t *testing.T) string {
t.Helper()
dir, err := os.Getwd()
if err != nil {
t.Fatalf("getwd: %v", err)
}
for {
if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil {
return dir
}
parent := filepath.Dir(dir)
if parent == dir {
t.Fatal("could not find repo root (go.mod)")
}
dir = parent
}
}
// The schema accepts only option values sync can write back unchanged, as LoadSource does.
func TestSchema_MCPOptionValuesMatchRuntime(t *testing.T) {
data, err := os.ReadFile("../../schemas/mcp.schema.json")
if err != nil {
t.Fatal(err)
}
var schema map[string]any
if err := json.Unmarshal(data, &schema); err != nil {
t.Fatal(err)
}
defs := schema["$defs"].(map[string]any)
options := defs["server"].(map[string]any)["properties"].(map[string]any)["options"].(map[string]any)
if options["additionalProperties"].(map[string]any)["additionalProperties"].(map[string]any)["$ref"] != "#/$defs/optionValue" {
t.Fatalf("option fields unconstrained: %v", options["additionalProperties"])
}
scalar, _ := json.Marshal(defs["optionScalar"])
value, _ := json.Marshal(defs["optionValue"])
if string(scalar) != `{"type":["string","number","boolean"]}` || !strings.Contains(string(value), `"items":{"$ref":"#/$defs/optionScalar"}`) {
t.Fatalf("option value allows null or nested lists: %s %s", scalar, value)
}
}
func TestSchema_TargetsAllowFiles(t *testing.T) {
root := findRepoRoot(t)
cases := map[string]func(defs map[string]any) any{
"schemas/config.schema.json": func(defs map[string]any) any {
return defs["targetConfig"]
},
"schemas/project-config.schema.json": func(defs map[string]any) any {
return defs["projectTargetEntry"].(map[string]any)["oneOf"].([]any)[1]
},
}
for file, target := range cases {
data, err := os.ReadFile(filepath.Join(root, file))
if err != nil {
t.Fatalf("read %s: %v", file, err)
}
var schema map[string]any
if err := json.Unmarshal(data, &schema); err != nil {
t.Fatalf("%s: invalid JSON: %v", file, err)
}
props := target(schema["$defs"].(map[string]any)).(map[string]any)["properties"].(map[string]any)
if files, ok := props["files"].(map[string]any); !ok || files["type"] != "array" {
t.Errorf("%s: target missing 'files' array property", file)
}
}
}
func TestSchema_HooksAllowAccountBindings(t *testing.T) {
data, err := os.ReadFile("../../schemas/hooks.schema.json")
if err != nil {
t.Fatal(err)
}
var schema map[string]any
if err := json.Unmarshal(data, &schema); err != nil {
t.Fatal(err)
}
defs := schema["$defs"].(map[string]any)
gb, ok := defs["globalBindings"].(map[string]any)
if !ok {
t.Fatal("globalBindings missing")
}
pattern := `^(?!(claude|codex|gemini|qwen|copilot|cursor|droid|factory|antigravity|antigravity-cli|agy|pi|omp|amp|opencode|git)$)[A-Za-z0-9][A-Za-z0-9._-]*$`
patterns := gb["patternProperties"].(map[string]any)
if len(patterns) != 1 || patterns[pattern] == nil {
t.Fatalf("%v", patterns)
}
refs := patterns[pattern].(map[string]any)["anyOf"].([]any)
if len(refs) != 2 || refs[0].(map[string]any)["$ref"] != "#/$defs/commandBinding" || refs[1].(map[string]any)["$ref"] != "#/$defs/openCodeBinding" {
t.Fatalf("%v", refs)
}
global := defs["globalConfiguration"].(map[string]any)["properties"].(map[string]any)
if global["entries"].(map[string]any)["$ref"] != "#/$defs/globalEntries" {
t.Fatal("global entries strict")
}
project := global["projects"].(map[string]any)["additionalProperties"].(map[string]any)["properties"].(map[string]any)
if project["entries"].(map[string]any)["$ref"] != "#/$defs/entries" || schema["$ref"] != "#/$defs/globalEntry" {
t.Fatal("incorrect project or root scope")
}
}
func TestSchema_TargetConfigAllowsOpenCodeAgent(t *testing.T) {
root := findRepoRoot(t)
data, err := os.ReadFile(filepath.Join(root, "schemas", "config.schema.json"))
if err != nil {
t.Fatal(err)
}
var schema map[string]any
if err := json.Unmarshal(data, &schema); err != nil {
t.Fatal(err)
}
defs := schema["$defs"].(map[string]any)
agentProp := defs["targetConfig"].(map[string]any)["properties"].(map[string]any)["agent"].(map[string]any)
enums := agentProp["enum"].([]any)
var found bool
for _, e := range enums {
if e == "opencode" {
found = true
break
}
}
if !found {
t.Fatalf("targetConfig.agent enum missing opencode: %v", enums)
}
}