|
| 1 | +// |
| 2 | +// Copyright 2026 The Chainloop Authors. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | + |
| 16 | +package action |
| 17 | + |
| 18 | +import ( |
| 19 | + "testing" |
| 20 | + |
| 21 | + pb "github.com/chainloop-dev/chainloop/app/controlplane/api/controlplane/v1" |
| 22 | + "github.com/stretchr/testify/assert" |
| 23 | + "google.golang.org/protobuf/types/known/timestamppb" |
| 24 | +) |
| 25 | + |
| 26 | +// TestNewWorkflowCreateOptsToRequest pins that the create options, including the optional |
| 27 | +// workflow template reference, are forwarded verbatim to the API request. |
| 28 | +func TestNewWorkflowCreateOptsToRequest(t *testing.T) { |
| 29 | + testCases := []struct { |
| 30 | + desc string |
| 31 | + opts *NewWorkflowCreateOpts |
| 32 | + }{ |
| 33 | + { |
| 34 | + desc: "without a template reference", |
| 35 | + opts: &NewWorkflowCreateOpts{ |
| 36 | + Name: "my-workflow", |
| 37 | + Project: "my-project", |
| 38 | + }, |
| 39 | + }, |
| 40 | + { |
| 41 | + desc: "with a template reference", |
| 42 | + opts: &NewWorkflowCreateOpts{ |
| 43 | + Name: "my-workflow", |
| 44 | + Project: "my-project", |
| 45 | + Team: "my-team", |
| 46 | + Description: "a description", |
| 47 | + ContractName: "my-contract", |
| 48 | + ContractBytes: []byte("schemaVersion: v1"), |
| 49 | + WorkflowTemplateID: "1b4c6f8a-2d3e-4f5a-8b9c-0d1e2f3a4b5c", |
| 50 | + }, |
| 51 | + }, |
| 52 | + } |
| 53 | + |
| 54 | + for _, tc := range testCases { |
| 55 | + t.Run(tc.desc, func(t *testing.T) { |
| 56 | + got := newWorkflowCreateRequest(tc.opts) |
| 57 | + |
| 58 | + assert.Equal(t, tc.opts.Name, got.GetName()) |
| 59 | + assert.Equal(t, tc.opts.Project, got.GetProjectName()) |
| 60 | + assert.Equal(t, tc.opts.Team, got.GetTeam()) |
| 61 | + assert.Equal(t, tc.opts.Description, got.GetDescription()) |
| 62 | + assert.Equal(t, tc.opts.ContractName, got.GetContractName()) |
| 63 | + assert.Equal(t, tc.opts.ContractBytes, got.GetContractBytes()) |
| 64 | + assert.Equal(t, tc.opts.WorkflowTemplateID, got.GetWorkflowTemplateId()) |
| 65 | + }) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +func TestPbWorkflowItemToActionWorkflowTemplateID(t *testing.T) { |
| 70 | + testCases := []struct { |
| 71 | + desc string |
| 72 | + item *pb.WorkflowItem |
| 73 | + want string |
| 74 | + }{ |
| 75 | + { |
| 76 | + desc: "unbound workflow leaves the template reference empty", |
| 77 | + item: &pb.WorkflowItem{ |
| 78 | + Id: "wf-id", |
| 79 | + Name: "my-workflow", |
| 80 | + CreatedAt: timestamppb.Now(), |
| 81 | + }, |
| 82 | + want: "", |
| 83 | + }, |
| 84 | + { |
| 85 | + desc: "template-backed workflow carries the template reference", |
| 86 | + item: &pb.WorkflowItem{ |
| 87 | + Id: "wf-id", |
| 88 | + Name: "my-workflow", |
| 89 | + CreatedAt: timestamppb.Now(), |
| 90 | + WorkflowTemplateId: "1b4c6f8a-2d3e-4f5a-8b9c-0d1e2f3a4b5c", |
| 91 | + }, |
| 92 | + want: "1b4c6f8a-2d3e-4f5a-8b9c-0d1e2f3a4b5c", |
| 93 | + }, |
| 94 | + } |
| 95 | + |
| 96 | + for _, tc := range testCases { |
| 97 | + t.Run(tc.desc, func(t *testing.T) { |
| 98 | + got := pbWorkflowItemToAction(tc.item) |
| 99 | + |
| 100 | + assert.Equal(t, tc.want, got.WorkflowTemplateID) |
| 101 | + }) |
| 102 | + } |
| 103 | +} |
0 commit comments