|
| 1 | +import request from "supertest"; |
| 2 | +import { createApp } from "../src/index"; |
| 3 | +import { db } from "../src/db/connection"; |
| 4 | +import { users, markets, predictions } from "../src/db/schema"; |
| 5 | +import { eq } from "drizzle-orm"; |
| 6 | + |
| 7 | +describe("GET /api/users/:address/predictions", () => { |
| 8 | + const testAddress = "GBBD47UZQ5DXGX23UKMHLGG5TZPJJKISVQYER3SPRINGS57LVEDSTQCEO"; |
| 9 | + |
| 10 | + beforeAll(async () => { |
| 11 | + // Clean up test data |
| 12 | + await db.delete(predictions); |
| 13 | + await db.delete(markets); |
| 14 | + await db.delete(users); |
| 15 | + |
| 16 | + // Seed test data |
| 17 | + await db.insert(users).values({ stellarAddress: testAddress }); |
| 18 | + const user = await db.query.users.findFirst({ |
| 19 | + where: eq(users.stellarAddress, testAddress), |
| 20 | + }); |
| 21 | + |
| 22 | + await db.insert(markets).values({ |
| 23 | + id: "market-1", |
| 24 | + question: "Will ETH reach $10k by EOY?", |
| 25 | + status: "active", |
| 26 | + resolutionTime: new Date(Date.now() + 90 * 24 * 60 * 60 * 1000), |
| 27 | + indexedLedger: 0, |
| 28 | + }); |
| 29 | + |
| 30 | + for (let i = 0; i < 25; i++) { |
| 31 | + await db.insert(predictions).values({ |
| 32 | + marketId: "market-1", |
| 33 | + userId: user!.id, |
| 34 | + outcome: i % 2 === 0 ? "yes" : "no", |
| 35 | + amount: "100", |
| 36 | + status: i < 10 ? "pending" : i < 15 ? "confirmed" : "won", |
| 37 | + createdAt: new Date(Date.now() - i * 60 * 60 * 1000), |
| 38 | + }); |
| 39 | + } |
| 40 | + }); |
| 41 | + |
| 42 | + afterAll(async () => { |
| 43 | + // Clean up |
| 44 | + await db.delete(predictions); |
| 45 | + await db.delete(markets); |
| 46 | + await db.delete(users); |
| 47 | + }); |
| 48 | + |
| 49 | + it("should return 404 for unknown address", async () => { |
| 50 | + const res = await request(createApp()).get( |
| 51 | + "/api/users/GBUNKKNOWN000000000000000000000000000000000000000000000000/predictions" |
| 52 | + ); |
| 53 | + expect(res.status).toBe(404); |
| 54 | + expect(res.body.error.code).toBe("not_found"); |
| 55 | + }); |
| 56 | + |
| 57 | + it("should return all predictions when no status filter", async () => { |
| 58 | + const res = await request(createApp()).get( |
| 59 | + `/api/users/${testAddress}/predictions?limit=10` |
| 60 | + ); |
| 61 | + expect(res.status).toBe(200); |
| 62 | + expect(res.body.data.length).toBe(10); |
| 63 | + expect(res.body.nextCursor).toBeDefined(); |
| 64 | + }); |
| 65 | + |
| 66 | + it("should filter by status", async () => { |
| 67 | + const res = await request(createApp()).get( |
| 68 | + `/api/users/${testAddress}/predictions?status=pending&limit=20` |
| 69 | + ); |
| 70 | + expect(res.status).toBe(200); |
| 71 | + expect(res.body.data.every((p: any) => p.status === "pending")).toBe(true); |
| 72 | + }); |
| 73 | + |
| 74 | + it("should handle pagination with cursor", async () => { |
| 75 | + const page1 = await request(createApp()).get( |
| 76 | + `/api/users/${testAddress}/predictions?limit=10` |
| 77 | + ); |
| 78 | + expect(page1.body.nextCursor).toBeDefined(); |
| 79 | + |
| 80 | + const page2 = await request(createApp()).get( |
| 81 | + `/api/users/${testAddress}/predictions?limit=10&cursor=${encodeURIComponent( |
| 82 | + page1.body.nextCursor |
| 83 | + )}` |
| 84 | + ); |
| 85 | + expect(page2.status).toBe(200); |
| 86 | + expect(page2.body.data.length).toBeGreaterThan(0); |
| 87 | + }); |
| 88 | + |
| 89 | + it("should validate address format", async () => { |
| 90 | + const res = await request(createApp()).get( |
| 91 | + "/api/users/invalid-address/predictions" |
| 92 | + ); |
| 93 | + expect(res.status).toBe(400); |
| 94 | + expect(res.body.error.code).toBe("invalid_address"); |
| 95 | + }); |
| 96 | + |
| 97 | + it("should be stable across status changes", async () => { |
| 98 | + // Query all predictions |
| 99 | + const allRes = await request(createApp()).get( |
| 100 | + `/api/users/${testAddress}/predictions?limit=100` |
| 101 | + ); |
| 102 | + |
| 103 | + // Query by status |
| 104 | + const statusRes = await request(createApp()).get( |
| 105 | + `/api/users/${testAddress}/predictions?status=pending&limit=100` |
| 106 | + ); |
| 107 | + |
| 108 | + // Cursor should work consistently |
| 109 | + expect(allRes.body.data).toBeDefined(); |
| 110 | + expect(statusRes.body.data).toBeDefined(); |
| 111 | + }); |
| 112 | +}); |
0 commit comments