|
| 1 | +import { describe, it, expect, vi, afterEach } from "vitest"; |
| 2 | +import type { VercelRequest, VercelResponse } from "@vercel/node"; |
| 3 | + |
| 4 | +vi.mock("@meridian/stellar-sdk-helpers", async (importOriginal) => { |
| 5 | + const actual = |
| 6 | + await importOriginal<typeof import("@meridian/stellar-sdk-helpers")>(); |
| 7 | + return { |
| 8 | + ...actual, |
| 9 | + // The header under test doesn't depend on vault contents, and the real |
| 10 | + // fetch would reach Stellar RPC / DeFiLlama from a unit test. |
| 11 | + fetchAllVaults: vi.fn(async () => [ |
| 12 | + { |
| 13 | + id: "meridian-usdc", |
| 14 | + protocol: "meridian", |
| 15 | + asset: "USDC", |
| 16 | + name: "Meridian", |
| 17 | + label: "USDC Vault", |
| 18 | + apy: 5, |
| 19 | + tvl: 1000, |
| 20 | + userBalance: 0, |
| 21 | + riskLevel: "safe", |
| 22 | + }, |
| 23 | + ]), |
| 24 | + }; |
| 25 | +}); |
| 26 | + |
| 27 | +const fakeReq = (obj: object) => |
| 28 | + ({ headers: {}, ...obj }) as unknown as VercelRequest; |
| 29 | + |
| 30 | +interface FakeRes { |
| 31 | + statusCode: number; |
| 32 | + body: unknown; |
| 33 | + headers: Record<string, string>; |
| 34 | + status(code: number): FakeRes; |
| 35 | + json(payload: unknown): FakeRes; |
| 36 | + setHeader(key: string, value: string): void; |
| 37 | +} |
| 38 | + |
| 39 | +function makeRes(): FakeRes & VercelResponse { |
| 40 | + const r: FakeRes = { |
| 41 | + statusCode: 200, |
| 42 | + body: undefined, |
| 43 | + headers: {}, |
| 44 | + status(code: number) { |
| 45 | + r.statusCode = code; |
| 46 | + return r; |
| 47 | + }, |
| 48 | + json(payload: unknown) { |
| 49 | + r.body = payload; |
| 50 | + return r; |
| 51 | + }, |
| 52 | + setHeader(key: string, value: string) { |
| 53 | + r.headers[key] = value; |
| 54 | + }, |
| 55 | + }; |
| 56 | + return r as unknown as FakeRes & VercelResponse; |
| 57 | +} |
| 58 | + |
| 59 | +const MAINNET_CACHE_HEADER = "public, s-maxage=60, stale-while-revalidate=300"; |
| 60 | + |
| 61 | +// APP_NETWORK is resolved from STELLAR_NETWORK once, at @meridian/shared |
| 62 | +// module load, so each network case needs a fresh module graph rather than a |
| 63 | +// mutated constant. |
| 64 | +const savedNetwork = process.env.STELLAR_NETWORK; |
| 65 | + |
| 66 | +afterEach(() => { |
| 67 | + if (savedNetwork === undefined) delete process.env.STELLAR_NETWORK; |
| 68 | + else process.env.STELLAR_NETWORK = savedNetwork; |
| 69 | + vi.resetModules(); |
| 70 | +}); |
| 71 | + |
| 72 | +describe("vaultReadCacheControl", () => { |
| 73 | + it("caches mainnet vault reads at the CDN", async () => { |
| 74 | + delete process.env.STELLAR_NETWORK; |
| 75 | + vi.resetModules(); |
| 76 | + const { vaultReadCacheControl } = await import("../_lib/middleware.js"); |
| 77 | + expect(vaultReadCacheControl()).toBe(MAINNET_CACHE_HEADER); |
| 78 | + }); |
| 79 | + |
| 80 | + it("serves testnet vault reads uncached", async () => { |
| 81 | + process.env.STELLAR_NETWORK = "testnet"; |
| 82 | + vi.resetModules(); |
| 83 | + const { vaultReadCacheControl } = await import("../_lib/middleware.js"); |
| 84 | + expect(vaultReadCacheControl()).toBe("no-store"); |
| 85 | + }); |
| 86 | +}); |
| 87 | + |
| 88 | +// #1051: the detail endpoint used to answer with |
| 89 | +// `public, s-maxage=60, stale-while-revalidate=300` on every network, so a |
| 90 | +// testnet deposit was invisible behind the CDN for up to a minute while the |
| 91 | +// list endpoint (already no-store on testnet) showed the new balance. |
| 92 | +describe("GET /api/v1/vaults/:vaultId on testnet (#1051)", () => { |
| 93 | + it("serves a 200 single-vault read with no-store", async () => { |
| 94 | + process.env.STELLAR_NETWORK = "testnet"; |
| 95 | + vi.resetModules(); |
| 96 | + const { default: handler } = await import("../v1/vaults/[vaultId]"); |
| 97 | + const res = makeRes(); |
| 98 | + await handler( |
| 99 | + fakeReq({ method: "GET", query: { vaultId: "meridian-usdc" } }), |
| 100 | + res |
| 101 | + ); |
| 102 | + expect(res.statusCode).toBe(200); |
| 103 | + expect(res.headers["Cache-Control"]).toBe("no-store"); |
| 104 | + }); |
| 105 | +}); |
0 commit comments