|
| 1 | +import { describe, expect, test } from "bun:test"; |
| 2 | +import { Hono } from "hono"; |
| 3 | +import { errorHandler } from "../src/middleware/error.ts"; |
| 4 | +import subgraphsRouter from "../src/routes/subgraphs.ts"; |
| 5 | + |
| 6 | +/** |
| 7 | + * Integration tests for POST /api/subgraphs/bundle — the server-side bundler |
| 8 | + * that powers the web chat authoring loop for subgraphs. Mirrors |
| 9 | + * workflows-bundle.test.ts: mount the router behind a tiny stand-in middleware |
| 10 | + * so `getApiKeyId(c)` succeeds without touching the real auth stack. |
| 11 | + */ |
| 12 | + |
| 13 | +type TestVariables = { |
| 14 | + apiKey: { id: string }; |
| 15 | + accountId: string; |
| 16 | +}; |
| 17 | + |
| 18 | +function buildApp() { |
| 19 | + const app = new Hono<{ Variables: TestVariables }>(); |
| 20 | + app.onError(errorHandler); |
| 21 | + app.use("/subgraphs/*", async (c, next) => { |
| 22 | + c.set("apiKey", { id: "test-key-subgraphs-bundle" }); |
| 23 | + c.set("accountId", "test-account-subgraphs-bundle"); |
| 24 | + await next(); |
| 25 | + }); |
| 26 | + app.route("/subgraphs", subgraphsRouter); |
| 27 | + return app; |
| 28 | +} |
| 29 | + |
| 30 | +const validSource = ` |
| 31 | +import { defineSubgraph } from "@secondlayer/subgraphs"; |
| 32 | +export default defineSubgraph({ |
| 33 | + name: "bundle-test", |
| 34 | + sources: { |
| 35 | + swap: { |
| 36 | + type: "print_event", |
| 37 | + contractId: "SP102V8P0F7JX67ARQ77WEA3D3CFB5XW39REDT0AM.amm-pool-v2-01", |
| 38 | + topic: "swap", |
| 39 | + }, |
| 40 | + }, |
| 41 | + schema: { |
| 42 | + swaps: { |
| 43 | + columns: { |
| 44 | + sender: { type: "principal", indexed: true }, |
| 45 | + amount: { type: "uint" }, |
| 46 | + }, |
| 47 | + }, |
| 48 | + }, |
| 49 | + handlers: { |
| 50 | + swap: (_event, ctx) => { |
| 51 | + ctx.insert("swaps", { sender: ctx.tx.sender, amount: 0 }); |
| 52 | + }, |
| 53 | + }, |
| 54 | +}); |
| 55 | +`; |
| 56 | + |
| 57 | +describe("POST /api/subgraphs/bundle", () => { |
| 58 | + test("happy path: valid source returns bundled handler + metadata", async () => { |
| 59 | + const app = buildApp(); |
| 60 | + const res = await app.request("/subgraphs/bundle", { |
| 61 | + method: "POST", |
| 62 | + headers: { "Content-Type": "application/json" }, |
| 63 | + body: JSON.stringify({ code: validSource }), |
| 64 | + }); |
| 65 | + expect(res.status).toBe(200); |
| 66 | + const body = (await res.json()) as { |
| 67 | + ok: boolean; |
| 68 | + name: string; |
| 69 | + handlerCode: string; |
| 70 | + sourceCode: string; |
| 71 | + bundleSize: number; |
| 72 | + sources: Record<string, unknown>; |
| 73 | + schema: Record<string, unknown>; |
| 74 | + }; |
| 75 | + expect(body.ok).toBe(true); |
| 76 | + expect(body.name).toBe("bundle-test"); |
| 77 | + expect(body.handlerCode.length).toBeGreaterThan(0); |
| 78 | + expect(body.sourceCode).toBe(validSource); |
| 79 | + expect(body.bundleSize).toBeGreaterThan(0); |
| 80 | + expect(Object.keys(body.sources).length).toBeGreaterThan(0); |
| 81 | + expect(Object.keys(body.schema).length).toBeGreaterThan(0); |
| 82 | + }); |
| 83 | + |
| 84 | + test("missing body returns 400", async () => { |
| 85 | + const app = buildApp(); |
| 86 | + const res = await app.request("/subgraphs/bundle", { |
| 87 | + method: "POST", |
| 88 | + headers: { "Content-Type": "application/json" }, |
| 89 | + body: JSON.stringify({}), |
| 90 | + }); |
| 91 | + expect(res.status).toBe(400); |
| 92 | + const body = (await res.json()) as { error: string }; |
| 93 | + expect(body.error).toMatch(/code/); |
| 94 | + }); |
| 95 | + |
| 96 | + test("invalid JSON body returns 400", async () => { |
| 97 | + const app = buildApp(); |
| 98 | + const res = await app.request("/subgraphs/bundle", { |
| 99 | + method: "POST", |
| 100 | + headers: { "Content-Type": "application/json" }, |
| 101 | + body: "not json", |
| 102 | + }); |
| 103 | + expect(res.status).toBe(400); |
| 104 | + }); |
| 105 | + |
| 106 | + test("oversized bundle returns 413 with actualBytes/maxBytes", async () => { |
| 107 | + const app = buildApp(); |
| 108 | + const oversized = ` |
| 109 | +import { defineSubgraph } from "@secondlayer/subgraphs"; |
| 110 | +const BIG = ${JSON.stringify("x".repeat(5_000_000))}; |
| 111 | +export default defineSubgraph({ |
| 112 | + name: "too-big", |
| 113 | + sources: { |
| 114 | + swap: { |
| 115 | + type: "print_event", |
| 116 | + contractId: "SP102V8P0F7JX67ARQ77WEA3D3CFB5XW39REDT0AM.amm-pool-v2-01", |
| 117 | + topic: "swap", |
| 118 | + }, |
| 119 | + }, |
| 120 | + schema: { swaps: { columns: { amount: { type: "uint" } } } }, |
| 121 | + handlers: { |
| 122 | + swap: (_event, ctx) => { |
| 123 | + ctx.insert("swaps", { amount: BIG.length }); |
| 124 | + }, |
| 125 | + }, |
| 126 | +}); |
| 127 | +`; |
| 128 | + const res = await app.request("/subgraphs/bundle", { |
| 129 | + method: "POST", |
| 130 | + headers: { "Content-Type": "application/json" }, |
| 131 | + body: JSON.stringify({ code: oversized }), |
| 132 | + }); |
| 133 | + expect(res.status).toBe(413); |
| 134 | + const body = (await res.json()) as { |
| 135 | + ok: boolean; |
| 136 | + code: string; |
| 137 | + actualBytes: number; |
| 138 | + maxBytes: number; |
| 139 | + }; |
| 140 | + expect(body.ok).toBe(false); |
| 141 | + expect(body.code).toBe("BUNDLE_TOO_LARGE"); |
| 142 | + expect(body.actualBytes).toBeGreaterThan(body.maxBytes); |
| 143 | + }); |
| 144 | + |
| 145 | + test("malformed TS returns 400 with BUNDLE_FAILED", async () => { |
| 146 | + const app = buildApp(); |
| 147 | + const res = await app.request("/subgraphs/bundle", { |
| 148 | + method: "POST", |
| 149 | + headers: { "Content-Type": "application/json" }, |
| 150 | + body: JSON.stringify({ code: "@@@ not valid typescript !!!" }), |
| 151 | + }); |
| 152 | + expect(res.status).toBe(400); |
| 153 | + const body = (await res.json()) as { ok: boolean; code: string }; |
| 154 | + expect(body.ok).toBe(false); |
| 155 | + expect(body.code).toBe("BUNDLE_FAILED"); |
| 156 | + }); |
| 157 | +}); |
0 commit comments