flake-update-20260201
  1package sources
  2
  3import (
  4	"os/exec"
  5	"testing"
  6	"time"
  7
  8	"github.com/vdemeester/home/tools/review-tool/internal/activity"
  9	"github.com/vdemeester/home/tools/review-tool/internal/config"
 10)
 11
 12func TestJiraSource_ParsePlainOutput(t *testing.T) {
 13	// Test parsing jira CLI plain output
 14	plainOutput := `KEY		SUMMARY											STATUS	UPDATED
 15SRVKP-9988	Fix performance issue								On QA	2026-01-26 15:55:24
 16SRVKP-1234	Implement new feature								Done	2026-01-25 10:00:00`
 17
 18	items, err := parseJiraPlainOutput(plainOutput, "https://issues.redhat.com")
 19	if err != nil {
 20		t.Fatalf("parseJiraPlainOutput() error = %v", err)
 21	}
 22
 23	if len(items) != 2 {
 24		t.Fatalf("expected 2 items, got %d", len(items))
 25	}
 26
 27	// Check first item
 28	item := items[0]
 29	if item.Title != "Fix performance issue" {
 30		t.Errorf("Title = %q, want %q", item.Title, "Fix performance issue")
 31	}
 32	if item.Type != "issue_updated" {
 33		t.Errorf("Type = %q, want %q", item.Type, "issue_updated")
 34	}
 35	if item.Category != activity.CategoryJira {
 36		t.Errorf("Category = %q, want %q", item.Category, activity.CategoryJira)
 37	}
 38	if item.Metadata["key"] != "SRVKP-9988" {
 39		t.Errorf("key = %q, want %q", item.Metadata["key"], "SRVKP-9988")
 40	}
 41	if item.Metadata["status"] != "On QA" {
 42		t.Errorf("status = %q, want %q", item.Metadata["status"], "On QA")
 43	}
 44	if item.URL != "https://issues.redhat.com/browse/SRVKP-9988" {
 45		t.Errorf("URL = %q, want correct URL", item.URL)
 46	}
 47}
 48
 49func TestJiraSource_ParseTimestamp(t *testing.T) {
 50	ts, err := parseJiraTimestamp("2026-01-26 15:55:24")
 51	if err != nil {
 52		t.Fatalf("parseJiraTimestamp() error = %v", err)
 53	}
 54
 55	if ts.Day() != 26 || ts.Month() != 1 || ts.Year() != 2026 {
 56		t.Errorf("parsed date wrong: %v", ts)
 57	}
 58	if ts.Hour() != 15 || ts.Minute() != 55 {
 59		t.Errorf("parsed time wrong: %v", ts)
 60	}
 61}
 62
 63func TestJiraSource_FilterByDateRange(t *testing.T) {
 64	items := []activity.ActivityItem{
 65		{Title: "Old issue", Timestamp: time.Date(2026, 1, 10, 0, 0, 0, 0, time.UTC)},
 66		{Title: "Recent issue", Timestamp: time.Date(2026, 1, 25, 0, 0, 0, 0, time.UTC)},
 67	}
 68
 69	start := time.Date(2026, 1, 20, 0, 0, 0, 0, time.UTC)
 70	end := time.Date(2026, 1, 31, 23, 59, 59, 0, time.UTC)
 71
 72	filtered := filterByDateRange(items, start, end)
 73
 74	if len(filtered) != 1 {
 75		t.Errorf("expected 1 item in range, got %d", len(filtered))
 76	}
 77}
 78
 79// Integration test - requires jira CLI
 80func TestJiraSource_RealData(t *testing.T) {
 81	// Check if jira CLI is available
 82	if _, err := exec.LookPath("jira"); err != nil {
 83		t.Skip("jira CLI not found, skipping integration test")
 84	}
 85
 86	cfg := &config.JiraConfig{
 87		Enabled:  true,
 88		Server:   "https://issues.redhat.com",
 89		Projects: []string{"SRVKP"},
 90	}
 91
 92	source := NewJiraSource(cfg)
 93
 94	// Last 7 days
 95	now := time.Now()
 96	start := time.Date(now.Year(), now.Month(), now.Day()-7, 0, 0, 0, 0, now.Location())
 97
 98	act, err := source.Fetch(t.Context(), start, now)
 99	if err != nil {
100		t.Fatalf("Fetch error: %v", err)
101	}
102
103	t.Logf("Found %d Jira items", len(act.Items))
104	for i, item := range act.Items {
105		if i < 5 {
106			t.Logf("  [%s] %s (%s)", item.Metadata["key"], item.Title, item.Metadata["status"])
107		}
108	}
109}