import { describe, expect, it } from 'vitest' import { identify, letters, type Marks } from './asset' /** * What an asset's identity is derived from. * * The letters are the whole reason the fallback helps. Letters that collide * mislead faster than none — on a chain where `L` alone names LUX, LBTC, LUSD * and LZOO, two characters name nothing. */ describe('the letters', () => { it('separates a family whose symbols share their first letter', () => { const family = ['LUX', 'LBTC', 'LUSD', 'LZOO', 'LETH', 'LFTM', 'LCELO'] expect(new Set(family.map((s) => letters(s))).size).toBe(family.length) }) it('is an aid and not an identifier, so a collision survives', () => { // USDC and USDT still share three characters, and are meant to: the symbol // is in the row beside the mark and the shade comes from the address. expect(letters('USDC')).toBe(letters('USDT')) }) it('upper-cases, so the same asset reads the same however it was recorded', () => { expect(letters('wlux')).toBe(letters('WLUX')) }) it('drops punctuation rather than spending a slot on it', () => { expect(letters('$LUX')).toBe('LUX') expect(letters('w-eth')).toBe('WET') }) it('falls back to the address where an asset states no symbol', () => { // An indexer records a pool before it has ingested the tokens in it, so a // symbol is genuinely absent sometimes. Two such assets still differ. expect(letters(undefined, '0xab12')).toBe('AB') expect(letters('', '0xcd34')).toBe('CD') expect(letters(undefined, '0xab12')).not.toBe(letters(undefined, '0xcd34')) }) it('says so rather than drawing an empty circle when it has nothing', () => { expect(letters(undefined, undefined)).toBe('??') }) }) describe('one asset, as a build can draw it', () => { const served: Marks = { token: (symbol) => (symbol === 'lux' ? '/tokens/lux.svg' : null), chain: (key) => (key === 'lux' ? '/chains/lux.png' : null), } it('wears the mark this build serves, found by the symbol lower-cased', () => { expect(identify({ symbol: 'LUX' }, served).icon).toBe('/tokens/lux.svg') }) it('answers null for a mark the build has no file for, rather than a path', () => { // A path to a file that is not there is a 404 and a broken frame. Null is // what makes the letters draw instead. expect(identify({ symbol: 'LUSD' }, served).icon).toBeNull() expect(identify({ symbol: 'LUSD' }, served).fallback).toBe('LUS') }) it('badges an asset only where the caller named a chain the build serves', () => { expect(identify({ symbol: 'LUX', chain: 'lux' }, served).badge).toBe('/chains/lux.png') expect(identify({ symbol: 'LUX', chain: 'zoo' }, served).badge).toBeNull() expect(identify({ symbol: 'LUX' }, served).badge).toBeNull() }) it('lower-cases the address, so one asset takes one shade', () => { const A = '0x4888E4A2EE0F03051C72D2BD3ACF755ED3498B3E' expect(identify({ address: A }, served).address).toBe(A.toLowerCase()) }) it('draws letters for a build that serves nothing', () => { const it = identify({ symbol: 'LUX', chain: 'lux' }) expect(it.icon).toBeNull() expect(it.badge).toBeNull() expect(it.fallback).toBe('LUX') }) })