Skip to content

Commit

Permalink
fix(test): unit tests for poller
Browse files Browse the repository at this point in the history
  • Loading branch information
liu-zhipeng committed May 24, 2023
1 parent 28eb9ec commit c222238
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 1 deletion.
39 changes: 39 additions & 0 deletions packages/adapters/subgraph/test/mock.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
AggregatedRoot,
Asset,
chainDataToMap,
DestinationTransfer,
mkAddress,
Expand All @@ -12,6 +13,7 @@ import {
RelayerFeesIncrease,
RootMessage,
RouterBalance,
RouterDailyTVL,
SlippageUpdate,
StableSwapExchange,
StableSwapLpBalance,
Expand Down Expand Up @@ -255,6 +257,31 @@ export const mockRouterResponse: RouterBalance[] = [
},
];

export const mockAssetsResponse: Asset[] = [
{
id: mkAddress("0x1"),
key: mkBytes32("0xa"),
decimal: "18",
adoptedAsset: mkAddress("0x2"),
canonicalId: mkBytes32("0xa"),
canonicalDomain: "1337",
localAsset: mkAddress("0x1"),
blockNumber: "23423423",
domain: "1337",
},
{
id: mkAddress("0x1"),
key: mkBytes32("0xa"),
decimal: "18",
adoptedAsset: mkAddress("0x2"),
canonicalId: mkBytes32("0xa"),
canonicalDomain: "1337",
localAsset: mkAddress("0x1"),
blockNumber: "23423423",
domain: "1338",
},
];

export const mockConnectorMeta = [
{
hubDomain: "1337",
Expand Down Expand Up @@ -394,6 +421,17 @@ export const mockSlippageUpdateResponse: SlippageUpdate[] = [
},
];

export const mockRouterDailyTVLResponse: RouterDailyTVL[] = [
{
id: `${mkBytes32("0xa")}-${mkBytes32("0xb")}-0`,
asset: mkAddress("0xa"),
router: mkAddress("0xb"),
domain: "1337",
timestamp: 1673421076,
balance: "23123123",
},
];

export const mockSubgraph = () =>
createStubInstance(SubgraphReader, {
getOriginMessagesByDomain: Promise.resolve(mockOriginMessageSubgraphResponse),
Expand All @@ -420,4 +458,5 @@ export const mockSubgraph = () =>
getStableSwapPoolEventsByDomainAndNonce: Promise.resolve(mockStableSwapAddLiquidityResponse),
getRelayerFeesIncreasesByDomainAndTimestamp: Promise.resolve(mockRelayerFeesIncreaseResponse),
getSlippageUpdatesByDomainAndTimestamp: Promise.resolve(mockSlippageUpdateResponse),
getRouterDailyTVLByDomainAndTimestamp: Promise.resolve(mockRouterDailyTVLResponse),
});
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { SinonStub } from "sinon";
import { expect } from "@connext/nxtp-utils";
import { mockRouterResponse } from "@connext/nxtp-adapters-subgraph/test/mock";
import {
mockAssetsResponse,
mockRouterDailyTVLResponse,
mockRouterResponse,
} from "@connext/nxtp-adapters-subgraph/test/mock";

import { mockContext } from "../../globalTestHook";
import { updateRouters } from "../../../src/lib/operations";
import { updateAssets, updateDailyRouterTvl } from "../../../src/lib/operations/routers";

describe("Routers operations", () => {
describe("#updateRouters", () => {
Expand Down Expand Up @@ -57,4 +62,51 @@ describe("Routers operations", () => {
);
});
});

describe("#updateAssets", () => {
it("should work", async () => {
(mockContext.adapters.subgraph.getAssets as SinonStub)
.withArgs(mockContext.domains[0])
.resolves([mockAssetsResponse[0]]);
(mockContext.adapters.subgraph.getAssets as SinonStub)
.withArgs(mockContext.domains[1])
.resolves([mockAssetsResponse[1]]);

await updateAssets();

expect(mockContext.adapters.database.saveAssets as SinonStub).callCount(1);
expect(mockContext.adapters.database.saveAssets as SinonStub).to.be.calledWithExactly(mockAssetsResponse);
});
});

describe("#updateDailyRouterTvl", () => {
it("should work", async () => {
(mockContext.adapters.subgraph.getLatestBlockNumber as SinonStub).resolves(
new Map([
["1337", 1],
["1338", 1],
]),
);
await updateDailyRouterTvl();

expect(mockContext.adapters.database.getCheckPoint as SinonStub).callCount(mockContext.domains.length);
expect(mockContext.adapters.database.getCheckPoint as SinonStub).to.be.calledWithExactly(
"daily_router_tvl_timestamp_" + mockContext.domains[0],
);
expect(mockContext.adapters.database.getCheckPoint as SinonStub).to.be.calledWithExactly(
"daily_router_tvl_timestamp_" + mockContext.domains[1],
);

expect(mockContext.adapters.database.saveRouterDailyTVL as SinonStub).callCount(1);
expect(mockContext.adapters.database.saveRouterDailyTVL as SinonStub).to.be.calledWithExactly(
mockRouterDailyTVLResponse,
);

expect(mockContext.adapters.database.saveCheckPoint as SinonStub).callCount(1);
expect(mockContext.adapters.database.saveCheckPoint as SinonStub).to.be.calledWithExactly(
"daily_router_tvl_timestamp_" + mockContext.domains[0],
mockRouterDailyTVLResponse[0].timestamp,
);
});
});
});

0 comments on commit c222238

Please sign in to comment.