feat(scenario): export/import endpoints and integration tests

- add GET /scenarios/:id/export returning ScenarioExportDto
- add POST /scenarios/import creating scenario with all steps
- add ScenarioExportDto and ScenarioStepExportDto classes
- fix McpServer singleton by creating fresh instance per handle() call
- add 12 integration tests covering shape, ordering, round-trip, validation
This commit is contained in:
ars9 committed 2026-04-08 16:33:58 +03:00
1 parent e85acc8fb2
commit af45a281dd
5 files changed
+271 -10

No files matched your search

+10 -8
View File
@@ -15,8 +15,6 @@ import pkg from '../../package.json';
@Injectable()
export class McpService {
private readonly server: McpServer;
constructor(
private readonly authService: AuthService,
private readonly sessionService: SessionService,
@@ -24,13 +22,15 @@ export class McpService {
private readonly browserService: BrowserService,
private readonly codeExecutor: CodeExecutorService,
private readonly scenarioService: ScenarioService,
) {
this.server = new McpServer({ name: pkg.name, version: pkg.version });
this.registerTools();
) {}
private createServer(): McpServer {
const server = new McpServer({ name: pkg.name, version: pkg.version });
this.registerTools(server);
return server;
}
private registerTools(): void {
const server = this.server;
private registerTools(server: McpServer): void {
// ── Auth ──────────────────────────────────────────────────────────────────
@@ -454,12 +454,14 @@ export class McpService {
}
async handle(req: Request, res: Response): Promise<void> {
const server = this.createServer();
const transport = new StreamableHTTPServerTransport({ sessionIdGenerator: undefined });
await this.server.connect(transport);
await server.connect(transport);
try {
await transport.handleRequest(req, res, req.body);
} finally {
await transport.close();
await server.close();
}
}
}
+54
View File
@@ -0,0 +1,54 @@
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { Type } from 'class-transformer';
import {
IsArray,
IsIn,
IsInt,
IsNotEmpty,
IsOptional,
IsString,
Min,
ValidateNested,
} from 'class-validator';
import { StepType } from '../scenario-step.entity';
export class ScenarioStepExportDto {
@ApiProperty()
@IsInt()
@Min(0)
order: number;
@ApiProperty({ enum: ['login', 'exec', 'sign'] })
@IsIn(['login', 'exec', 'sign'])
type: StepType;
@ApiProperty()
@IsString()
@IsNotEmpty()
sessionName: string;
@ApiPropertyOptional()
@IsOptional()
@IsString()
@IsNotEmpty()
execCode: string | null;
@ApiPropertyOptional()
@IsOptional()
@IsString()
@IsNotEmpty()
validateCode: string | null;
}
export class ScenarioExportDto {
@ApiProperty()
@IsString()
@IsNotEmpty()
name: string;
@ApiProperty({ type: [ScenarioStepExportDto] })
@IsArray()
@ValidateNested({ each: true })
@Type(() => ScenarioStepExportDto)
steps: ScenarioStepExportDto[];
}
+16
View File
@@ -19,6 +19,7 @@ import { UpdateScenarioStepDto } from './dto/update-scenario-step.dto';
import { PaginationQueryDto } from './dto/pagination-query.dto';
import { ScenarioOrderBy } from './scenario.service';
import { RunsQueryDto } from './dto/runs-query.dto';
import { ScenarioExportDto } from './dto/scenario-export.dto';
@ApiTags('scenarios')
@Controller('scenarios')
@@ -34,6 +35,13 @@ export class ScenarioController {
return this.scenarioService.create(dto);
}
@Post('import')
@ApiOperation({ summary: 'Import a scenario from an export payload' })
@ApiResponse({ status: 201, description: 'Scenario imported' })
importScenario(@Body() dto: ScenarioExportDto) {
return this.scenarioService.importScenario(dto);
}
@Get()
@ApiOperation({ summary: 'List all scenarios (paginated)' })
@ApiResponse({ status: 200 })
@@ -66,6 +74,14 @@ export class ScenarioController {
return this.scenarioService.remove(id);
}
@Get(':id/export')
@ApiOperation({ summary: 'Export a scenario as a portable JSON payload' })
@ApiResponse({ status: 200 })
@ApiResponse({ status: 404, description: 'Scenario not found' })
exportScenario(@Param('id', ParseIntPipe) id: number) {
return this.scenarioService.exportScenario(id);
}
// ── Steps ─────────────────────────────────────────────────────────────────
@Post(':id/steps')
+38 -2
View File
@@ -11,6 +11,7 @@ import { CreateScenarioStepDto } from './dto/create-scenario-step.dto';
import { UpdateScenarioStepDto } from './dto/update-scenario-step.dto';
import { PaginationQueryDto, PaginatedResult } from '../common/dto/pagination.dto';
import { RunsQueryDto } from './dto/runs-query.dto';
import { ScenarioExportDto } from './dto/scenario-export.dto';
export { PaginatedResult } from '../common/dto/pagination.dto';
export type ScenarioOrderBy = 'id' | 'name' | 'createdAt' | 'updatedAt';
@@ -115,8 +116,7 @@ export class ScenarioService {
return { data, total, page, limit };
}
async createRun(scenarioId: number): Promise<ScenarioRunEntity> {
const scenario = await this.findOne(scenarioId);
async createRun(scenarioId: number): Promise<ScenarioRunEntity> { const scenario = await this.findOne(scenarioId);
const run = await this.runRepo.save(
this.runRepo.create({ scenarioId, status: 'pending' }),
@@ -140,5 +140,41 @@ export class ScenarioService {
order: { stepRuns: { order: 'ASC' } },
}) as Promise<ScenarioRunEntity>;
}
// ── Export / Import ───────────────────────────────────────────────────────
async exportScenario(id: number): Promise<ScenarioExportDto> {
const scenario = await this.findOne(id);
return {
name: scenario.name,
steps: scenario.steps.map((s) => ({
order: s.order,
type: s.type,
sessionName: s.sessionName,
execCode: s.execCode,
validateCode: s.validateCode,
})),
};
}
async importScenario(dto: ScenarioExportDto): Promise<ScenarioEntity> {
const scenario = await this.scenarioRepo.save(
this.scenarioRepo.create({ name: dto.name }),
);
if (dto.steps.length > 0) {
const steps = dto.steps.map((s) =>
this.stepRepo.create({
scenarioId: scenario.id,
order: s.order,
type: s.type,
sessionName: s.sessionName,
execCode: s.execCode ?? null,
validateCode: s.validateCode ?? null,
}),
);
await this.stepRepo.save(steps);
}
return this.findOne(scenario.id);
}
}