forked from PathOfBuildingCommunity/PathOfBuilding-PoE2
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTestBuildExportPoE2_spec.lua
More file actions
330 lines (284 loc) · 10.5 KB
/
Copy pathTestBuildExportPoE2_spec.lua
File metadata and controls
330 lines (284 loc) · 10.5 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
local BuildExportPoE2 = require("Modules/BuildExportPoE2")
local dkjson = require("dkjson")
describe("TestBuildExportPoE2", function()
describe("ClampLevel", function()
it("Returns nil for non-numeric input", function()
assert.is_nil(BuildExportPoE2.ClampLevel("abc"))
end)
it("Returns nil for empty string", function()
assert.is_nil(BuildExportPoE2.ClampLevel(""))
end)
it("Returns nil for nil input", function()
assert.is_nil(BuildExportPoE2.ClampLevel(nil))
end)
it("Floors decimal values", function()
assert.are.equals(5, BuildExportPoE2.ClampLevel(5.7))
assert.are.equals(3, BuildExportPoE2.ClampLevel(3.2))
end)
it("Floors decimal strings", function()
assert.are.equals(5, BuildExportPoE2.ClampLevel("5.7"))
end)
it("Clamps negative values to 0", function()
assert.are.equals(0, BuildExportPoE2.ClampLevel(-10))
assert.are.equals(0, BuildExportPoE2.ClampLevel(-0.5))
end)
it("Clamps values above 100 to 100", function()
assert.are.equals(100, BuildExportPoE2.ClampLevel(150))
assert.are.equals(100, BuildExportPoE2.ClampLevel(100.5))
end)
it("Passes through valid integers", function()
assert.are.equals(1, BuildExportPoE2.ClampLevel(1))
assert.are.equals(50, BuildExportPoE2.ClampLevel(50))
assert.are.equals(100, BuildExportPoE2.ClampLevel(100))
end)
it("Passes through valid integer strings", function()
assert.are.equals(42, BuildExportPoE2.ClampLevel("42"))
end)
end)
describe("PresetNextLevels", function()
it("Seeds first entry and new entry to [1,30] when no existing levels", function()
local existing = { { id = 1 }, { id = 2 } }
local newEntry = { id = 3 }
BuildExportPoE2.PresetNextLevels(existing, newEntry)
assert.are.equals(1, existing[1].levelMin)
assert.are.equals(30, existing[1].levelMax)
assert.are.equals(30, newEntry.levelMin)
assert.are.equals(60, newEntry.levelMax)
end)
it("Advances to next bracket when existing levels are set", function()
local existing = { { id = 1, levelMin = 1, levelMax = 30 } }
local newEntry = { id = 2 }
BuildExportPoE2.PresetNextLevels(existing, newEntry)
assert.are.equals(30, newEntry.levelMin)
assert.are.equals(60, newEntry.levelMax)
end)
it("Caps at 100 when maxLvl is near ceiling", function()
local existing = { { id = 1, levelMin = 1, levelMax = 90 } }
local newEntry = { id = 2 }
BuildExportPoE2.PresetNextLevels(existing, newEntry)
assert.are.equals(90, newEntry.levelMin)
assert.are.equals(100, newEntry.levelMax)
end)
it("Uses highest levelMax across multiple entries", function()
local existing = {
{ id = 1, levelMin = 1, levelMax = 30 },
{ id = 2, levelMin = 30, levelMax = 60 },
}
local newEntry = { id = 3 }
BuildExportPoE2.PresetNextLevels(existing, newEntry)
assert.are.equals(60, newEntry.levelMin)
assert.are.equals(90, newEntry.levelMax)
end)
it("Handles nil existingEntries", function()
local newEntry = { id = 1 }
BuildExportPoE2.PresetNextLevels(nil, newEntry)
assert.are.equals(1, newEntry.levelMin)
assert.are.equals(30, newEntry.levelMax)
end)
it("Handles empty existingEntries", function()
local newEntry = { id = 1 }
BuildExportPoE2.PresetNextLevels({}, newEntry)
assert.are.equals(1, newEntry.levelMin)
assert.are.equals(30, newEntry.levelMax)
end)
it("Treats levelMin-only entry as having a level set", function()
local existing = { { id = 1, levelMin = 1 } }
local newEntry = { id = 2 }
BuildExportPoE2.PresetNextLevels(existing, newEntry)
-- anyHas=true but maxLvl=0 (no levelMax), so new entry gets [1,30]
assert.are.equals(1, newEntry.levelMin)
assert.are.equals(30, newEntry.levelMax)
-- Must NOT re-seed the existing entry (it already had levelMin set)
assert.is_nil(existing[1].levelMax)
end)
end)
describe("NextLoadoutBracket", function()
before_each(function()
newBuild()
end)
it("Returns [1,30] when no sets have levels", function()
local lo, hi = BuildExportPoE2.NextLoadoutBracket(build)
assert.are.equals(1, lo)
assert.are.equals(30, hi)
end)
it("Advances past highest existing levelMax", function()
build.treeTab.specList[1].levelMin = 1
build.treeTab.specList[1].levelMax = 30
local lo, hi = BuildExportPoE2.NextLoadoutBracket(build)
assert.are.equals(30, lo)
assert.are.equals(60, hi)
end)
end)
describe("DefaultDir", function()
it("Returns a non-empty string", function()
local dir = BuildExportPoE2.DefaultDir()
assert.is_not_nil(dir)
assert.is_true(#dir > 0)
end)
it("Contains Path of Exile 2 and BuildPlanner", function()
local dir = BuildExportPoE2.DefaultDir()
assert.is_truthy(dir:find("Path of Exile 2"))
assert.is_truthy(dir:find("BuildPlanner"))
end)
end)
describe("DefaultPath", function()
before_each(function()
newBuild()
end)
it("Returns a string ending in .build", function()
local path = BuildExportPoE2.DefaultPath(build)
assert.is_truthy(path:find("%.build$"))
end)
it("Contains the build name", function()
build.buildName = "TestBuild"
local path = BuildExportPoE2.DefaultPath(build)
assert.is_truthy(path:find("TestBuild"))
end)
it("Sanitizes special characters in build name", function()
build.buildName = "My Build Name"
local path = BuildExportPoE2.DefaultPath(build)
assert.is_truthy(path:find("My Build Name"))
build.buildName = "Build<>Test"
local path2 = BuildExportPoE2.DefaultPath(build)
assert.is_falsy(path2:find("<"))
assert.is_falsy(path2:find(">"))
end)
it("Falls back to Unnamed for empty build name", function()
build.buildName = ""
local path = BuildExportPoE2.DefaultPath(build)
assert.is_truthy(path:find("Unnamed"))
end)
end)
describe("SlotMap", function()
it("Has entries for standard equipment slots", function()
assert.is_not_nil(BuildExportPoE2.SlotMap["Weapon 1"])
assert.is_not_nil(BuildExportPoE2.SlotMap["Helmet"])
assert.is_not_nil(BuildExportPoE2.SlotMap["Body Armour"])
assert.is_not_nil(BuildExportPoE2.SlotMap["Boots"])
assert.is_not_nil(BuildExportPoE2.SlotMap["Ring 1"])
end)
it("Has correct inventory_id values", function()
assert.are.equals("Weapon1", BuildExportPoE2.SlotMap["Weapon 1"].inventory_id)
assert.are.equals("Helm", BuildExportPoE2.SlotMap["Helmet"].inventory_id)
assert.are.equals("BodyArmour", BuildExportPoE2.SlotMap["Body Armour"].inventory_id)
end)
it("Marks swap weapons with weapon_set = 2", function()
assert.are.equals(2, BuildExportPoE2.SlotMap["Weapon 1 Swap"].weapon_set)
assert.are.equals(2, BuildExportPoE2.SlotMap["Weapon 2 Swap"].weapon_set)
end)
end)
describe("BuildTable", function()
before_each(function()
newBuild()
end)
it("Returns a table with passives, skills, items keys", function()
local root = BuildExportPoE2.BuildTable(build)
assert.is_not_nil(root)
assert.is_not_nil(root.passives)
assert.is_not_nil(root.skills)
assert.is_not_nil(root.items)
end)
it("Has a name field", function()
local root = BuildExportPoE2.BuildTable(build)
assert.is_not_nil(root.name)
assert.is_true(type(root.name) == "string")
end)
it("Returns lists for fresh build", function()
local root = BuildExportPoE2.BuildTable(build)
assert.is_true(type(root.passives) == "table")
assert.is_true(type(root.skills) == "table")
assert.is_true(type(root.items) == "table")
end)
it("Includes ascendancy when class is set", function()
local root = BuildExportPoE2.BuildTable(build)
assert.is_true(type(root) == "table")
end)
end)
describe("Export", function()
before_each(function()
newBuild()
end)
it("Returns a JSON string", function()
local json, err = BuildExportPoE2.Export(build)
assert.is_not_nil(json)
assert.is_nil(err)
assert.is_true(type(json) == "string")
end)
it("Returns valid JSON that decodes", function()
local json = BuildExportPoE2.Export(build)
local decoded, _, decodeErr = dkjson.decode(json)
assert.is_not_nil(decoded)
assert.is_nil(decodeErr)
end)
it("Decoded JSON has expected keys", function()
local json = BuildExportPoE2.Export(build)
local decoded = dkjson.decode(json)
assert.is_not_nil(decoded.name)
assert.is_not_nil(decoded.passives)
assert.is_not_nil(decoded.skills)
assert.is_not_nil(decoded.items)
end)
it("Produces consistent output", function()
local json1 = BuildExportPoE2.Export(build)
local json2 = BuildExportPoE2.Export(build)
assert.are.equals(json1, json2)
end)
it("Returns nil warning for an empty build", function()
local _, err, warning = BuildExportPoE2.Export(build)
assert.is_nil(err)
-- An empty build has no passives so no stringId check fires.
assert.is_nil(warning)
end)
it("Returns warning when allocated nodes lack stringId", function()
-- Simulate a node without stringId to trigger the degraded-export path.
build.treeTab.specList[1].allocNodes = { [1] = {} }
local _, err, warning = BuildExportPoE2.Export(build)
assert.is_nil(err)
assert.is_not_nil(warning)
assert.is_truthy(warning:find("stringId"))
end)
it("autoBracket never produces hi < lo for n >= 100 specs", function()
-- With n=100, i=1: old code gave hi=floor(1/100*99)=0 < lo=1.
-- The m_max fix ensures hi >= lo.
build.treeTab.specList[1].allocNodes = { [1] = {} }
for i = 2, 100 do
build.treeTab.specList[i] = { id = i, allocNodes = {} }
end
local root = BuildExportPoE2.BuildTable(build)
local first = root.passives[1]
if type(first) == "table" and first.level_interval then
assert.is_true(first.level_interval[1] <= first.level_interval[2],
"autoBracket produced hi < lo: " .. tostring(first.level_interval[1]) .. " > " .. tostring(first.level_interval[2]))
end
end)
end)
describe("WriteFile", function()
before_each(function()
newBuild()
end)
it("Writes a file and returns path on success", function()
local testPath = os.tmpname() .. ".build"
local path, err = BuildExportPoE2.WriteFile(build, testPath)
assert.are.equals(testPath, path)
assert.is_nil(err)
os.remove(testPath)
end)
it("Returns nil and error for invalid path", function()
local path, err = BuildExportPoE2.WriteFile(build, "/nonexistent/dir/nope/build.build")
assert.is_nil(path)
assert.is_not_nil(err)
end)
it("File content is valid JSON", function()
local testPath = os.tmpname() .. ".build"
BuildExportPoE2.WriteFile(build, testPath)
local f = io.open(testPath, "r")
assert.is_not_nil(f)
local content = f:read("*a")
f:close()
local decoded = dkjson.decode(content)
assert.is_not_nil(decoded)
assert.is_not_nil(decoded.name)
os.remove(testPath)
end)
end)
end)