|
1 |
| -import { SubgraphDeploymentID } from "@graphprotocol/common-ts"; |
2 |
| -import { SubgraphDependencies, SubgraphManifestResolver } from "../graph-node"; |
3 |
| -import express, { Request, Response } from "express"; |
4 |
| -import { AddressInfo } from "net"; |
5 |
| -import { utils } from "ethers"; |
6 |
| -import { base58 } from "ethers/lib/utils"; |
| 1 | +import { createLogger, SubgraphDeploymentID } from '@graphprotocol/common-ts' |
| 2 | +import { SubgraphDependencies, SubgraphManifestResolver } from '../graph-node' |
| 3 | +import express, { Request, Response } from 'express' |
| 4 | +import { AddressInfo } from 'net' |
| 5 | +import { utils } from 'ethers' |
7 | 6 |
|
8 |
| -const EXAMPLE_VALID_IPFS_HASH = "Qmd9nZKCH8UZU1pBzk7G8ECJr3jX3a2vAf3vowuTwFvrQg" |
9 |
| -const EXAMPLE_NON_MANIFEST_VALID_IPFS_HASH = "QmddQDkcHHM7mGvYrrnoGnQ1q9GdHQfbTvj2mfbyz2Q49K" |
| 7 | +const EXAMPLE_VALID_IPFS_HASH = 'Qmd9nZKCH8UZU1pBzk7G8ECJr3jX3a2vAf3vowuTwFvrQg' |
| 8 | +const EXAMPLE_NON_MANIFEST_VALID_IPFS_HASH = |
| 9 | + 'QmddQDkcHHM7mGvYrrnoGnQ1q9GdHQfbTvj2mfbyz2Q49K' |
10 | 10 |
|
11 | 11 | function mockManifestHash(input: string): string {
|
12 |
| - const utf8Bytes = utils.toUtf8Bytes(input) |
13 |
| - const hash = utils.keccak256(utf8Bytes) // Generate a keccak256 hash of the input |
14 |
| - return new SubgraphDeploymentID(hash).ipfsHash |
| 12 | + const utf8Bytes = utils.toUtf8Bytes(input) |
| 13 | + const hash = utils.keccak256(utf8Bytes) // Generate a keccak256 hash of the input |
| 14 | + return new SubgraphDeploymentID(hash).ipfsHash |
15 | 15 | }
|
16 | 16 |
|
17 |
| -const DEP_ROOT_HASH = mockManifestHash("root") |
18 |
| -const DEP_1 = mockManifestHash("dep2") |
19 |
| -const DEP_2 = mockManifestHash("dep3") |
| 17 | +const DEP_ROOT_HASH = mockManifestHash('root') |
| 18 | +const DEP_1 = mockManifestHash('dep2') |
| 19 | +const DEP_2 = mockManifestHash('dep3') |
20 | 20 |
|
21 | 21 | describe(SubgraphManifestResolver, () => {
|
22 |
| - let ipfs: SubgraphManifestResolver |
23 |
| - let app = express() |
24 |
| - let server: any |
| 22 | + let ipfs: SubgraphManifestResolver |
| 23 | + const app = express() |
25 | 24 |
|
26 |
| - let manifestMap = new Map<string, string>() |
27 |
| - manifestMap.set(EXAMPLE_VALID_IPFS_HASH, ` |
| 25 | + /* eslint-disable @typescript-eslint/no-explicit-any */ |
| 26 | + let server: any |
| 27 | + |
| 28 | + const manifestMap = new Map<string, string>() |
| 29 | + manifestMap.set( |
| 30 | + EXAMPLE_VALID_IPFS_HASH, |
| 31 | + ` |
28 | 32 | specVersion: "0.0.2"
|
29 | 33 | name: "test"
|
30 | 34 | graft:
|
31 | 35 | base: "test"
|
32 | 36 | block: 5
|
33 |
| - `) |
| 37 | + `, |
| 38 | + ) |
34 | 39 |
|
35 |
| - // this example is a real world contract schema |
36 |
| - manifestMap.set(EXAMPLE_NON_MANIFEST_VALID_IPFS_HASH, ` |
| 40 | + // this example is a real world contract schema |
| 41 | + manifestMap.set( |
| 42 | + EXAMPLE_NON_MANIFEST_VALID_IPFS_HASH, |
| 43 | + ` |
37 | 44 | {
|
38 | 45 | "name": "test"
|
39 | 46 | }
|
40 |
| - `) |
| 47 | + `, |
| 48 | + ) |
41 | 49 |
|
42 |
| - manifestMap.set(DEP_ROOT_HASH, ` |
| 50 | + manifestMap.set( |
| 51 | + DEP_ROOT_HASH, |
| 52 | + ` |
43 | 53 | specVersion: "0.0.2"
|
44 | 54 | name: "root"
|
45 | 55 | graft:
|
46 | 56 | base: ${DEP_1}
|
47 | 57 | block: 4
|
48 |
| - `) |
49 |
| - manifestMap.set(DEP_1, ` |
| 58 | + `, |
| 59 | + ) |
| 60 | + manifestMap.set( |
| 61 | + DEP_1, |
| 62 | + ` |
50 | 63 | specVersion: "0.0.2"
|
51 | 64 | name: "dep1"
|
52 | 65 | graft:
|
53 | 66 | base: ${DEP_2}
|
54 | 67 | block: 5
|
55 |
| - `) |
56 |
| - manifestMap.set(DEP_2, ` |
| 68 | + `, |
| 69 | + ) |
| 70 | + manifestMap.set( |
| 71 | + DEP_2, |
| 72 | + ` |
57 | 73 | specVersion: "0.0.2"
|
58 | 74 | name: "dep2"
|
59 |
| - `) |
60 |
| - |
61 |
| - beforeAll(async () => { |
62 |
| - // Mock endpoint for IPFS CID requests |
63 |
| - app.get('/ipfs/:cid', (req: Request, res: Response) => { |
64 |
| - const { cid } = req.params; |
65 |
| - // Example: Respond with different data based on the CID |
66 |
| - if (manifestMap.has(cid)) { |
67 |
| - res.send(manifestMap.get(cid)); |
68 |
| - } else { |
69 |
| - res.status(404).send(); |
70 |
| - } |
71 |
| - }); |
72 |
| - // Start server and bind to a random port |
73 |
| - server = app.listen(0, () => { |
74 |
| - console.log(`Mock server running on ${server.address()}`) |
75 |
| - }); |
| 75 | + `, |
| 76 | + ) |
76 | 77 |
|
77 |
| - const address: AddressInfo = server.address() as AddressInfo |
78 |
| - const serverAddress = `http://localhost:${address.port}`; |
79 |
| - ipfs = new SubgraphManifestResolver(serverAddress); |
| 78 | + beforeAll(async () => { |
| 79 | + // Mock endpoint for IPFS CID requests |
| 80 | + app.get('/ipfs/:cid', (req: Request, res: Response) => { |
| 81 | + const { cid } = req.params |
| 82 | + // Example: Respond with different data based on the CID |
| 83 | + if (manifestMap.has(cid)) { |
| 84 | + res.send(manifestMap.get(cid)) |
| 85 | + } else { |
| 86 | + res.status(404).send() |
| 87 | + } |
| 88 | + }) |
| 89 | + // Start server and bind to a random port |
| 90 | + server = app.listen(0, () => { |
| 91 | + console.log(`Mock server running on ${server.address()}`) |
80 | 92 | })
|
81 | 93 |
|
82 |
| - afterAll(async () => { |
83 |
| - // Shut down the server |
84 |
| - if (server) { |
85 |
| - await new Promise<void>((resolve, reject) => { |
86 |
| - server.close((err: Error | undefined) => { |
87 |
| - if (err) { |
88 |
| - reject(err); |
89 |
| - } else { |
90 |
| - resolve(); |
91 |
| - } |
92 |
| - }); |
93 |
| - }); |
94 |
| - console.log("Mock server shut down"); |
95 |
| - } |
96 |
| - }); |
| 94 | + const address: AddressInfo = server.address() as AddressInfo |
| 95 | + const serverAddress = `http://localhost:${address.port}` |
| 96 | + ipfs = new SubgraphManifestResolver(serverAddress, createLogger({ name: 'test' })) |
| 97 | + }) |
97 | 98 |
|
98 |
| - it("should fetch and parse a valid manifest", async () => { |
99 |
| - const manifest = await ipfs.resolve(new SubgraphDeploymentID(EXAMPLE_VALID_IPFS_HASH)); |
100 |
| - expect(manifest).toEqual({ |
101 |
| - specVersion: "0.0.2", |
102 |
| - name: "test", |
103 |
| - graft: { base: "test", block: 5 } |
104 |
| - }); |
105 |
| - }) |
| 99 | + afterAll(async () => { |
| 100 | + // Shut down the server |
| 101 | + if (server) { |
| 102 | + await new Promise<void>((resolve, reject) => { |
| 103 | + server.close((err: Error | undefined) => { |
| 104 | + if (err) { |
| 105 | + reject(err) |
| 106 | + } else { |
| 107 | + resolve() |
| 108 | + } |
| 109 | + }) |
| 110 | + }) |
| 111 | + console.log('Mock server shut down') |
| 112 | + } |
| 113 | + }) |
106 | 114 |
|
107 |
| - it("should throw an error when fetching an invalid manifest", async () => { |
108 |
| - await expect(ipfs.resolve(new SubgraphDeploymentID(EXAMPLE_NON_MANIFEST_VALID_IPFS_HASH))).rejects.toThrow(); |
| 115 | + it('should fetch and parse a valid manifest', async () => { |
| 116 | + const manifest = await ipfs.resolve(new SubgraphDeploymentID(EXAMPLE_VALID_IPFS_HASH)) |
| 117 | + expect(manifest).toEqual({ |
| 118 | + specVersion: '0.0.2', |
| 119 | + name: 'test', |
| 120 | + graft: { base: 'test', block: 5 }, |
109 | 121 | })
|
| 122 | + }) |
110 | 123 |
|
111 |
| - it("should throw an error when fetching a non-existent manifest", async () => { |
112 |
| - await expect(ipfs.resolve(new SubgraphDeploymentID("QmeDVcAvgYPKFCw2VCqTK3JRexHT8jkgvQ7AJ9WxhuFNM8"))).rejects.toThrow(); |
113 |
| - }) |
| 124 | + it('should throw an error when fetching an invalid manifest', async () => { |
| 125 | + await expect( |
| 126 | + ipfs.resolve(new SubgraphDeploymentID(EXAMPLE_NON_MANIFEST_VALID_IPFS_HASH)), |
| 127 | + ).rejects.toThrow() |
| 128 | + }) |
114 | 129 |
|
115 |
| - it('should resolve dependencies', async () => { |
116 |
| - const manifest: SubgraphDependencies = await ipfs.resolveWithDependencies(new SubgraphDeploymentID(DEP_ROOT_HASH)); |
117 |
| - expect(manifest).toEqual({ |
118 |
| - root: new SubgraphDeploymentID(DEP_ROOT_HASH), |
119 |
| - dependencies: [ |
120 |
| - { base: new SubgraphDeploymentID(DEP_1), block: 4 }, |
121 |
| - { base: new SubgraphDeploymentID(DEP_2), block: 5 } |
122 |
| - ] |
123 |
| - }) |
124 |
| - }) |
| 130 | + it('should throw an error when fetching a non-existent manifest', async () => { |
| 131 | + await expect( |
| 132 | + ipfs.resolve( |
| 133 | + new SubgraphDeploymentID('QmeDVcAvgYPKFCw2VCqTK3JRexHT8jkgvQ7AJ9WxhuFNM8'), |
| 134 | + ), |
| 135 | + ).rejects.toThrow() |
| 136 | + }) |
125 | 137 |
|
| 138 | + it('should resolve dependencies', async () => { |
| 139 | + const manifest: SubgraphDependencies = await ipfs.resolveWithDependencies( |
| 140 | + new SubgraphDeploymentID(DEP_ROOT_HASH), |
| 141 | + ) |
| 142 | + expect(manifest).toEqual({ |
| 143 | + root: new SubgraphDeploymentID(DEP_ROOT_HASH), |
| 144 | + dependencies: [ |
| 145 | + { base: new SubgraphDeploymentID(DEP_1), block: 4 }, |
| 146 | + { base: new SubgraphDeploymentID(DEP_2), block: 5 }, |
| 147 | + ], |
| 148 | + }) |
| 149 | + }) |
126 | 150 | })
|
0 commit comments