package installation import ( "encoding/json" "os" "path/filepath" "testing" ) func TestCompiledCommandFindsManifestThroughSystemLink(t *testing.T) { t.Setenv("PIPELINES_PREFIX", "") root, _ := filepath.EvalSymlinks(t.TempDir()) os.Mkdir(filepath.Join(root, "bin"), 0700) binary := filepath.Join(root, "bin/pipelines") os.WriteFile(binary, []byte("binary"), 0700) config := Config{Format: 1, Prefix: root, ServerPort: 18080, OpenCode: "/host/opencode", Path: "/usr/bin"} data, _ := json.Marshal(config) os.WriteFile(filepath.Join(root, "pipelines-install.json"), data, 0600) link := filepath.Join(t.TempDir(), "pipelines") os.Symlink(binary, link) t.Setenv("PIPELINES_PREFIX", root) loaded, err := Load(link) if err != nil || loaded == nil || loaded.Prefix != root { t.Fatal(loaded, err) } t.Setenv("PIPELINES_URL", "") t.Setenv("PIPELINES_OPENCODE", "") t.Setenv("PATH", os.Getenv("PATH")) if err = loaded.Environment(); err != nil { t.Fatal(err) } if os.Getenv("PIPELINES_URL") != "ws://127.0.0.1:18080/connect" { t.Fatal("wrong installation selected") } t.Setenv("PIPELINES_URL", "ws://explicit/connect") loaded.Environment() if os.Getenv("PIPELINES_URL") != "ws://explicit/connect" { t.Fatal("explicit server overridden") } } func TestStandaloneGoInstalledCommandHasNoServiceDependency(t *testing.T) { t.Setenv("PIPELINES_PREFIX", "") binary := filepath.Join(t.TempDir(), "pipelines") os.WriteFile(binary, []byte("binary"), 0700) config, err := Load(binary) if err != nil || config != nil { t.Fatal(config, err) } } func nativeFixture(t *testing.T) (string, string) { t.Helper() root, err := filepath.EvalSymlinks(t.TempDir()) if err != nil { t.Fatal(err) } if err = os.Mkdir(filepath.Join(root, "bin"), 0700); err != nil { t.Fatal(err) } binary := filepath.Join(root, "bin", "pipelines") if err = os.WriteFile(binary, []byte("binary"), 0700); err != nil { t.Fatal(err) } config := Config{Format: 1, Prefix: root, ServerPort: 18080, Python: filepath.Join(root, "python3")} config.Ntfy.Mode = "native" data, _ := json.Marshal(config) if err = os.WriteFile(filepath.Join(root, "pipelines-install.json"), data, 0600); err != nil { t.Fatal(err) } return root, binary } func TestNativeExecutableDiscoveryAndExplicitPrefixPrecedence(t *testing.T) { t.Setenv("PIPELINES_PREFIX", "") root, binary := nativeFixture(t) link := filepath.Join(t.TempDir(), "pipelines") if err := os.Symlink(binary, link); err != nil { t.Fatal(err) } loaded, err := Load(link) if err != nil || loaded == nil || loaded.Prefix != root { t.Fatal("native executable not selected", loaded, err) } other, _ := nativeFixture(t) t.Setenv("PIPELINES_PREFIX", other) loaded, err = Load(link) if err != nil || loaded == nil || loaded.Prefix != other { t.Fatal("explicit selection lost precedence", loaded, err) } } func TestDiscoveryNeverGuessesHomeOrLegacyInstallation(t *testing.T) { t.Setenv("PIPELINES_PREFIX", "") root, binary := nativeFixture(t) home := t.TempDir() t.Setenv("HOME", home) if err := os.MkdirAll(filepath.Join(home, ".local", "share"), 0700); err != nil { t.Fatal(err) } if err := os.Symlink(root, filepath.Join(home, ".local", "share", "pipelines")); err != nil { t.Fatal(err) } unrelated := filepath.Join(t.TempDir(), "pipelines") if err := os.WriteFile(unrelated, []byte("binary"), 0700); err != nil { t.Fatal(err) } loaded, err := Load(unrelated) if err != nil || loaded != nil { t.Fatal("unrelated executable selected installation", loaded, err) } config := Config{Format: 1, Prefix: root, ServerPort: 18080} data, _ := json.Marshal(config) os.WriteFile(filepath.Join(root, "pipelines-install.json"), data, 0600) loaded, err = Load(binary) if err != nil || loaded != nil { t.Fatal("legacy manifest was inferred", loaded, err) } t.Setenv("PIPELINES_PREFIX", root) loaded, err = Load(binary) if err != nil || loaded == nil { t.Fatal("explicit legacy selection stopped working", loaded, err) } } func TestNativeDiscoveryRejectsSymlinkManifestAndMismatchedPrefix(t *testing.T) { for _, test := range []string{"symlink", "mismatch"} { t.Run(test, func(t *testing.T) { t.Setenv("PIPELINES_PREFIX", "") root, binary := nativeFixture(t) manifest := filepath.Join(root, "pipelines-install.json") if test == "symlink" { target := filepath.Join(root, "other.json") os.Rename(manifest, target) os.Symlink(target, manifest) } else { config := Config{Format: 1, Prefix: "/another/prefix", ServerPort: 18080} config.Ntfy.Mode = "native" data, _ := json.Marshal(config) os.WriteFile(manifest, data, 0600) } if _, err := Load(binary); err == nil { t.Fatal("unsafe manifest was accepted") } }) } }