package webui import ( "context" "testing" "google.golang.org/grpc" controlpb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1" "github.com/samcharles93/archie-core/internal/domain/workflow/task" ) // linkControl serves one repository policy on a Gitea connection. type linkControl struct { controlpb.ControlPlaneServiceClient } func (linkControl) Query(_ context.Context, r *controlpb.QueryRequest, _ ...grpc.CallOption) (*controlpb.QueryResponse, error) { value := map[string]string{ "repository-policies": `[{"owner":"sam","name":"archie","connection":"home"}]`, "forge-connections": `[{"id":"home","type":"gitea","host":"git.example.test"}]`, }[r.Kind] return &controlpb.QueryResponse{Resource: &controlpb.Resource{ValueJson: []byte(value)}}, nil } // A task links to the forge its repository's connection is on, not the // instance's default forge, which is GitHub unless configured otherwise. func TestTaskLinksFollowTheRepositoryConnection(t *testing.T) { api := &tasksAPI{ controlPlane: linkControl{}, configSource: func() ConfigViewSource { return func(context.Context) (ConfigView, bool, error) { return ConfigView{Identity: IdentityView{ForgeHost: "https://github.com", ForgeType: "github"}}, true, nil } }, } tests := []struct { name string task task.Task wantIssue string wantPR string }{ { "repository on a connection", task.Task{Owner: "Sam", Repo: "archie", IssueNumber: 5, PRNumber: 6}, "https://git.example.test/Sam/archie/issues/5", "https://git.example.test/Sam/archie/pulls/6", }, { "repository without a policy", task.Task{Owner: "acme", Repo: "app", IssueNumber: 1}, "https://github.com/acme/app/issues/1", "", }, } forge := api.resolveForge(context.Background()) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { _, issue, pr := taskURLs(tt.task, forge(tt.task)) if issue != tt.wantIssue || pr != tt.wantPR { t.Fatalf("issue %q pr %q, want %q and %q", issue, pr, tt.wantIssue, tt.wantPR) } }) } }