Skip to content

Commit

Permalink
test chat messages by configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
woodser committed Apr 30, 2024
1 parent 897d43d commit 7046924
Showing 1 changed file with 65 additions and 59 deletions.
124 changes: 65 additions & 59 deletions src/HavenoClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ const defaultTradeConfig: Partial<TradeContext> = {
disputeSummary: "Seller is winner",
walletSyncPeriodMs: 5000,
maxTimePeerNoticeMs: 5000,
testChatMessages: true,
stopOnFailure: true,
testPayoutConfirmed: true,
testPayoutUnlocked: false,
Expand Down Expand Up @@ -218,6 +219,7 @@ class TradeContext {
sellerOpenedDispute?: boolean;
walletSyncPeriodMs: number;
maxTimePeerNoticeMs: number;
testChatMessages: boolean;
stopOnFailure?: boolean;
buyerAppName?: string;
sellerAppName?: string;
Expand Down Expand Up @@ -2924,65 +2926,69 @@ async function testOpenDispute(ctxP: Partial<TradeContext>) {
await ctx.getDisputePeer()!.havenod!.addNotificationListener(notification => { HavenoUtils.log(3, "Dispute peer received notification " + notification.getType() + " " + (notification.getChatMessage() ? notification.getChatMessage()?.getMessage() : "")); disputePeerNotifications.push(notification); });
await arbitrator.addNotificationListener(notification => { HavenoUtils.log(3, "Arbitrator received notification " + notification.getType() + " " + (notification.getChatMessage() ? notification.getChatMessage()?.getMessage() : "")); arbitratorNotifications.push(notification); });

// arbitrator sends chat messages to traders
HavenoUtils.log(1, "Arbitrator sending chat messages to traders. tradeId=" + ctx.offerId + ", disputeId=" + openerDispute.getId());
await ctx.arbitrator.havenod!.sendDisputeChatMessage(arbDisputeOpener!.getId(), "Arbitrator chat message to dispute opener", []);
await ctx.arbitrator.havenod!.sendDisputeChatMessage(arbDisputePeer!.getId(), "Arbitrator chat message to dispute peer", []);

// traders reply to arbitrator chat messages
await wait(ctx.maxTimePeerNoticeMs); // wait for arbitrator's message to arrive
const attachment = new Attachment();
const bytes = new Uint8Array(Buffer.from("Proof dispute opener was scammed", "utf8"));
attachment.setBytes(bytes);
attachment.setFileName("proof.txt");
const attachment2 = new Attachment();
const bytes2 = new Uint8Array(Buffer.from("picture bytes", "utf8"));
attachment2.setBytes(bytes2);
attachment2.setFileName("proof.png");
HavenoUtils.log(2, "Dispute opener sending chat message to arbitrator. tradeId=" + ctx.offerId + ", disputeId=" + openerDispute.getId());
await ctx.getDisputeOpener()!.havenod!.sendDisputeChatMessage(openerDispute.getId(), "Dispute opener chat message", [attachment, attachment2]);
await wait(ctx.maxTimePeerNoticeMs); // wait for user2's message to arrive
HavenoUtils.log(2, "Dispute peer sending chat message to arbitrator. tradeId=" + ctx.offerId + ", disputeId=" + peerDispute.getId());
await ctx.getDisputePeer()!.havenod!.sendDisputeChatMessage(peerDispute.getId(), "Dispute peer chat message", []);

// test trader chat messages
await wait(ctx.maxTimePeerNoticeMs);
let dispute = await ctx.getDisputeOpener()!.havenod!.getDispute(ctx.offerId!);
let messages = dispute.getChatMessageList();
expect(messages.length).toBeGreaterThanOrEqual(3); // last messages are chat, first messages are system message and possibly DisputeOpenedMessage acks
expect(messages[messages.length - 2].getMessage()).toEqual("Arbitrator chat message to dispute opener");
expect(messages[messages.length - 1].getMessage()).toEqual("Dispute opener chat message");
let attachments = messages[messages.length - 1].getAttachmentsList();
expect(attachments.length).toEqual(2);
expect(attachments[0].getFileName()).toEqual("proof.txt");
expect(attachments[0].getBytes()).toEqual(bytes);
expect(attachments[1].getFileName()).toEqual("proof.png");
expect(attachments[1].getBytes()).toEqual(bytes2);
dispute = await ctx.getDisputePeer()!.havenod!.getDispute(ctx.offerId!);
messages = dispute.getChatMessageList();
expect(messages.length).toBeGreaterThanOrEqual(3);
expect(messages[messages.length - 2].getMessage()).toEqual("Arbitrator chat message to dispute peer");
expect(messages[messages.length - 1].getMessage()).toEqual("Dispute peer chat message");

// test notifications of chat messages
let chatNotifications = getNotifications(disputeOpenerNotifications, NotificationMessage.NotificationType.CHAT_MESSAGE, ctx.offerId);
expect(chatNotifications.length).toBe(1);
expect(chatNotifications[0].getChatMessage()?.getMessage()).toEqual("Arbitrator chat message to dispute opener");
chatNotifications = getNotifications(disputePeerNotifications, NotificationMessage.NotificationType.CHAT_MESSAGE, ctx.offerId);
expect(chatNotifications.length).toBe(1);
expect(chatNotifications[0].getChatMessage()?.getMessage()).toEqual("Arbitrator chat message to dispute peer");

// arbitrator has 2 chat messages, one with attachments
chatNotifications = getNotifications(arbitratorNotifications, NotificationMessage.NotificationType.CHAT_MESSAGE, ctx.offerId);
expect(chatNotifications.length).toBe(2);
expect(chatNotifications[0].getChatMessage()?.getMessage()).toEqual("Dispute opener chat message");
assert(chatNotifications[0].getChatMessage()?.getAttachmentsList());
attachments = chatNotifications[0].getChatMessage()?.getAttachmentsList()!;
expect(attachments[0].getFileName()).toEqual("proof.txt");
expect(attachments[0].getBytes()).toEqual(bytes);
expect(attachments[1].getFileName()).toEqual("proof.png");
expect(attachments[1].getBytes()).toEqual(bytes2);
expect(chatNotifications[1].getChatMessage()?.getMessage()).toEqual("Dispute peer chat message");
// test chat messages
if (ctx.testChatMessages) {

// arbitrator sends chat messages to traders
HavenoUtils.log(1, "Arbitrator sending chat messages to traders. tradeId=" + ctx.offerId + ", disputeId=" + openerDispute.getId());
await ctx.arbitrator.havenod!.sendDisputeChatMessage(arbDisputeOpener!.getId(), "Arbitrator chat message to dispute opener", []);
await ctx.arbitrator.havenod!.sendDisputeChatMessage(arbDisputePeer!.getId(), "Arbitrator chat message to dispute peer", []);

// traders reply to arbitrator chat messages
await wait(ctx.maxTimePeerNoticeMs); // wait for arbitrator's message to arrive
const attachment = new Attachment();
const bytes = new Uint8Array(Buffer.from("Proof dispute opener was scammed", "utf8"));
attachment.setBytes(bytes);
attachment.setFileName("proof.txt");
const attachment2 = new Attachment();
const bytes2 = new Uint8Array(Buffer.from("picture bytes", "utf8"));
attachment2.setBytes(bytes2);
attachment2.setFileName("proof.png");
HavenoUtils.log(2, "Dispute opener sending chat message to arbitrator. tradeId=" + ctx.offerId + ", disputeId=" + openerDispute.getId());
await ctx.getDisputeOpener()!.havenod!.sendDisputeChatMessage(openerDispute.getId(), "Dispute opener chat message", [attachment, attachment2]);
await wait(ctx.maxTimePeerNoticeMs); // wait for user2's message to arrive
HavenoUtils.log(2, "Dispute peer sending chat message to arbitrator. tradeId=" + ctx.offerId + ", disputeId=" + peerDispute.getId());
await ctx.getDisputePeer()!.havenod!.sendDisputeChatMessage(peerDispute.getId(), "Dispute peer chat message", []);

// test trader chat messages
await wait(ctx.maxTimePeerNoticeMs);
let dispute = await ctx.getDisputeOpener()!.havenod!.getDispute(ctx.offerId!);
let messages = dispute.getChatMessageList();
expect(messages.length).toBeGreaterThanOrEqual(3); // last messages are chat, first messages are system message and possibly DisputeOpenedMessage acks
expect(messages[messages.length - 2].getMessage()).toEqual("Arbitrator chat message to dispute opener");
expect(messages[messages.length - 1].getMessage()).toEqual("Dispute opener chat message");
let attachments = messages[messages.length - 1].getAttachmentsList();
expect(attachments.length).toEqual(2);
expect(attachments[0].getFileName()).toEqual("proof.txt");
expect(attachments[0].getBytes()).toEqual(bytes);
expect(attachments[1].getFileName()).toEqual("proof.png");
expect(attachments[1].getBytes()).toEqual(bytes2);
dispute = await ctx.getDisputePeer()!.havenod!.getDispute(ctx.offerId!);
messages = dispute.getChatMessageList();
expect(messages.length).toBeGreaterThanOrEqual(3);
expect(messages[messages.length - 2].getMessage()).toEqual("Arbitrator chat message to dispute peer");
expect(messages[messages.length - 1].getMessage()).toEqual("Dispute peer chat message");

// test notifications of chat messages
let chatNotifications = getNotifications(disputeOpenerNotifications, NotificationMessage.NotificationType.CHAT_MESSAGE, ctx.offerId);
expect(chatNotifications.length).toBe(1);
expect(chatNotifications[0].getChatMessage()?.getMessage()).toEqual("Arbitrator chat message to dispute opener");
chatNotifications = getNotifications(disputePeerNotifications, NotificationMessage.NotificationType.CHAT_MESSAGE, ctx.offerId);
expect(chatNotifications.length).toBe(1);
expect(chatNotifications[0].getChatMessage()?.getMessage()).toEqual("Arbitrator chat message to dispute peer");

// arbitrator has 2 chat messages, one with attachments
chatNotifications = getNotifications(arbitratorNotifications, NotificationMessage.NotificationType.CHAT_MESSAGE, ctx.offerId);
expect(chatNotifications.length).toBe(2);
expect(chatNotifications[0].getChatMessage()?.getMessage()).toEqual("Dispute opener chat message");
assert(chatNotifications[0].getChatMessage()?.getAttachmentsList());
attachments = chatNotifications[0].getChatMessage()?.getAttachmentsList()!;
expect(attachments[0].getFileName()).toEqual("proof.txt");
expect(attachments[0].getBytes()).toEqual(bytes);
expect(attachments[1].getFileName()).toEqual("proof.png");
expect(attachments[1].getBytes()).toEqual(bytes2);
expect(chatNotifications[1].getChatMessage()?.getMessage()).toEqual("Dispute peer chat message");
}
}

async function resolveDispute(ctxP: Partial<TradeContext>) {
Expand Down

0 comments on commit 7046924

Please sign in to comment.