Repository navigation
Expand file tree
/
Copy pathpull_test.go
More file actions
666 lines (608 loc) · 21.4 KB
/
Copy pathpull_test.go
File metadata and controls
666 lines (608 loc) · 21.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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
package specsync
import (
"context"
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"
)
// fakeIssue is the canned data a faked `gh issue view` returns.
type fakeIssue struct {
Number int `json:"number"`
URL string `json:"url"`
Title string `json:"title"`
Body string `json:"body"`
State string `json:"state"`
Labels []any `json:"labels"`
}
// ghRunner records calls and answers `issue view` with the given issue plus the
// minimal responses Push needs, so a pull and a follow-up push can be exercised
// against one fake.
func ghRunner(issue fakeIssue, calls *[][]string) func(context.Context, ...string) (string, error) {
return func(_ context.Context, args ...string) (string, error) {
*calls = append(*calls, args)
switch {
case len(args) >= 2 && args[0] == "issue" && args[1] == "view":
// labels query during a label reconcile asks only for labels.
if contains(args, "--json") && jsonFields(args) == "labels" {
return `{"labels":[]}`, nil
}
b, _ := json.Marshal(issue)
return string(b), nil
case len(args) >= 2 && args[0] == "issue" && args[1] == "list":
return "[]", nil
case len(args) >= 2 && args[0] == "issue" && args[1] == "create":
return issue.URL, nil
default:
return "", nil
}
}
}
func contains(args []string, want string) bool {
for _, a := range args {
if a == want {
return true
}
}
return false
}
func jsonFields(args []string) string {
for i, a := range args {
if a == "--json" && i+1 < len(args) {
return args[i+1]
}
}
return ""
}
func TestPullCreatesChangeWithTasks(t *testing.T) {
dir := t.TempDir()
issue := fakeIssue{
Number: 7,
URL: "https://github.com/o/r/issues/7",
Title: "Add CSV export",
State: "open",
Body: "<!-- specsync:change=add-csv-export -->\n\n# Add CSV export\n\n" +
"## Why\nUsers want their data.\n\n## Tasks\n\n- [ ] 1.1 build it\n- [ ] 1.2 test it\n",
}
var calls [][]string
prov := NewGitHubProviderFunc(ghRunner(issue, &calls))
res, err := Pull(context.Background(), PullOptions{
OpenSpecDir: dir,
Provider: prov,
IssueID: "7",
})
if err != nil {
t.Fatalf("Pull: %v", err)
}
if res.Slug != "add-csv-export" {
t.Fatalf("slug from marker = %q, want add-csv-export", res.Slug)
}
proposal := readFile(t, filepath.Join(dir, "changes", "add-csv-export", "proposal.md"))
if !strings.HasPrefix(proposal, "# Add CSV export") {
t.Fatalf("proposal should open with H1, got:\n%s", proposal)
}
if strings.Contains(proposal, "specsync:change=") {
t.Fatalf("proposal should not retain the identity marker:\n%s", proposal)
}
if strings.Contains(proposal, "## Tasks") {
t.Fatalf("proposal should not contain the Tasks section:\n%s", proposal)
}
tasks := readFile(t, filepath.Join(dir, "changes", "add-csv-export", "tasks.md"))
if !strings.Contains(tasks, "1.1 build it") || !strings.Contains(tasks, "1.2 test it") {
t.Fatalf("tasks.md missing checklist:\n%s", tasks)
}
}
func TestPullWithoutTasksWritesProposalOnly(t *testing.T) {
dir := t.TempDir()
issue := fakeIssue{
Number: 4083,
URL: "https://github.com/o/r/issues/4083",
Title: "Streamlined modals for integration onboarding",
State: "open",
Body: "Figma design\nhttps://example.com/figma\n",
}
var calls [][]string
prov := NewGitHubProviderFunc(ghRunner(issue, &calls))
res, err := Pull(context.Background(), PullOptions{OpenSpecDir: dir, Provider: prov, IssueID: "4083"})
if err != nil {
t.Fatalf("Pull: %v", err)
}
if res.Slug != "streamlined-modals-for-integration-onboarding" {
t.Fatalf("slug = %q", res.Slug)
}
if _, err := os.Stat(filepath.Join(dir, "changes", res.Slug, "tasks.md")); !os.IsNotExist(err) {
t.Fatalf("tasks.md should not exist for a body without a Tasks section")
}
proposal := readFile(t, filepath.Join(dir, "changes", res.Slug, "proposal.md"))
if !strings.HasPrefix(proposal, "# Streamlined modals for integration onboarding") {
t.Fatalf("proposal should be prefixed with an H1 title:\n%s", proposal)
}
if !strings.Contains(proposal, "Figma design") {
t.Fatalf("proposal should retain the issue body:\n%s", proposal)
}
}
func TestPullLinksIssueForRoundTrip(t *testing.T) {
dir := t.TempDir()
issue := fakeIssue{
Number: 7,
URL: "https://github.com/o/r/issues/7",
Title: "Round trip",
State: "open",
Body: "# Round trip\n\nbody\n",
}
var calls [][]string
prov := NewGitHubProviderFuncWithRepo("test/repo", ghRunner(issue, &calls))
res, err := Pull(context.Background(), PullOptions{OpenSpecDir: dir, Provider: prov, IssueID: "7"})
if err != nil {
t.Fatalf("Pull: %v", err)
}
// The ref cache must now bind the change to issue 7 so a push updates it.
refs, err := LoadRefs(res.Dir)
if err != nil {
t.Fatalf("loadRefs: %v", err)
}
if refs["github:test/repo"].ID != "7" {
t.Fatalf("cached ref id = %q, want 7", refs["github:test/repo"].ID)
}
// A follow-up sync of that change must edit issue 7, never create.
calls = nil
if _, err := Sync(context.Background(), Options{OpenSpecDir: dir, Provider: prov, Slug: res.Slug}); err != nil {
t.Fatalf("Sync: %v", err)
}
var edited, created bool
for _, c := range calls {
if len(c) >= 3 && c[0] == "issue" && c[1] == "edit" && c[2] == "7" {
edited = true
}
if len(c) >= 2 && c[0] == "issue" && c[1] == "create" {
created = true
}
}
if !edited {
t.Fatalf("expected an `issue edit 7`, calls: %v", calls)
}
if created {
t.Fatalf("round-trip must not create a duplicate issue, calls: %v", calls)
}
}
func TestPullDryRunWritesNothing(t *testing.T) {
dir := t.TempDir()
issue := fakeIssue{Number: 7, URL: "u", Title: "T", State: "open", Body: "# T\n\nbody\n## Tasks\n\n- [ ] 1.1 x\n"}
var calls [][]string
prov := NewGitHubProviderFunc(ghRunner(issue, &calls))
res, err := Pull(context.Background(), PullOptions{OpenSpecDir: dir, Provider: prov, IssueID: "7", DryRun: true})
if err != nil {
t.Fatalf("Pull: %v", err)
}
if res.Proposal == "" {
t.Fatalf("dry run should still render a proposal preview")
}
if _, err := os.Stat(filepath.Join(dir, "changes")); !os.IsNotExist(err) {
t.Fatalf("dry run must not create any change folder")
}
}
func readFile(t *testing.T, path string) string {
t.Helper()
b, err := os.ReadFile(path)
if err != nil {
t.Fatalf("read %s: %v", path, err)
}
return string(b)
}
func TestWorkItemForAddedCount(t *testing.T) {
baseline := 2
c := Change{
Slug: "test",
Body: "# Test\n\nBody.\n",
TasksMarkdown: "- [x] one\n- [ ] two\n- [ ] three\n",
BaselineTasks: &baseline,
}
wi := WorkItemFor(c, false)
if !strings.Contains(wi.Body, "+1 added") {
t.Errorf("expected '+1 added' in body, got:\n%s", wi.Body)
}
}
func TestWorkItemForNoAddedAtBaseline(t *testing.T) {
added := 3
c := Change{
Slug: "test",
Body: "# Test\n\nBody.\n",
TasksMarkdown: "- [x] one\n- [ ] two\n- [ ] three\n",
BaselineTasks: &added,
}
wi := WorkItemFor(c, false)
if strings.Contains(wi.Body, "added") {
t.Errorf("expected no 'added' in body, got:\n%s", wi.Body)
}
}
func TestWorkItemForNoAddedNoBaseline(t *testing.T) {
c := Change{
Slug: "test",
Body: "# Test\n\nBody.\n",
TasksMarkdown: "- [x] one\n- [ ] two\n- [ ] three\n",
}
wi := WorkItemFor(c, false)
if strings.Contains(wi.Body, "added") {
t.Errorf("expected no 'added' in body, got:\n%s", wi.Body)
}
}
func TestOriginalAskRoundTrip(t *testing.T) {
dir := t.TempDir()
issue := fakeIssue{
Number: 42,
URL: "https://github.com/o/r/issues/42",
Title: "Round trip",
State: "open",
Body: "# Round trip\n\nOriginal body text.\n\n## Tasks\n\n- [ ] one\n",
}
var calls [][]string
prov := NewGitHubProviderFunc(ghRunner(issue, &calls))
// First pull seeds original-ask.md.
res, err := Pull(context.Background(), PullOptions{
OpenSpecDir: dir,
Provider: prov,
IssueID: "42",
})
if err != nil {
t.Fatalf("Pull: %v", err)
}
// Sync renders it back into the issue.
calls = nil
if _, err := Sync(context.Background(), Options{OpenSpecDir: dir, Provider: prov, Slug: res.Slug}); err != nil {
t.Fatalf("Sync: %v", err)
}
// Re-pull should strip the managed sections and leave original-ask.md unchanged.
calls = nil
_, err = Pull(context.Background(), PullOptions{
OpenSpecDir: dir,
Provider: prov,
IssueID: "42",
})
if err != nil {
t.Fatalf("re-Pull: %v", err)
}
// Verify original-ask.md still exists and is unchanged.
askPath := filepath.Join(dir, "changes", res.Slug, "original-ask.md")
ask, err := os.ReadFile(askPath)
if err != nil {
t.Fatalf("read original-ask.md: %v", err)
}
if !strings.Contains(string(ask), "Original body text") {
t.Fatalf("original-ask.md lost content: %s", ask)
}
}
func TestSplitBodyParsesCollapsedSections(t *testing.T) {
body := "<!-- specsync:change=test -->\n\n" +
"<details>\n<summary>Proposal</summary>\n<!-- specsync:section=proposal -->\n\n" +
"# Test\n\nWhy this matters.\n\n</details>\n\n" +
"<details>\n<summary>Original ask</summary>\n<!-- specsync:section=original-ask -->\n\n" +
"The original request.\n\n</details>\n\n" +
"<details>\n<summary>Design notes</summary>\n<!-- specsync:section=design-notes -->\n\n" +
"Picked plan A.\n\n</details>\n\n" +
"<details>\n<summary>Discoveries</summary>\n<!-- specsync:section=discoveries -->\n\n" +
"Found a bug.\n\n</details>\n\n" +
"## Tasks\n\n- [ ] one\n"
proposal, tasks, _, ask, design, disc := splitBody(body, "Test")
if !strings.Contains(proposal, "Why this matters") {
t.Errorf("expected proposal content, got:\n%s", proposal)
}
if strings.Contains(proposal, "<details>") || strings.Contains(proposal, "specsync:section") {
t.Errorf("expected no leaked markup in proposal, got:\n%s", proposal)
}
if strings.TrimSpace(ask) != "The original request." {
t.Errorf("expected original ask 'The original request.', got %q", ask)
}
if strings.TrimSpace(design) != "Picked plan A." {
t.Errorf("expected design notes 'Picked plan A.', got %q", design)
}
if strings.TrimSpace(disc) != "Found a bug." {
t.Errorf("expected discoveries 'Found a bug.', got %q", disc)
}
if !strings.Contains(tasks, "- [ ] one") {
t.Errorf("expected tasks content, got:\n%s", tasks)
}
}
func TestSplitBodyFallsBackToLegacyHeadings(t *testing.T) {
// A body synced by an older specsync: bare headings, no <details>/markers.
body := "<!-- specsync:change=test -->\n\n# Test\n\nWhy this matters.\n\n" +
"## Original ask\n\nThe original request.\n\n" +
"## Design notes\n\nPicked plan A.\n\n" +
"## Discoveries\n\nFound a bug.\n\n" +
"## Tasks\n\n- [ ] one\n"
proposal, tasks, _, ask, design, disc := splitBody(body, "Test")
if strings.TrimSpace(design) != "Picked plan A." {
t.Errorf("expected design notes 'Picked plan A.', got %q", design)
}
if !strings.Contains(proposal, "Why this matters") {
t.Errorf("expected proposal content, got:\n%s", proposal)
}
if strings.TrimSpace(ask) != "The original request." {
t.Errorf("expected original ask 'The original request.', got %q", ask)
}
if strings.TrimSpace(disc) != "Found a bug." {
t.Errorf("expected discoveries 'Found a bug.', got %q", disc)
}
if !strings.Contains(tasks, "- [ ] one") {
t.Errorf("expected tasks content, got:\n%s", tasks)
}
}
func TestWorkItemForIsIdempotentAcrossRepeatedSyncs(t *testing.T) {
c := Change{
Slug: "test",
Body: "# Test\n\nWhy this matters.\n",
OriginalAsk: "Original ask.",
DesignNotes: "Picked plan A.",
Discoveries: "Discovery.",
TasksMarkdown: "- [ ] one\n",
}
first := WorkItemFor(c, false)
second := WorkItemFor(c, false)
if first.Body != second.Body {
t.Errorf("expected re-rendering the same Change to produce an identical body (no double-wrapping), got:\nfirst:\n%s\nsecond:\n%s", first.Body, second.Body)
}
if n := strings.Count(second.Body, "<!-- specsync:section=proposal -->"); n != 1 {
t.Errorf("expected exactly one proposal section marker, got %d", n)
}
if n := strings.Count(second.Body, "<!-- specsync:section=design-notes -->"); n != 1 {
t.Errorf("expected exactly one design-notes section marker, got %d", n)
}
}
func TestWorkItemForRendersOriginalAsk(t *testing.T) {
c := Change{
Slug: "test",
Body: "# Test\n\nBody.\n",
OriginalAsk: "This was the original request.",
}
wi := WorkItemFor(c, false)
if !strings.Contains(wi.Body, "<!-- specsync:section=original-ask -->") {
t.Errorf("expected original-ask section marker in body, got:\n%s", wi.Body)
}
if !strings.Contains(wi.Body, "This was the original request") {
t.Errorf("expected original ask content in body, got:\n%s", wi.Body)
}
}
func TestWorkItemForRendersDiscoveries(t *testing.T) {
c := Change{
Slug: "test",
Body: "# Test\n\nBody.\n",
Discoveries: "Found a bug in the auth flow.",
}
wi := WorkItemFor(c, false)
if !strings.Contains(wi.Body, "<!-- specsync:section=discoveries -->") {
t.Errorf("expected discoveries section marker in body, got:\n%s", wi.Body)
}
if !strings.Contains(wi.Body, "Found a bug in the auth flow") {
t.Errorf("expected discoveries content in body, got:\n%s", wi.Body)
}
}
func TestWorkItemForSectionOrder(t *testing.T) {
c := Change{
Slug: "test",
Body: "# Test\n\nBody.\n",
OriginalAsk: "Original ask.",
DesignNotes: "Design decision.",
Discoveries: "Discovery.",
TasksMarkdown: "- [ ] one\n",
Links: []Ref{{ID: "1", URL: "https://example.com/o/r/1"}},
}
wi := WorkItemFor(c, false)
oa := strings.Index(wi.Body, "<!-- specsync:section=original-ask -->")
dn := strings.Index(wi.Body, "<!-- specsync:section=design-notes -->")
di := strings.Index(wi.Body, "<!-- specsync:section=discoveries -->")
ti := strings.Index(wi.Body, "## Tasks")
ri := strings.Index(wi.Body, "## Related")
if oa >= dn || dn >= di || di >= ti || ti >= ri {
t.Errorf("sections not in order (Original ask < Design notes < Discoveries < Tasks < Related):\n%s", wi.Body)
}
}
func TestWorkItemForRendersDesignNotes(t *testing.T) {
c := Change{
Slug: "test",
Body: "# Test\n\nBody.\n",
DesignNotes: "Picked the picker pattern over a modal.",
}
wi := WorkItemFor(c, false)
if !strings.Contains(wi.Body, "<!-- specsync:section=design-notes -->") {
t.Errorf("expected design-notes section marker in body, got:\n%s", wi.Body)
}
if !strings.Contains(wi.Body, "Picked the picker pattern over a modal") {
t.Errorf("expected design notes content in body, got:\n%s", wi.Body)
}
if wi.DesignNotes != c.DesignNotes {
t.Errorf("WorkItem.DesignNotes = %q, want %q", wi.DesignNotes, c.DesignNotes)
}
}
func TestWorkItemForOmitsDesignNotesWhenEmpty(t *testing.T) {
c := Change{Slug: "test", Body: "# Test\n\nBody.\n"}
wi := WorkItemFor(c, false)
if strings.Contains(wi.Body, "specsync:section=design-notes") {
t.Errorf("expected no design-notes section, got:\n%s", wi.Body)
}
}
func TestWorkItemForCollapsesProposalAndKeepsTasksVisible(t *testing.T) {
c := Change{
Slug: "test",
Body: "# Test\n\nWhy this matters.\n",
OriginalAsk: "Original ask.",
Discoveries: "Discovery.",
TasksMarkdown: "- [ ] one\n",
}
wi := WorkItemFor(c, false)
if strings.Contains(wi.Body, "# Test\n") {
t.Errorf("expected the redundant leading H1 to be stripped from the proposal section, got:\n%s", wi.Body)
}
if !strings.Contains(wi.Body, "<!-- specsync:section=proposal -->") {
t.Errorf("expected a proposal section marker, got:\n%s", wi.Body)
}
// Tasks must not be inside any <details> block: find the last </details>
// before ## Tasks and make sure ## Tasks comes after it, not before.
ti := strings.Index(wi.Body, "## Tasks")
if ti < 0 {
t.Fatalf("expected '## Tasks' in body, got:\n%s", wi.Body)
}
if strings.Count(wi.Body[:ti], "<details>") != strings.Count(wi.Body[:ti], "</details>") {
t.Errorf("expected ## Tasks to be outside any <details> block, got:\n%s", wi.Body)
}
}
func TestWorkItemForStripsRedundantTasksH1(t *testing.T) {
c := Change{
Slug: "test",
Body: "# Test\n\nBody.\n",
TasksMarkdown: "# Tasks\n\n- [ ] one\n",
}
wi := WorkItemFor(c, false)
if n := strings.Count(wi.Body, "Tasks"); n != 1 {
t.Errorf("expected exactly one 'Tasks' heading, got %d, body:\n%s", n, wi.Body)
}
if !strings.Contains(wi.Body, "## Tasks\n\n- [ ] one") {
t.Errorf("expected '## Tasks' directly followed by the checklist, got:\n%s", wi.Body)
}
}
func TestPullSavesOriginalAsk(t *testing.T) {
dir := t.TempDir()
issue := fakeIssue{
Number: 99,
URL: "https://github.com/o/r/issues/99",
Title: "Feature",
State: "open",
Body: "# Feature\n\nThe original request.\n\n## Tasks\n\n- [ ] one\n",
}
var calls [][]string
prov := NewGitHubProviderFunc(ghRunner(issue, &calls))
res, err := Pull(context.Background(), PullOptions{
OpenSpecDir: dir,
Provider: prov,
IssueID: "99",
})
if err != nil {
t.Fatalf("Pull: %v", err)
}
askPath := filepath.Join(dir, "changes", res.Slug, "original-ask.md")
ask, err := os.ReadFile(askPath)
if err != nil {
t.Fatalf("read original-ask.md: %v", err)
}
if !strings.Contains(string(ask), "The original request") {
t.Fatalf("original-ask.md missing content: %s", ask)
}
}
// TestDesignNotesWriteOnceOnPull: design.md is write-once like
// original-ask.md, not overwritten-every-pull like discoveries.md.
func TestDesignNotesWriteOnceOnPull(t *testing.T) {
dir := t.TempDir()
issue := fakeIssue{
Number: 42,
URL: "https://github.com/o/r/issues/42",
Title: "Design write-once",
State: "open",
Body: "# Design write-once\n\nbody\n\n## Design notes\n\nSynced decision.\n",
}
var calls [][]string
prov := NewGitHubProviderFunc(ghRunner(issue, &calls))
res, err := Pull(context.Background(), PullOptions{OpenSpecDir: dir, Provider: prov, IssueID: "42"})
if err != nil {
t.Fatalf("Pull: %v", err)
}
designPath := filepath.Join(dir, "changes", res.Slug, "design.md")
design, err := os.ReadFile(designPath)
if err != nil {
t.Fatalf("read design.md: %v", err)
}
if !strings.Contains(string(design), "Synced decision") {
t.Fatalf("design.md missing synced content: %s", design)
}
richer := "Synced decision, plus a locally-written follow-up not yet pushed.\n"
if err := os.WriteFile(designPath, []byte(richer), 0o644); err != nil {
t.Fatalf("write richer design.md: %v", err)
}
if _, err := Pull(context.Background(), PullOptions{OpenSpecDir: dir, Provider: prov, IssueID: "42"}); err != nil {
t.Fatalf("re-Pull: %v", err)
}
design, err = os.ReadFile(designPath)
if err != nil {
t.Fatalf("read design.md after re-pull: %v", err)
}
if string(design) != richer {
t.Fatalf("re-pull clobbered local design.md: got %q, want %q", design, richer)
}
}
func TestPullSavesDesignNotes(t *testing.T) {
dir := t.TempDir()
issue := fakeIssue{
Number: 99,
URL: "https://github.com/o/r/issues/99",
Title: "Feature",
State: "open",
Body: "# Feature\n\nbody\n\n## Design notes\n\nPicked approach A.\n\n## Tasks\n\n- [ ] one\n",
}
var calls [][]string
prov := NewGitHubProviderFunc(ghRunner(issue, &calls))
res, err := Pull(context.Background(), PullOptions{OpenSpecDir: dir, Provider: prov, IssueID: "99"})
if err != nil {
t.Fatalf("Pull: %v", err)
}
designPath := filepath.Join(dir, "changes", res.Slug, "design.md")
design, err := os.ReadFile(designPath)
if err != nil {
t.Fatalf("read design.md: %v", err)
}
if !strings.Contains(string(design), "Picked approach A") {
t.Fatalf("design.md missing content: %s", design)
}
}
// TestPullNoDesignNotesWritesNoDesignFile: pull must not invent a design.md
// for a change whose issue has no "## Design notes" section.
func TestPullNoDesignNotesWritesNoDesignFile(t *testing.T) {
dir := t.TempDir()
issue := fakeIssue{
Number: 100,
URL: "https://github.com/o/r/issues/100",
Title: "No design notes",
State: "open",
Body: "# No design notes\n\nbody\n",
}
var calls [][]string
prov := NewGitHubProviderFunc(ghRunner(issue, &calls))
res, err := Pull(context.Background(), PullOptions{OpenSpecDir: dir, Provider: prov, IssueID: "100"})
if err != nil {
t.Fatalf("Pull: %v", err)
}
if _, err := os.Stat(filepath.Join(dir, "changes", res.Slug, "design.md")); !os.IsNotExist(err) {
t.Fatalf("design.md should not exist when the issue has no Design notes section")
}
}
func TestPull_RequiresIssueForNonGithub(t *testing.T) {
dir := t.TempDir()
_, err := Pull(context.Background(), PullOptions{
OpenSpecDir: dir,
Provider: &stubNonGithubProvider{},
IssueID: "",
})
if err == nil {
t.Fatal("expected error when no issue for non-github provider")
}
if !strings.Contains(err.Error(), "issue id is required for non-github providers") {
t.Fatalf("unexpected error: %v", err)
}
}
type stubNonGithubProvider struct{}
func (s *stubNonGithubProvider) Name() string { return "beads" }
func (s *stubNonGithubProvider) Find(context.Context, string) (*Ref, error) { return nil, nil }
func (s *stubNonGithubProvider) Push(context.Context, WorkItem, *Ref) (Ref, error) { return Ref{}, nil }
func (s *stubNonGithubProvider) Get(context.Context, string) (FetchedItem, error) { return FetchedItem{}, nil }
func TestPull_CannotResolveFromBranch(t *testing.T) {
dir := t.TempDir()
// On a branch that doesn't match the pattern, Pull should error.
_, err := Pull(context.Background(), PullOptions{
OpenSpecDir: dir,
Provider: NewGitHubProviderFuncWithRepo("o/r", func(context.Context, ...string) (string, error) { return "", nil }),
IssueID: "",
})
if err == nil {
t.Fatal("expected error when branch name doesn't match")
}
if !strings.Contains(err.Error(), "could not resolve issue from branch name") {
t.Fatalf("unexpected error: %v", err)
}
}