package adapter

import (
	"os"
	"testing"
)

func TestCallbackURL(t *testing.T) {
	got := callbackURL("http://datasource-proxy-prod.ns/devops/prod/", "ALT-1", 42)
	want := "http://datasource-proxy-prod.ns/devops/prod/api/v1/alertstore/alerts/ALT-1/agent-runs/42"
	if got != want {
		t.Fatalf("got %q want %q", got, want)
	}
}

func TestDispatchOverridesCallbackWhenBaseSet(t *testing.T) {
	promptPath := t.TempDir() + "/p.md"
	if err := os.WriteFile(promptPath, []byte("x {{CALLBACK_URL}}\n"), 0o600); err != nil {
		t.Fatal(err)
	}
	a := New(Config{PromptPath: promptPath, AlertstoreBase: "http://base.ns/devops/prod"}, discardLogger())
	d := &Dispatch{AlertID: "ALT-9", RunID: 7, Callback: Callback{URL: "http://127.0.0.1:8990/x/agent-runs/7"}}
	if a.cfg.AlertstoreBase != "" {
		d.Callback.URL = callbackURL(a.cfg.AlertstoreBase, d.AlertID, d.RunID)
	}
	want := "http://base.ns/devops/prod/api/v1/alertstore/alerts/ALT-9/agent-runs/7"
	if d.Callback.URL != want {
		t.Fatalf("override: got %q want %q", d.Callback.URL, want)
	}
}
