wow-addons/tests/test_fk.lua
rope 38e942ac1d Fish Key: quiet all but sound effects while fishing
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 15:42:26 +00:00

250 lines
10 KiB
Lua

-- Runs FishKey.lua against a mock of the WoW API.
-- arg[1] = addon file
local spells = { [7620] = "Fishing", [7731] = "Fishing", [133] = "Fireball" }
C_Spell = { GetSpellName = function(id) return spells[id] end }
local actions = { [9] = { "spell", 7731 }, [30] = { "macro", 4 }, [40] = { "spell", 133 } }
function GetActionInfo(slot)
local a = actions[slot]
if a then return a[1], a[2] end
end
local macros = { [4] = 7620, [5] = 133 }
function GetMacroSpell(i) return macros[i] end
local macroNames = { ["FishMacro"] = 4, ["Nuke"] = 5 }
function GetMacroIndexByName(n) return macroNames[n] or 0 end
ActionButton9 = { bindingAction = "ACTIONBUTTON9", action = 9 }
ActionButton10 = { bindingAction = "ACTIONBUTTON10", action = 40 }
MultiBarBottomLeftButton6 = { bindingAction = "MULTIACTIONBAR1BUTTON6", action = 30 }
SomeAddonButton = { GetAttribute = function(_, k) if k == "action" then return 9 end end }
local bindings = {
{ "ACTIONBUTTON9", "ACTIONBAR", "F", "SHIFT-1" },
{ "ACTIONBUTTON10", "ACTIONBAR", "G" },
{ "MULTIACTIONBAR1BUTTON6", "ACTIONBAR" },
{ "SPELL Fishing", "SPELLS", "CTRL-F" },
{ "MACRO FishMacro", "MACROS", "ALT-F" },
{ "MACRO Nuke", "MACROS", "N" },
{ "CLICK SomeAddonButton:LeftButton", "ADDONS", "BUTTON4" },
{ "INTERACTTARGET", "TARGETING", "SHIFT-F" },
}
function GetNumBindings() return #bindings end
function GetBinding(i) return unpack(bindings[i]) end
local overrides = {}
function SetOverrideBinding(_, priority, key, command) overrides[key] = command end
function ClearOverrideBindings() overrides = {} end
local inCombat = false
function InCombatLockdown() return inCombat end
local held = {}
function IsKeyDown(key)
assert(not key:find("^SHIFT%-") and not key:find("^CTRL%-") and not key:find("^ALT%-"), "modifier passed to IsKeyDown: " .. key)
return held[key] or false
end
local clock, timers = 0, {}
C_Timer = { After = function(sec, fn) timers[#timers + 1] = { at = clock + sec, fn = fn } end }
function GetTime() return clock end
function GetNetStats() return 0, 0, 20, 50 end
function hooksecurefunc(t, name, fn)
local orig = t[name]
t[name] = function(...) local r = orig(...); fn(...); return r end
end
C_PlayerInteractionManager = { InteractUnit = function() return true end }
local function advance(sec)
local target = clock + sec
while true do
local nextIndex
for i, t in ipairs(timers) do
if t.at <= target and (not nextIndex or t.at < timers[nextIndex].at) then nextIndex = i end
end
if not nextIndex then break end
local t = table.remove(timers, nextIndex)
clock = t.at
t.fn()
end
clock = target
end
local cvars = {
SoftTargetInteract = "0", SoftTargetInteractRange = "10", ActionButtonUseKeyHeldSpell = "1",
Sound_EnableMusic = "1", Sound_EnableAmbience = "1", Sound_EnableDialog = "0",
Sound_EnablePetSounds = "1", Sound_EnableErrorSpeech = "1", Sound_EnableSFX = "1",
Sound_SFXVolume = "0.6", Sound_EnableSoundWhenGameIsInBG = "0",
Sound_EnablePositionalLowPassFilter = "1",
}
local temp = {}
C_CVar = {
GetCVar = function(n) return temp[n] or cvars[n] end,
SetTempCVar = function(n, v) temp[n] = v end,
RemoveTempCVar = function(n) temp[n] = nil end,
}
local handler
CreateFrame = function()
return {
RegisterEvent = function() end,
RegisterUnitEvent = function() end,
SetScript = function(_, _, fn) handler = fn end,
}
end
SlashCmdList = {}
local printed = {}
DEFAULT_CHAT_FRAME = { AddMessage = function(_, m) printed[#printed + 1] = m end }
local failures = 0
local function check(cond, what)
if not cond then failures = failures + 1 end
print((cond and "ok " or "FAIL ") .. what)
end
local function count(t) local n = 0 for _ in pairs(t) do n = n + 1 end return n end
local function stop() handler(nil, "UNIT_SPELLCAST_CHANNEL_STOP", "player", "guid", 7620) end
-- Cast ends with no loot and every key released: everything back to normal.
local function finish() stop(); advance(2) end
assert(loadfile(arg[1]))("FishKey", {})
handler(nil, "ADDON_LOADED", "FishKey")
check(FishKeyDB.enabled == true, "enabled by default")
handler(nil, "UNIT_SPELLCAST_CHANNEL_START", "player", "guid", 7731)
check(overrides["F"] == "INTERACTTARGET", "action bar key becomes interact")
check(overrides["SHIFT-1"] == "INTERACTTARGET", "second key on the same button too")
check(overrides["CTRL-F"] == "INTERACTTARGET", "SPELL binding")
check(overrides["ALT-F"] == "INTERACTTARGET", "macro binding casting Fishing")
check(overrides["BUTTON4"] == "INTERACTTARGET", "CLICK binding to an addon button")
check(overrides["G"] == nil and overrides["N"] == nil, "other spells untouched")
check(count(overrides) == 5, "only Fishing keys overridden")
check(C_CVar.GetCVar("SoftTargetInteractRange") == "30", "interact range raised while fishing")
check(C_CVar.GetCVar("SoftTargetInteract") == "3", "Interact Key turned on while fishing")
local function restored()
return count(overrides) == 0 and C_CVar.GetCVar("SoftTargetInteract") == "0"
and C_CVar.GetCVar("SoftTargetInteractRange") == "10" and C_CVar.GetCVar("ActionButtonUseKeyHeldSpell") == "1"
and C_CVar.GetCVar("Sound_EnableMusic") == "1" and C_CVar.GetCVar("Sound_SFXVolume") == "0.6"
end
check(C_CVar.GetCVar("ActionButtonUseKeyHeldSpell") == "0", "press and hold casting off while fishing")
check(C_CVar.GetCVar("Sound_EnableMusic") == "0" and C_CVar.GetCVar("Sound_EnableAmbience") == "0"
and C_CVar.GetCVar("Sound_EnablePetSounds") == "0" and C_CVar.GetCVar("Sound_EnableErrorSpeech") == "0",
"music, ambience, pets and error speech off while fishing")
check(C_CVar.GetCVar("Sound_SFXVolume") == "1" and C_CVar.GetCVar("Sound_EnableSoundWhenGameIsInBG") == "1"
and C_CVar.GetCVar("Sound_EnablePositionalLowPassFilter") == "0", "effects full, background sound on, no muffling")
check(temp.Sound_EnableDialog == nil and temp.Sound_EnableSFX == nil, "settings already right are left alone")
local function press() C_PlayerInteractionManager.InteractUnit("anyinteract") end
-- Catch with the key held through the loot (press and hold, slow release).
held["F"] = true
press()
handler(nil, "UNIT_SPELLCAST_CHANNEL_STOP", "player", "guid", 7731)
check(overrides["F"] == "INTERACTTARGET", "still interact right after the channel ends")
handler(nil, "LOOT_OPENED")
advance(2)
check(overrides["F"] == "INTERACTTARGET", "still interact while the loot window is open")
handler(nil, "LOOT_CLOSED")
advance(0.3)
check(overrides["F"] == "INTERACTTARGET" and C_CVar.GetCVar("ActionButtonUseKeyHeldSpell") == "0", "still interact while the key is held")
held["F"] = nil
advance(0.1)
check(restored(), "key and settings restored once looted and released")
-- Cast ends with no press (timeout, moved): back at once.
handler(nil, "UNIT_SPELLCAST_CHANNEL_START", "player", "guid", 7620)
stop()
check(restored(), "no press: restored immediately")
-- An early press long before the timeout doesn't count.
handler(nil, "UNIT_SPELLCAST_CHANNEL_START", "player", "guid", 7620)
press()
advance(10)
stop()
check(restored(), "old press: restored immediately")
-- Pressed with nothing hooked: the error message ends the wait.
handler(nil, "UNIT_SPELLCAST_CHANNEL_START", "player", "guid", 7620)
press()
stop()
check(overrides["F"] == "INTERACTTARGET", "press: waits for loot")
handler(nil, "UI_ERROR_MESSAGE", 1, "No fish are hooked.")
check(restored(), "error message: restored immediately")
-- Pressed, no loot and no error: short latency-based wait (0.25 + 2 x 50ms).
handler(nil, "UNIT_SPELLCAST_CHANNEL_START", "player", "guid", 7620)
press()
stop()
advance(0.3)
check(overrides["F"] == "INTERACTTARGET", "still waiting at 0.3s")
advance(0.1)
check(restored(), "restored after 0.35s")
-- Loot arrives and closes before the channel stop, modifier key held.
handler(nil, "UNIT_SPELLCAST_CHANNEL_START", "player", "guid", 7620)
press()
handler(nil, "LOOT_OPENED")
held["1"] = true
handler(nil, "LOOT_CLOSED")
stop()
advance(0.2)
check(overrides["SHIFT-1"] == "INTERACTTARGET", "held SHIFT-1 counts as held")
held["1"] = nil
advance(0.06)
check(restored(), "restored after loot that came first, no extra wait")
-- A stale loot timer must not cut short the next cast.
handler(nil, "UNIT_SPELLCAST_CHANNEL_START", "player", "guid", 7620)
press()
stop()
handler(nil, "UNIT_SPELLCAST_CHANNEL_START", "player", "guid", 7620)
advance(2)
check(overrides["F"] == "INTERACTTARGET", "old timer leaves the new cast alone")
finish()
handler(nil, "UNIT_SPELLCAST_CHANNEL_START", "player", "guid", 133)
check(count(overrides) == 0, "other channels ignored")
handler(nil, "UNIT_SPELLCAST_CHANNEL_START", "player", "guid", 7620)
handler(nil, "PLAYER_REGEN_DISABLED")
check(count(overrides) == 0, "cleared when combat starts")
inCombat = true
handler(nil, "UNIT_SPELLCAST_CHANNEL_START", "player", "guid", 7620)
check(count(overrides) == 0, "no override in combat")
inCombat = false
cvars.SoftTargetInteractRange = "40"
handler(nil, "UNIT_SPELLCAST_CHANNEL_START", "player", "guid", 7620)
check(temp.SoftTargetInteractRange == nil, "longer range left alone")
finish()
cvars.SoftTargetInteract = "3"
handler(nil, "UNIT_SPELLCAST_CHANNEL_START", "player", "guid", 7620)
check(temp.SoftTargetInteract == nil, "Interact Key already on is left alone")
finish()
cvars.SoftTargetInteract = "0"
C_CVar.SetTempCVar = function() error("secure cvar") end
handler(nil, "UNIT_SPELLCAST_CHANNEL_START", "player", "guid", 7620)
check(overrides["F"] == "INTERACTTARGET" and printed[#printed]:find("Settings", 1, true), "blocked setting warns, key still overridden")
finish()
C_CVar.SetTempCVar = function(n, v) temp[n] = v end
SlashCmdList.FISHKEY("off")
check(FishKeyDB.enabled == false, "/fish off")
check(printed[#printed]:find("off", 1, true) and printed[#printed]:find("F, SHIFT-1", 1, true), "status lists keys")
handler(nil, "UNIT_SPELLCAST_CHANNEL_START", "player", "guid", 7620)
check(count(overrides) == 0, "nothing when off")
SlashCmdList.FISHKEY("on")
check(FishKeyDB.enabled == true, "/fish on")
finish()
SlashCmdList.FISHKEY("sound off")
check(FishKeyDB.sound == false and printed[#printed]:find("quiet sound off", 1, true), "/fish sound off")
handler(nil, "UNIT_SPELLCAST_CHANNEL_START", "player", "guid", 7620)
check(overrides["F"] == "INTERACTTARGET" and C_CVar.GetCVar("Sound_EnableMusic") == "1"
and C_CVar.GetCVar("SoftTargetInteract") == "3", "sound off: key still works, sound untouched")
finish()
SlashCmdList.FISHKEY("sound on")
check(FishKeyDB.sound == true, "/fish sound on")
print(failures == 0 and "all passed" or (failures .. " failed"))
os.exit(failures == 0 and 0 or 1)