wow-addons/tests/test_qit.lua
rope de17b0006e Initial addons
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 13:02:16 +00:00

263 lines
12 KiB
Lua

-- Runs QuestItemTip.lua against a mock of the WoW API.
-- arg[1] = addon file
local hideCollapsed -- true: collapsed headers hide their quests (legacy behaviour)
local toggles = 0
local log = {}
local function reset()
toggles = 0
log = {
{ title = "Elwynn", isHeader = true, isCollapsed = false },
{ title = "Linen Quest", questID = 10, level = 12, desc = "Peacebloom grows nearby.", objs = {
{ type = "item", text = "Linen Cloth: 5/5", numRequired = 5, numFulfilled = 5, finished = true },
{ type = "monster", text = "Kobold slain: 1/8", numRequired = 8, numFulfilled = 1 },
} },
{ title = "Second Linen Quest", questID = 11, level = 20, objs = {
{ type = "item", text = "3/10 Linen Cloth (Optional)", numRequired = 10, numFulfilled = 3 },
} },
{ title = "Book Quest", questID = 12, level = 30, objs = {
{ type = "item", text = "Advanced Armorsmithing: Volume II: 0/1", numRequired = 1, numFulfilled = 0 },
} },
{ title = "Westfall", isHeader = true, isCollapsed = true },
{ title = "Collapsed Quest", questID = 20, level = 18, objText = "Bring the Hidden Parcel to Bob.", objs = {
{ type = "item", text = "Red Bandana: 1/15", numRequired = 15, numFulfilled = 1 },
} },
{ title = "Hidden Quest", questID = 30, level = 1, isHidden = true, objs = {
{ type = "item", text = "Secret Thing: 0/1", numRequired = 1, numFulfilled = 0 },
} },
}
end
local function visible()
local out, inCollapsed = {}, false
for _, e in ipairs(log) do
if e.isHeader then
inCollapsed = e.isCollapsed
out[#out + 1] = e
elseif not (hideCollapsed and inCollapsed) then
out[#out + 1] = e
end
end
return out
end
C_QuestLog = {
GetNumQuestLogEntries = function()
local n = 0
for _, e in ipairs(log) do if not e.isHeader then n = n + 1 end end
return #visible(), n
end,
GetInfo = function(i) return visible()[i] end,
GetQuestObjectives = function(id)
for _, e in ipairs(log) do if e.questID == id then return e.objs end end
end,
}
local selectedQuest, abandonQuest = 77, nil
local abandonItems = { [12] = { 500 } }
local completed, popup = {}, false
C_QuestLog.GetSelectedQuest = function() return selectedQuest end
C_QuestLog.SetSelectedQuest = function(id) selectedQuest = id end
C_QuestLog.SetAbandonQuest = function() abandonQuest = selectedQuest end
C_QuestLog.GetAbandonQuestItems = function() return abandonItems[abandonQuest] or {} end
C_QuestLog.IsOnQuest = function(id)
for _, e in ipairs(log) do if e.questID == id then return true end end
return false
end
C_QuestLog.IsQuestFlaggedCompleted = function(id) return completed[id] == true end
function StaticPopup_Visible() return popup end
function GetQuestLogQuestText(i)
local e = visible()[i]
if e.noText then return "", "" end
return e.desc or "Some story.", e.objText or "Do the thing."
end
ITEM_STARTS_QUEST = "This Item Begins a Quest"
function ExpandQuestHeader(i) toggles = toggles + 1; visible()[i].isCollapsed = false end
function CollapseQuestHeader(i) toggles = toggles + 1; visible()[i].isCollapsed = true end
function GetQuestDifficultyColor() return { r = 0.1, g = 0.2, b = 0.3 } end
local names = { [1] = "Linen Cloth", [2] = "Red Bandana", [3] = "Secret Thing", [4] = "Peacebloom", [5] = "Advanced Armorsmithing: Volume II", [500] = "Sealed Letter", [501] = "Old Quest Thing", [502] = "Mystery Thing", [503] = "Odd Starter", [504] = "Abandoned Thing", [506] = "Vial of Purest Water", [507] = "Hidden Parcel" }
local bagCount = { [1] = 7, [2] = 2, [3] = 1, [4] = 9, [5] = 0, [500] = 1, [501] = 1, [502] = 1, [503] = 1, [504] = 1, [505] = 0, [506] = 1, [507] = 1 }
local bankCount = { [1] = 0, [2] = 3 }
C_Item = {
GetItemNameByID = function(id) return names[id] end,
GetItemInfoInstant = function(id) local c = (id >= 501 and id <= 507) and 12 or 7; return id, "", "", "", 0, c end,
GetItemInfo = function() return nil end,
GetItemCount = function(id, incBank) return bagCount[id] + (incBank and (bankCount[id] or 0) or 0) end,
}
local lines = {}
GameTooltip = {
AddDoubleLine = function(_, l, r, _, _, _, rr, rg) lines[#lines + 1] = { l = l, r = r, green = (rr == 0.25 and rg == 1) } end,
AddLine = function(_, l) lines[#lines + 1] = { l = l } end,
Show = function() end,
GetItem = function() return nil end,
}
ItemRefTooltip = GameTooltip
Enum = { TooltipDataType = { Item = 0 } }
local postCall
TooltipDataProcessor = { AddTooltipPostCall = function(_, fn) postCall = fn end }
local handler
local registered = {}
CreateFrame = function()
return {
RegisterEvent = function(_, e) registered[e] = true 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 tip(id)
lines = {}
postCall(GameTooltip, { id = id })
return lines
end
for _, mode in ipairs({ false, true }) do
hideCollapsed = mode
print("--- collapsed headers hide quests: " .. tostring(mode))
reset()
QuestItemTipDB = nil
assert(loadfile(arg[1]))("QuestItemTip", {})
handler(nil, "ADDON_LOADED", "QuestItemTip")
handler(nil, "PLAYER_ENTERING_WORLD")
local l = tip(1)
check(#l == 2, "item needed by two quests gets two lines")
check(l[1].l == "Quest: [12] Linen Quest" and l[1].r == "7 / 5", "have 7, need 5: " .. tostring(l[1].l) .. " | " .. tostring(l[1].r))
check(l[1].green == true, "met requirement is green")
check(l[2].r == "7 / 10" and l[2].green == false, "alternate objective format, unmet: " .. tostring(l[2].r))
l = tip(2)
check(#l == 1 and l[1].l == "Quest: [18] Collapsed Quest", "quest under a collapsed header is found")
check(l[1] and l[1].r == "2 / 15 (+3 bank)", "bank shown apart: " .. tostring(l[1] and l[1].r))
check(log[5].isCollapsed == true, "collapsed header is restored")
check(toggles == (mode and 2 or 0), "headers toggled only when needed: " .. toggles)
check(#tip(3) == 0, "hidden quest ignored")
check(#tip(4) == 0, "non-quest item adds nothing")
l = tip(5)
check(#l == 1 and l[1].r == "0 / 1", "item name containing a colon")
local before = toggles
handler(nil, "QUEST_LOG_UPDATE")
tip(1); tip(2)
check(toggles == before, "header-toggle QUEST_LOG_UPDATE causes no rescan")
table.insert(log[2].objs, { type = "item", text = "Peacebloom: 0/4", numRequired = 4, numFulfilled = 0 })
handler(nil, "QUEST_ACCEPTED")
l = tip(4)
check(#l == 1 and l[1].r == "9 / 4", "new objective picked up after QUEST_ACCEPTED")
log[3] = { title = "Filler", questID = 40, level = 2, objs = {} }
log[#log + 1] = { title = "Another", questID = 41, level = 2, objs = {} }
handler(nil, "QUEST_LOG_UPDATE")
check(#tip(1) == 1, "quest count change rebuilds on QUEST_LOG_UPDATE")
-- === leftover and provided items ===
reset()
assert(loadfile(arg[1]))("QuestItemTip", {})
handler(nil, "ADDON_LOADED", "QuestItemTip")
handler(nil, "PLAYER_ENTERING_WORLD")
selectedQuest = 77
l = tip(500)
check(#l == 1 and l[1].l == "Quest: [30] Book Quest" and l[1].r == nil, "item a quest handed you names it: " .. tostring(l[1] and l[1].l))
check(selectedQuest == 77, "selected quest is restored")
log[#log + 1] = { title = "Done Soon", questID = 50, level = 9, objs = {
{ type = "item", text = "Old Quest Thing: 1/1", numRequired = 1, numFulfilled = 1 },
} }
log[#log + 1] = { title = "Drop Soon", questID = 51, level = 11, objs = {
{ type = "item", text = "Abandoned Thing: 0/2", numRequired = 2, numFulfilled = 0 },
} }
handler(nil, "QUEST_ACCEPTED")
handler(nil, "QUEST_LOG_UPDATE")
check(QuestItemTipDB["old quest thing"] and QuestItemTipDB["old quest thing"].title == "Done Soon", "quest is recorded without hovering anything")
local n = #log
table.remove(log, n); table.remove(log, n - 1)
completed[50] = true
handler(nil, "QUEST_TURNED_IN")
handler(nil, "QUEST_LOG_UPDATE")
l = tip(501)
check(#l == 1 and l[1].l == "Quest done: [9] Done Soon" and l[1].r == "can delete", "finished quest: " .. tostring(l[1] and l[1].l))
l = tip(504)
check(#l == 1 and l[1].l == "Quest dropped: [11] Drop Soon" and l[1].r == "can delete", "dropped quest: " .. tostring(l[1] and l[1].l))
check(#tip(505) == 0, "not owned: no leftover warning")
l = tip(502)
check(#l == 1 and l[1].l == "Quest item: no quest in your log uses it", "unknown quest item gets the soft note, not can-delete")
lines = {}
postCall(GameTooltip, { id = 503, lines = { { leftText = "Odd Starter" }, { leftText = "This Item Begins a Quest" } } })
check(#lines == 0, "quest-starting item is left alone")
check(#tip(4) == 0 or tip(4)[1].l:find("Quest item", 1, true) == nil, "ordinary item never gets the quest-item note")
-- Delivery quest: no objective, the item is only named in the quest text.
l = tip(507)
check(#l == 1 and l[1].l == "Quest: [18] Collapsed Quest", "quest text under a collapsed header: " .. tostring(l[1] and l[1].l))
check(log[5].isCollapsed == true, "header closed again after reading text")
log[#log + 1] = { title = "Delivery Quest", questID = 52, level = 23, objs = {}, objText = "Bring the Vial of Purest Water to Norric Lochthane in Loch Modan." }
QuestItemTipDB[506] = { questID = 99, title = "Earlier Step", level = 22 }
handler(nil, "QUEST_ACCEPTED")
handler(nil, "QUEST_LOG_UPDATE")
l = tip(506)
check(#l == 1 and l[1].l == "Quest: [23] Delivery Quest" and l[1].r == nil, "delivery item names its quest: " .. tostring(l[1] and l[1].l))
check(QuestItemTipDB[506].questID == 52, "delivery item is remembered, replacing the earlier step")
table.remove(log)
handler(nil, "QUEST_REMOVED")
handler(nil, "QUEST_LOG_UPDATE")
l = tip(506)
check(#l == 1 and l[1].l == "Quest dropped: [23] Delivery Quest" and l[1].r == "can delete", "delivery item after the quest ends: " .. tostring(l[1] and l[1].l))
-- Objective read before the item's name has loaded.
log[#log + 1] = { title = "Waterskin", questID = 53, level = 23, objs = {
{ type = "item", text = " : 0/1", numRequired = 1, numFulfilled = 0 },
} }
handler(nil, "QUEST_ACCEPTED")
handler(nil, "QUEST_LOG_UPDATE")
check(QuestItemTipDB[" "] == nil, "blank objective name is not recorded")
log[#log].objs[1].text = "Peacebloom: 0/1"
handler(nil, "QUEST_LOG_UPDATE")
l = tip(4)
check(#l == 1 and l[1].r == "9 / 1", "objective is read once the name loads: " .. tostring(l[1] and l[1].r))
table.remove(log)
handler(nil, "QUEST_REMOVED")
handler(nil, "QUEST_LOG_UPDATE")
-- Still on the quest, but the objective no longer lists the item.
QuestItemTipDB["peacebloom"] = { questID = 12, title = "Book Quest", level = 30 }
l = tip(4)
check(#l == 1 and l[1].l == "Quest: [30] Book Quest" and l[1].r == nil, "still-active recorded quest is not a delete warning")
QuestItemTipDB["peacebloom"] = nil
-- Mid-abandon: do not disturb Blizzard's popup.
popup = true
selectedQuest = 88
local abandonBefore = abandonQuest
handler(nil, "QUEST_ACCEPTED")
handler(nil, "QUEST_LOG_UPDATE")
check(abandonQuest == abandonBefore and selectedQuest == 88, "abandon popup is left alone")
l = tip(500)
check(#l == 1 and l[1].l == "Quest: [30] Book Quest", "provided items are kept meanwhile")
popup = false
printed = {}
SlashCmdList.QUESTITEMTIP()
check(#printed > 0, "/qit prints the index")
end
print(failures == 0 and "ALL PASSED" or (failures .. " FAILED"))
os.exit(failures == 0 and 0 or 1)