- add POST /import, an import page and the import_yaml mcp tool; items are validated up front and imported in dependency order - switch list page import buttons to the central import - write scenario exports as separate --- yaml documents per entity - give each test app its own files dir to avoid cross-suite races - use node: prefix for node built-in imports - bump version to 1.18.0
125 lines
4.8 KiB
TypeScript
125 lines
4.8 KiB
TypeScript
import { INestApplication, ValidationPipe } from "@nestjs/common";
|
|
import * as fs from "node:fs";
|
|
import * as os from "node:os";
|
|
import * as path from "node:path";
|
|
import { Test, TestingModule } from "@nestjs/testing";
|
|
import { TypeOrmModule } from "@nestjs/typeorm";
|
|
import { ConfigModule } from "@nestjs/config";
|
|
import type { NestExpressApplication } from "@nestjs/platform-express";
|
|
import { ScheduleModule } from "@nestjs/schedule";
|
|
|
|
import { configureBodyParsers } from "../src/common/body-parsers";
|
|
import { ImportModule } from "../src/import/import.module";
|
|
|
|
jest.mock("@nestjs/common", () => {
|
|
const actual = jest.requireActual("@nestjs/common");
|
|
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
const log = require("debug")("test");
|
|
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
const { Logger } = require("@nestjs/common/services/logger.service");
|
|
|
|
Logger.prototype.error = function (
|
|
message: unknown,
|
|
stack?: string,
|
|
context?: string,
|
|
) {
|
|
const ctx = context ?? this.context ?? "App";
|
|
log(`[${ctx}]`, "error", message, ...(stack ? [stack] : []));
|
|
};
|
|
|
|
for (const level of ["log", "warn", "debug", "verbose", "fatal"] as const) {
|
|
Logger.prototype[level] = function (message: unknown, context?: string) {
|
|
const ctx = context ?? this.context ?? "App";
|
|
log(`[${ctx}]`, level, message);
|
|
};
|
|
}
|
|
|
|
return actual;
|
|
});
|
|
|
|
import { BrowserModule } from "../src/browser/browser.module";
|
|
import { SessionModule } from "../src/session/session.module";
|
|
import { EnvironmentModule } from "../src/environment/environment.module";
|
|
import { ScenarioModule } from "../src/scenario/scenario.module";
|
|
import { McpModule } from "../src/mcp/mcp.module";
|
|
import { SessionEntity } from "../src/session/session.entity";
|
|
import { EnvironmentEntity } from "../src/environment/environment.entity";
|
|
import { ScenarioEntity } from "../src/scenario/scenario.entity";
|
|
import { ScenarioStepEntity } from "../src/scenario/scenario-step.entity";
|
|
import { ScenarioVersionEntity } from "../src/scenario/scenario-version.entity";
|
|
import { SnippetVersionEntity } from "../src/snippet/snippet-version.entity";
|
|
import { ScenarioRunEntity } from "../src/scenario/scenario-run.entity";
|
|
import { ScenarioRunStepEntity } from "../src/scenario/scenario-run-step.entity";
|
|
import { ScenarioRunLogEntity } from "../src/scenario/scenario-run-log.entity";
|
|
import { ScenarioCredentialEntity } from "../src/scenario/scenario-credential.entity";
|
|
import { CredentialEntity } from "../src/credential/credential.entity";
|
|
import { SnippetEntity } from "../src/snippet/snippet.entity";
|
|
import { HealthController } from "../src/health/health.controller";
|
|
import { HttpExceptionFilter } from "../src/filters/http-exception.filter";
|
|
import { LoggingInterceptor } from "../src/interceptors/logging.interceptor";
|
|
import { validateAppConfig } from "../src/config/app.config";
|
|
import { FileEntity } from "../src/file/file.entity";
|
|
import { ScenarioFileEntity } from "../src/file/scenario-file.entity";
|
|
import { ScenarioRunFileEntity } from "../src/file/scenario-run-file.entity";
|
|
|
|
export async function buildTestApp(): Promise<INestApplication> {
|
|
// Each test app stores files in its own temp dir: suites run in parallel and
|
|
// some clean up their files dir, which must not affect other suites.
|
|
const filesDir = fs.mkdtempSync(path.join(os.tmpdir(), "liqa-test-files-"));
|
|
process.env.FILES_DIR = filesDir;
|
|
|
|
const module: TestingModule = await Test.createTestingModule({
|
|
imports: [
|
|
ConfigModule.forRoot({
|
|
isGlobal: true,
|
|
ignoreEnvFile: true,
|
|
validate: validateAppConfig,
|
|
}),
|
|
TypeOrmModule.forRoot({
|
|
type: "better-sqlite3",
|
|
database: ":memory:",
|
|
entities: [
|
|
SessionEntity,
|
|
EnvironmentEntity,
|
|
ScenarioEntity,
|
|
ScenarioStepEntity,
|
|
ScenarioVersionEntity,
|
|
ScenarioRunEntity,
|
|
ScenarioRunStepEntity,
|
|
ScenarioRunLogEntity,
|
|
ScenarioCredentialEntity,
|
|
CredentialEntity,
|
|
SnippetEntity,
|
|
SnippetVersionEntity,
|
|
FileEntity,
|
|
ScenarioFileEntity,
|
|
ScenarioRunFileEntity,
|
|
],
|
|
synchronize: true,
|
|
}),
|
|
ScheduleModule.forRoot(),
|
|
BrowserModule,
|
|
SessionModule,
|
|
EnvironmentModule,
|
|
ScenarioModule,
|
|
ImportModule,
|
|
McpModule,
|
|
],
|
|
controllers: [HealthController],
|
|
}).compile();
|
|
|
|
const app = module.createNestApplication<NestExpressApplication>();
|
|
configureBodyParsers(app);
|
|
app.useGlobalPipes(new ValidationPipe({ transform: true, whitelist: true }));
|
|
app.useGlobalFilters(new HttpExceptionFilter());
|
|
app.useGlobalInterceptors(new LoggingInterceptor());
|
|
await app.init();
|
|
|
|
const close = app.close.bind(app);
|
|
app.close = async () => {
|
|
await close();
|
|
fs.rmSync(filesDir, { recursive: true, force: true });
|
|
};
|
|
return app;
|
|
}
|