Skip to content

Commit 01fa42e

Browse files
committed
add formatter tests
1 parent b4998b5 commit 01fa42e

File tree

1 file changed

+267
-0
lines changed

1 file changed

+267
-0
lines changed
Lines changed: 267 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,267 @@
1+
import assert from "assert";
2+
3+
import { formatLog, formatReceiptLog } from "../providers/format.js";
4+
import { Log } from "../providers/provider.js";
5+
6+
describe("Test Log Formatting", function() {
7+
describe("formatLog with blockTimestamp", function() {
8+
it("should parse blockTimestamp from hex string", function() {
9+
const logData = {
10+
address: '0x1234567890123456789012345678901234567890',
11+
blockHash: '0x1111111111111111111111111111111111111111111111111111111111111111',
12+
blockNumber: '0x10',
13+
blockTimestamp: '0x65a1b2c3',
14+
data: '0xabcd',
15+
logIndex: '0x5',
16+
removed: false,
17+
topics: ['0x2222222222222222222222222222222222222222222222222222222222222222'],
18+
transactionHash: '0x3333333333333333333333333333333333333333333333333333333333333333',
19+
transactionIndex: '0x1'
20+
};
21+
22+
const formatted = formatLog(logData);
23+
24+
assert.strictEqual(formatted.blockTimestamp, 1705095875, "blockTimestamp should be parsed to number");
25+
assert.strictEqual(formatted.blockNumber, 16, "blockNumber should be parsed");
26+
assert.strictEqual(formatted.transactionIndex, 1, "transactionIndex should be parsed");
27+
});
28+
29+
it("should handle missing blockTimestamp gracefully", function() {
30+
const logData = {
31+
address: '0x1234567890123456789012345678901234567890',
32+
blockHash: '0x1111111111111111111111111111111111111111111111111111111111111111',
33+
blockNumber: '0x10',
34+
data: '0xabcd',
35+
logIndex: '0x5',
36+
removed: false,
37+
topics: ['0x2222222222222222222222222222222222222222222222222222222222222222'],
38+
transactionHash: '0x3333333333333333333333333333333333333333333333333333333333333333',
39+
transactionIndex: '0x1'
40+
};
41+
42+
const formatted = formatLog(logData);
43+
44+
assert.strictEqual(formatted.blockTimestamp, undefined, "blockTimestamp should be undefined when not provided");
45+
assert.strictEqual(formatted.blockNumber, 16, "blockNumber should still be parsed");
46+
});
47+
48+
it("should handle null blockTimestamp", function() {
49+
const logData = {
50+
address: '0x1234567890123456789012345678901234567890',
51+
blockHash: '0x1111111111111111111111111111111111111111111111111111111111111111',
52+
blockNumber: '0x10',
53+
blockTimestamp: null,
54+
data: '0xabcd',
55+
logIndex: '0x5',
56+
removed: false,
57+
topics: ['0x2222222222222222222222222222222222222222222222222222222222222222'],
58+
transactionHash: '0x3333333333333333333333333333333333333333333333333333333333333333',
59+
transactionIndex: '0x1'
60+
};
61+
62+
const formatted = formatLog(logData);
63+
64+
assert.strictEqual(formatted.blockTimestamp, undefined, "blockTimestamp should be undefined when null");
65+
});
66+
67+
it("should parse blockTimestamp 0x0", function() {
68+
const logData = {
69+
address: '0x1234567890123456789012345678901234567890',
70+
blockHash: '0x1111111111111111111111111111111111111111111111111111111111111111',
71+
blockNumber: '0x10',
72+
blockTimestamp: '0x0',
73+
data: '0xabcd',
74+
logIndex: '0x5',
75+
removed: false,
76+
topics: ['0x2222222222222222222222222222222222222222222222222222222222222222'],
77+
transactionHash: '0x3333333333333333333333333333333333333333333333333333333333333333',
78+
transactionIndex: '0x1'
79+
};
80+
81+
const formatted = formatLog(logData);
82+
83+
assert.strictEqual(formatted.blockTimestamp, 0, "blockTimestamp should be 0 for 0x0");
84+
});
85+
});
86+
87+
describe("formatReceiptLog with blockTimestamp", function() {
88+
it("should parse blockTimestamp from hex string", function() {
89+
const logData = {
90+
address: '0x1234567890123456789012345678901234567890',
91+
blockHash: '0x1111111111111111111111111111111111111111111111111111111111111111',
92+
blockNumber: '0x10',
93+
blockTimestamp: '0x65a1b2c3',
94+
data: '0xabcd',
95+
logIndex: '0x5',
96+
removed: false,
97+
topics: ['0x2222222222222222222222222222222222222222222222222222222222222222'],
98+
transactionHash: '0x3333333333333333333333333333333333333333333333333333333333333333',
99+
transactionIndex: '0x1'
100+
};
101+
102+
const formatted = formatReceiptLog(logData);
103+
104+
assert.strictEqual(formatted.blockTimestamp, 1705095875, "blockTimestamp should be parsed to number");
105+
assert.strictEqual(formatted.blockNumber, 16, "blockNumber should be parsed");
106+
assert.strictEqual(formatted.transactionIndex, 1, "transactionIndex should be parsed");
107+
});
108+
109+
it("should handle missing blockTimestamp gracefully", function() {
110+
const logData = {
111+
address: '0x1234567890123456789012345678901234567890',
112+
blockHash: '0x1111111111111111111111111111111111111111111111111111111111111111',
113+
blockNumber: '0x10',
114+
data: '0xabcd',
115+
logIndex: '0x5',
116+
removed: false,
117+
topics: ['0x2222222222222222222222222222222222222222222222222222222222222222'],
118+
transactionHash: '0x3333333333333333333333333333333333333333333333333333333333333333',
119+
transactionIndex: '0x1'
120+
};
121+
122+
const formatted = formatReceiptLog(logData);
123+
124+
assert.strictEqual(formatted.blockTimestamp, undefined, "blockTimestamp should be undefined when not provided");
125+
});
126+
});
127+
128+
describe("Log class with blockTimestamp", function() {
129+
const mockProvider: any = {
130+
getBlock: () => Promise.resolve(null),
131+
getTransaction: () => Promise.resolve(null),
132+
getTransactionReceipt: () => Promise.resolve(null)
133+
};
134+
135+
it("should store blockTimestamp in Log instance", function() {
136+
const logParams = {
137+
address: '0x1234567890123456789012345678901234567890',
138+
blockHash: '0x1111111111111111111111111111111111111111111111111111111111111111',
139+
blockNumber: 16,
140+
blockTimestamp: 1705095875,
141+
data: '0xabcd',
142+
index: 5,
143+
removed: false,
144+
topics: ['0x2222222222222222222222222222222222222222222222222222222222222222'],
145+
transactionHash: '0x3333333333333333333333333333333333333333333333333333333333333333',
146+
transactionIndex: 1
147+
};
148+
149+
const log = new Log(logParams, mockProvider);
150+
151+
assert.strictEqual(log.blockTimestamp, 1705095875, "Log instance should have blockTimestamp");
152+
assert.strictEqual(log.blockNumber, 16, "Log instance should have blockNumber");
153+
assert.strictEqual(log.address, '0x1234567890123456789012345678901234567890', "Log instance should have address");
154+
});
155+
156+
it("should handle undefined blockTimestamp in Log instance", function() {
157+
const logParams = {
158+
address: '0x1234567890123456789012345678901234567890',
159+
blockHash: '0x1111111111111111111111111111111111111111111111111111111111111111',
160+
blockNumber: 16,
161+
data: '0xabcd',
162+
index: 5,
163+
removed: false,
164+
topics: ['0x2222222222222222222222222222222222222222222222222222222222222222'],
165+
transactionHash: '0x3333333333333333333333333333333333333333333333333333333333333333',
166+
transactionIndex: 1
167+
};
168+
169+
const log = new Log(logParams, mockProvider);
170+
171+
assert.strictEqual(log.blockTimestamp, undefined, "Log instance should have undefined blockTimestamp");
172+
});
173+
174+
it("should include blockTimestamp in toJSON output", function() {
175+
const logParams = {
176+
address: '0x1234567890123456789012345678901234567890',
177+
blockHash: '0x1111111111111111111111111111111111111111111111111111111111111111',
178+
blockNumber: 16,
179+
blockTimestamp: 1705095875,
180+
data: '0xabcd',
181+
index: 5,
182+
removed: false,
183+
topics: ['0x2222222222222222222222222222222222222222222222222222222222222222'],
184+
transactionHash: '0x3333333333333333333333333333333333333333333333333333333333333333',
185+
transactionIndex: 1
186+
};
187+
188+
const log = new Log(logParams, mockProvider);
189+
const json = log.toJSON();
190+
191+
assert.strictEqual(json._type, "log", "JSON should have _type");
192+
assert.strictEqual(json.blockTimestamp, 1705095875, "JSON should include blockTimestamp");
193+
assert.strictEqual(json.blockNumber, 16, "JSON should include blockNumber");
194+
assert.strictEqual(json.address, '0x1234567890123456789012345678901234567890', "JSON should include address");
195+
});
196+
197+
it("should exclude undefined blockTimestamp from toJSON output", function() {
198+
const logParams = {
199+
address: '0x1234567890123456789012345678901234567890',
200+
blockHash: '0x1111111111111111111111111111111111111111111111111111111111111111',
201+
blockNumber: 16,
202+
data: '0xabcd',
203+
index: 5,
204+
removed: false,
205+
topics: ['0x2222222222222222222222222222222222222222222222222222222222222222'],
206+
transactionHash: '0x3333333333333333333333333333333333333333333333333333333333333333',
207+
transactionIndex: 1
208+
};
209+
210+
const log = new Log(logParams, mockProvider);
211+
const json = log.toJSON();
212+
213+
assert.strictEqual(json._type, "log", "JSON should have _type");
214+
assert.strictEqual(json.blockTimestamp, undefined, "JSON should have undefined blockTimestamp");
215+
assert.ok(!("blockTimestamp" in json) || json.blockTimestamp === undefined, "blockTimestamp should not be in JSON or be undefined");
216+
});
217+
});
218+
219+
describe("Integration: formatLog -> Log class", function() {
220+
const mockProvider: any = {
221+
getBlock: () => Promise.resolve(null),
222+
getTransaction: () => Promise.resolve(null),
223+
getTransactionReceipt: () => Promise.resolve(null)
224+
};
225+
226+
it("should preserve blockTimestamp through formatting and Log creation", function() {
227+
const rawLogData = {
228+
address: '0x1234567890123456789012345678901234567890',
229+
blockHash: '0x1111111111111111111111111111111111111111111111111111111111111111',
230+
blockNumber: '0x10',
231+
blockTimestamp: '0x65a1b2c3',
232+
data: '0xabcd',
233+
logIndex: '0x5',
234+
removed: false,
235+
topics: ['0x2222222222222222222222222222222222222222222222222222222222222222'],
236+
transactionHash: '0x3333333333333333333333333333333333333333333333333333333333333333',
237+
transactionIndex: '0x1'
238+
};
239+
240+
const formatted = formatLog(rawLogData);
241+
const log = new Log(formatted, mockProvider);
242+
243+
assert.strictEqual(log.blockTimestamp, 1705095875, "blockTimestamp should be preserved through formatting and Log creation");
244+
assert.strictEqual(log.toJSON().blockTimestamp, 1705095875, "blockTimestamp should be in JSON output");
245+
});
246+
247+
it("should handle missing blockTimestamp through full pipeline", function() {
248+
const rawLogData = {
249+
address: '0x1234567890123456789012345678901234567890',
250+
blockHash: '0x1111111111111111111111111111111111111111111111111111111111111111',
251+
blockNumber: '0x10',
252+
data: '0xabcd',
253+
logIndex: '0x5',
254+
removed: false,
255+
topics: ['0x2222222222222222222222222222222222222222222222222222222222222222'],
256+
transactionHash: '0x3333333333333333333333333333333333333333333333333333333333333333',
257+
transactionIndex: '0x1'
258+
};
259+
260+
const formatted = formatLog(rawLogData);
261+
const log = new Log(formatted, mockProvider);
262+
263+
assert.strictEqual(log.blockTimestamp, undefined, "blockTimestamp should be undefined through full pipeline");
264+
assert.strictEqual(log.blockNumber, 16, "blockNumber should still work");
265+
});
266+
});
267+
});

0 commit comments

Comments
 (0)