Skip to content

Commit 0ebfd91

Browse files
authored
Merge pull request #11 from ephemeraHQ/fix_readme4
Added tests
2 parents 4b74505 + f72f58d commit 0ebfd91

File tree

6 files changed

+97
-39
lines changed

6 files changed

+97
-39
lines changed

.changeset/unlucky-chairs-protect.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@xmtp/agent-starter": patch
3+
---
4+
5+
New tests. Cleaner types.

README.md

-7
Original file line numberDiff line numberDiff line change
@@ -225,10 +225,3 @@ yarn build
225225
# or run a specific example
226226
yarn examples gm
227227
```
228-
229-
Use a `.env` file for your environment variables:
230-
231-
```bash
232-
WALLET_KEY= # the private key of the wallet
233-
ENCRYPTION_KEY= # a second fixed or random 32 bytes encryption key for the local db
234-
```

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

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

55
describe("Client Private Key Configuration Tests", () => {
66
test("creates a client with a random generated key", async () => {
@@ -29,7 +29,7 @@ describe("Client Private Key Configuration Tests", () => {
2929
}, 15000); // Added 15 second timeout
3030

3131
test("Creates a key with a agent name", async () => {
32-
const agentName = "bob1";
32+
const agentName = "bob3";
3333
const xmtp = await xmtpClient({
3434
name: agentName,
3535
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
await new Promise((resolve) => setTimeout(resolve, 1000));
10+
expect(message.content.text).toBe("Hello, Alice!");
11+
expect(message.sender.address).toBe(xmtp2.address);
12+
},
13+
});
14+
console.log("Bob's client initialized");
15+
16+
const xmtp2 = await xmtpClient({
17+
name: "alice2",
18+
onMessage: async (message: Message) => {
19+
console.log(
20+
"Alice received message:",
21+
message.content.text,
22+
"from",
23+
message.sender.address,
24+
);
25+
await new Promise((resolve) => setTimeout(resolve, 1000));
26+
expect(message.content.text).toBe("Hello, Bob!");
27+
expect(message.sender.address).toBe(xmtp.address);
28+
},
29+
});
30+
31+
test("Send a message to a client", async () => {
32+
const message = await xmtp.send({
33+
message: "Hello, Alice!",
34+
receivers: [xmtp2.address as string],
35+
metadata: {},
36+
});
37+
const message2 = await xmtp2.send({
38+
message: "Hello, Bob!",
39+
receivers: [xmtp.address as string],
40+
metadata: {},
41+
});
42+
expect(message).toBeDefined();
43+
expect(message2).toBeDefined();
44+
}, 25000);
45+
});

0 commit comments

Comments
 (0)