package http

import (
	"encoding/json"
	stdhttp "net/http"
	"testing"

	"gitea.dcglab.co.uk/steve/restic-manager/internal/version"
)

func TestVersionEndpoint(t *testing.T) {
	t.Parallel()

	prevV, prevC := version.Version, version.Commit
	version.Version = "v9.9.9-test"
	version.Commit = "abc1234"
	t.Cleanup(func() {
		version.Version = prevV
		version.Commit = prevC
	})

	_, url, _ := newTestServerWithHub(t)

	res, err := stdhttp.Get(url + "/api/version")
	if err != nil {
		t.Fatalf("get: %v", err)
	}
	defer res.Body.Close()
	if res.StatusCode != stdhttp.StatusOK {
		t.Fatalf("status: got %d want 200", res.StatusCode)
	}
	var body map[string]string
	if err := json.NewDecoder(res.Body).Decode(&body); err != nil {
		t.Fatalf("decode: %v", err)
	}
	if body["version"] != "v9.9.9-test" {
		t.Fatalf("version: got %q", body["version"])
	}
	if body["commit"] != "abc1234" {
		t.Fatalf("commit: got %q", body["commit"])
	}
}
