-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathrefresh.test.ts
180 lines (154 loc) · 4.56 KB
/
refresh.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import test from "ava";
import { spy } from "sinon";
import {
createLatestTypeIdResolver,
createRpcResolver,
FetchOutputsByTxHashes,
refreshScriptConfigs,
} from "../src/refresh";
import { hexify } from "@ckb-lumos/codec/lib/bytes";
import { OutPoint, Output, Script } from "@ckb-lumos/base";
import { randomBytes } from "@ckb-lumos/crypto";
import { ScriptConfigs } from "../src";
test("refresh without update", async (t) => {
const scriptConfigs: ScriptConfigs = {
A: {
CODE_HASH: randomHash(),
TX_HASH: randomHash(),
HASH_TYPE: "type",
DEP_TYPE: "code",
INDEX: "0x1",
},
B: {
CODE_HASH: randomHash(),
TX_HASH: randomHash(),
HASH_TYPE: "type",
DEP_TYPE: "code",
INDEX: "0x1",
},
};
const refreshed1 = await refreshScriptConfigs(scriptConfigs, {
resolve: (outPoints) => outPoints,
});
t.deepEqual(refreshed1, scriptConfigs);
});
test("refresh with skip", async (t) => {
const scriptConfigs: ScriptConfigs = {
A: {
CODE_HASH: randomHash(),
TX_HASH: randomHash(),
HASH_TYPE: "type",
DEP_TYPE: "code",
INDEX: "0x1",
},
B: {
CODE_HASH: randomHash(),
TX_HASH: randomHash(),
HASH_TYPE: "type",
DEP_TYPE: "code",
INDEX: "0x1",
},
};
const refreshed2 = await refreshScriptConfigs(scriptConfigs, {
resolve: async (outPoints) =>
outPoints.map(() => ({ txHash: randomHash(), index: "0x0" })),
skip: ["B"],
});
t.notDeepEqual(scriptConfigs.A, refreshed2.A);
t.deepEqual(scriptConfigs.B, refreshed2.B);
});
test("resolve empty outpoints should be empty", async (t) => {
const resolve = createLatestTypeIdResolver(
(hashes) => hashes.map(() => ({ outputs: [randomOutput()] })),
(scripts) => scripts.map(() => ({ outPoint: randomOutPoint() }))
);
t.deepEqual([], await resolve([]));
});
test("LatestTypeIdResolver should work as expected", async (t) => {
const originalOutPoint = randomOutPoint();
const originalOutput = randomOutput();
const latestOutPoint = randomOutPoint();
const fetchOutputsByTxHashes = spy((hashes) => {
return hashes.map(() => ({ outputs: [originalOutput] }));
});
const fetchLatestTypeIdOutPoints = spy((scripts) =>
scripts.map(() => ({ outPoint: latestOutPoint }))
);
const resolve = createLatestTypeIdResolver(
fetchOutputsByTxHashes,
fetchLatestTypeIdOutPoints
);
const resolved = await resolve([originalOutPoint]);
t.true(fetchOutputsByTxHashes.calledWith([originalOutPoint.txHash]));
t.true(fetchLatestTypeIdOutPoints.calledWith([originalOutput.type]));
t.deepEqual(resolved, [latestOutPoint]);
});
test("should resolve as original OutPoint if type script is empty", async (t) => {
const originalOutPoint = randomOutPoint();
const fetchOutputsByTxHashes = spy(((hashes: string[]) =>
hashes.map(() => ({
outputs: [{ capacity: "0x0", lock: randomScript() }],
}))) satisfies FetchOutputsByTxHashes);
const resolve = createLatestTypeIdResolver(fetchOutputsByTxHashes, () => []);
const resolved = await resolve([originalOutPoint]);
t.deepEqual(resolved, [originalOutPoint]);
});
test("RPCResolver should work as expected", async (t) => {
const oldOutPoint = randomOutPoint();
const newOutPoint = randomOutPoint();
const typeId = randomScript();
const batchRequest = spy((args: any[]) => ({
exec: async (): Promise<any[]> => {
if (args[0][0] === "getTransaction") {
return [{ transaction: { outputs: [{ type: typeId }] } }];
}
if (args[0][0] === "getCells") {
return [{ objects: [{ outPoint: newOutPoint }] }];
}
throw new Error("Unreachable");
},
}));
const resolve = createRpcResolver({
createBatchRequest: batchRequest,
});
await resolve([oldOutPoint]);
t.true(batchRequest.calledWith([["getTransaction", oldOutPoint.txHash]]));
t.true(
batchRequest.calledWith([
[
"getCells",
{
script: typeId,
scriptType: "type",
scriptSearchMode: "exact",
withData: false,
},
"asc",
"0x1",
],
])
);
});
function randomOutPoint(): OutPoint {
return { txHash: randomHash(), index: "0x0" };
}
function randomOutput(): Output {
return {
capacity: randomHex(8),
type: randomScript(),
lock: randomScript(),
};
}
function randomScript(): Script {
return {
codeHash: randomHash(),
hashType: "type",
args: hexify(randomBytes(20)),
};
}
function randomHash() {
return randomHex(32);
}
function randomHex(size: number) {
return hexify(randomBytes(size));
}