Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
Fix incorrectly written tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sitetester committed Oct 24, 2023
1 parent becc629 commit faa634e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export class InitializeStateRecoveryCommand extends BaseInteroperabilityCommand<
sidechainAccountBuffer,
);

if (await terminatedStateSubstore.has(context, chainID)) {
if (terminatedStateAccountExists) {
await terminatedStateSubstore.set(context, chainID, {
stateRoot: deserializedSidechainAccount.lastCertificate.stateRoot,
mainchainStateRoot: EMPTY_HASH,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,17 @@ describe('Sidechain InitializeStateRecoveryCommand', () => {
let commandVerifyContext: CommandVerifyContext<StateRecoveryInitParams>;
let stateStore: PrefixedStateReadWriter;
let mainchainAccount: ChainAccount;
let ownChainAccount: OwnChainAccount;

beforeEach(async () => {
stateRecoveryInitCommand = interopMod['_stateRecoveryInitCommand'];

ownChainAccount = {
name: 'sidechain',
chainID: utils.intToBuffer(2, 4),
nonce: BigInt('0'),
};

sidechainChainAccount = {
name: 'sidechain1',
lastCertificate: {
Expand Down Expand Up @@ -174,7 +181,6 @@ describe('Sidechain InitializeStateRecoveryCommand', () => {
});

describe('verify', () => {
let ownChainAccount: OwnChainAccount;
beforeEach(() => {
mainchainAccount = {
name: 'mainchain',
Expand All @@ -186,17 +192,10 @@ describe('Sidechain InitializeStateRecoveryCommand', () => {
},
status: ChainStatus.ACTIVE,
};
ownChainAccount = {
name: 'sidechain',
chainID: utils.intToBuffer(2, 4),
nonce: BigInt('0'),
};

terminatedStateAccountMock.has.mockResolvedValue(true);
ownChainAccountStoreMock.get.mockResolvedValue(ownChainAccount);
chainAccountStoreMock.get.mockResolvedValue(mainchainAccount);
interopStoreMock = {
createTerminatedStateAccount: jest.fn(),
};
commandVerifyContext = transactionContext.createCommandVerifyContext<StateRecoveryInitParams>(
stateRecoveryInitParamsSchema,
);
Expand Down Expand Up @@ -408,6 +407,7 @@ describe('Sidechain InitializeStateRecoveryCommand', () => {
InvalidSMTVerification,
invalidSMTVerificationEvent,
);
ownChainAccountStoreMock.get.mockResolvedValue(ownChainAccount);
});

it('should return error if terminated state account exists and proof of inclusion is not verified', async () => {
Expand Down Expand Up @@ -446,37 +446,60 @@ describe('Sidechain InitializeStateRecoveryCommand', () => {
});

it('should create a terminated state account when there is none', async () => {
// Arrange & Assign & Act
const mainchainID = getMainchainID(commandExecuteContext.chainID);

when(chainAccountStoreMock.get)
.calledWith(expect.anything(), mainchainID)
.mockResolvedValue(mainchainAccount);

jest.spyOn(terminatedStateSubstore, 'has').mockResolvedValue(false);
jest.spyOn(stateRecoveryInitCommand['internalMethod'], 'createTerminatedStateAccount');

await stateRecoveryInitCommand.execute(commandExecuteContext);

const accountFromStore = await terminatedStateSubstore.get(
commandExecuteContext,
transactionParams.chainID,
);

// Assert
expect(accountFromStore).toEqual({ ...terminatedStateAccount, initialized: true });
expect(interopStoreMock.createTerminatedStateAccount).not.toHaveBeenCalled();
expect(
stateRecoveryInitCommand['internalMethod'].createTerminatedStateAccount,
).toHaveBeenCalledTimes(1);
});

it('should update the terminated state account when there is one', async () => {
// Arrange & Assign & Act
when(terminatedStateAccountMock.has)
.calledWith(expect.anything(), transactionParams.chainID)
.mockResolvedValue(false);

const terminatedStateStore = interopMod.stores.get(TerminatedStateStore);
terminatedStateStore.get = terminatedStateAccountMock.get;
terminatedStateAccountMock.get.mockResolvedValue(terminatedStateAccount);

jest.spyOn(stateRecoveryInitCommand['internalMethod'], 'createTerminatedStateAccount');
jest.spyOn(terminatedStateStore, 'get').mockResolvedValue(terminatedStateAccount);
jest.spyOn(terminatedStateStore, 'set');

await stateRecoveryInitCommand.execute(commandExecuteContext);

const deserializedSidechainAccount = codec.decode<ChainAccount>(
chainDataSchema,
commandExecuteContext.params.sidechainAccount,
);
expect(terminatedStateStore.set).toHaveBeenCalledWith(
commandExecuteContext,
commandExecuteContext.params.chainID,
{
stateRoot: deserializedSidechainAccount.lastCertificate.stateRoot,
mainchainStateRoot: EMPTY_HASH,
initialized: true,
},
);

const accountFromStore = await terminatedStateSubstore.get(
commandExecuteContext,
transactionParams.chainID,
);

// Assert
expect(accountFromStore).toEqual(terminatedStateAccount);

expect(
stateRecoveryInitCommand['internalMethod'].createTerminatedStateAccount,
).not.toHaveBeenCalled();
});
});
});

0 comments on commit faa634e

Please sign in to comment.