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