|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { |
| 3 | + computeOpportunityFreshness, |
| 4 | + type FreshnessIssue, |
| 5 | +} from "../../packages/gittensory-engine/src/opportunity-freshness"; |
| 6 | +import { |
| 7 | + buildContributorOutcomeHistory, |
| 8 | + buildContributorProfile, |
| 9 | +} from "../../src/signals/engine"; |
| 10 | +import { buildRepoRewardRisk, rewardRiskFreshnessInternals } from "../../src/signals/reward-risk"; |
| 11 | +import type { IssueRecord, RepositoryRecord, ScoringModelSnapshotRecord } from "../../src/types"; |
| 12 | + |
| 13 | +function scoringSnapshot(): ScoringModelSnapshotRecord { |
| 14 | + return { |
| 15 | + id: "freshness-scoring", |
| 16 | + sourceKind: "test", |
| 17 | + sourceUrl: "fixture://freshness", |
| 18 | + fetchedAt: "2026-05-25T00:00:00.000Z", |
| 19 | + activeModel: "current_density_model", |
| 20 | + constants: {}, |
| 21 | + programmingLanguages: {}, |
| 22 | + warnings: [], |
| 23 | + payload: {}, |
| 24 | + }; |
| 25 | +} |
| 26 | + |
| 27 | +function repo(fullName: string): RepositoryRecord { |
| 28 | + const [owner, name] = fullName.split("/") as [string, string]; |
| 29 | + return { |
| 30 | + fullName, |
| 31 | + owner, |
| 32 | + name, |
| 33 | + isInstalled: true, |
| 34 | + isRegistered: true, |
| 35 | + isPrivate: false, |
| 36 | + defaultBranch: "main", |
| 37 | + registryConfig: { |
| 38 | + repo: fullName, |
| 39 | + emissionShare: 0.02, |
| 40 | + issueDiscoveryShare: 0, |
| 41 | + labelMultipliers: {}, |
| 42 | + trustedLabelPipeline: false, |
| 43 | + maintainerCut: 0, |
| 44 | + raw: {}, |
| 45 | + }, |
| 46 | + }; |
| 47 | +} |
| 48 | + |
| 49 | +function issue(fullName: string, number: number, title: string, overrides: Partial<IssueRecord> = {}): IssueRecord { |
| 50 | + return { |
| 51 | + repoFullName: fullName, |
| 52 | + number, |
| 53 | + title, |
| 54 | + state: "open", |
| 55 | + authorLogin: "dev", |
| 56 | + authorAssociation: "NONE", |
| 57 | + labels: [], |
| 58 | + linkedPrs: [], |
| 59 | + body: "Issue body", |
| 60 | + createdAt: new Date().toISOString(), |
| 61 | + updatedAt: new Date().toISOString(), |
| 62 | + ...overrides, |
| 63 | + }; |
| 64 | +} |
| 65 | + |
| 66 | +function toFreshnessIssues(issues: IssueRecord[]): FreshnessIssue[] { |
| 67 | + return issues.map((item) => ({ |
| 68 | + state: item.state, |
| 69 | + updatedAt: item.updatedAt ?? null, |
| 70 | + createdAt: item.createdAt ?? null, |
| 71 | + })); |
| 72 | +} |
| 73 | + |
| 74 | +describe("reward-risk freshness parity with gittensory-engine", () => { |
| 75 | + const collab = repo("owner/collab-repo"); |
| 76 | + const profile = buildContributorProfile("dev", { login: "dev", topLanguages: [], source: "github" }, [], []); |
| 77 | + const history = buildContributorOutcomeHistory({ |
| 78 | + login: "dev", |
| 79 | + profile, |
| 80 | + repositories: [collab], |
| 81 | + pullRequests: [], |
| 82 | + issues: [], |
| 83 | + repoStats: [], |
| 84 | + }); |
| 85 | + const base = { |
| 86 | + login: "dev" as const, |
| 87 | + repo: collab, |
| 88 | + repoFullName: collab.fullName, |
| 89 | + profile, |
| 90 | + outcomeHistory: history, |
| 91 | + scoringSnapshot: scoringSnapshot(), |
| 92 | + }; |
| 93 | + |
| 94 | + it("matches computeOpportunityFreshness for fresh, stale, and undated open issues", () => { |
| 95 | + const nowMs = Date.now(); |
| 96 | + const freshIssues = [ |
| 97 | + issue(collab.fullName, 1, "Fresh", { updatedAt: new Date(nowMs - 2 * 86_400_000).toISOString() }), |
| 98 | + ]; |
| 99 | + const staleIssues = [issue(collab.fullName, 2, "Stale", { updatedAt: "2020-01-01T00:00:00.000Z" })]; |
| 100 | + const undatedIssues = [issue(collab.fullName, 3, "Undated", { updatedAt: null, createdAt: null })]; |
| 101 | + |
| 102 | + const fresh = buildRepoRewardRisk({ ...base, issues: freshIssues, pullRequests: [] }); |
| 103 | + const stale = buildRepoRewardRisk({ ...base, issues: staleIssues, pullRequests: [] }); |
| 104 | + const undated = buildRepoRewardRisk({ ...base, issues: undatedIssues, pullRequests: [] }); |
| 105 | + |
| 106 | + expect(fresh.rewardUpside.opportunityFactors.freshnessFactor).toBe( |
| 107 | + computeOpportunityFreshness(toFreshnessIssues(freshIssues), nowMs), |
| 108 | + ); |
| 109 | + expect(stale.rewardUpside.opportunityFactors.freshnessFactor).toBe( |
| 110 | + computeOpportunityFreshness(toFreshnessIssues(staleIssues), nowMs), |
| 111 | + ); |
| 112 | + expect(undated.rewardUpside.opportunityFactors.freshnessFactor).toBe( |
| 113 | + computeOpportunityFreshness(toFreshnessIssues(undatedIssues), nowMs), |
| 114 | + ); |
| 115 | + expect(undated.rewardUpside.opportunityFactors.freshnessFactor).toBeLessThanOrEqual(0.05); |
| 116 | + }); |
| 117 | + |
| 118 | + it("uses createdAt when updatedAt is malformed", () => { |
| 119 | + const issuesForRisk = [ |
| 120 | + issue(collab.fullName, 1, "Fallback", { |
| 121 | + updatedAt: "not-a-date", |
| 122 | + createdAt: new Date(Date.now() - 2 * 86_400_000).toISOString(), |
| 123 | + }), |
| 124 | + ]; |
| 125 | + const result = buildRepoRewardRisk({ ...base, issues: issuesForRisk, pullRequests: [] }); |
| 126 | + expect(result.rewardUpside.opportunityFactors.freshnessFactor).toBeGreaterThan(0.7); |
| 127 | + }); |
| 128 | + |
| 129 | + it("scores from the freshest open issue when multiple are present", () => { |
| 130 | + const issuesForRisk = [ |
| 131 | + issue(collab.fullName, 1, "Stale", { updatedAt: "2020-01-01T00:00:00.000Z" }), |
| 132 | + issue(collab.fullName, 2, "Fresh", { updatedAt: new Date(Date.now() - 2 * 86_400_000).toISOString() }), |
| 133 | + ]; |
| 134 | + const result = buildRepoRewardRisk({ ...base, issues: issuesForRisk, pullRequests: [] }); |
| 135 | + expect(result.rewardUpside.opportunityFactors.freshnessFactor).toBeGreaterThan(0.7); |
| 136 | + }); |
| 137 | + |
| 138 | + it("pickIssueTimestamp and issueAgeDays cover defensive timestamp branches", () => { |
| 139 | + const { pickIssueTimestamp, issueAgeDays } = rewardRiskFreshnessInternals; |
| 140 | + expect( |
| 141 | + pickIssueTimestamp({ |
| 142 | + repoFullName: collab.fullName, |
| 143 | + number: 1, |
| 144 | + title: "t", |
| 145 | + state: "open", |
| 146 | + labels: [], |
| 147 | + linkedPrs: [], |
| 148 | + updatedAt: "2026-07-03T00:00:00.000Z", |
| 149 | + createdAt: "2020-01-01T00:00:00.000Z", |
| 150 | + }), |
| 151 | + ).toBe("2026-07-03T00:00:00.000Z"); |
| 152 | + expect( |
| 153 | + pickIssueTimestamp({ |
| 154 | + repoFullName: collab.fullName, |
| 155 | + number: 2, |
| 156 | + title: "t", |
| 157 | + state: "open", |
| 158 | + labels: [], |
| 159 | + linkedPrs: [], |
| 160 | + updatedAt: " ", |
| 161 | + createdAt: "2026-07-03T00:00:00.000Z", |
| 162 | + }), |
| 163 | + ).toBe("2026-07-03T00:00:00.000Z"); |
| 164 | + expect( |
| 165 | + pickIssueTimestamp({ |
| 166 | + repoFullName: collab.fullName, |
| 167 | + number: 3, |
| 168 | + title: "t", |
| 169 | + state: "open", |
| 170 | + labels: [], |
| 171 | + linkedPrs: [], |
| 172 | + updatedAt: null, |
| 173 | + createdAt: null, |
| 174 | + }), |
| 175 | + ).toBeNull(); |
| 176 | + expect(issueAgeDays(null)).toBe(Number.POSITIVE_INFINITY); |
| 177 | + expect(issueAgeDays("not-a-date")).toBe(Number.POSITIVE_INFINITY); |
| 178 | + expect(issueAgeDays("2026-07-03T00:00:00.000Z")).toBeGreaterThanOrEqual(0); |
| 179 | + }); |
| 180 | +}); |
0 commit comments