|
| 1 | +import assert from 'node:assert'; |
| 2 | +import { describe, it } from 'node:test'; |
| 3 | +import { AccountUpdate, Field, Mina, SmartContract, declareState, method } from 'o1js'; |
| 4 | + |
| 5 | +type TestSpec = { |
| 6 | + nAppStateFields: number; |
| 7 | +}; |
| 8 | + |
| 9 | +function specToString(t: TestSpec) { |
| 10 | + return `(fields: ${t.nAppStateFields})`; |
| 11 | +} |
| 12 | + |
| 13 | +async function runTest(t: TestSpec, feePayer: Mina.TestPublicKey) { |
| 14 | + class Contract extends SmartContract { |
| 15 | + @method async noop() {} |
| 16 | + } |
| 17 | + |
| 18 | + const entries = []; |
| 19 | + for (let idx = 0; idx < t.nAppStateFields; idx++) { |
| 20 | + entries.push([`field${idx}`, Field]); |
| 21 | + } |
| 22 | + const fields = Object.fromEntries(entries); |
| 23 | + declareState(Contract, fields); |
| 24 | + |
| 25 | + const contractAccount = Mina.TestPublicKey.random(); |
| 26 | + const contract = new Contract(contractAccount); |
| 27 | + await Contract.compile(); |
| 28 | + { |
| 29 | + const tx = await Mina.transaction(feePayer, async () => { |
| 30 | + AccountUpdate.fundNewAccount(feePayer); |
| 31 | + await contract.deploy(); |
| 32 | + }); |
| 33 | + await tx.sign([feePayer.key, contractAccount.key]).send(); |
| 34 | + } |
| 35 | + |
| 36 | + return contract; |
| 37 | +} |
| 38 | + |
| 39 | +await describe('app state updates', async () => { |
| 40 | + let Local = await Mina.LocalBlockchain({ proofsEnabled: true }); |
| 41 | + Mina.setActiveInstance(Local); |
| 42 | + const [feePayer] = Local.testAccounts; |
| 43 | + |
| 44 | + const tests: TestSpec[] = [ |
| 45 | + { |
| 46 | + nAppStateFields: 1, |
| 47 | + }, |
| 48 | + { |
| 49 | + nAppStateFields: 8, |
| 50 | + }, |
| 51 | + { |
| 52 | + nAppStateFields: 32, |
| 53 | + }, |
| 54 | + ]; |
| 55 | + |
| 56 | + for (const test of tests) { |
| 57 | + await it(`should succeed with spec: ${specToString(test)}`, async () => { |
| 58 | + await assert.doesNotReject(async () => { |
| 59 | + await runTest(test, feePayer); |
| 60 | + }, 'the contract should deploy properly'); |
| 61 | + }); |
| 62 | + } |
| 63 | + |
| 64 | + const rejects: TestSpec[] = [ |
| 65 | + { |
| 66 | + nAppStateFields: 33, |
| 67 | + }, |
| 68 | + ]; |
| 69 | + for (const test of rejects) { |
| 70 | + await it(`should reject with spec: ${specToString(test)}`, async () => { |
| 71 | + await assert.rejects(async () => { |
| 72 | + await runTest(test, feePayer); |
| 73 | + }); |
| 74 | + }); |
| 75 | + } |
| 76 | +}); |
0 commit comments