|
| 1 | +package core |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | +) |
| 8 | + |
| 9 | +func TestGenerateReport(t *testing.T) { |
| 10 | + now := time.Now().UTC() |
| 11 | + |
| 12 | + resolver := &SkipperResolver{ |
| 13 | + cache: map[string]*time.Time{ |
| 14 | + "test1": ptrTime(now.AddDate(0, 0, 3)), // disabled, due in 3 days |
| 15 | + "test2": ptrTime(now.AddDate(0, 0, 10)), // disabled, due in 10 days |
| 16 | + "test3": ptrTime(now.AddDate(0, 0, -2)), // reenabled (past date) |
| 17 | + "test4": nil, // no date, enabled |
| 18 | + }, |
| 19 | + } |
| 20 | + |
| 21 | + report := GenerateReport(resolver) |
| 22 | + |
| 23 | + if report == nil { |
| 24 | + t.Fatalf("GenerateReport returned nil") |
| 25 | + } |
| 26 | + |
| 27 | + if report.DisabledCount != 2 { |
| 28 | + t.Errorf("expected DisabledCount=2, got %d", report.DisabledCount) |
| 29 | + } |
| 30 | + |
| 31 | + if report.ReenabledCount != 1 { |
| 32 | + t.Errorf("expected ReenabledCount=1, got %d", report.ReenabledCount) |
| 33 | + } |
| 34 | + |
| 35 | + if len(report.DueThisWeek) != 1 { |
| 36 | + t.Errorf("expected 1 test due this week, got %d", len(report.DueThisWeek)) |
| 37 | + } |
| 38 | + |
| 39 | + if report.DueThisWeek[0] != "test1" { |
| 40 | + t.Errorf("expected 'test1' in DueThisWeek, got %v", report.DueThisWeek) |
| 41 | + } |
| 42 | + |
| 43 | + // Check quarantine days debt: test1 (3 days) + test2 (10 days) = 13 |
| 44 | + expectedDebt := 3 + 10 |
| 45 | + if report.QuarantineDaysDebt != expectedDebt { |
| 46 | + t.Errorf("expected QuarantineDaysDebt=%d, got %d", expectedDebt, report.QuarantineDaysDebt) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +func TestGenerateReportNilResolver(t *testing.T) { |
| 51 | + report := GenerateReport(nil) |
| 52 | + |
| 53 | + if report == nil { |
| 54 | + t.Fatalf("GenerateReport(nil) returned nil, expected empty report") |
| 55 | + } |
| 56 | + |
| 57 | + if report.DisabledCount != 0 { |
| 58 | + t.Errorf("expected DisabledCount=0 for nil resolver, got %d", report.DisabledCount) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func TestWriteReport(t *testing.T) { |
| 63 | + tmpDir := t.TempDir() |
| 64 | + originalCwd, err := os.Getwd() |
| 65 | + if err != nil { |
| 66 | + t.Fatalf("could not get cwd: %v", err) |
| 67 | + } |
| 68 | + defer os.Chdir(originalCwd) |
| 69 | + |
| 70 | + if err := os.Chdir(tmpDir); err != nil { |
| 71 | + t.Fatalf("could not change to temp dir: %v", err) |
| 72 | + } |
| 73 | + |
| 74 | + report := &Report{ |
| 75 | + DisabledCount: 2, |
| 76 | + DueThisWeek: []string{"test1", "test2"}, |
| 77 | + ReenabledCount: 1, |
| 78 | + QuarantineDaysDebt: 10, |
| 79 | + GeneratedAt: time.Now().UTC(), |
| 80 | + } |
| 81 | + |
| 82 | + if err := WriteReport(report); err != nil { |
| 83 | + t.Fatalf("WriteReport failed: %v", err) |
| 84 | + } |
| 85 | + |
| 86 | + // Check skipper-report.json was created |
| 87 | + data, err := os.ReadFile("skipper-report.json") |
| 88 | + if err != nil { |
| 89 | + t.Fatalf("skipper-report.json not found: %v", err) |
| 90 | + } |
| 91 | + |
| 92 | + if len(data) == 0 { |
| 93 | + t.Errorf("skipper-report.json is empty") |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +func TestWriteReportGitHubActionsIntegration(t *testing.T) { |
| 98 | + tmpDir := t.TempDir() |
| 99 | + summaryFile := tmpDir + "/summary.txt" |
| 100 | + |
| 101 | + originalCwd, err := os.Getwd() |
| 102 | + if err != nil { |
| 103 | + t.Fatalf("could not get cwd: %v", err) |
| 104 | + } |
| 105 | + defer os.Chdir(originalCwd) |
| 106 | + |
| 107 | + if err := os.Chdir(tmpDir); err != nil { |
| 108 | + t.Fatalf("could not change to temp dir: %v", err) |
| 109 | + } |
| 110 | + |
| 111 | + os.Setenv("GITHUB_STEP_SUMMARY", summaryFile) |
| 112 | + defer os.Unsetenv("GITHUB_STEP_SUMMARY") |
| 113 | + |
| 114 | + report := &Report{ |
| 115 | + DisabledCount: 2, |
| 116 | + DueThisWeek: []string{"test1"}, |
| 117 | + ReenabledCount: 1, |
| 118 | + QuarantineDaysDebt: 15, |
| 119 | + GeneratedAt: time.Now().UTC(), |
| 120 | + } |
| 121 | + |
| 122 | + if err := WriteReport(report); err != nil { |
| 123 | + t.Fatalf("WriteReport failed: %v", err) |
| 124 | + } |
| 125 | + |
| 126 | + // Check summary file was created |
| 127 | + data, err := os.ReadFile(summaryFile) |
| 128 | + if err != nil { |
| 129 | + t.Fatalf("summary file not found: %v", err) |
| 130 | + } |
| 131 | + |
| 132 | + markdown := string(data) |
| 133 | + if len(markdown) == 0 { |
| 134 | + t.Errorf("summary file is empty") |
| 135 | + } |
| 136 | + |
| 137 | + if !contains(markdown, "Quarantine Report") { |
| 138 | + t.Errorf("summary missing title") |
| 139 | + } |
| 140 | + |
| 141 | + if !contains(markdown, "2") { |
| 142 | + t.Errorf("summary missing disabled count") |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +func ptrTime(t time.Time) *time.Time { |
| 147 | + return &t |
| 148 | +} |
| 149 | + |
| 150 | +func contains(s, substr string) bool { |
| 151 | + for i := 0; i <= len(s)-len(substr); i++ { |
| 152 | + if s[i:i+len(substr)] == substr { |
| 153 | + return true |
| 154 | + } |
| 155 | + } |
| 156 | + return false |
| 157 | +} |
0 commit comments