-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpression-complexity.spec.ts
More file actions
226 lines (189 loc) · 6.45 KB
/
expression-complexity.spec.ts
File metadata and controls
226 lines (189 loc) · 6.45 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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import { describe, expect, it } from "vitest";
import { expressionComplexity } from "./expression-complexity.js";
import { SELChecker } from "../../checker/checker.js";
import type { ContractSchema, MethodSchema, SELSchema } from "@seljs/schema";
/**
* Creates a method schema for tests.
* The `abi` field is required by MethodSchema but unused by the checker.
*/
const method = (m: Omit<MethodSchema, "abi">): MethodSchema =>
m as MethodSchema;
/**
* Creates a contract schema for tests.
* The `address` field is required by ContractSchema but unused by the checker.
*/
const contract = (c: Omit<ContractSchema, "address">): ContractSchema =>
c as ContractSchema;
const TEST_SCHEMA: SELSchema = {
version: "1.0.0",
contracts: [
contract({
name: "token",
methods: [
method({
name: "balanceOf",
params: [{ name: "account", type: "sol_address" }],
returns: "sol_int",
}),
method({ name: "totalSupply", params: [], returns: "sol_int" }),
method({
name: "allowance",
params: [
{ name: "owner", type: "sol_address" },
{ name: "spender", type: "sol_address" },
],
returns: "sol_int",
}),
],
}),
],
variables: [
{ name: "user", type: "sol_address" },
{ name: "threshold", type: "sol_int" },
],
types: [],
functions: [],
macros: [],
};
describe("expressionComplexity rule", () => {
it("simple expression produces no diagnostic", () => {
const checker = new SELChecker(TEST_SCHEMA, {
rules: [expressionComplexity()],
});
const result = checker.check("1 + 2");
expect(result.diagnostics).toHaveLength(0);
});
it("expression exceeding maxAstNodes triggers error", () => {
const checker = new SELChecker(TEST_SCHEMA, {
rules: [expressionComplexity({ maxAstNodes: 3 })],
});
/* "1 + 2 + 3 + 4" has many nodes (7+) */
const result = checker.check("1 + 2 + 3 + 4");
const msgs = result.diagnostics.map((d) => d.message);
expect(msgs.some((m) => m.includes("AST node count"))).toBe(true);
expect(result.diagnostics[0]?.severity).toBe("error");
});
it("deeply nested ternary triggers maxDepth", () => {
const checker = new SELChecker(TEST_SCHEMA, {
rules: [expressionComplexity({ maxDepth: 1 })],
});
/*
* root ternary at depth 0, inner ternary children at depth 1, leaf at depth 2
* maxDepth recorded will be 2, which exceeds limit of 1
*/
const result = checker.check("true ? (true ? 1 : 2) : 3");
const msgs = result.diagnostics.map((d) => d.message);
expect(msgs.some((m) => m.includes("nesting depth"))).toBe(true);
});
it("multiple contract calls trigger maxCalls", () => {
const checker = new SELChecker(TEST_SCHEMA, {
rules: [expressionComplexity({ maxCalls: 1 })],
});
/* Two contract calls */
const result = checker.check("token.totalSupply() > token.totalSupply()");
const msgs = result.diagnostics.map((d) => d.message);
expect(msgs.some((m) => m.includes("contract calls"))).toBe(true);
});
it("many operators trigger maxOperators (&&/|| NOT counted)", () => {
const checker = new SELChecker(TEST_SCHEMA, {
rules: [expressionComplexity({ maxOperators: 2 })],
});
/* 3 arithmetic operators, 1 logical (&&) — && should NOT count */
const result = checker.check("1 + 2 + 3 > 0 && true");
const msgs = result.diagnostics.map((d) => d.message);
expect(msgs.some((m) => m.includes("operators"))).toBe(true);
});
it("&&/|| are NOT counted as operators", () => {
const checker = new SELChecker(TEST_SCHEMA, {
rules: [expressionComplexity({ maxOperators: 2, maxBranches: 100 })],
});
/* Only && and ||, no other operators */
const result = checker.check("true && false || true");
const msgs = result.diagnostics.map((d) => d.message);
expect(msgs.some((m) => m.includes("operators"))).toBe(false);
});
it("many branches (&&/||/ternary) trigger maxBranches", () => {
const checker = new SELChecker(TEST_SCHEMA, {
rules: [expressionComplexity({ maxBranches: 2 })],
});
/* 3 branches: &&, ||, ?: */
const result = checker.check("true && false || (true ? 1 : 2) == 1");
const msgs = result.diagnostics.map((d) => d.message);
expect(msgs.some((m) => m.includes("branches"))).toBe(true);
});
it("multiple thresholds exceeded produces multiple diagnostics", () => {
const checker = new SELChecker(TEST_SCHEMA, {
rules: [
expressionComplexity({
maxAstNodes: 1,
maxDepth: 0,
maxOperators: 0,
}),
],
});
const result = checker.check("1 + 2");
/* Should have diagnostics for nodes, depth, and operators */
expect(result.diagnostics.length).toBeGreaterThanOrEqual(3);
});
it("custom thresholds override defaults", () => {
/* With very high thresholds, nothing should trigger */
const checker = new SELChecker(TEST_SCHEMA, {
rules: [
expressionComplexity({
maxAstNodes: 1000,
maxDepth: 1000,
maxCalls: 1000,
maxOperators: 1000,
maxBranches: 1000,
}),
],
});
const result = checker.check(
"token.totalSupply() > threshold && token.totalSupply() > 0",
);
expect(result.diagnostics).toHaveLength(0);
});
it("setting threshold to Infinity disables that metric", () => {
const checker = new SELChecker(TEST_SCHEMA, {
rules: [
expressionComplexity({
maxAstNodes: Infinity,
maxDepth: Infinity,
maxCalls: Infinity,
maxOperators: Infinity,
maxBranches: Infinity,
}),
],
});
/* Even a complex expression should produce no diagnostics */
const result = checker.check(
"token.totalSupply() > threshold && token.totalSupply() > 0",
);
expect(result.diagnostics).toHaveLength(0);
});
it("diagnostic message contains actual and limit values", () => {
const checker = new SELChecker(TEST_SCHEMA, {
rules: [expressionComplexity({ maxAstNodes: 3 })],
});
const result = checker.check("1 + 2 + 3 + 4");
const nodeMsg = result.diagnostics.find((d) =>
d.message.includes("AST node count"),
);
expect(nodeMsg).toBeDefined();
/* Message should mention the actual count and the maximum */
expect(nodeMsg?.message).toMatch(/\d+ exceeds maximum of 3/);
});
it("diagnostic spans full expression (from: 0, to: expression.length)", () => {
const expr = "1 + 2 + 3 + 4";
const checker = new SELChecker(TEST_SCHEMA, {
rules: [expressionComplexity({ maxAstNodes: 3 })],
});
const result = checker.check(expr);
const diag = result.diagnostics.find((d) =>
d.message.includes("AST node count"),
);
expect(diag).toBeDefined();
expect(diag?.from).toBe(0);
expect(diag?.to).toBe(expr.length);
});
});