| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { |
| 4 | CraftingStationId, |
| 5 | FIREPLACE_DURATION_MS, |
| 6 | CRAFT_DURATION_MS, |
| 7 | craftingStationDefinition |
| 8 | } from "../../shared/definitions/craftingStations.js"; |
| 9 | import { SkillId } from "../../shared/definitions/skills.js"; |
| 10 | import { ItemId, itemDefinition } from "../../shared/definitions/items.js"; |
| 11 | import { emptySlots } from "../../shared/items/inventory.js"; |
| 12 | import { canCraftFitInventory, CRAFT_DURATION_MS as RECIPE_CRAFT_MS, canStartCraft } from "../../shared/crafting/recipes.js"; |
| 13 | import { recipesForStation, recipeDefinition } from "../../shared/crafting/stationRecipes.js"; |
| 14 | import { CraftingItemId } from "../../shared/definitions/craftingItems.js"; |
| 15 | |
| 16 | test("crafting stations map to the expected skills", () => |
| 17 | { |
| 18 | assert.equal(craftingStationDefinition(CraftingStationId.furnace).skillId, SkillId.smithing); |
| 19 | assert.equal(craftingStationDefinition(CraftingStationId.anvil).skillId, SkillId.smithing); |
| 20 | assert.equal(craftingStationDefinition(CraftingStationId.loom).skillId, SkillId.tailoring); |
| 21 | assert.equal(craftingStationDefinition(CraftingStationId.craftingBench).skillId, SkillId.crafting); |
| 22 | assert.equal(craftingStationDefinition(CraftingStationId.oven).skillId, SkillId.cooking); |
| 23 | assert.equal(craftingStationDefinition(CraftingStationId.fireplace).skillId, SkillId.cooking); |
| 24 | }); |
| 25 | |
| 26 | test("fireplace is portable with a ten minute lifetime", () => |
| 27 | { |
| 28 | const def = craftingStationDefinition(CraftingStationId.fireplace); |
| 29 | assert.equal(def.portable, true); |
| 30 | assert.equal(def.maxPerTile, 1); |
| 31 | assert.equal(def.durationMs, FIREPLACE_DURATION_MS); |
| 32 | assert.equal(FIREPLACE_DURATION_MS, 10 * 60 * 1000); |
| 33 | }); |
| 34 | |
| 35 | test("every craft takes two seconds", () => |
| 36 | { |
| 37 | assert.equal(CRAFT_DURATION_MS, 2000); |
| 38 | assert.equal(RECIPE_CRAFT_MS, CRAFT_DURATION_MS); |
| 39 | }); |
| 40 | |
| 41 | test("canCraftFitInventory accounts for space freed by consumed materials", () => |
| 42 | { |
| 43 | const recipe = { |
| 44 | output: { itemId: ItemId.stick, quantity: 1 }, |
| 45 | inputs: [{ itemId: ItemId.paper, quantity: 2 }] |
| 46 | }; |
| 47 | const slots = emptySlots(); |
| 48 | slots[0] = { itemId: ItemId.paper, quantity: 10 }; |
| 49 | for (let i = 1; i < 32; i++) |
| 50 | { slots[i] = { itemId: ItemId.coins, quantity: 1 }; } |
| 51 | assert.equal(canCraftFitInventory(slots, recipe), false); |
| 52 | slots[31] = null; |
| 53 | assert.equal(canCraftFitInventory(slots, recipe), true); |
| 54 | }); |
| 55 | |
| 56 | test("canCraftFitInventory rejects missing inputs", () => |
| 57 | { |
| 58 | const slots = emptySlots(); |
| 59 | slots[0] = { itemId: ItemId.paper, quantity: 1 }; |
| 60 | assert.equal(canCraftFitInventory(slots, { |
| 61 | output: { itemId: ItemId.stick, quantity: 1 }, |
| 62 | inputs: [{ itemId: ItemId.paper, quantity: 2 }] |
| 63 | }), false); |
| 64 | }); |
| 65 | |
| 66 | test("recipesForStation lists bench recipes", () => |
| 67 | { |
| 68 | const bench = recipesForStation(CraftingStationId.craftingBench); |
| 69 | assert.ok(bench.length >= 1); |
| 70 | assert.equal(bench[0].recipeId, "dev-stick-from-paper"); |
| 71 | }); |
| 72 | |
| 73 | test("canStartCraft enforces minimum skill level", () => |
| 74 | { |
| 75 | const slots = emptySlots(); |
| 76 | slots[0] = { itemId: ItemId.paper, quantity: 5 }; |
| 77 | const recipe = recipeDefinition("dev-stick-from-paper"); |
| 78 | assert.equal(canStartCraft(slots, recipe, 1), true); |
| 79 | assert.equal(canStartCraft(slots, recipe, 0), false); |
| 80 | }); |
| 81 | |
| 82 | test("crafting item stubs are registered", () => |
| 83 | { |
| 84 | assert.equal(itemDefinition(ItemId.pineLog)?.key, "pine-log"); |
| 85 | assert.equal(itemDefinition(ItemId.pineHandle)?.defaultState, "handle"); |
| 86 | assert.equal(itemDefinition(CraftingItemId.bronzeBar)?.key, "bronze-bar"); |
| 87 | }); |
| 88 | |
| 89 | test("woodworking and anvil expose pickaxe chain recipes", () => |
| 90 | { |
| 91 | assert.equal(craftingStationDefinition(CraftingStationId.woodworkingBench).skillId, SkillId.crafting); |
| 92 | const handle = recipeDefinition("handle-pine"); |
| 93 | assert.ok(handle); |
| 94 | assert.equal(handle.stationId, CraftingStationId.woodworkingBench); |
| 95 | assert.equal(handle.output.itemId, ItemId.pineHandle); |
| 96 | const head = recipeDefinition("pickaxe-head-bronze"); |
| 97 | assert.ok(head); |
| 98 | assert.equal(head.stationId, CraftingStationId.anvil); |
| 99 | assert.equal(head.output.itemId, ItemId.bronzePickaxeHead); |
| 100 | const pickaxe = recipeDefinition("pickaxe-bronze"); |
| 101 | assert.ok(pickaxe); |
| 102 | assert.deepEqual(pickaxe.inputs.map((row) => row.itemId).sort(), [ |
| 103 | ItemId.pineHandle, |
| 104 | ItemId.bronzePickaxeHead |
| 105 | ].sort()); |
| 106 | assert.equal(pickaxe.output.itemId, ItemId.bronzePickaxe); |
| 107 | }); |