diff --git a/Compat/Compat.lua b/Compat/Compat.lua index e78705a..4547a65 100644 --- a/Compat/Compat.lua +++ b/Compat/Compat.lua @@ -720,9 +720,18 @@ end -- Returns a list of quests the character has completed in its lifetime. -- https://wowpedia.fandom.com/wiki/API_GetQuestsCompleted +local function ClearQuestCompleteTable(tbl) + if type(tbl) ~= "table" then return end + for key in pairs(tbl) do + tbl[key] = nil + end +end + function QuestieCompat.GetQuestsCompleted() if not Questie.db.char.complete then Questie.db.char.complete = {} + else + ClearQuestCompleteTable(Questie.db.char.complete) end QueryQuestsCompleted() @@ -732,6 +741,10 @@ end -- Fires when the data requested by QueryQuestsCompleted() is available. -- https://wowpedia.fandom.com/wiki/QUEST_QUERY_COMPLETE function QuestieCompat:QUEST_QUERY_COMPLETE(event) + if not Questie.db.char.complete then + Questie.db.char.complete = {} + end + ClearQuestCompleteTable(Questie.db.char.complete) GetQuestsCompleted(Questie.db.char.complete) local questId = next(Questie.db.char.complete) diff --git a/Tests/QuestieCompatCompletionRefresh_spec.lua b/Tests/QuestieCompatCompletionRefresh_spec.lua new file mode 100644 index 0000000..459e67c --- /dev/null +++ b/Tests/QuestieCompatCompletionRefresh_spec.lua @@ -0,0 +1,31 @@ +local function read(path) + local f = assert(io.open(path, "r"), "cannot open " .. path) + local content = f:read("*a") + f:close() + return content +end + +describe("QuestieCompat completed quest refresh", function() + local compat = read("Compat/Compat.lua") + + it("clears the completed-quest table before repopulating it", function() + local getQuestsStart = assert(compat:find("function QuestieCompat.GetQuestsCompleted()", 1, true)) + local getQuestsEnd = assert(compat:find("-- Fires when the data requested by QueryQuestsCompleted() is available.", getQuestsStart, true)) + local getQuests = compat:sub(getQuestsStart, getQuestsEnd) + + local clearPos = assert(getQuests:find("ClearQuestCompleteTable(Questie.db.char.complete)", 1, true)) + local queryPos = assert(getQuests:find("QueryQuestsCompleted()", 1, true)) + assert.is_true(clearPos < queryPos) + end) + + it("clears stale completions again when the server response arrives", function() + local eventStart = assert(compat:find("function QuestieCompat:QUEST_QUERY_COMPLETE(event)", 1, true)) + local eventEnd = assert(compat:find("-- https://wowpedia.fandom.com/wiki/API_IsQuestFlaggedCompleted", eventStart, true)) + local eventBlock = compat:sub(eventStart, eventEnd) + + local clearPos = assert(eventBlock:find("ClearQuestCompleteTable(Questie.db.char.complete)", 1, true)) + local fillPos = assert(eventBlock:find("GetQuestsCompleted(Questie.db.char.complete)", 1, true)) + + assert.is_true(clearPos < fillPos) + end) +end)