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) } }