package cli_test import ( "bytes" "context" "encoding/json" "fmt" "git.a-wan.net/alan/pipelines/internal/author" "net/http/httptest" "os" "path/filepath" "strings" "testing" "git.a-wan.net/alan/pipelines/internal/cli" "git.a-wan.net/alan/pipelines/internal/draft" "git.a-wan.net/alan/pipelines/internal/pipeline" "git.a-wan.net/alan/pipelines/internal/protocol" "git.a-wan.net/alan/pipelines/internal/server" ) func setup(t *testing.T) (func(...string) (string, error), *pipeline.Store, string) { t.Helper() ctx, cancel := context.WithCancel(context.Background()) store, err := pipeline.Open(filepath.Join(t.TempDir(), "cli.db")) if err != nil { t.Fatal(err) } handler := server.Handler(ctx, store) http := httptest.NewServer(handler) t.Cleanup(func() { cancel(); http.Close(); server.WaitRuns(handler); store.Close() }) url := "ws" + strings.TrimPrefix(http.URL, "http") + "/connect" t.Setenv("PIPELINES_DRAFT_DIR", t.TempDir()) // Isolate harness configuration: a developer's real Docker-linked profile must // not route debug/author commands through a different service or compare its // loopback port against this test's ephemeral server. A missing native fixture // keeps the host executable authoritative, matching unconfigured setups. t.Setenv("PIPELINES_HARNESS_CONFIG", filepath.Join(t.TempDir(), "harnesses.json")) return func(args ...string) (string, error) { if len(args) > 0 && (args[0] == "submit" || args[0] == "new" || args[0] == "edit") { args = append(args, "--yes") } var out, stderr bytes.Buffer app := cli.App{Out: &out, Err: &stderr, In: strings.NewReader("")} err := app.Run(ctx, append([]string{"--url", url}, args...)) return out.String(), err }, store, url } func TestCLIHelpDoesNotNeedServer(t *testing.T) { for _, args := range [][]string{{"help"}, {"--help"}, {"help", "edit"}, {"help", "start"}, {"run", "--help"}} { var out, stderr bytes.Buffer err := (cli.App{Out: &out, Err: &stderr}).Run(context.Background(), args) if err != nil { t.Fatal(err) } text := out.String() + stderr.String() if !strings.Contains(text, "pipelines") && !strings.Contains(text, "Usage") { t.Fatal("missing help", text) } } } func TestCLIStatusAndStatistics(t *testing.T) { t.Setenv("PIPELINES_NTFY_URL", "") run, _, _ := setup(t) output, err := run("status") if err != nil { t.Fatal(err) } for _, text := range []string{"Pipelines server", "ntfy", "not configured", "Port:"} { if !strings.Contains(output, text) { t.Fatal("missing CLI status", text, output) } } output, err = run("status", "--json") if err != nil { t.Fatal(err) } var status protocol.SystemStatus if err := json.Unmarshal([]byte(output), &status); err != nil || status.Server.Health != "healthy" || status.Server.Port == "" { t.Fatal(output, err) } output, err = run("stats", "--json") if err != nil { t.Fatal(err) } var usage protocol.UsageStats if err := json.Unmarshal([]byte(output), &usage); err != nil || usage.Total != 0 || len(usage.Days) != 14 { t.Fatal(output, err) } if _, err := run("status", "extra"); err == nil { t.Fatal("status accepted extra positional") } } func TestCLIDraftValidateSubmitAndImmutableHistory(t *testing.T) { run, store, _ := setup(t) output, err := run("new", "example", "--template", "task", "--json") if err != nil { t.Fatal(err) } var d draft.Draft if err = json.Unmarshal([]byte(output), &d); err != nil { t.Fatal(err) } items, _ := store.List(context.Background()) if len(items) != 0 { t.Fatal("draft seeded database") } if !d.CreateOnly { t.Fatal("new draft can overwrite") } if _, err = run("validate", d.Path, "--json"); err != nil { t.Fatal(err) } if _, err = run("submit", d.Path, "--json"); err != nil { t.Fatal(err) } if _, err = run("submit", d.Path); err == nil || !strings.Contains(err.Error(), "already exists") { t.Fatal("duplicate submitted", err) } if _, err = run("new", "example", "--json"); err == nil { t.Fatal("new accepted existing name") } output, err = run("edit", "example", "Add a review step", "--json") if err != nil { t.Fatal(err) } json.Unmarshal([]byte(output), &d) if d.BaseVersion != 1 || d.CreateOnly { t.Fatal("edit missing base version", d) } original, err := run("export", "example") if err != nil { t.Fatal(err) } changed := strings.Replace(original, "# stage planner", "# stage planner", 1) + "\n" os.WriteFile(d.Path, []byte(changed), 0600) if _, err = run("submit", d.Path, "--json"); err != nil { t.Fatal(err) } output, err = run("versions", "example", "--json") if err != nil { t.Fatal(err) } var versions []protocol.VersionSummary json.Unmarshal([]byte(output), &versions) if len(versions) != 2 { t.Fatal(versions) } old, err := run("export", "example", "--version", "1") if err != nil || old != original { t.Fatal("history rewritten", err) } os.WriteFile(d.Path, []byte(changed+"\n"), 0600) if _, err = run("submit", d.Path); err == nil || !strings.Contains(err.Error(), "conflict") { t.Fatal("stale draft accepted", err) } if _, err = run("submit", d.Path, "--force"); err != nil { t.Fatal(err) } os.WriteFile(d.Path, []byte("not a definition"), 0600) if _, err = run("validate", d.Path, "--json"); err == nil { t.Fatal("invalid draft accepted") } if _, err = run("submit", d.Path, "--force"); err == nil { t.Fatal("force bypassed validation") } items, _ = store.List(context.Background()) if items[0].Version != 3 { t.Fatal("invalid submission changed live version", items) } for _, command := range [][]string{{"list", "--json"}, {"show", "example", "--json"}, {"templates", "--json"}, {"template", "task", "--source"}, {"drafts", "--json"}, {"runs", "--json"}, {"connection", "--json"}, {"ping"}} { if _, err = run(command...); err != nil { t.Fatal(command, err) } } if _, err = run("--json", "list"); err != nil { t.Fatal("global JSON flag before command", err) } } func TestCLIAuthorOpenCodeReferencesGlobalSkillAndLeavesDraft(t *testing.T) { t.Setenv("XDG_CONFIG_HOME", t.TempDir()) if _, err := author.InstallSkill(); err != nil { t.Fatal(err) } run, store, _ := setup(t) // A fake executable captures launch behavior without spending model tokens. root := t.TempDir() capture := filepath.Join(root, "invocation") script := filepath.Join(root, "opencode") os.WriteFile(script, []byte("#!/bin/sh\nif [ \"$1\" = --help ]; then printf '%s\\n' ' api commands'; exit 0; fi\nif [ \"$1\" = api ]; then\n if [ \"$3\" = /api/session ]; then printf '%s' '{\"data\":{\"id\":\"ses_test\"}}'; fi\n exit 0\nfi\nprintf '%s\\n' \"$@\" > "+"'"+capture+"'"+"\n"), 0700) configPath := filepath.Join(root, "harnesses.json") t.Setenv("PIPELINES_HARNESS_CONFIG", configPath) configData, _ := json.Marshal(map[string]any{"default": "opencode", "profiles": map[string]any{"opencode": map[string]string{"executable": script, "model": "fixture/model", "pipeline_model": "fixture/model"}}}) if err := os.WriteFile(configPath, configData, 0600); err != nil { t.Fatal(err) } if _, err := run("new", "scoped skill", "Make a digest", "--opencode", script); err != nil { t.Fatal(err) } data, err := os.ReadFile(capture) if err != nil { t.Fatal(err) } if !strings.HasPrefix(string(data), "mini\n--model\nfixture/model\n--prompt\n@pipelines") || !strings.Contains(string(data), "Make a digest") { t.Fatal(string(data)) } skillDir, err := author.SkillDirectory() if err != nil { t.Fatal(err) } if _, err := os.Stat(filepath.Join(skillDir, "SKILL.md")); err != nil { t.Fatal(err) } if _, err := run("new", "--opencode", script); err != nil { t.Fatal("unnamed draft should go straight to OpenCode", err) } items, _ := store.List(context.Background()) if len(items) != 0 { t.Fatal("OpenCode launch submitted draft") } } func TestCLIBoundedFilesPortableDraftsAndNoninteractiveGuards(t *testing.T) { run, _, url := setup(t) d, err := draft.Create("test", pipeline.Template([]string{"worker"}, true), url+"-other", true, 0) if err != nil { t.Fatal(err) } if _, err = run("submit", d.Path); err != nil { t.Fatal(err) } os.WriteFile(d.Path, bytes.Repeat([]byte("x"), (1<<20)+1), 0600) if _, err = run("validate", d.Path); err == nil { t.Fatal("oversized draft accepted") } for _, args := range [][]string{{"run", "--json"}, {"run", "x", "prompt", "--json"}, {"run", "x", "prompt", "--yes", "--json", "--write-paths", "a.go"}, {"edit", "--json"}, {"new", "--json"}, {"new", "test", "--template", "missing", "--json"}, {"inspect", "0"}, {"cancel", "no"}, {"show", "missing"}, {"list", "unexpected"}} { if _, err := run(args...); err == nil { t.Fatal("expected error", fmt.Sprint(args)) } } }