|
| 1 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 2 | + |
| 3 | +// Mock helius before importing death-classifier |
| 4 | +vi.mock('../services/helius', () => ({ |
| 5 | + getEnhancedTransactions: vi.fn().mockResolvedValue([]), |
| 6 | + checkDeployerHoldings: vi.fn().mockResolvedValue(null), |
| 7 | + checkMintAuthority: vi.fn().mockResolvedValue(null), |
| 8 | + findFundingSource: vi.fn().mockResolvedValue(null), |
| 9 | + DEX_PROGRAM_IDS: new Set(['675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8']), |
| 10 | +})); |
| 11 | + |
| 12 | +import { classifyDeaths } from '../services/death-classifier'; |
| 13 | +import { checkMintAuthority, checkDeployerHoldings, getEnhancedTransactions } from '../services/helius'; |
| 14 | + |
| 15 | +const DEPLOYER = 'DeployerWallet111111111111111111111111111111'; |
| 16 | +const FUNDER = 'FunderWallet1111111111111111111111111111111'; |
| 17 | + |
| 18 | +beforeEach(() => { |
| 19 | + vi.clearAllMocks(); |
| 20 | +}); |
| 21 | + |
| 22 | +describe('Death Classifier: lifespan_hours correctness', () => { |
| 23 | + it('caps lifespan_hours at 168 (7 days) for old dead tokens', async () => { |
| 24 | + const sixMonthsAgo = new Date(Date.now() - 180 * 24 * 60 * 60 * 1000).toISOString(); |
| 25 | + const results = await classifyDeaths( |
| 26 | + DEPLOYER, |
| 27 | + [{ address: 'Token111111111111111111111111111111111111111', liquidity: 500, created_at: sixMonthsAgo }], |
| 28 | + null, |
| 29 | + ); |
| 30 | + const classification = results.get('Token111111111111111111111111111111111111111'); |
| 31 | + expect(classification).toBeDefined(); |
| 32 | + expect(classification!.evidence.lifespan_hours).toBeLessThanOrEqual(168); |
| 33 | + }); |
| 34 | + |
| 35 | + it('preserves short lifespan for recent tokens', async () => { |
| 36 | + const twoHoursAgo = new Date(Date.now() - 2 * 60 * 60 * 1000).toISOString(); |
| 37 | + const results = await classifyDeaths( |
| 38 | + DEPLOYER, |
| 39 | + [{ address: 'Token222222222222222222222222222222222222222', liquidity: 500, created_at: twoHoursAgo }], |
| 40 | + null, |
| 41 | + ); |
| 42 | + const classification = results.get('Token222222222222222222222222222222222222222'); |
| 43 | + expect(classification).toBeDefined(); |
| 44 | + expect(classification!.evidence.lifespan_hours).toBeLessThanOrEqual(3); // ~2h with rounding |
| 45 | + }); |
| 46 | +}); |
| 47 | + |
| 48 | +describe('Death Classifier: likely_rug detection', () => { |
| 49 | + it('classifies as likely_rug when deployer sold and had real buyers', async () => { |
| 50 | + const tokenAddr = 'Token333333333333333333333333333333333333333'; |
| 51 | + vi.mocked(checkMintAuthority).mockResolvedValue({ |
| 52 | + mintAuthority: null, |
| 53 | + freezeAuthority: null, |
| 54 | + supply: 1000000000, |
| 55 | + decimals: 9, |
| 56 | + }); |
| 57 | + vi.mocked(checkDeployerHoldings).mockResolvedValue(0); // deployer sold everything |
| 58 | + |
| 59 | + const results = await classifyDeaths( |
| 60 | + DEPLOYER, |
| 61 | + [{ address: tokenAddr, liquidity: 500, created_at: new Date(Date.now() - 3 * 24 * 60 * 60 * 1000).toISOString() }], |
| 62 | + null, |
| 63 | + ); |
| 64 | + const c = results.get(tokenAddr); |
| 65 | + expect(c).toBeDefined(); |
| 66 | + expect(c!.type).toBe('likely_rug'); |
| 67 | + expect(c!.evidence.deployer_sold).toBe(true); |
| 68 | + }); |
| 69 | + |
| 70 | + it('classifies quick dump within 48h as likely_rug', async () => { |
| 71 | + const tokenAddr = 'Token444444444444444444444444444444444444444'; |
| 72 | + vi.mocked(checkMintAuthority).mockResolvedValue({ |
| 73 | + mintAuthority: null, |
| 74 | + freezeAuthority: null, |
| 75 | + supply: 1000000000, |
| 76 | + decimals: 9, |
| 77 | + }); |
| 78 | + vi.mocked(checkDeployerHoldings).mockResolvedValue(0); // deployer sold |
| 79 | + |
| 80 | + // Token created 6 hours ago — should still trigger quick dump after lifespan fix |
| 81 | + const sixHoursAgo = new Date(Date.now() - 6 * 60 * 60 * 1000).toISOString(); |
| 82 | + const results = await classifyDeaths( |
| 83 | + DEPLOYER, |
| 84 | + [{ address: tokenAddr, liquidity: 10, created_at: sixHoursAgo }], // low liquidity but deployer sold |
| 85 | + null, |
| 86 | + ); |
| 87 | + const c = results.get(tokenAddr); |
| 88 | + expect(c).toBeDefined(); |
| 89 | + expect(c!.type).toBe('likely_rug'); |
| 90 | + expect(c!.evidence.lifespan_hours).toBeLessThan(48); |
| 91 | + }); |
| 92 | +}); |
| 93 | + |
| 94 | +describe('Death Classifier: natural death', () => { |
| 95 | + it('classifies as natural when no real buyers and deployer still holds', async () => { |
| 96 | + const tokenAddr = 'Token555555555555555555555555555555555555555'; |
| 97 | + vi.mocked(checkMintAuthority).mockResolvedValue({ |
| 98 | + mintAuthority: null, |
| 99 | + freezeAuthority: null, |
| 100 | + supply: 1000000000, |
| 101 | + decimals: 9, |
| 102 | + }); |
| 103 | + vi.mocked(checkDeployerHoldings).mockResolvedValue(50); // deployer still holds 50% |
| 104 | + |
| 105 | + const results = await classifyDeaths( |
| 106 | + DEPLOYER, |
| 107 | + [{ address: tokenAddr, liquidity: 10, created_at: new Date(Date.now() - 24 * 60 * 60 * 1000).toISOString() }], |
| 108 | + null, |
| 109 | + ); |
| 110 | + const c = results.get(tokenAddr); |
| 111 | + expect(c).toBeDefined(); |
| 112 | + expect(c!.type).toBe('natural'); |
| 113 | + expect(c!.evidence.deployer_sold).toBe(false); |
| 114 | + }); |
| 115 | + |
| 116 | + it('classifies tokens with no DexScreener data as natural (never got traction)', async () => { |
| 117 | + const tokenAddr = 'Token666666666666666666666666666666666666666'; |
| 118 | + const results = await classifyDeaths( |
| 119 | + DEPLOYER, |
| 120 | + [{ address: tokenAddr, liquidity: 0, created_at: null }], |
| 121 | + null, |
| 122 | + ); |
| 123 | + const c = results.get(tokenAddr); |
| 124 | + expect(c).toBeDefined(); |
| 125 | + expect(c!.type).toBe('natural'); |
| 126 | + }); |
| 127 | +}); |
| 128 | + |
| 129 | +describe('Death Classifier: token at 48h boundary', () => { |
| 130 | + it('token at exactly 48h with deployer sold = likely_rug (boundary)', async () => { |
| 131 | + const tokenAddr = 'Token777777777777777777777777777777777777777'; |
| 132 | + vi.mocked(checkMintAuthority).mockResolvedValue({ |
| 133 | + mintAuthority: null, |
| 134 | + freezeAuthority: null, |
| 135 | + supply: 1000000000, |
| 136 | + decimals: 9, |
| 137 | + }); |
| 138 | + vi.mocked(checkDeployerHoldings).mockResolvedValue(0); |
| 139 | + |
| 140 | + // Exactly 47h ago — should still be under 48h after rounding |
| 141 | + const created = new Date(Date.now() - 47 * 60 * 60 * 1000).toISOString(); |
| 142 | + const results = await classifyDeaths( |
| 143 | + DEPLOYER, |
| 144 | + [{ address: tokenAddr, liquidity: 10, created_at: created }], |
| 145 | + null, |
| 146 | + ); |
| 147 | + const c = results.get(tokenAddr); |
| 148 | + expect(c).toBeDefined(); |
| 149 | + expect(c!.type).toBe('likely_rug'); |
| 150 | + expect(c!.evidence.lifespan_hours).toBeLessThan(48); |
| 151 | + }); |
| 152 | +}); |
| 153 | + |
| 154 | +describe('Death Classifier: classification limit', () => { |
| 155 | + it('classifies up to 50 tokens (not just 20)', async () => { |
| 156 | + const deadTokens = Array.from({ length: 60 }, (_, i) => ({ |
| 157 | + address: `Token${String(i).padStart(39, '0')}AA`, |
| 158 | + liquidity: 100 + i, |
| 159 | + created_at: new Date(Date.now() - 24 * 60 * 60 * 1000).toISOString(), |
| 160 | + })); |
| 161 | + |
| 162 | + const results = await classifyDeaths(DEPLOYER, deadTokens, null); |
| 163 | + // All 60 should get some classification (50 classifiable + 10 overflow as natural) |
| 164 | + expect(results.size).toBe(60); |
| 165 | + // The top 50 by liquidity should have been processed (even if they end up as natural/unverified) |
| 166 | + // The bottom 10 should be natural (no DexScreener classification needed) |
| 167 | + }); |
| 168 | +}); |
0 commit comments