This repository has no description
1package db
2
3import (
4 "context"
5 "encoding/json"
6 "slices"
7 "testing"
8 "time"
9
10 "tangled.org/core/api/tangled"
11)
12
13func seedPipelineEvent(t *testing.T, d *DB, rkey, repoDid, kind string, created int64) {
14 t.Helper()
15 repo := repoDid
16 tm := &tangled.Pipeline_TriggerMetadata{
17 Kind: kind,
18 Repo: &tangled.Pipeline_TriggerRepo{Knot: "knot.test", RepoDid: &repo, Did: repoDid},
19 }
20 switch kind {
21 case "push":
22 tm.Push = &tangled.Pipeline_PushTriggerData{NewSha: "sha-" + rkey, Ref: "refs/heads/main"}
23 case "pull_request":
24 tm.PullRequest = &tangled.Pipeline_PullRequestTriggerData{SourceSha: "sha-" + rkey, SourceBranch: "feature", TargetBranch: "main"}
25 case "manual":
26 tm.Manual = &tangled.Pipeline_ManualTriggerData{Sha: "sha-" + rkey}
27 }
28 raw := tangled.Pipeline{
29 TriggerMetadata: tm,
30 Workflows: []*tangled.Pipeline_Workflow{{Name: "ci.yml"}},
31 }
32 eventJson, err := json.Marshal(raw)
33 if err != nil {
34 t.Fatalf("marshal pipeline: %v", err)
35 }
36 if _, err := d.Exec(
37 `insert into events (rkey, nsid, event, created) values (?, 'sh.tangled.pipeline', ?, ?)`,
38 rkey, string(eventJson), created,
39 ); err != nil {
40 t.Fatalf("seed event %s: %v", rkey, err)
41 }
42}
43
44func TestQueryPipelines_FilterByKind(t *testing.T) {
45 d := newTestDB(t)
46 ctx := context.Background()
47 repo := "did:plc:boltless"
48 base := time.Now().UnixNano()
49
50 seedPipelineEvent(t, d, "p-push", repo, "push", base+1)
51 seedPipelineEvent(t, d, "p-pull", repo, "pull_request", base+2)
52 seedPipelineEvent(t, d, "p-manual", repo, "manual", base+3)
53
54 cases := []struct {
55 kinds []string
56 wantTotal int64
57 wantKinds []string
58 }{
59 {nil, 3, []string{"manual", "pull_request", "push"}},
60 {[]string{"push"}, 1, []string{"push"}},
61 {[]string{"pull_request"}, 1, []string{"pull_request"}},
62 {[]string{"manual"}, 1, []string{"manual"}},
63 {[]string{"push", "pull_request"}, 2, []string{"pull_request", "push"}},
64 }
65
66 for _, tc := range cases {
67 pipelines, _, total, err := d.QueryPipelines(ctx, repo, nil, "", tc.kinds, 30)
68 if err != nil {
69 t.Fatalf("kinds=%v: QueryPipelines: %v", tc.kinds, err)
70 }
71 if total != tc.wantTotal {
72 t.Errorf("kinds=%v: total = %d, want %d", tc.kinds, total, tc.wantTotal)
73 }
74 var gotKinds []string
75 for _, p := range pipelines {
76 gotKinds = append(gotKinds, triggerKindOf(p))
77 }
78 slices.Sort(gotKinds)
79 if !slices.Equal(gotKinds, tc.wantKinds) {
80 t.Errorf("kinds=%v: returned %v, want %v", tc.kinds, gotKinds, tc.wantKinds)
81 }
82 }
83}
84
85func TestQueryPipelines_KindScopedToRepo(t *testing.T) {
86 d := newTestDB(t)
87 ctx := context.Background()
88 base := time.Now().UnixNano()
89
90 seedPipelineEvent(t, d, "a-push", "did:plc:alice", "push", base+1)
91 seedPipelineEvent(t, d, "b-push", "did:plc:bob", "push", base+2)
92
93 pipelines, _, total, err := d.QueryPipelines(ctx, "did:plc:alice", nil, "", []string{"push"}, 30)
94 if err != nil {
95 t.Fatalf("QueryPipelines: %v", err)
96 }
97 if total != 1 || len(pipelines) != 1 {
98 t.Fatalf("total=%d len=%d, want exactly alice's single push pipeline", total, len(pipelines))
99 }
100 if pipelines[0].Repo == nil || *pipelines[0].Repo != "did:plc:alice" {
101 t.Errorf("returned pipeline repo = %v, want did:plc:alice", pipelines[0].Repo)
102 }
103}
104
105func triggerKindOf(p *tangled.CiPipeline) string {
106 if p.Trigger == nil {
107 return ""
108 }
109 switch {
110 case p.Trigger.CiTrigger_Push != nil:
111 return "push"
112 case p.Trigger.CiTrigger_PullRequest != nil:
113 return "pull_request"
114 case p.Trigger.CiTrigger_Manual != nil:
115 return "manual"
116 }
117 return ""
118}