Initial commit

This commit is contained in:
2026-09-22 16:27:53 +02:00
commit 9b29dc8253
70 changed files with 14726 additions and 0 deletions
+199
View File
@@ -0,0 +1,199 @@
// Integration tests exercising the real Express app + a real Postgres
// database through Prisma. These need DATABASE_URL pointed at a reachable,
// migrated database (see /README.md) — they are skipped automatically if the
// DB isn't reachable so `npm test` still passes in an environment with no DB.
const request = require('supertest');
const crypto = require('crypto');
const prisma = require('../db');
const { createApp } = require('../app');
const app = createApp();
app.set('io', { to: () => ({ emit: () => {} }) }); // stub Socket.io for tests
let dbAvailable = true;
const cleanupCampaignIds = [];
const cleanupUserIds = [];
beforeAll(async () => {
try {
await prisma.$connect();
} catch {
dbAvailable = false;
}
});
afterAll(async () => {
if (dbAvailable) {
for (const id of cleanupCampaignIds) {
await prisma.campaign.delete({ where: { id } }).catch(() => {});
}
for (const id of cleanupUserIds) {
await prisma.user.delete({ where: { id } }).catch(() => {});
}
}
await prisma.$disconnect();
});
function uniqueEmail(prefix) {
return `${prefix}-${crypto.randomUUID()}@test.local`;
}
function itIfDb(name, fn) {
test(name, async () => {
if (!dbAvailable) {
console.warn(`Skipping "${name}" — no reachable database (set DATABASE_URL and run migrations).`);
return;
}
await fn();
});
}
describe('auth', () => {
itIfDb('registers, logs in, and reads the current user', async () => {
const email = uniqueEmail('dm');
const registerRes = await request(app)
.post('/auth/register')
.send({ email, password: 'password123', displayName: 'Test DM' });
expect(registerRes.status).toBe(201);
expect(registerRes.body.token).toBeTruthy();
cleanupUserIds.push(registerRes.body.user.id);
const loginRes = await request(app).post('/auth/login').send({ email, password: 'password123' });
expect(loginRes.status).toBe(200);
const meRes = await request(app).get('/auth/me').set('Authorization', `Bearer ${loginRes.body.token}`);
expect(meRes.status).toBe(200);
expect(meRes.body.user.email).toBe(email);
});
itIfDb('rejects a bad password', async () => {
const email = uniqueEmail('baduser');
const registerRes = await request(app)
.post('/auth/register')
.send({ email, password: 'password123', displayName: 'Bad Login' });
cleanupUserIds.push(registerRes.body.user.id);
const loginRes = await request(app).post('/auth/login').send({ email, password: 'wrong-password' });
expect(loginRes.status).toBe(401);
});
});
describe('campaigns + characters + build flow', () => {
itIfDb('DM creates a campaign, player joins, DM builds a character end-to-end', async () => {
const dmEmail = uniqueEmail('dm2');
const playerEmail = uniqueEmail('player2');
const dmRegister = await request(app)
.post('/auth/register')
.send({ email: dmEmail, password: 'password123', displayName: 'DM Two' });
const dmToken = dmRegister.body.token;
cleanupUserIds.push(dmRegister.body.user.id);
const playerRegister = await request(app)
.post('/auth/register')
.send({ email: playerEmail, password: 'password123', displayName: 'Player Two' });
cleanupUserIds.push(playerRegister.body.user.id);
const campaignRes = await request(app)
.post('/campaigns')
.set('Authorization', `Bearer ${dmToken}`)
.send({ name: 'Integration Test Campaign', startingItemCount: 1, startingSpellCount: 0, skipMotivationRoll: true });
expect(campaignRes.status).toBe(201);
const campaign = campaignRes.body.campaign;
cleanupCampaignIds.push(campaign.id);
const joinRes = await request(app)
.post(`/campaigns/join/${campaign.inviteCode}`)
.set('Authorization', `Bearer ${playerRegister.body.token}`);
expect(joinRes.status).toBe(201);
// DM adds a homebrew weapon to the pool so the equipment step has something to pick.
const weaponRes = await request(app)
.post(`/campaigns/${campaign.id}/weapons`)
.set('Authorization', `Bearer ${dmToken}`)
.send({ name: 'Test Dagger', damageDice: '1d4', damageType: 'piercing', attackAbility: 'FINESSE', properties: [] });
expect(weaponRes.status).toBe(201);
const weaponId = weaponRes.body.weapon.id;
const characterRes = await request(app)
.post(`/campaigns/${campaign.id}/characters`)
.set('Authorization', `Bearer ${dmToken}`)
.send({ name: 'Integration Hero' });
expect(characterRes.status).toBe(201);
const characterId = characterRes.body.character.id;
const auth = { Authorization: `Bearer ${dmToken}` };
// Step 1: species
let rollRes = await request(app).post(`/characters/${characterId}/build/roll`).set(auth);
expect(rollRes.status).toBe(200);
let speciesChoices = rollRes.body.buildState.speciesChoices;
const species = speciesChoices.options[0];
let chooseRes = await request(app)
.post(`/characters/${characterId}/build/choose`)
.set(auth)
.send(species === 'HALF_ELF' ? { species, halfElfBonusAbilities: ['STR', 'INT'] } : { species });
expect(chooseRes.status).toBe(200);
expect(chooseRes.body.buildStage).toBe('ROLLING_CLASS');
// Step 2: class (skip any reroll results deterministically)
rollRes = await request(app).post(`/characters/${characterId}/build/roll`).set(auth);
while (rollRes.body.buildState.classChoices.type === 'reroll') {
rollRes = await request(app).post(`/characters/${characterId}/build/roll`).set(auth);
}
const characterClass = rollRes.body.buildState.classChoices.options[0];
chooseRes = await request(app).post(`/characters/${characterId}/build/choose`).set(auth).send({ characterClass });
expect(chooseRes.status).toBe(200);
// skipMotivationRoll was true, so motivation is skipped entirely.
expect(chooseRes.body.buildStage).toBe('ASSIGNING_SCORES');
// Step 4: ability scores
rollRes = await request(app).post(`/characters/${characterId}/build/roll`).set(auth);
expect(rollRes.body.buildState.abilityRolls).toHaveLength(6);
chooseRes = await request(app)
.post(`/characters/${characterId}/build/choose`)
.set(auth)
.send({ assignments: { STR: 0, DEX: 1, CON: 2, INT: 3, WIS: 4, CHA: 5 } });
expect(chooseRes.status).toBe(200);
expect(chooseRes.body.buildStage).toBe('ASSIGNING_PROFICIENCIES');
// Step 5: skills
chooseRes = await request(app)
.post(`/characters/${characterId}/build/choose`)
.set(auth)
.send({ skillProficiencies: [{ skill: 'ATHLETICS', proficient: true, expertise: false }] });
expect(chooseRes.status).toBe(200);
expect(chooseRes.body.buildStage).toBe('CHOOSING_EQUIPMENT');
// Step 6: equipment
chooseRes = await request(app)
.post(`/characters/${characterId}/build/choose`)
.set(auth)
.send({ weaponIds: [weaponId], spellIds: [] });
expect(chooseRes.status).toBe(200);
expect(chooseRes.body.buildStage).toBe('CHOOSING_EQUIPMENT'); // stays here until finalize
// Step 7: finalize
const finalizeRes = await request(app).post(`/characters/${characterId}/build/finalize`).set(auth);
expect(finalizeRes.status).toBe(200);
const character = finalizeRes.body.character;
expect(character.buildStage).toBe('COMPLETE');
expect(character.sheet).not.toBeNull();
expect(character.currentHp).toBe(character.sheet.hp.max);
expect(character.weapons).toHaveLength(1);
// Quick stats should now work.
const quickStatsRes = await request(app).get(`/characters/${characterId}/quick-stats`).set(auth);
expect(quickStatsRes.status).toBe(200);
expect(quickStatsRes.body.quickStats.weapons).toHaveLength(1);
// A non-member cannot read the character.
const outsiderRegister = await request(app)
.post('/auth/register')
.send({ email: uniqueEmail('outsider'), password: 'password123', displayName: 'Outsider' });
cleanupUserIds.push(outsiderRegister.body.user.id);
const forbiddenRes = await request(app)
.get(`/characters/${characterId}`)
.set('Authorization', `Bearer ${outsiderRegister.body.token}`);
expect(forbiddenRes.status).toBe(403);
});
});