Add three new services completing the final service layer: - WikiService: CRUD operations for repository wiki pages - MiscService: markdown rendering, licence/gitignore templates, nodeinfo, version - CommitService: commit statuses (list, create, combined) and git notes - PostRaw method on Client for endpoints returning raw text (e.g. /markdown) - Remove services_stub.go (all stubs now replaced with real implementations) - Wire Commits field into Forge struct Co-Authored-By: Virgil <virgil@lethean.io> Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
227 lines
6 KiB
Go
227 lines
6 KiB
Go
package forge
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"forge.lthn.ai/core/go-forge/types"
|
|
)
|
|
|
|
func TestMiscService_Good_RenderMarkdown(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/markdown" {
|
|
t.Errorf("wrong path: %s", r.URL.Path)
|
|
}
|
|
var opts types.MarkdownOption
|
|
if err := json.NewDecoder(r.Body).Decode(&opts); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if opts.Text != "# Hello" {
|
|
t.Errorf("got text=%q, want %q", opts.Text, "# Hello")
|
|
}
|
|
if opts.Mode != "gfm" {
|
|
t.Errorf("got mode=%q, want %q", opts.Mode, "gfm")
|
|
}
|
|
w.Header().Set("Content-Type", "text/html")
|
|
w.Write([]byte("<h1>Hello</h1>\n"))
|
|
}))
|
|
defer srv.Close()
|
|
|
|
f := NewForge(srv.URL, "tok")
|
|
html, err := f.Misc.RenderMarkdown(context.Background(), "# Hello", "gfm")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want := "<h1>Hello</h1>\n"
|
|
if html != want {
|
|
t.Errorf("got %q, want %q", html, want)
|
|
}
|
|
}
|
|
|
|
func TestMiscService_Good_GetVersion(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/version" {
|
|
t.Errorf("wrong path: %s", r.URL.Path)
|
|
}
|
|
json.NewEncoder(w).Encode(types.ServerVersion{
|
|
Version: "1.21.0",
|
|
})
|
|
}))
|
|
defer srv.Close()
|
|
|
|
f := NewForge(srv.URL, "tok")
|
|
ver, err := f.Misc.GetVersion(context.Background())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if ver.Version != "1.21.0" {
|
|
t.Errorf("got version=%q, want %q", ver.Version, "1.21.0")
|
|
}
|
|
}
|
|
|
|
func TestMiscService_Good_ListLicenses(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/licenses" {
|
|
t.Errorf("wrong path: %s", r.URL.Path)
|
|
}
|
|
json.NewEncoder(w).Encode([]types.LicensesTemplateListEntry{
|
|
{Key: "mit", Name: "MIT License"},
|
|
{Key: "gpl-3.0", Name: "GNU General Public License v3.0"},
|
|
})
|
|
}))
|
|
defer srv.Close()
|
|
|
|
f := NewForge(srv.URL, "tok")
|
|
licenses, err := f.Misc.ListLicenses(context.Background())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(licenses) != 2 {
|
|
t.Fatalf("got %d licenses, want 2", len(licenses))
|
|
}
|
|
if licenses[0].Key != "mit" {
|
|
t.Errorf("got key=%q, want %q", licenses[0].Key, "mit")
|
|
}
|
|
if licenses[1].Key != "gpl-3.0" {
|
|
t.Errorf("got key=%q, want %q", licenses[1].Key, "gpl-3.0")
|
|
}
|
|
}
|
|
|
|
func TestMiscService_Good_GetLicense(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/licenses/mit" {
|
|
t.Errorf("wrong path: %s", r.URL.Path)
|
|
}
|
|
json.NewEncoder(w).Encode(types.LicenseTemplateInfo{
|
|
Key: "mit",
|
|
Name: "MIT License",
|
|
Body: "MIT License body text...",
|
|
})
|
|
}))
|
|
defer srv.Close()
|
|
|
|
f := NewForge(srv.URL, "tok")
|
|
lic, err := f.Misc.GetLicense(context.Background(), "mit")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if lic.Key != "mit" {
|
|
t.Errorf("got key=%q, want %q", lic.Key, "mit")
|
|
}
|
|
if lic.Name != "MIT License" {
|
|
t.Errorf("got name=%q, want %q", lic.Name, "MIT License")
|
|
}
|
|
}
|
|
|
|
func TestMiscService_Good_ListGitignoreTemplates(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/gitignore/templates" {
|
|
t.Errorf("wrong path: %s", r.URL.Path)
|
|
}
|
|
json.NewEncoder(w).Encode([]string{"Go", "Python", "Node"})
|
|
}))
|
|
defer srv.Close()
|
|
|
|
f := NewForge(srv.URL, "tok")
|
|
names, err := f.Misc.ListGitignoreTemplates(context.Background())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(names) != 3 {
|
|
t.Fatalf("got %d templates, want 3", len(names))
|
|
}
|
|
if names[0] != "Go" {
|
|
t.Errorf("got [0]=%q, want %q", names[0], "Go")
|
|
}
|
|
}
|
|
|
|
func TestMiscService_Good_GetGitignoreTemplate(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/gitignore/templates/Go" {
|
|
t.Errorf("wrong path: %s", r.URL.Path)
|
|
}
|
|
json.NewEncoder(w).Encode(types.GitignoreTemplateInfo{
|
|
Name: "Go",
|
|
Source: "*.exe\n*.test\n/vendor/",
|
|
})
|
|
}))
|
|
defer srv.Close()
|
|
|
|
f := NewForge(srv.URL, "tok")
|
|
tmpl, err := f.Misc.GetGitignoreTemplate(context.Background(), "Go")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if tmpl.Name != "Go" {
|
|
t.Errorf("got name=%q, want %q", tmpl.Name, "Go")
|
|
}
|
|
}
|
|
|
|
func TestMiscService_Good_GetNodeInfo(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/nodeinfo" {
|
|
t.Errorf("wrong path: %s", r.URL.Path)
|
|
}
|
|
json.NewEncoder(w).Encode(types.NodeInfo{
|
|
Version: "2.1",
|
|
Software: &types.NodeInfoSoftware{
|
|
Name: "forgejo",
|
|
Version: "1.21.0",
|
|
},
|
|
})
|
|
}))
|
|
defer srv.Close()
|
|
|
|
f := NewForge(srv.URL, "tok")
|
|
info, err := f.Misc.GetNodeInfo(context.Background())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if info.Version != "2.1" {
|
|
t.Errorf("got version=%q, want %q", info.Version, "2.1")
|
|
}
|
|
if info.Software.Name != "forgejo" {
|
|
t.Errorf("got software name=%q, want %q", info.Software.Name, "forgejo")
|
|
}
|
|
}
|
|
|
|
func TestMiscService_Bad_NotFound(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusNotFound)
|
|
json.NewEncoder(w).Encode(map[string]string{"message": "not found"})
|
|
}))
|
|
defer srv.Close()
|
|
|
|
f := NewForge(srv.URL, "tok")
|
|
_, err := f.Misc.GetLicense(context.Background(), "nonexistent")
|
|
if err == nil {
|
|
t.Fatal("expected error, got nil")
|
|
}
|
|
if !IsNotFound(err) {
|
|
t.Errorf("expected not-found error, got %v", err)
|
|
}
|
|
}
|