Files
gitea-tea/internal/services/comments_test.go
T
bircni 12e415a7a7 refactor: add services layer on the Gitea Go SDK (#1125)
- `internal/services`: typed Gitea workflows built on `gitea.dev/sdk`, independent of CLI flags and terminal I/O: issues, pull requests, reviews, comments, labels, milestones, releases and assets, repositories, branches, wiki, Actions (workflows, runs, jobs, logs, artifacts, secrets, variables), organizations, teams, webhooks, notifications, SSH keys, tracked time, admin users, search and status.
- SDK types are mapped to tea's own output types, so `--json` stays a stable contract. Requests the SDK cannot express yet (e.g. pull list filters, multi-status run lists, org label archiving) go through small raw helpers, each marked `TODO(sdk)` with the missing SDK piece.
- Targets Gitea 28 or newer: no version or API-schema probing. List helpers fill pages capped by `MAX_RESPONSE_ITEMS`.

Nothing imports this package yet.

Reviewed-on: https://gitea.com/gitea/tea/pulls/1125
Reviewed-by: TheFox0x7 <95654+thefox0x7@noreply.gitea.com>
2026-10-11 17:15:41 +00:00

64 lines
2.3 KiB
Go

// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package services
import (
"context"
"encoding/json"
"net/http"
"testing"
gitea "gitea.dev/sdk"
"github.com/stretchr/testify/require"
)
func TestCommentFromMapsServerFields(t *testing.T) {
require.Equal(t, Comment{ID: 2, Body: "text", Author: "alice", CreatedAt: "2026-10-01T12:00:00Z", UpdatedAt: "2026-10-02T12:00:00+02:00"},
commentFrom(decode[gitea.Comment](t, `{"id":2,"body":"text","user":{"login":"alice"},"created_at":"2026-10-01T12:00:00Z","updated_at":"2026-10-02T12:00:00+02:00"}`)))
}
func TestCommentPaginationIsLocal(t *testing.T) {
client := newTestClient(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
require.Equal(t, "0", r.URL.Query().Get("limit"))
_, _ = w.Write([]byte(`[{"id":1},{"id":2},{"id":3}]`))
return
}
var body map[string]string
require.NoError(t, json.NewDecoder(r.Body).Decode(&body))
require.Equal(t, "text", body["body"])
require.Equal(t, "/api/v1/repos/alice/project/issues/7/comments", r.URL.Path)
_, _ = w.Write([]byte(`{"id":8,"body":"text","html_url":"https://example.test/comment","user":{"login":"alice"}}`))
}))
service := Comments{Client: client, Repository: "alice/project"}
items, err := service.Page(context.Background(), 7, 2, 2)
require.NoError(t, err)
require.Len(t, items, 1)
require.Equal(t, int64(3), items[0].ID)
items, err = service.Page(context.Background(), 7, 2, 3)
require.NoError(t, err)
require.Empty(t, items)
require.NotNil(t, items)
item, err := service.Create(context.Background(), 7, "text")
require.NoError(t, err)
require.Equal(t, "alice", item.Author)
require.Equal(t, "https://example.test/comment", item.URL)
}
func TestRemoveMilestoneRejectsDifferentMembership(t *testing.T) {
mutations := 0
client := newTestClient(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
mutations++
w.WriteHeader(http.StatusNoContent)
return
}
_, _ = w.Write([]byte(`{"milestone":{"id":9}}`))
}))
service := Issues{Client: client, Repository: "alice/project"}
_, err := service.RemoveMilestone(context.Background(), 7, 8)
require.ErrorContains(t, err, "not assigned to milestone 8")
require.Zero(t, mutations)
}