caelum0x 49b183818c Unify gitgram + aitimemachine into the monorepo (de-submodule) and merge vendored engines
Convert gitgram/ and aitimemachine/ from embedded git repos (which showed as
opaque submodule gitlinks on GitHub) into fully-tracked first-party content.

AITM now merges its 7 vendored repos as first-party engines:
- memory: beads (Go task DAG) + context7 (docs SDK), unified into saved_contexts
- git: vendored C git (bundled binary) + gitnexus (in-process code intelligence)
- agentic: a2a (@a2a-js/sdk) + pal (AITM-managed Python subprocess)

Includes the full session's product build: Gitgram web/mobile/backend features,
AITM website/CLI/MCP, the unified memory layer, engine build tooling
(scripts/build-engines.sh, Dockerfile, CI), docs (README, ENGINES.md, ARCHITECTURE.md),
and extensive test suites.

Build artifacts, node_modules, external/, and gemini-cli are gitignored.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-23 14:14:09 +03:00

414 lines
12 KiB
Go

package jira
import (
"context"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestNewClientDefaultsToV3(t *testing.T) {
c := NewClient("https://example.atlassian.net", "user@example.com", "token123")
if c.APIVersion != "3" {
t.Errorf("NewClient APIVersion = %q, want %q", c.APIVersion, "3")
}
}
func TestAPIBase(t *testing.T) {
tests := []struct {
version string
want string
}{
{"3", "https://jira.example.com/rest/api/3"},
{"2", "https://jira.example.com/rest/api/2"},
{"", "https://jira.example.com/rest/api/3"}, // empty defaults to 3
}
for _, tt := range tests {
c := &Client{URL: "https://jira.example.com", APIVersion: tt.version}
if got := c.apiBase(); got != tt.want {
t.Errorf("apiBase() with version %q = %q, want %q", tt.version, got, tt.want)
}
}
}
// newTestClient creates a Client pointed at a test server with a fixed API token.
func newTestClient(serverURL, version string) *Client {
c := NewClient(serverURL, "user", "token")
c.APIVersion = version
return c
}
func TestSearchIssuesURL(t *testing.T) {
tests := []struct {
version string
wantPath string
wantPathPart string // substring check on full path
}{
{"3", "/rest/api/3/search/jql", "search/jql"},
{"2", "/rest/api/2/search", ""},
}
for _, tt := range tests {
t.Run("v"+tt.version, func(t *testing.T) {
var gotPath string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotPath = r.URL.Path
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(SearchResult{Total: 0, Issues: []Issue{}})
}))
defer srv.Close()
c := newTestClient(srv.URL, tt.version)
_, err := c.SearchIssues(context.Background(), "project = TEST")
if err != nil {
t.Fatalf("SearchIssues error: %v", err)
}
if gotPath != tt.wantPath {
t.Errorf("SearchIssues path = %q, want %q", gotPath, tt.wantPath)
}
})
}
}
func TestFetchIssueTimestampURL(t *testing.T) {
tests := []struct {
version string
wantPath string
}{
{"3", "/rest/api/3/issue/PROJ-1"},
{"2", "/rest/api/2/issue/PROJ-1"},
}
for _, tt := range tests {
t.Run("v"+tt.version, func(t *testing.T) {
var gotPath string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotPath = r.URL.Path
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(map[string]interface{}{
"fields": map[string]string{"updated": "2024-01-15T10:30:00.000+0000"},
})
}))
defer srv.Close()
c := newTestClient(srv.URL, tt.version)
_, err := c.FetchIssueTimestamp(context.Background(), "PROJ-1")
if err != nil {
t.Fatalf("FetchIssueTimestamp error: %v", err)
}
if gotPath != tt.wantPath {
t.Errorf("FetchIssueTimestamp path = %q, want %q", gotPath, tt.wantPath)
}
})
}
}
func TestGetIssueURL(t *testing.T) {
tests := []struct {
version string
wantPath string
}{
{"3", "/rest/api/3/issue/PROJ-42"},
{"2", "/rest/api/2/issue/PROJ-42"},
}
for _, tt := range tests {
t.Run("v"+tt.version, func(t *testing.T) {
var gotPath string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotPath = r.URL.Path
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(Issue{ID: "1", Key: "PROJ-42"})
}))
defer srv.Close()
c := newTestClient(srv.URL, tt.version)
_, err := c.GetIssue(context.Background(), "PROJ-42")
if err != nil {
t.Fatalf("GetIssue error: %v", err)
}
if gotPath != tt.wantPath {
t.Errorf("GetIssue path = %q, want %q", gotPath, tt.wantPath)
}
})
}
}
func TestCreateIssueURL(t *testing.T) {
tests := []struct {
version string
wantCreatePath string
}{
{"3", "/rest/api/3/issue"},
{"2", "/rest/api/2/issue"},
}
for _, tt := range tests {
t.Run("v"+tt.version, func(t *testing.T) {
var paths []string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
paths = append(paths, r.URL.Path)
w.Header().Set("Content-Type", "application/json")
if r.Method == http.MethodPost {
// Create response
_ = json.NewEncoder(w).Encode(map[string]string{"id": "1", "key": "PROJ-1"})
} else {
// GetIssue follow-up
_ = json.NewEncoder(w).Encode(Issue{ID: "1", Key: "PROJ-1"})
}
}))
defer srv.Close()
c := newTestClient(srv.URL, tt.version)
_, err := c.CreateIssue(context.Background(), map[string]interface{}{"summary": "test"})
if err != nil {
t.Fatalf("CreateIssue error: %v", err)
}
if len(paths) == 0 || paths[0] != tt.wantCreatePath {
t.Errorf("CreateIssue POST path = %q, want %q", paths[0], tt.wantCreatePath)
}
})
}
}
func TestUpdateIssueURL(t *testing.T) {
tests := []struct {
version string
wantPath string
}{
{"3", "/rest/api/3/issue/PROJ-7"},
{"2", "/rest/api/2/issue/PROJ-7"},
}
for _, tt := range tests {
t.Run("v"+tt.version, func(t *testing.T) {
var gotPath string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotPath = r.URL.Path
w.WriteHeader(http.StatusNoContent)
}))
defer srv.Close()
c := newTestClient(srv.URL, tt.version)
err := c.UpdateIssue(context.Background(), "PROJ-7", map[string]interface{}{"summary": "updated"})
if err != nil {
t.Fatalf("UpdateIssue error: %v", err)
}
if gotPath != tt.wantPath {
t.Errorf("UpdateIssue path = %q, want %q", gotPath, tt.wantPath)
}
})
}
}
func TestGetIssueTransitionsURL(t *testing.T) {
for _, version := range []string{"2", "3"} {
t.Run("v"+version, func(t *testing.T) {
wantPath := "/rest/api/" + version + "/issue/PROJ-5/transitions"
var gotPath string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotPath = r.URL.Path
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(TransitionsResult{})
}))
defer srv.Close()
c := newTestClient(srv.URL, version)
_, err := c.GetIssueTransitions(context.Background(), "PROJ-5")
if err != nil {
t.Fatalf("GetIssueTransitions error: %v", err)
}
if gotPath != wantPath {
t.Errorf("GetIssueTransitions path = %q, want %q", gotPath, wantPath)
}
})
}
}
func TestGetIssueTransitionsParsesResponse(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(TransitionsResult{
Transitions: []Transition{
{ID: "11", Name: "Start Progress", To: StatusField{ID: "3", Name: "In Progress"}},
{ID: "21", Name: "Done", To: StatusField{ID: "10002", Name: "Done"}},
},
})
}))
defer srv.Close()
c := newTestClient(srv.URL, "3")
transitions, err := c.GetIssueTransitions(context.Background(), "PROJ-1")
if err != nil {
t.Fatalf("GetIssueTransitions error: %v", err)
}
if len(transitions) != 2 {
t.Fatalf("transitions count = %d, want 2", len(transitions))
}
if transitions[0].ID != "11" {
t.Errorf("transitions[0].ID = %q, want %q", transitions[0].ID, "11")
}
if transitions[0].To.Name != "In Progress" {
t.Errorf("transitions[0].To.Name = %q, want %q", transitions[0].To.Name, "In Progress")
}
}
func TestTransitionIssueURL(t *testing.T) {
for _, version := range []string{"2", "3"} {
t.Run("v"+version, func(t *testing.T) {
wantPath := "/rest/api/" + version + "/issue/PROJ-5/transitions"
var gotPath, gotMethod string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotPath = r.URL.Path
gotMethod = r.Method
w.WriteHeader(http.StatusNoContent)
}))
defer srv.Close()
c := newTestClient(srv.URL, version)
err := c.TransitionIssue(context.Background(), "PROJ-5", "21")
if err != nil {
t.Fatalf("TransitionIssue error: %v", err)
}
if gotPath != wantPath {
t.Errorf("TransitionIssue path = %q, want %q", gotPath, wantPath)
}
if gotMethod != http.MethodPost {
t.Errorf("TransitionIssue method = %q, want POST", gotMethod)
}
})
}
}
func TestTransitionIssuePayload(t *testing.T) {
var gotBody []byte
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotBody, _ = io.ReadAll(r.Body)
w.WriteHeader(http.StatusNoContent)
}))
defer srv.Close()
c := newTestClient(srv.URL, "3")
err := c.TransitionIssue(context.Background(), "PROJ-1", "42")
if err != nil {
t.Fatalf("TransitionIssue error: %v", err)
}
var payload struct {
Transition map[string]string `json:"transition"`
}
if err := json.Unmarshal(gotBody, &payload); err != nil {
t.Fatalf("parse payload: %v", err)
}
if payload.Transition["id"] != "42" {
t.Errorf("transition.id = %q, want %q", payload.Transition["id"], "42")
}
}
func TestSearchIssuesQueryParam(t *testing.T) {
// Verify jql is passed as a query parameter for both versions.
for _, version := range []string{"2", "3"} {
t.Run("v"+version, func(t *testing.T) {
var gotJQL string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotJQL = r.URL.Query().Get("jql")
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(SearchResult{Total: 0, Issues: []Issue{}})
}))
defer srv.Close()
c := newTestClient(srv.URL, version)
_, err := c.SearchIssues(context.Background(), "project = TEST AND status = Open")
if err != nil {
t.Fatalf("SearchIssues error: %v", err)
}
if !strings.Contains(gotJQL, "project = TEST") {
t.Errorf("jql query param = %q, want it to contain %q", gotJQL, "project = TEST")
}
})
}
}
func TestSearchIssuesV3UsesNextPageTokenPagination(t *testing.T) {
var gotTokens []string
var gotStartAts []string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotTokens = append(gotTokens, r.URL.Query().Get("nextPageToken"))
gotStartAts = append(gotStartAts, r.URL.Query().Get("startAt"))
w.Header().Set("Content-Type", "application/json")
switch len(gotTokens) {
case 1:
_ = json.NewEncoder(w).Encode(SearchResult{
NextPageToken: "page-2",
IsLast: false,
Issues: []Issue{{Key: "PROJ-1"}, {Key: "PROJ-2"}},
})
case 2:
_ = json.NewEncoder(w).Encode(SearchResult{
IsLast: true,
Issues: []Issue{{Key: "PROJ-3"}},
})
default:
t.Fatalf("unexpected extra pagination request %d", len(gotTokens))
}
}))
defer srv.Close()
c := newTestClient(srv.URL, "3")
issues, err := c.SearchIssues(context.Background(), "project = PROJ")
if err != nil {
t.Fatalf("SearchIssues error: %v", err)
}
if len(issues) != 3 {
t.Fatalf("issues count = %d, want 3", len(issues))
}
if want := []string{"", "page-2"}; len(gotTokens) != len(want) || gotTokens[0] != want[0] || gotTokens[1] != want[1] {
t.Fatalf("nextPageToken values = %#v, want %#v", gotTokens, want)
}
for i, startAt := range gotStartAts {
if startAt != "" {
t.Fatalf("request %d unexpectedly sent startAt=%q for v3 token pagination", i+1, startAt)
}
}
}
func TestSearchIssuesV2UsesStartAtPagination(t *testing.T) {
var gotStartAts []string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotStartAts = append(gotStartAts, r.URL.Query().Get("startAt"))
w.Header().Set("Content-Type", "application/json")
switch len(gotStartAts) {
case 1:
_ = json.NewEncoder(w).Encode(SearchResult{
Total: 3,
Issues: []Issue{{Key: "PROJ-1"}, {Key: "PROJ-2"}},
})
case 2:
_ = json.NewEncoder(w).Encode(SearchResult{
Total: 3,
Issues: []Issue{{Key: "PROJ-3"}},
})
default:
t.Fatalf("unexpected extra pagination request %d", len(gotStartAts))
}
}))
defer srv.Close()
c := newTestClient(srv.URL, "2")
issues, err := c.SearchIssues(context.Background(), "project = PROJ")
if err != nil {
t.Fatalf("SearchIssues error: %v", err)
}
if len(issues) != 3 {
t.Fatalf("issues count = %d, want 3", len(issues))
}
if want := []string{"0", "2"}; len(gotStartAts) != len(want) || gotStartAts[0] != want[0] || gotStartAts[1] != want[1] {
t.Fatalf("startAt values = %#v, want %#v", gotStartAts, want)
}
}