Debian Codegraph @

lib/pama/pama_test.go

raw
1 package pama_test
2
3 import (
4 "errors"
5 "slices"
6
7 "git.sr.ht/~rjarry/aerc/lib/pama"
8 "git.sr.ht/~rjarry/aerc/lib/pama/models"
9 )
10
11 var errNotFound = errors.New("not found")
12
13 func newCommit(id, subj, tag string) models.Commit {
14 return models.Commit{ID: id, Subject: subj, Tag: tag}
15 }
16
17 func newTestManager(
18 commits []string,
19 subjects []string,
20 data map[string]models.Project,
21 current string,
22 ) (pama.PatchManager, models.RevisionController, models.PersistentStorer) {
23 rc := mockRevctrl{
24 commitIDs: commits,
25 titles: subjects,
26 }
27 store := mockStore{
28 data: data,
29 current: current,
30 }
31 return pama.FromFunc(
32 nil,
33 func(_ string, _ string) (models.RevisionController, error) {
34 return &rc, nil
35 },
36 func() models.PersistentStorer {
37 return &store
38 },
39 ), &rc, &store
40 }
41
42 type mockRevctrl struct {
43 commitIDs []string
44 titles []string
45 }
46
47 func (c *mockRevctrl) Support() bool {
48 return true
49 }
50
51 func (c *mockRevctrl) Clean() bool {
52 return true
53 }
54
55 func (c *mockRevctrl) Root() (string, error) {
56 return "", nil
57 }
58
59 func (c *mockRevctrl) Head() (string, error) {
60 return c.commitIDs[len(c.commitIDs)-1], nil
61 }
62
63 func (c *mockRevctrl) History(commit string) ([]string, error) {
64 for i, s := range c.commitIDs {
65 if s == commit {
66 cp := make([]string, len(c.commitIDs[i+1:]))
67 copy(cp, c.commitIDs[i+1:])
68 return cp, nil
69 }
70 }
71 return nil, errNotFound
72 }
73
74 func (c *mockRevctrl) Exists(commit string) bool {
75 return slices.Contains(c.commitIDs, commit)
76 }
77
78 func (c *mockRevctrl) Subject(commit string) string {
79 for i, s := range c.commitIDs {
80 if s == commit {
81 return c.titles[i]
82 }
83 }
84 return ""
85 }
86
87 func (c *mockRevctrl) Author(commit string) string {
88 return ""
89 }
90
91 func (c *mockRevctrl) Date(commit string) string {
92 return ""
93 }
94
95 func (c *mockRevctrl) Drop(commit string) error {
96 for i, s := range c.commitIDs {
97 if s == commit {
98 c.commitIDs = append(c.commitIDs[:i], c.commitIDs[i+1:]...)
99 c.titles = append(c.titles[:i], c.titles[i+1:]...)
100 // modify commitIDs to simulate a "real" change in
101 // commit history that will also change all subsequent
102 // commitIDs
103 for j := i; j < len(c.commitIDs); j++ {
104 c.commitIDs[j] += "_new"
105 }
106 return nil
107 }
108 }
109 return errNotFound
110 }
111
112 func (c *mockRevctrl) CreateWorktree(_, _ string) error {
113 return nil
114 }
115
116 func (c *mockRevctrl) DeleteWorktree(_ string) error {
117 return nil
118 }
119
120 func (c *mockRevctrl) ApplyCmd() string {
121 return ""
122 }
123
124 type mockStore struct {
125 data map[string]models.Project
126 current string
127 }
128
129 func (s *mockStore) StoreProject(p models.Project, ow bool) error {
130 _, ok := s.data[p.Name]
131 if ok && !ow {
132 return errors.New("already there")
133 }
134 s.data[p.Name] = p
135 return nil
136 }
137
138 func (s *mockStore) DeleteProject(name string) error {
139 delete(s.data, name)
140 return nil
141 }
142
143 func (s *mockStore) CurrentName() (string, error) {
144 return s.current, nil
145 }
146
147 func (s *mockStore) SetCurrent(c string) error {
148 s.current = c
149 return nil
150 }
151
152 func (s *mockStore) Current() (models.Project, error) {
153 return s.data[s.current], nil
154 }
155
156 func (s *mockStore) Names() ([]string, error) {
157 var names []string
158 for name := range s.data {
159 names = append(names, name)
160 }
161 return names, nil
162 }
163
164 func (s *mockStore) Project(_ string) (models.Project, error) {
165 return models.Project{}, nil
166 }
167
168 func (s *mockStore) Projects() ([]models.Project, error) {
169 var ps []models.Project
170 for _, p := range s.data {
171 ps = append(ps, p)
172 }
173 return ps, nil
174 }