package cli

import "testing"

func TestParseVersionOutput(t *testing.T) {
	cases := []struct {
		name        string
		in          string
		wantVersion string
		wantCommit  string
		wantBuilt   string
		wantErr     bool
	}{
		{
			name: "happy path — three-line shape from banger version",
			in: `version: v0.1.2
commit: a0b5c7fa3ca95a37ba99b35280fc75e5647b59e8
built_at: 2026-04-29T17:34:45Z
`,
			wantVersion: "v0.1.2",
			wantCommit:  "a0b5c7fa3ca95a37ba99b35280fc75e5647b59e8",
			wantBuilt:   "2026-04-29T17:34:45Z",
		},
		{
			name: "tolerates extra whitespace around the values",
			in: `  version :   v0.1.2
  commit :    abc123
  built_at :  2026-01-01T00:00:00Z`,
			wantVersion: "v0.1.2",
			wantCommit:  "abc123",
			wantBuilt:   "2026-01-01T00:00:00Z",
		},
		{
			name:    "missing commit field is rejected",
			in:      "version: v0.1.2\nbuilt_at: 2026-01-01T00:00:00Z\n",
			wantErr: true,
		},
		{
			name:    "empty input is rejected",
			in:      "",
			wantErr: true,
		},
		{
			name: "unrelated lines are ignored",
			in: `banger v0.1.2
some other diagnostic line: with a colon
version: v0.1.2
commit: abc
built_at: 2026-01-01T00:00:00Z
`,
			wantVersion: "v0.1.2",
			wantCommit:  "abc",
			wantBuilt:   "2026-01-01T00:00:00Z",
		},
	}
	for _, tc := range cases {
		t.Run(tc.name, func(t *testing.T) {
			got, err := parseVersionOutput(tc.in)
			if tc.wantErr {
				if err == nil {
					t.Fatalf("want error, got nil; parsed=%+v", got)
				}
				return
			}
			if err != nil {
				t.Fatalf("unexpected error: %v", err)
			}
			if got.Version != tc.wantVersion {
				t.Errorf("Version: got %q, want %q", got.Version, tc.wantVersion)
			}
			if got.Commit != tc.wantCommit {
				t.Errorf("Commit: got %q, want %q", got.Commit, tc.wantCommit)
			}
			if got.BuiltAt != tc.wantBuilt {
				t.Errorf("BuiltAt: got %q, want %q", got.BuiltAt, tc.wantBuilt)
			}
		})
	}
}
