mirror of
https://github.com/haveno-dex/haveno-ts.git
synced 2025-08-09 06:52:58 -04:00
update and enable chat message tests
This commit is contained in:
parent
49be11e389
commit
9fee02b4c0
1 changed files with 57 additions and 38 deletions
|
@ -146,7 +146,8 @@ const defaultTradeConfig: Partial<TradeContext> = {
|
||||||
disputeSummary: "Seller is winner",
|
disputeSummary: "Seller is winner",
|
||||||
walletSyncPeriodMs: 5000,
|
walletSyncPeriodMs: 5000,
|
||||||
maxTimePeerNoticeMs: 6000,
|
maxTimePeerNoticeMs: 6000,
|
||||||
testChatMessages: true,
|
testTradeChatMessages: true,
|
||||||
|
testDisputeChatMessages: true,
|
||||||
stopOnFailure: false, // TODO: setting to true can cause error: Http response at 400 or 500 level, http status code: 503
|
stopOnFailure: false, // TODO: setting to true can cause error: Http response at 400 or 500 level, http status code: 503
|
||||||
testPayoutConfirmed: true,
|
testPayoutConfirmed: true,
|
||||||
testPayoutUnlocked: false,
|
testPayoutUnlocked: false,
|
||||||
|
@ -203,7 +204,8 @@ class TradeContext {
|
||||||
offerId?: string;
|
offerId?: string;
|
||||||
takerPaymentAccountId?: string;
|
takerPaymentAccountId?: string;
|
||||||
challenge?: string;
|
challenge?: string;
|
||||||
testTraderChat?: boolean;
|
testTradeChatMessages?: boolean;
|
||||||
|
tradeChatMessagesTested?: boolean;
|
||||||
|
|
||||||
// resolve dispute
|
// resolve dispute
|
||||||
resolveDispute?: boolean
|
resolveDispute?: boolean
|
||||||
|
@ -230,7 +232,8 @@ class TradeContext {
|
||||||
sellerOpenedDispute?: boolean;
|
sellerOpenedDispute?: boolean;
|
||||||
walletSyncPeriodMs!: number;
|
walletSyncPeriodMs!: number;
|
||||||
maxTimePeerNoticeMs!: number;
|
maxTimePeerNoticeMs!: number;
|
||||||
testChatMessages!: boolean;
|
testDisputeChatMessages!: boolean;
|
||||||
|
disputeChatMessagesTested!: boolean;
|
||||||
stopOnFailure?: boolean;
|
stopOnFailure?: boolean;
|
||||||
buyerAppName?: string;
|
buyerAppName?: string;
|
||||||
sellerAppName?: string;
|
sellerAppName?: string;
|
||||||
|
@ -2706,7 +2709,7 @@ async function executeTrade(ctxP: Partial<TradeContext>): Promise<string> {
|
||||||
|
|
||||||
// test trader chat
|
// test trader chat
|
||||||
if (ctx.isStopped) return ctx.offerId!;
|
if (ctx.isStopped) return ctx.offerId!;
|
||||||
if (ctx.testTraderChat) await testTradeChat(ctx);
|
if (ctx.testTradeChatMessages && !ctx.tradeChatMessagesTested) await testTradeChat(ctx); // test trader chat once
|
||||||
|
|
||||||
// get expected payment account payloads
|
// get expected payment account payloads
|
||||||
if (ctx.isStopped) return ctx.offerId!;
|
if (ctx.isStopped) return ctx.offerId!;
|
||||||
|
@ -3345,7 +3348,7 @@ async function testOpenDispute(ctxP: Partial<TradeContext>) {
|
||||||
await arbitrator.addNotificationListener(notification => { HavenoUtils.log(3, "Arbitrator received notification " + notification.getType() + " " + (notification.getChatMessage() ? notification.getChatMessage()?.getMessage() : "")); arbitratorNotifications.push(notification); });
|
await arbitrator.addNotificationListener(notification => { HavenoUtils.log(3, "Arbitrator received notification " + notification.getType() + " " + (notification.getChatMessage() ? notification.getChatMessage()?.getMessage() : "")); arbitratorNotifications.push(notification); });
|
||||||
|
|
||||||
// test chat messages
|
// test chat messages
|
||||||
if (ctx.testChatMessages) {
|
if (ctx.testDisputeChatMessages && !ctx.disputeChatMessagesTested) {
|
||||||
|
|
||||||
// arbitrator sends chat messages to traders
|
// arbitrator sends chat messages to traders
|
||||||
HavenoUtils.log(1, "Arbitrator sending chat messages to traders. tradeId=" + ctx.offerId + ", disputeId=" + openerDispute.getId());
|
HavenoUtils.log(1, "Arbitrator sending chat messages to traders. tradeId=" + ctx.offerId + ", disputeId=" + openerDispute.getId());
|
||||||
|
@ -3419,6 +3422,8 @@ async function testOpenDispute(ctxP: Partial<TradeContext>) {
|
||||||
expect(attachments[1].getFileName()).toEqual("proof.png");
|
expect(attachments[1].getFileName()).toEqual("proof.png");
|
||||||
expect(attachments[1].getBytes()).toEqual(bytes2);
|
expect(attachments[1].getBytes()).toEqual(bytes2);
|
||||||
expect(chatNotifications[1].getChatMessage()?.getMessage()).toEqual("Dispute peer chat message");
|
expect(chatNotifications[1].getChatMessage()?.getMessage()).toEqual("Dispute peer chat message");
|
||||||
|
|
||||||
|
ctx.disputeChatMessagesTested = true; // mark chat messages as tested
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3612,74 +3617,88 @@ async function testTradeChat(ctxP: Partial<TradeContext>) {
|
||||||
|
|
||||||
// invalid trade should throw error
|
// invalid trade should throw error
|
||||||
try {
|
try {
|
||||||
await user1.getChatMessages("invalid");
|
await ctx.maker.havenod!.getChatMessages("invalid");
|
||||||
throw new Error("get chat messages with invalid id should fail");
|
throw new Error("get chat messages with invalid id should fail");
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
assert.equal(err.message, "trade with id 'invalid' not found");
|
assert.equal(err.message, "trade with id 'invalid' not found");
|
||||||
}
|
}
|
||||||
|
|
||||||
// trade chat should be in initial state
|
// trade chat should be in initial state
|
||||||
let messages = await user1.getChatMessages(ctx.offerId!);
|
let messages = await ctx.maker.havenod!.getChatMessages(ctx.offerId!);
|
||||||
assert(messages.length === 0);
|
expect(messages.length).toEqual(0);
|
||||||
messages = await user2.getChatMessages(ctx.offerId!);
|
messages = await ctx.taker.havenod!.getChatMessages(ctx.offerId!);
|
||||||
assert(messages.length === 0);
|
expect(messages.length).toEqual(0);
|
||||||
|
|
||||||
// add notification handlers and send some messages
|
// add notification handlers and send some messages
|
||||||
const user1Notifications: NotificationMessage[] = [];
|
const makerNotifications: NotificationMessage[] = [];
|
||||||
const user2Notifications: NotificationMessage[] = [];
|
const takerNotifications: NotificationMessage[] = [];
|
||||||
await user1.addNotificationListener(notification => { user1Notifications.push(notification); });
|
await ctx.maker.havenod!.addNotificationListener(notification => { makerNotifications.push(notification); });
|
||||||
await user2.addNotificationListener(notification => { user2Notifications.push(notification); });
|
await ctx.taker.havenod!.addNotificationListener(notification => { takerNotifications.push(notification); });
|
||||||
|
|
||||||
// send simple conversation and verify the list of messages
|
// send simple conversation and verify the list of messages
|
||||||
const user1Msg = "Hi I'm user1";
|
const makerMsg = "Hi I'm the maker";
|
||||||
await user1.sendChatMessage(ctx.offerId!, user1Msg);
|
await await ctx.maker.havenod!.sendChatMessage(ctx.offerId!, makerMsg);
|
||||||
await wait(ctx.maxTimePeerNoticeMs);
|
await wait(ctx.maxTimePeerNoticeMs);
|
||||||
messages = await user2.getChatMessages(ctx.offerId!);
|
messages = await ctx.taker.havenod!.getChatMessages(ctx.offerId!);
|
||||||
expect(messages.length).toEqual(2);
|
expect(messages.length).toEqual(2);
|
||||||
expect(messages[0].getIsSystemMessage()).toEqual(true); // first message is system
|
expect(messages[0].getIsSystemMessage()).toEqual(true); // first message is system
|
||||||
expect(messages[1].getMessage()).toEqual(user1Msg);
|
expect(messages[1].getMessage()).toEqual(makerMsg);
|
||||||
|
|
||||||
const user2Msg = "Hello I'm user2";
|
const takerMsg = "Hello I'm the taker";
|
||||||
await user2.sendChatMessage(ctx.offerId!, user2Msg);
|
await ctx.taker.havenod!.sendChatMessage(ctx.offerId!, takerMsg);
|
||||||
await wait(ctx.maxTimePeerNoticeMs);
|
await wait(ctx.maxTimePeerNoticeMs);
|
||||||
messages = await user1.getChatMessages(ctx.offerId!);
|
messages = await ctx.maker.havenod!.getChatMessages(ctx.offerId!);
|
||||||
expect(messages.length).toEqual(3);
|
expect(messages.length).toEqual(3);
|
||||||
expect(messages[0].getIsSystemMessage()).toEqual(true);
|
expect(messages[0].getIsSystemMessage()).toEqual(true);
|
||||||
expect(messages[1].getMessage()).toEqual(user1Msg);
|
expect(messages[1].getMessage()).toEqual(makerMsg);
|
||||||
expect(messages[2].getMessage()).toEqual(user2Msg);
|
expect(messages[2].getMessage()).toEqual(takerMsg);
|
||||||
|
|
||||||
// verify notifications
|
// verify notifications
|
||||||
let chatNotifications = getNotifications(user1Notifications, NotificationMessage.NotificationType.CHAT_MESSAGE);
|
let chatNotifications = getNotifications(makerNotifications, NotificationMessage.NotificationType.CHAT_MESSAGE);
|
||||||
expect(chatNotifications.length).toBe(1);
|
if (ctx.concurrentTrades) {
|
||||||
expect(chatNotifications[0].getChatMessage()?.getMessage()).toEqual(user2Msg);
|
expect(chatNotifications.length).toBeGreaterThanOrEqual(1);
|
||||||
chatNotifications = getNotifications(user2Notifications, NotificationMessage.NotificationType.CHAT_MESSAGE);
|
} else {
|
||||||
expect(chatNotifications.length).toBe(1);
|
expect(chatNotifications.length).toBe(1);
|
||||||
expect(chatNotifications[0].getChatMessage()?.getMessage()).toEqual(user1Msg);
|
expect(chatNotifications[0].getChatMessage()?.getMessage()).toEqual(takerMsg);
|
||||||
|
}
|
||||||
|
chatNotifications = getNotifications(takerNotifications, NotificationMessage.NotificationType.CHAT_MESSAGE);
|
||||||
|
if (ctx.concurrentTrades) {
|
||||||
|
expect(chatNotifications.length).toBeGreaterThanOrEqual(1);
|
||||||
|
} else {
|
||||||
|
expect(chatNotifications.length).toBe(1);
|
||||||
|
expect(chatNotifications[0].getChatMessage()?.getMessage()).toEqual(makerMsg);
|
||||||
|
}
|
||||||
|
|
||||||
// additional msgs
|
// additional msgs
|
||||||
const msgs = ["", " ", "<script>alert('test');</script>", "さようなら"];
|
const msgs = ["", " ", "<script>alert('test');</script>", "さようなら"];
|
||||||
for(const msg of msgs) {
|
for(const msg of msgs) {
|
||||||
await user1.sendChatMessage(ctx.offerId!, msg);
|
await ctx.maker.havenod!.sendChatMessage(ctx.offerId!, msg);
|
||||||
await wait(1000); // the async operation can result in out of order messages
|
await wait(1000); // the async operation can result in out of order messages
|
||||||
}
|
}
|
||||||
await wait(ctx.maxTimePeerNoticeMs);
|
await wait(ctx.maxTimePeerNoticeMs);
|
||||||
messages = await user2.getChatMessages(ctx.offerId!);
|
messages = await ctx.taker.havenod!.getChatMessages(ctx.offerId!);
|
||||||
let offset = 3; // 3 existing messages
|
let offset = 3; // 3 existing messages
|
||||||
expect(messages.length).toEqual(offset + msgs.length);
|
expect(messages.length).toEqual(offset + msgs.length);
|
||||||
expect(messages[0].getIsSystemMessage()).toEqual(true);
|
expect(messages[0].getIsSystemMessage()).toEqual(true);
|
||||||
expect(messages[1].getMessage()).toEqual(user1Msg);
|
expect(messages[1].getMessage()).toEqual(makerMsg);
|
||||||
expect(messages[2].getMessage()).toEqual(user2Msg);
|
expect(messages[2].getMessage()).toEqual(takerMsg);
|
||||||
for (let i = 0; i < msgs.length; i++) {
|
for (let i = 0; i < msgs.length; i++) {
|
||||||
expect(messages[i + offset].getMessage()).toEqual(msgs[i]);
|
expect(messages[i + offset].getMessage()).toEqual(msgs[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
chatNotifications = getNotifications(user2Notifications, NotificationMessage.NotificationType.CHAT_MESSAGE);
|
chatNotifications = getNotifications(takerNotifications, NotificationMessage.NotificationType.CHAT_MESSAGE);
|
||||||
offset = 1; // 1 existing notification
|
offset = 1; // 1 existing notification
|
||||||
expect(chatNotifications.length).toBe(offset + msgs.length);
|
if (ctx.concurrentTrades) {
|
||||||
expect(chatNotifications[0].getChatMessage()?.getMessage()).toEqual(user1Msg);
|
expect(chatNotifications.length).toBeGreaterThanOrEqual(offset + msgs.length);
|
||||||
for (let i = 0; i < msgs.length; i++) {
|
} else {
|
||||||
expect(chatNotifications[i + offset].getChatMessage()?.getMessage()).toEqual(msgs[i]);
|
expect(chatNotifications.length).toBe(offset + msgs.length);
|
||||||
|
expect(chatNotifications[0].getChatMessage()?.getMessage()).toEqual(makerMsg);
|
||||||
|
for (let i = 0; i < msgs.length; i++) {
|
||||||
|
expect(chatNotifications[i + offset].getChatMessage()?.getMessage()).toEqual(msgs[i]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ctx.tradeChatMessagesTested = true; // mark trade chat as tested
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------------- OTHER HELPERS ---------------------------------
|
// ---------------------------- OTHER HELPERS ---------------------------------
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue