Skip to content

Commit

Permalink
fix: add lh unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
preethamr committed Oct 16, 2023
1 parent 8727df9 commit 84f0a52
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
2 changes: 2 additions & 0 deletions packages/agents/lighthouse/test/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
RootMessage,
RelayerType,
ReceivedAggregateRoot,
ModeType,
} from "@connext/nxtp-utils";
import { Relayer } from "@connext/nxtp-adapters-relayer";
import { mockRelayer } from "@connext/nxtp-adapters-relayer/test/mock";
Expand Down Expand Up @@ -102,6 +103,7 @@ export const mock = {
},
config: mock.config(),
chainData: mock.chainData(),
mode: ModeType.SlowMode,
};
},
processFromRootCtx: (): ProcessFromRootContext => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const mockBrokerMesage: BrokerMessage = {
messageRootCount: 2,
aggregateRoot: mkBytes32("0x222"),
aggregateRootCount: 2,
snapshotRoots: [],
};

const requestContext = createRequestContext("Publisher");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { expect, createRequestContext, mkBytes32 } from "@connext/nxtp-utils";
import { expect, createRequestContext, mkBytes32, ModeType } from "@connext/nxtp-utils";
import { SinonStub, stub } from "sinon";

import {
enqueue,
prefetch,
createBrokerMessage,
getUnProcessedMessagesByIndex,
acquireLock,
releaseLock,
} from "../../../../src/tasks/prover/operations/publisher";
import * as PublisherFns from "../../../../src/tasks/prover/operations/publisher";
import { mock, mockXMessage1, mockXMessage2, mockRootMessage, mockReceivedRoot } from "../../../mock";
Expand All @@ -22,6 +24,7 @@ const mockBrokerMesage: BrokerMessage = {
messageRootCount: 2,
aggregateRoot: mkBytes32("0x222"),
aggregateRootCount: 2,
snapshotRoots: [],
};

const requestContext = createRequestContext("Publisher");
Expand Down Expand Up @@ -70,6 +73,44 @@ describe("Operations: Publisher", () => {
expect(createBrokerMessageStub.callCount).to.be.eq(4);
});

it("happy case should enqueue a broker messages in op mode", async () => {
acquireLock();
const channel = await proverCtxMock.adapters.mqClient.createChannel();
const publishStub = (channel.publish as SinonStub).resolves();
proverCtxMock.mode = ModeType.OptimisticMode;

(proverCtxMock.adapters.database.getLatestAggregateRoots as SinonStub).resolves([mockReceivedRoot]);
(proverCtxMock.adapters.database.getLatestMessageRoot as SinonStub).resolves(mockRootMessage);
(proverCtxMock.adapters.database.getPendingAggregateRoot as SinonStub).resolves(mock.entity.snapshot());
(proverCtxMock.adapters.database.getRootMessage as SinonStub).resolves(mockRootMessage);
(proverCtxMock.adapters.database.getMessageRootCount as SinonStub).resolves(1);
(proverCtxMock.adapters.database.getMessageRootIndex as SinonStub).resolves(1);
(proverCtxMock.adapters.database.getAggregateRootCount as SinonStub).resolves(1);
(proverCtxMock.adapters.cache.messages.setStatus as SinonStub).resolves();
createBrokerMessageStub.resolves(mockBrokerMesage);
await enqueue();
releaseLock();
expect(createBrokerMessageStub.callCount).to.be.eq(4);
});

it("should not enqueue if no snapshot root in op mode", async () => {
const channel = await proverCtxMock.adapters.mqClient.createChannel();
const publishStub = (channel.publish as SinonStub).resolves();
proverCtxMock.mode = ModeType.OptimisticMode;

(proverCtxMock.adapters.database.getLatestAggregateRoots as SinonStub).resolves([mockReceivedRoot]);
(proverCtxMock.adapters.database.getLatestMessageRoot as SinonStub).resolves(mockRootMessage);
(proverCtxMock.adapters.database.getPendingAggregateRoot as SinonStub).resolves(undefined);
(proverCtxMock.adapters.database.getRootMessage as SinonStub).resolves(mockRootMessage);
(proverCtxMock.adapters.database.getMessageRootCount as SinonStub).resolves(1);
(proverCtxMock.adapters.database.getMessageRootIndex as SinonStub).resolves(1);
(proverCtxMock.adapters.database.getAggregateRootCount as SinonStub).resolves(1);
(proverCtxMock.adapters.cache.messages.setStatus as SinonStub).resolves();
createBrokerMessageStub.resolves(mockBrokerMesage);
await enqueue();
expect(createBrokerMessageStub.callCount).to.be.eq(0);
});

it("should catch error if no pending aggregate root", async () => {
(proverCtxMock.adapters.database.getLatestAggregateRoots as SinonStub).resolves([mockReceivedRoot]);
(proverCtxMock.adapters.database.getLatestMessageRoot as SinonStub).resolves(mockRootMessage);
Expand All @@ -80,7 +121,7 @@ describe("Operations: Publisher", () => {
(proverCtxMock.adapters.cache.messages.setNonce as SinonStub).resolves();
createBrokerMessageStub.resolves(mockBrokerMesage);
expect(await enqueue()).to.throw;
expect(createBrokerMessageStub.callCount).to.be.eq(0);
expect(createBrokerMessageStub.callCount).to.be.eq(4);
});
it("should catch error if no received aggregate root", async () => {
(proverCtxMock.adapters.database.getLatestAggregateRoots as SinonStub).resolves([]);
Expand Down Expand Up @@ -142,6 +183,7 @@ describe("Operations: Publisher", () => {
mockBrokerMesage.messageRootCount,
mockBrokerMesage.aggregateRoot,
mockBrokerMesage.aggregateRootCount,
mockBrokerMesage.snapshotRoots,
requestContext,
);
expect(brokerMessage).to.be.deep.eq(mockBrokerMesage);
Expand All @@ -157,6 +199,7 @@ describe("Operations: Publisher", () => {
mockBrokerMesage.messageRootCount,
mockBrokerMesage.aggregateRoot,
mockBrokerMesage.aggregateRootCount,
mockBrokerMesage.snapshotRoots,
requestContext,
),
).to.eventually.be.rejectedWith(NoDestinationDomainForProof);
Expand All @@ -175,6 +218,7 @@ describe("Operations: Publisher", () => {
mockBrokerMesage.messageRootCount,
mockBrokerMesage.aggregateRoot,
mockBrokerMesage.aggregateRootCount,
mockBrokerMesage.snapshotRoots,
requestContext,
);
expect(brokerMessage).to.be.undefined;
Expand Down

0 comments on commit 84f0a52

Please sign in to comment.