package cli

import (
	"bytes"
	"encoding/json"
	"strings"
	"testing"
)

func TestVersionText(t *testing.T) {
	resetRootFlags(t)
	var buf bytes.Buffer
	rootCmd.SetOut(&buf)
	rootCmd.SetErr(&buf)
	rootCmd.SetArgs([]string{"version"})
	if err := rootCmd.Execute(); err != nil {
		t.Fatalf("version: %v", err)
	}
	out := buf.String()
	if !strings.Contains(out, "orca version") {
		t.Errorf("version text output unexpected: %s", out)
	}
	if !strings.Contains(out, "git commit") {
		t.Errorf("version output missing git commit: %s", out)
	}
}

func TestVersionJSON(t *testing.T) {
	resetRootFlags(t)
	var buf bytes.Buffer
	rootCmd.SetOut(&buf)
	rootCmd.SetErr(&buf)
	rootCmd.SetArgs([]string{"version", "--json"})
	if err := rootCmd.Execute(); err != nil {
		t.Fatalf("version --json: %v", err)
	}
	var info map[string]string
	if err := json.Unmarshal(bytes.TrimSpace(buf.Bytes()), &info); err != nil {
		t.Fatalf("unmarshal version json: %v\n%s", err, buf.String())
	}
	if info["version"] == "" {
		t.Errorf("version json missing version field: %v", info)
	}
	if info["git_commit"] == "" {
		t.Errorf("version json missing git_commit field: %v", info)
	}
}
