import { expect, test } from 'vitest' import { allowed, connected, covered, entered, first, known, quoted, waits, type Condition, } from './condition' /** * The label a person reads before they press. * * These assertions are about wording as much as logic. "Swap" on a button that * is about to revert is the failure this module removes, and every way that * failure comes back is a condition wrongly reported as met — so each case * below is one of those ways. */ const nothing = async () => {} test('the first unmet condition is the one that shows', () => { const conditions: Condition[] = [ connected('Connect wallet', '0xabc', nothing), entered('Enter an amount', ''), covered('Not enough LUX', 5n, 1n), ] expect(first(conditions)?.label).toBe('Enter an amount') }) test('order comes from the kind, not from where a screen listed it', () => { // Written backwards on purpose: approval before funds before amount before // wallet. A screen cannot reorder these by listing them differently. const conditions: Condition[] = [ allowed('Approve USDC', 5n, 0n, nothing), covered('Not enough USDC', 5n, 1n), entered('Enter an amount', ''), connected('Connect wallet', null, nothing), ] expect(first(conditions)?.label).toBe('Connect wallet') }) test('a missing quote waits until the person has said what they want', () => { // A card that refuses before a reader has said anything has spent its one // press on a sentence. const asked = (account: string | null, amount: string): Condition[] => [ connected('Connect wallet', account, nothing), entered('Enter an amount', amount), quoted('No quotes found', false), ] expect(first(asked(null, ''))?.label).toBe('Connect wallet') expect(first(asked('0xabc', ''))?.label).toBe('Enter an amount') expect(first(asked('0xabc', '100'))?.label).toBe('No quotes found') }) test('a quote nobody can produce offers no press', () => { expect(quoted('No quotes found', false).act).toBeUndefined() expect(quoted('No quotes found', true).met).toBe(true) }) test('every condition holding is the only state that reaches the verb', () => { expect(first([connected('Connect wallet', '0xabc', nothing), entered('Enter an amount', 5n)])).toBeNull() expect(first([])).toBeNull() }) test('nobody connected offers the one press that helps', () => { const c = connected('Connect wallet', null, nothing) expect(c.met).toBe(false) // There is a press: an accurate sentence the reader cannot act on is still a // dead end, and getting a wallet is the thing that would help. expect(c.act).toBeTypeOf('function') }) test('a missing amount is not something a press can supply', () => { expect(entered('Enter an amount', '').act).toBeUndefined() expect(entered('Enter a recipient', undefined).met).toBe(false) expect(entered('Enter an amount', 0n).met).toBe(false) expect(entered('Enter an amount', '0.5').met).toBe(true) }) test('an unread balance holds rather than announcing a shortfall', () => { expect(covered('Not enough LUX', 5n, undefined).met).toBe(true) expect(covered('Not enough LUX', null, 0n).met).toBe(true) expect(covered('Not enough LUX', 5n, 5n).met).toBe(true) expect(covered('Not enough LUX', 5n, 4n).met).toBe(false) }) test('an unread allowance does not demand an approval', () => { expect(allowed('Approve USDC', 5n, undefined, nothing).met).toBe(true) expect(allowed('Approve USDC', 5n, 5n, nothing).met).toBe(true) expect(allowed('Approve USDC', 5n, 4n, nothing).met).toBe(false) }) test('approval is a press; a shortfall is not', () => { expect(allowed('Approve USDC', 5n, 0n, nothing).act).toBeDefined() expect(covered('Not enough USDC', 5n, 0n).act).toBeUndefined() }) test('what the chain has not said is asked about before anything the person can supply', () => { const conditions: Condition[] = [ connected('Connect wallet', null, nothing), entered('Enter an amount', ''), known('Nothing here can read that position', false), ] expect(first(conditions)?.label).toBe('Nothing here can read that position') }) test('an obstacle that is nobody’s fault offers no press', () => { expect(known('This needs a call this interface cannot make', false).act).toBeUndefined() expect(known('never seen', true).met).toBe(true) expect(first([known('never seen', true), entered('Enter an amount', 1n)])).toBeNull() }) test('the chooser opens here; everything else is handed out and waited on', () => { // A form reads the button's sentence off this: a press that stays on the page // has handed nobody anything to confirm, and the button keeps its sentence // while the chooser is open. expect(waits(connected('Connect wallet', null, nothing))).toBe(false) expect(waits(allowed('Approve USDC', 5n, 0n, nothing))).toBe(true) expect(waits(entered('Enter an amount', ''))).toBe(true) // Nothing in the way is the verb itself, which is the press that goes out. expect(waits(null)).toBe(true) })