|
| 1 | +import { |
| 2 | + follow, |
| 3 | + unfollow, |
| 4 | + getFollowerCount, |
| 5 | + compactShardsForCreator, |
| 6 | +} from './follower.service'; |
| 7 | +import { prisma } from '../../utils/prisma.utils'; |
| 8 | + |
| 9 | +jest.mock('../../utils/logger.utils', () => ({ |
| 10 | + logger: { |
| 11 | + debug: jest.fn(), |
| 12 | + info: jest.fn(), |
| 13 | + warn: jest.fn(), |
| 14 | + error: jest.fn(), |
| 15 | + }, |
| 16 | +})); |
| 17 | + |
| 18 | +// In-memory mock for Prisma to test CRDT logic isolated from real DB |
| 19 | +jest.mock('../../utils/prisma.utils', () => { |
| 20 | + const followEvents = new Map<string, { followerWallet: string; creatorWallet: string; direction: string }>(); |
| 21 | + const shards = new Map<string, { id: string; creatorWallet: string; nodeId: string; increments: bigint; decrements: bigint; updatedAt: Date }>(); |
| 22 | + |
| 23 | + return { |
| 24 | + prisma: { |
| 25 | + followEvent: { |
| 26 | + findUnique: jest.fn().mockImplementation(async ({ where }) => { |
| 27 | + const key = `${where.followerWallet_creatorWallet.followerWallet}:${where.followerWallet_creatorWallet.creatorWallet}`; |
| 28 | + return followEvents.get(key) || null; |
| 29 | + }), |
| 30 | + upsert: jest.fn().mockImplementation(async ({ where, update, create }) => { |
| 31 | + const key = `${where.followerWallet_creatorWallet.followerWallet}:${where.followerWallet_creatorWallet.creatorWallet}`; |
| 32 | + const existing = followEvents.get(key); |
| 33 | + const direction = existing ? update.direction : create.direction; |
| 34 | + const record = { |
| 35 | + followerWallet: where.followerWallet_creatorWallet.followerWallet, |
| 36 | + creatorWallet: where.followerWallet_creatorWallet.creatorWallet, |
| 37 | + direction, |
| 38 | + }; |
| 39 | + followEvents.set(key, record); |
| 40 | + return record; |
| 41 | + }), |
| 42 | + update: jest.fn().mockImplementation(async ({ where, data }) => { |
| 43 | + const key = `${where.followerWallet_creatorWallet.followerWallet}:${where.followerWallet_creatorWallet.creatorWallet}`; |
| 44 | + const existing = followEvents.get(key); |
| 45 | + if (existing) { |
| 46 | + existing.direction = data.direction; |
| 47 | + } |
| 48 | + return existing; |
| 49 | + }), |
| 50 | + }, |
| 51 | + followerCounterShard: { |
| 52 | + findUnique: jest.fn().mockImplementation(async ({ where }) => { |
| 53 | + const key = `${where.creatorWallet_nodeId.creatorWallet}:${where.creatorWallet_nodeId.nodeId}`; |
| 54 | + return shards.get(key) || null; |
| 55 | + }), |
| 56 | + findMany: jest.fn().mockImplementation(async ({ where }) => { |
| 57 | + const list = Array.from(shards.values()).filter( |
| 58 | + s => s.creatorWallet === where.creatorWallet |
| 59 | + ); |
| 60 | + if (where.updatedAt?.gte) { |
| 61 | + return list.filter(s => s.updatedAt >= where.updatedAt.gte); |
| 62 | + } |
| 63 | + return list; |
| 64 | + }), |
| 65 | + findFirst: jest.fn().mockImplementation(async ({ where }) => { |
| 66 | + const list = Array.from(shards.values()).filter( |
| 67 | + s => s.creatorWallet === where.creatorWallet |
| 68 | + ); |
| 69 | + if (where.updatedAt?.gte) { |
| 70 | + return list.find(s => s.updatedAt >= where.updatedAt.gte) || null; |
| 71 | + } |
| 72 | + return list[0] || null; |
| 73 | + }), |
| 74 | + create: jest.fn().mockImplementation(async ({ data }) => { |
| 75 | + const key = `${data.creatorWallet}:${data.nodeId}`; |
| 76 | + let record = shards.get(key); |
| 77 | + if (record) { |
| 78 | + record.increments += BigInt(data.increments ?? 0); |
| 79 | + record.decrements += BigInt(data.decrements ?? 0); |
| 80 | + } else { |
| 81 | + record = { |
| 82 | + id: key, |
| 83 | + creatorWallet: data.creatorWallet, |
| 84 | + nodeId: data.nodeId, |
| 85 | + increments: BigInt(data.increments ?? 0), |
| 86 | + decrements: BigInt(data.decrements ?? 0), |
| 87 | + updatedAt: new Date(), |
| 88 | + }; |
| 89 | + shards.set(key, record); |
| 90 | + } |
| 91 | + return record; |
| 92 | + }), |
| 93 | + update: jest.fn().mockImplementation(async ({ where, data }) => { |
| 94 | + const key = `${where.creatorWallet_nodeId.creatorWallet}:${where.creatorWallet_nodeId.nodeId}`; |
| 95 | + let record = shards.get(key); |
| 96 | + if (!record) { |
| 97 | + record = { |
| 98 | + id: key, |
| 99 | + creatorWallet: where.creatorWallet_nodeId.creatorWallet, |
| 100 | + nodeId: where.creatorWallet_nodeId.nodeId, |
| 101 | + increments: 0n, |
| 102 | + decrements: 0n, |
| 103 | + updatedAt: new Date(), |
| 104 | + }; |
| 105 | + shards.set(key, record); |
| 106 | + } |
| 107 | + if (data.increments?.increment) { |
| 108 | + record.increments += BigInt(data.increments.increment); |
| 109 | + } |
| 110 | + if (data.decrements?.increment) { |
| 111 | + record.decrements += BigInt(data.decrements.increment); |
| 112 | + } |
| 113 | + record.updatedAt = new Date(); |
| 114 | + return record; |
| 115 | + }), |
| 116 | + deleteMany: jest.fn().mockImplementation(async ({ where }) => { |
| 117 | + for (const [key, shard] of shards.entries()) { |
| 118 | + if (shard.creatorWallet === where.creatorWallet) { |
| 119 | + shards.delete(key); |
| 120 | + } |
| 121 | + } |
| 122 | + return { count: 1 }; |
| 123 | + }), |
| 124 | + }, |
| 125 | + $executeRaw: jest.fn().mockRejectedValue(new Error('raw query fallback')), |
| 126 | + $transaction: jest.fn().mockImplementation(async (actions) => Promise.all(actions)), |
| 127 | + _reset: () => { |
| 128 | + followEvents.clear(); |
| 129 | + shards.clear(); |
| 130 | + }, |
| 131 | + }, |
| 132 | + }; |
| 133 | +}); |
| 134 | + |
| 135 | +jest.mock('../../utils/redis.utils', () => { |
| 136 | + const redisStore = new Map<string, string>(); |
| 137 | + return { |
| 138 | + getRedis: () => ({ |
| 139 | + get: jest.fn().mockImplementation(async (key: string) => redisStore.get(key) ?? null), |
| 140 | + set: jest.fn().mockImplementation(async (key: string, val: string) => { |
| 141 | + redisStore.set(key, val); |
| 142 | + return 'OK'; |
| 143 | + }), |
| 144 | + del: jest.fn().mockImplementation(async (key: string) => { |
| 145 | + redisStore.delete(key); |
| 146 | + return 1; |
| 147 | + }), |
| 148 | + }), |
| 149 | + }; |
| 150 | +}); |
| 151 | + |
| 152 | +describe('CRDT Follower Counter (#757)', () => { |
| 153 | + const creatorWallet = 'GCREATOR_CRDT_TEST'; |
| 154 | + |
| 155 | + beforeEach(() => { |
| 156 | + (prisma as any)._reset(); |
| 157 | + jest.clearAllMocks(); |
| 158 | + }); |
| 159 | + |
| 160 | + it('100 concurrent follows from different wallets produces count of exactly 100', async () => { |
| 161 | + const promises = Array.from({ length: 100 }, (_, i) => |
| 162 | + follow(`follower-wallet-${i}`, creatorWallet, 'node-1') |
| 163 | + ); |
| 164 | + |
| 165 | + await Promise.all(promises); |
| 166 | + |
| 167 | + const count = await getFollowerCount(creatorWallet); |
| 168 | + expect(count).toBe(100); |
| 169 | + }); |
| 170 | + |
| 171 | + it('50 concurrent follows and 30 concurrent unfollows produces net count of 20', async () => { |
| 172 | + // First 50 follow |
| 173 | + const followPromises = Array.from({ length: 50 }, (_, i) => |
| 174 | + follow(`follower-${i}`, creatorWallet, 'node-1') |
| 175 | + ); |
| 176 | + await Promise.all(followPromises); |
| 177 | + |
| 178 | + // 30 of them unfollow |
| 179 | + const unfollowPromises = Array.from({ length: 30 }, (_, i) => |
| 180 | + unfollow(`follower-${i}`, creatorWallet, 'node-1') |
| 181 | + ); |
| 182 | + await Promise.all(unfollowPromises); |
| 183 | + |
| 184 | + const count = await getFollowerCount(creatorWallet); |
| 185 | + expect(count).toBe(20); |
| 186 | + }); |
| 187 | + |
| 188 | + it('double-follow from the same wallet is idempotent and increments count only once', async () => { |
| 189 | + const first = await follow('wallet-alice', creatorWallet, 'node-1'); |
| 190 | + const second = await follow('wallet-alice', creatorWallet, 'node-1'); |
| 191 | + |
| 192 | + expect(first.followed).toBe(true); |
| 193 | + expect(second.followed).toBe(false); |
| 194 | + |
| 195 | + const count = await getFollowerCount(creatorWallet); |
| 196 | + expect(count).toBe(1); |
| 197 | + }); |
| 198 | + |
| 199 | + it('sums counts across multiple node shards correctly', async () => { |
| 200 | + await follow('wallet-1', creatorWallet, 'node-alpha'); |
| 201 | + await follow('wallet-2', creatorWallet, 'node-alpha'); |
| 202 | + await follow('wallet-3', creatorWallet, 'node-beta'); |
| 203 | + |
| 204 | + const count = await getFollowerCount(creatorWallet); |
| 205 | + expect(count).toBe(3); |
| 206 | + }); |
| 207 | + |
| 208 | + it('nightly compaction merges shards atomically while maintaining identical resolved count', async () => { |
| 209 | + await follow('wallet-1', creatorWallet, 'node-1'); |
| 210 | + await follow('wallet-2', creatorWallet, 'node-2'); |
| 211 | + await follow('wallet-3', creatorWallet, 'node-3'); |
| 212 | + |
| 213 | + const countBefore = await getFollowerCount(creatorWallet); |
| 214 | + expect(countBefore).toBe(3); |
| 215 | + |
| 216 | + // Mock Date.now to simulate 6 minutes passing so compaction is allowed |
| 217 | + const realNow = Date.now; |
| 218 | + jest.spyOn(Date, 'now').mockReturnValue(realNow() + 6 * 60 * 1000); |
| 219 | + |
| 220 | + const compacted = await compactShardsForCreator(creatorWallet); |
| 221 | + expect(compacted).toBe(true); |
| 222 | + |
| 223 | + const countAfter = await getFollowerCount(creatorWallet); |
| 224 | + expect(countAfter).toBe(3); |
| 225 | + |
| 226 | + jest.restoreAllMocks(); |
| 227 | + }); |
| 228 | +}); |
0 commit comments