Repository navigation
Expand file tree
/
Copy pathtest_loading_order.lua
More file actions
304 lines (250 loc) · 9.56 KB
/
Copy pathtest_loading_order.lua
File metadata and controls
304 lines (250 loc) · 9.56 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
#!/usr/bin/env lua
-- HealIQ Loading Order Test
-- Test to prevent regression of the .toc loading order issue that caused rule files to fail loading
-- This test ensures Core.lua and Logging.lua are loaded before rule files
local function print_result(passed, message)
if passed then
print("✓ " .. message)
else
print("✗ " .. message)
error("Test failed: " .. message)
end
end
local function test_toc_loading_order()
print("\n=== Testing .toc Loading Order ===")
-- Read .toc file
local tocPath = "HealIQ.toc"
local tocFile = io.open(tocPath, "r")
if not tocFile then
error("Cannot read HealIQ.toc file")
end
local content = tocFile:read("*all")
tocFile:close()
-- Parse file list from .toc
local files = {}
local fileOrder = {}
for line in content:gmatch("[^\r\n]+") do
line = line:gsub("^%s*", ""):gsub("%s*$", "") -- trim whitespace
if line:match("%.lua$") and not line:match("^#") then
table.insert(files, line)
fileOrder[line] = #files
end
end
print("Found " .. #files .. " Lua files in .toc")
-- Test that Core.lua comes before rules/
local corePos = fileOrder["Core.lua"]
local firstRulePos = nil
for _, file in ipairs(files) do
if file:match("^rules/") then
firstRulePos = fileOrder[file]
break
end
end
print_result(corePos ~= nil, "Core.lua found in .toc file")
print_result(firstRulePos ~= nil, "Rule files found in .toc file")
print_result(corePos < firstRulePos, "Core.lua loads before rule files")
-- Test that Logging.lua comes before rules/
local loggingPos = fileOrder["Logging.lua"]
if loggingPos then
print_result(loggingPos < firstRulePos, "Logging.lua loads before rule files")
end
-- Test that SpellData.lua comes before rules/ and the Tracker/Engine.
-- rules/DefensiveCooldowns captures HealIQ.SpellData at file-load time, so
-- SpellData must load first or spell-ID lookups are nil (issue #98 class).
local spellDataPos = fileOrder["SpellData.lua"]
print_result(spellDataPos ~= nil, "SpellData.lua found in .toc file")
if spellDataPos then
print_result(spellDataPos < firstRulePos, "SpellData.lua loads before rule files")
end
-- Test that specific rule files are present
local expectedRuleFiles = {
"rules/BaseRule.lua",
"rules/DefensiveCooldowns.lua",
"rules/HealingCooldowns.lua",
"rules/UtilityRules.lua",
"rules/AoERules.lua",
"rules/OffensiveRules.lua"
}
for _, ruleFile in ipairs(expectedRuleFiles) do
print_result(fileOrder[ruleFile] ~= nil, ruleFile .. " found in .toc file")
end
-- Test that BaseRule.lua comes first among rule files
local baseRulePos = fileOrder["rules/BaseRule.lua"]
if baseRulePos then
local allRulesAfterBase = true
for _, ruleFile in ipairs(expectedRuleFiles) do
if ruleFile ~= "rules/BaseRule.lua" then
local pos = fileOrder[ruleFile]
if pos and pos < baseRulePos then
allRulesAfterBase = false
break
end
end
end
print_result(allRulesAfterBase, "BaseRule.lua loads first among rule files")
end
-- Print loading order for verification
print("\nLoading order:")
for i, luaFile in ipairs(files) do
print(string.format("%2d. %s", i, luaFile))
end
return true
end
local function test_rule_file_structure()
print("\n=== Testing Rule File Structure ===")
-- Test that rule files have defensive initialization
local ruleFiles = {
"rules/BaseRule.lua",
"rules/DefensiveCooldowns.lua",
"rules/HealingCooldowns.lua",
"rules/UtilityRules.lua",
"rules/AoERules.lua",
"rules/OffensiveRules.lua"
}
for _, ruleFile in ipairs(ruleFiles) do
local file = io.open(ruleFile, "r")
if file then
local content = file:read("*all")
file:close()
-- Check for correct global namespace access pattern (WoW-compatible)
-- Look for the correct pattern and ensure no non-comment lines use the wrong pattern
local hasCorrectPattern = content:find("local HealIQ = _G%.HealIQ")
local hasIncorrectPattern = false
-- Check each line to see if it uses the incorrect pattern (but not in comments)
for line in content:gmatch("[^\r\n]+") do
local trimmedLine = line:gsub("^%s*", "")
if trimmedLine:find("^local addonName, HealIQ = ") and not trimmedLine:find("^%-%-") then
hasIncorrectPattern = true
break
end
end
hasCorrectPattern = hasCorrectPattern and not hasIncorrectPattern
-- Check for defensive initialization
local hasDefensiveInit = (
content:find("HealIQ%.Rules = HealIQ%.Rules or {}") or
content:find("_G%.HealIQ = _G%.HealIQ or {}")
)
print_result(hasCorrectPattern, ruleFile .. " uses correct global namespace pattern")
print_result(hasDefensiveInit, ruleFile .. " has defensive initialization")
else
print_result(false, ruleFile .. " could not be read")
end
end
return true
end
local function test_loading_simulation()
print("\n=== Testing Loading Simulation ===")
-- Simulate the loading process that WoW would do
local mockGlobal = {}
local originalGlobal = _G
-- Mock basic WoW API
mockGlobal.print = print
mockGlobal.pairs = pairs
mockGlobal.ipairs = ipairs
mockGlobal.type = type
mockGlobal.table = table
mockGlobal.string = string
mockGlobal.GetTime = function() return os.time() end
mockGlobal.InCombatLockdown = function() return false end
-- Set up the global environment
_G = mockGlobal
-- Simulate loading Core.lua first
local coreSuccess = pcall(function()
-- Load Core.lua logic
local addonName, HealIQ = "HealIQ", {}
HealIQ.version = "0.1.4"
HealIQ.debug = false
function HealIQ:SafeCall(func, ...)
local success, result = pcall(func, ...)
if not success then
print("SafeCall error: " .. tostring(result))
end
return success, result
end
-- Store in mock global
mockGlobal.HealIQ = HealIQ
return true
end)
print_result(coreSuccess, "Core.lua simulation loads without error")
-- Simulate loading BaseRule.lua after Core.lua
local baseRuleSuccess = pcall(function()
local addonName, HealIQ = "HealIQ", mockGlobal.HealIQ
-- Defensive initialization (should work now that HealIQ exists)
HealIQ = HealIQ or {}
HealIQ.Rules = HealIQ.Rules or {}
-- Define BaseRule
HealIQ.Rules.BaseRule = {}
function HealIQ.Rules.BaseRule:GetRecentDamageCount(tracker, seconds)
seconds = seconds or 5
local currentTime = mockGlobal.GetTime()
local count = 0
if tracker and tracker.trackedData and tracker.trackedData.recentDamage then
for timestamp, _ in pairs(tracker.trackedData.recentDamage) do
if currentTime - timestamp <= seconds then
count = count + 1
end
end
end
return count
end
return true
end)
print_result(baseRuleSuccess, "BaseRule.lua simulation loads after Core.lua")
-- Test that BaseRule works correctly
if baseRuleSuccess and mockGlobal.HealIQ and mockGlobal.HealIQ.Rules.BaseRule then
local testTracker = {
trackedData = {
recentDamage = {
[os.time() - 2] = true,
[os.time() - 4] = true,
[os.time() - 10] = true
}
}
}
local damageCount = mockGlobal.HealIQ.Rules.BaseRule:GetRecentDamageCount(testTracker, 5)
print_result(damageCount == 2, "BaseRule functionality works correctly after loading")
end
-- Restore global
_G = originalGlobal
return true
end
local function run_all_tests()
print("HealIQ Loading Order Test Suite")
print("===============================")
local tests = {
test_toc_loading_order,
test_rule_file_structure,
test_loading_simulation
}
local passed = 0
local total = #tests
for i, test in ipairs(tests) do
local success = pcall(test)
if success then
passed = passed + 1
else
print("Test " .. i .. " failed")
end
end
print(string.format("\n=== Results: %d/%d tests passed ===", passed, total))
if passed == total then
print("✓ All loading order tests passed!")
print("✓ The .toc loading order issue has been fixed and is protected against regression.")
return true
else
print("✗ Some tests failed!")
return false
end
end
-- Run tests if this file is executed directly
if arg and arg[0] and arg[0]:match("test_loading_order%.lua") then
local success = run_all_tests()
os.exit(success and 0 or 1)
end
-- Export for use in other test files
return {
test_toc_loading_order = test_toc_loading_order,
test_rule_file_structure = test_rule_file_structure,
test_loading_simulation = test_loading_simulation,
run_all_tests = run_all_tests
}