Gitfed
bastien-mrq/gitfed / cmd / gitfed-web / branch_options_test.go
package main

import "testing"

func TestBranchOptions(t *testing.T) {
	got := branchOptions("alice/demo", []string{"main", "feature/x"}, "cmd/tool.go", "feature/x")
	if len(got) != 2 {
		t.Fatalf("expected 2 options, got %d: %+v", len(got), got)
	}

	main, feature := got[0], got[1]
	if main.Name != "main" || main.Active {
		t.Errorf("main option = %+v, want Name=main Active=false", main)
	}
	if feature.Name != "feature/x" || !feature.Active {
		t.Errorf("feature option = %+v, want Name=feature/x Active=true", feature)
	}

	wantMainURL := "/r/alice%2Fdemo?branch=main&path=cmd%2Ftool.go"
	if main.URL != wantMainURL {
		t.Errorf("main.URL = %q, want %q", main.URL, wantMainURL)
	}
	wantFeatureURL := "/r/alice%2Fdemo?branch=feature%2Fx&path=cmd%2Ftool.go"
	if feature.URL != wantFeatureURL {
		t.Errorf("feature.URL = %q, want %q", feature.URL, wantFeatureURL)
	}
}

func TestBranchOptionsNoPath(t *testing.T) {
	got := branchOptions("demo", []string{"main"}, "", "main")
	if len(got) != 1 {
		t.Fatalf("expected 1 option, got %d", len(got))
	}
	if got[0].URL != "/r/demo?branch=main" {
		t.Errorf("URL = %q, want %q (no &path= when path is empty)", got[0].URL, "/r/demo?branch=main")
	}
}

func TestContainsBranch(t *testing.T) {
	branches := []string{"main", "feature/x"}
	if !containsBranch(branches, "main") {
		t.Error("expected containsBranch to find an existing branch")
	}
	if containsBranch(branches, "nonexistent") {
		t.Error("expected containsBranch to reject an unknown branch")
	}
}