|
| 1 | +import { test, mock } from "node:test"; |
| 2 | +import assert from "node:assert/strict"; |
| 3 | +import { checkAndAwardBadges } from "./badge-awarder.js"; |
| 4 | + |
| 5 | +const ALL_BADGES = [ |
| 6 | + { id: "b-first", criteria: "first_support", name: "First Supporter" }, |
| 7 | + { id: "b-ten", criteria: "ten_supporters", name: "10 Supporters" }, |
| 8 | + { id: "b-xlm", criteria: "total_100_xlm", name: "100 XLM Club" }, |
| 9 | + { id: "b-milestone", criteria: "milestone_reached", name: "Milestone Maker" }, |
| 10 | +]; |
| 11 | + |
| 12 | +function buildPrismaMock(overrides: { |
| 13 | + badges?: unknown[]; |
| 14 | + existingAwards?: { badgeId: string }[]; |
| 15 | + txCount?: number; |
| 16 | + uniqueSupporters?: { supporterAddress: string }[]; |
| 17 | + totalsByAsset?: { assetCode: string; assetIssuer: string | null; _sum: { amount: unknown } }[]; |
| 18 | + milestonesReached?: number; |
| 19 | + createImpl?: () => Promise<unknown>; |
| 20 | +} = {}) { |
| 21 | + const profileBadgeCreate = mock.fn(overrides.createImpl ?? (() => Promise.resolve({}))); |
| 22 | + |
| 23 | + const tx = { |
| 24 | + $executeRaw: mock.fn(() => Promise.resolve()), |
| 25 | + badge: { |
| 26 | + findMany: mock.fn(() => Promise.resolve(overrides.badges ?? ALL_BADGES)), |
| 27 | + }, |
| 28 | + profileBadge: { |
| 29 | + findMany: mock.fn(() => Promise.resolve(overrides.existingAwards ?? [])), |
| 30 | + create: profileBadgeCreate, |
| 31 | + }, |
| 32 | + supportTransaction: { |
| 33 | + count: mock.fn(() => Promise.resolve(overrides.txCount ?? 0)), |
| 34 | + findMany: mock.fn(() => Promise.resolve(overrides.uniqueSupporters ?? [])), |
| 35 | + groupBy: mock.fn(() => Promise.resolve(overrides.totalsByAsset ?? [])), |
| 36 | + }, |
| 37 | + milestone: { |
| 38 | + count: mock.fn(() => Promise.resolve(overrides.milestonesReached ?? 0)), |
| 39 | + }, |
| 40 | + }; |
| 41 | + |
| 42 | + const $transaction = mock.fn((cb: (tx: unknown) => Promise<void>) => cb(tx)); |
| 43 | + |
| 44 | + return { $transaction, tx, profileBadgeCreate }; |
| 45 | +} |
| 46 | + |
| 47 | +function awardedCriteria(profileBadgeCreate: ReturnType<typeof mock.fn>): string[] { |
| 48 | + return profileBadgeCreate.mock.calls.map((c) => { |
| 49 | + const badgeId = (c.arguments[0] as { data: { badgeId: string } }).data.badgeId; |
| 50 | + return ALL_BADGES.find((b) => b.id === badgeId)?.criteria ?? badgeId; |
| 51 | + }); |
| 52 | +} |
| 53 | + |
| 54 | +test("first_support is awarded when txCount >= 1", async () => { |
| 55 | + const { $transaction, profileBadgeCreate } = buildPrismaMock({ |
| 56 | + badges: [ALL_BADGES[0]], |
| 57 | + txCount: 1, |
| 58 | + }); |
| 59 | + await checkAndAwardBadges("profile-1", { $transaction } as any); |
| 60 | + assert.deepEqual(awardedCriteria(profileBadgeCreate), ["first_support"]); |
| 61 | +}); |
| 62 | + |
| 63 | +test("first_support is NOT awarded when txCount is 0", async () => { |
| 64 | + const { $transaction, profileBadgeCreate } = buildPrismaMock({ |
| 65 | + badges: [ALL_BADGES[0]], |
| 66 | + txCount: 0, |
| 67 | + }); |
| 68 | + await checkAndAwardBadges("profile-1", { $transaction } as any); |
| 69 | + assert.equal(profileBadgeCreate.mock.calls.length, 0); |
| 70 | +}); |
| 71 | + |
| 72 | +test("ten_supporters is awarded when uniqueSupporters.length >= 10", async () => { |
| 73 | + const supporters = Array.from({ length: 10 }, (_, i) => ({ supporterAddress: `G${i}` })); |
| 74 | + const { $transaction, profileBadgeCreate } = buildPrismaMock({ |
| 75 | + badges: [ALL_BADGES[1]], |
| 76 | + uniqueSupporters: supporters, |
| 77 | + }); |
| 78 | + await checkAndAwardBadges("profile-1", { $transaction } as any); |
| 79 | + assert.deepEqual(awardedCriteria(profileBadgeCreate), ["ten_supporters"]); |
| 80 | +}); |
| 81 | + |
| 82 | +test("ten_supporters is NOT awarded when uniqueSupporters.length < 10", async () => { |
| 83 | + const supporters = Array.from({ length: 9 }, (_, i) => ({ supporterAddress: `G${i}` })); |
| 84 | + const { $transaction, profileBadgeCreate } = buildPrismaMock({ |
| 85 | + badges: [ALL_BADGES[1]], |
| 86 | + uniqueSupporters: supporters, |
| 87 | + }); |
| 88 | + await checkAndAwardBadges("profile-1", { $transaction } as any); |
| 89 | + assert.equal(profileBadgeCreate.mock.calls.length, 0); |
| 90 | +}); |
| 91 | + |
| 92 | +test("total_100_xlm is awarded for XLM totals >= 100, ignoring USDC", async () => { |
| 93 | + const { $transaction, profileBadgeCreate } = buildPrismaMock({ |
| 94 | + badges: [ALL_BADGES[2]], |
| 95 | + totalsByAsset: [ |
| 96 | + { assetCode: "XLM", assetIssuer: null, _sum: { amount: 150 } }, |
| 97 | + { assetCode: "USDC", assetIssuer: "GISSUER", _sum: { amount: 100000 } }, |
| 98 | + ], |
| 99 | + }); |
| 100 | + await checkAndAwardBadges("profile-1", { $transaction } as any); |
| 101 | + assert.deepEqual(awardedCriteria(profileBadgeCreate), ["total_100_xlm"]); |
| 102 | +}); |
| 103 | + |
| 104 | +test("total_100_xlm is NOT awarded from USDC totals alone", async () => { |
| 105 | + const { $transaction, profileBadgeCreate } = buildPrismaMock({ |
| 106 | + badges: [ALL_BADGES[2]], |
| 107 | + totalsByAsset: [ |
| 108 | + { assetCode: "USDC", assetIssuer: "GISSUER", _sum: { amount: 100000 } }, |
| 109 | + ], |
| 110 | + }); |
| 111 | + await checkAndAwardBadges("profile-1", { $transaction } as any); |
| 112 | + assert.equal(profileBadgeCreate.mock.calls.length, 0); |
| 113 | +}); |
| 114 | + |
| 115 | +test("milestone_reached is awarded when at least one milestone is reached", async () => { |
| 116 | + const { $transaction, profileBadgeCreate } = buildPrismaMock({ |
| 117 | + badges: [ALL_BADGES[3]], |
| 118 | + milestonesReached: 1, |
| 119 | + }); |
| 120 | + await checkAndAwardBadges("profile-1", { $transaction } as any); |
| 121 | + assert.deepEqual(awardedCriteria(profileBadgeCreate), ["milestone_reached"]); |
| 122 | +}); |
| 123 | + |
| 124 | +test("already-awarded badge is not re-awarded, and a P2002 race is silently ignored", async () => { |
| 125 | + const { $transaction, profileBadgeCreate } = buildPrismaMock({ |
| 126 | + badges: [ALL_BADGES[0]], |
| 127 | + existingAwards: [{ badgeId: "b-first" }], |
| 128 | + txCount: 5, |
| 129 | + }); |
| 130 | + await checkAndAwardBadges("profile-1", { $transaction } as any); |
| 131 | + assert.equal(profileBadgeCreate.mock.calls.length, 0); |
| 132 | + |
| 133 | + const p2002Error = Object.assign(new Error("Unique constraint failed"), { code: "P2002" }); |
| 134 | + const { $transaction: tx2, profileBadgeCreate: create2 } = buildPrismaMock({ |
| 135 | + badges: [ALL_BADGES[0]], |
| 136 | + txCount: 1, |
| 137 | + createImpl: () => Promise.reject(p2002Error), |
| 138 | + }); |
| 139 | + await assert.doesNotReject(() => checkAndAwardBadges("profile-1", { $transaction: tx2 } as any)); |
| 140 | + assert.equal(create2.mock.calls.length, 1); |
| 141 | +}); |
| 142 | + |
| 143 | +test("all badges already awarded returns early without checking any criteria", async () => { |
| 144 | + const { $transaction, tx, profileBadgeCreate } = buildPrismaMock({ |
| 145 | + badges: ALL_BADGES, |
| 146 | + existingAwards: ALL_BADGES.map((b) => ({ badgeId: b.id })), |
| 147 | + }); |
| 148 | + await checkAndAwardBadges("profile-1", { $transaction } as any); |
| 149 | + assert.equal(profileBadgeCreate.mock.calls.length, 0); |
| 150 | + assert.equal((tx.supportTransaction.count as ReturnType<typeof mock.fn>).mock.calls.length, 0); |
| 151 | +}); |
0 commit comments