|
3 | 3 | */ |
4 | 4 |
|
5 | 5 | import { describe, it, expect } from 'vitest'; |
6 | | -import { computePRsByRepo, computeTopRepos, getMonthlyData } from './dashboard-data.js'; |
7 | | -import type { DailyDigest, AgentState } from '../core/types.js'; |
| 6 | +import { buildDashboardStats, computePRsByRepo, computeTopRepos, getMonthlyData } from './dashboard-data.js'; |
| 7 | +import type { DailyDigest, AgentState, ShelvedPRRef } from '../core/types.js'; |
8 | 8 |
|
9 | 9 | function makeDigest(overrides: Partial<DailyDigest> = {}): DailyDigest { |
10 | 10 | return { |
@@ -50,6 +50,108 @@ function makeState(overrides: Partial<AgentState> = {}): AgentState { |
50 | 50 | } as AgentState; |
51 | 51 | } |
52 | 52 |
|
| 53 | +// --------------------------------------------------------------------------- |
| 54 | +// buildDashboardStats |
| 55 | +// --------------------------------------------------------------------------- |
| 56 | + |
| 57 | +describe('buildDashboardStats', () => { |
| 58 | + it('returns zeros when digest has no summary', () => { |
| 59 | + const digest = makeDigest(); |
| 60 | + // Remove summary to trigger the default fallback |
| 61 | + (digest as any).summary = undefined; |
| 62 | + const stats = buildDashboardStats(digest, makeState()); |
| 63 | + expect(stats).toEqual({ |
| 64 | + activePRs: 0, |
| 65 | + shelvedPRs: 0, |
| 66 | + mergedPRs: 0, |
| 67 | + closedPRs: 0, |
| 68 | + mergeRate: '0.0%', |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + it('pulls activePRs from summary.totalActivePRs', () => { |
| 73 | + const digest = makeDigest({ |
| 74 | + summary: { totalActivePRs: 5, totalNeedingAttention: 2, totalMergedAllTime: 10, mergeRate: 80 }, |
| 75 | + }); |
| 76 | + const stats = buildDashboardStats(digest, makeState()); |
| 77 | + expect(stats.activePRs).toBe(5); |
| 78 | + }); |
| 79 | + |
| 80 | + it('counts shelvedPRs from digest.shelvedPRs array length', () => { |
| 81 | + const shelvedPRs: ShelvedPRRef[] = [ |
| 82 | + { number: 1, url: 'u1', title: 't1', repo: 'r/1', daysSinceActivity: 5, status: 'healthy' }, |
| 83 | + { number: 2, url: 'u2', title: 't2', repo: 'r/2', daysSinceActivity: 10, status: 'dormant' }, |
| 84 | + ]; |
| 85 | + const digest = makeDigest({ shelvedPRs }); |
| 86 | + const stats = buildDashboardStats(digest, makeState()); |
| 87 | + expect(stats.shelvedPRs).toBe(2); |
| 88 | + }); |
| 89 | + |
| 90 | + it('pulls mergedPRs from summary.totalMergedAllTime', () => { |
| 91 | + const digest = makeDigest({ |
| 92 | + summary: { totalActivePRs: 0, totalNeedingAttention: 0, totalMergedAllTime: 42, mergeRate: 85 }, |
| 93 | + }); |
| 94 | + const stats = buildDashboardStats(digest, makeState()); |
| 95 | + expect(stats.mergedPRs).toBe(42); |
| 96 | + }); |
| 97 | + |
| 98 | + it('sums closedWithoutMergeCount across all repoScores', () => { |
| 99 | + const state = makeState({ |
| 100 | + repoScores: { |
| 101 | + 'a/b': { |
| 102 | + repo: 'a/b', |
| 103 | + score: 5, |
| 104 | + mergedPRCount: 1, |
| 105 | + closedWithoutMergeCount: 3, |
| 106 | + avgResponseDays: null, |
| 107 | + lastEvaluatedAt: '2025-06-01T00:00:00Z', |
| 108 | + signals: { hasActiveMaintainers: true, isResponsive: true, hasHostileComments: false }, |
| 109 | + }, |
| 110 | + 'c/d': { |
| 111 | + repo: 'c/d', |
| 112 | + score: 7, |
| 113 | + mergedPRCount: 2, |
| 114 | + closedWithoutMergeCount: 1, |
| 115 | + avgResponseDays: null, |
| 116 | + lastEvaluatedAt: '2025-06-01T00:00:00Z', |
| 117 | + signals: { hasActiveMaintainers: true, isResponsive: true, hasHostileComments: false }, |
| 118 | + }, |
| 119 | + }, |
| 120 | + }); |
| 121 | + const stats = buildDashboardStats(makeDigest(), state); |
| 122 | + expect(stats.closedPRs).toBe(4); // 3 + 1 |
| 123 | + }); |
| 124 | + |
| 125 | + it('formats mergeRate as a percentage string', () => { |
| 126 | + const digest = makeDigest({ |
| 127 | + summary: { totalActivePRs: 0, totalNeedingAttention: 0, totalMergedAllTime: 0, mergeRate: 72.3456 }, |
| 128 | + }); |
| 129 | + const stats = buildDashboardStats(digest, makeState()); |
| 130 | + expect(stats.mergeRate).toBe('72.3%'); |
| 131 | + }); |
| 132 | + |
| 133 | + it('handles null/undefined mergeRate gracefully', () => { |
| 134 | + const digest = makeDigest(); |
| 135 | + (digest.summary as any).mergeRate = null; |
| 136 | + const stats = buildDashboardStats(digest, makeState()); |
| 137 | + expect(stats.mergeRate).toBe('0.0%'); |
| 138 | + }); |
| 139 | + |
| 140 | + it('handles missing repoScores gracefully', () => { |
| 141 | + const state = makeState(); |
| 142 | + (state as any).repoScores = undefined; |
| 143 | + const stats = buildDashboardStats(makeDigest(), state); |
| 144 | + expect(stats.closedPRs).toBe(0); |
| 145 | + }); |
| 146 | + |
| 147 | + it('handles missing shelvedPRs array', () => { |
| 148 | + const digest = makeDigest(); |
| 149 | + (digest as any).shelvedPRs = undefined; |
| 150 | + const stats = buildDashboardStats(digest, makeState()); |
| 151 | + expect(stats.shelvedPRs).toBe(0); |
| 152 | + }); |
| 153 | +}); |
| 154 | + |
53 | 155 | describe('computePRsByRepo', () => { |
54 | 156 | it('should group active PRs by repo', () => { |
55 | 157 | const digest = makeDigest({ |
|
0 commit comments