step 10.1: done
This commit is contained in:
@@ -404,4 +404,50 @@ describe('expeditions', () => {
|
||||
assert.equal(res.status, 401);
|
||||
assert.equal(body.ok, false);
|
||||
});
|
||||
|
||||
// ─── 10.1: expedition start must deduct energy correctly ──────────────
|
||||
|
||||
it('10.1: starts expedition → DB energy = energy - cost, timestamp NOT in future', async () => {
|
||||
const { cookie, characterId } = await setupHero({ energy: 100, gold: 500 });
|
||||
|
||||
const res = await fetch(`${baseUrl}/api/expeditions/start`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json', ...authHeaders(cookie) },
|
||||
body: JSON.stringify({ defId: 1 }), // scout, cost=10
|
||||
});
|
||||
assert.equal(res.status, 200);
|
||||
const body = await res.json();
|
||||
assert.equal(body.ok, true);
|
||||
|
||||
// Assert DB state directly — this is the core of 10.1.
|
||||
const db = require('../../src/db').getDb();
|
||||
const ch = db.prepare('SELECT energy, energy_updated_at FROM characters WHERE id = ?').get(characterId);
|
||||
assert.equal(ch.energy, 90, 'DB energy must be 100 - 10 = 90');
|
||||
assert.ok(ch.energy_updated_at <= Date.now(), 'energy_updated_at must NOT be in the future');
|
||||
|
||||
// Assert GET /api/character returns the same energy.
|
||||
const charRes = await fetch(`${baseUrl}/api/character`, {
|
||||
headers: authHeaders(cookie),
|
||||
});
|
||||
assert.equal(charRes.status, 200);
|
||||
const charBody = await charRes.json();
|
||||
assert.equal(charBody.data.energy.current, 90);
|
||||
});
|
||||
|
||||
it('10.1: starting at energy 55 → DB energy = 45', async () => {
|
||||
const { cookie, characterId } = await setupHero({ energy: 55, gold: 500 });
|
||||
|
||||
const res = await fetch(`${baseUrl}/api/expeditions/start`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json', ...authHeaders(cookie) },
|
||||
body: JSON.stringify({ defId: 1 }), // scout, cost=10
|
||||
});
|
||||
assert.equal(res.status, 200);
|
||||
const body = await res.json();
|
||||
assert.equal(body.ok, true);
|
||||
|
||||
const db = require('../../src/db').getDb();
|
||||
const ch = db.prepare('SELECT energy FROM characters WHERE id = ?').get(characterId);
|
||||
assert.equal(ch.energy, 45, 'DB energy must be 55 - 10 = 45');
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user