|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/julianknutsen/wasteland/internal/commons" |
| 9 | +) |
| 10 | + |
| 11 | +func TestCloseWanted_Success(t *testing.T) { |
| 12 | + t.Parallel() |
| 13 | + store := newFakeWLCommonsStore() |
| 14 | + _ = store.InsertWanted(&commons.WantedItem{ID: "w-abc", Title: "Fix bug", PostedBy: "my-rig"}) |
| 15 | + _ = store.ClaimWanted("w-abc", "my-rig") |
| 16 | + _ = store.SubmitCompletion("c-test123", "w-abc", "my-rig", "https://github.com/pr/1") |
| 17 | + |
| 18 | + err := closeWanted(store, "w-abc", "my-rig") |
| 19 | + if err != nil { |
| 20 | + t.Fatalf("closeWanted() error: %v", err) |
| 21 | + } |
| 22 | + |
| 23 | + item, _ := store.QueryWanted("w-abc") |
| 24 | + if item.Status != "completed" { |
| 25 | + t.Errorf("Status = %q, want %q", item.Status, "completed") |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +func TestCloseWanted_NotInReview(t *testing.T) { |
| 30 | + t.Parallel() |
| 31 | + tests := []struct { |
| 32 | + name string |
| 33 | + status string |
| 34 | + }{ |
| 35 | + {"open item", ""}, |
| 36 | + {"claimed item", "claimed"}, |
| 37 | + } |
| 38 | + for _, tt := range tests { |
| 39 | + t.Run(tt.name, func(t *testing.T) { |
| 40 | + t.Parallel() |
| 41 | + store := newFakeWLCommonsStore() |
| 42 | + item := &commons.WantedItem{ID: "w-abc", Title: "Fix bug", PostedBy: "my-rig"} |
| 43 | + if tt.status != "" { |
| 44 | + item.Status = tt.status |
| 45 | + } |
| 46 | + _ = store.InsertWanted(item) |
| 47 | + if tt.status == "claimed" { |
| 48 | + _ = store.ClaimWanted("w-abc", "my-rig") |
| 49 | + } |
| 50 | + |
| 51 | + err := closeWanted(store, "w-abc", "my-rig") |
| 52 | + if err == nil { |
| 53 | + t.Fatal("closeWanted() expected error for non-in_review item") |
| 54 | + } |
| 55 | + if !strings.Contains(err.Error(), "not in_review") { |
| 56 | + t.Errorf("error = %q, want to contain 'not in_review'", err.Error()) |
| 57 | + } |
| 58 | + }) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func TestCloseWanted_WrongPoster(t *testing.T) { |
| 63 | + t.Parallel() |
| 64 | + store := newFakeWLCommonsStore() |
| 65 | + _ = store.InsertWanted(&commons.WantedItem{ID: "w-abc", Title: "Fix bug", PostedBy: "poster-rig"}) |
| 66 | + _ = store.ClaimWanted("w-abc", "worker-rig") |
| 67 | + _ = store.SubmitCompletion("c-test123", "w-abc", "worker-rig", "evidence") |
| 68 | + |
| 69 | + err := closeWanted(store, "w-abc", "other-rig") |
| 70 | + if err == nil { |
| 71 | + t.Fatal("closeWanted() expected error for non-poster") |
| 72 | + } |
| 73 | + if !strings.Contains(err.Error(), "only the poster can close") { |
| 74 | + t.Errorf("error = %q, want to contain 'only the poster can close'", err.Error()) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +func TestCloseWanted_NotFound(t *testing.T) { |
| 79 | + t.Parallel() |
| 80 | + store := newFakeWLCommonsStore() |
| 81 | + |
| 82 | + err := closeWanted(store, "w-nonexistent", "my-rig") |
| 83 | + if err == nil { |
| 84 | + t.Fatal("closeWanted() expected error for missing item") |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func TestCloseWanted_StoreError(t *testing.T) { |
| 89 | + t.Parallel() |
| 90 | + store := newFakeWLCommonsStore() |
| 91 | + _ = store.InsertWanted(&commons.WantedItem{ID: "w-abc", Title: "Fix bug", Status: "in_review", PostedBy: "my-rig"}) |
| 92 | + store.CloseWantedErr = fmt.Errorf("close store error") |
| 93 | + |
| 94 | + err := closeWanted(store, "w-abc", "my-rig") |
| 95 | + if err == nil { |
| 96 | + t.Fatal("closeWanted() expected error when CloseWanted fails") |
| 97 | + } |
| 98 | + if !strings.Contains(err.Error(), "close store error") { |
| 99 | + t.Errorf("error = %q, want to contain 'close store error'", err.Error()) |
| 100 | + } |
| 101 | +} |
0 commit comments