From 605776a9fcbed0a8552eda1e065ac066a91bf80e Mon Sep 17 00:00:00 2001 From: Dean Tribble Date: Sun, 3 Apr 2022 16:54:52 -0700 Subject: [PATCH 1/2] test(runStake): use test driver * convert the test cases in the data file to use test driver explicitly * step converts to a call on the driver. --- .../test/runStake/runStake-test-steps.js | 209 ---------------- .../test/runStake/test-runStake.js | 236 +++++++++++++++--- 2 files changed, 206 insertions(+), 239 deletions(-) delete mode 100644 packages/run-protocol/test/runStake/runStake-test-steps.js diff --git a/packages/run-protocol/test/runStake/runStake-test-steps.js b/packages/run-protocol/test/runStake/runStake-test-steps.js deleted file mode 100644 index 1e2f38ec3d1..00000000000 --- a/packages/run-protocol/test/runStake/runStake-test-steps.js +++ /dev/null @@ -1,209 +0,0 @@ -/** - * @typedef {[string, unknown] | [string, unknown, false]} Step - * @typedef {[string, string, Step[]]} TestCase - * @type {TestCase[]} - */ -export const CASES = [ - [ - '0', - 'Borrow, pay off', - [ - ['buyBLD', 80000n], - ['stakeBLD', 80000n], - ['lienBLD', 8000n], - ['borrowRUN', 1000n], - ['checkRUNBalance', 1000n], - ['earnRUNReward', 25n], - ['payoffRUN', 1020n], - ['checkRUNDebt', 0n], - ['checkBLDLiened', 0n], - ['checkRUNBalance', 5n], - ], - ], - [ - '1', - 'Starting LoC', - [ - ['buyBLD', 9000n], - ['stakeBLD', 9000n], - ['checkBLDLiened', 0n], - ['checkRUNBalance', 0n], - ['lienBLD', 6000n], - ['borrowRUN', 100n], - ['checkRUNDebt', 102n], - ['checkBLDLiened', 6000n], - ['checkRUNBalance', 100n], - ['borrowMoreRUN', 100n], - ['checkRUNBalance', 200n], - ['checkRUNDebt', 204n], - ['checkBLDLiened', 6000n], - ['stakeBLD', 5000n], - ['lienBLD', 8000n], - ['checkBLDLiened', 8000n], - ['borrowMoreRUN', 1400n], - ['checkRUNDebt', 1632n], - ], - ], - [ - '4', - 'Extending LoC - CR increases (FAIL)', - [ - ['buyBLD', 80000n], - ['stakeBLD', 80000n], - ['lienBLD', 8000n], - ['borrowRUN', 1000n], - ['setMintingRatio', [16n, 100n]], - ['borrowMoreRUN', 500n, false], - ['checkRUNBalance', 1000n], - ['checkBLDLiened', 8000n], - ['earnRUNReward', 25n], - ['payoffRUN', 1021n], - ['checkRUNDebt', 0n], - ['checkBLDLiened', 0n], - ], - ], - [ - '6', - 'Partial repayment - CR remains the same', - [ - ['buyBLD', 10000n], - ['stakeBLD', 10000n], - ['lienBLD', 10000n], - ['borrowRUN', 1000n], - ['payDownRUN', 50n], - ['checkRUNBalance', 950n], - ['checkRUNDebt', 970n], - ], - ], - [ - '7', - 'Partial repayment - CR increases*', - [ - ['buyBLD', 10000n], - ['stakeBLD', 10000n], - ['lienBLD', 400n], - ['borrowRUN', 100n], - ['setMintingRatio', [16n, 100n]], - ['payDownRUN', 5n], - ['checkRUNBalance', 95n], - ['checkBLDLiened', 400n], - ], - ], - [ - '11', - 'Partial repay - unbonded ok', - [ - ['buyBLD', 1000n], - ['stakeBLD', 800n], - ['lienBLD', 800n], - ['borrowRUN', 100n], - ['slash', 700n], - ['checkBLDLiened', 800n], - ['checkRUNBalance', 100n], - ['payDownRUN', 50n], - ['checkRUNBalance', 50n], - ['checkBLDLiened', 800n], - ['checkBLDStaked', 100n], - ], - ], - [ - '14', - 'Add collateral - more BLD required (FAIL)', - [ - ['buyBLD', 1000n], - ['stakeBLD', 1000n], - ['lienBLD', 800n], - ['borrowRUN', 100n], - ['borrowMoreRUN', 200n, false], - ['checkRUNBalance', 100n], - ['checkBLDLiened', 800n], - ], - ], - [ - '15', - 'Lower collateral', - [ - ['buyBLD', 1000n], - ['stakeBLD', 1000n], - ['lienBLD', 800n], - ['borrowRUN', 100n], - ['unlienBLD', 350n], - ['checkRUNBalance', 100n], - ['checkBLDLiened', 450n], - ], - ], - [ - '16', - 'Lower collateral - CR increase (FAIL)', - [ - ['buyBLD', 1000n], - ['stakeBLD', 1000n], - ['lienBLD', 800n], - ['borrowRUN', 100n], - ['setMintingRatio', [16n, 100n]], - ['unlienBLD', 400n, false], - ['checkBLDLiened', 800n], - ], - ], - [ - '17', - 'Lower collateral - unbonded ok', - [ - ['buyBLD', 1000n], - ['stakeBLD', 1000n], - ['earnRUNReward', 5n], - ['lienBLD', 800n], - ['borrowRUN', 100n], - ['slash', 770n], - ['checkBLDLiened', 800n], - ['unlienBLD', 375n], - ['checkRUNBalance', 105n], - ['checkBLDLiened', 425n], - ['setMintingRatio', [16n, 100n]], - ['payoffRUN', 103n], - ['checkRUNBalance', 3n], - ['checkBLDLiened', 0n], - ], - ], - [ - '18', - 'Lower collateral by paying off DEBT', - [ - ['buyBLD', 1000n], - ['stakeBLD', 1000n], - ['lienBLD', 800n], - ['borrowRUN', 190n], - ['payToUnlien', [100n, 300n]], - ['checkBLDLiened', 500n], - ], - ], - [ - '19', - 'Watch interest accrue', - [ - ['buyBLD', 1000n], - ['stakeBLD', 1000n], - ['lienBLD', 800n], - ['borrowRUN', 190n], - ['checkRUNDebt', 194n], - ['waitDays', 90n], - ['checkRUNDebt', 195n], - ], - ], - [ - '20', - 'payoff more than you owe', - [ - ['buyBLD', 1000n], - ['stakeBLD', 1000n], - ['lienBLD', 800n], - ['borrowRUN', 190n], - ['checkRUNDebt', 194n], - ['earnRUNReward', 20n], - ['payoffRUN', 200n], - ['checkRUNDebt', 0n], - ['checkBLDLiened', 0n], - ['checkRUNBalance', 16n], - ], - ], -]; diff --git a/packages/run-protocol/test/runStake/test-runStake.js b/packages/run-protocol/test/runStake/test-runStake.js index 011da83d064..59437b85f07 100644 --- a/packages/run-protocol/test/runStake/test-runStake.js +++ b/packages/run-protocol/test/runStake/test-runStake.js @@ -34,7 +34,7 @@ import { KW } from '../../src/runStake/params.js'; // 10 Partial repay - insufficient funds (FAIL) - Zoe prevents this // 12 Add collateral - lots of test harness for little gain // 13 Add collateral - CR increase ok -import { CASES as TestData } from './runStake-test-steps.js'; +// import { CASES as TestData } from './runStake-test-steps.js'; const contractRoots = { runStake: '../../src/runStake/runStake.js', @@ -478,9 +478,9 @@ const approxEqual = (t, actual, expected, epsilon) => { } }; -const makeWorld = async t0 => { - const bundles = theBundles(t0); - const timer = buildManualTimer(t0.log, 0n, SECONDS_PER_DAY); +const makeWorld = async t => { + const bundles = theBundles(t); + const timer = buildManualTimer(t.log, 0n, SECONDS_PER_DAY); const { chain, space } = await bootstrapRunStake(bundles, timer); const { consume } = space; @@ -566,7 +566,7 @@ const makeWorld = async t0 => { const attPmt = await E(bobWallet.attMaker).makeAttestation(delta); await E(attPurse).deposit(attPmt); }, - checkBLDStaked: async (expected, t) => { + checkBLDStaked: async expected => { const actual = await E(lienBridge).getAccountState( bob.getAddress(), bldBrand, @@ -576,7 +576,7 @@ const makeWorld = async t0 => { AmountMath.make(bldBrand, expected * micro.unit), ); }, - checkBLDLiened: async (expected, t) => { + checkBLDLiened: async expected => { const actual = await E(lienBridge).getAccountState( bob.getAddress(), bldBrand, @@ -698,7 +698,7 @@ const makeWorld = async t0 => { await returnAttestation(attBack); await E(runPurse).deposit(runPmt); }, - checkRUNBalance: async (target, t) => { + checkRUNBalance: async target => { const actual = await E(runPurse).getCurrentAmount(); approxEqual( t, @@ -707,7 +707,7 @@ const makeWorld = async t0 => { epsilon, ); }, - checkRUNDebt: async (expected, t) => { + checkRUNDebt: async expected => { const { vault } = offerResult; const debt = await E(vault).getCurrentDebt(); approxEqual( @@ -739,28 +739,28 @@ const makeWorld = async t0 => { return driver; }; -const makeTests = async () => { - for await (const [num, name, steps] of TestData) { - test(`Test Data ${num} ${name}`, async t => { - // TODO: use different accounts / addresses in the same world. - const driver = await makeWorld(t); - for await (const [tag, value, pass] of steps) { - t.log({ tag, value }); - const fn = driver[tag]; - if (!fn) { - throw Error(`bad tag: ${tag}`); - } - if (pass === false) { - await t.throwsAsync(async () => fn(value, t)); - } else { - await fn(value, t); - } - } - }); - } -}; - -makeTests(); +// const makeTests = async () => { +// for await (const [num, name, steps] of TestData) { +// test(`Test Data ${num} ${name}`, async t => { +// // TODO: use different accounts / addresses in the same world. +// const driver = await makeWorld(t); +// for await (const [tag, value, pass] of steps) { +// t.log({ tag, value }); +// const fn = driver[tag]; +// if (!fn) { +// throw Error(`bad tag: ${tag}`); +// } +// if (pass === false) { +// await t.throwsAsync(async () => fn(value, t)); +// } else { +// await fn(value, t); +// } +// } +// }); +// } +// }; + +// makeTests(); test('borrowing past the debt limit', async t => { const driver = await makeWorld(t); @@ -779,3 +779,179 @@ test('borrowing past the debt limit', async t => { 'Minting {"brand":"[Alleged: RUN brand]","value":"[1020000000000n]"} past {"brand":"[Alleged: RUN brand]","value":"[0n]"} would exceed total debt limit {"brand":"[Alleged: RUN brand]","value":"[1000000000000n]"}', }); }); + +test('Borrow, pay off', async t => { + const d = await makeWorld(t); + await d.buyBLD(80000n); + await d.stakeBLD(80000n); + await d.lienBLD(8000n); + await d.borrowRUN(1000n); + await d.checkRUNBalance(1000n); + await d.earnRUNReward(25n); + await d.payoffRUN(1020n); + await d.checkRUNDebt(0n); + await d.checkBLDLiened(0n); + await d.checkRUNBalance(5n); +}); + +test('Starting LoC', async t => { + const d = await makeWorld(t); + await d.buyBLD(9000n); + await d.stakeBLD(9000n); + await d.checkBLDLiened(0n); + await d.checkRUNBalance(0n); + await d.lienBLD(6000n); + await d.borrowRUN(100n); + await d.checkRUNDebt(102n); + await d.checkBLDLiened(6000n); + await d.checkRUNBalance(100n); + await d.borrowMoreRUN(100n); + await d.checkRUNBalance(200n); + await d.checkRUNDebt(204n); + await d.checkBLDLiened(6000n); + await d.stakeBLD(5000n); + await d.lienBLD(8000n); + await d.checkBLDLiened(8000n); + await d.borrowMoreRUN(1400n); + await d.checkRUNDebt(1632n); +}); + +test('Extending LoC - CR increases (FAIL)', async t => { + const d = await makeWorld(t); + await d.buyBLD(80000n); + await d.stakeBLD(80000n); + await d.lienBLD(8000n); + await d.borrowRUN(1000n); + await d.setMintingRatio([16n, 100n]); + await t.throwsAsync(async () => d.borrowMoreRUN(500n)); + await d.checkRUNBalance(1000n); + await d.checkBLDLiened(8000n); + await d.earnRUNReward(25n); + await d.payoffRUN(1021n); + await d.checkRUNDebt(0n); + await d.checkBLDLiened(0n); +}); + +test('Partial repayment - CR remains the same', async t => { + const d = await makeWorld(t); + await d.buyBLD(10000n); + await d.stakeBLD(10000n); + await d.lienBLD(10000n); + await d.borrowRUN(1000n); + await d.payDownRUN(50n); + await d.checkRUNBalance(950n); + await d.checkRUNDebt(970n); +}); + +test('Partial repayment - CR increases*', async t => { + const d = await makeWorld(t); + await d.buyBLD(10000n); + await d.stakeBLD(10000n); + await d.lienBLD(400n); + await d.borrowRUN(100n); + await d.setMintingRatio([16n, 100n]); + await d.payDownRUN(5n); + await d.checkRUNBalance(95n); + await d.checkBLDLiened(400n); +}); + +test('Partial repay - unbonded ok', async t => { + const d = await makeWorld(t); + await d.buyBLD(1000n); + await d.stakeBLD(800n); + await d.lienBLD(800n); + await d.borrowRUN(100n); + await d.slash(700n); + await d.checkBLDLiened(800n); + await d.checkRUNBalance(100n); + await d.payDownRUN(50n); + await d.checkRUNBalance(50n); + await d.checkBLDLiened(800n); + await d.checkBLDStaked(100n); +}); + +test('Add collateral - more BLD required (FAIL)', async t => { + const d = await makeWorld(t); + await d.buyBLD(1000n); + await d.stakeBLD(1000n); + await d.lienBLD(800n); + await d.borrowRUN(100n); + await t.throwsAsync(async () => d.borrowMoreRUN(200n)); + await d.checkRUNBalance(100n); + await d.checkBLDLiened(800n); +}); + +test('Lower collateral', async t => { + const d = await makeWorld(t); + await d.buyBLD(1000n); + await d.stakeBLD(1000n); + await d.lienBLD(800n); + await d.borrowRUN(100n); + await d.unlienBLD(350n); + await d.checkRUNBalance(100n); + await d.checkBLDLiened(450n); +}); + +test('Lower collateral - CR increase (FAIL)', async t => { + const d = await makeWorld(t); + await d.buyBLD(1000n); + await d.stakeBLD(1000n); + await d.lienBLD(800n); + await d.borrowRUN(100n); + await d.setMintingRatio([16n, 100n]); + await t.throwsAsync(async () => d.unlienBLD(400n)); + await d.checkBLDLiened(800n); +}); + +test('Lower collateral - unbonded ok', async t => { + const d = await makeWorld(t); + await d.buyBLD(1000n); + await d.stakeBLD(1000n); + await d.earnRUNReward(5n); + await d.lienBLD(800n); + await d.borrowRUN(100n); + await d.slash(770n); + await d.checkBLDLiened(800n); + await d.unlienBLD(375n); + await d.checkRUNBalance(105n); + await d.checkBLDLiened(425n); + await d.setMintingRatio([16n, 100n]); + await d.payoffRUN(103n); + await d.checkRUNBalance(3n); + await d.checkBLDLiened(0n); +}); + +test('Lower collateral by paying off DEBT', async t => { + const d = await makeWorld(t); + await d.buyBLD(1000n); + await d.stakeBLD(1000n); + await d.lienBLD(800n); + await d.borrowRUN(190n); + await d.payToUnlien([100n, 300n]); + await d.checkBLDLiened(500n); +}); + +test('Watch interest accrue', async t => { + const d = await makeWorld(t); + await d.buyBLD(1000n); + await d.stakeBLD(1000n); + await d.lienBLD(800n); + await d.borrowRUN(190n); + await d.checkRUNDebt(194n); + await d.waitDays(90n); + await d.checkRUNDebt(195n); +}); + +test('payoff more than you owe', async t => { + const d = await makeWorld(t); + await d.buyBLD(1000n); + await d.stakeBLD(1000n); + await d.lienBLD(800n); + await d.borrowRUN(190n); + await d.checkRUNDebt(194n); + await d.earnRUNReward(20n); + await d.payoffRUN(200n); + await d.checkRUNDebt(0n); + await d.checkBLDLiened(0n); + await d.checkRUNBalance(16n); +}); From b03838e9be7e9a8cc2c4124443efc6732f1f7cb1 Mon Sep 17 00:00:00 2001 From: Dean Tribble Date: Wed, 6 Apr 2022 17:44:28 -0700 Subject: [PATCH 2/2] test(runStake): review cleanup * delete commented-out code --- .../test/runStake/test-runStake.js | 24 ------------------- 1 file changed, 24 deletions(-) diff --git a/packages/run-protocol/test/runStake/test-runStake.js b/packages/run-protocol/test/runStake/test-runStake.js index 59437b85f07..bdadac8efa2 100644 --- a/packages/run-protocol/test/runStake/test-runStake.js +++ b/packages/run-protocol/test/runStake/test-runStake.js @@ -34,7 +34,6 @@ import { KW } from '../../src/runStake/params.js'; // 10 Partial repay - insufficient funds (FAIL) - Zoe prevents this // 12 Add collateral - lots of test harness for little gain // 13 Add collateral - CR increase ok -// import { CASES as TestData } from './runStake-test-steps.js'; const contractRoots = { runStake: '../../src/runStake/runStake.js', @@ -739,29 +738,6 @@ const makeWorld = async t => { return driver; }; -// const makeTests = async () => { -// for await (const [num, name, steps] of TestData) { -// test(`Test Data ${num} ${name}`, async t => { -// // TODO: use different accounts / addresses in the same world. -// const driver = await makeWorld(t); -// for await (const [tag, value, pass] of steps) { -// t.log({ tag, value }); -// const fn = driver[tag]; -// if (!fn) { -// throw Error(`bad tag: ${tag}`); -// } -// if (pass === false) { -// await t.throwsAsync(async () => fn(value, t)); -// } else { -// await fn(value, t); -// } -// } -// }); -// } -// }; - -// makeTests(); - test('borrowing past the debt limit', async t => { const driver = await makeWorld(t);