|
| 1 | +import request from "supertest"; |
| 2 | +import express, { Express } from "express"; |
| 3 | + |
| 4 | +jest.mock("../lib/db.js", () => ({ |
| 5 | + prisma: { |
| 6 | + stream: { |
| 7 | + findMany: jest.fn(), |
| 8 | + count: jest.fn(), |
| 9 | + }, |
| 10 | + eventLog: { |
| 11 | + findMany: jest.fn(), |
| 12 | + }, |
| 13 | + }, |
| 14 | +})); |
| 15 | + |
| 16 | +// sanitize.ts uses `import.meta.url`, which ts-jest can't compile under the |
| 17 | +// CommonJS transform this project's jest.config.js forces. Stub it out so |
| 18 | +// importing streams.routes.ts (which depends on it) doesn't fail to compile. |
| 19 | +jest.mock("../security/sanitize.js", () => ({ |
| 20 | + sanitizeUnknown: (input: unknown) => input, |
| 21 | +})); |
| 22 | + |
| 23 | +import { prisma } from "../lib/db.js"; |
| 24 | +import streamsRouter from "../api/streams.routes.js"; |
| 25 | +import v2StreamsRouter from "../api/v2/streams.routes.js"; |
| 26 | +import v3HistoryRouter from "../api/v3/history.routes.js"; |
| 27 | +import { getSearch } from "../api/public.js"; |
| 28 | + |
| 29 | +const ADDRESS = "G" + "A".repeat(55); |
| 30 | +const MAX_ALLOWED_QUERIES = 5; |
| 31 | + |
| 32 | +function buildMockStream(overrides: Partial<Record<string, unknown>> = {}) { |
| 33 | + return { |
| 34 | + id: `stream_${Math.random().toString(36).slice(2)}`, |
| 35 | + streamId: `contract_${Math.random().toString(36).slice(2)}`, |
| 36 | + txHash: `tx_${Math.random().toString(36).slice(2)}`, |
| 37 | + sender: ADDRESS, |
| 38 | + receiver: "GBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB", |
| 39 | + tokenAddress: "CUSDC", |
| 40 | + amount: "1000000000", |
| 41 | + duration: 86400, |
| 42 | + status: "ACTIVE", |
| 43 | + withdrawn: "0", |
| 44 | + legacy: false, |
| 45 | + migrated: false, |
| 46 | + isPrivate: false, |
| 47 | + createdAt: new Date(), |
| 48 | + ...overrides, |
| 49 | + }; |
| 50 | +} |
| 51 | + |
| 52 | +function totalMockCalls(): number { |
| 53 | + return ( |
| 54 | + (prisma.stream.findMany as jest.Mock).mock.calls.length + |
| 55 | + (prisma.stream.count as jest.Mock).mock.calls.length + |
| 56 | + (prisma.eventLog.findMany as jest.Mock).mock.calls.length |
| 57 | + ); |
| 58 | +} |
| 59 | + |
| 60 | +describe("Stream listing endpoints stay below the N+1 query budget", () => { |
| 61 | + beforeEach(() => { |
| 62 | + jest.clearAllMocks(); |
| 63 | + }); |
| 64 | + |
| 65 | + describe.each([50, 1000])("with %d streams", (count) => { |
| 66 | + const mockStreams = () => |
| 67 | + Array.from({ length: count }, () => buildMockStream()); |
| 68 | + |
| 69 | + it("GET /api/v1/streams/:address", async () => { |
| 70 | + const app: Express = express(); |
| 71 | + app.use(express.json()); |
| 72 | + app.use("/api/v1", streamsRouter); |
| 73 | + |
| 74 | + (prisma.stream.findMany as jest.Mock).mockResolvedValueOnce(mockStreams()); |
| 75 | + |
| 76 | + const res = await request(app).get(`/api/v1/streams/${ADDRESS}`); |
| 77 | + |
| 78 | + expect(res.status).toBe(200); |
| 79 | + expect(res.body.count).toBe(count); |
| 80 | + expect(totalMockCalls()).toBeLessThan(MAX_ALLOWED_QUERIES); |
| 81 | + }); |
| 82 | + |
| 83 | + it("GET /api/v1/streams/export/:address", async () => { |
| 84 | + const app: Express = express(); |
| 85 | + app.use(express.json()); |
| 86 | + app.use("/api/v1", streamsRouter); |
| 87 | + |
| 88 | + const streams = mockStreams(); |
| 89 | + (prisma.stream.findMany as jest.Mock).mockResolvedValueOnce(streams); |
| 90 | + (prisma.eventLog.findMany as jest.Mock).mockResolvedValueOnce([]); |
| 91 | + |
| 92 | + const res = await request(app).get(`/api/v1/streams/export/${ADDRESS}`); |
| 93 | + |
| 94 | + expect(res.status).toBe(200); |
| 95 | + expect(totalMockCalls()).toBeLessThan(MAX_ALLOWED_QUERIES); |
| 96 | + }); |
| 97 | + |
| 98 | + it("GET /api/v2/streams/:address", async () => { |
| 99 | + const app: Express = express(); |
| 100 | + app.use(express.json()); |
| 101 | + app.use("/api/v2/streams", v2StreamsRouter); |
| 102 | + |
| 103 | + (prisma.stream.findMany as jest.Mock).mockResolvedValueOnce(mockStreams()); |
| 104 | + |
| 105 | + const res = await request(app).get(`/api/v2/streams/${ADDRESS}`); |
| 106 | + |
| 107 | + expect(res.status).toBe(200); |
| 108 | + expect(res.body.v1).toHaveLength(count); |
| 109 | + expect(totalMockCalls()).toBeLessThan(MAX_ALLOWED_QUERIES); |
| 110 | + }); |
| 111 | + |
| 112 | + it("GET /api/v3/history/:address", async () => { |
| 113 | + const app: Express = express(); |
| 114 | + app.use(express.json()); |
| 115 | + app.use("/api/v3", v3HistoryRouter); |
| 116 | + |
| 117 | + (prisma.stream.count as jest.Mock).mockResolvedValueOnce(count); |
| 118 | + (prisma.stream.findMany as jest.Mock).mockResolvedValueOnce( |
| 119 | + mockStreams().slice(0, 50), |
| 120 | + ); |
| 121 | + |
| 122 | + const res = await request(app).get(`/api/v3/history/${ADDRESS}`); |
| 123 | + |
| 124 | + expect(res.status).toBe(200); |
| 125 | + expect(res.body.data.total).toBe(count); |
| 126 | + expect(totalMockCalls()).toBeLessThan(MAX_ALLOWED_QUERIES); |
| 127 | + }); |
| 128 | + |
| 129 | + it("GET /api/v1/search", async () => { |
| 130 | + const app: Express = express(); |
| 131 | + app.use(express.json()); |
| 132 | + app.get("/api/v1/search", getSearch); |
| 133 | + |
| 134 | + (prisma.stream.findMany as jest.Mock).mockResolvedValueOnce( |
| 135 | + mockStreams().slice(0, 20), |
| 136 | + ); |
| 137 | + (prisma.stream.count as jest.Mock).mockResolvedValueOnce(count); |
| 138 | + |
| 139 | + const res = await request(app).get("/api/v1/search"); |
| 140 | + |
| 141 | + expect(res.status).toBe(200); |
| 142 | + expect(res.body.total).toBe(count); |
| 143 | + expect(totalMockCalls()).toBeLessThan(MAX_ALLOWED_QUERIES); |
| 144 | + }); |
| 145 | + }); |
| 146 | + |
| 147 | + it("query count for 1000 streams is identical to the query count for 50 streams (no per-row queries)", async () => { |
| 148 | + const app: Express = express(); |
| 149 | + app.use(express.json()); |
| 150 | + app.use("/api/v1", streamsRouter); |
| 151 | + |
| 152 | + (prisma.stream.findMany as jest.Mock).mockResolvedValueOnce( |
| 153 | + Array.from({ length: 50 }, () => buildMockStream()), |
| 154 | + ); |
| 155 | + await request(app).get(`/api/v1/streams/${ADDRESS}`); |
| 156 | + const callsAt50 = totalMockCalls(); |
| 157 | + |
| 158 | + jest.clearAllMocks(); |
| 159 | + |
| 160 | + (prisma.stream.findMany as jest.Mock).mockResolvedValueOnce( |
| 161 | + Array.from({ length: 1000 }, () => buildMockStream()), |
| 162 | + ); |
| 163 | + await request(app).get(`/api/v1/streams/${ADDRESS}`); |
| 164 | + const callsAt1000 = totalMockCalls(); |
| 165 | + |
| 166 | + expect(callsAt1000).toBe(callsAt50); |
| 167 | + }); |
| 168 | +}); |
0 commit comments