|
1 | 1 | import {expect} from "aegir/chai"; |
2 | | -import {resolve, unixfsPathSelector, getPeer} from "../src/resolver.js"; |
| 2 | +import { |
| 3 | + resolve, |
| 4 | + unixfsPathSelector, |
| 5 | + getPeer, |
| 6 | + resolveQuery, |
| 7 | +} from "../src/resolver.js"; |
| 8 | +import { |
| 9 | + selectorBuilder as sb, |
| 10 | + BasicNode, |
| 11 | + parseContext, |
| 12 | + LinkSystem, |
| 13 | +} from "../src/traversal.js"; |
3 | 14 | import {MemoryBlockstore} from "blockstore-core/memory"; |
4 | 15 | import {MockLibp2p, concatChunkIterator} from "./mock-libp2p.js"; |
5 | 16 | import {peerIdFromString} from "@libp2p/peer-id"; |
6 | 17 | import {GraphSync} from "../src/graphsync.js"; |
7 | 18 | import {importer} from "ipfs-unixfs-importer"; |
| 19 | +import {encode} from "multiformats/block"; |
| 20 | +import * as codec from "@ipld/dag-cbor"; |
| 21 | +import {sha256 as hasher} from "multiformats/hashes/sha2"; |
8 | 22 |
|
9 | 23 | describe("resolver", () => { |
10 | 24 | it("parse a unixfs path", () => { |
@@ -68,4 +82,74 @@ describe("resolver", () => { |
68 | 82 | ); |
69 | 83 | expect(multiaddrs[0].protos()[0].name).to.equal("ip4"); |
70 | 84 | }); |
| 85 | + |
| 86 | + it("resolves a query", async () => { |
| 87 | + const blocks = new MemoryBlockstore(); |
| 88 | + |
| 89 | + const account1 = { |
| 90 | + balance: 300, |
| 91 | + lastUpdated: "yesterday", |
| 92 | + }; |
| 93 | + const account1Block = await encode({value: account1, codec, hasher}); |
| 94 | + await blocks.put(account1Block.cid, account1Block.bytes); |
| 95 | + |
| 96 | + const account2 = { |
| 97 | + balance: 100, |
| 98 | + lastUpdated: "now", |
| 99 | + }; |
| 100 | + const account2Block = await encode({value: account2, codec, hasher}); |
| 101 | + await blocks.put(account2Block.cid, account2Block.bytes); |
| 102 | + |
| 103 | + const state = { |
| 104 | + "0x01": account1Block.cid, |
| 105 | + "0x02": account2Block.cid, |
| 106 | + }; |
| 107 | + const stateBlock = await encode({value: state, codec, hasher}); |
| 108 | + await blocks.put(stateBlock.cid, stateBlock.bytes); |
| 109 | + |
| 110 | + const msg = { |
| 111 | + from: "0x01", |
| 112 | + to: "0x02", |
| 113 | + amount: 100, |
| 114 | + }; |
| 115 | + const msgBlock = await encode({value: msg, codec, hasher}); |
| 116 | + await blocks.put(msgBlock.cid, msgBlock.bytes); |
| 117 | + |
| 118 | + const root = { |
| 119 | + state: stateBlock.cid, |
| 120 | + epoch: Date.now(), |
| 121 | + messages: [msgBlock.cid], |
| 122 | + }; |
| 123 | + const rootBlock = await encode({value: root, codec, hasher}); |
| 124 | + await blocks.put(rootBlock.cid, rootBlock.bytes); |
| 125 | + |
| 126 | + const selector = sb.exploreFields({ |
| 127 | + state: sb.exploreFields({ |
| 128 | + "0x02": sb.exploreFields({ |
| 129 | + balance: sb.match(), |
| 130 | + lastUpdated: sb.match(), |
| 131 | + }), |
| 132 | + }), |
| 133 | + messages: sb.exploreIndex( |
| 134 | + 0, |
| 135 | + sb.exploreFields({ |
| 136 | + amount: sb.match(), |
| 137 | + }) |
| 138 | + ), |
| 139 | + }); |
| 140 | + const sel = parseContext().parseSelector(selector); |
| 141 | + const ls = new LinkSystem(blocks); |
| 142 | + |
| 143 | + const result = await resolveQuery(new BasicNode(root), sel, ls); |
| 144 | + |
| 145 | + expect(result).to.deep.equal({ |
| 146 | + state: { |
| 147 | + "0x02": { |
| 148 | + balance: 100, |
| 149 | + lastUpdated: "now", |
| 150 | + }, |
| 151 | + }, |
| 152 | + messages: [{amount: 100}], |
| 153 | + }); |
| 154 | + }); |
71 | 155 | }); |
0 commit comments