- add jest, ts-jest, supertest, and related dev dependencies - add test scripts (test, test:debug, test:watch) to package.json - add jest.config.js with ts-jest transform and module name mappers - add controller specs for auth, browser, environment, mcp, scenario, session - add mocks for jsdom, playwright, and readability - add test/tsconfig.json and exclude test dir from main tsconfig
99 lines
3.3 KiB
TypeScript
99 lines
3.3 KiB
TypeScript
import { INestApplication } from '@nestjs/common';
|
|
import { getRepositoryToken } from '@nestjs/typeorm';
|
|
import request from 'supertest';
|
|
import { buildTestApp } from './app.harness';
|
|
import { SessionEntity } from '../src/session/session.entity';
|
|
import { Repository } from 'typeorm';
|
|
|
|
/**
|
|
* Browser controller integration tests.
|
|
*
|
|
* POST /open and POST /exec need a running Playwright browser. We test
|
|
* validation rejections (no browser launched) and session-not-found paths,
|
|
* which are safe to run in a headless CI environment.
|
|
*/
|
|
describe('BrowserController', () => {
|
|
let app: INestApplication;
|
|
let sessionRepo: Repository<SessionEntity>;
|
|
|
|
const FAKE_SESSION = 'test-browser-session';
|
|
|
|
beforeAll(async () => {
|
|
app = await buildTestApp();
|
|
sessionRepo = app.get<Repository<SessionEntity>>(getRepositoryToken(SessionEntity));
|
|
|
|
// Seed a session with minimal but valid JSON so the browser code can
|
|
// deserialise it (it will still fail to open a real page, tested separately)
|
|
await sessionRepo.save(
|
|
sessionRepo.create({
|
|
sessionName: FAKE_SESSION,
|
|
token: 'fake-token',
|
|
cookies: '[]',
|
|
localStorage: '{}',
|
|
}),
|
|
);
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await app.close();
|
|
});
|
|
|
|
// ── POST /open ─────────────────────────────────────────────────────────────
|
|
|
|
describe('POST /open', () => {
|
|
it('returns 400 when body is empty', async () => {
|
|
await request(app.getHttpServer()).post('/open').send({}).expect(400);
|
|
});
|
|
|
|
it('returns 400 when sessionName is missing', async () => {
|
|
await request(app.getHttpServer())
|
|
.post('/open')
|
|
.send({ url: 'https://example.com' })
|
|
.expect(400);
|
|
});
|
|
|
|
it('returns 400 when url is missing', async () => {
|
|
await request(app.getHttpServer())
|
|
.post('/open')
|
|
.send({ sessionName: FAKE_SESSION })
|
|
.expect(400);
|
|
});
|
|
|
|
it('returns 404 when session does not exist', async () => {
|
|
await request(app.getHttpServer())
|
|
.post('/open')
|
|
.send({ sessionName: 'no-such-session', url: 'https://example.com' })
|
|
.expect(404);
|
|
});
|
|
});
|
|
|
|
// ── POST /exec ─────────────────────────────────────────────────────────────
|
|
|
|
describe('POST /exec', () => {
|
|
it('returns 400 when body is empty', async () => {
|
|
await request(app.getHttpServer()).post('/exec').send({}).expect(400);
|
|
});
|
|
|
|
it('returns 400 when code is missing', async () => {
|
|
await request(app.getHttpServer())
|
|
.post('/exec')
|
|
.send({ sessionName: FAKE_SESSION })
|
|
.expect(400);
|
|
});
|
|
|
|
it('returns 400 when code has a syntax error', async () => {
|
|
await request(app.getHttpServer())
|
|
.post('/exec')
|
|
.send({ sessionName: FAKE_SESSION, code: 'this is not valid {{{' })
|
|
.expect(400);
|
|
});
|
|
|
|
it('returns 404 when session does not exist', async () => {
|
|
await request(app.getHttpServer())
|
|
.post('/exec')
|
|
.send({ sessionName: 'no-such-session', code: 'return 1;' })
|
|
.expect(404);
|
|
});
|
|
});
|
|
});
|