go-forge/issues_test.go
Snider 107c78dc07
All checks were successful
Security Scan / security (pull_request) Successful in 8s
Test / test (pull_request) Successful in 49s
test(services): road-test resource CRUD paths
Co-Authored-By: Virgil <virgil@lethean.io>
2026-03-22 14:13:40 +00:00

223 lines
6.5 KiB
Go

package forge
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"dappco.re/go/core/forge/types"
)
func TestIssueService_Good_List(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
t.Errorf("expected GET, got %s", r.Method)
}
if r.URL.Path != "/api/v1/repos/core/go-forge/issues" {
t.Errorf("wrong path: %s", r.URL.Path)
http.NotFound(w, r)
return
}
w.Header().Set("X-Total-Count", "2")
json.NewEncoder(w).Encode([]types.Issue{
{ID: 1, Title: "bug report"},
{ID: 2, Title: "feature request"},
})
}))
defer srv.Close()
f := NewForge(srv.URL, "tok")
result, err := f.Issues.List(context.Background(), Params{"owner": "core", "repo": "go-forge"}, DefaultList)
if err != nil {
t.Fatal(err)
}
if len(result.Items) != 2 {
t.Errorf("got %d items, want 2", len(result.Items))
}
if result.Items[0].Title != "bug report" {
t.Errorf("got title=%q, want %q", result.Items[0].Title, "bug report")
}
}
func TestIssueService_Good_Get(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
t.Errorf("expected GET, got %s", r.Method)
}
if r.URL.Path != "/api/v1/repos/core/go-forge/issues/1" {
t.Errorf("wrong path: %s", r.URL.Path)
}
json.NewEncoder(w).Encode(types.Issue{ID: 1, Title: "bug report", Index: 1})
}))
defer srv.Close()
f := NewForge(srv.URL, "tok")
issue, err := f.Issues.Get(context.Background(), Params{"owner": "core", "repo": "go-forge", "index": "1"})
if err != nil {
t.Fatal(err)
}
if issue.Title != "bug report" {
t.Errorf("got title=%q", issue.Title)
}
}
func TestIssueService_Good_Create(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
t.Errorf("expected POST, got %s", r.Method)
}
if r.URL.Path != "/api/v1/repos/core/go-forge/issues" {
t.Errorf("wrong path: %s", r.URL.Path)
http.NotFound(w, r)
return
}
var body types.CreateIssueOption
json.NewDecoder(r.Body).Decode(&body)
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(types.Issue{ID: 1, Title: body.Title, Index: 1})
}))
defer srv.Close()
f := NewForge(srv.URL, "tok")
issue, err := f.Issues.Create(context.Background(), Params{"owner": "core", "repo": "go-forge"}, &types.CreateIssueOption{
Title: "new issue",
Body: "description here",
})
if err != nil {
t.Fatal(err)
}
if issue.Title != "new issue" {
t.Errorf("got title=%q", issue.Title)
}
}
func TestIssueService_Good_Update(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPatch {
t.Errorf("expected PATCH, got %s", r.Method)
}
if r.URL.Path != "/api/v1/repos/core/go-forge/issues/1" {
t.Errorf("wrong path: %s", r.URL.Path)
http.NotFound(w, r)
return
}
var body types.EditIssueOption
json.NewDecoder(r.Body).Decode(&body)
json.NewEncoder(w).Encode(types.Issue{ID: 1, Title: body.Title, Index: 1})
}))
defer srv.Close()
f := NewForge(srv.URL, "tok")
issue, err := f.Issues.Update(context.Background(), Params{"owner": "core", "repo": "go-forge", "index": "1"}, &types.EditIssueOption{
Title: "updated issue",
})
if err != nil {
t.Fatal(err)
}
if issue.Title != "updated issue" {
t.Errorf("got title=%q", issue.Title)
}
}
func TestIssueService_Good_Delete(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodDelete {
t.Errorf("expected DELETE, got %s", r.Method)
}
if r.URL.Path != "/api/v1/repos/core/go-forge/issues/1" {
t.Errorf("wrong path: %s", r.URL.Path)
http.NotFound(w, r)
return
}
w.WriteHeader(http.StatusNoContent)
}))
defer srv.Close()
f := NewForge(srv.URL, "tok")
if err := f.Issues.Delete(context.Background(), Params{"owner": "core", "repo": "go-forge", "index": "1"}); err != nil {
t.Fatal(err)
}
}
func TestIssueService_Good_CreateComment(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
t.Errorf("expected POST, got %s", r.Method)
}
if r.URL.Path != "/api/v1/repos/core/go-forge/issues/1/comments" {
t.Errorf("wrong path: %s", r.URL.Path)
http.NotFound(w, r)
return
}
var body types.CreateIssueCommentOption
json.NewDecoder(r.Body).Decode(&body)
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(types.Comment{ID: 7, Body: body.Body})
}))
defer srv.Close()
f := NewForge(srv.URL, "tok")
comment, err := f.Issues.CreateComment(context.Background(), "core", "go-forge", 1, "first!")
if err != nil {
t.Fatal(err)
}
if comment.Body != "first!" {
t.Errorf("got body=%q", comment.Body)
}
}
func TestIssueService_Good_Pin(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
t.Errorf("expected POST, got %s", r.Method)
}
if r.URL.Path != "/api/v1/repos/core/go-forge/issues/42/pin" {
t.Errorf("wrong path: %s", r.URL.Path)
}
w.WriteHeader(http.StatusNoContent)
}))
defer srv.Close()
f := NewForge(srv.URL, "tok")
err := f.Issues.Pin(context.Background(), "core", "go-forge", 42)
if err != nil {
t.Fatal(err)
}
}
func TestIssueService_Bad_List(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(map[string]string{"message": "boom"})
}))
defer srv.Close()
f := NewForge(srv.URL, "tok")
if _, err := f.Issues.List(context.Background(), Params{"owner": "core", "repo": "go-forge"}, DefaultList); err == nil {
t.Fatal("expected error")
}
}
func TestIssueService_Ugly_ListIgnoresIndexParam(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/v1/repos/core/go-forge/issues" {
t.Errorf("wrong path: %s", r.URL.Path)
http.NotFound(w, r)
return
}
w.Header().Set("X-Total-Count", "0")
json.NewEncoder(w).Encode([]types.Issue{})
}))
defer srv.Close()
f := NewForge(srv.URL, "tok")
result, err := f.Issues.List(context.Background(), Params{"owner": "core", "repo": "go-forge", "index": "99"}, DefaultList)
if err != nil {
t.Fatal(err)
}
if len(result.Items) != 0 {
t.Errorf("got %d items, want 0", len(result.Items))
}
}