| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { ITEM_DEFINITIONS, listAllItemPickerEntries } from "../../shared/definitions/items.js"; |
| 4 | import { CRAFTING_ITEM_STUBS } from "../../shared/definitions/craftingItems.js"; |
| 5 | import { LEATHER_ITEM_DEFINITIONS } from "../../shared/definitions/leatherItems.js"; |
| 6 | import { LEATHER_ARMOUR_ITEM_DEFINITIONS } from "../../shared/definitions/leatherArmourItems.js"; |
| 7 | import { WIZARD_ITEM_DEFINITIONS } from "../../shared/definitions/wizardItems.js"; |
| 8 | import { METAL_ITEM_DEFINITIONS } from "../../shared/definitions/metalItems.js"; |
| 9 | import { WOOD_ITEM_DEFINITIONS } from "../../shared/definitions/woodItems.js"; |
| 10 | import { ARMOUR_ITEM_DEFINITIONS } from "../../shared/definitions/armourItems.js"; |
| 11 | import { CLOTH_ITEM_DEFINITIONS } from "../../shared/definitions/clothItems.js"; |
| 12 | import { HERB_ITEM_DEFINITIONS } from "../../shared/definitions/herbs.js"; |
| 13 | import { RANGED_ITEM_DEFINITIONS } from "../../shared/definitions/rangedItems.js"; |
| 14 | import { SPELLBOOK_ITEM_DEFINITIONS } from "../../shared/definitions/spellItems.js"; |
| 15 | import { FISH_ITEM_DEFINITIONS } from "../../shared/definitions/fishItems.js"; |
| 16 | import { GEM_ITEM_DEFINITIONS } from "../../shared/definitions/gemItems.js"; |
| 17 | import { JEWELRY_ITEM_DEFINITIONS } from "../../shared/definitions/jewelryItems.js"; |
| 18 | import { SWORD_ITEM_DEFINITIONS } from "../../shared/definitions/swordItems.js"; |
| 19 | import { WAR_AXE_ITEM_DEFINITIONS } from "../../shared/definitions/warAxeItems.js"; |
| 20 | import { ESSENCE_ITEM_DEFINITIONS } from "../../shared/definitions/essenceItems.js"; |
| 21 | import { DyeItemId, FLOWER_ITEM_DEFINITION, DYE_ITEM_DEFINITION } from "../../shared/definitions/dyes.js"; |
| 22 | import { SpellId, spellDefinition, spellMinMagicLevel } from "../../shared/definitions/spells.js"; |
| 23 | |
| 24 | /** Inline core item ids in items.js before the category spreads. */ |
| 25 | const CORE_ITEM_IDS = Object.freeze([1, 6, 2, 275, 3, 4, 7, 8, 26, 27, 28, 29, 32, 33]); |
| 26 | |
| 27 | /** |
| 28 | * Maps spread into ITEM_DEFINITIONS. Later spreads overwrite earlier ones on |
| 29 | * collision — that is only safe when both sides describe the same item. |
| 30 | */ |
| 31 | const ITEM_DEFINITION_SOURCES = Object.freeze([ |
| 32 | ["core", Object.fromEntries(CORE_ITEM_IDS.map((id) => [id, ITEM_DEFINITIONS[id]]))], |
| 33 | ["crafting", CRAFTING_ITEM_STUBS], |
| 34 | ["leather", LEATHER_ITEM_DEFINITIONS], |
| 35 | ["leatherArmour", LEATHER_ARMOUR_ITEM_DEFINITIONS], |
| 36 | ["wizard", WIZARD_ITEM_DEFINITIONS], |
| 37 | ["metal", METAL_ITEM_DEFINITIONS], |
| 38 | ["wood", WOOD_ITEM_DEFINITIONS], |
| 39 | ["armour", ARMOUR_ITEM_DEFINITIONS], |
| 40 | ["cloth", CLOTH_ITEM_DEFINITIONS], |
| 41 | ["herb", HERB_ITEM_DEFINITIONS], |
| 42 | ["ranged", RANGED_ITEM_DEFINITIONS], |
| 43 | ["spellbook", SPELLBOOK_ITEM_DEFINITIONS], |
| 44 | ["fish", FISH_ITEM_DEFINITIONS], |
| 45 | ["gem", GEM_ITEM_DEFINITIONS], |
| 46 | ["jewelry", JEWELRY_ITEM_DEFINITIONS], |
| 47 | ["sword", SWORD_ITEM_DEFINITIONS], |
| 48 | ["warAxe", WAR_AXE_ITEM_DEFINITIONS], |
| 49 | ["essence", ESSENCE_ITEM_DEFINITIONS], |
| 50 | ["dye", { |
| 51 | [DyeItemId.flower]: FLOWER_ITEM_DEFINITION, |
| 52 | [DyeItemId.dye]: DYE_ITEM_DEFINITION |
| 53 | }] |
| 54 | ]); |
| 55 | |
| 56 | function itemIdentity (def) |
| 57 | { |
| 58 | if (!def) |
| 59 | { return null; } |
| 60 | if (def.key) |
| 61 | { return `key:${def.key}`; } |
| 62 | if (def.consumableSpellId) |
| 63 | { return `spell:${def.consumableSpellId}`; } |
| 64 | if (def.displayName) |
| 65 | { return `name:${def.displayName}`; } |
| 66 | return null; |
| 67 | } |
| 68 | |
| 69 | test("item definition sources do not collide on the same id", () => |
| 70 | { |
| 71 | /** @type {Map<number, { source: string, identity: string, label: string }>} */ |
| 72 | const owner = new Map(); |
| 73 | for (const [source, map] of ITEM_DEFINITION_SOURCES) |
| 74 | { |
| 75 | for (const [key, def] of Object.entries(map)) |
| 76 | { |
| 77 | const id = Number(key); |
| 78 | assert.ok(Number.isFinite(id), `${source} key ${key} is not numeric`); |
| 79 | if (def?.itemId != null) |
| 80 | { assert.equal(def.itemId | 0, id, `${source} id ${id} key/itemId mismatch`); } |
| 81 | const identity = itemIdentity(def); |
| 82 | assert.ok(identity, `${source} id ${id} has no identity`); |
| 83 | const label = def.displayName ?? def.key ?? key; |
| 84 | const prev = owner.get(id); |
| 85 | if (prev && prev.identity !== identity) |
| 86 | { |
| 87 | assert.fail( |
| 88 | `item id ${id} collision: ${prev.source} (${prev.label}) vs ${source} (${label})` |
| 89 | ); |
| 90 | } |
| 91 | owner.set(id, { source, identity, label }); |
| 92 | } |
| 93 | } |
| 94 | }); |
| 95 | |
| 96 | test("merged ITEM_DEFINITIONS keep each source item identity", () => |
| 97 | { |
| 98 | for (const [source, map] of ITEM_DEFINITION_SOURCES) |
| 99 | { |
| 100 | for (const [key, def] of Object.entries(map)) |
| 101 | { |
| 102 | const id = Number(key); |
| 103 | const merged = ITEM_DEFINITIONS[id]; |
| 104 | assert.ok(merged, `${source} item ${id} missing from merge`); |
| 105 | assert.equal(merged.itemId | 0, id); |
| 106 | const expected = itemIdentity(def); |
| 107 | const actual = itemIdentity(merged); |
| 108 | assert.equal( |
| 109 | actual, |
| 110 | expected, |
| 111 | `${source} item ${id} overwritten in merge (${expected} → ${actual})` |
| 112 | ); |
| 113 | } |
| 114 | } |
| 115 | }); |
| 116 | |
| 117 | test("Mana Bullet is a tier-1 learnable attack with a shop-listed spellbook", () => |
| 118 | { |
| 119 | const spell = spellDefinition(SpellId.manaBullet); |
| 120 | assert.ok(spell); |
| 121 | assert.equal(spell.tier, 1); |
| 122 | assert.equal(spell.effect, "damage"); |
| 123 | assert.equal(spell.target, "entity"); |
| 124 | assert.equal(spellMinMagicLevel(spell), 1); |
| 125 | assert.equal(spell.starter, undefined); |
| 126 | |
| 127 | const entry = listAllItemPickerEntries().find((e) => e.displayName === "Spellbook: Mana Bullet"); |
| 128 | assert.ok(entry, "Mana Bullet spellbook missing from shop stock picker"); |
| 129 | assert.equal(entry.itemId, 490); |
| 130 | assert.equal(ITEM_DEFINITIONS[490]?.consumableSpellId, SpellId.manaBullet); |
| 131 | }); |
| 132 | |
| 133 | test("Magic Missile spellbook survives the item catalog merge", () => |
| 134 | { |
| 135 | const entry = listAllItemPickerEntries().find((e) => e.displayName === "Spellbook: Magic Missile"); |
| 136 | assert.ok(entry); |
| 137 | assert.equal(entry.itemId, 489); |
| 138 | assert.equal(ITEM_DEFINITIONS[489]?.consumableSpellId, SpellId.magicMissile); |
| 139 | assert.equal(ITEM_DEFINITIONS[402]?.key, "tourmaline-cut"); |
| 140 | }); |