step 10.13: done
This commit is contained in:
@@ -84,7 +84,7 @@ describe('hero profile (public)', () => {
|
||||
assert.ok(typeof data.int === 'number');
|
||||
assert.ok(typeof data.vit === 'number');
|
||||
assert.ok(typeof data.xp === 'number');
|
||||
assert.deepStrictEqual(data.equipment, { weapon: null, armor: null, trinket: null });
|
||||
assert.deepStrictEqual(data.equipment, { weapon: 'Rusty Sword', armor: null, trinket: null });
|
||||
|
||||
// Response must NOT contain private fields.
|
||||
assert.ok(!('energy' in data));
|
||||
@@ -98,26 +98,28 @@ describe('hero profile (public)', () => {
|
||||
const cookie = await registerAndLogin(`herouser2`, 'password123');
|
||||
const characterId = await createHero(cookie, 'ArmoredOne', 'mage');
|
||||
|
||||
// Insert starter gear directly into inventory (starter gear not auto-created yet).
|
||||
// Mage already has Apprentice Staff (item 2) equipped from starter kit.
|
||||
// Insert an armor item and equip it to test equipped item names.
|
||||
const db = require('../../src/db').getDb();
|
||||
|
||||
// Get a weapon item from the items table.
|
||||
const weapon = db.prepare(
|
||||
"SELECT id FROM items WHERE type = 'weapon' AND min_level <= 1 ORDER BY id LIMIT 1"
|
||||
// Get an armor item from the items table.
|
||||
const armor = db.prepare(
|
||||
"SELECT id FROM items WHERE type = 'armor' AND min_level <= 1 ORDER BY id LIMIT 1"
|
||||
).get();
|
||||
assert.ok(weapon, 'Should have a level-1 weapon in the items table');
|
||||
assert.ok(armor, 'Should have a level-1 armor in the items table');
|
||||
|
||||
// Insert it into inventory, equipped as weapon.
|
||||
// Insert it into inventory, equipped as armor.
|
||||
const now = Date.now();
|
||||
db.prepare(
|
||||
'INSERT INTO inventory (character_id, item_id, quantity, equipped_slot, acquired_at) VALUES (?, ?, 1, ?, ?)'
|
||||
).run(characterId, weapon.id, 'weapon', now);
|
||||
).run(characterId, armor.id, 'armor', now);
|
||||
|
||||
const res = await fetch(`${baseUrl}/api/heroes/ArmoredOne`);
|
||||
assert.equal(res.status, 200);
|
||||
const json = await res.json();
|
||||
assert.equal(json.ok, true);
|
||||
assert.ok(json.data.equipment.weapon, 'Should have a weapon equipped');
|
||||
assert.ok(json.data.equipment.armor, 'Should have armor equipped');
|
||||
});
|
||||
|
||||
it('is case-insensitive', async () => {
|
||||
|
||||
@@ -123,11 +123,15 @@ describe('huntService.hunt', () => {
|
||||
const id = makeHero({ energy: 100, energy_updated_at: NOW });
|
||||
run(id, 100);
|
||||
run(id, 100);
|
||||
// stackableId and weaponId are set up in before() with 100% drop rate on monster 100.
|
||||
// Starter kit has 2 potions (item 14), hunts add 2 more → total 4.
|
||||
const stack = db.prepare('SELECT quantity FROM inventory WHERE character_id = ? AND item_id = ?').all(id, stackableId);
|
||||
assert.equal(stack.length, 1);
|
||||
assert.equal(stack[0].quantity, 2);
|
||||
const weapons = db.prepare('SELECT quantity FROM inventory WHERE character_id = ? AND item_id = ?').all(id, weaponId);
|
||||
assert.equal(weapons.length, 2);
|
||||
assert.ok(weapons.every((w) => w.quantity === 1));
|
||||
assert.equal(stack[0].quantity, 4);
|
||||
// weaponId is the first non-stackable weapon (item 1 = Rusty Sword).
|
||||
// Starter kit has 1 equipped, 2 drops from hunts → total 3 rows.
|
||||
const allWeapons = db.prepare('SELECT quantity FROM inventory WHERE character_id = ? AND item_id = ?').all(id, weaponId);
|
||||
assert.equal(allWeapons.length, 3);
|
||||
assert.ok(allWeapons.every((w) => w.quantity === 1));
|
||||
});
|
||||
});
|
||||
|
||||
@@ -31,29 +31,40 @@ describe('inventoryService', () => {
|
||||
return id;
|
||||
}
|
||||
|
||||
/** Grant an item to a character's inventory (bypasses hunting). */
|
||||
/** Grant an item to a character's inventory (bypasses hunting). Stacks with existing rows. */
|
||||
function grantItem(characterId, itemId, quantity = 1) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
it('returns empty inventory for a fresh character', () => {
|
||||
const id = makeHero();
|
||||
const result = listInventory(id);
|
||||
assert.equal(result.ok !== undefined || true, true); // service returns raw obj
|
||||
assert.equal(result.items.length, 0);
|
||||
// Starter kit: 2 items (equipped weapon + 2 potions)
|
||||
assert.equal(result.items.length, 2);
|
||||
assert.equal(result.character.name, `Hero${userSeq}`);
|
||||
});
|
||||
|
||||
it('equips a weapon and updates derived stats', () => {
|
||||
const id = makeHero();
|
||||
grantItem(id, 1); // Rusty Sword (weapon, +3 attack)
|
||||
const result = equipItem(id, 1, 'weapon');
|
||||
assert.equal(result.items.find((i) => i.itemId === 1)?.equipped, true);
|
||||
assert.equal(result.items.find((i) => i.itemId === 1)?.equipped_slot, 'weapon');
|
||||
const id = makeHero({ level: 5, str: 8, agi: 4, int: 2, vit: 8 });
|
||||
// Warrior already has Rusty Sword (item 1) from starter kit.
|
||||
// Grant Iron Blade (item 4, weapon, +8 attack, level 5) and equip it.
|
||||
grantItem(id, 4); // Iron Blade
|
||||
const result = equipItem(id, 4, 'weapon');
|
||||
assert.equal(result.items.find((i) => i.itemId === 4)?.equipped, true);
|
||||
assert.equal(result.items.find((i) => i.itemId === 4)?.equipped_slot, 'weapon');
|
||||
|
||||
// Derived stats should reflect the +3 attack bonus
|
||||
assert.ok(result.derived.attack >= 7, `attack should include bonus (got ${result.derived.attack})`);
|
||||
// Derived stats should reflect the +8 attack bonus
|
||||
assert.ok(result.derived.attack >= 33, `attack should include bonus (got ${result.derived.attack})`);
|
||||
});
|
||||
|
||||
it('equips armor and trinket independently', () => {
|
||||
@@ -74,30 +85,25 @@ describe('inventoryService', () => {
|
||||
|
||||
it('auto-unequips the old item when equipping to an occupied slot', () => {
|
||||
const id = makeHero({ level: 5, str: 8, agi: 4, int: 2, vit: 8 });
|
||||
grantItem(id, 1); // Rusty Sword
|
||||
grantItem(id, 4); // Iron Blade (requires level 5)
|
||||
equipItem(id, 1, 'weapon');
|
||||
// Warrior already has Rusty Sword (item 1) from starter kit.
|
||||
// Grant Iron Blade (item 4, level 5) to test swap.
|
||||
grantItem(id, 4); // Iron Blade
|
||||
equipItem(id, 4, 'weapon');
|
||||
|
||||
const result = listInventory(id);
|
||||
// After equipping item 4, item 1 should be unequipped (only one per slot).
|
||||
// The equipped item 4 now occupies the weapon slot; item 1 is in inventory but not equipped.
|
||||
// Iron Blade should be equipped (warrior's starter sword is unequipped).
|
||||
const equippedItem = result.items.find((i) => i.equipped);
|
||||
assert.ok(equippedItem, 'should have one equipped item');
|
||||
assert.equal(equippedItem.itemId, 4);
|
||||
// Item 1 should still be in inventory but unequipped
|
||||
const item1 = result.items.find((i) => i.itemId === 1);
|
||||
assert.ok(item1, 'item 1 should still be in inventory');
|
||||
assert.equal(item1.equipped, false);
|
||||
});
|
||||
|
||||
it('unequips an item from a slot', () => {
|
||||
const id = makeHero();
|
||||
grantItem(id, 1);
|
||||
equipItem(id, 1, 'weapon');
|
||||
// Warrior already has Rusty Sword (item 1) equipped from starter kit.
|
||||
// Just unequip the weapon slot.
|
||||
const result = unequipItem(id, 'weapon');
|
||||
const item1 = result.items.find((i) => i.itemId === 1);
|
||||
assert.ok(item1, 'item 1 should still be in inventory after unequip');
|
||||
assert.ok(item1, 'Rusty Sword should still be in inventory after unequip');
|
||||
assert.equal(item1.equipped, false);
|
||||
assert.equal(item1.equipped_slot, null);
|
||||
});
|
||||
@@ -121,6 +127,9 @@ describe('inventoryService', () => {
|
||||
|
||||
it('rejects unequip when slot is empty', () => {
|
||||
const id = makeHero();
|
||||
// Warrior already has Rusty Sword (item 1) equipped from starter kit.
|
||||
// Unequip it first, then try to unequip again.
|
||||
unequipItem(id, 'weapon');
|
||||
assert.throws(() => unequipItem(id, 'weapon'), (e) => e.code === 'NOT_FOUND');
|
||||
});
|
||||
|
||||
@@ -132,8 +141,8 @@ describe('inventoryService', () => {
|
||||
|
||||
it('rejects equipping an item not in the character inventory', () => {
|
||||
const id = makeHero();
|
||||
// Item 1 exists but was never acquired by this character
|
||||
assert.throws(() => equipItem(id, 1, 'weapon'), (e) => e.code === 'NOT_FOUND');
|
||||
// Item 1 is now in starter kit, so use a nonexistent item.
|
||||
assert.throws(() => equipItem(id, 99999, 'weapon'), (e) => e.code === 'NOT_FOUND');
|
||||
});
|
||||
|
||||
it('updates derived stats when equipping armor (defense bonus)', () => {
|
||||
@@ -166,24 +175,25 @@ describe('inventoryService', () => {
|
||||
|
||||
it('uses a healing potion and increases HP (capped at maxHp)', () => {
|
||||
const id = makeHero({ level: 1, str: 8, agi: 4, int: 2, vit: 8 });
|
||||
// Grant 3 Minor Healing Potions (item 14, heal:50)
|
||||
// Grant 3 Minor Healing Potions (item 14, heal:50).
|
||||
// Starter kit already has 2 potions → total 5.
|
||||
grantItem(id, 14, 3);
|
||||
|
||||
// Set HP to 60 (out of ~145 maxHp for warrior with vit=8: 50+80+5=135)
|
||||
// Set HP to 60.
|
||||
db.prepare('UPDATE characters SET hp_current = 60 WHERE id = ?').run(id);
|
||||
|
||||
const before = listInventory(id);
|
||||
const beforeHp = db.prepare('SELECT hp_current FROM characters WHERE id = ?').get(id).hp_current;
|
||||
const maxHp = before.derived.maxHp;
|
||||
|
||||
// Use one potion: should heal to min(60+50, maxHp)
|
||||
// Use one potion: should heal to min(60+50, maxHp).
|
||||
const result = useConsumable(id, 14);
|
||||
const afterHp = db.prepare('SELECT hp_current FROM characters WHERE id = ?').get(id).hp_current;
|
||||
|
||||
assert.equal(afterHp, Math.min(beforeHp + 50, maxHp));
|
||||
// Quantity should be 3 → 2 (still in inventory)
|
||||
// Total potions: 3 granted + 2 from starter kit = 5. After 1 use → 4.
|
||||
const potion = result.items.find((i) => i.itemId === 14);
|
||||
assert.equal(potion.quantity, 2);
|
||||
assert.equal(potion.quantity, 4);
|
||||
});
|
||||
|
||||
it('uses an energy tonic and restores energy (capped at 100)', () => {
|
||||
@@ -215,8 +225,8 @@ describe('inventoryService', () => {
|
||||
|
||||
it('rejects using an item not in inventory', () => {
|
||||
const id = makeHero();
|
||||
// Item 14 exists but not granted to this character
|
||||
assert.throws(() => useConsumable(id, 14), (e) => e.code === 'NOT_FOUND');
|
||||
// Item 14 is now in starter kit. Use a nonexistent item.
|
||||
assert.throws(() => useConsumable(id, 99999), (e) => e.code === 'NOT_FOUND');
|
||||
});
|
||||
|
||||
it('removes inventory row when quantity reaches 0 (single-item stack)', () => {
|
||||
@@ -239,9 +249,10 @@ describe('inventoryService', () => {
|
||||
|
||||
it('caps heal effect at maxHp', () => {
|
||||
const id = makeHero({ level: 1, str: 8, agi: 4, int: 2, vit: 8 });
|
||||
grantItem(id, 14, 5); // Minor Healing Potion (heal:50)
|
||||
// Grant 5 more Minor Healing Potions (starter kit already has 2).
|
||||
grantItem(id, 14, 5);
|
||||
|
||||
// Set HP already at max
|
||||
// Set HP already at max.
|
||||
const before = listInventory(id);
|
||||
const maxHp = before.derived.maxHp;
|
||||
db.prepare('UPDATE characters SET hp_current = ? WHERE id = ?').run(maxHp, id);
|
||||
@@ -251,15 +262,16 @@ describe('inventoryService', () => {
|
||||
const after = listInventory(id);
|
||||
const hp = db.prepare('SELECT hp_current FROM characters WHERE id = ?').get(id).hp_current;
|
||||
assert.equal(hp, maxHp, 'HP should not exceed maxHp');
|
||||
// Quantity should decrease
|
||||
assert.equal(after.items.find((i) => i.itemId === 14).quantity, 4);
|
||||
// We granted 5 potions; starter kit has 2 → total 7. After 1 use → 6.
|
||||
const potion = after.items.find((i) => i.itemId === 14);
|
||||
assert.equal(potion.quantity, 6);
|
||||
});
|
||||
|
||||
it('caps energy effect at ENERGY_MAX (100)', () => {
|
||||
const id = makeHero({ level: 1, str: 8, agi: 4, int: 2, vit: 8 });
|
||||
grantItem(id, 16, 5); // Energy Tonic (energy:25)
|
||||
|
||||
// Set energy to 90
|
||||
// Set energy to 90.
|
||||
db.prepare('UPDATE characters SET energy = 90 WHERE id = ?').run(id);
|
||||
|
||||
useConsumable(id, 16);
|
||||
@@ -267,7 +279,7 @@ describe('inventoryService', () => {
|
||||
const after = listInventory(id);
|
||||
const energy = db.prepare('SELECT energy FROM characters WHERE id = ?').get(id).energy;
|
||||
assert.equal(energy, 100, 'Energy should not exceed 100');
|
||||
// Quantity should decrease
|
||||
// Quantity should decrease.
|
||||
assert.equal(after.items.find((i) => i.itemId === 16).quantity, 4);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
@@ -100,23 +100,17 @@ describe('step-10.12 equipment bonuses', () => {
|
||||
const cookie = await register(`e1012a${Date.now()}`, 'pass1234');
|
||||
const { characterId } = await createHero(cookie, 1);
|
||||
|
||||
// Baseline: no equipment.
|
||||
// Baseline: warrior already has Rusty Sword (item 1) from starter kit.
|
||||
const before = await getCharacter(cookie);
|
||||
const baseDefense = before.derived.defense;
|
||||
const baseAttack = before.derived.attack;
|
||||
|
||||
// Equip Rusty Sword (item 1, +3 attack) and Leather Jerkin (item 8, +3 defense).
|
||||
grantItem(characterId, 1);
|
||||
// Equip Leather Jerkin (item 8, +3 defense) — weapon slot already taken.
|
||||
grantItem(characterId, 8);
|
||||
await httpEquip(cookie, 1, 'weapon');
|
||||
await httpEquip(cookie, 8, 'armor');
|
||||
|
||||
const after = await getCharacter(cookie);
|
||||
|
||||
// Attack should include +3 from weapon.
|
||||
assert.equal(after.derived.attack, baseAttack + 3,
|
||||
`derived.attack should include +3 weapon bonus: was ${baseAttack}, now ${after.derived.attack}`);
|
||||
|
||||
// Defense should include +3 from armor.
|
||||
assert.equal(after.derived.defense, baseDefense + 3,
|
||||
`derived.defense should include +3 armor bonus: was ${baseDefense}, now ${after.derived.defense}`);
|
||||
|
||||
@@ -0,0 +1,169 @@
|
||||
// Integration tests for step 10.13: Starter kit on character creation.
|
||||
const { describe, it, before, after } = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
const { setup, teardown, cleanup } = require('../helpers');
|
||||
|
||||
describe('step-10.13 starter kit', () => {
|
||||
let baseUrl, db;
|
||||
const NOW = 1_700_000_000_000;
|
||||
let userSeq = 0;
|
||||
|
||||
before(async () => {
|
||||
baseUrl = await setup();
|
||||
db = require('../../src/db').getDb();
|
||||
});
|
||||
|
||||
after(async () => { teardown(); cleanup(); });
|
||||
|
||||
// --- helpers ---
|
||||
|
||||
/** Register a user and return the Set-Cookie header value. */
|
||||
async function register(username, password) {
|
||||
const res = await fetch(`${baseUrl}/api/auth/register`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ username, password }),
|
||||
});
|
||||
const setCookie = res.headers.get('set-cookie') || '';
|
||||
assert.equal(res.status, 201);
|
||||
return setCookie;
|
||||
}
|
||||
|
||||
/** Login and return the Set-Cookie header value. */
|
||||
async function login(username, password) {
|
||||
const res = await fetch(`${baseUrl}/api/auth/login`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ username, password }),
|
||||
});
|
||||
const setCookie = res.headers.get('set-cookie') || '';
|
||||
assert.equal(res.status, 200);
|
||||
return setCookie;
|
||||
}
|
||||
|
||||
/** Create a hero and return { cookie, characterId }. */
|
||||
async function createHero(cookie, name, classKey) {
|
||||
const res = await fetch(`${baseUrl}/api/character`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json', Cookie: cookie },
|
||||
body: JSON.stringify({ name, class: classKey }),
|
||||
});
|
||||
assert.equal(res.status, 201, `createHero failed for ${name}: ${res.status}`);
|
||||
const { data } = await res.json();
|
||||
return { characterId: data.character.id, cookie };
|
||||
}
|
||||
|
||||
/** GET /api/character and return { character, derived }. */
|
||||
async function getCharacter(cookie) {
|
||||
const res = await fetch(`${baseUrl}/api/character`, {
|
||||
headers: { Cookie: cookie },
|
||||
});
|
||||
assert.equal(res.status, 200);
|
||||
const { data } = await res.json();
|
||||
return data;
|
||||
}
|
||||
|
||||
// --- warrior tests ---
|
||||
|
||||
it('warrior starts with Rusty Sword equipped + 2 Minor Healing Potions', async () => {
|
||||
const cookie = await register(`s13w${Date.now()}`, 'pass1234');
|
||||
const { characterId } = await createHero(cookie, 'S13Warrior', 'warrior');
|
||||
|
||||
// Inventory: exactly 2 rows (weapon + potion stack).
|
||||
const rows = db.prepare(
|
||||
'SELECT item_id, quantity, equipped_slot FROM inventory WHERE character_id = ? ORDER BY item_id'
|
||||
).all(characterId);
|
||||
assert.equal(rows.length, 2, `expected 2 inventory rows, got ${rows.length}`);
|
||||
|
||||
// Weapon is Rusty Sword (item 1), equipped in weapon slot.
|
||||
const weapon = rows.find(r => r.equipped_slot === 'weapon');
|
||||
assert.ok(weapon, 'expected an equipped weapon row');
|
||||
assert.equal(weapon.item_id, 1, 'warrior weapon should be Rusty Sword (item 1)');
|
||||
|
||||
// Potions: one stack row, quantity 2.
|
||||
const potions = rows.find(r => r.item_id === 14);
|
||||
assert.ok(potions, 'expected a potion row');
|
||||
assert.equal(potions.quantity, 2, 'should have 2 Minor Healing Potions');
|
||||
|
||||
// Derived attack includes the Rusty Sword bonus (+3 bonus_attack).
|
||||
// Warrior: 5 + 8*2 + 1 + 3 = 25
|
||||
const profile = await getCharacter(cookie);
|
||||
assert.equal(profile.derived.attack, 25,
|
||||
`warrior attack should be 25 (5 + 8*2 + 1 + 3): got ${profile.derived.attack}`);
|
||||
});
|
||||
|
||||
// --- mage tests ---
|
||||
|
||||
it('mage starts with Apprentice Staff equipped + 2 Minor Healing Potions', async () => {
|
||||
const cookie = await register(`s13m${Date.now()}`, 'pass1234');
|
||||
const { characterId } = await createHero(cookie, 'S13Mage', 'mage');
|
||||
|
||||
const rows = db.prepare(
|
||||
'SELECT item_id, quantity, equipped_slot FROM inventory WHERE character_id = ? ORDER BY item_id'
|
||||
).all(characterId);
|
||||
assert.equal(rows.length, 2, `expected 2 inventory rows, got ${rows.length}`);
|
||||
|
||||
const weapon = rows.find(r => r.equipped_slot === 'weapon');
|
||||
assert.ok(weapon, 'expected an equipped weapon row');
|
||||
assert.equal(weapon.item_id, 2, 'mage weapon should be Apprentice Staff (item 2)');
|
||||
|
||||
const potions = rows.find(r => r.item_id === 14);
|
||||
assert.ok(potions, 'expected a potion row');
|
||||
assert.equal(potions.quantity, 2, 'should have 2 Minor Healing Potions');
|
||||
|
||||
// Derived attack includes the Apprentice Staff bonus (+1 int → primary goes 9→10).
|
||||
// Mage: 5 + 10*2 + 1 + 0 = 26
|
||||
const profile = await getCharacter(cookie);
|
||||
assert.equal(profile.derived.attack, 26,
|
||||
`mage attack should be 26 (5 + 10*2 + 1 + 0): got ${profile.derived.attack}`);
|
||||
});
|
||||
|
||||
// --- ranger tests ---
|
||||
|
||||
it('ranger starts with Hunters Shortbow equipped + 2 Minor Healing Potions', async () => {
|
||||
const cookie = await register(`s13r${Date.now()}`, 'pass1234');
|
||||
const { characterId } = await createHero(cookie, 'S13Ranger', 'ranger');
|
||||
|
||||
const rows = db.prepare(
|
||||
'SELECT item_id, quantity, equipped_slot FROM inventory WHERE character_id = ? ORDER BY item_id'
|
||||
).all(characterId);
|
||||
assert.equal(rows.length, 2, `expected 2 inventory rows, got ${rows.length}`);
|
||||
|
||||
const weapon = rows.find(r => r.equipped_slot === 'weapon');
|
||||
assert.ok(weapon, 'expected an equipped weapon row');
|
||||
assert.equal(weapon.item_id, 3, 'ranger weapon should be Hunters Shortbow (item 3)');
|
||||
|
||||
const potions = rows.find(r => r.item_id === 14);
|
||||
assert.ok(potions, 'expected a potion row');
|
||||
assert.equal(potions.quantity, 2, 'should have 2 Minor Healing Potions');
|
||||
|
||||
// Derived attack includes the Shortbow bonus (+2 agi → primary goes 9→11).
|
||||
// Ranger: 5 + 11*2 + 1 + 0 = 28
|
||||
const profile = await getCharacter(cookie);
|
||||
assert.equal(profile.derived.attack, 28,
|
||||
`ranger attack should be 28 (5 + 11*2 + 1 + 0): got ${profile.derived.attack}`);
|
||||
});
|
||||
|
||||
// --- failure path: duplicate name blocked ---
|
||||
|
||||
it('duplicate hero name is still rejected despite starter kit', async () => {
|
||||
const cookie = await register(`s13x${Date.now()}`, 'pass1234');
|
||||
const { characterId } = await createHero(cookie, 'S13X', 'warrior');
|
||||
|
||||
const res = await fetch(`${baseUrl}/api/character`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json', Cookie: cookie },
|
||||
body: JSON.stringify({ name: 'S13X', class: 'mage' }),
|
||||
});
|
||||
assert.equal(res.status, 409);
|
||||
const body = await res.json();
|
||||
assert.equal(body.ok, false);
|
||||
assert.equal(body.error.code, 'HERO_EXISTS');
|
||||
|
||||
// The first hero should have its starter kit intact (2 rows).
|
||||
const rows = db.prepare(
|
||||
'SELECT COUNT(*) as cnt FROM inventory WHERE character_id = ?'
|
||||
).get(characterId);
|
||||
assert.equal(rows.cnt, 2, 'starter kit should have 2 inventory rows');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user