helpers.ts
TypeScript · 36 рядків
1// Тести на PostgreSQL: кожен файл — своя база flow_test_<name> у контейнері wms-flow-pg (docker compose up -d).
2// Перестворюється перед файлом; SQLite не імпортується (SQLITE_PATH → неіснуючий файл), тож база стартує з демо.
3import { startServer, type StartOptions } from '../server/index.ts'
4
5export async function testServer(name: string, options: StartOptions = {}) {
6 process.env.SQLITE_PATH = `.data/__no_sqlite_for_tests_${name}.sqlite`
7 const server = await startServer({ database: `flow_test_${name}`, recreate: true, port: 0, dispatch: false, ...options })
8 const call = async (method: string, path: string, body?: unknown) => {
9 const response = await fetch(server.url + path, {
10 method, headers: body === undefined ? {} : { 'content-type': 'application/json' }, body: body === undefined ? undefined : JSON.stringify(body),
11 })
12 const text = await response.text()
13 return { status: response.status, body: text ? JSON.parse(text) : null }
14 }
15 return { ...server, call, sql: server.db.sql() }
16}
17
18export async function reopen(name: string, options: StartOptions = {}) {
19 process.env.SQLITE_PATH = `.data/__no_sqlite_for_tests_${name}.sqlite`
20 return startServer({ database: `flow_test_${name}`, recreate: false, port: 0, dispatch: false, ...options })
21}
22
23/** Опублікований workflow через API (чернетка → публікація). */
24export async function publishWorkflow(call: (m: string, p: string, b?: unknown) => Promise<{ status: number; body: any }>, workflow: any) {
25 const saved = await call('POST', '/api/workflows', workflow)
26 if (saved.status !== 201) throw new Error(`save ${saved.status} ${JSON.stringify(saved.body)}`)
27 const published = await call('POST', `/api/workflows/${saved.body.id}/publish`)
28 if (published.status !== 200) throw new Error(`publish ${published.status} ${JSON.stringify(published.body)}`)
29 return published.body
30}
31
32export const node = (id: string, type: string, config: any = {}, outputs: string[] = ['next'], inputs: string[] = ['in']) =>
33 ({ id, type, name: id.toUpperCase(), x: 0, y: 0, inputs: inputs.map((p) => ({ id: p, name: p })), outputs: outputs.map((p) => ({ id: p, name: p })), config })
34export const edge = (source: string, sourcePort: string, target: string, targetPort = 'in') =>
35 ({ id: `${source}-${sourcePort}-${target}`, source, sourcePort, target, targetPort, condition: '' })
36