|
| 1 | +import { SorobanContractFingerprinter } from './soroban-contract-fingerprinter'; |
| 2 | + |
| 3 | +const CONTRACT_A = ` |
| 4 | +impl TokenContract { |
| 5 | + pub fn new(env: Env) -> Self { Self {} } |
| 6 | + pub fn transfer(env: Env, from: Address, to: Address, amount: i128) {} |
| 7 | + fn validate(amount: i128) -> bool { amount > 0 } |
| 8 | +} |
| 9 | +`; |
| 10 | + |
| 11 | +const CONTRACT_B = ` |
| 12 | +impl TokenContract { |
| 13 | + pub fn new(env: Env) -> Self { Self {} } |
| 14 | + pub fn transfer(env: Env, from: Address, to: Address, amount: i128) {} |
| 15 | + fn validate(amount: i128) -> bool { amount > 0 } |
| 16 | +} |
| 17 | +`; |
| 18 | + |
| 19 | +const CONTRACT_C = ` |
| 20 | +impl StakingContract { |
| 21 | + pub fn stake(env: Env, amount: i128) {} |
| 22 | +} |
| 23 | +`; |
| 24 | + |
| 25 | +describe('SorobanContractFingerprinter', () => { |
| 26 | + let fingerprinter: SorobanContractFingerprinter; |
| 27 | + |
| 28 | + beforeEach(() => { |
| 29 | + fingerprinter = new SorobanContractFingerprinter(); |
| 30 | + }); |
| 31 | + |
| 32 | + describe('fingerprint', () => { |
| 33 | + it('should generate a fingerprint with expected fields', () => { |
| 34 | + const fp = fingerprinter.fingerprint(CONTRACT_A, 'contracts/token.rs'); |
| 35 | + expect(fp.fingerprint).toBeTruthy(); |
| 36 | + expect(fp.structuralHash).toBeTruthy(); |
| 37 | + expect(fp.contractName).toBe('TokenContract'); |
| 38 | + expect(fp.functionCount).toBeGreaterThan(0); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should produce the same fingerprint for identical sources', () => { |
| 42 | + const fp1 = fingerprinter.fingerprint(CONTRACT_A, 'a.rs'); |
| 43 | + const fp2 = fingerprinter.fingerprint(CONTRACT_B, 'b.rs'); |
| 44 | + expect(fp1.fingerprint).toBe(fp2.fingerprint); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should produce different fingerprints for different sources', () => { |
| 48 | + const fp1 = fingerprinter.fingerprint(CONTRACT_A, 'a.rs'); |
| 49 | + const fp2 = fingerprinter.fingerprint(CONTRACT_C, 'c.rs'); |
| 50 | + expect(fp1.fingerprint).not.toBe(fp2.fingerprint); |
| 51 | + }); |
| 52 | + }); |
| 53 | + |
| 54 | + describe('detectDuplicates', () => { |
| 55 | + it('should detect exact duplicates', () => { |
| 56 | + fingerprinter.fingerprint(CONTRACT_A, 'a.rs'); |
| 57 | + fingerprinter.fingerprint(CONTRACT_B, 'b.rs'); |
| 58 | + const duplicates = fingerprinter.detectDuplicates(); |
| 59 | + expect(duplicates).toHaveLength(1); |
| 60 | + expect(duplicates[0].similarity).toBe('exact'); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should return no duplicates for unique contracts', () => { |
| 64 | + fingerprinter.fingerprint(CONTRACT_A, 'a.rs'); |
| 65 | + fingerprinter.fingerprint(CONTRACT_C, 'c.rs'); |
| 66 | + expect(fingerprinter.detectDuplicates()).toHaveLength(0); |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + describe('generateReport', () => { |
| 71 | + it('should report duplicate and unique counts', () => { |
| 72 | + fingerprinter.fingerprint(CONTRACT_A, 'a.rs'); |
| 73 | + fingerprinter.fingerprint(CONTRACT_B, 'b.rs'); |
| 74 | + fingerprinter.fingerprint(CONTRACT_C, 'c.rs'); |
| 75 | + const report = fingerprinter.generateReport(); |
| 76 | + expect(report.duplicateCount).toBe(1); |
| 77 | + expect(report.uniqueCount).toBe(2); |
| 78 | + }); |
| 79 | + }); |
| 80 | +}); |
0 commit comments