Skip to content

Commit

Permalink
test(vaultFactory): refactor to macro
Browse files Browse the repository at this point in the history
  • Loading branch information
turadg committed Aug 10, 2022
1 parent 2e22c39 commit 370b80c
Showing 1 changed file with 42 additions and 34 deletions.
76 changes: 42 additions & 34 deletions packages/inter-protocol/test/vaultFactory/test-storeUtils.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,58 @@
// @ts-check
// Must be first to set up globals
import { test } from '@agoric/zoe/tools/prepare-test-env-ava.js';
import '@endo/init/debug.js';
// Consider ses-ava once https://github.com/endojs/endo/issues/1235 is resolved
import test from 'ava';

import { AmountMath } from '@agoric/ertp';
import { Far } from '@endo/marshal';
import * as StoreUtils from '../../src/vaultFactory/storeUtils.js';

export const mockBrand = Far('brand');

for (const [debt, collat, vaultId, expectedKey, numberOut] of [
[0, 100, 'vault-A', 'fc399000000000000:vault-A', 450359962737049600], // Infinity collateralized but we treat 0 as epsilon for safer serialization
[1, 100, 'vault-B', 'fc059000000000000:vault-B', 100.0],
[1000, 100, 'vault-C', 'fbfb999999999999a:vault-C', 0.1],
[1000, 101, 'vault-D', 'fbfb9db22d0e56042:vault-D', 0.101],
[
100,
Number.MAX_SAFE_INTEGER,
'vault-MAX-COLLATERAL',
'fc2d47ae147ae147a:vault-MAX-COLLATERAL',
90071992547409.9,
],
[
Number.MAX_SAFE_INTEGER,
100,
'vault-MAX-DEBT',
'fbd09000000000001:vault-MAX-DEBT',
1.1102230246251567e-14,
],
[
Number.MAX_SAFE_INTEGER,
Number.MAX_SAFE_INTEGER,
'vault-MAX-EVEN',
'fbff0000000000000:vault-MAX-EVEN',
1,
],
[1, 0, 'vault-NOCOLLATERAL', 'f8000000000000000:vault-NOCOLLATERAL', 0],
]) {
test(`vault keys: (${debt}/${collat}, ${vaultId}) => ${expectedKey} ==> ${numberOut}, ${vaultId}`, t => {
const keysMatch = test.macro({
exec(t, [debt, collat, vaultId], [expectedKey, numberOut]) {
const key = StoreUtils.toVaultKey(
// @ts-expect-error cast to NormalizedDebt
AmountMath.make(mockBrand, BigInt(debt)),
AmountMath.make(mockBrand, BigInt(collat)),
String(vaultId),
);
// @ts-expect-error TODO use macros
t.is(key, expectedKey);
t.deepEqual(StoreUtils.fromVaultKey(key), [numberOut, vaultId]);
});
}
},
title(providedTitle = '', [debt, collat, vaultId], [expectedKey, numberOut]) {
return `${providedTitle} (${debt}/${collat}, ${vaultId}) => ${expectedKey} ==> ${numberOut}, ${vaultId})`;
},
});

test(
'Infinity collateralized',
keysMatch,
[0, 100, 'vault-A'],
// treat 0 as epsilon for safer serialization
['fc399000000000000:vault-A', 450359962737049600],
);
test(keysMatch, [1, 100, 'vault-B'], ['fc059000000000000:vault-B', 100.0]);
test(keysMatch, [1000, 100, 'vault-C'], ['fbfb999999999999a:vault-C', 0.1]);
test(keysMatch, [1000, 101, 'vault-D'], ['fbfb9db22d0e56042:vault-D', 0.101]);

test(
keysMatch,
[100, Number.MAX_SAFE_INTEGER, 'vault-MAX-COLLATERAL'],
['fc2d47ae147ae147a:vault-MAX-COLLATERAL', 90071992547409.9],
);
test(
keysMatch,
[Number.MAX_SAFE_INTEGER, 100, 'vault-MAX-DEBT'],
['fbd09000000000001:vault-MAX-DEBT', 1.1102230246251567e-14],
);
test(
keysMatch,
[Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER, 'vault-MAX-EVEN'],
['fbff0000000000000:vault-MAX-EVEN', 1],
);
test(
keysMatch,
[1, 0, 'vault-NOCOLLATERAL'],
['f8000000000000000:vault-NOCOLLATERAL', 0],
);

0 comments on commit 370b80c

Please sign in to comment.