Skip to content

Commit

Permalink
chore(avm-simulator): test cleanup using expect.objectContaining() (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
dbanks12 authored Mar 4, 2024
1 parent c22b8c6 commit c4ecfdd
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 26 deletions.
36 changes: 18 additions & 18 deletions yarn-project/simulator/src/avm/journal/journal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ describe('journal', () => {
expect(exists).toEqual(false);

const journalUpdates = journal.flush();
expect(journalUpdates.nullifierChecks.map(c => [c.nullifier, c.exists])).toEqual([[utxo, false]]);
expect(journalUpdates.nullifierChecks).toEqual([expect.objectContaining({ nullifier: utxo, exists: false })]);
});
it('checkNullifierExists works for existing nullifiers', async () => {
const contractAddress = new Fr(1);
Expand All @@ -82,7 +82,7 @@ describe('journal', () => {
expect(exists).toEqual(true);

const journalUpdates = journal.flush();
expect(journalUpdates.nullifierChecks.map(c => [c.nullifier, c.exists])).toEqual([[utxo, true]]);
expect(journalUpdates.nullifierChecks).toEqual([expect.objectContaining({ nullifier: utxo, exists: true })]);
});
it('Should maintain nullifiers', async () => {
const contractAddress = new Fr(1);
Expand All @@ -100,8 +100,8 @@ describe('journal', () => {
expect(exists).toEqual(false);

const journalUpdates = journal.flush();
expect(journalUpdates.l1ToL2MessageChecks.map(c => [c.leafIndex, c.msgHash, c.exists])).toEqual([
[leafIndex, utxo, false],
expect(journalUpdates.l1ToL2MessageChecks).toEqual([
expect.objectContaining({ leafIndex: leafIndex, msgHash: utxo, exists: false }),
]);
});
it('checkL1ToL2MessageExists works for existing nullifiers', async () => {
Expand All @@ -113,8 +113,8 @@ describe('journal', () => {
expect(exists).toEqual(true);

const journalUpdates = journal.flush();
expect(journalUpdates.l1ToL2MessageChecks.map(c => [c.leafIndex, c.msgHash, c.exists])).toEqual([
[leafIndex, utxo, true],
expect(journalUpdates.l1ToL2MessageChecks).toEqual([
expect.objectContaining({ leafIndex: leafIndex, msgHash: utxo, exists: true }),
]);
});
it('Should maintain nullifiers', async () => {
Expand Down Expand Up @@ -200,14 +200,14 @@ describe('journal', () => {
{ recipient, content: commitment },
{ recipient, content: commitmentT1 },
]);
expect(journalUpdates.nullifierChecks.map(c => [c.nullifier, c.exists])).toEqual([
[commitment, true],
[commitmentT1, true],
expect(journalUpdates.nullifierChecks).toEqual([
expect.objectContaining({ nullifier: commitment, exists: true }),
expect.objectContaining({ nullifier: commitmentT1, exists: true }),
]);
expect(journalUpdates.newNullifiers).toEqual([commitment, commitmentT1]);
expect(journalUpdates.l1ToL2MessageChecks.map(c => [c.leafIndex, c.msgHash, c.exists])).toEqual([
[index, commitment, false],
[indexT1, commitmentT1, false],
expect(journalUpdates.l1ToL2MessageChecks).toEqual([
expect.objectContaining({ leafIndex: index, msgHash: commitment, exists: false }),
expect.objectContaining({ leafIndex: indexT1, msgHash: commitmentT1, exists: false }),
]);
});

Expand Down Expand Up @@ -274,14 +274,14 @@ describe('journal', () => {

// Check that the world state _traces_ are merged even on rejection
expect(journalUpdates.newNoteHashes).toEqual([commitment, commitmentT1]);
expect(journalUpdates.nullifierChecks.map(c => [c.nullifier, c.exists])).toEqual([
[commitment, true],
[commitmentT1, true],
expect(journalUpdates.nullifierChecks).toEqual([
expect.objectContaining({ nullifier: commitment, exists: true }),
expect.objectContaining({ nullifier: commitmentT1, exists: true }),
]);
expect(journalUpdates.newNullifiers).toEqual([commitment, commitmentT1]);
expect(journalUpdates.l1ToL2MessageChecks.map(c => [c.leafIndex, c.msgHash, c.exists])).toEqual([
[index, commitment, false],
[indexT1, commitmentT1, false],
expect(journalUpdates.l1ToL2MessageChecks).toEqual([
expect.objectContaining({ leafIndex: index, msgHash: commitment, exists: false }),
expect.objectContaining({ leafIndex: indexT1, msgHash: commitmentT1, exists: false }),
]);

// Check that rejected Accrued Substate is absent
Expand Down
4 changes: 4 additions & 0 deletions yarn-project/simulator/src/avm/journal/trace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,5 +224,9 @@ describe('world state access trace', () => {
exists: c.exists,
})),
).toEqual([expectedMessageCheck, expectedMessageCheckT1]);
expect(trace.l1ToL2MessageChecks).toEqual([
expect.objectContaining({ leafIndex: msgLeafIndex, msgHash: msgHash, exists: msgExists }),
expect.objectContaining({ leafIndex: msgLeafIndexT1, msgHash: msgHashT1, exists: msgExistsT1 }),
]);
});
});
20 changes: 12 additions & 8 deletions yarn-project/simulator/src/avm/opcodes/accrued_substate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,9 @@ describe('Accrued Substate', () => {
expect(exists).toEqual(new Uint8(0));

const journalState = context.persistableState.flush();
expect(journalState.nullifierChecks.length).toEqual(1);
expect(journalState.nullifierChecks[0].exists).toEqual(false);
expect(journalState.nullifierChecks).toEqual([
expect.objectContaining({ nullifier: value.toFr(), exists: false }),
]);
});

it('Should correctly show true when nullifier exists', async () => {
Expand All @@ -214,8 +215,9 @@ describe('Accrued Substate', () => {
expect(exists).toEqual(new Uint8(1));

const journalState = context.persistableState.flush();
expect(journalState.nullifierChecks.length).toEqual(1);
expect(journalState.nullifierChecks[0].exists).toEqual(true);
expect(journalState.nullifierChecks).toEqual([
expect.objectContaining({ nullifier: value.toFr(), exists: true }),
]);
});
});

Expand Down Expand Up @@ -314,8 +316,9 @@ describe('Accrued Substate', () => {
expect(exists).toEqual(new Uint8(0));

const journalState = context.persistableState.flush();
expect(journalState.l1ToL2MessageChecks.length).toEqual(1);
expect(journalState.l1ToL2MessageChecks[0].exists).toEqual(false);
expect(journalState.l1ToL2MessageChecks).toEqual([
expect.objectContaining({ leafIndex: leafIndex.toFr(), msgHash: msgHash.toFr(), exists: false }),
]);
});

it('Should correctly show true when L1ToL2 message exists', async () => {
Expand All @@ -340,8 +343,9 @@ describe('Accrued Substate', () => {
expect(exists).toEqual(new Uint8(1));

const journalState = context.persistableState.flush();
expect(journalState.l1ToL2MessageChecks.length).toEqual(1);
expect(journalState.l1ToL2MessageChecks[0].exists).toEqual(true);
expect(journalState.l1ToL2MessageChecks).toEqual([
expect.objectContaining({ leafIndex: leafIndex.toFr(), msgHash: msgHash.toFr(), exists: true }),
]);
});
});

Expand Down

0 comments on commit c4ecfdd

Please sign in to comment.