step 10.13: done
This commit is contained in:
@@ -49,7 +49,7 @@ describe('shop', () => {
|
||||
/**
|
||||
* Register, login, create a character, optionally tweak DB fields, return cookie + characterId.
|
||||
*/
|
||||
async function setupHero(overrides = {}) {
|
||||
async function setupHero(overrides = {}, characterClass = 'warrior') {
|
||||
userSeq += 1;
|
||||
const username = `shopu${userSeq}`;
|
||||
const cookie = await registerAndLogin(baseUrl, username, 'password123');
|
||||
@@ -58,7 +58,7 @@ describe('shop', () => {
|
||||
const charRes = await fetch(`${baseUrl}/api/character`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json', ...authHeaders(cookie) },
|
||||
body: JSON.stringify({ name: `ShopHero${userSeq}`, class: 'warrior' }),
|
||||
body: JSON.stringify({ name: `ShopHero${userSeq}`, class: characterClass }),
|
||||
});
|
||||
assert.equal(charRes.status, 201);
|
||||
const charJson = await charRes.json();
|
||||
@@ -75,12 +75,19 @@ describe('shop', () => {
|
||||
return { cookie, characterId };
|
||||
}
|
||||
|
||||
/** Grant an item to a character's inventory. */
|
||||
/** Grant an item to a character's inventory. Stacks with existing rows. */
|
||||
function grantItem(characterId, itemId, quantity = 1) {
|
||||
const db = require('../../src/db').getDb();
|
||||
const NOW = 1_700_000_000_000;
|
||||
db.prepare('INSERT INTO inventory (character_id, item_id, quantity, equipped_slot, acquired_at) VALUES (?, ?, ?, NULL, ?)')
|
||||
.run(characterId, itemId, quantity, NOW);
|
||||
const existing = db.prepare(
|
||||
'SELECT id, quantity FROM inventory WHERE character_id = ? AND item_id = ?'
|
||||
).get(characterId, itemId);
|
||||
if (existing) {
|
||||
db.prepare('UPDATE inventory SET quantity = quantity + ? WHERE id = ?').run(quantity, existing.id);
|
||||
} else {
|
||||
db.prepare('INSERT INTO inventory (character_id, item_id, quantity, equipped_slot, acquired_at) VALUES (?, ?, ?, NULL, ?)')
|
||||
.run(characterId, itemId, quantity, NOW);
|
||||
}
|
||||
}
|
||||
|
||||
// ─── GET /api/shop ────────────────────────────────────────────────────
|
||||
@@ -162,7 +169,8 @@ describe('shop', () => {
|
||||
it('buys multiple quantity and stacks into existing item', async () => {
|
||||
const { cookie } = await setupHero({ gold: 200 });
|
||||
|
||||
// Buy 2 Minor Healing Potions (item 14, stackable, buy_price 15)
|
||||
// Buy 2 Minor Healing Potions (item 14, stackable, buy_price 15).
|
||||
// Starter kit already has 2 potions → total 4 after this buy.
|
||||
const res1 = await fetch(`${baseUrl}/api/shop/buy`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json', ...authHeaders(cookie) },
|
||||
@@ -172,9 +180,9 @@ describe('shop', () => {
|
||||
assert.equal(body1.ok, true);
|
||||
assert.equal(body1.data.character.gold, 170); // 200 - 2*15
|
||||
const potion1 = body1.data.items.find((i) => i.itemId === 14);
|
||||
assert.equal(potion1.quantity, 2);
|
||||
assert.equal(potion1.quantity, 4); // 2 starter + 2 bought
|
||||
|
||||
// Buy 3 more of the same
|
||||
// Buy 3 more of the same → total 7.
|
||||
const res2 = await fetch(`${baseUrl}/api/shop/buy`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json', ...authHeaders(cookie) },
|
||||
@@ -184,7 +192,7 @@ describe('shop', () => {
|
||||
assert.equal(body2.ok, true);
|
||||
assert.equal(body2.data.character.gold, 125); // 170 - 3*15
|
||||
const potion2 = body2.data.items.find((i) => i.itemId === 14);
|
||||
assert.equal(potion2.quantity, 5); // 2 + 3 stacked
|
||||
assert.equal(potion2.quantity, 7); // 4 + 3 stacked
|
||||
});
|
||||
|
||||
it('rejects buy with insufficient gold', async () => {
|
||||
@@ -372,19 +380,19 @@ describe('shop', () => {
|
||||
// ─── Sell equipped item rejection ─────────────────────────────────────
|
||||
|
||||
it('rejects selling an equipped item', async () => {
|
||||
const { cookie, characterId } = await setupHero({ gold: 100 });
|
||||
|
||||
// Grant and equip Rusty Sword (item 1, weapon)
|
||||
grantItem(characterId, 1, 1);
|
||||
// Use a ranger (starter weapon is Hunters Shortbow, item 3).
|
||||
// Grant a Leather Jerkin (item 8, armor) and equip it, then try to sell.
|
||||
const { cookie, characterId } = await setupHero({ gold: 100 }, 'ranger');
|
||||
|
||||
grantItem(characterId, 8, 1); // Leather Jerkin (armor)
|
||||
const db = require('../../src/db').getDb();
|
||||
db.prepare('UPDATE inventory SET equipped_slot = ? WHERE character_id = ? AND item_id = ?').run('weapon', characterId, 1);
|
||||
db.prepare('UPDATE inventory SET equipped_slot = ? WHERE character_id = ? AND item_id = ?').run('armor', characterId, 8);
|
||||
|
||||
// Try to sell the equipped Rusty Sword
|
||||
// Try to sell the equipped Leather Jerkin — not allowed.
|
||||
const res = await fetch(`${baseUrl}/api/shop/sell`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json', ...authHeaders(cookie) },
|
||||
body: JSON.stringify({ itemId: 1, quantity: 1 }),
|
||||
body: JSON.stringify({ itemId: 8, quantity: 1 }),
|
||||
});
|
||||
const body = await res.json();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user