beads/internal/linear/tracker_test.go
Harry 3ae2ef3a5a feat: multi-project/team support for Linear, Jira, and ADO
Add support for syncing issues across multiple projects/teams in all
three tracker integrations, with shared infrastructure to avoid
duplicating logic.

Shared infrastructure (internal/tracker/multiproject.go):
- ParseCommaSeparated: splits comma-separated config values
- DeduplicateStrings: removes duplicates preserving order
- ResolveProjectIDs: config precedence (CLI > plural > singular)

Linear (closes #2791):
- Tracker: map[string]*Client with per-team routing
- FetchIssues iterates all teams, deduplicates by issue ID
- UpdateIssue routes to owning team via FetchIssueByIdentifier
- Config: linear.team_ids / LINEAR_TEAM_IDS + --team flag

Jira:
- projectKey string → projectKeys []string
- FetchIssues uses JQL IN clause (single query, no iteration)
- Config: jira.projects / JIRA_PROJECTS + --project flag

ADO:
- project string → projects []string
- WIQL uses TeamProject IN clause (single query)
- Config: ado.projects / AZURE_DEVOPS_PROJECTS + --project flag

All singular config keys remain supported for backward compatibility.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-03-28 13:40:39 -07:00

197 lines
5.2 KiB
Go

package linear
import (
"testing"
"github.com/steveyegge/beads/internal/tracker"
"github.com/steveyegge/beads/internal/types"
)
func TestRegistered(t *testing.T) {
factory := tracker.Get("linear")
if factory == nil {
t.Fatal("linear tracker not registered")
}
tr := factory()
if tr.Name() != "linear" {
t.Errorf("Name() = %q, want %q", tr.Name(), "linear")
}
if tr.DisplayName() != "Linear" {
t.Errorf("DisplayName() = %q, want %q", tr.DisplayName(), "Linear")
}
if tr.ConfigPrefix() != "linear" {
t.Errorf("ConfigPrefix() = %q, want %q", tr.ConfigPrefix(), "linear")
}
}
func TestIsExternalRef(t *testing.T) {
tr := &Tracker{}
tests := []struct {
ref string
want bool
}{
{"https://linear.app/team/issue/PROJ-123", true},
{"https://linear.app/team/issue/PROJ-123/some-title", true},
{"https://github.com/org/repo/issues/1", false},
{"", false},
}
for _, tt := range tests {
if got := tr.IsExternalRef(tt.ref); got != tt.want {
t.Errorf("IsExternalRef(%q) = %v, want %v", tt.ref, got, tt.want)
}
}
}
func TestExtractIdentifier(t *testing.T) {
tr := &Tracker{}
tests := []struct {
ref string
want string
}{
{"https://linear.app/team/issue/PROJ-123/some-title", "PROJ-123"},
{"https://linear.app/team/issue/PROJ-123", "PROJ-123"},
}
for _, tt := range tests {
if got := tr.ExtractIdentifier(tt.ref); got != tt.want {
t.Errorf("ExtractIdentifier(%q) = %q, want %q", tt.ref, got, tt.want)
}
}
}
func TestBuildExternalRef(t *testing.T) {
tr := &Tracker{}
ti := &tracker.TrackerIssue{
URL: "https://linear.app/team/issue/PROJ-123/some-title-slug",
Identifier: "PROJ-123",
}
ref := tr.BuildExternalRef(ti)
want := "https://linear.app/team/issue/PROJ-123"
if ref != want {
t.Errorf("BuildExternalRef() = %q, want %q", ref, want)
}
}
func TestFieldMapperPriority(t *testing.T) {
m := &linearFieldMapper{config: DefaultMappingConfig()}
// Linear 1 (urgent) -> Beads 0 (critical)
if got := m.PriorityToBeads(1); got != 0 {
t.Errorf("PriorityToBeads(1) = %d, want 0", got)
}
// Beads 0 (critical) -> Linear 1 (urgent)
if got := m.PriorityToTracker(0); got != 1 {
t.Errorf("PriorityToTracker(0) = %v, want 1", got)
}
}
func TestFieldMapperStatus(t *testing.T) {
m := &linearFieldMapper{config: DefaultMappingConfig()}
// Started -> in_progress
state := &State{Type: "started", Name: "In Progress"}
if got := m.StatusToBeads(state); got != types.StatusInProgress {
t.Errorf("StatusToBeads(started) = %q, want %q", got, types.StatusInProgress)
}
// Completed -> closed
state = &State{Type: "completed", Name: "Done"}
if got := m.StatusToBeads(state); got != types.StatusClosed {
t.Errorf("StatusToBeads(completed) = %q, want %q", got, types.StatusClosed)
}
}
func TestTrackerMultiTeamValidate(t *testing.T) {
// Empty tracker should fail validation.
tr := &Tracker{}
if err := tr.Validate(); err == nil {
t.Error("expected Validate() to fail on uninitialized tracker")
}
// Tracker with clients should pass.
tr.clients = map[string]*Client{
"team-1": NewClient("key", "team-1"),
}
if err := tr.Validate(); err != nil {
t.Errorf("Validate() = %v, want nil", err)
}
}
func TestTrackerSetTeamIDs(t *testing.T) {
tr := &Tracker{}
ids := []string{"id-1", "id-2", "id-3"}
tr.SetTeamIDs(ids)
if len(tr.teamIDs) != 3 {
t.Fatalf("expected 3 team IDs, got %d", len(tr.teamIDs))
}
for i, want := range ids {
if tr.teamIDs[i] != want {
t.Errorf("teamIDs[%d] = %q, want %q", i, tr.teamIDs[i], want)
}
}
}
func TestTrackerTeamIDsAccessor(t *testing.T) {
tr := &Tracker{teamIDs: []string{"a", "b"}}
got := tr.TeamIDs()
if len(got) != 2 || got[0] != "a" || got[1] != "b" {
t.Errorf("TeamIDs() = %v, want [a b]", got)
}
}
func TestTrackerPrimaryClient(t *testing.T) {
tr := &Tracker{
teamIDs: []string{"team-1", "team-2"},
clients: map[string]*Client{
"team-1": NewClient("key", "team-1"),
"team-2": NewClient("key", "team-2"),
},
}
client := tr.PrimaryClient()
if client == nil {
t.Fatal("PrimaryClient() returned nil")
}
if client.TeamID != "team-1" {
t.Errorf("PrimaryClient().TeamID = %q, want %q", client.TeamID, "team-1")
}
// Empty tracker should return nil.
empty := &Tracker{}
if empty.PrimaryClient() != nil {
t.Error("PrimaryClient() should return nil for empty tracker")
}
}
func TestLinearToTrackerIssue(t *testing.T) {
li := &Issue{
ID: "uuid-123",
Identifier: "TEAM-42",
Title: "Fix the bug",
Description: "It's broken",
URL: "https://linear.app/team/issue/TEAM-42/fix-the-bug",
Priority: 2,
CreatedAt: "2026-01-15T10:00:00Z",
UpdatedAt: "2026-01-16T14:30:00Z",
Assignee: &User{ID: "user-1", Name: "Alice", Email: "alice@example.com"},
State: &State{Type: "started", Name: "In Progress"},
}
ti := linearToTrackerIssue(li)
if ti.ID != "uuid-123" {
t.Errorf("ID = %q, want %q", ti.ID, "uuid-123")
}
if ti.Identifier != "TEAM-42" {
t.Errorf("Identifier = %q, want %q", ti.Identifier, "TEAM-42")
}
if ti.Assignee != "Alice" {
t.Errorf("Assignee = %q, want %q", ti.Assignee, "Alice")
}
if ti.AssigneeEmail != "alice@example.com" {
t.Errorf("AssigneeEmail = %q, want %q", ti.AssigneeEmail, "alice@example.com")
}
if ti.Raw != li {
t.Error("Raw should reference original linear.Issue")
}
}