wow-addons/tests/test_wfr.lua
rope 00203dbe87 Windfury Reminder: warn when the main hand has no Windfury
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-23 22:29:41 +00:00

240 lines
9 KiB
Lua

-- Runs WindfuryReminder.lua against a mock of the WoW API.
-- arg[1] = addon file
local clock = 100
function GetTime() return clock end
STANDARD_TEXT_FONT = "Fonts\\FRIZQT__.TTF"
SOUNDKIT = { RAID_WARNING = 8959 }
Enum = { WeaponSlot = { MainHand = 0 }, ItemEnchantType = { Permanent = 1, Temporary = 2, Imbue = 3 } }
local sounds = {}
function PlaySound(id, channel) sounds[#sounds + 1] = id; assert(channel == "Master") end
local secrets = {}
function issecretvalue(v) return secrets[v] == true end
local printed = {}
function print(s) printed[#printed + 1] = s end
SlashCmdList = {}
-- World state.
local class, dead, taxi, resting, combat, weapon = "SHAMAN", false, false, false, false, 1234
local knows = true
local weaponEnchants = {} -- list returned by GetWeaponEnchantInfo
local tipLines = { "Quel'Serrar" }
local enchantSecret = false
local tipCalls = 0
function UnitClass() return "Shaman", class end
function UnitIsDeadOrGhost() return dead end
function UnitOnTaxi() return taxi end
function IsResting() return resting end
function InCombatLockdown() return combat end
function GetInventoryItemID(_, slot) assert(slot == 16); return weapon end
C_Spell = { GetSpellInfo = function(n) assert(n == "Windfury Weapon"); return knows and { spellID = 8232 } or nil end }
C_SpellBook = { IsSpellKnownOrInSpellBook = function(id) assert(id == 8232); return knows end }
C_Item = { GetWeaponEnchantInfo = function(slot)
assert(slot == 0)
if enchantSecret then local s = {}; secrets[s] = true; return s end
return weaponEnchants
end }
C_TooltipInfo = { GetInventoryItem = function(unit, slot)
assert(unit == "player" and slot == 16)
tipCalls = tipCalls + 1
local lines = {}
for i, t in ipairs(tipLines) do lines[i] = { leftText = t } end
return { lines = lines }
end }
local tickers = {}
C_Timer = { NewTicker = function(_, fn) tickers[#tickers + 1] = fn end }
local frames, fontStrings = {}, {}
local function region()
local r = { shown = true, alpha = 1 }
function r:SetAlpha(a) assert(a >= -1e-9 and a <= 1 + 1e-9, "alpha " .. a); self.alpha = a end
function r:Show() self.shown = true end
function r:Hide() self.shown = false end
function r:IsShown() return self.shown end
function r:SetText(t) self.text = t end
function r:SetDesaturated(d) self.desat = d end
function r:EnableMouse(m) self.mouse = m end
function r:GetPoint() return "TOP", UIParent, "TOP", 5, -40 end
local noop = function() end
for _, k in ipairs({ "SetPoint", "ClearAllPoints", "SetAllPoints", "SetSize", "SetTexture", "SetBlendMode",
"SetVertexColor", "SetFont", "SetTextColor", "SetFrameStrata", "SetMovable", "SetClampedToScreen",
"RegisterForDrag", "SetTexCoord", "StartMoving", "StopMovingOrSizing" }) do
r[k] = r[k] or noop
end
return r
end
function CreateFrame(_, name)
local fr = region()
fr.scripts, fr.events = {}, {}
function fr:SetScript(k, fn) self.scripts[k] = fn end
function fr:RegisterEvent(e) self.events[e] = true end
function fr:RegisterUnitEvent(e) self.events[e] = true end
function fr:UnregisterEvent(e) self.events[e] = nil end
function fr:CreateTexture() return region() end
function fr:CreateFontString() local s = region(); fontStrings[#fontStrings + 1] = s; return s end
frames[#frames + 1] = fr
if name then _G[name] = fr end
return fr
end
UIParent = region()
dofile(arg[1])
local frame = WindfuryReminderFrame
local ev
for _, fr in ipairs(frames) do if fr.events.ADDON_LOADED then ev = fr end end
assert(frame and ev)
local function fire(...) ev.scripts.OnEvent(ev, ...) end
local function tick(sec)
for _ = 1, math.floor(sec / 0.25 + 0.5) do
clock = clock + 0.25
for e in pairs(weaponEnchants) do
local w = weaponEnchants[e]
if w.timeLeft then w.timeLeft = math.max(0, w.timeLeft - 250) end
end
for _, fn in ipairs(tickers) do fn() end
if frame.shown then frame.scripts.OnUpdate(frame, 0.25) end
end
end
fire("ADDON_LOADED", "Other")
assert(WindfuryReminderDB == nil)
fire("ADDON_LOADED", "WindfuryReminder")
assert(WindfuryReminderDB.enabled and WindfuryReminderDB.sound and #tickers == 1)
assert(not frame.shown)
-- Nothing until the world is entered.
tick(1)
assert(not frame.shown)
fire("PLAYER_ENTERING_WORLD")
assert(frame.shown, "no enchant should warn")
local function lastPrint() return printed[#printed] or "" end
-- Windfury on: tooltip names it, ID gets saved, no reminder.
weaponEnchants = { { hasEnchant = true, enchantType = 3, timeLeft = 300000, charges = 0, enchantID = 1669 } }
tipLines = { "Quel'Serrar", "Main Hand", "Windfury 4 (5 min)" }
tick(0.25)
assert(not frame.shown, "Windfury present")
assert(WindfuryReminderDB.ids[1669] == true)
-- Cached: the tooltip isn't read again.
local calls = tipCalls
tick(1)
assert(tipCalls == calls)
-- Last 30 seconds: countdown.
tick(271)
assert(frame.shown and fontStrings[1].text:find("^WINDFURY 0:2%d$"), fontStrings[1].text)
-- Runs out in combat: one alarm, then "missing".
combat = true
fire("PLAYER_REGEN_DISABLED")
assert(#sounds == 0, "had Windfury at pull")
tick(30)
weaponEnchants = {}
tick(0.25)
assert(frame.shown and fontStrings[1].text == "NO WINDFURY")
assert(#sounds == 1, "expiry alarm " .. #sounds)
tick(5)
assert(#sounds == 1)
-- Combat start while missing: alarm.
combat = false
fire("PLAYER_REGEN_ENABLED")
combat = true
fire("PLAYER_REGEN_DISABLED")
assert(#sounds == 2)
combat = false
fire("PLAYER_REGEN_ENABLED")
-- Rockbiter isn't Windfury.
weaponEnchants = { { hasEnchant = true, enchantType = 3, timeLeft = 300000, charges = 0, enchantID = 503 } }
tipLines = { "Quel'Serrar", "Main Hand", "Rockbiter 3 (5 min)" }
tick(0.25)
assert(frame.shown, "Rockbiter should warn")
assert(WindfuryReminderDB.ids[503] == nil)
-- A weapon named Windfury doesn't count.
tipLines = { "Windfury Blade", "Main Hand", "Rockbiter 3 (5 min)" }
weaponEnchants[1].enchantID = 504
tick(0.25)
assert(frame.shown)
-- Totem Windfury (10s, refreshed): no countdown flicker, no reminder.
weaponEnchants = { { hasEnchant = true, enchantType = 2, timeLeft = 10000, charges = 0, enchantID = 1783 } }
tipLines = { "Quel'Serrar", "Windfury Totem 2 (10 sec)" }
for _ = 1, 4 do
tick(5)
assert(not frame.shown, "totem windfury")
weaponEnchants[1].timeLeft = 10000
end
-- Permanent enchants are ignored.
weaponEnchants = { { hasEnchant = true, enchantType = 1, timeLeft = 0, charges = 0, enchantID = 1900 } }
tipLines = { "Quel'Serrar", "Crusader" }
tick(0.25)
assert(frame.shown)
-- Two temp enchants, one cached Windfury: fine.
weaponEnchants = {
{ hasEnchant = true, enchantType = 2, timeLeft = 1800000, charges = 0, enchantID = 13 },
{ hasEnchant = true, enchantType = 3, timeLeft = 300000, charges = 0, enchantID = 1669 },
}
tipLines = { "Quel'Serrar", "Sharpened +2 (30 min)", "Windfury 4 (5 min)" }
tick(0.25)
assert(not frame.shown)
-- Secret values in combat: last state kept, countdown runs on.
combat = true
enchantSecret = true
tick(200)
assert(not frame.shown, "still has Windfury by the clock")
tick(80)
assert(frame.shown, "countdown from remembered expiry")
tick(25)
assert(frame.shown, "expired by the clock")
assert(#sounds == 3, "alarm when a remembered Windfury runs out")
enchantSecret = false
combat = false
-- Hidden: rest area, dead, taxi, no weapon, other class, not learned.
weaponEnchants = {}
tick(0.25)
assert(frame.shown)
resting = true; tick(0.25); assert(not frame.shown)
combat = true; tick(0.25); assert(frame.shown, "rest area in combat still warns")
combat = false; resting = false
dead = true; tick(0.25); assert(not frame.shown); dead = false
taxi = true; tick(0.25); assert(not frame.shown); taxi = false
weapon = nil; tick(0.25); assert(not frame.shown); weapon = 1234
knows = false; fire("SPELLS_CHANGED"); assert(not frame.shown)
knows = true; fire("SPELLS_CHANGED"); assert(frame.shown)
class = "WARRIOR"; fire("PLAYER_ENTERING_WORLD"); assert(not frame.shown)
class = "SHAMAN"; fire("PLAYER_ENTERING_WORLD"); assert(frame.shown)
-- Slash commands.
SlashCmdList.WINDFURYREMINDER("off")
assert(not WindfuryReminderDB.enabled and not frame.shown)
SlashCmdList.WINDFURYREMINDER("")
assert(frame.shown, "preview shows even when off")
assert(lastPrint():find("off, sound on"), lastPrint())
tick(5)
assert(not frame.shown, "preview ends")
SlashCmdList.WINDFURYREMINDER("on")
assert(frame.shown)
SlashCmdList.WINDFURYREMINDER("sound off")
assert(not WindfuryReminderDB.sound)
local n = #sounds
combat = true; fire("PLAYER_REGEN_DISABLED"); combat = false
assert(#sounds == n, "sound off")
SlashCmdList.WINDFURYREMINDER("move")
assert(frame.mouse)
frame.scripts.OnDragStop(frame)
assert(WindfuryReminderDB.point[1] == "TOP" and WindfuryReminderDB.point[4] == -40)
weaponEnchants = { { hasEnchant = true, enchantType = 3, timeLeft = 300000, charges = 0, enchantID = 1669 } }
tick(0.25)
assert(frame.shown, "stays visible while moving")
SlashCmdList.WINDFURYREMINDER("move")
assert(not frame.mouse and not frame.shown)
SlashCmdList.WINDFURYREMINDER("reset")
assert(WindfuryReminderDB.point == nil)
print = io.write
io.write("ok\n")