relay filter/appview bootstrap
at main 1.9 kB view raw
1import { describe, it, expect, mock, beforeEach } from "bun:test"; 2import { listShardsHandler } from "../../src/api/shard"; 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("listShardsHandler", () => { 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(listShardsHandler(ctx as any)).rejects.toThrow("Missing required parameter: author"); 39 }); 40 41 it("should return shards", async () => { 42 const mockData = [{ 43 uri: "at://did:plc:123/systems.gmstn.development.shard/rkey", 44 cid: "bafy_shard", 45 creator_did: "did:plc:123", 46 description: "Test Shard", 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 listShardsHandler(ctx as any); 54 55 expect(result.body.shards).toHaveLength(1); 56 expect(result.body.shards[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});