Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added tests #11

Merged
merged 7 commits into from
Jan 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/unlucky-chairs-protect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@xmtp/agent-starter": patch
---

New tests. Cleaner types.
7 changes: 0 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,10 +225,3 @@ yarn build
# or run a specific example
yarn examples gm
```

Use a `.env` file for your environment variables:

```bash
WALLET_KEY= # the private key of the wallet
ENCRYPTION_KEY= # a second fixed or random 32 bytes encryption key for the local db
```
35 changes: 17 additions & 18 deletions packages/agent-starter/src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,26 @@ export interface UserReturnType {
wallet: ReturnType<typeof createWalletClient>;
}

export type xmtpConfig = {
path?: string;
hideInitLogMessage?: boolean;
} & ClientOptions;

export type Agent = {
name?: string;
walletKey?: string;
encryptionKey?: string;
onMessage?: (message: Message) => Promise<void>;
config?: xmtpConfig;
config?: ClientOptions;
};

export type Conversation = {
id: string;
createdAt: Date;
topic?: string;
members?: User[];
members?: {
address: string;
inboxId: string;
installationIds: string[];
accountAddresses: string[];
username?: string;
ensDomain?: string;
}[];
admins?: string[];
name?: string;
superAdmins?: string[];
Expand All @@ -57,7 +59,6 @@ export type Conversation = {
export type Message = {
id: string; // Unique identifier for the message
sent: Date; // Date when the message was sent
isDM: boolean; // Whether the message is a direct message
content: {
text?: string | undefined; // Text content of the message
reply?: string | undefined; // Reply content if the message is a reply
Expand All @@ -67,19 +68,17 @@ export type Message = {
reference?: string | undefined; // Reference ID for the message
};
group?: Conversation; // Group the message belongs to
sender: User; // Sender of the message
sender: {
address: string;
inboxId: string;
installationIds: string[];
accountAddresses: string[];
username?: string;
ensDomain?: string;
}; // Sender of the message
typeId: string; // Type identifier for the message
client: {
address: string;
inboxId: string;
};
};

export interface User {
address: string;
inboxId: string;
installationIds: string[];
accountAddresses: string[];
username?: string;
ensDomain?: string;
}
40 changes: 28 additions & 12 deletions packages/agent-starter/src/lib/xmtp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,7 @@ import {
AgentMessageCodec,
ContentTypeAgentMessage,
} from "../content-types/agent-message.js";
import type {
Agent,
clientMessage,
Message,
User,
UserReturnType,
} from "./types.js";
import type { Agent, clientMessage, Message, UserReturnType } from "./types.js";

dotenv.config();

Expand Down Expand Up @@ -92,7 +86,7 @@ export class XMTP {

const volumePath =
process.env.RAILWAY_VOLUME_MOUNT_PATH ??
this.agent?.config?.path ??
this.agent?.config?.dbPath ??
".data/xmtp";

if (!fs.existsSync(volumePath)) {
Expand Down Expand Up @@ -484,6 +478,7 @@ async function streamMessages(
) {
continue;
}

const parsedMessage = await parseMessage(
message,
conversation,
Expand Down Expand Up @@ -587,7 +582,16 @@ export async function parseMessage(
};
}
const date = message.sentAt;
let sender: User | undefined = undefined;
let sender:
| {
inboxId: string;
address: string;
accountAddresses: string[];
installationIds: string[];
username?: string;
ensDomain?: string;
}
| undefined = undefined;

await conversation?.sync();
const members = await conversation?.members();
Expand All @@ -596,14 +600,26 @@ export async function parseMessage(
address: member.accountAddresses[0],
accountAddresses: member.accountAddresses,
installationIds: member.installationIds,
})) as User[];
})) as {
inboxId: string;
address: string;
accountAddresses: string[];
installationIds: string[];
}[];

sender = membersArray.find(
(member: User) => member.inboxId === message.senderInboxId,
(member: { inboxId: string }) => member.inboxId === message.senderInboxId,
);
return {
id: message.id,
sender,
sender: {
inboxId: sender?.inboxId || "",
address: sender?.address || "",
accountAddresses: sender?.accountAddresses || [],
installationIds: sender?.installationIds || [],
username: sender?.username || "",
ensDomain: sender?.ensDomain || "",
},
group: {
id: conversation?.id,
createdAt: conversation?.createdAt,
Expand Down
4 changes: 2 additions & 2 deletions packages/agent-starter/tests/Client.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createUser, XMTP, xmtpClient } from "@xmtp/agent-starter";
import { generatePrivateKey } from "viem/accounts";
import { describe, expect, test } from "vitest";
import { createUser, XMTP, xmtpClient } from "../src/lib/xmtp";

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

test("Creates a key with a agent name", async () => {
const agentName = "bob1";
const agentName = "bob3";
const xmtp = await xmtpClient({
name: agentName,
});
Expand Down
45 changes: 45 additions & 0 deletions packages/agent-starter/tests/Message.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { describe, expect, test } from "vitest";
import type { Message } from "../src/lib/types";
import { xmtpClient } from "../src/lib/xmtp";

describe("Client Private Key Configuration Tests", async () => {
const xmtp = await xmtpClient({
name: "bob2",
onMessage: async (message: Message) => {
await new Promise((resolve) => setTimeout(resolve, 1000));
expect(message.content.text).toBe("Hello, Alice!");
expect(message.sender.address).toBe(xmtp2.address);
},
});
console.log("Bob's client initialized");

const xmtp2 = await xmtpClient({
name: "alice2",
onMessage: async (message: Message) => {
console.log(
"Alice received message:",
message.content.text,
"from",
message.sender.address,
);
await new Promise((resolve) => setTimeout(resolve, 1000));
expect(message.content.text).toBe("Hello, Bob!");
expect(message.sender.address).toBe(xmtp.address);
},
});

test("Send a message to a client", async () => {
const message = await xmtp.send({
message: "Hello, Alice!",
receivers: [xmtp2.address as string],
metadata: {},
});
const message2 = await xmtp2.send({
message: "Hello, Bob!",
receivers: [xmtp.address as string],
metadata: {},
});
expect(message).toBeDefined();
expect(message2).toBeDefined();
}, 25000);
});
Loading