-
Notifications
You must be signed in to change notification settings - Fork 360
Expand file tree
/
Copy pathevents.test.ts
More file actions
138 lines (124 loc) · 3.73 KB
/
Copy pathevents.test.ts
File metadata and controls
138 lines (124 loc) · 3.73 KB
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
import { describe, it, expect } from "vitest";
import { humanizeEvents } from "../../../src/base/events.js";
import { nativeToScVal, scValToNative } from "../../../src/base/scval.js";
import { StrKey } from "../../../src/base/strkey.js";
import * as xdr from "../../../src/xdr/index.js";
describe("humanizing raw events", () => {
const contractId = "CA3D5KRYM6CB7OWQ6TWYRR3Z4T7GNZLKERYNZGGA5SOAOPIFY6YQGAXE";
const topics1 = nativeToScVal([1, 2, 3]).value as xdr.ScVal[];
const data1 = nativeToScVal({ hello: "world" });
const makeBody = (newBody: {
topics: xdr.ScVal[];
data: xdr.ScVal;
}): xdr.ContractEventBody =>
xdr.ContractEventBody.v0(
new xdr.ContractEventV0({
topics: newBody.topics,
data: newBody.data,
}),
);
const events = [
new xdr.DiagnosticEvent({
inSuccessfulContractCall: true,
event: new xdr.ContractEvent({
ext: xdr.ExtensionPoint.v0(),
contractId: new xdr.ContractId(StrKey.decodeContract(contractId)),
type: xdr.ContractEventType.contract,
body: makeBody({
topics: topics1,
data: data1,
}),
}),
}),
new xdr.DiagnosticEvent({
inSuccessfulContractCall: true,
event: new xdr.ContractEvent({
ext: xdr.ExtensionPoint.v0(),
contractId: null,
type: xdr.ContractEventType.contract,
body: makeBody({
topics: topics1,
data: data1,
}),
}),
}),
];
it("built valid events for testing", () => {
// sanity check: valid xdr
events.map((e) => e.toXdr());
});
it("makes diagnostic events human-readable", () => {
const readable = humanizeEvents(events);
expect(readable.length).toBe(events.length);
expect(readable[0]).toEqual({
type: "contract",
contractId: contractId,
topics: topics1.map(scValToNative),
data: scValToNative(data1),
});
expect(readable[1]).toEqual({
type: "contract",
topics: topics1.map(scValToNative),
data: scValToNative(data1),
});
});
it("makes contract events human-readable", () => {
const contractEvents = [
new xdr.ContractEvent({
ext: xdr.ExtensionPoint.v0(),
contractId: new xdr.ContractId(StrKey.decodeContract(contractId)),
type: xdr.ContractEventType.contract,
body: makeBody({
topics: topics1,
data: data1,
}),
}),
new xdr.ContractEvent({
ext: xdr.ExtensionPoint.v0(),
contractId: null,
type: xdr.ContractEventType.contract,
body: makeBody({
topics: topics1,
data: data1,
}),
}),
];
const readable = humanizeEvents(contractEvents);
expect(readable.length).toBe(contractEvents.length);
expect(readable[0]).toEqual({
type: "contract",
contractId: contractId,
topics: topics1.map(scValToNative),
data: scValToNative(data1),
});
expect(readable[1]).toEqual({
type: "contract",
topics: topics1.map(scValToNative),
data: scValToNative(data1),
});
});
it("handles system event type", () => {
const systemEvents = [
new xdr.ContractEvent({
ext: xdr.ExtensionPoint.v0(),
contractId: null,
type: xdr.ContractEventType.system,
body: makeBody({
topics: topics1,
data: data1,
}),
}),
];
const readable = humanizeEvents(systemEvents);
expect(readable.length).toBe(1);
expect(readable[0]).toEqual({
type: "system",
topics: topics1.map(scValToNative),
data: scValToNative(data1),
});
});
it("returns an empty array for empty input", () => {
const readable = humanizeEvents([]);
expect(readable).toEqual([]);
});
});