Skip to content

Commit

Permalink
fix: propose unit tests draft
Browse files Browse the repository at this point in the history
  • Loading branch information
liu-zhipeng committed Oct 10, 2023
1 parent 0dd0236 commit a718acf
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/adapters/database/test/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,6 @@ export const mockDatabase = (): Database => {
saveFinalizedRoots: stub().resolves(),
savePropagatedOptimisticRoots: stub().resolves(),
saveSnapshotRoots: stub().resolves(),
getPendingSnapshots: stub().resolves([mock.entity.snapshotRoot()]),
};
};
6 changes: 6 additions & 0 deletions packages/agents/lighthouse/.nycrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@
"src/tasks/sendOutboundRoot/sendOutboundRoot.ts",
"src/tasks/sendOutboundRoot/operations/index.ts",
"src/tasks/sendOutboundRoot/helpers/index.ts",
"src/tasks/propose/errors.ts",
"src/tasks/propose/context.ts",
"src/tasks/propose/index.ts",
"src/tasks/propose/propose.ts",
"src/tasks/propose/operations/index.ts",
"src/tasks/propose/adapters/index.ts",
"test/**/*.ts",
"./globalTestHook.ts"
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export const propose = async () => {
orderedSnapshotRoots.push(...snapshotRoots!.filter((s) => s.spokeDomain === Number(domain)));
});

proposeSnapshot(
await proposeSnapshot(
latestSnapshotId,
orderedSnapshotRoots.map((s) => s.root),
requestContext,
Expand Down
6 changes: 6 additions & 0 deletions packages/agents/lighthouse/test/globalTestHook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import { Relayer } from "@connext/nxtp-adapters-relayer";

import { ProverContext } from "../src/tasks/prover/context";
import { ProcessFromRootContext } from "../src/tasks/processFromRoot/context";
import { ProposeContext } from "../src/tasks/propose/context";
import { mock, mockTaskId } from "./mock";
import * as ProverFns from "../src/tasks/prover/prover";
import * as ProcessFromRootFns from "../src/tasks/processFromRoot/processFromRoot";
import * as PropagateFns from "../src/tasks/propagate/propagate";
import * as SendOutboundRootFns from "../src/tasks/sendOutboundRoot/sendOutboundRoot";
import * as ProposeFns from "../src/tasks/propose/propose";
import * as Mockable from "../src/mockable";
import { PropagateContext } from "../src/tasks/propagate/context";
import { SendOutboundRootContext } from "../src/tasks/sendOutboundRoot/context";
Expand All @@ -20,6 +22,7 @@ export let proverCtxMock: ProverContext;
export let processFromRootCtxMock: ProcessFromRootContext;
export let propagateCtxMock: PropagateContext;
export let sendOutboundRootCtxMock: SendOutboundRootContext;
export let proposeCtxMock: ProposeContext;

export let chainReaderMock: SinonStubbedInstance<ChainReader>;
export let existsSyncStub: SinonStub;
Expand Down Expand Up @@ -76,6 +79,9 @@ export const mochaHooks = {
} as any);

getBestProviderMock = stub(Mockable, "getBestProvider").resolves("http://example.com");

proposeCtxMock = mock.proposeCtx();
stub(ProposeFns, "getContext").returns(proposeCtxMock);
},
afterEach() {
restore();
Expand Down
20 changes: 20 additions & 0 deletions packages/agents/lighthouse/test/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { ProcessFromRootContext } from "../src/tasks/processFromRoot/context";
import { PropagateContext } from "../src/tasks/propagate/context";
import { mockSubgraph } from "@connext/nxtp-adapters-subgraph/test/mock";
import { SendOutboundRootContext } from "../src/tasks/sendOutboundRoot/context";
import { ProposeContext } from "../src/tasks/propose/context";

export const mockTaskId = mkBytes32("0xabcdef123");
export const mockRelayerAddress = mkAddress("0xabcdef123");
Expand Down Expand Up @@ -93,6 +94,7 @@ export const mock = {
contracts: mock.adapters.contracts(),
relayers: mock.adapters.relayers(),
database: mock.adapters.database(),
subgraph: mock.adapters.subgraph(),
databaseWriter: { database: mock.adapters.database(), pool: mockDatabasePool() },
cache: mockCache() as any,
mqClient: mockMqClient() as any,
Expand All @@ -109,6 +111,7 @@ export const mock = {
contracts: mock.adapters.deployments(),
relayers: mock.adapters.relayers(),
database: mock.adapters.database(),
subgraph: mock.adapters.subgraph(),
},
config: mock.config(),
chainData: mock.chainData(),
Expand All @@ -122,6 +125,7 @@ export const mock = {
deployments: mock.adapters.deployments(),
contracts: mock.adapters.contracts(),
relayers: mock.adapters.relayers(),
database: mock.adapters.database(),
subgraph: mock.adapters.subgraph(),
ambs: mock.adapters.ambs(),
},
Expand All @@ -144,6 +148,22 @@ export const mock = {
chainData: mock.chainData(),
};
},
proposeCtx: (): ProposeContext => {
return {
logger: new Logger({ name: "mock", level: process.env.LOG_LEVEL || "silent" }),
adapters: {
chainreader: mock.adapters.chainreader() as unknown as ChainReader,
deployments: mock.adapters.deployments(),
contracts: mock.adapters.contracts(),
relayers: mock.adapters.relayers(),
database: mock.adapters.database(),
subgraph: mock.adapters.subgraph(),
ambs: mock.adapters.ambs(),
},
config: mock.config(),
chainData: mock.chainData(),
};
},
config: (): NxtpLighthouseConfig => ({
chains: {
[mock.domain.A]: {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { expect, getNtpTimeSeconds, getRandomBytes32 } from "@connext/nxtp-utils";
import { SinonStub, stub } from "sinon";

import { NoChainIdForHubDomain } from "../../../../src/tasks/propose/errors";
import { propose } from "../../../../src/tasks/propose/operations";
import * as ProposeFns from "../../../../src/tasks/propose/operations/propose";
import { proposeCtxMock, sendWithRelayerWithBackupStub } from "../../../globalTestHook";
import { mock } from "../../../mock";
import * as Mockable from "../../../../src/mockable";

describe("Operations: Propose", () => {
describe("#propose", () => {
beforeEach(() => {});

it("should throw an error if no hub domain id", async () => {
proposeCtxMock.chainData = new Map();
await expect(propose()).to.eventually.be.rejectedWith(NoChainIdForHubDomain);
});

it("should call propose snapshot succesfully", async () => {
let proposeSnapshotStub = stub(ProposeFns, "proposeSnapshot").resolves();

(proposeCtxMock.adapters.database.getPendingSnapshots as SinonStub).resolves([mock.entity.snapshotRoot()]);

await propose();
expect(proposeSnapshotStub).callCount(1);
});
});
});

0 comments on commit a718acf

Please sign in to comment.