feat(pagination): add generic typed pagination with ordering to all list endpoints

- add PaginationQueryDto<TOrderBy> generic with orderBy and orderDir fields
- add SessionOrderBy, EnvironmentOrderBy, ScenarioOrderBy type aliases
- update session, environment, scenario services and controllers to use typed pagination
- update MCP list_sessions, list_environments, list_scenarios tools to expose orderBy/orderDir
- update tests for all list endpoints to cover page, limit, ordering, and invalid param rejection
This commit is contained in:
2026-04-07 17:54:23 +03:00
parent d9cdb64ee2
commit 36f8b8c0ca
16 changed files with 337 additions and 81 deletions
+44 -2
View File
@@ -64,9 +64,51 @@ describe('EnvironmentController', () => {
// ── GET /environments ──────────────────────────────────────────────────────
describe('GET /environments', () => {
it('returns 200 with an array', async () => {
it('returns 200 with a paginated result', async () => {
const res = await request(app.getHttpServer()).get('/environments').expect(200);
expect(Array.isArray(res.body)).toBe(true);
expect(Array.isArray(res.body.data)).toBe(true);
expect(typeof res.body.total).toBe('number');
expect(res.body).toHaveProperty('page');
expect(res.body).toHaveProperty('limit');
});
it('respects page and limit params', async () => {
// seed two extra environments
await request(app.getHttpServer()).post('/environments').send({ name: 'env-page-1', urls: {} }).expect(201);
await request(app.getHttpServer()).post('/environments').send({ name: 'env-page-2', urls: {} }).expect(201);
const res = await request(app.getHttpServer()).get('/environments?page=1&limit=1').expect(200);
expect(res.body.data).toHaveLength(1);
expect(res.body.page).toBe(1);
expect(res.body.limit).toBe(1);
});
it('returns empty data array for out-of-range page', async () => {
const res = await request(app.getHttpServer()).get('/environments?page=9999&limit=20').expect(200);
expect(res.body.data).toHaveLength(0);
});
it('returns 400 for invalid page param', async () => {
await request(app.getHttpServer()).get('/environments?page=0').expect(400);
});
it('orders by name ASC', async () => {
await request(app.getHttpServer()).post('/environments').send({ name: 'zzz-env', urls: {} });
await request(app.getHttpServer()).post('/environments').send({ name: 'aaa-env', urls: {} });
const res = await request(app.getHttpServer()).get('/environments?orderBy=name&orderDir=ASC').expect(200);
const names: string[] = res.body.data.map((e: any) => e.name);
expect(names).toEqual([...names].sort());
});
it('orders by name DESC', async () => {
const res = await request(app.getHttpServer()).get('/environments?orderBy=name&orderDir=DESC').expect(200);
const names: string[] = res.body.data.map((e: any) => e.name);
expect(names).toEqual([...names].sort().reverse());
});
it('returns 400 for invalid orderDir', async () => {
await request(app.getHttpServer()).get('/environments?orderDir=SIDEWAYS').expect(400);
});
});