You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
76 lines
2.9 KiB
76 lines
2.9 KiB
import 'dotenv/config'; |
|
import { readFileSync, existsSync, readdirSync } from 'node:fs'; |
|
import { dirname, join, resolve } from 'node:path'; |
|
import { fileURLToPath } from 'node:url'; |
|
import { PrismaClient } from '@reception/db'; |
|
import { runScenario } from './runner.js'; |
|
import type { Scenario } from './types.js'; |
|
|
|
const __dirname = dirname(fileURLToPath(import.meta.url)); |
|
const SCENARIOS_DIR = resolve(__dirname, '..', 'scenarios'); |
|
|
|
function parseArgs(argv: string[]): { scenario?: string; mode: 'realtime' | 'fast'; list: boolean } { |
|
let scenario: string | undefined; |
|
let mode: 'realtime' | 'fast' = 'fast'; |
|
let list = false; |
|
for (let i = 0; i < argv.length; i++) { |
|
const arg = argv[i]; |
|
if (arg === '--list') list = true; |
|
else if (arg?.startsWith('--scenario=')) scenario = arg.slice('--scenario='.length); |
|
else if (arg === '--scenario') scenario = argv[++i]; |
|
else if (arg?.startsWith('--mode=')) mode = arg.slice('--mode='.length) as 'realtime' | 'fast'; |
|
else if (arg === '--mode') mode = argv[++i] as 'realtime' | 'fast'; |
|
} |
|
return { scenario, mode, list }; |
|
} |
|
|
|
function loadScenario(name: string): Scenario { |
|
const path = join(SCENARIOS_DIR, `${name}.json`); |
|
if (!existsSync(path)) { |
|
throw new Error(`Сценарий "${name}" не найден в ${SCENARIOS_DIR}`); |
|
} |
|
return JSON.parse(readFileSync(path, 'utf-8')) as Scenario; |
|
} |
|
|
|
async function main() { |
|
const { scenario, mode, list } = parseArgs(process.argv.slice(2)); |
|
|
|
if (list || !scenario) { |
|
const files = readdirSync(SCENARIOS_DIR).filter((f) => f.endsWith('.json')); |
|
console.log('Доступные сценарии:'); |
|
for (const f of files) { |
|
const s = JSON.parse(readFileSync(join(SCENARIOS_DIR, f), 'utf-8')) as Scenario; |
|
console.log(` ${s.name.padEnd(24)} — ${s.description}`); |
|
} |
|
if (!scenario) { |
|
console.log('\nИспользование: pnpm fixtures:run --scenario=new-patient [--mode=realtime|fast]'); |
|
// --list — нормальный код выхода 0. Если просто отсутствует --scenario, тоже 0. |
|
return; |
|
} |
|
if (list) return; // --list + --scenario — показать список и продолжить выполнение? нет, выйти. |
|
} |
|
|
|
const apiBaseUrl = process.env.API_BASE_URL ?? 'http://localhost:4000'; |
|
const faceServiceUrl = process.env.FACE_SERVICE_URL ?? 'http://localhost:8001'; |
|
|
|
console.log(`Загружаю сценарий ${scenario}, mode=${mode}, api=${apiBaseUrl}, face=${faceServiceUrl}`); |
|
const s = loadScenario(scenario); |
|
const prisma = new PrismaClient(); |
|
try { |
|
const result = await runScenario({ |
|
scenario: s, |
|
apiBaseUrl, |
|
faceServiceUrl, |
|
prisma, |
|
realtime: mode === 'realtime', |
|
}); |
|
console.log('\nИтог:', JSON.stringify(result, null, 2)); |
|
} finally { |
|
await prisma.$disconnect(); |
|
} |
|
} |
|
|
|
main().catch((err) => { |
|
console.error(err); |
|
process.exit(1); |
|
});
|
|
|