Skip to content

Commit 0d38e4c

Browse files
committed
test(type-id): improve test cases
1 parent 9dc0115 commit 0d38e4c

File tree

2 files changed

+20
-107
lines changed

2 files changed

+20
-107
lines changed

packages/type-id/src/advancedBarrel.test.ts

Lines changed: 19 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable @typescript-eslint/unbound-method */
12
import { ccc } from "@ckb-ccc/core";
23
import { beforeEach, describe, expect, it, Mock, vi } from "vitest";
34
import { buildTypeIdOperations } from "./advancedBarrel.js";
@@ -21,7 +22,11 @@ describe("type-id", () => {
2122
depType: "code" as const,
2223
};
2324

25+
const mockTypeId = ccc.hexFrom("1".repeat(64));
26+
2427
beforeEach(() => {
28+
vi.spyOn(ccc, "hashTypeId").mockReturnValue(mockTypeId);
29+
2530
client = {
2631
getKnownScript: vi.fn(),
2732
getCellDeps: vi.fn(),
@@ -31,7 +36,17 @@ describe("type-id", () => {
3136
signer = {
3237
client,
3338
getRecommendedAddressObj: vi.fn(),
34-
findCells: vi.fn(),
39+
completeInputsAtLeastOne: vi
40+
.fn()
41+
.mockImplementation(async (tx: ccc.Transaction) => {
42+
tx.addInput({
43+
previousOutput: {
44+
txHash: "2".repeat(64),
45+
index: 0,
46+
},
47+
});
48+
return { tx, addedCount: 1 };
49+
}),
3550
} as unknown as ccc.Signer;
3651

3752
(client.getKnownScript as Mock).mockResolvedValue({
@@ -68,22 +83,6 @@ describe("type-id", () => {
6883
codec: customCodec,
6984
});
7085

71-
const inputCell = ccc.Cell.from({
72-
outPoint: { txHash: "0x" + "2".repeat(64), index: 0 },
73-
cellOutput: {
74-
capacity: ccc.fixedPointFrom(1000),
75-
lock: ccc.Script.from({
76-
codeHash: "0x" + "0".repeat(64),
77-
hashType: "type",
78-
args: "0x",
79-
}),
80-
},
81-
outputData: "0x",
82-
});
83-
(signer.findCells as Mock).mockImplementation(async function* () {
84-
yield inputCell;
85-
});
86-
8786
const { tx } = await create({
8887
signer,
8988
data: 123456,
@@ -104,22 +103,6 @@ describe("type-id", () => {
104103
calculateTypeId,
105104
});
106105

107-
const inputCell = ccc.Cell.from({
108-
outPoint: { txHash: "0x" + "2".repeat(64), index: 0 },
109-
cellOutput: {
110-
capacity: ccc.fixedPointFrom(1000),
111-
lock: ccc.Script.from({
112-
codeHash: "0x" + "0".repeat(64),
113-
hashType: "type",
114-
args: "0x",
115-
}),
116-
},
117-
outputData: "0x",
118-
});
119-
(signer.findCells as Mock).mockImplementation(async function* () {
120-
yield inputCell;
121-
});
122-
123106
const { id, tx } = await create({
124107
signer,
125108
data: "0x",
@@ -150,22 +133,6 @@ describe("type-id", () => {
150133
addCellDeps,
151134
});
152135

153-
const inputCell = ccc.Cell.from({
154-
outPoint: { txHash: "0x" + "2".repeat(64), index: 0 },
155-
cellOutput: {
156-
capacity: ccc.fixedPointFrom(1000),
157-
lock: ccc.Script.from({
158-
codeHash: "0x" + "0".repeat(64),
159-
hashType: "type",
160-
args: "0x",
161-
}),
162-
},
163-
outputData: "0x",
164-
});
165-
(signer.findCells as Mock).mockImplementation(async function* () {
166-
yield inputCell;
167-
});
168-
169136
const { tx } = await create({
170137
signer,
171138
data: "0x",
@@ -186,41 +153,19 @@ describe("type-id", () => {
186153

187154
describe("create", () => {
188155
it("should create a transaction with correct type id", async () => {
189-
const inputCell = ccc.Cell.from({
190-
outPoint: {
191-
txHash: "0x" + "2".repeat(64),
192-
index: 0,
193-
},
194-
cellOutput: {
195-
capacity: ccc.fixedPointFrom(1000),
196-
lock: ccc.Script.from({
197-
codeHash: "0x" + "0".repeat(64),
198-
hashType: "type",
199-
args: "0x",
200-
}),
201-
},
202-
outputData: "0x",
203-
});
204-
205-
(signer.findCells as Mock).mockImplementation(async function* () {
206-
yield inputCell;
207-
});
208-
209156
const data = "0x1234";
210157
const { tx, id, index } = await create({
211158
signer,
212159
data,
213160
});
214161

215-
expect(tx.inputs.length).toBe(1);
216-
expect(tx.inputs[0].previousOutput).toEqual(inputCell.outPoint);
162+
expect(signer.completeInputsAtLeastOne).toHaveBeenCalledWith(tx);
163+
expect(ccc.hashTypeId).toHaveBeenCalled();
164+
expect(id).toBe(mockTypeId);
217165

218166
expect(tx.outputs.length).toBe(1);
219167
expect(index).toBe(0);
220168

221-
const expectedId = ccc.hashTypeId(tx.inputs[0], 0);
222-
expect(id).toBe(expectedId);
223-
224169
const output = tx.outputs[0];
225170
expect(output.type).toBeDefined();
226171
expect(output.type?.codeHash).toBe(typeIdScript.codeHash);
@@ -239,22 +184,6 @@ describe("type-id", () => {
239184
headerDeps: ["0x" + "e".repeat(64)],
240185
});
241186

242-
const inputCell = ccc.Cell.from({
243-
outPoint: { txHash: "0x" + "2".repeat(64), index: 0 },
244-
cellOutput: {
245-
capacity: ccc.fixedPointFrom(1000),
246-
lock: ccc.Script.from({
247-
codeHash: "0x" + "0".repeat(64),
248-
hashType: "type",
249-
args: "0x",
250-
}),
251-
},
252-
outputData: "0x",
253-
});
254-
(signer.findCells as Mock).mockImplementation(async function* () {
255-
yield inputCell;
256-
});
257-
258187
const { tx } = await create({
259188
signer,
260189
data: "0x",
@@ -272,22 +201,6 @@ describe("type-id", () => {
272201
args: "0xffee",
273202
});
274203

275-
const inputCell = ccc.Cell.from({
276-
outPoint: { txHash: "0x" + "2".repeat(64), index: 0 },
277-
cellOutput: {
278-
capacity: ccc.fixedPointFrom(1000),
279-
lock: ccc.Script.from({
280-
codeHash: "0x" + "0".repeat(64),
281-
hashType: "type",
282-
args: "0x",
283-
}),
284-
},
285-
outputData: "0x",
286-
});
287-
(signer.findCells as Mock).mockImplementation(async function* () {
288-
yield inputCell;
289-
});
290-
291204
const { tx } = await create({
292205
signer,
293206
data: "0x",

packages/type-id/src/advancedBarrel.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export function buildTypeIdOperations<
9393
const { signer, receiver, data, tx: txLike } = props;
9494
const tx = ccc.Transaction.from(txLike ?? {});
9595

96-
await tx.completeInputsAtLeastOne(signer);
96+
await signer.completeInputsAtLeastOne(tx);
9797
const id = await calculateTypeId(signer.client, tx);
9898

9999
const scriptInfo = await getScriptInfo(signer.client);

0 commit comments

Comments
 (0)