A tool for parsing traffic on the jetstream and applying a moderation workstream based on regexp based rules
1import { beforeEach, describe, expect, it, vi } from "vitest";
2
3describe("Agent", () => {
4 beforeEach(() => {
5 vi.resetModules();
6 });
7
8 it("should create an agent and login", async () => {
9 // Mock the config variables
10 vi.doMock("../config.js", () => ({
11 BSKY_HANDLE: "test.bsky.social",
12 BSKY_PASSWORD: "password",
13 OZONE_PDS: "pds.test.com",
14 }));
15
16 // Mock session
17 const mockSession = {
18 did: "did:plc:test123",
19 handle: "test.bsky.social",
20 accessJwt: "test-access-jwt",
21 refreshJwt: "test-refresh-jwt",
22 };
23
24 // Mock the AtpAgent
25 const mockLogin = vi.fn(() =>
26 Promise.resolve({ success: true, data: mockSession }),
27 );
28 const mockConstructor = vi.fn();
29 vi.doMock("@atproto/api", () => ({
30 AtpAgent: class {
31 login = mockLogin;
32 service: URL;
33 session = mockSession;
34 constructor(options: { service: string }) {
35 mockConstructor(options);
36 this.service = new URL(options.service);
37 }
38 },
39 }));
40
41 const { agent, login } = await import("../agent.js");
42
43 // Check that the agent was created with the correct service URL
44 expect(mockConstructor).toHaveBeenCalledWith(
45 expect.objectContaining({
46 service: "https://pds.test.com",
47 }),
48 );
49 expect(agent.service.toString()).toBe("https://pds.test.com/");
50
51 // Check that the login function calls the mockLogin function
52 await login();
53 expect(mockLogin).toHaveBeenCalledWith({
54 identifier: "test.bsky.social",
55 password: "password",
56 });
57 });
58});