Initial pipelines server, CLI, TUI, and Go installation
This commit is contained in:
@@ -0,0 +1,189 @@
|
||||
package cli_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"pipelines/internal/cli"
|
||||
"pipelines/internal/draft"
|
||||
"pipelines/internal/pipeline"
|
||||
"pipelines/internal/protocol"
|
||||
"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())
|
||||
return func(args ...string) (string, error) {
|
||||
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 TestCLIDraftValidateSubmitAndImmutableHistory(t *testing.T) {
|
||||
run, store, _ := setup(t)
|
||||
output, err := run("new", "example", "--template", "bug-fix", "--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", "bug-fix", "--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 TestCLIAuthorOpenCodeGetsScopedSkillAndLeavesDraft(t *testing.T) {
|
||||
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\" = 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)
|
||||
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)
|
||||
}
|
||||
parts := strings.SplitN(string(data), "\n", 3)
|
||||
if parts[1] != "--session" || !strings.Contains(parts[2], "ses_test\n--prompt\n@pipelines") || !strings.Contains(parts[2], "Make a digest") {
|
||||
t.Fatal(string(data))
|
||||
}
|
||||
for _, name := range []string{"DRAFT.json", ".opencode/skills/pipelines/SKILL.md", ".opencode/skills/pipelines/references/format.md"} {
|
||||
if _, err := os.Stat(filepath.Join(parts[0], name)); err != nil {
|
||||
t.Fatal(name, err)
|
||||
}
|
||||
}
|
||||
items, _ := store.List(context.Background())
|
||||
if len(items) != 0 {
|
||||
t.Fatal("OpenCode launch submitted draft")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCLIBoundedFilesServerMismatchAndNoninteractiveGuards(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 || !strings.Contains(err.Error(), "belongs") {
|
||||
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))
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user