Files
pipelines/internal/cli/harness_transition_test.go
alan 914345cb31 Move harness profiles between host and Docker and stop silent draft retargeting
Native installs left Docker-era harness profiles in harnesses.json, so every
authoring launch failed trying to reach a Docker service, and a retained draft
bound to the old Docker server was silently projected onto the configured one.

- harness: add Profile.Mode and VerifyDockerLink as the single mode/verification
  helpers. `harness setup --host` configures or converts a host profile and
  clears docker_directory; reconfiguring a Docker-linked profile re-verifies the
  recorded directory still owns compose.yaml and fails naming it instead of
  saving a link that cannot launch. Setup's summary and `harness list` (via the
  new List/ListView shape) report the resulting mode and, for Docker, the
  directory, without changing the stored Config schema.
- setup-native.py: after build/install-skill it writes host (no docker_directory)
  profiles for the detected harnesses, defaulting to the detected OpenCode profile
  rather than a stale pi default, taking models from --model or the existing valid
  profile and never inventing one; it clears a stale Docker link and preserves
  unrelated keys/profiles. Detection-only: no harness install, upgrade or download.
- draft: add Draft.Resolve, the shared mismatch helper, which projects a draft
  onto the configured server and returns a dual-URL notice. CLI validate/submit
  and the TUI draft browser use it so a foreign draft is never silently
  retargeted; the TUI pre-wraps the notice to the panel width.
- Tests cover the Docker->host and host->host transitions, the missing-directory
  failure, installer write/migration (Python), and the CLI/TUI notice, using the
  real config file path in a temp dir.
- Update help text, README, docs and the packaged authoring skill reference to
  describe host as the default and the Docker-linked case.
2026-10-15 19:53:10 +01:00

176 lines
7.3 KiB
Go

package cli
import (
"bytes"
"context"
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"
)
// fixtureOpencode writes a fake opencode executable that answers the bounded
// model-discovery calls harness setup makes, so setup can run without a real
// harness or any network access.
func fixtureOpencode(t *testing.T, root string) string {
t.Helper()
script := filepath.Join(root, "opencode")
body := "#!/bin/sh\ncase \"$*\" in\n'api get /api/integration') echo '{}' ;;\nmodels) printf '%s\\n' fixture/harness fixture/stages ;;\n'auth list --format json') echo '[{\"id\":\"fixture\",\"connections\":[{}]}]' ;;\n*) exit 91;;\nesac\n"
if err := os.WriteFile(script, []byte(body), 0700); err != nil {
t.Fatal(err)
}
return script
}
func writeHarnessConfig(t *testing.T, path, body string) {
t.Helper()
if err := os.WriteFile(path, []byte(body), 0600); err != nil {
t.Fatal(err)
}
}
// TestHarnessSetupHostModeClearsDockerLinkDrivesTheRealConfigFile covers the
// Docker-linked -> host transition and host -> host idempotence by reading the
// saved JSON, not a private helper.
func TestHarnessSetupHostModeClearsDockerLinkDrivesTheRealConfigFile(t *testing.T) {
root := t.TempDir()
configPath := filepath.Join(root, "harnesses.json")
t.Setenv("PIPELINES_HARNESS_CONFIG", configPath)
script := fixtureOpencode(t, root)
writeHarnessConfig(t, configPath, `{"default":"opencode","profiles":{"opencode":{"docker_directory":"/checkout","executable":"opencode","model":"fixture/harness","pipeline_model":"fixture/stages"}}}`)
var out bytes.Buffer
app := App{In: bytes.NewBuffer(nil), Out: &out, Err: &out}
if err := app.Run(context.Background(), []string{"harness", "setup", "opencode", "--host", "--executable", script, "--model", "fixture/harness", "--pipeline-model", "fixture/stages"}); err != nil {
t.Fatal(err)
}
saved, err := os.ReadFile(configPath)
if err != nil {
t.Fatal(err)
}
if strings.Contains(string(saved), "docker_directory") {
t.Fatalf("host setup kept a Docker link:\n%s", saved)
}
if text := out.String(); !strings.Contains(text, "Launch mode:") || !strings.Contains(text, "host") {
t.Fatalf("summary did not report host mode:\n%s", text)
}
// host -> host leaves it absent and unchanged in mode.
if err := app.Run(context.Background(), []string{"harness", "setup", "opencode", "--host", "--executable", script, "--model", "fixture/harness", "--pipeline-model", "fixture/stages"}); err != nil {
t.Fatal(err)
}
again, err := os.ReadFile(configPath)
if err != nil {
t.Fatal(err)
}
if strings.Contains(string(again), "docker_directory") {
t.Fatalf("host -> host reintroduced a Docker link:\n%s", again)
}
}
// TestHarnessSetupReverifiesDockerLinkBeforeSaving fails a reconfigure whose
// recorded installation no longer has compose.yaml, names the directory, and
// leaves the file byte-identical.
func TestHarnessSetupReverifiesDockerLinkBeforeSaving(t *testing.T) {
root := t.TempDir()
configPath := filepath.Join(root, "harnesses.json")
t.Setenv("PIPELINES_HARNESS_CONFIG", configPath)
script := fixtureOpencode(t, root)
// The directory exists but has no compose.yaml, so the link is unusable.
checkout := filepath.Join(root, "checkout")
if err := os.Mkdir(checkout, 0700); err != nil {
t.Fatal(err)
}
writeHarnessConfig(t, configPath, `{"default":"opencode","profiles":{"opencode":{"docker_directory":"`+checkout+`","executable":"opencode","model":"fixture/harness","pipeline_model":"fixture/stages"}}}`)
before, err := os.ReadFile(configPath)
if err != nil {
t.Fatal(err)
}
var out bytes.Buffer
app := App{In: bytes.NewBuffer(nil), Out: &out, Err: &out}
err = app.Run(context.Background(), []string{"harness", "setup", "opencode", "--executable", script, "--model", "fixture/harness", "--pipeline-model", "fixture/stages"})
if err == nil {
t.Fatal("setup reported success for an unverifiable Docker link")
}
if !strings.Contains(err.Error(), checkout) {
t.Fatalf("error does not name the directory: %v", err)
}
after, readErr := os.ReadFile(configPath)
if readErr != nil {
t.Fatal(readErr)
}
if !bytes.Equal(before, after) {
t.Fatalf("failed setup rewrote the config:\nbefore %s\nafter %s", before, after)
}
}
// TestHarnessSetupKeepsVerifiedDockerLink confirms a still-verifiable Docker
// installation is preserved and its directory reported.
func TestHarnessSetupKeepsVerifiedDockerLink(t *testing.T) {
root := t.TempDir()
configPath := filepath.Join(root, "harnesses.json")
t.Setenv("PIPELINES_HARNESS_CONFIG", configPath)
script := fixtureOpencode(t, root)
checkout := filepath.Join(root, "checkout")
if err := os.Mkdir(checkout, 0700); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(checkout, "compose.yaml"), []byte("services: {}\n"), 0600); err != nil {
t.Fatal(err)
}
writeHarnessConfig(t, configPath, `{"default":"opencode","profiles":{"opencode":{"docker_directory":"`+checkout+`","executable":"opencode","model":"fixture/harness","pipeline_model":"fixture/stages"}}}`)
var out bytes.Buffer
app := App{In: bytes.NewBuffer(nil), Out: &out, Err: &out}
if err := app.Run(context.Background(), []string{"harness", "setup", "opencode", "--executable", script, "--model", "fixture/harness", "--pipeline-model", "fixture/stages"}); err != nil {
t.Fatal(err)
}
saved, err := os.ReadFile(configPath)
if err != nil || !strings.Contains(string(saved), checkout) {
t.Fatalf("verified Docker link was not preserved: %s %v", saved, err)
}
if text := out.String(); !strings.Contains(text, "Docker-linked") || !strings.Contains(text, checkout) {
t.Fatalf("summary did not report Docker mode and directory:\n%s", text)
}
}
// TestHarnessListReportsModeAndKeepsModelFields keeps the JSON compatible with
// scripts/setup-docker.py's link_host parsing (default, model, pipeline_model)
// while exposing an explicit per-profile mode.
func TestHarnessListReportsModeAndKeepsModelFields(t *testing.T) {
root := t.TempDir()
configPath := filepath.Join(root, "harnesses.json")
t.Setenv("PIPELINES_HARNESS_CONFIG", configPath)
writeHarnessConfig(t, configPath, `{"default":"opencode","profiles":{"opencode":{"docker_directory":"/checkout","executable":"opencode","model":"fixture/harness","pipeline_model":"fixture/stages"},"pi":{"executable":"pi","model":"provider/interactive","pipeline_model":"provider/stages"}}}`)
var out bytes.Buffer
app := App{In: bytes.NewBuffer(nil), Out: &out, Err: &out}
if err := app.Run(context.Background(), []string{"harness", "list", "--json"}); err != nil {
t.Fatal(err)
}
var view struct {
Default string `json:"default"`
Profiles map[string]struct {
Model string `json:"model"`
PipelineModel string `json:"pipeline_model"`
Mode string `json:"mode"`
DockerDirectory string `json:"docker_directory"`
} `json:"profiles"`
}
if err := json.Unmarshal(out.Bytes(), &view); err != nil {
t.Fatalf("harness list --json is not valid JSON: %v\n%s", err, out.String())
}
if view.Default != "opencode" {
t.Fatalf("default = %q", view.Default)
}
opencode := view.Profiles["opencode"]
if opencode.Model != "fixture/harness" || opencode.PipelineModel != "fixture/stages" || opencode.Mode != "Docker-linked (/checkout)" {
t.Fatalf("opencode profile changed shape: %+v", opencode)
}
pi := view.Profiles["pi"]
if pi.Mode != "host" || pi.DockerDirectory != "" || pi.Model != "provider/interactive" {
t.Fatalf("pi profile did not report host mode: %+v", pi)
}
}