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

218 lines
8.9 KiB
Lua

-- Runs QuestCraftHighlight.lua against a mock of the WoW API.
-- arg[1] = addon file
local log = {
{ title = "Leather", isHeader = true },
{ title = "Moonglow Vest", questID = 1, level = 18, objs = {
{ type = "item", text = "Embossed Leather Gloves: 0/1", numRequired = 1, numFulfilled = 0 },
} },
{ title = "Bars One", questID = 2, level = 10, objs = {
{ type = "item", text = "Copper Bar: 0/4", numRequired = 4, numFulfilled = 0 },
} },
{ title = "Bars Two", questID = 3, level = 12, objs = {
{ type = "item", text = "Copper Bar: 0/6", numRequired = 6, numFulfilled = 0 },
} },
}
C_QuestLog = {
GetNumQuestLogEntries = function()
local n = 0
for _, e in ipairs(log) do if not e.isHeader then n = n + 1 end end
return #log, n
end,
GetInfo = function(i) return log[i] end,
GetQuestObjectives = function(id)
for _, e in ipairs(log) do if e.questID == id then return e.objs end end
end,
}
function ExpandQuestHeader() end
function CollapseQuestHeader() end
function GetQuestDifficultyColor() return { r = 0.1, g = 0.2, b = 0.3 } end
local outputs = { [100] = 1, [101] = 2, [102] = 3, [103] = 4 }
C_TradeSkillUI = {
GetRecipeSchematic = function(id) return { outputItemID = outputs[id] } end,
}
local names = { [1] = "Embossed Leather Gloves", [2] = "Copper Bar", [3] = "Embossed Leather Cloak" }
local counts = { [1] = 0, [2] = 3, [3] = 0, [4] = 0 }
local requested = {}
C_Item = {
GetItemNameByID = function(id) return names[id] end,
GetItemCount = function(id) return counts[id] end,
RequestLoadItemDataByID = function(id) requested[id] = true end,
}
local queued = {}
C_Timer = { After = function(_, fn) queued[#queued + 1] = fn end }
local function flush() local q = queued; queued = {}; for _, fn in ipairs(q) do fn() end end
function hooksecurefunc(t, name, fn)
local orig = t[name]
t[name] = function(...) orig(...); fn(...) end
end
local function region(extra)
local r = { shown = true }
function r:Show() self.shown = true end
function r:Hide() self.shown = false end
function r:SetShown(v) self.shown = v and true or false end
function r:IsShown() return self.shown end
function r:SetAllPoints() end
function r:ClearAllPoints() end
function r:SetPoint(...) self.point = { ... } end
function r:SetColorTexture() end
function r:SetText(t) self.text = t end
function r:GetStringWidth() return self.textWidth or (#(self.text or "") * 6) end
function r:SetTextColor(a, b, c) self.color = { a, b, c } end
function r:SetWidth(w) self.width = w end
function r:GetWidth() return self.width or 0 end
for k, v in pairs(extra or {}) do r[k] = v end
return r
end
local tipLines, tipOwner = {}, nil
GameTooltip = {
IsOwned = function(_, f) return tipOwner == f end,
SetOwner = function(_, f) tipOwner = f; tipLines = {} end,
AddDoubleLine = function(_, l, r) tipLines[#tipLines + 1] = { l = l, r = r } end,
Show = function() end,
}
local handler
CreateFrame = function()
return { RegisterEvent = 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
-- The mixin only appears when Blizzard's load-on-demand addon does.
assert(loadfile(arg[1]))("QuestCraftHighlight", {})
ProfessionsRecipeListRecipeMixin = {
Init = function(row, node)
row.Label:SetText(node:GetData().recipeInfo.name)
row.Label:SetWidth(row:GetStringWidthForTest())
end,
OnEnter = function() end,
}
handler(nil, "ADDON_LOADED", "SomethingElse")
local initBefore = ProfessionsRecipeListRecipeMixin.Init
handler(nil, "ADDON_LOADED", "Blizzard_ProfessionsTemplates")
check(ProfessionsRecipeListRecipeMixin.Init ~= initBefore, "hooks installed when Blizzard_ProfessionsTemplates loads")
local initOnce = ProfessionsRecipeListRecipeMixin.Init
handler(nil, "ADDON_LOADED", "Blizzard_ProfessionsTemplates")
check(ProfessionsRecipeListRecipeMixin.Init == initOnce, "hooks are not installed twice")
handler(nil, "PLAYER_ENTERING_WORLD")
flush()
local function newRow()
local row = region({ width = 270, visible = true })
row.Label = region()
row.Count = region({ shown = false })
row.LockedIcon = region({ shown = false, width = 17 })
row.SkillUps = region({ width = 26 })
function row:CreateTexture() return region() end
function row:CreateFontString() return region() end
function row:IsVisible() return self.visible end
function row:GetStringWidthForTest() return self.Label:GetStringWidth() end
return row
end
local function node(id, name) return { GetData = function() return { recipeInfo = { recipeID = id, name = name } } end } end
local function init(row, id, name) ProfessionsRecipeListRecipeMixin.Init(row, node(id, name)) end
local row = newRow()
init(row, 100, "Embossed Leather Gloves")
check(row.qchTint and row.qchTint.shown, "needed recipe is tinted")
check(row.qchText.text == "! 0 / 1", "shows have / need: " .. tostring(row.qchText.text))
check(row.qchText.color[2] == 0.82, "unfinished count is gold")
ProfessionsRecipeListRecipeMixin.OnEnter(row)
check(#tipLines == 1 and tipLines[1].l == "Quest: [18] Moonglow Vest" and tipLines[1].r == "0 / 1", "hover shows quest, level, count")
check(tipOwner == row, "hover takes the tooltip when the row did not")
tipOwner = row.Label; tipLines = { { l = "Full Recipe Name" } }
ProfessionsRecipeListRecipeMixin.OnEnter(row)
check(#tipLines == 2 and tipLines[1].l == "Full Recipe Name", "keeps the truncated-name tooltip and appends")
counts[1] = 1
handler(nil, "BAG_UPDATE_DELAYED")
flush()
check(row.qchText.text == "! 1 / 1" and row.qchText.color[2] == 1, "crafting one updates the row live, count turns green")
check(row.qchTint.shown == false, "tint goes away once you have enough")
local bars = newRow()
init(bars, 101, "Smelt Copper")
check(bars.qchText and bars.qchText.text == "! 3 / 10", "matches output item, not recipe name, and sums quests: " .. tostring(bars.qchText and bars.qchText.text))
ProfessionsRecipeListRecipeMixin.OnEnter(bars)
check(#tipLines == 2 and tipLines[1].r == "3 / 4" and tipLines[2].r == "3 / 6", "tooltip lists each quest")
local other = newRow()
init(other, 102, "Embossed Leather Cloak")
check(other.qchTint == nil, "unrelated recipe is left untouched")
-- Pool reuse: a highlighted row scrolled onto an unrelated recipe.
counts[1] = 0
init(row, 102, "Embossed Leather Cloak")
check(row.qchTint.shown == false and row.qchText.shown == false, "reused row drops its highlight")
ProfessionsRecipeListRecipeMixin.OnEnter(row)
tipLines = {}; ProfessionsRecipeListRecipeMixin.OnEnter(row)
check(#tipLines == 0, "reused row adds no quest line on hover")
-- Label must not run under the count text.
local long = newRow()
names[1] = "Embossed Leather Gloves"
long.Label.textWidth = 500
init(long, 100, "Embossed Leather Gloves")
local avail = 270 - (10 + 26 + long.qchText:GetStringWidth() + 8)
check(long.Label.width == avail, "long name is shortened to clear the count: " .. tostring(long.Label.width) .. " vs " .. avail)
-- Output name not loaded yet.
local uncached = newRow()
log[#log + 1] = { title = "Mystery", questID = 4, level = 5, objs = {
{ type = "item", text = "Late Item: 0/2", numRequired = 2, numFulfilled = 0 },
} }
handler(nil, "QUEST_ACCEPTED")
flush()
init(uncached, 103, "Some Recipe Name")
check(requested[4] == true, "requests the name of an unloaded item")
check(uncached.qchMatch == nil, "no false match while name is loading")
names[4] = "Late Item"
handler(nil, "ITEM_DATA_LOAD_RESULT", 4, true)
flush()
check(uncached.qchText and uncached.qchText.text == "! 0 / 2", "row updates when the name arrives")
local fallback = newRow()
outputs[104] = 5; counts[5] = 0
init(fallback, 104, "Embossed Leather Gloves")
check(fallback.qchText and fallback.qchText.text == "! 0 / 1", "unloaded item falls back to the recipe name")
-- Quest gone: highlight disappears, name gets its width back.
table.remove(log, 2)
long.visible = true
handler(nil, "QUEST_REMOVED")
flush()
check(long.qchMatch == nil and long.qchTint.shown == false, "finished quest removes the highlight")
check(long.Label.width == 270 - 36, "name gets its width back: " .. tostring(long.Label.width))
-- Closed window: nothing to do for hidden rows.
log[#log + 1] = { title = "Again", questID = 9, level = 18, objs = {
{ type = "item", text = "Embossed Leather Gloves: 0/1", numRequired = 1, numFulfilled = 0 },
} }
long.visible = false
handler(nil, "QUEST_ACCEPTED")
flush()
check(long.qchMatch == nil, "hidden rows are not touched")
printed = {}
SlashCmdList.QUESTCRAFTHIGHLIGHT()
check(#printed >= 2 and printed[1]:find("hooked=true", 1, true) ~= nil, "/qch prints state: " .. tostring(printed[1]))
print(failures == 0 and "ALL PASSED" or (failures .. " FAILED"))
os.exit(failures == 0 and 0 or 1)