Skip to content

Commit 4d3bbc2

Browse files
committed
message test
1 parent 90124f8 commit 4d3bbc2

File tree

4 files changed

+142
-31
lines changed

4 files changed

+142
-31
lines changed

packages/agent-starter/src/lib/types.ts

+17-18
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,26 @@ export interface UserReturnType {
3131
wallet: ReturnType<typeof createWalletClient>;
3232
}
3333

34-
export type xmtpConfig = {
35-
path?: string;
36-
hideInitLogMessage?: boolean;
37-
} & ClientOptions;
38-
3934
export type Agent = {
4035
name?: string;
4136
walletKey?: string;
4237
encryptionKey?: string;
4338
onMessage?: (message: Message) => Promise<void>;
44-
config?: xmtpConfig;
39+
config?: ClientOptions;
4540
};
4641

4742
export type Conversation = {
4843
id: string;
4944
createdAt: Date;
5045
topic?: string;
51-
members?: User[];
46+
members?: {
47+
address: string;
48+
inboxId: string;
49+
installationIds: string[];
50+
accountAddresses: string[];
51+
username?: string;
52+
ensDomain?: string;
53+
}[];
5254
admins?: string[];
5355
name?: string;
5456
superAdmins?: string[];
@@ -57,7 +59,6 @@ export type Conversation = {
5759
export type Message = {
5860
id: string; // Unique identifier for the message
5961
sent: Date; // Date when the message was sent
60-
isDM: boolean; // Whether the message is a direct message
6162
content: {
6263
text?: string | undefined; // Text content of the message
6364
reply?: string | undefined; // Reply content if the message is a reply
@@ -67,19 +68,17 @@ export type Message = {
6768
reference?: string | undefined; // Reference ID for the message
6869
};
6970
group?: Conversation; // Group the message belongs to
70-
sender: User; // Sender of the message
71+
sender: {
72+
address: string;
73+
inboxId: string;
74+
installationIds: string[];
75+
accountAddresses: string[];
76+
username?: string;
77+
ensDomain?: string;
78+
}; // Sender of the message
7179
typeId: string; // Type identifier for the message
7280
client: {
7381
address: string;
7482
inboxId: string;
7583
};
7684
};
77-
78-
export interface User {
79-
address: string;
80-
inboxId: string;
81-
installationIds: string[];
82-
accountAddresses: string[];
83-
username?: string;
84-
ensDomain?: string;
85-
}

packages/agent-starter/src/lib/xmtp.ts

+28-12
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,7 @@ import {
3737
AgentMessageCodec,
3838
ContentTypeAgentMessage,
3939
} from "../content-types/agent-message.js";
40-
import type {
41-
Agent,
42-
clientMessage,
43-
Message,
44-
User,
45-
UserReturnType,
46-
} from "./types.js";
40+
import type { Agent, clientMessage, Message, UserReturnType } from "./types.js";
4741

4842
dotenv.config();
4943

@@ -92,7 +86,7 @@ export class XMTP {
9286

9387
const volumePath =
9488
process.env.RAILWAY_VOLUME_MOUNT_PATH ??
95-
this.agent?.config?.path ??
89+
this.agent?.config?.dbPath ??
9690
".data/xmtp";
9791

9892
if (!fs.existsSync(volumePath)) {
@@ -484,6 +478,7 @@ async function streamMessages(
484478
) {
485479
continue;
486480
}
481+
487482
const parsedMessage = await parseMessage(
488483
message,
489484
conversation,
@@ -587,7 +582,16 @@ export async function parseMessage(
587582
};
588583
}
589584
const date = message.sentAt;
590-
let sender: User | undefined = undefined;
585+
let sender:
586+
| {
587+
inboxId: string;
588+
address: string;
589+
accountAddresses: string[];
590+
installationIds: string[];
591+
username?: string;
592+
ensDomain?: string;
593+
}
594+
| undefined = undefined;
591595

592596
await conversation?.sync();
593597
const members = await conversation?.members();
@@ -596,14 +600,26 @@ export async function parseMessage(
596600
address: member.accountAddresses[0],
597601
accountAddresses: member.accountAddresses,
598602
installationIds: member.installationIds,
599-
})) as User[];
603+
})) as {
604+
inboxId: string;
605+
address: string;
606+
accountAddresses: string[];
607+
installationIds: string[];
608+
}[];
600609

601610
sender = membersArray.find(
602-
(member: User) => member.inboxId === message.senderInboxId,
611+
(member: { inboxId: string }) => member.inboxId === message.senderInboxId,
603612
);
604613
return {
605614
id: message.id,
606-
sender,
615+
sender: {
616+
inboxId: sender?.inboxId || "",
617+
address: sender?.address || "",
618+
accountAddresses: sender?.accountAddresses || [],
619+
installationIds: sender?.installationIds || [],
620+
username: sender?.username || "",
621+
ensDomain: sender?.ensDomain || "",
622+
},
607623
group: {
608624
id: conversation?.id,
609625
createdAt: conversation?.createdAt,

packages/agent-starter/tests/Client.test.ts

+44-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { createUser, XMTP, xmtpClient } from "@xmtp/agent-starter";
21
import { generatePrivateKey } from "viem/accounts";
32
import { describe, expect, test } from "vitest";
3+
import type { Message } from "../src/lib/types";
4+
import { createUser, XMTP, xmtpClient } from "../src/lib/xmtp";
45

56
describe("Client Private Key Configuration Tests", () => {
67
test("creates a client with a random generated key", async () => {
@@ -52,4 +53,46 @@ describe("Client Private Key Configuration Tests", () => {
5253
expect((error as Error).message).toContain("invalid private key");
5354
}
5455
}, 15000); // Added 15 second timeout
56+
57+
test("Creates a key with a agent name", async () => {
58+
const agentName = "bob1";
59+
const xmtp = await xmtpClient({
60+
name: agentName,
61+
onMessage: async (message: Message) => {
62+
await new Promise((resolve) => setTimeout(resolve, 1000));
63+
console.log(
64+
"message received",
65+
message.content.text,
66+
"from",
67+
message.sender.address,
68+
);
69+
},
70+
});
71+
const xmtp2 = await xmtpClient({
72+
name: "alice1",
73+
onMessage: async (message: Message) => {
74+
await new Promise((resolve) => setTimeout(resolve, 1000));
75+
console.log(
76+
"message received",
77+
message.content.text,
78+
"from",
79+
message.sender.address,
80+
);
81+
},
82+
});
83+
84+
const message1 = await xmtp.send({
85+
message: "Hello, Alice!",
86+
receivers: [xmtp2.inboxId || ""],
87+
metadata: {},
88+
});
89+
const message2 = await xmtp2.send({
90+
message: "Hello, Bob!",
91+
receivers: [xmtp.inboxId || ""],
92+
metadata: {},
93+
});
94+
95+
expect(message1).toBeDefined();
96+
expect(message2).toBeDefined();
97+
}, 15000);
5598
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { describe, expect, test } from "vitest";
2+
import type { Message } from "../src/lib/types";
3+
import { xmtpClient } from "../src/lib/xmtp";
4+
5+
describe("Client Private Key Configuration Tests", async () => {
6+
const xmtp = await xmtpClient({
7+
name: "bob2",
8+
onMessage: async (message: Message) => {
9+
console.log(
10+
"Bob received message:",
11+
message.content.text,
12+
"from",
13+
message.sender.address,
14+
);
15+
await new Promise((resolve) => setTimeout(resolve, 1000));
16+
expect(message.content.text).toBe("Hello, Alice!");
17+
expect(message.sender.address).toBe(xmtp2.address);
18+
},
19+
});
20+
console.log("Bob's client initialized");
21+
22+
const xmtp2 = await xmtpClient({
23+
name: "alice2",
24+
onMessage: async (message: Message) => {
25+
console.log(
26+
"Alice received message:",
27+
message.content.text,
28+
"from",
29+
message.sender.address,
30+
);
31+
await new Promise((resolve) => setTimeout(resolve, 1000));
32+
expect(message.content.text).toBe("Hello, Bob!");
33+
expect(message.sender.address).toBe(xmtp.address);
34+
},
35+
});
36+
37+
test("Send a message to a client", async () => {
38+
console.log("Bob's address:", xmtp.address);
39+
console.log("Alice's address:", xmtp2.address);
40+
const message = await xmtp.send({
41+
message: "Hello, Alice!",
42+
receivers: [xmtp2.address as string],
43+
metadata: {},
44+
});
45+
const message2 = await xmtp2.send({
46+
message: "Hello, Bob!",
47+
receivers: [xmtp.address as string],
48+
metadata: {},
49+
});
50+
console.log("Message sent:", message);
51+
console.log("Message sent:", message2);
52+
}, 25000);
53+
});

0 commit comments

Comments
 (0)