| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { BlockType } from "../../shared/definitions/tiles.js"; |
| 4 | import { ItemId } from "../../shared/definitions/items.js"; |
| 5 | import { AlchemyStepKind } from "../../shared/definitions/alchemy.js"; |
| 6 | import { |
| 7 | evaluateAlchemyBottle, |
| 8 | hashAlchemySteps, |
| 9 | resolvedAlchemyPotions, |
| 10 | unstablePotionFromSteps, |
| 11 | potionItemInstance |
| 12 | } from "../../shared/alchemy/recipes.js"; |
| 13 | import { emptyEquipment, emptySlots, insertStack } from "../../shared/items/inventory.js"; |
| 14 | import { itemDefinition } from "../../shared/definitions/items.js"; |
| 15 | import { createEmptyChunkData, encodeChunkBlob } from "../src/world/chunkData.js"; |
| 16 | import { Component } from "../src/ecs/components.js"; |
| 17 | import { |
| 18 | makeCharacter, |
| 19 | makeSimulation, |
| 20 | stepTimes |
| 21 | } from "./helpers/simulationHarness.js"; |
| 22 | |
| 23 | function placeCauldron (worldDb, tileX, tileY) |
| 24 | { |
| 25 | const chunkX = Math.floor(tileX / 16); |
| 26 | const chunkY = Math.floor(tileY / 16); |
| 27 | const data = createEmptyChunkData(); |
| 28 | data.open = true; |
| 29 | const localX = tileX - chunkX * 16; |
| 30 | const localY = tileY - chunkY * 16; |
| 31 | data.blocks[localY * 16 + localX] = { typeId: BlockType.cauldron, state: 0 }; |
| 32 | worldDb.saveChunkVersion( |
| 33 | chunkX, |
| 34 | chunkY, |
| 35 | worldDb.getActiveRevisionId(), |
| 36 | true, |
| 37 | encodeChunkBlob(data) |
| 38 | ); |
| 39 | } |
| 40 | |
| 41 | function countItem (slots, itemId) |
| 42 | { |
| 43 | return (slots ?? []) |
| 44 | .filter((slot) => slot?.itemId === itemId) |
| 45 | .reduce((sum, slot) => sum + (slot.quantity | 0), 0); |
| 46 | } |
| 47 | |
| 48 | test("lesser health tonic ratios and steps evaluate to a success batch", () => |
| 49 | { |
| 50 | const potion = resolvedAlchemyPotions().find((entry) => entry.potionKey === "lesserHealthTonic"); |
| 51 | assert.ok(potion); |
| 52 | const evaluation = evaluateAlchemyBottle({ |
| 53 | skills: { levels: { alchemy: 1 } }, |
| 54 | brewStacks: [ |
| 55 | { itemId: ItemId.thyme, quantity: 2 }, |
| 56 | { itemId: ItemId.mint, quantity: 2 } |
| 57 | ], |
| 58 | takenSteps: [ |
| 59 | { kind: AlchemyStepKind.insert, itemId: ItemId.thyme }, |
| 60 | { kind: AlchemyStepKind.heat }, |
| 61 | { kind: AlchemyStepKind.insert, itemId: ItemId.mint }, |
| 62 | { kind: AlchemyStepKind.stir } |
| 63 | ] |
| 64 | }); |
| 65 | assert.equal(evaluation.success, true); |
| 66 | assert.equal(evaluation.batch, 2); |
| 67 | assert.equal(evaluation.result.potionKey, "lesserHealthTonic"); |
| 68 | }); |
| 69 | |
| 70 | test("same wrong steps hash to the same unstable potion", () => |
| 71 | { |
| 72 | const steps = [ |
| 73 | { kind: AlchemyStepKind.heat }, |
| 74 | { kind: AlchemyStepKind.stir }, |
| 75 | { kind: AlchemyStepKind.cool } |
| 76 | ]; |
| 77 | const a = unstablePotionFromSteps(steps); |
| 78 | const b = unstablePotionFromSteps(steps); |
| 79 | assert.equal(a.potionKey, b.potionKey); |
| 80 | assert.equal(a.stepsHash, hashAlchemySteps(steps)); |
| 81 | assert.ok(["stamina-drain", "glow", "shrink", "blind"].includes(a.effectKey)); |
| 82 | }); |
| 83 | |
| 84 | test("cauldron bottles lesser health tonic when steps and ratios match", () => |
| 85 | { |
| 86 | const harness = makeSimulation({ workerCount: 1 }); |
| 87 | try |
| 88 | { |
| 89 | placeCauldron(harness.worldDb, 8, 8); |
| 90 | harness.simulation.reloadWorld(); |
| 91 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 }); |
| 92 | const inventory = emptySlots(); |
| 93 | inventory[0] = { itemId: ItemId.thyme, quantity: 2 }; |
| 94 | inventory[1] = { itemId: ItemId.mint, quantity: 2 }; |
| 95 | inventory[2] = { itemId: ItemId.glassBottle, quantity: 2 }; |
| 96 | inventory[3] = { itemId: ItemId.paper, quantity: 1 }; |
| 97 | harness.stateDb.saveBag(character.character_id, { |
| 98 | inventory, |
| 99 | equipment: emptyEquipment(), |
| 100 | vault: [], |
| 101 | vaultReserved: [] |
| 102 | }); |
| 103 | const entityId = harness.simulation.spawnCharacter(1, character); |
| 104 | const cid = character.character_id; |
| 105 | |
| 106 | harness.simulation.enqueue(1, { type: "open-cauldron", characterId: cid }); |
| 107 | harness.simulation.step(); |
| 108 | |
| 109 | harness.simulation.enqueue(1, { |
| 110 | type: "cauldron-insert", |
| 111 | characterId: cid, |
| 112 | itemId: ItemId.thyme, |
| 113 | quantity: 2 |
| 114 | }); |
| 115 | harness.simulation.enqueue(1, { |
| 116 | type: "cauldron-action", |
| 117 | characterId: cid, |
| 118 | action: AlchemyStepKind.heat |
| 119 | }); |
| 120 | harness.simulation.enqueue(1, { |
| 121 | type: "cauldron-insert", |
| 122 | characterId: cid, |
| 123 | itemId: ItemId.mint, |
| 124 | quantity: 2 |
| 125 | }); |
| 126 | harness.simulation.enqueue(1, { |
| 127 | type: "cauldron-action", |
| 128 | characterId: cid, |
| 129 | action: AlchemyStepKind.stir |
| 130 | }); |
| 131 | harness.simulation.step(); |
| 132 | |
| 133 | // Soft claims only — ingredients stay in the bag until bottle. |
| 134 | let bag = harness.simulation.bagOf(entityId); |
| 135 | assert.equal(countItem(bag.inventory, ItemId.thyme), 2); |
| 136 | assert.equal(countItem(bag.inventory, ItemId.mint), 2); |
| 137 | assert.equal(countItem(bag.inventory, ItemId.glassBottle), 2); |
| 138 | |
| 139 | harness.simulation.enqueue(1, { type: "cauldron-bottle", characterId: cid }); |
| 140 | harness.simulation.step(); |
| 141 | |
| 142 | bag = harness.simulation.bagOf(entityId); |
| 143 | assert.equal(countItem(bag.inventory, ItemId.thyme), 0); |
| 144 | assert.equal(countItem(bag.inventory, ItemId.mint), 0); |
| 145 | assert.equal(countItem(bag.inventory, ItemId.glassBottle), 0); |
| 146 | assert.equal(countItem(bag.inventory, ItemId.potion), 2); |
| 147 | const potion = bag.inventory.find((slot) => slot?.itemId === ItemId.potion); |
| 148 | assert.equal(potion.instance?.potionKey, "lesserHealthTonic"); |
| 149 | assert.equal(potion.instance?.unstable, false); |
| 150 | |
| 151 | harness.simulation.enqueue(1, { type: "cauldron-save-recipe", characterId: cid }); |
| 152 | harness.simulation.step(); |
| 153 | const after = harness.simulation.bagOf(entityId); |
| 154 | assert.equal(countItem(after.inventory, ItemId.paper), 0); |
| 155 | assert.equal(countItem(after.inventory, ItemId.recipePaper), 1); |
| 156 | const recipe = after.inventory.find((slot) => slot?.itemId === ItemId.recipePaper); |
| 157 | assert.match(recipe.instance?.body ?? "", /Lesser health tonic/); |
| 158 | assert.match(recipe.instance?.body ?? "", /Alchemy level required/); |
| 159 | assert.equal(recipe.instance?.displayName, "Lesser health tonic Recipe"); |
| 160 | assert.equal(recipe.instance?.authorDisplayName, `${character.first_name} ${character.last_name}`); |
| 161 | } |
| 162 | finally |
| 163 | { harness.close(); } |
| 164 | }); |
| 165 | |
| 166 | test("cauldron clear / close after insert does not consume ingredients", () => |
| 167 | { |
| 168 | const harness = makeSimulation({ workerCount: 1 }); |
| 169 | try |
| 170 | { |
| 171 | placeCauldron(harness.worldDb, 8, 8); |
| 172 | harness.simulation.reloadWorld(); |
| 173 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 }); |
| 174 | const inventory = emptySlots(); |
| 175 | inventory[0] = { itemId: ItemId.thyme, quantity: 2 }; |
| 176 | inventory[1] = { itemId: ItemId.glassBottle, quantity: 1 }; |
| 177 | harness.stateDb.saveBag(character.character_id, { |
| 178 | inventory, |
| 179 | equipment: emptyEquipment(), |
| 180 | vault: [], |
| 181 | vaultReserved: [] |
| 182 | }); |
| 183 | const entityId = harness.simulation.spawnCharacter(1, character); |
| 184 | const cid = character.character_id; |
| 185 | |
| 186 | harness.simulation.enqueue(1, { type: "open-cauldron", characterId: cid }); |
| 187 | harness.simulation.step(); |
| 188 | harness.simulation.enqueue(1, { |
| 189 | type: "cauldron-insert", |
| 190 | characterId: cid, |
| 191 | itemId: ItemId.thyme, |
| 192 | quantity: 2 |
| 193 | }); |
| 194 | harness.simulation.step(); |
| 195 | assert.equal(countItem(harness.simulation.bagOf(entityId).inventory, ItemId.thyme), 2); |
| 196 | |
| 197 | harness.simulation.enqueue(1, { type: "cauldron-clear", characterId: cid }); |
| 198 | harness.simulation.step(); |
| 199 | assert.equal(countItem(harness.simulation.bagOf(entityId).inventory, ItemId.thyme), 2); |
| 200 | |
| 201 | harness.simulation.enqueue(1, { |
| 202 | type: "cauldron-insert", |
| 203 | characterId: cid, |
| 204 | itemId: ItemId.thyme, |
| 205 | quantity: 2 |
| 206 | }); |
| 207 | harness.simulation.step(); |
| 208 | harness.simulation.enqueue(1, { type: "cauldron-close", characterId: cid }); |
| 209 | harness.simulation.step(); |
| 210 | assert.equal(countItem(harness.simulation.bagOf(entityId).inventory, ItemId.thyme), 2); |
| 211 | } |
| 212 | finally |
| 213 | { harness.close(); } |
| 214 | }); |
| 215 | |
| 216 | test("cauldron rejects a fifth distinct ingredient type", () => |
| 217 | { |
| 218 | const harness = makeSimulation({ workerCount: 1 }); |
| 219 | try |
| 220 | { |
| 221 | placeCauldron(harness.worldDb, 8, 8); |
| 222 | harness.simulation.reloadWorld(); |
| 223 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 }); |
| 224 | const inventory = emptySlots(); |
| 225 | inventory[0] = { itemId: ItemId.thyme, quantity: 1 }; |
| 226 | inventory[1] = { itemId: ItemId.mint, quantity: 1 }; |
| 227 | inventory[2] = { itemId: ItemId.lavender, quantity: 1 }; |
| 228 | inventory[3] = { itemId: ItemId.nettle, quantity: 1 }; |
| 229 | inventory[4] = { itemId: ItemId.rosemary, quantity: 1 }; |
| 230 | inventory[5] = { itemId: ItemId.glassBottle, quantity: 1 }; |
| 231 | harness.stateDb.saveBag(character.character_id, { |
| 232 | inventory, |
| 233 | equipment: emptyEquipment(), |
| 234 | vault: [], |
| 235 | vaultReserved: [] |
| 236 | }); |
| 237 | harness.simulation.spawnCharacter(1, character); |
| 238 | const cid = character.character_id; |
| 239 | |
| 240 | harness.simulation.enqueue(1, { type: "open-cauldron", characterId: cid }); |
| 241 | harness.simulation.step(); |
| 242 | for (const itemId of [ItemId.thyme, ItemId.mint, ItemId.lavender, ItemId.nettle]) |
| 243 | { |
| 244 | harness.simulation.enqueue(1, { |
| 245 | type: "cauldron-insert", |
| 246 | characterId: cid, |
| 247 | itemId, |
| 248 | quantity: 1 |
| 249 | }); |
| 250 | } |
| 251 | const four = harness.simulation.step(); |
| 252 | assert.equal(four.events.some((event) => event.type === "command-error"), false, JSON.stringify(four.events)); |
| 253 | |
| 254 | harness.simulation.enqueue(1, { |
| 255 | type: "cauldron-insert", |
| 256 | characterId: cid, |
| 257 | itemId: ItemId.rosemary, |
| 258 | quantity: 1 |
| 259 | }); |
| 260 | const fifth = harness.simulation.step(); |
| 261 | const error = fifth.events.find((event) => event.type === "command-error"); |
| 262 | assert.ok(error, JSON.stringify(fifth.events)); |
| 263 | assert.match(error.message, /at most 4/i); |
| 264 | } |
| 265 | finally |
| 266 | { harness.close(); } |
| 267 | }); |
| 268 | |
| 269 | test("drinking a lesser health tonic applies health regen", () => |
| 270 | { |
| 271 | const harness = makeSimulation({ workerCount: 1 }); |
| 272 | try |
| 273 | { |
| 274 | const character = makeCharacter(harness.stateDb); |
| 275 | const inventory = emptySlots(); |
| 276 | const instance = potionItemInstance({ |
| 277 | potionKey: "lesserHealthTonic", |
| 278 | displayName: "Lesser health tonic", |
| 279 | unstable: false, |
| 280 | color: { r: 0.35, g: 0.78, b: 0.42 }, |
| 281 | fillLevel: 4, |
| 282 | effect: { type: "health-regen", amountPerSecond: 1, durationMs: 10_000 }, |
| 283 | effectKey: "health-regen" |
| 284 | }); |
| 285 | const inserted = insertStack( |
| 286 | inventory, |
| 287 | { itemId: ItemId.potion, quantity: 1, instance }, |
| 288 | itemDefinition(ItemId.potion) |
| 289 | ); |
| 290 | harness.stateDb.saveBag(character.character_id, { |
| 291 | inventory: inserted.slots, |
| 292 | equipment: emptyEquipment(), |
| 293 | vault: [], |
| 294 | vaultReserved: [] |
| 295 | }); |
| 296 | const entityId = harness.simulation.spawnCharacter(1, character); |
| 297 | const record = harness.simulation.directory.get(entityId); |
| 298 | const worker = harness.simulation.workers[record.workerId]; |
| 299 | const handle = worker.world.ecs.handleOf(entityId); |
| 300 | const health = worker.world.ecs.get(handle, Component.health); |
| 301 | health.current = 5; |
| 302 | |
| 303 | const slotIndex = inserted.slots.findIndex((slot) => slot?.itemId === ItemId.potion); |
| 304 | harness.simulation.enqueue(1, { type: "drink-item", characterId: character.character_id, slotIndex }); |
| 305 | const drink = harness.simulation.step(); |
| 306 | assert.equal(drink.events.some((event) => event.type === "command-error"), false, JSON.stringify(drink.events)); |
| 307 | assert.ok(drink.events.some((event) => event.type === "status-effects")); |
| 308 | const status = worker.world.ecs.get(handle, Component.statusEffects); |
| 309 | assert.ok(status?.effects?.some((entry) => entry.type === "health-regen")); |
| 310 | |
| 311 | stepTimes(harness.simulation, 25); |
| 312 | assert.ok(Number(worker.world.ecs.get(handle, Component.health)?.current) > 5); |
| 313 | } |
| 314 | finally |
| 315 | { harness.close(); } |
| 316 | }); |