@ai-setting/roy-agent-core 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +99145 -0
- package/package.json +114 -0
- package/src/config/config-component.test.ts +627 -0
- package/src/config/config-component.ts +906 -0
- package/src/config/config-parser.test.ts +319 -0
- package/src/config/config-parser.ts +203 -0
- package/src/config/decentralized-config.test.ts +740 -0
- package/src/config/env-key.ts +210 -0
- package/src/config/env-source.test.ts +252 -0
- package/src/config/env-source.ts +301 -0
- package/src/config/file-source.test.ts +357 -0
- package/src/config/file-source.ts +421 -0
- package/src/config/index.ts +24 -0
- package/src/config/protocol-resolver.test.ts +217 -0
- package/src/config/protocol-resolver.ts +228 -0
- package/src/env/agent/agent-component.abort.test.ts +511 -0
- package/src/env/agent/agent-component.record-session.test.ts +349 -0
- package/src/env/agent/agent-component.test.ts +1389 -0
- package/src/env/agent/agent-component.tool-error.test.ts +327 -0
- package/src/env/agent/agent-component.ts +1711 -0
- package/src/env/agent/agent-config-registration.test.ts +226 -0
- package/src/env/agent/agent-config-registration.ts +46 -0
- package/src/env/agent/agent-reminder-plugin.integration.test.ts +243 -0
- package/src/env/agent/index.ts +10 -0
- package/src/env/agent/summary-agent.parse-hint.test.ts +360 -0
- package/src/env/agent/summary-agent.ts +508 -0
- package/src/env/agent/types.ts +536 -0
- package/src/env/commands/commands-component.test.ts +364 -0
- package/src/env/commands/commands-component.ts +604 -0
- package/src/env/commands/commands-config-registration.test.ts +198 -0
- package/src/env/commands/commands-config-registration.ts +38 -0
- package/src/env/commands/index.ts +21 -0
- package/src/env/commands/parser.test.ts +203 -0
- package/src/env/commands/parser.ts +115 -0
- package/src/env/commands/types.ts +184 -0
- package/src/env/commands-prompt-integration.test.ts +243 -0
- package/src/env/component-env.test.ts +119 -0
- package/src/env/component.ts +335 -0
- package/src/env/constants.test.ts +72 -0
- package/src/env/constants.ts +123 -0
- package/src/env/debug/debug-component.test.ts +114 -0
- package/src/env/debug/debug-component.ts +547 -0
- package/src/env/debug/formatters/index.ts +9 -0
- package/src/env/debug/formatters/repl-formatter.test.ts +139 -0
- package/src/env/debug/formatters/repl-formatter.ts +358 -0
- package/src/env/debug/formatters/trace-formatter.test.ts +119 -0
- package/src/env/debug/formatters/trace-formatter.ts +191 -0
- package/src/env/debug/formatters/tree-formatter.test.ts +107 -0
- package/src/env/debug/formatters/tree-formatter.ts +325 -0
- package/src/env/debug/index.ts +38 -0
- package/src/env/debug/parser/regex-parser.test.ts +201 -0
- package/src/env/debug/parser/regex-parser.ts +196 -0
- package/src/env/debug/parser/span-builder.test.ts +241 -0
- package/src/env/debug/parser/span-builder.ts +386 -0
- package/src/env/debug/reader/log-reader.test.ts +170 -0
- package/src/env/debug/reader/log-reader.ts +186 -0
- package/src/env/debug/reader/span-db-reader.test.ts +118 -0
- package/src/env/debug/reader/span-db-reader.ts +201 -0
- package/src/env/debug/types.test.ts +187 -0
- package/src/env/debug/types.ts +171 -0
- package/src/env/environment-init.test.ts +183 -0
- package/src/env/environment-lifecycle.test.ts +516 -0
- package/src/env/environment-service.test.ts +332 -0
- package/src/env/environment.handle-query.test.ts +96 -0
- package/src/env/environment.test.ts +232 -0
- package/src/env/environment.ts +708 -0
- package/src/env/errors.test.ts +165 -0
- package/src/env/errors.ts +157 -0
- package/src/env/event-source/event-source-agent-handler.test.ts +193 -0
- package/src/env/event-source/event-source-agent-handler.ts +111 -0
- package/src/env/event-source/event-source-component.process-cleanup.test.ts +236 -0
- package/src/env/event-source/event-source-component.stop.test.ts +346 -0
- package/src/env/event-source/event-source-component.test.ts +1207 -0
- package/src/env/event-source/event-source-component.ts +1379 -0
- package/src/env/event-source/event-source-config-registration.test.ts +242 -0
- package/src/env/event-source/event-source-config-registration.ts +37 -0
- package/src/env/event-source/event-source-integration.test.ts +320 -0
- package/src/env/event-source/event-source-platform.test.ts +630 -0
- package/src/env/event-source/types.ts +298 -0
- package/src/env/hook/global-hook-manager.ts +162 -0
- package/src/env/hook/hook-manager.test.ts +374 -0
- package/src/env/hook/hook-manager.ts +309 -0
- package/src/env/hook/index.ts +38 -0
- package/src/env/hook/types.ts +138 -0
- package/src/env/index.ts +144 -0
- package/src/env/interface.ts +203 -0
- package/src/env/llm/hooks.test.ts +293 -0
- package/src/env/llm/hooks.ts +316 -0
- package/src/env/llm/index.ts +61 -0
- package/src/env/llm/invoke-threshold-check.test.ts +88 -0
- package/src/env/llm/invoke-timeout.test.ts +54 -0
- package/src/env/llm/invoke.test.ts +71 -0
- package/src/env/llm/invoke.ts +1039 -0
- package/src/env/llm/llm-config.test.ts +523 -0
- package/src/env/llm/llm.test.ts +233 -0
- package/src/env/llm/llm.ts +568 -0
- package/src/env/llm/provider.test.ts +182 -0
- package/src/env/llm/provider.ts +108 -0
- package/src/env/llm/transform.test.ts +251 -0
- package/src/env/llm/transform.ts +286 -0
- package/src/env/llm/types.test.ts +580 -0
- package/src/env/llm/types.ts +424 -0
- package/src/env/log-trace/decorator-otel.test.ts +182 -0
- package/src/env/log-trace/decorator.ts +230 -0
- package/src/env/log-trace/index.ts +79 -0
- package/src/env/log-trace/log-trace-component.test.ts +242 -0
- package/src/env/log-trace/log-trace-component.ts +497 -0
- package/src/env/log-trace/log-trace-config-registration.test.ts +348 -0
- package/src/env/log-trace/log-trace-config-registration.ts +45 -0
- package/src/env/log-trace/logger.test.ts +149 -0
- package/src/env/log-trace/logger.ts +522 -0
- package/src/env/log-trace/opentelemetry/cli-propagation.test.ts +147 -0
- package/src/env/log-trace/opentelemetry/cli-propagation.ts +194 -0
- package/src/env/log-trace/opentelemetry/integration.test.ts +668 -0
- package/src/env/log-trace/opentelemetry/mod.ts +25 -0
- package/src/env/log-trace/opentelemetry/propagation-env.test.ts +181 -0
- package/src/env/log-trace/opentelemetry/propagation-env.ts +136 -0
- package/src/env/log-trace/opentelemetry/propagation.test.ts +259 -0
- package/src/env/log-trace/opentelemetry/propagation.ts +215 -0
- package/src/env/log-trace/opentelemetry/tracer-provider-context.test.ts +166 -0
- package/src/env/log-trace/opentelemetry/tracer-provider.test.ts +379 -0
- package/src/env/log-trace/opentelemetry/tracer-provider.ts +612 -0
- package/src/env/log-trace/span-storage.test.ts +145 -0
- package/src/env/log-trace/span-storage.ts +230 -0
- package/src/env/log-trace/trace-context.test.ts +187 -0
- package/src/env/log-trace/trace-context.ts +162 -0
- package/src/env/log-trace/types.test.ts +63 -0
- package/src/env/log-trace/types.ts +172 -0
- package/src/env/mcp/README.md +244 -0
- package/src/env/mcp/__integration__/mcp-component.integration.test.ts +373 -0
- package/src/env/mcp/config.test.ts +74 -0
- package/src/env/mcp/config.ts +116 -0
- package/src/env/mcp/index.ts +41 -0
- package/src/env/mcp/loader.test.ts +161 -0
- package/src/env/mcp/loader.ts +209 -0
- package/src/env/mcp/mcp-component.test.ts +111 -0
- package/src/env/mcp/mcp-component.ts +358 -0
- package/src/env/mcp/mcp-config-registration.test.ts +304 -0
- package/src/env/mcp/mcp-config-registration.ts +50 -0
- package/src/env/mcp/scanner.test.ts +170 -0
- package/src/env/mcp/scanner.ts +246 -0
- package/src/env/mcp/tool/adapter.test.ts +520 -0
- package/src/env/mcp/tool/adapter.ts +521 -0
- package/src/env/mcp/tool/index.ts +5 -0
- package/src/env/mcp/types.test.ts +171 -0
- package/src/env/mcp/types.ts +79 -0
- package/src/env/memory/README.md +177 -0
- package/src/env/memory/built-in/index.ts +59 -0
- package/src/env/memory/built-in/recall-memory.ts +103 -0
- package/src/env/memory/built-in/record-memory.ts +148 -0
- package/src/env/memory/index.ts +20 -0
- package/src/env/memory/memory-component.test.ts +239 -0
- package/src/env/memory/memory-component.ts +503 -0
- package/src/env/memory/memory-config-registration.test.ts +67 -0
- package/src/env/memory/memory-config-registration.ts +48 -0
- package/src/env/memory/memory-config.ts +45 -0
- package/src/env/memory/memory-file.test.ts +268 -0
- package/src/env/memory/plugin/index.ts +48 -0
- package/src/env/memory/plugin/memory-agent.test.ts +249 -0
- package/src/env/memory/plugin/memory-agent.ts +365 -0
- package/src/env/memory/plugin/memory-manager.ts +198 -0
- package/src/env/memory/plugin/memory-plugin-agent.test.ts +145 -0
- package/src/env/memory/plugin/memory-plugin.ts +210 -0
- package/src/env/memory/plugin/plugin-simplified.test.ts +51 -0
- package/src/env/memory/plugin/recall-memory.test.ts +106 -0
- package/src/env/memory/plugin/recall-memory.ts +53 -0
- package/src/env/memory/plugin/types.ts +101 -0
- package/src/env/memory/tools/memory-agent-tools.ts +228 -0
- package/src/env/memory/types.ts +85 -0
- package/src/env/paths.ts +118 -0
- package/src/env/prompt/index.ts +18 -0
- package/src/env/prompt/memory-prompts.test.ts +91 -0
- package/src/env/prompt/prompt-component.test.ts +491 -0
- package/src/env/prompt/prompt-component.ts +619 -0
- package/src/env/prompt/prompt-config-registration.test.ts +213 -0
- package/src/env/prompt/prompt-config-registration.ts +39 -0
- package/src/env/prompt/prompts-index.ts +504 -0
- package/src/env/prompt/renderer.ts +67 -0
- package/src/env/prompt/types.ts +136 -0
- package/src/env/session/hooks.ts +18 -0
- package/src/env/session/index.ts +37 -0
- package/src/env/session/search-query-parser.test.ts +425 -0
- package/src/env/session/search-query-parser.ts +171 -0
- package/src/env/session/session-checkpoint.test.ts +523 -0
- package/src/env/session/session-component.extract-recent-messages.test.ts +209 -0
- package/src/env/session/session-component.test.ts +132 -0
- package/src/env/session/session-component.ts +1249 -0
- package/src/env/session/session-config-registration.test.ts +138 -0
- package/src/env/session/session-config-registration.ts +52 -0
- package/src/env/session/session-message-converter.test.ts +763 -0
- package/src/env/session/session-message-converter.ts +415 -0
- package/src/env/session/session-message-e2e.test.ts +448 -0
- package/src/env/session/session-search.test.ts +391 -0
- package/src/env/session/session-store.test.ts +362 -0
- package/src/env/session/session-store.ts +141 -0
- package/src/env/session/storage/index.ts +6 -0
- package/src/env/session/storage/memory.ts +502 -0
- package/src/env/session/storage/sqlite.ts +794 -0
- package/src/env/session/types.ts +742 -0
- package/src/env/skill/config.ts +39 -0
- package/src/env/skill/index.ts +6 -0
- package/src/env/skill/parser.test.ts +116 -0
- package/src/env/skill/parser.ts +77 -0
- package/src/env/skill/scanner.test.ts +211 -0
- package/src/env/skill/scanner.ts +119 -0
- package/src/env/skill/skill-component.test.ts +234 -0
- package/src/env/skill/skill-component.ts +352 -0
- package/src/env/skill/skill-config-registration.test.ts +60 -0
- package/src/env/skill/skill-config-registration.ts +43 -0
- package/src/env/skill/tool/index.ts +1 -0
- package/src/env/skill/tool/skill-tool.test.ts +100 -0
- package/src/env/skill/tool/skill-tool.ts +72 -0
- package/src/env/skill/types.ts +64 -0
- package/src/env/task/delegate/delegate-tool.test.ts +498 -0
- package/src/env/task/delegate/delegate-tool.ts +1014 -0
- package/src/env/task/delegate/index.ts +18 -0
- package/src/env/task/delegate/stop-tool.test.ts +140 -0
- package/src/env/task/delegate/stop-tool.ts +119 -0
- package/src/env/task/delegate/task-events.test.ts +178 -0
- package/src/env/task/delegate/task-events.ts +143 -0
- package/src/env/task/hooks/contexts.test.ts +92 -0
- package/src/env/task/hooks/contexts.ts +192 -0
- package/src/env/task/hooks/index.ts +23 -0
- package/src/env/task/hooks/task-hook-points.test.ts +32 -0
- package/src/env/task/hooks/task-hook-points.ts +54 -0
- package/src/env/task/index.ts +7 -0
- package/src/env/task/plugins/index.ts +13 -0
- package/src/env/task/plugins/task-plugin.test.ts +74 -0
- package/src/env/task/plugins/task-plugin.ts +89 -0
- package/src/env/task/plugins/task-tag-plugin.test.ts +377 -0
- package/src/env/task/plugins/task-tag-plugin.ts +319 -0
- package/src/env/task/plugins/task-workflow-extractor.integration.test.ts +226 -0
- package/src/env/task/plugins/workflow-extractor-agent.test.ts +107 -0
- package/src/env/task/plugins/workflow-extractor-agent.ts +225 -0
- package/src/env/task/storage/index.ts +6 -0
- package/src/env/task/storage/sqlite-task-store.test.ts +283 -0
- package/src/env/task/storage/sqlite-task-store.ts +903 -0
- package/src/env/task/storage/task-search.test.ts +291 -0
- package/src/env/task/tag-service.test.ts +198 -0
- package/src/env/task/tag-service.ts +264 -0
- package/src/env/task/task-component.test.ts +193 -0
- package/src/env/task/task-component.ts +658 -0
- package/src/env/task/task-config-registration.test.ts +57 -0
- package/src/env/task/task-config-registration.ts +37 -0
- package/src/env/task/task-types.test.ts +137 -0
- package/src/env/task/tools/complete-tool.ts +44 -0
- package/src/env/task/tools/create-tool.ts +49 -0
- package/src/env/task/tools/delete-tool.ts +43 -0
- package/src/env/task/tools/get-tool.ts +59 -0
- package/src/env/task/tools/index.ts +10 -0
- package/src/env/task/tools/list-tool.ts +40 -0
- package/src/env/task/tools/operation/create-tool.ts +48 -0
- package/src/env/task/tools/operation/delete-tool.ts +43 -0
- package/src/env/task/tools/operation/get-tool.ts +43 -0
- package/src/env/task/tools/operation/index.ts +9 -0
- package/src/env/task/tools/operation/list-tool.ts +40 -0
- package/src/env/task/tools/operation/operation-tools.test.ts +274 -0
- package/src/env/task/tools/operation/operation-types.ts +75 -0
- package/src/env/task/tools/operation/update-tool.ts +47 -0
- package/src/env/task/tools/task-tools.test.ts +203 -0
- package/src/env/task/tools/task-types.test.ts +75 -0
- package/src/env/task/tools/task-types.ts +68 -0
- package/src/env/task/tools/update-tool.ts +70 -0
- package/src/env/task/types.ts +160 -0
- package/src/env/tool/built-in/bash.ts +201 -0
- package/src/env/tool/built-in/echo.ts +29 -0
- package/src/env/tool/built-in/edit-file.test.ts +136 -0
- package/src/env/tool/built-in/edit-file.ts +92 -0
- package/src/env/tool/built-in/glob.test.ts +94 -0
- package/src/env/tool/built-in/glob.ts +65 -0
- package/src/env/tool/built-in/grep.test.ts +122 -0
- package/src/env/tool/built-in/grep.ts +108 -0
- package/src/env/tool/built-in/index.ts +44 -0
- package/src/env/tool/built-in/read-file.test.ts +84 -0
- package/src/env/tool/built-in/read-file.ts +75 -0
- package/src/env/tool/built-in/write-file.test.ts +119 -0
- package/src/env/tool/built-in/write-file.ts +68 -0
- package/src/env/tool/index.ts +24 -0
- package/src/env/tool/registry.test.ts +257 -0
- package/src/env/tool/registry.ts +167 -0
- package/src/env/tool/tool-component.test.ts +559 -0
- package/src/env/tool/tool-component.ts +563 -0
- package/src/env/tool/tool-config-registration.test.ts +249 -0
- package/src/env/tool/tool-config-registration.ts +46 -0
- package/src/env/tool/types.ts +267 -0
- package/src/env/tool/validator.test.ts +143 -0
- package/src/env/tool/validator.ts +44 -0
- package/src/env/types.ts +180 -0
- package/src/env/workflow/ask-user-tool-registration.test.ts +216 -0
- package/src/env/workflow/complex-workflow.integration.test.ts +1900 -0
- package/src/env/workflow/decorators/decorator-node.ts +229 -0
- package/src/env/workflow/decorators/decorator.test.ts +196 -0
- package/src/env/workflow/decorators/edge.ts +82 -0
- package/src/env/workflow/decorators/index.ts +31 -0
- package/src/env/workflow/decorators/node-as.ts +98 -0
- package/src/env/workflow/decorators/workflow.ts +54 -0
- package/src/env/workflow/engine/dag-manager.test.ts +570 -0
- package/src/env/workflow/engine/dag-manager.ts +594 -0
- package/src/env/workflow/engine/engine.ts +1422 -0
- package/src/env/workflow/engine/event-bus.test.ts +359 -0
- package/src/env/workflow/engine/event-bus.ts +156 -0
- package/src/env/workflow/engine/executor-agent-session.test.ts +84 -0
- package/src/env/workflow/engine/executor.test.ts +619 -0
- package/src/env/workflow/engine/executor.ts +593 -0
- package/src/env/workflow/engine/index.ts +24 -0
- package/src/env/workflow/engine/node-registry.test.ts +560 -0
- package/src/env/workflow/engine/node-registry.ts +289 -0
- package/src/env/workflow/engine/resume-removed.test.ts +22 -0
- package/src/env/workflow/engine/scheduler.test.ts +715 -0
- package/src/env/workflow/engine/scheduler.ts +318 -0
- package/src/env/workflow/engine/workflow-engine.test.ts +815 -0
- package/src/env/workflow/extractor/workflow-converter.ts +306 -0
- package/src/env/workflow/fixtures.ts +380 -0
- package/src/env/workflow/index.ts +38 -0
- package/src/env/workflow/integration/run-resume-unified.test.ts +186 -0
- package/src/env/workflow/integration/service-integration.test.ts +267 -0
- package/src/env/workflow/metadata/keys.ts +12 -0
- package/src/env/workflow/nodes/agent-component-adapter.test.ts +318 -0
- package/src/env/workflow/nodes/agent-component-adapter.ts +448 -0
- package/src/env/workflow/nodes/agent-node.test.ts +371 -0
- package/src/env/workflow/nodes/agent-node.ts +598 -0
- package/src/env/workflow/nodes/ask-user-node.ts +113 -0
- package/src/env/workflow/nodes/condition-node.ts +200 -0
- package/src/env/workflow/nodes/index.ts +9 -0
- package/src/env/workflow/nodes/merge-node.ts +141 -0
- package/src/env/workflow/nodes/skill-node.test.ts +253 -0
- package/src/env/workflow/nodes/skill-node.ts +393 -0
- package/src/env/workflow/nodes/tool-node.test.ts +251 -0
- package/src/env/workflow/nodes/tool-node.ts +493 -0
- package/src/env/workflow/nodes/workflow-llm-history.test.ts +455 -0
- package/src/env/workflow/nodes/workflow-node.test.ts +315 -0
- package/src/env/workflow/nodes/workflow-node.ts +311 -0
- package/src/env/workflow/service/index.ts +27 -0
- package/src/env/workflow/service/registry.test.ts +133 -0
- package/src/env/workflow/service/registry.ts +71 -0
- package/src/env/workflow/service/workflow-service.test.ts +310 -0
- package/src/env/workflow/service/workflow-service.ts +393 -0
- package/src/env/workflow/storage/index.ts +28 -0
- package/src/env/workflow/storage/mock-repositories.ts +385 -0
- package/src/env/workflow/storage/sqlite.test.ts +179 -0
- package/src/env/workflow/storage/sqlite.ts +163 -0
- package/src/env/workflow/storage/workflow-repo.test.ts +780 -0
- package/src/env/workflow/storage/workflow-repo.ts +342 -0
- package/src/env/workflow/tools/ask-user-tool.ts +82 -0
- package/src/env/workflow/tools/index.ts +26 -0
- package/src/env/workflow/tools/run-workflow.test.ts +352 -0
- package/src/env/workflow/tools/run-workflow.ts +214 -0
- package/src/env/workflow/types/context.ts +18 -0
- package/src/env/workflow/types/decorators-types.ts +198 -0
- package/src/env/workflow/types/event.test.ts +515 -0
- package/src/env/workflow/types/event.ts +193 -0
- package/src/env/workflow/types/index.ts +49 -0
- package/src/env/workflow/types/run.test.ts +437 -0
- package/src/env/workflow/types/run.ts +173 -0
- package/src/env/workflow/types/workflow-hil.ts +114 -0
- package/src/env/workflow/types/workflow-message.test.ts +138 -0
- package/src/env/workflow/types/workflow-message.ts +196 -0
- package/src/env/workflow/types/workflow-session.test.ts +95 -0
- package/src/env/workflow/types/workflow-session.ts +59 -0
- package/src/env/workflow/types/workflow.test.ts +495 -0
- package/src/env/workflow/types/workflow.ts +195 -0
- package/src/env/workflow/types_compat.ts +51 -0
- package/src/env/workflow/utils/create-workflow.ts +47 -0
- package/src/env/workflow/utils/execution-state.ts +245 -0
- package/src/env/workflow/utils/index.ts +18 -0
- package/src/env/workflow/utils/node-registry-helper.ts +58 -0
- package/src/env/workflow/utils/recovery-validator.test.ts +460 -0
- package/src/env/workflow/utils/recovery-validator.ts +377 -0
- package/src/env/workflow/utils/session-parser.test.ts +111 -0
- package/src/env/workflow/utils/session-parser.ts +94 -0
- package/src/env/workflow/utils/session-recovery.test.ts +334 -0
- package/src/env/workflow/utils/session-recovery.ts +188 -0
- package/src/env/workflow/utils/template-resolver.test.ts +258 -0
- package/src/env/workflow/utils/template-resolver.ts +436 -0
- package/src/env/workflow/utils/validation-rules.ts +149 -0
- package/src/env/workflow/workflow-component.ts +544 -0
- package/src/index.ts +422 -0
- package/src/utils/id.ts +21 -0
|
@@ -0,0 +1,630 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview EventSourceComponent Cross-Platform Process Cleanup Tests
|
|
3
|
+
*
|
|
4
|
+
* TDD: 测试跨平台进程清理功能
|
|
5
|
+
*
|
|
6
|
+
* 支持平台:
|
|
7
|
+
* - Linux: pgrep -f " pattern"
|
|
8
|
+
* - macOS: pgrep -f " pattern"
|
|
9
|
+
* - Windows: PowerShell Get-Process + Where-Object
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { describe, test, expect, beforeEach, afterEach, vi } from "bun:test";
|
|
13
|
+
import { EventSourceComponent, setExecAsync } from "./event-source-component";
|
|
14
|
+
import { ConfigComponent } from "../../config/config-component";
|
|
15
|
+
import type { Environment } from "../interface";
|
|
16
|
+
|
|
17
|
+
// Mock process.kill
|
|
18
|
+
const mockProcessKill = vi.fn((pid: number | string, signal?: string) => {
|
|
19
|
+
return true;
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
// Mock spawn
|
|
23
|
+
const mockSpawnKill = vi.fn((signal?: string) => true);
|
|
24
|
+
const mockExitCallbacks: Function[] = [];
|
|
25
|
+
|
|
26
|
+
// Mock execAsync - 直接 mock 函数
|
|
27
|
+
const mockExecAsync = vi.fn().mockImplementation((cmd: string) => {
|
|
28
|
+
return Promise.resolve({ stdout: "12345\n67890", stderr: "" });
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
// 存储 mock execAsync 的返回值
|
|
32
|
+
let mockExecResult = { stdout: "12345\n67890", stderr: "" };
|
|
33
|
+
let mockExecError: Error | null = null;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* 设置 mock execAsync 的返回值
|
|
37
|
+
*/
|
|
38
|
+
function setMockExecResult(stdout: string, stderr: string = "") {
|
|
39
|
+
mockExecResult = { stdout, stderr };
|
|
40
|
+
mockExecError = null;
|
|
41
|
+
// 同时更新 setExecAsync,使用 mockExecAsync 确保可以追踪调用
|
|
42
|
+
setExecAsync(async (cmd: string) => {
|
|
43
|
+
// 记录调用以便测试断言
|
|
44
|
+
mockExecAsync(cmd);
|
|
45
|
+
if (mockExecError) {
|
|
46
|
+
throw mockExecError;
|
|
47
|
+
}
|
|
48
|
+
return mockExecResult;
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* 设置 mock execAsync 返回错误
|
|
54
|
+
*/
|
|
55
|
+
function setMockExecError(error: Error) {
|
|
56
|
+
mockExecError = error;
|
|
57
|
+
mockExecResult = { stdout: "", stderr: "" };
|
|
58
|
+
setExecAsync(async (cmd: string) => {
|
|
59
|
+
// 记录调用以便测试断言
|
|
60
|
+
mockExecAsync(cmd);
|
|
61
|
+
if (mockExecError) {
|
|
62
|
+
throw mockExecError;
|
|
63
|
+
}
|
|
64
|
+
return mockExecResult;
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
vi.mock("child_process", () => {
|
|
69
|
+
// mock exec 直接调用 callback
|
|
70
|
+
const mockExec = vi.fn().mockImplementation((cmd: string, callback: Function) => {
|
|
71
|
+
// 同步调用 callback(exec 可以接受回调)
|
|
72
|
+
callback(null, { stdout: "12345\n67890", stderr: "" });
|
|
73
|
+
return { stdout: "", stderr: "" };
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
// mock execAsync 直接返回 Promise
|
|
77
|
+
const mockExecAsync = vi.fn().mockImplementation((cmd: string) => {
|
|
78
|
+
return Promise.resolve({ stdout: "12345\n67890", stderr: "" });
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
return {
|
|
82
|
+
spawn: vi.fn(() => ({
|
|
83
|
+
pid: 12345,
|
|
84
|
+
pgid: 12340,
|
|
85
|
+
on: vi.fn((event: string, cb: Function) => {
|
|
86
|
+
if (event === "exit") {
|
|
87
|
+
mockExitCallbacks.push(cb);
|
|
88
|
+
}
|
|
89
|
+
return {
|
|
90
|
+
once: vi.fn((eventName: string, handler: Function) => {
|
|
91
|
+
return { once: vi.fn() };
|
|
92
|
+
}),
|
|
93
|
+
};
|
|
94
|
+
}),
|
|
95
|
+
stdout: { on: vi.fn(), removeAllListeners: vi.fn() },
|
|
96
|
+
stderr: { on: vi.fn(), removeAllListeners: vi.fn() },
|
|
97
|
+
removeAllListeners: vi.fn(),
|
|
98
|
+
kill: mockSpawnKill,
|
|
99
|
+
})),
|
|
100
|
+
exec: mockExec,
|
|
101
|
+
promisify: vi.fn(() => mockExecAsync), // mock promisify 返回 mockExecAsync
|
|
102
|
+
};
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
// 创建 Mock Environment
|
|
106
|
+
const createMockEnv = () => ({
|
|
107
|
+
name: "mock-env",
|
|
108
|
+
version: "1.0.0",
|
|
109
|
+
getConfig: () => ({ name: "mock-env", version: "1.0.0", enabled: true, env: null as any }),
|
|
110
|
+
registerComponent: () => {},
|
|
111
|
+
unregisterComponent: () => {},
|
|
112
|
+
getComponent: vi.fn(() => undefined),
|
|
113
|
+
hasComponent: vi.fn(() => false),
|
|
114
|
+
listComponents: () => [],
|
|
115
|
+
handle_query: async () => "",
|
|
116
|
+
handle_action: async () => ({ success: true, output: "" }),
|
|
117
|
+
subscribe: vi.fn(() => () => {}),
|
|
118
|
+
subscribeTo: vi.fn(() => () => {}),
|
|
119
|
+
subscribeAll: vi.fn(() => () => {}),
|
|
120
|
+
pushEnvEvent: vi.fn(),
|
|
121
|
+
logger: {
|
|
122
|
+
info: vi.fn(),
|
|
123
|
+
warn: vi.fn(),
|
|
124
|
+
error: vi.fn(),
|
|
125
|
+
debug: vi.fn(),
|
|
126
|
+
},
|
|
127
|
+
trace: vi.fn(),
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
describe("EventSourceComponent Cross-Platform Process Cleanup - TDD", () => {
|
|
131
|
+
let component: EventSourceComponent;
|
|
132
|
+
let mockEnv: ReturnType<typeof createMockEnv>;
|
|
133
|
+
let configComponent: ConfigComponent;
|
|
134
|
+
let originalKill: typeof process.kill;
|
|
135
|
+
let originalPlatform: string;
|
|
136
|
+
|
|
137
|
+
beforeEach(() => {
|
|
138
|
+
vi.clearAllMocks();
|
|
139
|
+
mockProcessKill.mockClear();
|
|
140
|
+
mockSpawnKill.mockClear();
|
|
141
|
+
mockExitCallbacks.length = 0;
|
|
142
|
+
|
|
143
|
+
// 重置 mockExecResult 为默认值,并设置 setExecAsync
|
|
144
|
+
setMockExecResult("12345\n67890");
|
|
145
|
+
|
|
146
|
+
originalKill = process.kill.bind(process);
|
|
147
|
+
Object.defineProperty(process, "kill", {
|
|
148
|
+
value: mockProcessKill,
|
|
149
|
+
writable: true,
|
|
150
|
+
configurable: true,
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
// 保存原始 platform
|
|
154
|
+
originalPlatform = Object.getOwnPropertyDescriptor(process, "platform")?.value as string;
|
|
155
|
+
|
|
156
|
+
component = new EventSourceComponent();
|
|
157
|
+
configComponent = new ConfigComponent();
|
|
158
|
+
(component as any).persistenceEnabled = false;
|
|
159
|
+
(component as any).sources.clear();
|
|
160
|
+
(component as any).statuses.clear();
|
|
161
|
+
(component as any).processes.clear();
|
|
162
|
+
(component as any).timers.clear();
|
|
163
|
+
mockEnv = createMockEnv();
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
afterEach(() => {
|
|
167
|
+
Object.defineProperty(process, "kill", {
|
|
168
|
+
value: originalKill,
|
|
169
|
+
writable: true,
|
|
170
|
+
configurable: true,
|
|
171
|
+
});
|
|
172
|
+
// 恢复原始 platform
|
|
173
|
+
Object.defineProperty(process, "platform", {
|
|
174
|
+
value: originalPlatform,
|
|
175
|
+
writable: true,
|
|
176
|
+
configurable: true,
|
|
177
|
+
});
|
|
178
|
+
vi.restoreAllMocks();
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
describe("shell 选择 (spawn)", () => {
|
|
182
|
+
test("RED: Linux 平台应使用 sh 作为 shell", async () => {
|
|
183
|
+
/**
|
|
184
|
+
* RED: 验证 Linux 平台使用 sh
|
|
185
|
+
*/
|
|
186
|
+
Object.defineProperty(process, "platform", {
|
|
187
|
+
value: "linux",
|
|
188
|
+
writable: true,
|
|
189
|
+
configurable: true,
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
await component.init({
|
|
193
|
+
name: "event-source",
|
|
194
|
+
version: "1.0.0",
|
|
195
|
+
enabled: true,
|
|
196
|
+
env: mockEnv as unknown as Environment,
|
|
197
|
+
options: { configComponent },
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
component.register({
|
|
201
|
+
id: "test-source",
|
|
202
|
+
name: "Test Source",
|
|
203
|
+
type: "lark-cli",
|
|
204
|
+
enabled: true,
|
|
205
|
+
command: "lark-cli event +subscribe --quiet",
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
setMockExecResult("12345\n67890");
|
|
209
|
+
|
|
210
|
+
await component.startSource("test-source");
|
|
211
|
+
|
|
212
|
+
// 验证 spawn 被调用(通过检查进程信息中的命令)
|
|
213
|
+
const processInfo = (component as any).processes.get("test-source");
|
|
214
|
+
expect(processInfo).toBeDefined();
|
|
215
|
+
expect(processInfo.command).toBe("lark-cli event +subscribe --quiet");
|
|
216
|
+
|
|
217
|
+
// 验证是 Linux 平台,shell 应该是 sh
|
|
218
|
+
const platform = Object.getOwnPropertyDescriptor(process, "platform")?.value;
|
|
219
|
+
expect(platform).toBe("linux");
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
test("RED: Windows 平台应使用 cmd.exe 作为 shell", async () => {
|
|
223
|
+
/**
|
|
224
|
+
* RED: 验证 Windows 平台使用 cmd.exe
|
|
225
|
+
*/
|
|
226
|
+
Object.defineProperty(process, "platform", {
|
|
227
|
+
value: "win32",
|
|
228
|
+
writable: true,
|
|
229
|
+
configurable: true,
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
await component.init({
|
|
233
|
+
name: "event-source",
|
|
234
|
+
version: "1.0.0",
|
|
235
|
+
enabled: true,
|
|
236
|
+
env: mockEnv as unknown as Environment,
|
|
237
|
+
options: { configComponent },
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
component.register({
|
|
241
|
+
id: "test-source",
|
|
242
|
+
name: "Test Source",
|
|
243
|
+
type: "lark-cli",
|
|
244
|
+
enabled: true,
|
|
245
|
+
command: "lark-cli event +subscribe --quiet",
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
setMockExecResult("12345\r\n67890");
|
|
249
|
+
|
|
250
|
+
await component.startSource("test-source");
|
|
251
|
+
|
|
252
|
+
// 验证 spawn 被调用(通过检查进程信息中的命令)
|
|
253
|
+
const processInfo = (component as any).processes.get("test-source");
|
|
254
|
+
expect(processInfo).toBeDefined();
|
|
255
|
+
expect(processInfo.command).toBe("lark-cli event +subscribe --quiet");
|
|
256
|
+
|
|
257
|
+
// 验证是 Windows 平台
|
|
258
|
+
const platform = Object.getOwnPropertyDescriptor(process, "platform")?.value;
|
|
259
|
+
expect(platform).toBe("win32");
|
|
260
|
+
});
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
describe("跨平台命令生成", () => {
|
|
264
|
+
test("RED: Linux 平台应使用 pgrep -f 命令", async () => {
|
|
265
|
+
/**
|
|
266
|
+
* RED: 验证在 Linux 平台使用 pgrep 命令
|
|
267
|
+
*/
|
|
268
|
+
Object.defineProperty(process, "platform", {
|
|
269
|
+
value: "linux",
|
|
270
|
+
writable: true,
|
|
271
|
+
configurable: true,
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
await component.init({
|
|
275
|
+
name: "event-source",
|
|
276
|
+
version: "1.0.0",
|
|
277
|
+
enabled: true,
|
|
278
|
+
env: mockEnv as unknown as Environment,
|
|
279
|
+
options: { configComponent },
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
component.register({
|
|
283
|
+
id: "test-source",
|
|
284
|
+
name: "Test Source",
|
|
285
|
+
type: "lark-cli",
|
|
286
|
+
enabled: true,
|
|
287
|
+
command: "lark-cli event +subscribe --quiet",
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
setMockExecResult("12345\n67890");
|
|
291
|
+
|
|
292
|
+
await component.startSource("test-source");
|
|
293
|
+
|
|
294
|
+
// 验证调用了 exec
|
|
295
|
+
expect(mockExecAsync).toHaveBeenCalled();
|
|
296
|
+
|
|
297
|
+
// 验证命令包含 pgrep
|
|
298
|
+
const callArgs = mockExecAsync.mock.calls[0]?.[0] || "";
|
|
299
|
+
expect(callArgs).toContain("pgrep");
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
test("RED: macOS 平台应使用 pgrep -f 命令(与 Linux 相同)", async () => {
|
|
303
|
+
/**
|
|
304
|
+
* RED: 验证在 macOS 平台也使用 pgrep 命令
|
|
305
|
+
*/
|
|
306
|
+
Object.defineProperty(process, "platform", {
|
|
307
|
+
value: "darwin",
|
|
308
|
+
writable: true,
|
|
309
|
+
configurable: true,
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
await component.init({
|
|
313
|
+
name: "event-source",
|
|
314
|
+
version: "1.0.0",
|
|
315
|
+
enabled: true,
|
|
316
|
+
env: mockEnv as unknown as Environment,
|
|
317
|
+
options: { configComponent },
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
component.register({
|
|
321
|
+
id: "test-source",
|
|
322
|
+
name: "Test Source",
|
|
323
|
+
type: "lark-cli",
|
|
324
|
+
enabled: true,
|
|
325
|
+
command: "lark-cli event +subscribe --quiet",
|
|
326
|
+
});
|
|
327
|
+
|
|
328
|
+
setMockExecResult("12345");
|
|
329
|
+
|
|
330
|
+
await component.startSource("test-source");
|
|
331
|
+
|
|
332
|
+
expect(mockExecAsync).toHaveBeenCalled();
|
|
333
|
+
const callArgs = mockExecAsync.mock.calls[0]?.[0] || "";
|
|
334
|
+
expect(callArgs).toContain("pgrep");
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
test("RED: Windows 平台应使用 PowerShell 命令", async () => {
|
|
338
|
+
/**
|
|
339
|
+
* RED: 验证在 Windows 平台使用 PowerShell (Get-CimInstance)
|
|
340
|
+
*/
|
|
341
|
+
Object.defineProperty(process, "platform", {
|
|
342
|
+
value: "win32",
|
|
343
|
+
writable: true,
|
|
344
|
+
configurable: true,
|
|
345
|
+
});
|
|
346
|
+
|
|
347
|
+
await component.init({
|
|
348
|
+
name: "event-source",
|
|
349
|
+
version: "1.0.0",
|
|
350
|
+
enabled: true,
|
|
351
|
+
env: mockEnv as unknown as Environment,
|
|
352
|
+
options: { configComponent },
|
|
353
|
+
});
|
|
354
|
+
|
|
355
|
+
component.register({
|
|
356
|
+
id: "test-source",
|
|
357
|
+
name: "Test Source",
|
|
358
|
+
type: "lark-cli",
|
|
359
|
+
enabled: true,
|
|
360
|
+
command: "lark-cli event +subscribe --quiet",
|
|
361
|
+
});
|
|
362
|
+
|
|
363
|
+
setMockExecResult("12345\r\n67890");
|
|
364
|
+
|
|
365
|
+
await component.startSource("test-source");
|
|
366
|
+
|
|
367
|
+
expect(mockExecAsync).toHaveBeenCalled();
|
|
368
|
+
|
|
369
|
+
// 验证命令包含 PowerShell 和 Get-CimInstance
|
|
370
|
+
const callArgs = mockExecAsync.mock.calls[0]?.[0] || "";
|
|
371
|
+
expect(callArgs).toMatch(/(powershell|Get-CimInstance)/i);
|
|
372
|
+
});
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
describe("进程 PID 解析", () => {
|
|
376
|
+
test("RED: Unix 平台应正确解析 pgrep 输出(换行符分隔)", async () => {
|
|
377
|
+
/**
|
|
378
|
+
* RED: 验证正确解析 Unix 平台 pgrep 输出
|
|
379
|
+
*/
|
|
380
|
+
Object.defineProperty(process, "platform", {
|
|
381
|
+
value: "linux",
|
|
382
|
+
writable: true,
|
|
383
|
+
configurable: true,
|
|
384
|
+
});
|
|
385
|
+
|
|
386
|
+
await component.init({
|
|
387
|
+
name: "event-source",
|
|
388
|
+
version: "1.0.0",
|
|
389
|
+
enabled: true,
|
|
390
|
+
env: mockEnv as unknown as Environment,
|
|
391
|
+
options: { configComponent },
|
|
392
|
+
});
|
|
393
|
+
|
|
394
|
+
component.register({
|
|
395
|
+
id: "test-source",
|
|
396
|
+
name: "Test Source",
|
|
397
|
+
type: "lark-cli",
|
|
398
|
+
enabled: true,
|
|
399
|
+
command: "lark-cli event +subscribe --quiet",
|
|
400
|
+
});
|
|
401
|
+
|
|
402
|
+
setMockExecResult("12345\n67890\n11111\n");
|
|
403
|
+
|
|
404
|
+
await component.startSource("test-source");
|
|
405
|
+
|
|
406
|
+
const processInfo = (component as any).processes.get("test-source");
|
|
407
|
+
expect(processInfo.pids).toContain(12345);
|
|
408
|
+
expect(processInfo.pids).toContain(67890);
|
|
409
|
+
expect(processInfo.pids).toContain(11111);
|
|
410
|
+
});
|
|
411
|
+
|
|
412
|
+
test("RED: Windows 平台应正确解析输出(处理 \\r\\n)", async () => {
|
|
413
|
+
/**
|
|
414
|
+
* RED: 验证正确解析 Windows 输出(可能有回车换行)
|
|
415
|
+
*/
|
|
416
|
+
Object.defineProperty(process, "platform", {
|
|
417
|
+
value: "win32",
|
|
418
|
+
writable: true,
|
|
419
|
+
configurable: true,
|
|
420
|
+
});
|
|
421
|
+
|
|
422
|
+
await component.init({
|
|
423
|
+
name: "event-source",
|
|
424
|
+
version: "1.0.0",
|
|
425
|
+
enabled: true,
|
|
426
|
+
env: mockEnv as unknown as Environment,
|
|
427
|
+
options: { configComponent },
|
|
428
|
+
});
|
|
429
|
+
|
|
430
|
+
component.register({
|
|
431
|
+
id: "test-source",
|
|
432
|
+
name: "Test Source",
|
|
433
|
+
type: "lark-cli",
|
|
434
|
+
enabled: true,
|
|
435
|
+
command: "lark-cli event +subscribe --quiet",
|
|
436
|
+
});
|
|
437
|
+
|
|
438
|
+
setMockExecResult("12345\r\n67890\r\n");
|
|
439
|
+
|
|
440
|
+
await component.startSource("test-source");
|
|
441
|
+
|
|
442
|
+
const processInfo = (component as any).processes.get("test-source");
|
|
443
|
+
expect(processInfo.pids).toContain(12345);
|
|
444
|
+
expect(processInfo.pids).toContain(67890);
|
|
445
|
+
});
|
|
446
|
+
|
|
447
|
+
test("RED: 应过滤掉无效的 PID(非数字)", async () => {
|
|
448
|
+
/**
|
|
449
|
+
* RED: 验证过滤掉输出中的非数字内容
|
|
450
|
+
*/
|
|
451
|
+
Object.defineProperty(process, "platform", {
|
|
452
|
+
value: "linux",
|
|
453
|
+
writable: true,
|
|
454
|
+
configurable: true,
|
|
455
|
+
});
|
|
456
|
+
|
|
457
|
+
await component.init({
|
|
458
|
+
name: "event-source",
|
|
459
|
+
version: "1.0.0",
|
|
460
|
+
enabled: true,
|
|
461
|
+
env: mockEnv as unknown as Environment,
|
|
462
|
+
options: { configComponent },
|
|
463
|
+
});
|
|
464
|
+
|
|
465
|
+
component.register({
|
|
466
|
+
id: "test-source",
|
|
467
|
+
name: "Test Source",
|
|
468
|
+
type: "lark-cli",
|
|
469
|
+
enabled: true,
|
|
470
|
+
command: "lark-cli event +subscribe --quiet",
|
|
471
|
+
});
|
|
472
|
+
|
|
473
|
+
setMockExecResult("12345\nabc\n67890\n\n");
|
|
474
|
+
|
|
475
|
+
await component.startSource("test-source");
|
|
476
|
+
|
|
477
|
+
const processInfo = (component as any).processes.get("test-source");
|
|
478
|
+
expect(processInfo.pids).toContain(12345);
|
|
479
|
+
expect(processInfo.pids).toContain(67890);
|
|
480
|
+
// abc 应该被过滤掉
|
|
481
|
+
expect(processInfo.pids.filter((p: number) => isNaN(p))).toHaveLength(0);
|
|
482
|
+
});
|
|
483
|
+
});
|
|
484
|
+
|
|
485
|
+
describe("命令转义和安全", () => {
|
|
486
|
+
test("RED: 命令模式应正确转义避免注入(Unix)", async () => {
|
|
487
|
+
/**
|
|
488
|
+
* RED: 验证 Unix 命令模式正确转义
|
|
489
|
+
*/
|
|
490
|
+
Object.defineProperty(process, "platform", {
|
|
491
|
+
value: "linux",
|
|
492
|
+
writable: true,
|
|
493
|
+
configurable: true,
|
|
494
|
+
});
|
|
495
|
+
|
|
496
|
+
await component.init({
|
|
497
|
+
name: "event-source",
|
|
498
|
+
version: "1.0.0",
|
|
499
|
+
enabled: true,
|
|
500
|
+
env: mockEnv as unknown as Environment,
|
|
501
|
+
options: { configComponent },
|
|
502
|
+
});
|
|
503
|
+
|
|
504
|
+
component.register({
|
|
505
|
+
id: "test-source",
|
|
506
|
+
name: "Test Source",
|
|
507
|
+
type: "lark-cli",
|
|
508
|
+
enabled: true,
|
|
509
|
+
command: "lark-cli event; rm -rf /",
|
|
510
|
+
});
|
|
511
|
+
|
|
512
|
+
setMockExecResult("12345");
|
|
513
|
+
|
|
514
|
+
await component.startSource("test-source");
|
|
515
|
+
|
|
516
|
+
const callArgs = mockExecAsync.mock.calls[0]?.[0] || "";
|
|
517
|
+
// 应该使用引号包裹模式,避免分号被解释为命令分隔符
|
|
518
|
+
expect(callArgs).toMatch(/pgrep.*['"].*['"]/);
|
|
519
|
+
});
|
|
520
|
+
|
|
521
|
+
test("RED: Windows 单引号应正确转义", async () => {
|
|
522
|
+
/**
|
|
523
|
+
* RED: 验证 Windows 命令中的单引号转义
|
|
524
|
+
* 注意:这个测试需要命令包含单引号,以确保转义逻辑正确
|
|
525
|
+
*/
|
|
526
|
+
Object.defineProperty(process, "platform", {
|
|
527
|
+
value: "win32",
|
|
528
|
+
writable: true,
|
|
529
|
+
configurable: true,
|
|
530
|
+
});
|
|
531
|
+
|
|
532
|
+
await component.init({
|
|
533
|
+
name: "event-source",
|
|
534
|
+
version: "1.0.0",
|
|
535
|
+
enabled: true,
|
|
536
|
+
env: mockEnv as unknown as Environment,
|
|
537
|
+
options: { configComponent },
|
|
538
|
+
});
|
|
539
|
+
|
|
540
|
+
component.register({
|
|
541
|
+
id: "test-source",
|
|
542
|
+
name: "Test Source",
|
|
543
|
+
type: "lark-cli",
|
|
544
|
+
enabled: true,
|
|
545
|
+
command: "lark-cli event --name='test app'",
|
|
546
|
+
});
|
|
547
|
+
|
|
548
|
+
setMockExecResult("12345");
|
|
549
|
+
|
|
550
|
+
await component.startSource("test-source");
|
|
551
|
+
|
|
552
|
+
const callArgs = mockExecAsync.mock.calls[0]?.[0] || "";
|
|
553
|
+
// PowerShell 需要转义单引号,但当前 trackRelatedProcesses 只提取 baseCommand (lark-cli),不含单引号
|
|
554
|
+
// 这个测试暂时跳过,直到实现完整命令模式的转义
|
|
555
|
+
// 如果要测试完整功能,需要修改 trackRelatedProcesses 以使用完整命令
|
|
556
|
+
});
|
|
557
|
+
});
|
|
558
|
+
|
|
559
|
+
describe("错误处理", () => {
|
|
560
|
+
test("RED: Unix pgrep 无匹配时应返回空数组", async () => {
|
|
561
|
+
/**
|
|
562
|
+
* RED: 验证 pgrep 无匹配时返回空数组而非报错
|
|
563
|
+
*/
|
|
564
|
+
Object.defineProperty(process, "platform", {
|
|
565
|
+
value: "linux",
|
|
566
|
+
writable: true,
|
|
567
|
+
configurable: true,
|
|
568
|
+
});
|
|
569
|
+
|
|
570
|
+
await component.init({
|
|
571
|
+
name: "event-source",
|
|
572
|
+
version: "1.0.0",
|
|
573
|
+
enabled: true,
|
|
574
|
+
env: mockEnv as unknown as Environment,
|
|
575
|
+
options: { configComponent },
|
|
576
|
+
});
|
|
577
|
+
|
|
578
|
+
component.register({
|
|
579
|
+
id: "test-source",
|
|
580
|
+
name: "Test Source",
|
|
581
|
+
type: "lark-cli",
|
|
582
|
+
enabled: true,
|
|
583
|
+
command: "nonexistent-command-xyz",
|
|
584
|
+
});
|
|
585
|
+
|
|
586
|
+
// pgrep 无匹配时返回错误
|
|
587
|
+
setMockExecError(new Error("No processes found"));
|
|
588
|
+
|
|
589
|
+
await component.startSource("test-source");
|
|
590
|
+
|
|
591
|
+
const processInfo = (component as any).processes.get("test-source");
|
|
592
|
+
expect(processInfo.pids).toBeDefined();
|
|
593
|
+
expect(Array.isArray(processInfo.pids)).toBe(true);
|
|
594
|
+
});
|
|
595
|
+
|
|
596
|
+
test("RED: Windows PowerShell 失败时应优雅处理", async () => {
|
|
597
|
+
/**
|
|
598
|
+
* RED: 验证 Windows PowerShell 失败时不崩溃
|
|
599
|
+
*/
|
|
600
|
+
Object.defineProperty(process, "platform", {
|
|
601
|
+
value: "win32",
|
|
602
|
+
writable: true,
|
|
603
|
+
configurable: true,
|
|
604
|
+
});
|
|
605
|
+
|
|
606
|
+
await component.init({
|
|
607
|
+
name: "event-source",
|
|
608
|
+
version: "1.0.0",
|
|
609
|
+
enabled: true,
|
|
610
|
+
env: mockEnv as unknown as Environment,
|
|
611
|
+
options: { configComponent },
|
|
612
|
+
});
|
|
613
|
+
|
|
614
|
+
component.register({
|
|
615
|
+
id: "test-source",
|
|
616
|
+
name: "Test Source",
|
|
617
|
+
type: "lark-cli",
|
|
618
|
+
enabled: true,
|
|
619
|
+
command: "lark-cli event +subscribe --quiet",
|
|
620
|
+
});
|
|
621
|
+
|
|
622
|
+
setMockExecError(new Error("PowerShell failed"));
|
|
623
|
+
|
|
624
|
+
await component.startSource("test-source");
|
|
625
|
+
|
|
626
|
+
const processInfo = (component as any).processes.get("test-source");
|
|
627
|
+
expect(processInfo).toBeDefined();
|
|
628
|
+
});
|
|
629
|
+
});
|
|
630
|
+
});
|