Repository navigation
Expand file tree
/
Copy pathepic_test.go
More file actions
446 lines (400 loc) · 13.4 KB
/
Copy pathepic_test.go
File metadata and controls
446 lines (400 loc) · 13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
package specsync
import (
"context"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"testing"
)
// fakeEpicIssue is one in-memory issue tracked by fakeEpicProvider.
type fakeEpicIssue struct {
title string
body string
labels []string
}
// fakeEpicProvider is an in-memory WorkProvider + IssueReader that mirrors
// GitHubProvider's Push/Find/marker semantics closely enough to exercise
// Epic's idempotency without shelling out to gh. One instance models one repo.
type fakeEpicProvider struct {
repo string
issues map[string]*fakeEpicIssue
nextID int
creates int // number of "issue create" calls made, for idempotency assertions
}
func newFakeEpicProvider(repo string) *fakeEpicProvider {
return &fakeEpicProvider{repo: repo, issues: map[string]*fakeEpicIssue{}}
}
func (f *fakeEpicProvider) Name() string { return "github:" + f.repo }
func (f *fakeEpicProvider) Push(_ context.Context, item WorkItem, existing *Ref) (Ref, error) {
body := marker(item.Slug) + "\n\n" + item.Body
if existing == nil {
if found, _ := f.Find(context.Background(), item.Slug); found != nil {
existing = found
}
}
if existing == nil {
f.nextID++
f.creates++
id := strconv.Itoa(f.nextID)
f.issues[id] = &fakeEpicIssue{title: item.Title, body: body, labels: item.Labels}
return Ref{Provider: f.Name(), ID: id, URL: f.urlFor(id)}, nil
}
iss, ok := f.issues[existing.ID]
if !ok {
return Ref{}, fmt.Errorf("fakeEpicProvider: issue %s not found", existing.ID)
}
iss.title = item.Title
iss.body = body
// Mirror labelDelta's "add" side: desired-but-not-current labels are
// always added, unconditionally.
current := map[string]bool{}
for _, l := range iss.labels {
current[l] = true
}
for _, l := range item.Labels {
if !current[l] {
iss.labels = append(iss.labels, l)
}
}
return *existing, nil
}
func (f *fakeEpicProvider) Find(_ context.Context, slug string) (*Ref, error) {
want := marker(slug)
for id, iss := range f.issues {
if strings.Contains(iss.body, want) {
return &Ref{Provider: f.Name(), ID: id, URL: f.urlFor(id)}, nil
}
}
return nil, nil
}
func (f *fakeEpicProvider) Get(_ context.Context, id string) (FetchedItem, error) {
iss, ok := f.issues[id]
if !ok {
return FetchedItem{}, fmt.Errorf("fakeEpicProvider: issue %s not found", id)
}
return FetchedItem{ID: id, URL: f.urlFor(id), Title: iss.title, Body: iss.body, Labels: iss.labels}, nil
}
func (f *fakeEpicProvider) urlFor(id string) string {
return "https://github.com/" + f.repo + "/issues/" + id
}
// seed pre-populates issue id with a body containing no marker (an existing
// bare issue reference, not a specsync-managed change).
func (f *fakeEpicProvider) seed(id, title, body string, labels []string) {
if f.nextID < mustAtoi(id) {
f.nextID = mustAtoi(id)
}
f.issues[id] = &fakeEpicIssue{title: title, body: body, labels: labels}
}
func mustAtoi(s string) int {
n, _ := strconv.Atoi(s)
return n
}
func TestEpic_CreatesEpicAndWiresIssueRefChildren(t *testing.T) {
root := t.TempDir()
openspecDir := filepath.Join(root, "openspec")
os.MkdirAll(openspecDir, 0o755)
planning := newFakeEpicProvider("org/planning")
backend := newFakeEpicProvider("org/backend")
backend.seed("12", "Add backend support", "some existing description", nil)
providerFor := func(repo string) WorkProvider {
if repo == "org/backend" {
return backend
}
return planning
}
res, err := Epic(context.Background(), EpicOptions{
OpenSpecDir: openspecDir,
Title: "Feature X: cross-repo widgets",
Repo: "org/planning",
Children: []string{"org/backend#12"},
EpicProvider: planning,
ChildProviderFor: providerFor,
})
if err != nil {
t.Fatalf("Epic: %v", err)
}
if !res.Created {
t.Error("want Created=true on first invocation")
}
if len(res.Children) != 1 {
t.Fatalf("want 1 child, got %d", len(res.Children))
}
if res.Children[0].Ref.ID != "12" || res.Children[0].Repo != "org/backend" {
t.Errorf("child = %+v", res.Children[0])
}
// Epic issue's body must list the child in a "## Related" section.
epicIssue := planning.issues[res.Ref.ID]
if !strings.Contains(epicIssue.body, "## Related") || !strings.Contains(epicIssue.body, "org/backend#12") {
t.Errorf("epic body missing Related child:\n%s", epicIssue.body)
}
// Epic issue must carry the type:epic + specsync labels.
if !containsStr2(epicIssue.labels, "type:epic") || !containsStr2(epicIssue.labels, "specsync") {
t.Errorf("epic labels = %v, want type:epic and specsync", epicIssue.labels)
}
// Child issue's body must be edited to point back at the epic.
childIssue := backend.issues["12"]
if !strings.Contains(childIssue.body, "## Related") || !strings.Contains(childIssue.body, epicIssue.title) && !strings.Contains(childIssue.body, res.Ref.URL) {
t.Errorf("child body missing backlink to epic:\n%s", childIssue.body)
}
if !strings.Contains(childIssue.body, "some existing description") {
t.Errorf("child body lost its original content:\n%s", childIssue.body)
}
}
func TestEpic_IdempotentRerun(t *testing.T) {
root := t.TempDir()
openspecDir := filepath.Join(root, "openspec")
os.MkdirAll(openspecDir, 0o755)
planning := newFakeEpicProvider("org/planning")
backend := newFakeEpicProvider("org/backend")
backend.seed("12", "Add backend support", "desc", nil)
providerFor := func(repo string) WorkProvider {
if repo == "org/backend" {
return backend
}
return planning
}
opts := EpicOptions{
OpenSpecDir: openspecDir,
Title: "Feature X: cross-repo widgets",
Repo: "org/planning",
Children: []string{"org/backend#12"},
EpicProvider: planning,
ChildProviderFor: providerFor,
}
first, err := Epic(context.Background(), opts)
if err != nil {
t.Fatalf("Epic (first): %v", err)
}
firstBody := planning.issues[first.Ref.ID].body
second, err := Epic(context.Background(), opts)
if err != nil {
t.Fatalf("Epic (second): %v", err)
}
if planning.creates != 1 {
t.Errorf("want exactly 1 epic issue created across both runs, got %d", planning.creates)
}
if second.Ref.ID != first.Ref.ID {
t.Errorf("second run created a different epic issue: %s vs %s", second.Ref.ID, first.Ref.ID)
}
if second.Created {
t.Error("second run must converge onto the existing epic, not report Created")
}
if len(planning.issues) != 1 {
t.Errorf("want exactly 1 epic issue in the store, got %d", len(planning.issues))
}
secondBody := planning.issues[second.Ref.ID].body
if secondBody != firstBody {
t.Errorf("re-run changed the epic body content:\nfirst: %q\nsecond: %q", firstBody, secondBody)
}
// The child issue's marker must not have duplicated across re-runs.
childBody := backend.issues["12"].body
if n := strings.Count(childBody, "<!-- specsync:change="); n != 1 {
t.Errorf("want exactly 1 identity marker in child body after 2 runs, got %d:\n%s", n, childBody)
}
}
func TestEpic_MixedSlugAndIssueRefChildren(t *testing.T) {
root := t.TempDir()
openspecDir := filepath.Join(root, "openspec")
cdir := filepath.Join(openspecDir, "changes", "frontend-widget-view")
os.MkdirAll(cdir, 0o755)
os.WriteFile(filepath.Join(cdir, "proposal.md"), []byte("# frontend-widget-view\n\nbody\n"), 0o644)
os.WriteFile(filepath.Join(cdir, "tasks.md"), []byte("- [ ] task\n"), 0o644)
planning := newFakeEpicProvider("org/planning")
frontendRepo := newFakeEpicProvider("") // "" = current repo (auto-detect), matches slug children
backend := newFakeEpicProvider("org/backend")
backend.seed("12", "Add backend support", "desc", nil)
providerFor := func(repo string) WorkProvider {
switch repo {
case "org/backend":
return backend
case "":
return frontendRepo
default:
return planning
}
}
res, err := Epic(context.Background(), EpicOptions{
OpenSpecDir: openspecDir,
Title: "Feature X: cross-repo widgets",
Repo: "org/planning",
Children: []string{"org/backend#12", "frontend-widget-view"},
EpicProvider: planning,
ChildProviderFor: providerFor,
})
if err != nil {
t.Fatalf("Epic: %v", err)
}
if len(res.Children) != 2 {
t.Fatalf("want 2 children, got %d", len(res.Children))
}
var sawSlug, sawIssue bool
for _, c := range res.Children {
switch c.Kind {
case "slug":
sawSlug = true
if c.Slug != "frontend-widget-view" {
t.Errorf("slug child slug = %q", c.Slug)
}
if !c.Synced {
t.Error("unsynced slug child must be reported as synced")
}
case "issue":
sawIssue = true
if c.Ref.ID != "12" {
t.Errorf("issue child ID = %q", c.Ref.ID)
}
default:
t.Errorf("unexpected kind %q", c.Kind)
}
}
if !sawSlug || !sawIssue {
t.Errorf("want both kinds present: slug=%v issue=%v", sawSlug, sawIssue)
}
// The slug child must now have a ref persisted on disk.
refs, err := LoadRefs(cdir)
if err != nil {
t.Fatalf("LoadRefs: %v", err)
}
if len(refs) == 0 {
t.Error("slug child should have a persisted ref after being synced")
}
// The slug child's issue must carry a backlink to the epic.
var slugChildRef Ref
for _, c := range res.Children {
if c.Kind == "slug" {
slugChildRef = c.Ref
}
}
iss, ok := frontendRepo.issues[slugChildRef.ID]
if !ok {
t.Fatalf("slug child issue %s not found in frontend provider", slugChildRef.ID)
}
if !strings.Contains(iss.body, "## Related") {
t.Errorf("slug child body missing backlink to epic:\n%s", iss.body)
}
if n := strings.Count(iss.body, "<!-- specsync:change="); n != 1 {
t.Errorf("want exactly 1 identity marker in slug child body, got %d:\n%s", n, iss.body)
}
}
func TestEpic_DryRunDoesNotPersistSlugRef(t *testing.T) {
root := t.TempDir()
openspecDir := filepath.Join(root, "openspec")
cdir := filepath.Join(openspecDir, "changes", "frontend-widget-view")
os.MkdirAll(cdir, 0o755)
os.WriteFile(filepath.Join(cdir, "proposal.md"), []byte("# frontend-widget-view\n\nbody\n"), 0o644)
os.WriteFile(filepath.Join(cdir, "tasks.md"), []byte("- [ ] task\n"), 0o644)
planning := newFakeEpicProvider("org/planning")
frontendRepo := newFakeEpicProvider("")
providerFor := func(repo string) WorkProvider {
if repo == "" {
return frontendRepo
}
return planning
}
res, err := Epic(context.Background(), EpicOptions{
OpenSpecDir: openspecDir,
Title: "Feature X: cross-repo widgets",
Repo: "org/planning",
Children: []string{"frontend-widget-view"},
DryRun: true,
EpicProvider: planning,
ChildProviderFor: providerFor,
})
if err != nil {
t.Fatalf("Epic: %v", err)
}
if len(res.Children) != 1 {
t.Fatalf("want 1 child, got %d", len(res.Children))
}
refs, err := LoadRefs(cdir)
if err != nil {
t.Fatalf("LoadRefs: %v", err)
}
if len(refs) != 0 {
t.Errorf("dry run must not persist a ref to disk, got %v", refs)
}
}
func TestEpic_UnknownChildErrors(t *testing.T) {
root := t.TempDir()
openspecDir := filepath.Join(root, "openspec")
os.MkdirAll(openspecDir, 0o755)
planning := newFakeEpicProvider("org/planning")
_, err := Epic(context.Background(), EpicOptions{
OpenSpecDir: openspecDir,
Title: "Feature X",
Repo: "org/planning",
Children: []string{"nonexistent-slug"},
EpicProvider: planning,
ChildProviderFor: func(string) WorkProvider { return planning },
})
if err == nil {
t.Fatal("want an error for an unresolvable --child argument")
}
if planning.creates != 0 {
t.Error("a bad --child argument must fail before the epic issue is created")
}
}
func TestEpic_RequiresTitle(t *testing.T) {
planning := newFakeEpicProvider("org/planning")
_, err := Epic(context.Background(), EpicOptions{
OpenSpecDir: t.TempDir(),
Title: " ",
EpicProvider: planning,
ChildProviderFor: func(string) WorkProvider { return planning },
})
if err == nil {
t.Fatal("want an error for an empty title")
}
}
func TestStripLeadingMarker(t *testing.T) {
tests := []struct {
name string
body string
want string
}{
{"no marker", "hello world", "hello world"},
{"marker present", "<!-- specsync:change=foo -->\n\nhello world", "hello world"},
{"marker with leading blank lines", "\n<!-- specsync:change=foo -->\n\nhello", "hello"},
{"empty body", "", ""},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := stripLeadingMarker(tt.body)
if got != tt.want {
t.Errorf("stripLeadingMarker(%q) = %q, want %q", tt.body, got, tt.want)
}
})
}
}
func TestPushRelatedEdit_NoDoubleMarkerAcrossRuns(t *testing.T) {
prov := newFakeEpicProvider("owner/repo")
prov.seed("1", "My Change", marker("my-change")+"\n\noriginal content", []string{"specsync"})
ref := Ref{Provider: prov.Name(), ID: "1", URL: prov.urlFor("1")}
other := Ref{Provider: "github:owner/other", ID: "9", URL: "https://github.com/owner/other/issues/9"}
if _, err := PushRelatedEdit(context.Background(), prov, ref, "my-change", []Ref{other}); err != nil {
t.Fatalf("PushRelatedEdit (first): %v", err)
}
if _, err := PushRelatedEdit(context.Background(), prov, ref, "my-change", []Ref{other}); err != nil {
t.Fatalf("PushRelatedEdit (second): %v", err)
}
body := prov.issues["1"].body
if n := strings.Count(body, "<!-- specsync:change="); n != 1 {
t.Errorf("want exactly 1 marker after 2 runs, got %d:\n%s", n, body)
}
if !strings.Contains(body, "original content") {
t.Errorf("original content lost:\n%s", body)
}
if !strings.Contains(body, "owner/other#9") {
t.Errorf("missing related link:\n%s", body)
}
}
func containsStr2(list []string, want string) bool {
for _, s := range list {
if s == want {
return true
}
}
return false
}