Skip to content

Commit

Permalink
add testcode
Browse files Browse the repository at this point in the history
  • Loading branch information
carolk-dev committed Dec 20, 2024
1 parent 1e6de69 commit 16e1379
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 1 deletion.
2 changes: 1 addition & 1 deletion bridge/src/messages/pending-transaction-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class PendingTransactionMessage implements Message {
}

render(): ForceOmit<Partial<ChatPostMessageArguments>, "channel"> {
if (this.transactions) {
if (this.transactions.length !== 0) {
console.log("Pending Transactions : ", this.transactions);
return {
text: `${this.transactions.length} Pending Transactions Found`,
Expand Down
70 changes: 70 additions & 0 deletions bridge/test/messages/pending-transaction-message.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { PendingTransactionMessage } from "../../src/messages/pending-transaction-message";
import { ExchangeHistory } from "../../src/interfaces/exchange-history-store";
import { MultiPlanetary } from "../../src/multi-planetary";
import { TransactionStatus } from "../../src/types/transaction-status";

describe("PendingTransactionMessage", () => {
const mockMultiPlanetary = {
getRequestPlanetName: jest.fn().mockReturnValue("Odin"),
} as unknown as MultiPlanetary;

it("should render pending transactions correctly for BSCereum", () => {
const transactions: ExchangeHistory[] = [
{
network: "BSCereum",
tx_id: "TX-123",
sender: "0xSenderAddress",
recipient: "0xRecipientAddress",
timestamp: new Date().toISOString(),
amount: 100,
status: TransactionStatus.PENDING,
},
];

const message = new PendingTransactionMessage(
transactions,
mockMultiPlanetary
);
const result = message.render() as {
text: string;
attachments: {
author_name: string;
fields: { title: string; value: string }[];
}[];
};

expect(result.text).toBe("1 Pending Transactions Found");
expect(result.attachments).toHaveLength(1);
expect(result.attachments[0].author_name).toBe(
"[BSC] wNCG → NCG pending event"
);
expect(result.attachments[0].fields[0].title).toBe("BSC transaction");
expect(result.attachments[0].fields[1].title).toBe("Planet Name");
expect(result.attachments[0].fields[1].value).toBe("Odin");
expect(result.attachments[0].fields[2].title).toBe("Sender(BSC)");
expect(result.attachments[0].fields[2].value).toBe("0xSenderAddress");
expect(result.attachments[0].fields[3].title).toBe("Recipient(9c)");
expect(result.attachments[0].fields[3].value).toBe("0xRecipientAddress");
expect(result.attachments[0].fields[4].title).toBe("Amount");
expect(result.attachments[0].fields[4].value).toBe("100");
expect(result.attachments[0].fields[5].title).toBe("Timestamp");
expect(result.attachments[0].fields[5].value).toBe(
transactions[0].timestamp
);
});

it("should render no pending transactions message", () => {
const message = new PendingTransactionMessage([], mockMultiPlanetary);
const result = message.render() as {
text: string;
attachments: {
author_name: string;
fields: { title: string; value: string }[];
}[];
};

expect(result.text).toBe("No pending transactions");
expect(result.attachments).toHaveLength(1);
expect(result.attachments[0].author_name).toBe("BSC Bridge Restarted");
});
});
86 changes: 86 additions & 0 deletions bridge/test/pending-transactions.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { PendingTransactionHandler } from "../src/pending-transactions";
import { IExchangeHistoryStore } from "../src/interfaces/exchange-history-store";
import { INCGTransfer } from "../src/interfaces/ncg-transfer";
import { MultiPlanetary } from "../src/multi-planetary";
import { ISlackMessageSender } from "../src/interfaces/slack-message-sender";
import { PendingTransactionMessage } from "../src/messages/pending-transaction-message";
import { TransactionStatus } from "../src/types/transaction-status";

describe("PendingTransactionHandler", () => {
let exchangeHistoryStore: jest.Mocked<IExchangeHistoryStore>;
let ncgTransfer: jest.Mocked<INCGTransfer>;
let multiPlanetary: jest.Mocked<MultiPlanetary>;
let slackMessageSender: jest.Mocked<ISlackMessageSender>;
let handler: PendingTransactionHandler;

beforeEach(() => {
exchangeHistoryStore = {
getPendingTransactions: jest.fn(),
updateStatus: jest.fn(),
} as unknown as jest.Mocked<IExchangeHistoryStore>;

ncgTransfer = {} as jest.Mocked<INCGTransfer>;
multiPlanetary = {} as jest.Mocked<MultiPlanetary>;
slackMessageSender = {
sendMessage: jest.fn(),
} as unknown as jest.Mocked<ISlackMessageSender>;

handler = new PendingTransactionHandler(
exchangeHistoryStore,
ncgTransfer,
multiPlanetary,
slackMessageSender
);
});

it("should send a message for pending transactions and update their status", async () => {
const pendingTransactions = [
{
tx_id: "TX-1",
network: "ethereum",
amount: 100,
sender: "sender1",
recipient: "recipient1",
timestamp: new Date().toISOString(),
status: TransactionStatus.PENDING,
},
{
tx_id: "TX-2",
network: "nineChronicles",
amount: 200,
sender: "sender2",
recipient: "recipient2",
timestamp: new Date().toISOString(),
status: TransactionStatus.PENDING,
},
];

exchangeHistoryStore.getPendingTransactions.mockResolvedValue(
pendingTransactions
);

await handler.messagePendingTransactions();

expect(slackMessageSender.sendMessage).toHaveBeenCalledWith(
expect.any(PendingTransactionMessage)
);
expect(exchangeHistoryStore.updateStatus).toHaveBeenCalledTimes(2);
expect(exchangeHistoryStore.updateStatus).toHaveBeenCalledWith(
"TX-1",
TransactionStatus.FAILED
);
expect(exchangeHistoryStore.updateStatus).toHaveBeenCalledWith(
"TX-2",
TransactionStatus.FAILED
);
});

it("should not send a message if there are no pending transactions", async () => {
exchangeHistoryStore.getPendingTransactions.mockResolvedValue([]);

await handler.messagePendingTransactions();

expect(slackMessageSender.sendMessage).not.toHaveBeenCalled();
expect(exchangeHistoryStore.updateStatus).not.toHaveBeenCalled();
});
});

0 comments on commit 16e1379

Please sign in to comment.