|
| 1 | +import { Worker } from 'near-workspaces'; |
| 2 | +import {readFile} from 'fs/promises' |
| 3 | +import test from 'ava'; |
| 4 | + |
| 5 | +// TODO: make this function part of the npm package when it is available |
| 6 | +function encodeCall(contract, method, args) { |
| 7 | + return Buffer.concat([Buffer.from(contract), Buffer.from([0]), Buffer.from(method), Buffer.from([0]), Buffer.from(JSON.stringify(args))]) |
| 8 | +} |
| 9 | + |
| 10 | +test.beforeEach(async t => { |
| 11 | + // Init the worker and start a Sandbox server |
| 12 | + const worker = await Worker.init(); |
| 13 | + |
| 14 | + // Prepare sandbox for tests, create accounts, deploy contracts, etx. |
| 15 | + const root = worker.rootAccount; |
| 16 | + |
| 17 | + // Deploy the jsvm contract. |
| 18 | + const jsvm = await root.createAndDeploy( |
| 19 | + root.getSubAccount('jsvm').accountId, |
| 20 | + 'build/jsvm.wasm', |
| 21 | + ); |
| 22 | + |
| 23 | + // Deploy status-message JS contract |
| 24 | + const statusMessageContract = await root.createSubAccount('status-message'); |
| 25 | + let statusContractBase64 = (await readFile('res/status-message.base64')).toString(); |
| 26 | + await statusMessageContract.call(jsvm, 'deploy_js_contract', Buffer.from(statusContractBase64, 'base64'), {attachedDeposit: '400000000000000000000000'}); |
| 27 | + await statusMessageContract.call(jsvm, 'call_js_contract', encodeCall(statusMessageContract.accountId, 'init', []), {attachedDeposit: '400000000000000000000000'}); |
| 28 | + |
| 29 | + // Deploy on-call contrat |
| 30 | + const onCallContract = await root.createSubAccount('on-call'); |
| 31 | + let cross_cc_contract_base64 = (await readFile('build/on-call.base64')).toString(); |
| 32 | + await onCallContract.call(jsvm, 'deploy_js_contract', Buffer.from(cross_cc_contract_base64, 'base64'), {attachedDeposit: '400000000000000000000000'}); |
| 33 | + await onCallContract.call(jsvm, 'call_js_contract', encodeCall(onCallContract.accountId, 'init', []), {attachedDeposit: '400000000000000000000000'}); |
| 34 | + |
| 35 | + // Create test accounts |
| 36 | + const ali = await root.createSubAccount('ali'); |
| 37 | + const bob = await root.createSubAccount('bob'); |
| 38 | + |
| 39 | + // Save state for test runs, it is unique for each test |
| 40 | + t.context.worker = worker; |
| 41 | + t.context.accounts = { |
| 42 | + root, |
| 43 | + jsvm, |
| 44 | + statusMessageContract, |
| 45 | + onCallContract, |
| 46 | + ali, |
| 47 | + bob, |
| 48 | + }; |
| 49 | +}); |
| 50 | + |
| 51 | +test.afterEach(async t => { |
| 52 | + await t.context.worker.tearDown().catch(error => { |
| 53 | + console.log('Failed tear down the worker:', error); |
| 54 | + }); |
| 55 | +}); |
| 56 | + |
| 57 | +test('Nobody is on-call in the beginning', async t => { |
| 58 | + const { jsvm, onCallContract } = t.context.accounts; |
| 59 | + const result = await jsvm.view('view_js_contract', encodeCall(onCallContract.accountId, 'person_on_call', [])); |
| 60 | + t.is(result, 'undefined'); |
| 61 | +}); |
| 62 | + |
| 63 | +test('Person can be set on-call if AVAILABLE', async t => { |
| 64 | + const { ali, bob, jsvm, statusMessageContract, onCallContract } = t.context.accounts; |
| 65 | + |
| 66 | + // Ali set her status as AVAILABLE |
| 67 | + await ali.call(jsvm, 'call_js_contract', encodeCall(statusMessageContract.accountId, 'set_status', ['AVAILABLE']), {attachedDeposit: '100000000000000000000000'}); |
| 68 | + // Bob sets Ali on-call |
| 69 | + await bob.call(jsvm, 'call_js_contract', encodeCall(onCallContract.accountId, 'set_person_on_call', [ali.accountId]), {attachedDeposit: '100000000000000000000000'}); |
| 70 | + |
| 71 | + // Check that Ali is on-call |
| 72 | + t.is( |
| 73 | + await jsvm.view('view_js_contract', encodeCall(onCallContract.accountId, 'person_on_call', [])), |
| 74 | + ali.accountId |
| 75 | + ); |
| 76 | +}); |
| 77 | + |
| 78 | +test('Person can NOT be set on-call if UNAVAILABLE', async t => { |
| 79 | + const { ali, bob, jsvm, statusMessageContract, onCallContract } = t.context.accounts; |
| 80 | + |
| 81 | + // Ali set her status as AVAILABLE |
| 82 | + await ali.call(jsvm, 'call_js_contract', encodeCall(statusMessageContract.accountId, 'set_status', ['UNAVAILABLE']), {attachedDeposit: '100000000000000000000000'}); |
| 83 | + // Bob tries to sets Ali on-call |
| 84 | + await bob.call(jsvm, 'call_js_contract', encodeCall(onCallContract.accountId, 'set_person_on_call', [ali.accountId]), {attachedDeposit: '100000000000000000000000'}); |
| 85 | + |
| 86 | + // Check that Ali is NOT on-call |
| 87 | + t.not( |
| 88 | + await jsvm.view('view_js_contract', encodeCall(onCallContract.accountId, 'person_on_call', [])), |
| 89 | + ali.accountId |
| 90 | + ); |
| 91 | +}); |
0 commit comments