| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { BlockType } from "../../shared/definitions/tiles.js"; |
| 4 | import { ItemId, itemDefinition, itemDescription } from "../../shared/definitions/items.js"; |
| 5 | import { flowerItemInstance } from "../../shared/definitions/dyes.js"; |
| 6 | import { AlchemyStepKind } from "../../shared/definitions/alchemy.js"; |
| 7 | import |
| 8 | { |
| 9 | evaluateAlchemyBottle, |
| 10 | hashAlchemySteps, |
| 11 | resolvedAlchemyPotions, |
| 12 | unstablePotionFromSteps, |
| 13 | potionItemInstance, |
| 14 | recipePaperBody, |
| 15 | formatAlchemyStep |
| 16 | } from "../../shared/alchemy/recipes.js"; |
| 17 | import { alchemyPotionDefinition } from "../../shared/definitions/alchemy.js"; |
| 18 | import { emptyEquipment, emptySlots, insertStack } from "../../shared/items/inventory.js"; |
| 19 | import { createEmptyChunkData, encodeChunkBlob } from "../src/world/chunkData.js"; |
| 20 | import { Component } from "../src/ecs/components.js"; |
| 21 | import |
| 22 | { |
| 23 | makeCharacter, |
| 24 | makeSimulation, |
| 25 | stepTimes |
| 26 | } from "./helpers/simulationHarness.js"; |
| 27 | |
| 28 | function placeCauldron (worldDb, tileX, tileY) |
| 29 | { |
| 30 | const chunkX = Math.floor(tileX / 16); |
| 31 | const chunkY = Math.floor(tileY / 16); |
| 32 | const data = createEmptyChunkData(); |
| 33 | data.open = true; |
| 34 | const localX = tileX - chunkX * 16; |
| 35 | const localY = tileY - chunkY * 16; |
| 36 | data.blocks[localY * 16 + localX] = { typeId: BlockType.cauldron, state: 0 }; |
| 37 | worldDb.saveChunkVersion( |
| 38 | chunkX, |
| 39 | chunkY, |
| 40 | worldDb.getActiveRevisionId(), |
| 41 | true, |
| 42 | encodeChunkBlob(data) |
| 43 | ); |
| 44 | } |
| 45 | |
| 46 | function countItem (slots, itemId) |
| 47 | { |
| 48 | return (slots ?? []) |
| 49 | .filter((slot) => slot?.itemId === itemId) |
| 50 | .reduce((sum, slot) => sum + (slot.quantity | 0), 0); |
| 51 | } |
| 52 | |
| 53 | test("recipe paper body resolves ingredient keys to display names", () => |
| 54 | { |
| 55 | const potion = alchemyPotionDefinition("tanningBrew"); |
| 56 | assert.ok(potion); |
| 57 | const body = recipePaperBody(potion); |
| 58 | assert.match(body, /2× Oak bark/); |
| 59 | assert.match(body, /1× Rock salt/); |
| 60 | assert.match(body, /1× Bottle of Animal Fat \(full bottle\)/); |
| 61 | assert.doesNotMatch(body, /oak-bark/); |
| 62 | assert.doesNotMatch(body, /rock-salt/); |
| 63 | assert.match(body, /- insert Oak bark/); |
| 64 | assert.match(body, /- insert Rock salt/); |
| 65 | assert.equal( |
| 66 | formatAlchemyStep({ kind: AlchemyStepKind.insert, itemKey: "oak-bark" }), |
| 67 | "insert Oak bark" |
| 68 | ); |
| 69 | }); |
| 70 | |
| 71 | test("potion item descriptions come from alchemy defs without repeating the name", () => |
| 72 | { |
| 73 | const stack = { |
| 74 | itemId: ItemId.potion, |
| 75 | quantity: 1, |
| 76 | instance: { |
| 77 | kind: "potion", |
| 78 | potionKey: "lesserHealthTonic", |
| 79 | displayName: "Lesser health tonic", |
| 80 | unstable: false |
| 81 | } |
| 82 | }; |
| 83 | const desc = itemDescription(stack); |
| 84 | assert.equal(desc, alchemyPotionDefinition("lesserHealthTonic").description); |
| 85 | assert.doesNotMatch(desc, /Lesser health tonic/i); |
| 86 | assert.equal( |
| 87 | itemDescription({ |
| 88 | itemId: ItemId.potion, |
| 89 | quantity: 1, |
| 90 | instance: { kind: "potion", potionKey: "x", displayName: "Bad", unstable: true } |
| 91 | }), |
| 92 | "The brew smells wrong — best sipped with caution." |
| 93 | ); |
| 94 | }); |
| 95 | |
| 96 | test("lesser health tonic ratios and steps evaluate to a success batch", () => |
| 97 | { |
| 98 | const potion = resolvedAlchemyPotions().find((entry) => entry.potionKey === "lesserHealthTonic"); |
| 99 | assert.ok(potion); |
| 100 | const evaluation = evaluateAlchemyBottle({ |
| 101 | skills: { levels: { alchemy: 1 } }, |
| 102 | brewStacks: [ |
| 103 | { itemId: ItemId.thyme, quantity: 2 }, |
| 104 | { itemId: ItemId.mint, quantity: 2 } |
| 105 | ], |
| 106 | takenSteps: [ |
| 107 | { kind: AlchemyStepKind.insert, itemId: ItemId.thyme }, |
| 108 | { kind: AlchemyStepKind.heat }, |
| 109 | { kind: AlchemyStepKind.insert, itemId: ItemId.mint }, |
| 110 | { kind: AlchemyStepKind.stir } |
| 111 | ] |
| 112 | }); |
| 113 | assert.equal(evaluation.success, true); |
| 114 | assert.equal(evaluation.batch, 2); |
| 115 | assert.equal(evaluation.result.potionKey, "lesserHealthTonic"); |
| 116 | }); |
| 117 | |
| 118 | test("same wrong steps hash to the same unstable potion", () => |
| 119 | { |
| 120 | const steps = [ |
| 121 | { kind: AlchemyStepKind.heat }, |
| 122 | { kind: AlchemyStepKind.stir }, |
| 123 | { kind: AlchemyStepKind.cool } |
| 124 | ]; |
| 125 | const a = unstablePotionFromSteps(steps); |
| 126 | const b = unstablePotionFromSteps(steps); |
| 127 | assert.equal(a.potionKey, b.potionKey); |
| 128 | assert.equal(a.stepsHash, hashAlchemySteps(steps)); |
| 129 | assert.ok(["stamina-drain", "glow", "shrink", "blind"].includes(a.effectKey)); |
| 130 | }); |
| 131 | |
| 132 | test("cauldron bottles lesser health tonic when steps and ratios match", () => |
| 133 | { |
| 134 | const harness = makeSimulation({ workerCount: 1 }); |
| 135 | try |
| 136 | { |
| 137 | placeCauldron(harness.worldDb, 8, 8); |
| 138 | harness.simulation.reloadWorld(); |
| 139 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 }); |
| 140 | const inventory = emptySlots(); |
| 141 | inventory[0] = { itemId: ItemId.thyme, quantity: 2 }; |
| 142 | inventory[1] = { itemId: ItemId.mint, quantity: 2 }; |
| 143 | inventory[2] = { itemId: ItemId.glassBottle, quantity: 2 }; |
| 144 | inventory[3] = { itemId: ItemId.paper, quantity: 1 }; |
| 145 | harness.stateDb.saveBag(character.character_id, { |
| 146 | inventory, |
| 147 | equipment: emptyEquipment(), |
| 148 | vault: [], |
| 149 | vaultReserved: [] |
| 150 | }); |
| 151 | const entityId = harness.simulation.spawnCharacter(1, character); |
| 152 | const cid = character.character_id; |
| 153 | const alchemyBefore = harness.stateDb.getSkills(cid).xp.alchemy ?? 0; |
| 154 | |
| 155 | harness.simulation.enqueue(1, { type: "open-cauldron", characterId: cid }); |
| 156 | harness.simulation.step(); |
| 157 | |
| 158 | harness.simulation.enqueue(1, { |
| 159 | type: "cauldron-insert", |
| 160 | characterId: cid, |
| 161 | itemId: ItemId.thyme, |
| 162 | quantity: 2 |
| 163 | }); |
| 164 | harness.simulation.enqueue(1, { |
| 165 | type: "cauldron-action", |
| 166 | characterId: cid, |
| 167 | action: AlchemyStepKind.heat |
| 168 | }); |
| 169 | harness.simulation.enqueue(1, { |
| 170 | type: "cauldron-insert", |
| 171 | characterId: cid, |
| 172 | itemId: ItemId.mint, |
| 173 | quantity: 2 |
| 174 | }); |
| 175 | harness.simulation.enqueue(1, { |
| 176 | type: "cauldron-action", |
| 177 | characterId: cid, |
| 178 | action: AlchemyStepKind.stir |
| 179 | }); |
| 180 | harness.simulation.step(); |
| 181 | |
| 182 | // Soft claims only — ingredients stay in the bag until bottle. |
| 183 | let bag = harness.simulation.bagOf(entityId); |
| 184 | assert.equal(countItem(bag.inventory, ItemId.thyme), 2); |
| 185 | assert.equal(countItem(bag.inventory, ItemId.mint), 2); |
| 186 | assert.equal(countItem(bag.inventory, ItemId.glassBottle), 2); |
| 187 | |
| 188 | harness.simulation.enqueue(1, { type: "cauldron-bottle", characterId: cid }); |
| 189 | harness.simulation.step(); |
| 190 | |
| 191 | bag = harness.simulation.bagOf(entityId); |
| 192 | assert.equal(countItem(bag.inventory, ItemId.thyme), 0); |
| 193 | assert.equal(countItem(bag.inventory, ItemId.mint), 0); |
| 194 | assert.equal(countItem(bag.inventory, ItemId.glassBottle), 0); |
| 195 | assert.equal(countItem(bag.inventory, ItemId.potion), 2); |
| 196 | const potion = bag.inventory.find((slot) => slot?.itemId === ItemId.potion); |
| 197 | assert.equal(potion.instance?.potionKey, "lesserHealthTonic"); |
| 198 | assert.equal(potion.instance?.unstable, false); |
| 199 | const alchemyAfter = harness.stateDb.getSkills(cid).xp.alchemy ?? 0; |
| 200 | // thyme×2 (tier 2) + mint×2 (tier 1), alchemy 2× |
| 201 | assert.equal(alchemyAfter - alchemyBefore, 12); |
| 202 | |
| 203 | harness.simulation.enqueue(1, { type: "cauldron-save-recipe", characterId: cid }); |
| 204 | harness.simulation.step(); |
| 205 | const after = harness.simulation.bagOf(entityId); |
| 206 | assert.equal(countItem(after.inventory, ItemId.paper), 0); |
| 207 | assert.equal(countItem(after.inventory, ItemId.recipePaper), 1); |
| 208 | const recipe = after.inventory.find((slot) => slot?.itemId === ItemId.recipePaper); |
| 209 | assert.match(recipe.instance?.body ?? "", /Lesser health tonic/); |
| 210 | assert.match(recipe.instance?.body ?? "", /Alchemy level required/); |
| 211 | assert.equal(recipe.instance?.displayName, "Lesser health tonic recipe"); |
| 212 | assert.equal(recipe.instance?.authorDisplayName, `${character.first_name} ${character.last_name}`); |
| 213 | } |
| 214 | finally |
| 215 | { harness.close(); } |
| 216 | }); |
| 217 | |
| 218 | test("cauldron clear / close after insert does not consume ingredients", () => |
| 219 | { |
| 220 | const harness = makeSimulation({ workerCount: 1 }); |
| 221 | try |
| 222 | { |
| 223 | placeCauldron(harness.worldDb, 8, 8); |
| 224 | harness.simulation.reloadWorld(); |
| 225 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 }); |
| 226 | const inventory = emptySlots(); |
| 227 | inventory[0] = { itemId: ItemId.thyme, quantity: 2 }; |
| 228 | inventory[1] = { itemId: ItemId.glassBottle, quantity: 1 }; |
| 229 | harness.stateDb.saveBag(character.character_id, { |
| 230 | inventory, |
| 231 | equipment: emptyEquipment(), |
| 232 | vault: [], |
| 233 | vaultReserved: [] |
| 234 | }); |
| 235 | const entityId = harness.simulation.spawnCharacter(1, character); |
| 236 | const cid = character.character_id; |
| 237 | |
| 238 | harness.simulation.enqueue(1, { type: "open-cauldron", characterId: cid }); |
| 239 | harness.simulation.step(); |
| 240 | harness.simulation.enqueue(1, { |
| 241 | type: "cauldron-insert", |
| 242 | characterId: cid, |
| 243 | itemId: ItemId.thyme, |
| 244 | quantity: 2 |
| 245 | }); |
| 246 | harness.simulation.step(); |
| 247 | assert.equal(countItem(harness.simulation.bagOf(entityId).inventory, ItemId.thyme), 2); |
| 248 | |
| 249 | harness.simulation.enqueue(1, { type: "cauldron-clear", characterId: cid }); |
| 250 | harness.simulation.step(); |
| 251 | assert.equal(countItem(harness.simulation.bagOf(entityId).inventory, ItemId.thyme), 2); |
| 252 | |
| 253 | harness.simulation.enqueue(1, { |
| 254 | type: "cauldron-insert", |
| 255 | characterId: cid, |
| 256 | itemId: ItemId.thyme, |
| 257 | quantity: 2 |
| 258 | }); |
| 259 | harness.simulation.step(); |
| 260 | harness.simulation.enqueue(1, { type: "cauldron-close", characterId: cid }); |
| 261 | harness.simulation.step(); |
| 262 | assert.equal(countItem(harness.simulation.bagOf(entityId).inventory, ItemId.thyme), 2); |
| 263 | } |
| 264 | finally |
| 265 | { harness.close(); } |
| 266 | }); |
| 267 | |
| 268 | test("cauldron rejects a fifth distinct ingredient type", () => |
| 269 | { |
| 270 | const harness = makeSimulation({ workerCount: 1 }); |
| 271 | try |
| 272 | { |
| 273 | placeCauldron(harness.worldDb, 8, 8); |
| 274 | harness.simulation.reloadWorld(); |
| 275 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 }); |
| 276 | const inventory = emptySlots(); |
| 277 | inventory[0] = { itemId: ItemId.thyme, quantity: 1 }; |
| 278 | inventory[1] = { itemId: ItemId.mint, quantity: 1 }; |
| 279 | inventory[2] = { itemId: ItemId.lavender, quantity: 1 }; |
| 280 | inventory[3] = { itemId: ItemId.nettle, quantity: 1 }; |
| 281 | inventory[4] = { itemId: ItemId.rosemary, quantity: 1 }; |
| 282 | inventory[5] = { itemId: ItemId.glassBottle, quantity: 1 }; |
| 283 | harness.stateDb.saveBag(character.character_id, { |
| 284 | inventory, |
| 285 | equipment: emptyEquipment(), |
| 286 | vault: [], |
| 287 | vaultReserved: [] |
| 288 | }); |
| 289 | harness.simulation.spawnCharacter(1, character); |
| 290 | const cid = character.character_id; |
| 291 | |
| 292 | harness.simulation.enqueue(1, { type: "open-cauldron", characterId: cid }); |
| 293 | harness.simulation.step(); |
| 294 | for (const itemId of [ItemId.thyme, ItemId.mint, ItemId.lavender, ItemId.nettle]) |
| 295 | { |
| 296 | harness.simulation.enqueue(1, { |
| 297 | type: "cauldron-insert", |
| 298 | characterId: cid, |
| 299 | itemId, |
| 300 | quantity: 1 |
| 301 | }); |
| 302 | } |
| 303 | const four = harness.simulation.step(); |
| 304 | assert.equal(four.events.some((event) => event.type === "command-error"), false, JSON.stringify(four.events)); |
| 305 | |
| 306 | harness.simulation.enqueue(1, { |
| 307 | type: "cauldron-insert", |
| 308 | characterId: cid, |
| 309 | itemId: ItemId.rosemary, |
| 310 | quantity: 1 |
| 311 | }); |
| 312 | const fifth = harness.simulation.step(); |
| 313 | const error = fifth.events.find((event) => event.type === "command-error"); |
| 314 | assert.ok(error, JSON.stringify(fifth.events)); |
| 315 | assert.match(error.message, /at most 4/i); |
| 316 | } |
| 317 | finally |
| 318 | { harness.close(); } |
| 319 | }); |
| 320 | |
| 321 | test("drinking a lesser health tonic applies health regen", () => |
| 322 | { |
| 323 | const harness = makeSimulation({ workerCount: 1 }); |
| 324 | try |
| 325 | { |
| 326 | const character = makeCharacter(harness.stateDb); |
| 327 | const inventory = emptySlots(); |
| 328 | const instance = potionItemInstance({ |
| 329 | potionKey: "lesserHealthTonic", |
| 330 | displayName: "Lesser health tonic", |
| 331 | unstable: false, |
| 332 | color: { r: 0.35, g: 0.78, b: 0.42 }, |
| 333 | fillLevel: 4, |
| 334 | effect: { type: "health-regen", amountPerSecond: 1, durationMs: 10_000 }, |
| 335 | effectKey: "health-regen" |
| 336 | }); |
| 337 | const inserted = insertStack( |
| 338 | inventory, |
| 339 | { itemId: ItemId.potion, quantity: 1, instance }, |
| 340 | itemDefinition(ItemId.potion) |
| 341 | ); |
| 342 | harness.stateDb.saveBag(character.character_id, { |
| 343 | inventory: inserted.slots, |
| 344 | equipment: emptyEquipment(), |
| 345 | vault: [], |
| 346 | vaultReserved: [] |
| 347 | }); |
| 348 | const entityId = harness.simulation.spawnCharacter(1, character); |
| 349 | const record = harness.simulation.directory.get(entityId); |
| 350 | const worker = harness.simulation.workers[record.workerId]; |
| 351 | const handle = worker.world.ecs.handleOf(entityId); |
| 352 | const health = worker.world.ecs.get(handle, Component.health); |
| 353 | health.current = 4; |
| 354 | |
| 355 | const slotIndex = inserted.slots.findIndex((slot) => slot?.itemId === ItemId.potion); |
| 356 | harness.simulation.enqueue(1, { type: "drink-item", characterId: character.character_id, slotIndex }); |
| 357 | const drink = harness.simulation.step(); |
| 358 | assert.equal(drink.events.some((event) => event.type === "command-error"), false, JSON.stringify(drink.events)); |
| 359 | assert.ok(drink.events.some((event) => event.type === "status-effects")); |
| 360 | const status = worker.world.ecs.get(handle, Component.statusEffects); |
| 361 | assert.ok(status?.effects?.some((entry) => entry.type === "health-regen")); |
| 362 | |
| 363 | stepTimes(harness.simulation, 25); |
| 364 | assert.ok(Number(worker.world.ecs.get(handle, Component.health)?.current) > 4); |
| 365 | } |
| 366 | finally |
| 367 | { harness.close(); } |
| 368 | }); |
| 369 | |
| 370 | test("creating dye awards 2 alchemy XP per flower and caps a slot at 16", () => |
| 371 | { |
| 372 | const harness = makeSimulation({ workerCount: 1 }); |
| 373 | try |
| 374 | { |
| 375 | placeCauldron(harness.worldDb, 8, 8); |
| 376 | harness.simulation.reloadWorld(); |
| 377 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 }); |
| 378 | const inventory = emptySlots(); |
| 379 | const instance = flowerItemInstance("red"); |
| 380 | const filled = insertStack( |
| 381 | inventory, |
| 382 | { itemId: ItemId.flower, quantity: 16, instance }, |
| 383 | itemDefinition(ItemId.flower) |
| 384 | ); |
| 385 | filled.slots[1] = { itemId: ItemId.glassBottle, quantity: 16 }; |
| 386 | harness.stateDb.saveBag(character.character_id, { |
| 387 | inventory: filled.slots, |
| 388 | equipment: emptyEquipment(), |
| 389 | vault: [], |
| 390 | vaultReserved: [] |
| 391 | }); |
| 392 | const entityId = harness.simulation.spawnCharacter(1, character); |
| 393 | const cid = character.character_id; |
| 394 | const alchemyBefore = harness.stateDb.getSkills(cid).xp.alchemy ?? 0; |
| 395 | |
| 396 | harness.simulation.enqueue(1, { type: "open-cauldron", characterId: cid }); |
| 397 | harness.simulation.step(); |
| 398 | harness.simulation.enqueue(1, { |
| 399 | type: "cauldron-create-dye", |
| 400 | characterId: cid, |
| 401 | flowers: [{ itemId: ItemId.flower, quantity: 99, instance }] |
| 402 | }); |
| 403 | const made = harness.simulation.step(); |
| 404 | assert.equal(made.events.some((event) => event.type === "command-error"), false, JSON.stringify(made.events)); |
| 405 | const alchemyAfter = harness.stateDb.getSkills(cid).xp.alchemy ?? 0; |
| 406 | assert.equal(alchemyAfter - alchemyBefore, 32); |
| 407 | const bag = harness.simulation.bagOf(entityId); |
| 408 | assert.equal(countItem(bag.inventory, ItemId.flower), 0); |
| 409 | assert.equal(countItem(bag.inventory, ItemId.dye), 16); |
| 410 | } |
| 411 | finally |
| 412 | { harness.close(); } |
| 413 | }); |
| 414 | |
| 415 | test("create dye with fewer bottles than flowers returns dyeConfirm on inventory", () => |
| 416 | { |
| 417 | const harness = makeSimulation({ workerCount: 1 }); |
| 418 | try |
| 419 | { |
| 420 | placeCauldron(harness.worldDb, 8, 8); |
| 421 | harness.simulation.reloadWorld(); |
| 422 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 }); |
| 423 | const yellow = flowerItemInstance("yellow"); |
| 424 | const magenta = flowerItemInstance("magenta"); |
| 425 | let slots = emptySlots(); |
| 426 | slots = insertStack( |
| 427 | slots, |
| 428 | { itemId: ItemId.flower, quantity: 1, instance: yellow }, |
| 429 | itemDefinition(ItemId.flower) |
| 430 | ).slots; |
| 431 | slots = insertStack( |
| 432 | slots, |
| 433 | { itemId: ItemId.flower, quantity: 1, instance: magenta }, |
| 434 | itemDefinition(ItemId.flower) |
| 435 | ).slots; |
| 436 | slots = insertStack( |
| 437 | slots, |
| 438 | { itemId: ItemId.glassBottle, quantity: 1 }, |
| 439 | itemDefinition(ItemId.glassBottle) |
| 440 | ).slots; |
| 441 | harness.stateDb.saveBag(character.character_id, { |
| 442 | inventory: slots, |
| 443 | equipment: emptyEquipment(), |
| 444 | vault: [], |
| 445 | vaultReserved: [] |
| 446 | }); |
| 447 | const entityId = harness.simulation.spawnCharacter(1, character); |
| 448 | const cid = character.character_id; |
| 449 | |
| 450 | harness.simulation.enqueue(1, { type: "open-cauldron", characterId: cid }); |
| 451 | harness.simulation.step(); |
| 452 | harness.simulation.enqueue(1, { |
| 453 | type: "cauldron-create-dye", |
| 454 | characterId: cid, |
| 455 | confirmOverflow: false, |
| 456 | flowers: [ |
| 457 | { itemId: ItemId.flower, quantity: 1, instance: yellow }, |
| 458 | { itemId: ItemId.flower, quantity: 1, instance: magenta } |
| 459 | ] |
| 460 | }); |
| 461 | const made = harness.simulation.step(); |
| 462 | const inv = made.events.find((event) => event.type === "inventory" && event.dyeConfirm); |
| 463 | assert.ok(inv, JSON.stringify(made.events)); |
| 464 | assert.deepEqual(inv.dyeConfirm, { wanted: 2, bottles: 1, lost: 1 }); |
| 465 | const bag = harness.simulation.bagOf(entityId); |
| 466 | assert.equal(countItem(bag.inventory, ItemId.flower), 2); |
| 467 | assert.equal(countItem(bag.inventory, ItemId.dye), 0); |
| 468 | assert.equal(countItem(bag.inventory, ItemId.glassBottle), 1); |
| 469 | } |
| 470 | finally |
| 471 | { harness.close(); } |
| 472 | }); |