| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { ItemId } from "../../shared/definitions/items.js"; |
| 4 | import { BlockType } from "../../shared/definitions/tiles.js"; |
| 5 | import { canSellOnMarketplace, assertMarketplaceSellable } from "../../shared/items/marketplace.js"; |
| 6 | import { midpointTradePrice, MARKET_ORDER_MAX_AGE_MS, soldLetterBody } from "../../shared/market/exchange.js"; |
| 7 | import { emptyEquipment, emptySlots, addCoinsToSlots, countCoinsInSlots } from "../../shared/items/inventory.js"; |
| 8 | import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js"; |
| 9 | import { createEmptyChunkData, encodeChunkBlob } from "../src/world/chunkData.js"; |
| 10 | import { CHUNK_SIZE } from "../../shared/protocol/constants.js"; |
| 11 | |
| 12 | test("blank cheques cannot be listed on the marketplace", () => |
| 13 | { |
| 14 | const stack = { itemId: ItemId.blankCheque, quantity: 1 }; |
| 15 | assert.equal(canSellOnMarketplace(stack), false); |
| 16 | assert.throws( |
| 17 | () => assertMarketplaceSellable(stack), |
| 18 | (err) => err.message === "Blank cheques cannot be sold on the marketplace." |
| 19 | ); |
| 20 | }); |
| 21 | |
| 22 | test("written cheques can be listed on the marketplace", () => |
| 23 | { |
| 24 | const stack = { |
| 25 | itemId: ItemId.filledCheque, |
| 26 | quantity: 1, |
| 27 | instance: { kind: "filled-cheque", chequeId: "x", issuerCharacterId: 1, itemId: ItemId.apple, quantity: 2 } |
| 28 | }; |
| 29 | assert.equal(canSellOnMarketplace(stack), true); |
| 30 | assert.doesNotThrow(() => assertMarketplaceSellable(stack)); |
| 31 | }); |
| 32 | |
| 33 | test("normal items are marketplace sellable by default", () => |
| 34 | { |
| 35 | assert.equal(canSellOnMarketplace({ itemId: ItemId.apple, quantity: 3 }), true); |
| 36 | }); |
| 37 | |
| 38 | test("midpoint trade price rounds half units deterministically", () => |
| 39 | { |
| 40 | assert.equal(midpointTradePrice(100, 80), 90); |
| 41 | assert.equal(midpointTradePrice(101, 80), 91); |
| 42 | assert.equal(midpointTradePrice(80, 100), 90); |
| 43 | }); |
| 44 | |
| 45 | function placePlaza (worldDb) |
| 46 | { |
| 47 | const data = createEmptyChunkData(); |
| 48 | data.open = true; |
| 49 | data.blocks[8 * CHUNK_SIZE + 9] = { typeId: BlockType.marketplace, state: null }; |
| 50 | data.blocks[8 * CHUNK_SIZE + 10] = { typeId: BlockType.bank, state: null }; |
| 51 | worldDb.saveChunkVersion(0, 0, worldDb.getActiveRevisionId(), true, encodeChunkBlob(data)); |
| 52 | } |
| 53 | |
| 54 | test("sell then buy at Central Exchange matches at midpoint and pays seller by pigeon letter", () => |
| 55 | { |
| 56 | const harness = makeSimulation({ workerCount: 1 }); |
| 57 | try |
| 58 | { |
| 59 | placePlaza(harness.worldDb); |
| 60 | harness.simulation.reloadWorld(); |
| 61 | |
| 62 | const seller = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8, firstName: "Sel", lastName: "Ler" }); |
| 63 | const buyer = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9, firstName: "Buy", lastName: "Er" }); |
| 64 | const sellerBag = addCoinsToSlots(emptySlots(), 0); |
| 65 | sellerBag.slots[0] = { itemId: ItemId.apple, quantity: 2 }; |
| 66 | harness.stateDb.saveBag(seller.character_id, { |
| 67 | inventory: sellerBag.slots, |
| 68 | equipment: emptyEquipment(), |
| 69 | vault: [], |
| 70 | vaultReserved: [] |
| 71 | }); |
| 72 | const buyerBag = addCoinsToSlots(emptySlots(), 100); |
| 73 | assert.equal(buyerBag.ok, true); |
| 74 | harness.stateDb.saveBag(buyer.character_id, { |
| 75 | inventory: buyerBag.slots, |
| 76 | equipment: emptyEquipment(), |
| 77 | vault: [], |
| 78 | vaultReserved: [] |
| 79 | }); |
| 80 | |
| 81 | const sellerId = harness.simulation.spawnCharacter(1, seller); |
| 82 | const buyerId = harness.simulation.spawnCharacter(2, buyer); |
| 83 | harness.simulation.step(); |
| 84 | |
| 85 | harness.simulation.enqueue(1, { |
| 86 | type: "place-market-sell", |
| 87 | characterId: seller.character_id, |
| 88 | slotIndex: 0, |
| 89 | quantity: 2, |
| 90 | minUnitPrice: 8 |
| 91 | }); |
| 92 | const sellResult = harness.simulation.step(); |
| 93 | assert.equal(sellResult.events.some((event) => event.type === "command-error"), false, JSON.stringify(sellResult.events)); |
| 94 | assert.ok(sellResult.events.some((event) => event.type === "market-open")); |
| 95 | |
| 96 | harness.simulation.enqueue(2, { |
| 97 | type: "place-market-buy", |
| 98 | characterId: buyer.character_id, |
| 99 | itemId: ItemId.apple, |
| 100 | quantity: 2, |
| 101 | maxUnitPrice: 12 |
| 102 | }); |
| 103 | const buyResult = harness.simulation.step(); |
| 104 | assert.equal(buyResult.events.some((event) => event.type === "command-error"), false, JSON.stringify(buyResult.events)); |
| 105 | const trade = buyResult.events.find((event) => event.type === "market-trade"); |
| 106 | assert.ok(trade, JSON.stringify(buyResult.events)); |
| 107 | assert.equal(trade.unitPrice, 10); |
| 108 | assert.equal(trade.quantity, 2); |
| 109 | |
| 110 | const buyerBagNow = harness.simulation.bagOf(buyerId); |
| 111 | assert.equal((buyerBagNow.inventory ?? []).some((slot) => slot?.itemId === ItemId.apple), true); |
| 112 | |
| 113 | // Advance courier flight until pigeons deliver to bank. |
| 114 | for (let i = 0; i < 500; i++) |
| 115 | { |
| 116 | harness.advance(1000); |
| 117 | harness.simulation.step(); |
| 118 | const sellerVault = harness.stateDb.getBag(seller.character_id); |
| 119 | const coins = countCoinsInSlots(sellerVault.vault ?? []); |
| 120 | const hasLetter = (sellerVault.vault ?? []).some((slot) => slot?.itemId === ItemId.letter); |
| 121 | if (coins >= 20 && hasLetter) |
| 122 | { |
| 123 | const letter = (sellerVault.vault ?? []).find((slot) => slot?.itemId === ItemId.letter); |
| 124 | assert.match(letter.instance.body, /Central Exchange/); |
| 125 | assert.match(letter.instance.body, /sold|filled|proceeds/i); |
| 126 | assert.equal(harness.simulation.market.lastSaleFor(ItemId.apple)?.lastTradePrice, 10); |
| 127 | return; |
| 128 | } |
| 129 | } |
| 130 | assert.fail("Seller did not receive coins and letter in vault"); |
| 131 | } |
| 132 | finally |
| 133 | { harness.close(); } |
| 134 | }); |
| 135 | |
| 136 | test("orders older than one month auto-cancel and return escrow to bank", () => |
| 137 | { |
| 138 | const harness = makeSimulation({ workerCount: 1 }); |
| 139 | try |
| 140 | { |
| 141 | placePlaza(harness.worldDb); |
| 142 | harness.simulation.reloadWorld(); |
| 143 | |
| 144 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 145 | const bag = addCoinsToSlots(emptySlots(), 50); |
| 146 | assert.equal(bag.ok, true); |
| 147 | harness.stateDb.saveBag(character.character_id, { |
| 148 | inventory: bag.slots, |
| 149 | equipment: emptyEquipment(), |
| 150 | vault: [], |
| 151 | vaultReserved: [] |
| 152 | }); |
| 153 | harness.simulation.spawnCharacter(1, character); |
| 154 | harness.simulation.step(); |
| 155 | |
| 156 | harness.simulation.enqueue(1, { |
| 157 | type: "place-market-buy", |
| 158 | characterId: character.character_id, |
| 159 | itemId: ItemId.apple, |
| 160 | quantity: 1, |
| 161 | maxUnitPrice: 10 |
| 162 | }); |
| 163 | const placed = harness.simulation.step(); |
| 164 | assert.equal(placed.events.some((event) => event.type === "command-error"), false, JSON.stringify(placed.events)); |
| 165 | const open = harness.stateDb.listOpenMarketOrdersForCharacter(character.character_id); |
| 166 | assert.equal(open.length, 1); |
| 167 | |
| 168 | const order = open[0]; |
| 169 | harness.stateDb.db.prepare("UPDATE market_orders SET created_at = ? WHERE order_id = ?") |
| 170 | .run(Date.now() - MARKET_ORDER_MAX_AGE_MS - 1000, order.orderId); |
| 171 | |
| 172 | const expired = harness.simulation.market.step(Date.now()); |
| 173 | assert.ok(expired.some((event) => event.type === "market-order-expired")); |
| 174 | assert.equal(harness.stateDb.listOpenMarketOrdersForCharacter(character.character_id).length, 0); |
| 175 | |
| 176 | for (let i = 0; i < 500; i++) |
| 177 | { |
| 178 | harness.advance(1000); |
| 179 | harness.simulation.step(); |
| 180 | const vault = harness.stateDb.getBag(character.character_id); |
| 181 | const coins = countCoinsInSlots(vault.vault ?? []); |
| 182 | const hasLetter = (vault.vault ?? []).some((slot) => slot?.itemId === ItemId.letter); |
| 183 | if (coins >= 10 && hasLetter) |
| 184 | { |
| 185 | const letter = (vault.vault ?? []).find((slot) => slot?.itemId === ItemId.letter); |
| 186 | assert.match(letter.instance.body, /one month/i); |
| 187 | assert.match(letter.instance.body, /Central Exchange/); |
| 188 | return; |
| 189 | } |
| 190 | } |
| 191 | assert.fail("Expired order did not return escrow letter to bank"); |
| 192 | } |
| 193 | finally |
| 194 | { harness.close(); } |
| 195 | }); |
| 196 | |
| 197 | test("exchange letter copy mentions Central Exchange staff signature", () => |
| 198 | { |
| 199 | const body = soldLetterBody({ |
| 200 | itemName: "Apple", |
| 201 | quantity: 1, |
| 202 | unitPrice: 5, |
| 203 | clerkName: "Bobby Pennies" |
| 204 | }); |
| 205 | assert.match(body, /Bobby Pennies/); |
| 206 | assert.match(body, /Central Exchange/); |
| 207 | }); |