Add the sessions module (create/fetch session, Agent 1 conversation and confirmation endpoints, all JWT-protected) and a reusable OpenRouter client with DeepSeek/Qwen fallback + backoff on rate-limit/timeout. Agent 1 runs a Socratic dialogue to capture the 3 mandatory pillars (subject, professional goal, duration) and enforces two guardrail levels before any explicit user confirmation: level 1 is hard-coded length/format rules (objective-guardrails.ts), level 2 is an LLM coherence score across 3 dimensions (reject below 0.6). The objective is only persisted to session_objective, and the session only moves to objective_validated, once the user explicitly confirms via POST /sessions/:id/objective/confirm - never on the LLM's say-so alone. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
70 lines
2.3 KiB
TypeScript
70 lines
2.3 KiB
TypeScript
import { Test, TestingModule } from '@nestjs/testing';
|
|
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
|
|
import type { AuthenticatedRequest } from '../auth/jwt-auth.guard';
|
|
import { SessionsController } from './sessions.controller';
|
|
import { SessionsService } from './sessions.service';
|
|
|
|
describe('SessionsController', () => {
|
|
let controller: SessionsController;
|
|
const sessionsService = {
|
|
create: jest.fn(),
|
|
findOne: jest.fn(),
|
|
sendObjectiveMessage: jest.fn(),
|
|
confirmObjective: jest.fn(),
|
|
};
|
|
const request = {
|
|
user: { sub: 7, email: 'w@example.com' },
|
|
} as AuthenticatedRequest;
|
|
|
|
beforeEach(async () => {
|
|
jest.clearAllMocks();
|
|
const module: TestingModule = await Test.createTestingModule({
|
|
controllers: [SessionsController],
|
|
providers: [{ provide: SessionsService, useValue: sessionsService }],
|
|
})
|
|
.overrideGuard(JwtAuthGuard)
|
|
.useValue({ canActivate: () => true })
|
|
.compile();
|
|
|
|
controller = module.get(SessionsController);
|
|
});
|
|
|
|
it('should create a session for the authenticated user', async () => {
|
|
sessionsService.create.mockResolvedValueOnce({
|
|
id: 1,
|
|
status: 'objective_discovery',
|
|
});
|
|
const result = await controller.create(request);
|
|
expect(sessionsService.create).toHaveBeenCalledWith(7);
|
|
expect(result.id).toBe(1);
|
|
});
|
|
|
|
it('should fetch a session by id for the authenticated user', async () => {
|
|
sessionsService.findOne.mockResolvedValueOnce({
|
|
id: 1,
|
|
status: 'objective_discovery',
|
|
});
|
|
await controller.findOne(1, request);
|
|
expect(sessionsService.findOne).toHaveBeenCalledWith(1, 7);
|
|
});
|
|
|
|
it('should forward an objective message to the service', async () => {
|
|
sessionsService.sendObjectiveMessage.mockResolvedValueOnce({
|
|
reply: 'ok',
|
|
ready: false,
|
|
});
|
|
await controller.sendObjectiveMessage(1, { message: 'Bonjour' }, request);
|
|
expect(sessionsService.sendObjectiveMessage).toHaveBeenCalledWith(
|
|
1,
|
|
7,
|
|
'Bonjour',
|
|
);
|
|
});
|
|
|
|
it('should confirm the objective for the authenticated user', async () => {
|
|
sessionsService.confirmObjective.mockResolvedValueOnce({ id: 1 });
|
|
await controller.confirmObjective(1, request);
|
|
expect(sessionsService.confirmObjective).toHaveBeenCalledWith(1, 7);
|
|
});
|
|
});
|