|
| 1 | +import { afterEach, describe, expect, it, jest } from "@jest/globals"; |
| 2 | +import { |
| 3 | + createProposalMetadataFallback, |
| 4 | + fetchProposalMetadataWithFallback, |
| 5 | + getAnchorUrls, |
| 6 | + normalizeProposalMetadata, |
| 7 | +} from "@/lib/governance/proposalMetadata"; |
| 8 | + |
| 9 | +const proposal = { |
| 10 | + tx_hash: "tx-proposal", |
| 11 | + cert_index: 0, |
| 12 | + governance_type: "info_action", |
| 13 | +}; |
| 14 | + |
| 15 | +afterEach(() => { |
| 16 | + jest.restoreAllMocks(); |
| 17 | +}); |
| 18 | + |
| 19 | +describe("proposal metadata helpers", () => { |
| 20 | + it("normalizes usable Blockfrost metadata without extra fetching", async () => { |
| 21 | + const provider = { |
| 22 | + get: jest.fn(async () => ({ |
| 23 | + tx_hash: "tx-proposal", |
| 24 | + cert_index: 0, |
| 25 | + hash: "hash", |
| 26 | + url: "https://example.com/metadata.json", |
| 27 | + bytes: "123", |
| 28 | + json_metadata: { |
| 29 | + body: { |
| 30 | + title: "Proposal title", |
| 31 | + abstract: "Proposal abstract", |
| 32 | + motivation: "Motivation", |
| 33 | + rationale: "Rationale", |
| 34 | + references: [{ "@type": "Other", label: "Spec", uri: "https://example.com" }], |
| 35 | + }, |
| 36 | + authors: [{ name: "Ada" }], |
| 37 | + }, |
| 38 | + })), |
| 39 | + }; |
| 40 | + |
| 41 | + const metadata = await fetchProposalMetadataWithFallback({ provider, proposal }); |
| 42 | + |
| 43 | + expect(provider.get).toHaveBeenCalledTimes(1); |
| 44 | + expect(metadata).toMatchObject({ |
| 45 | + tx_hash: "tx-proposal", |
| 46 | + cert_index: 0, |
| 47 | + governance_type: "info_action", |
| 48 | + hash: "hash", |
| 49 | + json_metadata: { |
| 50 | + body: { |
| 51 | + title: "Proposal title", |
| 52 | + abstract: "Proposal abstract", |
| 53 | + motivation: "Motivation", |
| 54 | + rationale: "Rationale", |
| 55 | + }, |
| 56 | + authors: [{ name: "Ada" }], |
| 57 | + }, |
| 58 | + }); |
| 59 | + }); |
| 60 | + |
| 61 | + it("hydrates metadata from a regular anchor URL", async () => { |
| 62 | + const provider = { |
| 63 | + get: jest.fn(async () => ({ |
| 64 | + tx_hash: "tx-proposal", |
| 65 | + cert_index: 0, |
| 66 | + url: "https://example.com/anchor.json", |
| 67 | + })), |
| 68 | + }; |
| 69 | + const fetchSpy = jest.spyOn(globalThis, "fetch").mockResolvedValue( |
| 70 | + new Response( |
| 71 | + JSON.stringify({ |
| 72 | + body: { |
| 73 | + title: "Anchor title", |
| 74 | + abstract: "Anchor abstract", |
| 75 | + }, |
| 76 | + authors: [{ name: "Anchor author" }], |
| 77 | + }), |
| 78 | + { status: 200 }, |
| 79 | + ), |
| 80 | + ); |
| 81 | + |
| 82 | + const metadata = await fetchProposalMetadataWithFallback({ provider, proposal }); |
| 83 | + |
| 84 | + expect(fetchSpy).toHaveBeenCalledWith( |
| 85 | + "https://example.com/anchor.json", |
| 86 | + expect.objectContaining({ method: "GET" }), |
| 87 | + ); |
| 88 | + expect(metadata?.json_metadata.body.title).toBe("Anchor title"); |
| 89 | + expect(metadata?.json_metadata.authors).toEqual([{ name: "Anchor author" }]); |
| 90 | + }); |
| 91 | + |
| 92 | + it("tries IPFS gateway fallbacks until one returns usable JSON", async () => { |
| 93 | + expect(getAnchorUrls("ipfs://cid/path.json")).toEqual([ |
| 94 | + "https://ipfs.io/ipfs/cid/path.json", |
| 95 | + "https://cloudflare-ipfs.com/ipfs/cid/path.json", |
| 96 | + "https://dweb.link/ipfs/cid/path.json", |
| 97 | + ]); |
| 98 | + |
| 99 | + const provider = { |
| 100 | + get: jest.fn(async () => ({ |
| 101 | + tx_hash: "tx-proposal", |
| 102 | + cert_index: 0, |
| 103 | + url: "ipfs://cid/path.json", |
| 104 | + })), |
| 105 | + }; |
| 106 | + const fetchSpy = jest |
| 107 | + .spyOn(globalThis, "fetch") |
| 108 | + .mockResolvedValueOnce(new Response("not found", { status: 504 })) |
| 109 | + .mockResolvedValueOnce( |
| 110 | + new Response(JSON.stringify({ title: "Wrapped anchor title" }), { |
| 111 | + status: 200, |
| 112 | + }), |
| 113 | + ); |
| 114 | + |
| 115 | + const metadata = await fetchProposalMetadataWithFallback({ provider, proposal }); |
| 116 | + |
| 117 | + expect(fetchSpy).toHaveBeenCalledTimes(2); |
| 118 | + expect(metadata?.json_metadata.body.title).toBe("Wrapped anchor title"); |
| 119 | + }); |
| 120 | + |
| 121 | + it("returns fallback metadata without changing the ProposalMetadata shape", () => { |
| 122 | + const metadata = createProposalMetadataFallback(proposal); |
| 123 | + |
| 124 | + expect(metadata).toEqual({ |
| 125 | + tx_hash: "tx-proposal", |
| 126 | + cert_index: 0, |
| 127 | + governance_type: "info_action", |
| 128 | + hash: "", |
| 129 | + url: "", |
| 130 | + bytes: "", |
| 131 | + json_metadata: { |
| 132 | + body: { |
| 133 | + title: "Metadata could not be loaded.", |
| 134 | + abstract: "tx-proposal#0", |
| 135 | + motivation: "", |
| 136 | + rationale: "", |
| 137 | + references: [], |
| 138 | + }, |
| 139 | + authors: [], |
| 140 | + }, |
| 141 | + }); |
| 142 | + }); |
| 143 | + |
| 144 | + it("normalizes missing or unusable metadata fields into safe defaults", () => { |
| 145 | + const metadata = normalizeProposalMetadata({ json_metadata: { body: null } }, proposal); |
| 146 | + |
| 147 | + expect(metadata).toMatchObject({ |
| 148 | + tx_hash: "tx-proposal", |
| 149 | + cert_index: 0, |
| 150 | + governance_type: "info_action", |
| 151 | + json_metadata: { |
| 152 | + body: { |
| 153 | + title: "Metadata could not be loaded.", |
| 154 | + abstract: "tx-proposal#0", |
| 155 | + motivation: "", |
| 156 | + rationale: "", |
| 157 | + references: [], |
| 158 | + }, |
| 159 | + authors: [], |
| 160 | + }, |
| 161 | + }); |
| 162 | + }); |
| 163 | +}); |
0 commit comments