This repository has no description
1package pulls
2
3import (
4 "context"
5 "encoding/json"
6 "io"
7 "log/slog"
8 "net/http"
9 "net/http/httptest"
10 "testing"
11 "time"
12
13 "github.com/bluesky-social/indigo/atproto/syntax"
14 "tangled.org/core/api/tangled"
15 "tangled.org/core/appview/config"
16 "tangled.org/core/appview/models"
17 "tangled.org/core/appview/pages"
18)
19
20const (
21 resubmitTestOwnerDID = "did:plc:boltless"
22 resubmitTestRepoDID = "did:plc:akshay"
23 resubmitTestBranch = "feature"
24 resubmitTestSourceRev = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
25)
26
27func newKnotmirrorStub(t *testing.T, hash string) *httptest.Server {
28 t.Helper()
29 return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
30 if r.URL.Path != "/xrpc/"+tangled.GitTempGetBranchNSID {
31 t.Errorf("unexpected path %q", r.URL.Path)
32 http.Error(w, "wrong path", http.StatusNotFound)
33 return
34 }
35 repoQuery := r.URL.Query().Get("repo")
36 if _, err := syntax.ParseDID(repoQuery); err != nil {
37 t.Errorf("repo param %q is not a DID: %v", repoQuery, err)
38 http.Error(w, "repo must be a DID", http.StatusBadRequest)
39 return
40 }
41 if got := r.URL.Query().Get("name"); got != resubmitTestBranch {
42 t.Errorf("name param = %q, want %q", got, resubmitTestBranch)
43 }
44 w.Header().Set("Content-Type", "application/json")
45 _ = json.NewEncoder(w).Encode(tangled.GitTempGetBranch_Output{
46 Name: resubmitTestBranch,
47 Hash: hash,
48 When: time.Now().UTC().Format(time.RFC3339),
49 })
50 }))
51}
52
53func newPullsFromKnotURL(url string) *Pulls {
54 return &Pulls{
55 logger: slog.New(slog.NewTextHandler(io.Discard, nil)),
56 config: &config.Config{
57 KnotMirror: config.KnotMirrorConfig{Url: url},
58 },
59 }
60}
61
62func newForkPull(state models.PullState) (*models.Pull, *models.Repo) {
63 sourceRepoDid := syntax.DID(resubmitTestRepoDID)
64 sourceBranch := resubmitTestBranch
65 pull := &models.Pull{
66 State: state,
67 OwnerDid: resubmitTestOwnerDID,
68 TargetBranch: "main",
69 Versions: []models.PullVersion{
70 {Head: resubmitTestSourceRev},
71 },
72 SourceBranch: &sourceBranch,
73 SourceRepo: sourceRepoDid,
74 }
75 repo := &models.Repo{RepoDid: resubmitTestRepoDID}
76 return pull, repo
77}
78
79func TestResubmitCheck_BranchAdvanced(t *testing.T) {
80 srv := newKnotmirrorStub(t, "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")
81 defer srv.Close()
82
83 s := newPullsFromKnotURL(srv.URL)
84 ctx := t.Context()
85 pull, _ := newForkPull(models.PullOpen)
86
87 got := s.resubmitCheck(ctx, pull)
88 if got != pages.ShouldResubmit {
89 t.Errorf("resubmitCheck() = %v, want ShouldResubmit", got)
90 }
91}
92
93func TestResubmitCheck_BranchUpToDate(t *testing.T) {
94 srv := newKnotmirrorStub(t, resubmitTestSourceRev)
95 defer srv.Close()
96
97 s := newPullsFromKnotURL(srv.URL)
98 ctx := t.Context()
99 pull, _ := newForkPull(models.PullOpen)
100
101 got := s.resubmitCheck(ctx, pull)
102 if got != pages.ShouldNotResubmit {
103 t.Errorf("resubmitCheck() = %v, want ShouldNotResubmit", got)
104 }
105}
106
107func TestResubmitCheck_MergedReturnsUnknown(t *testing.T) {
108 s := newPullsFromKnotURL("http://unused")
109 ctx := t.Context()
110 pull, _ := newForkPull(models.PullMerged)
111
112 if got := s.resubmitCheck(ctx, pull); got != pages.Unknown {
113 t.Errorf("resubmitCheck() = %v, want Unknown for merged pull", got)
114 }
115}
116
117func TestResubmitCheck_PatchBasedReturnsUnknown(t *testing.T) {
118 s := newPullsFromKnotURL("http://unused")
119 ctx := t.Context()
120 pull, _ := newForkPull(models.PullOpen)
121 pull.SourceBranch = nil
122
123 if got := s.resubmitCheck(ctx, pull); got != pages.Unknown {
124 t.Errorf("resubmitCheck() = %v, want Unknown for patch-based pull", got)
125 }
126}
127
128func TestResubmitCheck_KnotUnreachableReturnsUnknown(t *testing.T) {
129 s := newPullsFromKnotURL("http://127.0.0.1:1")
130 ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
131 defer cancel()
132 pull, _ := newForkPull(models.PullOpen)
133
134 if got := s.resubmitCheck(ctx, pull); got != pages.Unknown {
135 t.Errorf("resubmitCheck() = %v, want Unknown when knot unreachable", got)
136 }
137}