feat: wire HuntingController/HuntingModule and enrich current-location with monster pool

Registers HuntingModule (POST /api/hunts) into the DI graph alongside its
new entities, and adds possibleMonsters (enabled LocationMonster pool,
weight-descending, empty when hunting is disabled) to
WorldService.getCurrentLocation. Also updates the pre-existing
DB-less app.e2e-spec.ts to override HuntingModule the same way the other
feature modules already are, since it now needs a real DataSource.
This commit is contained in:
Bastian Wagner
2026-08-19 12:53:09 +02:00
parent 568478dcd2
commit bb307da0ff
9 changed files with 341 additions and 59 deletions

View File

@@ -6,6 +6,7 @@ import { AppModule } from './../src/app.module';
import { CharactersModule } from './../src/characters/characters.module';
import { DatabaseModule } from './../src/database/database.module';
import { configureApplication } from './../src/app.config';
import { HuntingModule } from './../src/hunting/hunting.module';
import { TravelModule } from './../src/travel/travel.module';
import { WorldModule } from './../src/world/world.module';
@@ -21,6 +22,9 @@ class TestTravelModule {}
@Module({})
class TestWorldModule {}
@Module({})
class TestHuntingModule {}
describe('API (e2e)', () => {
let app: INestApplication<App>;
@@ -36,6 +40,8 @@ describe('API (e2e)', () => {
.useModule(TestTravelModule)
.overrideModule(WorldModule)
.useModule(TestWorldModule)
.overrideModule(HuntingModule)
.useModule(TestHuntingModule)
.compile();
app = moduleFixture.createNestApplication();

View File

@@ -8,6 +8,7 @@ import { AppModule } from './../src/app.module';
import { CharactersModule } from './../src/characters/characters.module';
import { DatabaseModule } from './../src/database/database.module';
import { configureApplication } from './../src/app.config';
import { HuntingModule } from './../src/hunting/hunting.module';
import { TravelModule } from './../src/travel/travel.module';
import { WorldModule } from './../src/world/world.module';
import { DEMO_CHARACTER_ID } from './../src/demo/demo-character.constants';
@@ -31,6 +32,9 @@ class TestTravelModule {}
@Module({})
class TestWorldModule {}
@Module({})
class TestHuntingModule {}
describe('Visible vertical slice smoke (e2e)', () => {
describe('without a developer database', () => {
let app: INestApplication<App>;
@@ -47,6 +51,8 @@ describe('Visible vertical slice smoke (e2e)', () => {
.useModule(TestTravelModule)
.overrideModule(WorldModule)
.useModule(TestWorldModule)
.overrideModule(HuntingModule)
.useModule(TestHuntingModule)
.compile();
app = moduleFixture.createNestApplication();
@@ -153,57 +159,151 @@ describe('Visible vertical slice smoke (e2e)', () => {
.expect(400);
});
it(
'POST /api/travel starts a travel, rejects a concurrent start, and completes into the moved character (full happy path)',
async () => {
// `GET current-location` lazily completes any overdue travel left
// behind by a previous run before we read the starting point, so
// this test is safe to re-run without a fresh seed.
const origin = await request(app.getHttpServer())
.get('/api/world/current-location')
.expect(200);
const originLocationId: string = origin.body.id;
const outbound = origin.body.connections[0];
const targetLocationId: string = outbound.targetLocation.id;
const travelDurationSeconds: number = outbound.travelDurationSeconds;
it('POST /api/travel starts a travel, rejects a concurrent start, and completes into the moved character (full happy path)', async () => {
// `GET current-location` lazily completes any overdue travel left
// behind by a previous run before we read the starting point, so
// this test is safe to re-run without a fresh seed.
const origin = await request(app.getHttpServer())
.get('/api/world/current-location')
.expect(200);
const originLocationId: string = origin.body.id;
const outbound = origin.body.connections[0];
const targetLocationId: string = outbound.targetLocation.id;
const travelDurationSeconds: number = outbound.travelDurationSeconds;
const started = await request(app.getHttpServer())
const started = await request(app.getHttpServer())
.post('/api/travel')
.send({ targetLocationId })
.expect(201);
expect(started.body).toMatchObject({
status: 'TRAVELLING',
targetLocation: { id: targetLocationId },
});
expect(typeof started.body.arrivesAt).toBe('string');
expect(Number.isNaN(Date.parse(started.body.arrivesAt))).toBe(false);
const concurrentStart = await request(app.getHttpServer())
.post('/api/travel')
.send({ targetLocationId })
.expect(409);
expect(concurrentStart.body).toMatchObject({
code: 'TRAVEL_ALREADY_ACTIVE',
});
const arrivedTravel = await pollUntilTravelCompletes(
app,
travelDurationSeconds,
);
expect(arrivedTravel).toMatchObject({
status: 'COMPLETED',
targetLocation: { id: targetLocationId },
});
const arrivedLocation = await request(app.getHttpServer())
.get('/api/world/current-location')
.expect(200);
expect(arrivedLocation.body.id).toBe(targetLocationId);
// Restore the demo character to its original location so the suite
// (and this test) stays safely re-runnable.
const returnConnection = arrivedLocation.body.connections.find(
(connection: { targetLocation: { id: string } }) =>
connection.targetLocation.id === originLocationId,
);
expect(returnConnection).toBeDefined();
await request(app.getHttpServer())
.post('/api/travel')
.send({ targetLocationId: originLocationId })
.expect(201);
const returnedTravel = await pollUntilTravelCompletes(
app,
returnConnection.travelDurationSeconds,
);
expect(returnedTravel).toMatchObject({
status: 'COMPLETED',
targetLocation: { id: originLocationId },
});
const restoredLocation = await request(app.getHttpServer())
.get('/api/world/current-location')
.expect(200);
expect(restoredLocation.body.id).toBe(originLocationId);
}, 30_000);
it('POST /api/hunts starts a hunt at burned-road, and a second call supersedes the first', async () => {
// Get to burned-road (the only hunting-enabled seeded location),
// remembering the origin so we can restore it afterwards and keep
// this test safely re-runnable.
const origin = await request(app.getHttpServer())
.get('/api/world/current-location')
.expect(200);
const originLocationId: string = origin.body.id;
const originLocationKey: string = origin.body.key;
let atBurnedRoad = origin.body;
if (originLocationKey !== 'burned-road') {
const toBurnedRoad = origin.body.connections.find(
(connection: { targetLocation: { key: string } }) =>
connection.targetLocation.key === 'burned-road',
);
expect(toBurnedRoad).toBeDefined();
await request(app.getHttpServer())
.post('/api/travel')
.send({ targetLocationId })
.send({ targetLocationId: toBurnedRoad.targetLocation.id })
.expect(201);
expect(started.body).toMatchObject({
status: 'TRAVELLING',
targetLocation: { id: targetLocationId },
});
expect(typeof started.body.arrivesAt).toBe('string');
expect(Number.isNaN(Date.parse(started.body.arrivesAt))).toBe(false);
await pollUntilTravelCompletes(app, toBurnedRoad.travelDurationSeconds);
const concurrentStart = await request(app.getHttpServer())
.post('/api/travel')
.send({ targetLocationId })
.expect(409);
expect(concurrentStart.body).toMatchObject({
code: 'TRAVEL_ALREADY_ACTIVE',
});
const arrivedTravel = await pollUntilTravelCompletes(
app,
travelDurationSeconds,
);
expect(arrivedTravel).toMatchObject({
status: 'COMPLETED',
targetLocation: { id: targetLocationId },
});
const arrivedLocation = await request(app.getHttpServer())
const arrived = await request(app.getHttpServer())
.get('/api/world/current-location')
.expect(200);
expect(arrivedLocation.body.id).toBe(targetLocationId);
atBurnedRoad = arrived.body;
}
// Restore the demo character to its original location so the suite
// (and this test) stays safely re-runnable.
const returnConnection = arrivedLocation.body.connections.find(
expect(atBurnedRoad.key).toBe('burned-road');
expect(atBurnedRoad.possibleMonsters).toEqual([
'Aschenratte',
'Straßenräuber',
]);
const firstHunt = await request(app.getHttpServer())
.post('/api/hunts')
.expect(201);
expect(typeof firstHunt.body.id).toBe('string');
expect(firstHunt.body.location).toMatchObject({ key: 'burned-road' });
expect(firstHunt.body.encounters).toHaveLength(3);
for (const encounter of firstHunt.body.encounters as Array<{
id: string;
monster: { key: string };
dangerRating: string;
}>) {
expect(typeof encounter.id).toBe('string');
expect(['ash-rat', 'road-bandit']).toContain(encounter.monster.key);
expect([
'WEAK',
'MATCH',
'STRONG',
'VERY_DANGEROUS',
'DEADLY',
]).toContain(encounter.dangerRating);
}
const secondHunt = await request(app.getHttpServer())
.post('/api/hunts')
.expect(201);
expect(typeof secondHunt.body.id).toBe('string');
expect(secondHunt.body.id).not.toBe(firstHunt.body.id);
// Restore the demo character to its original location so the suite
// stays safely re-runnable.
if (originLocationKey !== 'burned-road') {
const returnConnection = atBurnedRoad.connections.find(
(connection: { targetLocation: { id: string } }) =>
connection.targetLocation.id === originLocationId,
);
@@ -214,22 +314,12 @@ describe('Visible vertical slice smoke (e2e)', () => {
.send({ targetLocationId: originLocationId })
.expect(201);
const returnedTravel = await pollUntilTravelCompletes(
await pollUntilTravelCompletes(
app,
returnConnection.travelDurationSeconds,
);
expect(returnedTravel).toMatchObject({
status: 'COMPLETED',
targetLocation: { id: originLocationId },
});
const restoredLocation = await request(app.getHttpServer())
.get('/api/world/current-location')
.expect(200);
expect(restoredLocation.body.id).toBe(originLocationId);
},
30_000,
);
}
}, 30_000);
async function pollUntilTravelCompletes(
application: INestApplication<App>,