|
| 1 | +-- Tests for the bench craft evaluation feature: |
| 2 | +-- - Free slot detection: affixMax from rarity, explicit mod counting |
| 3 | +-- - Craft filtering: craft.types[item.type] gate |
| 4 | +-- - Skip conditions: corrupted/mirrored items |
| 5 | +-- - Weight selection: best bench craft wins, nil when no improvement |
| 6 | + |
| 7 | +local function makeHelm(explicits, corrupted, mirrored) |
| 8 | + local lines = { "Rarity: Rare", "Test Helm", "Vine Circlet" } |
| 9 | + for _, mod in ipairs(explicits or {}) do |
| 10 | + table.insert(lines, mod) |
| 11 | + end |
| 12 | + if corrupted then table.insert(lines, "Corrupted") end |
| 13 | + if mirrored then table.insert(lines, "Mirrored") end |
| 14 | + return table.concat(lines, "\n") |
| 15 | +end |
| 16 | + |
| 17 | +local function makeRing(explicits, corrupted, mirrored) |
| 18 | + local lines = { "Rarity: Rare", "Test Ring", "Coral Ring" } |
| 19 | + for _, mod in ipairs(explicits or {}) do |
| 20 | + table.insert(lines, mod) |
| 21 | + end |
| 22 | + if corrupted then table.insert(lines, "Corrupted") end |
| 23 | + if mirrored then table.insert(lines, "Mirrored") end |
| 24 | + return table.concat(lines, "\n") |
| 25 | +end |
| 26 | + |
| 27 | +-- Mirrors affixMax logic in GetResultEvaluation |
| 28 | +-- prefixes.limit and suffixes.limit are deltas on top of the rarity default |
| 29 | +local function getAffixMax(item) |
| 30 | + if item.rarity == "RARE" then |
| 31 | + local defaultHalf = (item.type == "Jewel") and 2 or 3 |
| 32 | + return (item.prefixes.limit or 0) + defaultHalf + (item.suffixes.limit or 0) + defaultHalf |
| 33 | + elseif item.rarity == "MAGIC" then |
| 34 | + return (item.prefixes.limit or 0) + 1 + (item.suffixes.limit or 0) + 1 |
| 35 | + end |
| 36 | + return 0 |
| 37 | +end |
| 38 | + |
| 39 | +-- Mirrors explicit count logic in GetResultEvaluation (all mods, including crafted) |
| 40 | +local function countExplicit(item) |
| 41 | + return #(item.explicitModLines or {}) |
| 42 | +end |
| 43 | + |
| 44 | +describe("Bench Craft Eval — free slot detection", function() |
| 45 | + -- Pass: 3 mods on a rare leaves 3 free slots detected |
| 46 | + -- Fail: affixMax computed wrong or explicit count wrong → bench craft skipped when slots are free |
| 47 | + it("detects free slots on a rare with 3 mods", function() |
| 48 | + local item = new("Item", makeHelm({ |
| 49 | + "+50 to maximum Life", |
| 50 | + "+30% to Fire Resistance", |
| 51 | + "+25% to Cold Resistance", |
| 52 | + })) |
| 53 | + local affixMax = getAffixMax(item) |
| 54 | + local explicit = countExplicit(item) |
| 55 | + assert.are.equal(6, affixMax) |
| 56 | + assert.are.equal(3, explicit) |
| 57 | + assert.is_true(explicit < affixMax) |
| 58 | + end) |
| 59 | + |
| 60 | + -- Pass: 6 mods on a rare → no free slots |
| 61 | + -- Fail: wrong count → bench craft attempted on a full item |
| 62 | + it("detects no free slots on a full rare (6 mods)", function() |
| 63 | + local item = new("Item", makeHelm({ |
| 64 | + "+50 to maximum Life", |
| 65 | + "+30% to Fire Resistance", |
| 66 | + "+25% to Cold Resistance", |
| 67 | + "+40 to Strength", |
| 68 | + "+35% to Lightning Resistance", |
| 69 | + "10% increased Attack Speed", |
| 70 | + })) |
| 71 | + local affixMax = getAffixMax(item) |
| 72 | + local explicit = countExplicit(item) |
| 73 | + assert.are.equal(6, affixMax) |
| 74 | + assert.are.equal(6, explicit) |
| 75 | + assert.is_false(explicit < affixMax) |
| 76 | + end) |
| 77 | + |
| 78 | + -- Pass: 1 mod on a magic item leaves 1 free slot |
| 79 | + -- Fail: magic item gets wrong affixMax (6) → bench craft attempted too eagerly |
| 80 | + it("affixMax is 2 for magic items", function() |
| 81 | + local lines = "Rarity: Magic\nTest\nVine Circlet\n+50 to maximum Life" |
| 82 | + local item = new("Item", lines) |
| 83 | + local affixMax = getAffixMax(item) |
| 84 | + assert.are.equal(2, affixMax) |
| 85 | + assert.is_true(countExplicit(item) < affixMax) |
| 86 | + end) |
| 87 | + |
| 88 | + -- Pass: unique item gets affixMax 0 → bench craft never triggered |
| 89 | + -- Fail: unique items get bench-crafted → nonsensical evaluation |
| 90 | + it("affixMax is 0 for unique items", function() |
| 91 | + local lines = "Rarity: Unique\nShav\nShavronnes Wrappings\n+50 to maximum Life" |
| 92 | + local item = new("Item", lines) |
| 93 | + local affixMax = getAffixMax(item) |
| 94 | + assert.are.equal(0, affixMax) |
| 95 | + end) |
| 96 | + |
| 97 | + -- Pass: Eldritch implicit "+1 prefix allowed" raises affixMax to 7 on a rare helmet |
| 98 | + -- Fail: fixed affixMax=6 used → item with Eldritch +1 prefix has 7 mods but bench craft still triggered |
| 99 | + it("affixMax accounts for +1 prefix modifiers allowed (Eldritch implicit)", function() |
| 100 | + local item = new("Item", makeHelm({ |
| 101 | + "+50 to maximum Life", |
| 102 | + "+30% to Fire Resistance", |
| 103 | + "+25% to Cold Resistance", |
| 104 | + "+40 to Strength", |
| 105 | + "+35% to Lightning Resistance", |
| 106 | + "10% increased Attack Speed", |
| 107 | + "+1 Prefix Modifier allowed", |
| 108 | + })) |
| 109 | + local affixMax = getAffixMax(item) |
| 110 | + -- prefixes.limit = 1 (delta), suffixes.limit = nil (0 delta), so affixMax = (0+1+3) + (0+3) = 7 |
| 111 | + assert.are.equal(7, affixMax) |
| 112 | + -- 7 mods on the item → no free slot |
| 113 | + assert.is_false(countExplicit(item) < affixMax) |
| 114 | + end) |
| 115 | + |
| 116 | + -- Pass: crafted mod counts as an occupied slot → prevents simulating a second crafted mod |
| 117 | + -- Fail: crafted mod excluded → item with 5 regular + 1 crafted = 5 counted, bench craft attempted, simulation adds 7th mod |
| 118 | + it("counts crafted mods as occupying an explicit slot", function() |
| 119 | + local item = new("Item", makeHelm({ |
| 120 | + "+50 to maximum Life", |
| 121 | + "+30% to Fire Resistance", |
| 122 | + "{crafted}10% increased Attack Speed", |
| 123 | + })) |
| 124 | + local explicit = countExplicit(item) |
| 125 | + -- All 3 mods (including the crafted one) occupy slots |
| 126 | + assert.are.equal(3, explicit) |
| 127 | + end) |
| 128 | +end) |
| 129 | + |
| 130 | +describe("Bench Craft Eval — skip conditions", function() |
| 131 | + -- Pass: corrupted flag detected → bench craft simulation skipped |
| 132 | + -- Fail: corrupted items get simulated → invalid crafting on corrupted item shown to user |
| 133 | + it("marks corrupted item correctly", function() |
| 134 | + local item = new("Item", makeHelm({ "+50 to maximum Life" }, true)) |
| 135 | + assert.is_true(item.corrupted) |
| 136 | + end) |
| 137 | + |
| 138 | + -- Pass: mirrored flag detected → bench craft simulation skipped |
| 139 | + -- Fail: mirrored items get simulated → invalid crafting on mirrored item shown to user |
| 140 | + it("marks mirrored item correctly", function() |
| 141 | + local item = new("Item", makeHelm({ "+50 to maximum Life" }, false, true)) |
| 142 | + assert.is_true(item.mirrored) |
| 143 | + end) |
| 144 | + |
| 145 | + -- Pass: normal item is neither corrupted nor mirrored |
| 146 | + -- Fail: normal item wrongly flagged → bench craft silently skipped on craftable items |
| 147 | + it("normal rare item is not corrupted or mirrored", function() |
| 148 | + local item = new("Item", makeHelm({ "+50 to maximum Life" })) |
| 149 | + assert.is_not_true(item.corrupted) |
| 150 | + assert.is_not_true(item.mirrored) |
| 151 | + end) |
| 152 | +end) |
| 153 | + |
| 154 | +describe("Bench Craft Eval — craft type filtering", function() |
| 155 | + -- Mirrors the craft.types[item.type] gate in GetResultEvaluation |
| 156 | + |
| 157 | + local function craftAppliesToType(craft, itemType) |
| 158 | + return craft.types ~= nil and craft.types[itemType] == true |
| 159 | + end |
| 160 | + |
| 161 | + -- Pass: craft with matching type is accepted |
| 162 | + -- Fail: gate broken → wrong item types get bench-crafted, causing invalid simulations |
| 163 | + it("accepts a craft whose types include the item type", function() |
| 164 | + local craft = { types = { Helmet = true, Gloves = true }, type = "Suffix" } |
| 165 | + assert.is_true(craftAppliesToType(craft, "Helmet")) |
| 166 | + assert.is_true(craftAppliesToType(craft, "Gloves")) |
| 167 | + end) |
| 168 | + |
| 169 | + -- Pass: craft without matching type is rejected |
| 170 | + -- Fail: all crafts tried on all items → incorrect evaluation with weapon-only mods on armour |
| 171 | + it("rejects a craft whose types do not include the item type", function() |
| 172 | + local craft = { types = { Weapon = true }, type = "Prefix" } |
| 173 | + assert.is_false(craftAppliesToType(craft, "Helmet")) |
| 174 | + end) |
| 175 | + |
| 176 | + -- Pass: craft with nil types is safely rejected |
| 177 | + -- Fail: nil types crash the gate → runtime error during bench craft loop |
| 178 | + it("safely rejects craft with nil types", function() |
| 179 | + local craft = { type = "Prefix" } |
| 180 | + assert.is_false(craftAppliesToType(craft, "Helmet")) |
| 181 | + end) |
| 182 | +end) |
| 183 | + |
| 184 | +describe("Bench Craft Eval — weight selection", function() |
| 185 | + -- Mirrors the best-weight selection loop in GetResultEvaluation |
| 186 | + |
| 187 | + local function simulateBenchCraftSelection(baseWeight, crafts) |
| 188 | + local weight = baseWeight |
| 189 | + local benchCraft = nil |
| 190 | + for _, craft in ipairs(crafts) do |
| 191 | + if craft.craftWeight > weight then |
| 192 | + weight = craft.craftWeight |
| 193 | + benchCraft = craft.label |
| 194 | + end |
| 195 | + end |
| 196 | + return weight, benchCraft |
| 197 | + end |
| 198 | + |
| 199 | + -- Pass: best craft is selected when it beats the base weight |
| 200 | + -- Fail: wrong craft selected → user shown a suboptimal bench craft hint |
| 201 | + it("selects the craft with the highest weight above base", function() |
| 202 | + local weight, benchCraft = simulateBenchCraftSelection(1.0, { |
| 203 | + { label = "adds life (Prefix)", craftWeight = 1.2 }, |
| 204 | + { label = "adds es (Prefix)", craftWeight = 1.5 }, |
| 205 | + { label = "adds armour (Suffix)", craftWeight = 1.1 }, |
| 206 | + }) |
| 207 | + assert.are.equal(1.5, weight) |
| 208 | + assert.are.equal("adds es (Prefix)", benchCraft) |
| 209 | + end) |
| 210 | + |
| 211 | + -- Pass: benchCraft is nil when no craft beats base weight |
| 212 | + -- Fail: a worse craft is stored → tooltip shows misleading hint that doesn't improve the item |
| 213 | + it("returns nil benchCraft when no craft improves the base weight", function() |
| 214 | + local weight, benchCraft = simulateBenchCraftSelection(1.5, { |
| 215 | + { label = "adds life (Prefix)", craftWeight = 1.2 }, |
| 216 | + { label = "adds es (Prefix)", craftWeight = 1.3 }, |
| 217 | + }) |
| 218 | + assert.are.equal(1.5, weight) |
| 219 | + assert.is_nil(benchCraft) |
| 220 | + end) |
| 221 | + |
| 222 | + -- Pass: empty craft list leaves weight and benchCraft unchanged |
| 223 | + -- Fail: crash or wrong defaults → bench craft code fails on items with no applicable mods |
| 224 | + it("handles empty craft list gracefully", function() |
| 225 | + local weight, benchCraft = simulateBenchCraftSelection(1.0, {}) |
| 226 | + assert.are.equal(1.0, weight) |
| 227 | + assert.is_nil(benchCraft) |
| 228 | + end) |
| 229 | + |
| 230 | + -- Pass: single improving craft is always selected |
| 231 | + -- Fail: off-by-one in > comparison → craft equal to base is wrongly selected |
| 232 | + it("requires strictly greater weight to select a craft", function() |
| 233 | + local weight, benchCraft = simulateBenchCraftSelection(1.0, { |
| 234 | + { label = "equal craft (Prefix)", craftWeight = 1.0 }, |
| 235 | + }) |
| 236 | + assert.are.equal(1.0, weight) |
| 237 | + assert.is_nil(benchCraft) -- equal weight does not trigger selection |
| 238 | + end) |
| 239 | + |
| 240 | + -- Pass: only the highest craft wins over all others |
| 241 | + -- Fail: first-wins logic → suboptimal craft shown when a later one is better |
| 242 | + it("always picks the globally best craft, not just the first improving one", function() |
| 243 | + local weight, benchCraft = simulateBenchCraftSelection(1.0, { |
| 244 | + { label = "craft_a (Prefix)", craftWeight = 1.3 }, |
| 245 | + { label = "craft_b (Prefix)", craftWeight = 1.8 }, |
| 246 | + { label = "craft_c (Suffix)", craftWeight = 1.6 }, |
| 247 | + }) |
| 248 | + assert.are.equal(1.8, weight) |
| 249 | + assert.are.equal("craft_b (Prefix)", benchCraft) |
| 250 | + end) |
| 251 | +end) |
| 252 | + |
| 253 | +describe("Bench Craft Eval — item parsing with crafted mod", function() |
| 254 | + -- Pass: crafted mod line added to explicitModLines is flagged correctly |
| 255 | + -- Fail: crafted flag missing → crafted mod counted as an occupied explicit slot |
| 256 | + it("item with a crafted mod has the crafted flag set on that mod line", function() |
| 257 | + local item = new("Item", makeRing({ |
| 258 | + "+50 to maximum Life", |
| 259 | + "{crafted}+35% to Cold Resistance", |
| 260 | + })) |
| 261 | + local craftedCount = 0 |
| 262 | + for _, modLine in ipairs(item.explicitModLines or {}) do |
| 263 | + if modLine.crafted then |
| 264 | + craftedCount = craftedCount + 1 |
| 265 | + end |
| 266 | + end |
| 267 | + assert.are.equal(1, craftedCount) |
| 268 | + end) |
| 269 | + |
| 270 | + -- Pass: total explicit count (including crafted) is 2 |
| 271 | + -- Fail: wrong total → logic relying on total explicit count is broken |
| 272 | + it("total explicitModLines count includes crafted mods", function() |
| 273 | + local item = new("Item", makeRing({ |
| 274 | + "+50 to maximum Life", |
| 275 | + "{crafted}+35% to Cold Resistance", |
| 276 | + })) |
| 277 | + assert.are.equal(2, #item.explicitModLines) |
| 278 | + end) |
| 279 | +end) |
0 commit comments