231 lines
5.1 KiB
Go
231 lines
5.1 KiB
Go
package config
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func writeConfig(t *testing.T, body string) string {
|
|
t.Helper()
|
|
dir := t.TempDir()
|
|
path := filepath.Join(dir, "config.toml")
|
|
if err := os.WriteFile(path, []byte(body), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return path
|
|
}
|
|
|
|
func TestService_ProviderReference(t *testing.T) {
|
|
path := writeConfig(t, `
|
|
[providers.sherlock-cli]
|
|
issuer = "https://id.example/application/o/sherlock-cli/"
|
|
client_id = "shared-client"
|
|
|
|
[services.grafana]
|
|
provider = "sherlock-cli"
|
|
base_url = "https://grafana.example"
|
|
`)
|
|
c, err := LoadFrom(path)
|
|
if err != nil {
|
|
t.Fatalf("LoadFrom: %v", err)
|
|
}
|
|
got, err := c.Service("grafana")
|
|
if err != nil {
|
|
t.Fatalf("Service: %v", err)
|
|
}
|
|
if got.Issuer != "https://id.example/application/o/sherlock-cli/" {
|
|
t.Fatalf("issuer = %q", got.Issuer)
|
|
}
|
|
if got.ClientID != "shared-client" {
|
|
t.Fatalf("client_id = %q", got.ClientID)
|
|
}
|
|
if got.BaseURL != "https://grafana.example" {
|
|
t.Fatalf("base_url = %q", got.BaseURL)
|
|
}
|
|
}
|
|
|
|
func TestService_InlineIdentity(t *testing.T) {
|
|
path := writeConfig(t, `
|
|
[services.gitea]
|
|
issuer = "https://gitea.example"
|
|
client_id = "gitea-client"
|
|
base_url = "https://gitea.example"
|
|
`)
|
|
c, _ := LoadFrom(path)
|
|
got, err := c.Service("gitea")
|
|
if err != nil {
|
|
t.Fatalf("Service: %v", err)
|
|
}
|
|
if got.ClientID != "gitea-client" || got.Issuer != "https://gitea.example" {
|
|
t.Fatalf("resolved = %+v", got)
|
|
}
|
|
}
|
|
|
|
func TestService_OAuthBrowser(t *testing.T) {
|
|
path := writeConfig(t, `
|
|
[oauth]
|
|
browser = "firefox"
|
|
|
|
[services.gitea]
|
|
issuer = "https://gitea.example"
|
|
client_id = "gitea-client"
|
|
base_url = "https://gitea.example"
|
|
`)
|
|
c, _ := LoadFrom(path)
|
|
got, err := c.Service("gitea")
|
|
if err != nil {
|
|
t.Fatalf("Service: %v", err)
|
|
}
|
|
if got.OAuthBrowser != "firefox" {
|
|
t.Fatalf("OAuthBrowser = %q", got.OAuthBrowser)
|
|
}
|
|
}
|
|
|
|
func TestService_UnknownService(t *testing.T) {
|
|
path := writeConfig(t, `
|
|
[services.gitea]
|
|
issuer = "https://gitea.example"
|
|
client_id = "x"
|
|
base_url = "https://gitea.example"
|
|
`)
|
|
c, _ := LoadFrom(path)
|
|
if _, err := c.Service("grafana"); err == nil {
|
|
t.Fatal("expected error for missing service")
|
|
}
|
|
}
|
|
|
|
func TestService_UnknownProvider(t *testing.T) {
|
|
path := writeConfig(t, `
|
|
[services.grafana]
|
|
provider = "nope"
|
|
base_url = "https://grafana.example"
|
|
`)
|
|
c, _ := LoadFrom(path)
|
|
_, err := c.Service("grafana")
|
|
if err == nil {
|
|
t.Fatal("expected error for unknown provider reference")
|
|
}
|
|
}
|
|
|
|
func TestService_MissingRequiredFields(t *testing.T) {
|
|
path := writeConfig(t, `
|
|
[services.grafana]
|
|
base_url = "https://grafana.example"
|
|
`)
|
|
c, _ := LoadFrom(path)
|
|
_, err := c.Service("grafana")
|
|
if err == nil {
|
|
t.Fatal("expected error: issuer + client_id missing")
|
|
}
|
|
}
|
|
|
|
func TestService_MissingBaseURL(t *testing.T) {
|
|
path := writeConfig(t, `
|
|
[providers.p]
|
|
issuer = "https://id.example/"
|
|
client_id = "c"
|
|
|
|
[services.grafana]
|
|
provider = "p"
|
|
`)
|
|
c, _ := LoadFrom(path)
|
|
_, err := c.Service("grafana")
|
|
if err == nil {
|
|
t.Fatal("expected error: base_url missing")
|
|
}
|
|
}
|
|
|
|
func TestService_ProviderAndInlineConflict(t *testing.T) {
|
|
path := writeConfig(t, `
|
|
[providers.p]
|
|
issuer = "https://id.example/"
|
|
client_id = "c"
|
|
|
|
[services.grafana]
|
|
provider = "p"
|
|
client_id = "override"
|
|
base_url = "https://grafana.example"
|
|
`)
|
|
c, _ := LoadFrom(path)
|
|
_, err := c.Service("grafana")
|
|
if err == nil {
|
|
t.Fatal("expected error: provider + inline identity conflict")
|
|
}
|
|
}
|
|
|
|
func TestLoadFrom_NotFound(t *testing.T) {
|
|
_, err := LoadFrom(filepath.Join(t.TempDir(), "does-not-exist.toml"))
|
|
if !errors.Is(err, ErrNotFound) {
|
|
t.Fatalf("err = %v, want ErrNotFound", err)
|
|
}
|
|
}
|
|
|
|
func TestDefaultPath_EnvOverride(t *testing.T) {
|
|
t.Setenv(EnvConfigPath, "/custom/sherlock.toml")
|
|
got, err := DefaultPath()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got != "/custom/sherlock.toml" {
|
|
t.Fatalf("DefaultPath = %q", got)
|
|
}
|
|
}
|
|
|
|
func TestDefaultPath_XDG(t *testing.T) {
|
|
t.Setenv(EnvConfigPath, "")
|
|
t.Setenv("XDG_CONFIG_HOME", "/xdg")
|
|
got, err := DefaultPath()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got != filepath.Join("/xdg", "sherlock", "config.toml") {
|
|
t.Fatalf("DefaultPath = %q", got)
|
|
}
|
|
}
|
|
|
|
func TestAgents_Parsed(t *testing.T) {
|
|
path := writeConfig(t, `
|
|
[agents.copilot-local]
|
|
command = "copilot-local"
|
|
backend = "copilot"
|
|
|
|
[agents.claude-local]
|
|
backend = "claude"
|
|
`)
|
|
c, err := LoadFrom(path)
|
|
if err != nil {
|
|
t.Fatalf("LoadFrom: %v", err)
|
|
}
|
|
if len(c.Agents) != 2 {
|
|
t.Fatalf("expected 2 agents, got %d: %+v", len(c.Agents), c.Agents)
|
|
}
|
|
cp := c.Agents["copilot-local"]
|
|
if cp.Command != "copilot-local" || cp.Backend != "copilot" {
|
|
t.Fatalf("copilot-local = %+v", cp)
|
|
}
|
|
// command is optional; absence leaves it empty for the agent layer
|
|
// to default to the name.
|
|
cl := c.Agents["claude-local"]
|
|
if cl.Command != "" || cl.Backend != "claude" {
|
|
t.Fatalf("claude-local = %+v", cl)
|
|
}
|
|
}
|
|
|
|
func TestAgents_AbsentIsEmpty(t *testing.T) {
|
|
path := writeConfig(t, `
|
|
[services.gitea]
|
|
issuer = "https://gitea.example"
|
|
client_id = "x"
|
|
base_url = "https://gitea.example"
|
|
`)
|
|
c, err := LoadFrom(path)
|
|
if err != nil {
|
|
t.Fatalf("LoadFrom: %v", err)
|
|
}
|
|
if len(c.Agents) != 0 {
|
|
t.Fatalf("expected no agents, got %+v", c.Agents)
|
|
}
|
|
}
|