package author import ( "context" "encoding/json" "git.a-wan.net/alan/pipelines/internal/draft" "os" "path/filepath" "strings" "testing" ) func TestInstalledSkillUpdatesWithoutOverwritingUserFiles(t *testing.T) { t.Setenv("XDG_CONFIG_HOME", t.TempDir()) dir, err := InstallSkill() if err != nil { t.Fatal(err) } if _, err = InstallSkill(); err != nil { t.Fatal("repeat install", err) } // Simulate an older package-owned file: update must refresh it, not refuse it. old := []byte("old packaged skill") markerData, _ := os.ReadFile(filepath.Join(dir, ".pipelines-managed.json")) var hashes map[string]string json.Unmarshal(markerData, &hashes) hashes["SKILL.md"] = digest(old) markerData, _ = json.Marshal(hashes) os.WriteFile(filepath.Join(dir, "SKILL.md"), old, 0600) os.WriteFile(filepath.Join(dir, ".pipelines-managed.json"), markerData, 0600) if _, err := InstallSkill(); err != nil { t.Fatal("skill update failed", err) } for _, file := range skillFiles { actual, err := os.ReadFile(filepath.Join(dir, file)) expected, _ := content.ReadFile("skill/" + file) if err != nil || string(actual) != string(expected) { t.Fatal(file, err) } } if err := os.WriteFile(filepath.Join(dir, "SKILL.md"), []byte("user modification"), 0600); err != nil { t.Fatal(err) } if _, err := InstallSkill(); err == nil { t.Fatal("overwrote user-modified skill") } data, _ := os.ReadFile(filepath.Join(dir, "SKILL.md")) if string(data) != "user modification" { t.Fatal("lost user changes") } } func TestCommandLaunchesOnceWithShortSkillReference(t *testing.T) { t.Setenv("XDG_CONFIG_HOME", t.TempDir()) t.Setenv("PIPELINES_DRAFT_DIR", t.TempDir()) if _, err := InstallSkill(); err != nil { t.Fatal(err) } d, err := draft.Create("", "source", "ws://local", true, 0) if err != nil { t.Fatal(err) } capture := filepath.Join(t.TempDir(), "calls") script := filepath.Join(t.TempDir(), "opencode") os.WriteFile(script, []byte("#!/bin/sh\nprintf '%s\\n' \"$@\" >> '"+capture+"'\n"), 0700) cmd, err := Command(context.Background(), script, d, "", "pipelines") if err != nil { t.Fatal(err) } if err := cmd.Run(); err != nil { t.Fatal(err) } data, _ := os.ReadFile(capture) text := string(data) if strings.Contains(text, "--session") || strings.Contains(text, "api\n") || strings.Contains(text, "--help") || !strings.Contains(text, "--prompt\n@pipelines\n") { t.Fatal("unexpected prelaunch calls", text) } if len(Prompt(d, "", "pipelines")) > 512 || !strings.Contains(text, d.Path+".json") { t.Fatal("prompt must reference context, not preload instructions") } } func TestPromptStartsWithIntentRatherThanInvestigation(t *testing.T) { for _, test := range []struct { name, request, want string createOnly bool baseVersion int }{ {"new", "", "What should this pipeline do?", true, 0}, {"edit", "", "What would you like to change in this pipeline?", false, 1}, {"whitespace", " \n\t", "What would you like to change in this pipeline?", false, 2}, {"request", "Notify on success or error", "briefly acknowledge the request", false, 1}, } { t.Run(test.name, func(t *testing.T) { d := draft.Draft{Path: "/private/drafts/workflow.md", Name: "Research a topic", CreateOnly: test.createOnly, BaseVersion: test.baseVersion} cli := "'/custom/bin/pipelines' --url 'ws://server:8081/connect'" prompt := Prompt(d, test.request, cli) if !strings.Contains(prompt, test.want) || !strings.Contains(prompt, "CLI: "+cli) || !strings.Contains(prompt, d.Path+".json") { t.Fatal("missing launch guidance", prompt) } if strings.TrimSpace(test.request) == "" { if !strings.Contains(prompt, "before reading files or running commands") || strings.Contains(prompt, "Request:") { t.Fatal("unspecified task invites investigation", prompt) } } else if !strings.Contains(prompt, "Request: "+test.request) || strings.Contains(prompt, "What would you like") { t.Fatal("supplied intent ignored", prompt) } }) } } func TestSkillUpgradeAddsSelfContainedNotificationReference(t *testing.T) { t.Setenv("XDG_CONFIG_HOME", t.TempDir()) dir, err := InstallSkill() if err != nil { t.Fatal(err) } marker := filepath.Join(dir, ".pipelines-managed.json") data, err := os.ReadFile(marker) if err != nil { t.Fatal(err) } var hashes map[string]string if err = json.Unmarshal(data, &hashes); err != nil { t.Fatal(err) } // A real old installation has only the original two managed files. delete(hashes, "references/notifications.md") data, _ = json.Marshal(hashes) if err = os.WriteFile(marker, data, 0600); err != nil { t.Fatal(err) } if err = os.Remove(filepath.Join(dir, "references/notifications.md")); err != nil { t.Fatal(err) } if _, err = InstallSkill(); err != nil { t.Fatal("old skill cannot upgrade", err) } data, err = os.ReadFile(filepath.Join(dir, "references/notifications.md")) if err != nil { t.Fatal(err) } for _, want := range []string{"on=failure", "on=success,failure", "destination=system", "cancellation", "output.markdown"} { if !strings.Contains(string(data), want) { t.Fatal("missing local recipe", want) } } data, err = os.ReadFile(marker) if err != nil { t.Fatal(err) } if err = json.Unmarshal(data, &hashes); err != nil || hashes["references/notifications.md"] == "" { t.Fatal("new reference not managed", err) } if err = os.WriteFile(filepath.Join(dir, "references/notifications.md"), []byte("user notes"), 0600); err != nil { t.Fatal(err) } if _, err = InstallSkill(); err == nil { t.Fatal("modified notification guide overwritten") } data, _ = os.ReadFile(filepath.Join(dir, "references/notifications.md")) if string(data) != "user notes" { t.Fatal("user notes lost") } }