1// SPDX-License-Identifier: AGPL-3.0-or-later
2
3package usecase
4
5import (
6 "context"
7 "errors"
8 "testing"
9
10 "bnema.dev/ermite/internal/domain"
11 "bnema.dev/ermite/internal/ports"
12 "bnema.dev/ermite/internal/ports/mocks"
13 "github.com/stretchr/testify/mock"
14 "github.com/stretchr/testify/require"
15)
16
17func TestCreateRunWorkflowPolicy(t *testing.T) {
18 repo, err := domain.ParseRepoName("project")
19 require.NoError(t, err)
20 failure := errors.New("state unavailable")
21 for _, tc := range []struct {
22 name string
23 off []string
24 stateErr, want error
25 }{
26 {name: "enabled"},
27 {name: "other disabled", off: []string{"other.yml"}},
28 {name: "disabled", off: []string{"ci.yml"}, want: domain.ErrWorkflowDisabled},
29 {name: "state failure", stateErr: failure, want: failure},
30 } {
31 t.Run(tc.name, func(t *testing.T) {
32 state := mocks.NewCIWorkflowStore(t)
33 store := mocks.NewCIStore(t)
34 state.EXPECT().DisabledWorkflows(context.Background(), repo).Return(tc.off, tc.stateErr).Once()
35 c := &CI{cfg: CIConfig{Store: store, WorkflowState: state}}
36 build := func(id, number int64) ports.CIRunDraft { return ports.CIRunDraft{} }
37 if tc.want == nil {
38 store.EXPECT().CreateRun(context.Background(), repo, "ci.yml", mock.Anything).Return(ports.CIRunDraft{}, nil).Once()
39 }
40 _, err := c.createRun(context.Background(), pushFacts{}, repo, "ci.yml", build)
41 if tc.want == nil {
42 require.NoError(t, err)
43 } else {
44 require.ErrorIs(t, err, tc.want)
45 }
46 })
47 }
48}