mirror of
https://github.com/langgenius/dify.git
synced 2026-05-08 11:47:35 +08:00
Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: jyong <718720800@qq.com> Co-authored-by: Yansong Zhang <916125788@qq.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: hj24 <mambahj24@gmail.com> Co-authored-by: hj24 <huangjian@dify.ai> Co-authored-by: Joel <iamjoel007@gmail.com> Co-authored-by: Stephen Zhou <38493346+hyoban@users.noreply.github.com> Co-authored-by: CodingOnStar <hanxujiang@dify.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: 非法操作 <hjlarry@163.com> Co-authored-by: Ayush Baluni <73417844+aayushbaluni@users.noreply.github.com> Co-authored-by: yyh <92089059+lyzno1@users.noreply.github.com> Co-authored-by: jimcody1995 <jjimcody@gmail.com> Co-authored-by: James <63717587+jamesrayammons@users.noreply.github.com> Co-authored-by: Yunlu Wen <yunlu.wen@dify.ai> Co-authored-by: Stephen Zhou <hi@hyoban.cc> Co-authored-by: Coding On Star <447357187@qq.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: jerryzai <jerryzh8710@protonmail.com> Co-authored-by: NVIDIAN <speedy.hpc@hotmail.com> Co-authored-by: ai-hpc <ai-hpc@users.noreply.github.com> Co-authored-by: Asuka Minato <i@asukaminato.eu.org> Co-authored-by: Junghwan <70629228+shaun0927@users.noreply.github.com> Co-authored-by: HeYinKazune <70251095+HeYin-OS@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: yyh <yuanyouhuilyz@gmail.com> Co-authored-by: Jingyi <jingyi.qi@dify.ai> Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com> Co-authored-by: sxxtony <166789813+sxxtony@users.noreply.github.com>
154 lines
3.7 KiB
TypeScript
154 lines
3.7 KiB
TypeScript
import { mkdir, rm } from 'node:fs/promises'
|
|
import path from 'node:path'
|
|
import { startLoggedProcess, stopManagedProcess, waitForUrl } from '../support/process'
|
|
import { startWebServer, stopWebServer } from '../support/web-server'
|
|
import { apiURL, baseURL, reuseExistingWebServer } from '../test-env'
|
|
import { e2eDir, isMainModule, runCommand } from './common'
|
|
import { resetState, startMiddleware, stopMiddleware } from './setup'
|
|
|
|
type RunOptions = {
|
|
forwardArgs: string[]
|
|
full: boolean
|
|
headed: boolean
|
|
}
|
|
|
|
const parseArgs = (argv: string[]): RunOptions => {
|
|
let full = false
|
|
let headed = false
|
|
const forwardArgs: string[] = []
|
|
|
|
for (const [index, arg] of argv.entries()) {
|
|
if (arg === '--') {
|
|
forwardArgs.push(...argv.slice(index + 1))
|
|
return { forwardArgs, full, headed }
|
|
}
|
|
|
|
if (arg === '--full') {
|
|
full = true
|
|
continue
|
|
}
|
|
|
|
if (arg === '--headed') {
|
|
headed = true
|
|
continue
|
|
}
|
|
|
|
forwardArgs.push(arg)
|
|
}
|
|
|
|
return { forwardArgs, full, headed }
|
|
}
|
|
|
|
const hasCustomTags = (forwardArgs: string[]) =>
|
|
forwardArgs.some(arg => arg === '--tags' || arg.startsWith('--tags='))
|
|
|
|
const main = async () => {
|
|
const { forwardArgs, full, headed } = parseArgs(process.argv.slice(2))
|
|
const startMiddlewareForRun = full
|
|
const resetStateForRun = full
|
|
|
|
if (resetStateForRun)
|
|
await resetState()
|
|
|
|
if (startMiddlewareForRun)
|
|
await startMiddleware()
|
|
|
|
const cucumberReportDir = path.join(e2eDir, 'cucumber-report')
|
|
const logDir = path.join(e2eDir, '.logs')
|
|
|
|
await rm(cucumberReportDir, { force: true, recursive: true })
|
|
await mkdir(logDir, { recursive: true })
|
|
|
|
const apiProcess = await startLoggedProcess({
|
|
command: 'npx',
|
|
args: ['tsx', './scripts/setup.ts', 'api'],
|
|
cwd: e2eDir,
|
|
label: 'api server',
|
|
logFilePath: path.join(logDir, 'cucumber-api.log'),
|
|
})
|
|
|
|
let cleanupPromise: Promise<void> | undefined
|
|
const cleanup = async () => {
|
|
if (!cleanupPromise) {
|
|
cleanupPromise = (async () => {
|
|
await stopWebServer()
|
|
await stopManagedProcess(apiProcess)
|
|
|
|
if (startMiddlewareForRun) {
|
|
try {
|
|
await stopMiddleware()
|
|
}
|
|
catch {
|
|
// Cleanup should continue even if middleware shutdown fails.
|
|
}
|
|
}
|
|
})()
|
|
}
|
|
|
|
await cleanupPromise
|
|
}
|
|
|
|
const onTerminate = () => {
|
|
void cleanup().finally(() => {
|
|
process.exit(1)
|
|
})
|
|
}
|
|
|
|
process.once('SIGINT', onTerminate)
|
|
process.once('SIGTERM', onTerminate)
|
|
|
|
try {
|
|
try {
|
|
await waitForUrl(`${apiURL}/health`, 180_000, 1_000)
|
|
}
|
|
catch {
|
|
throw new Error(`API did not become ready at ${apiURL}/health.`)
|
|
}
|
|
|
|
await startWebServer({
|
|
baseURL,
|
|
command: 'npx',
|
|
args: ['tsx', './scripts/setup.ts', 'web'],
|
|
cwd: e2eDir,
|
|
logFilePath: path.join(logDir, 'cucumber-web.log'),
|
|
reuseExistingServer: reuseExistingWebServer,
|
|
timeoutMs: 300_000,
|
|
})
|
|
|
|
const cucumberEnv: NodeJS.ProcessEnv = {
|
|
...process.env,
|
|
CUCUMBER_HEADLESS: headed ? '0' : '1',
|
|
}
|
|
|
|
if (startMiddlewareForRun && !hasCustomTags(forwardArgs))
|
|
cucumberEnv.E2E_CUCUMBER_TAGS = 'not @skip'
|
|
|
|
const result = await runCommand({
|
|
command: 'npx',
|
|
args: [
|
|
'tsx',
|
|
'./node_modules/@cucumber/cucumber/bin/cucumber.js',
|
|
'--config',
|
|
'./cucumber.config.ts',
|
|
...forwardArgs,
|
|
],
|
|
cwd: e2eDir,
|
|
env: cucumberEnv,
|
|
})
|
|
|
|
process.exitCode = result.exitCode
|
|
}
|
|
finally {
|
|
process.off('SIGINT', onTerminate)
|
|
process.off('SIGTERM', onTerminate)
|
|
await cleanup()
|
|
}
|
|
}
|
|
|
|
if (isMainModule(import.meta.url)) {
|
|
void main().catch((error) => {
|
|
console.error(error instanceof Error ? error.message : String(error))
|
|
process.exit(1)
|
|
})
|
|
}
|