relay filter/appview bootstrap
1import { describe, it, expect, mock, beforeEach } from "bun:test";
2import { listLatticesHandler } from "../../src/api/lattice";
3
4const createChainableMock = () => {
5 const mockObj: any = {};
6 const mockFn = mock(() => mockObj);
7
8 mockObj.selectFrom = mockFn;
9 mockObj.selectAll = mockFn;
10 mockObj.select = mockFn;
11 mockObj.where = mockFn;
12 mockObj.orderBy = mockFn;
13 mockObj.limit = mockFn;
14 mockObj.offset = mockFn;
15 mockObj.innerJoin = mockFn;
16 mockObj.leftJoin = mockFn;
17 mockObj.updateTable = mockFn;
18 mockObj.set = mockFn;
19 mockObj.execute = mock(() => Promise.resolve([]));
20
21 return mockObj;
22};
23
24const dbMock = createChainableMock();
25
26mock.module("../../src/db", () => ({
27 db: dbMock
28}));
29
30describe("listLatticesHandler", () => {
31 beforeEach(() => {
32 dbMock.execute.mockResolvedValue([]);
33 dbMock.selectFrom.mockClear();
34 });
35
36 it("should throw if author is missing", async () => {
37 const ctx = { params: {} };
38 expect(listLatticesHandler(ctx as any)).rejects.toThrow("Missing required parameter: author");
39 });
40
41 it("should return lattices", async () => {
42 const mockData = [{
43 uri: "at://did:plc:123/systems.gmstn.development.lattice/rkey",
44 cid: "bafy_lattice",
45 creator_did: "did:plc:123",
46 description: "Test Lattice",
47 created_at: new Date("2023-01-01T00:00:00Z"),
48 indexed_at: new Date("2023-01-02T00:00:00Z")
49 }];
50 dbMock.execute.mockResolvedValue(mockData);
51
52 const ctx = { params: { author: "did:plc:123" } };
53 const result = await listLatticesHandler(ctx as any);
54
55 expect(result.body.lattices).toHaveLength(1);
56 expect(result.body.lattices[0]).toEqual({
57 uri: mockData[0].uri,
58 cid: mockData[0].cid,
59 author: mockData[0].creator_did,
60 description: mockData[0].description,
61 createdAt: mockData[0].created_at.toISOString(),
62 indexedAt: mockData[0].indexed_at.toISOString()
63 });
64 });
65
66 it("should handle cursor", async () => {
67 dbMock.execute.mockResolvedValue([]);
68 const ctx = { params: { author: "did:plc:123", cursor: "2023-01-01T00:00:00Z" } };
69 await listLatticesHandler(ctx as any);
70
71 expect(dbMock.selectFrom).toHaveBeenCalled();
72 });
73});