@zhushanwen/subagent-core 0.2.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/README.md +114 -0
- package/dist/chunk-3VOERJPJ.js +22 -0
- package/dist/chunk-A4IVZWVX.js +31 -0
- package/dist/execution/engine/engines/zcode/constants.cjs +54 -0
- package/dist/execution/engine/engines/zcode/constants.d.cts +31 -0
- package/dist/execution/engine/engines/zcode/constants.d.ts +31 -0
- package/dist/execution/engine/engines/zcode/constants.js +22 -0
- package/dist/execution/engine/engines/zcode/reader.cjs +276 -0
- package/dist/execution/engine/engines/zcode/reader.d.cts +20 -0
- package/dist/execution/engine/engines/zcode/reader.d.ts +20 -0
- package/dist/execution/engine/engines/zcode/reader.js +239 -0
- package/dist/execution/engine/paths.cjs +55 -0
- package/dist/execution/engine/paths.d.cts +8 -0
- package/dist/execution/engine/paths.d.ts +8 -0
- package/dist/execution/engine/paths.js +26 -0
- package/dist/execution/relay-env.cjs +62 -0
- package/dist/execution/relay-env.d.cts +33 -0
- package/dist/execution/relay-env.d.ts +33 -0
- package/dist/execution/relay-env.js +20 -0
- package/dist/index.cjs +1852 -0
- package/dist/index.d.cts +1184 -0
- package/dist/index.d.ts +1184 -0
- package/dist/index.js +1810 -0
- package/dist/types-BxyAidGf.d.cts +875 -0
- package/dist/types-BxyAidGf.d.ts +875 -0
- package/package.json +99 -0
- package/src/__tests__/agent-opts-resolver-schema-prompt.test.ts +142 -0
- package/src/__tests__/append-system-prompt-assembly.test.ts +242 -0
- package/src/__tests__/builtin-workflows-structure.test.ts +180 -0
- package/src/__tests__/derive-closed-display-parity.test.ts +93 -0
- package/src/__tests__/fr4-get-state-handshake.test.ts +125 -0
- package/src/__tests__/fr8-orphan-recovery.test.ts +259 -0
- package/src/__tests__/m2-append-content-probe.test.ts +73 -0
- package/src/__tests__/manifest-store.test.ts +368 -0
- package/src/__tests__/record-store-cache.test.ts +335 -0
- package/src/__tests__/record-store-index.test.ts +560 -0
- package/src/__tests__/record-store-last-line.test.ts +129 -0
- package/src/__tests__/review-fix-loop-script.test.ts +288 -0
- package/src/__tests__/review-fix-loop-utils.test.ts +2377 -0
- package/src/__tests__/robustness-low-batch1.test.ts +286 -0
- package/src/__tests__/robustness-medium-batch1.test.ts +69 -0
- package/src/__tests__/robustness-medium-batch2.test.ts +65 -0
- package/src/__tests__/robustness-medium-batch3.test.ts +30 -0
- package/src/__tests__/robustness-medium-batch4.test.ts +358 -0
- package/src/__tests__/session-runner.test.ts +69 -0
- package/src/__tests__/smoke.test.ts +9 -0
- package/src/__tests__/stream-sink.test.ts +203 -0
- package/src/core/__tests__/host-services.test.ts +144 -0
- package/src/core/__tests__/logger.test.ts +78 -0
- package/src/core/__tests__/notify-ports.test.ts +104 -0
- package/src/core/host-services.ts +97 -0
- package/src/core/logger.ts +47 -0
- package/src/core/notify-ports.ts +126 -0
- package/src/execution/__tests__/__fixtures__/notifier-golden-snapshots.json +53 -0
- package/src/execution/__tests__/__fixtures__/truncline.snapshot.json +1 -0
- package/src/execution/__tests__/agent-result-mapper.test.ts +150 -0
- package/src/execution/__tests__/alive-store.test.ts +147 -0
- package/src/execution/__tests__/ask-user-transit-e2e.test.ts +490 -0
- package/src/execution/__tests__/channel-registry-handshake.test.ts +242 -0
- package/src/execution/__tests__/chat-engine-routing.test.ts +610 -0
- package/src/execution/__tests__/chatmode-first-round-closure-spawn.test.ts +190 -0
- package/src/execution/__tests__/concurrency-pool.test.ts +250 -0
- package/src/execution/__tests__/config.test.ts +302 -0
- package/src/execution/__tests__/delivery-methods.test.ts +424 -0
- package/src/execution/__tests__/dialog-queue.test.ts +299 -0
- package/src/execution/__tests__/epipe-fallback.test.ts +241 -0
- package/src/execution/__tests__/execute-and-await-worktree.test.ts +243 -0
- package/src/execution/__tests__/execute-nesting.test.ts +323 -0
- package/src/execution/__tests__/execute-options-mapper.test.ts +200 -0
- package/src/execution/__tests__/execution-record.test.ts +1394 -0
- package/src/execution/__tests__/explicit-agent-ref-guard.test.ts +171 -0
- package/src/execution/__tests__/finalize-record.test.ts +367 -0
- package/src/execution/__tests__/finalized-marker.test.ts +104 -0
- package/src/execution/__tests__/format-schema-instruction.test.ts +169 -0
- package/src/execution/__tests__/gc-timer.test.ts +184 -0
- package/src/execution/__tests__/get-record-for-action-restart.test.ts +254 -0
- package/src/execution/__tests__/helpers/mock-extension-api.ts +30 -0
- package/src/execution/__tests__/helpers/spawn-mock.ts +242 -0
- package/src/execution/__tests__/host-mode.test.ts +87 -0
- package/src/execution/__tests__/lifecycle-manager-lock.test.ts +211 -0
- package/src/execution/__tests__/lifecycle-manager.test.ts +383 -0
- package/src/execution/__tests__/lifecycle-predicates.test.ts +116 -0
- package/src/execution/__tests__/manifest-parentid.test.ts +117 -0
- package/src/execution/__tests__/model-resolver.test.ts +465 -0
- package/src/execution/__tests__/nested-visibility-env-propagation.test.ts +287 -0
- package/src/execution/__tests__/nested-visibility.test.ts +325 -0
- package/src/execution/__tests__/output-collector.test.ts +358 -0
- package/src/execution/__tests__/path-encoding.test.ts +104 -0
- package/src/execution/__tests__/pi-invocation.test.ts +134 -0
- package/src/execution/__tests__/record-store.test.ts +1057 -0
- package/src/execution/__tests__/records-cwd-isolation.test.ts +91 -0
- package/src/execution/__tests__/recursive-visibility-baseline.test.ts +339 -0
- package/src/execution/__tests__/recursive-visibility-env.test.ts +273 -0
- package/src/execution/__tests__/relay-env.test.ts +42 -0
- package/src/execution/__tests__/resource-policy.test.ts +109 -0
- package/src/execution/__tests__/rpc-mode.test.ts +89 -0
- package/src/execution/__tests__/run-and-finalize-chatmode.test.ts +267 -0
- package/src/execution/__tests__/run-spawn-chatmode-settled.test.ts +253 -0
- package/src/execution/__tests__/run-spawn-edges.test.ts +687 -0
- package/src/execution/__tests__/run-spawn-integration.test.ts +933 -0
- package/src/execution/__tests__/run-spawn-resume.test.ts +322 -0
- package/src/execution/__tests__/run-spawn-rpc-mode.test.ts +196 -0
- package/src/execution/__tests__/run-spawn-stdout-callback-throw.test.ts +199 -0
- package/src/execution/__tests__/session-context-resolver.test.ts +167 -0
- package/src/execution/__tests__/session-file-gc.test.ts +293 -0
- package/src/execution/__tests__/session-reconstructor.test.ts +379 -0
- package/src/execution/__tests__/session-runner-epipe.test.ts +178 -0
- package/src/execution/__tests__/session-runner-schema-env.test.ts +350 -0
- package/src/execution/__tests__/spawn-args.test.ts +445 -0
- package/src/execution/__tests__/spawn-event-adapter-rpc.test.ts +189 -0
- package/src/execution/__tests__/spawn-event-adapter.test.ts +167 -0
- package/src/execution/__tests__/spawn-worktree-guidance.test.ts +207 -0
- package/src/execution/__tests__/spawned-children.test.ts +92 -0
- package/src/execution/__tests__/start-sync-model-guard.test.ts +150 -0
- package/src/execution/__tests__/stdin-writer.test.ts +462 -0
- package/src/execution/__tests__/stream-sink-retirement.test.ts +261 -0
- package/src/execution/__tests__/subagent-service-abort.test.ts +60 -0
- package/src/execution/__tests__/subagent-service-message-close.test.ts +629 -0
- package/src/execution/__tests__/subagent-service-parent-guard.test.ts +180 -0
- package/src/execution/__tests__/subagent-service.test.ts +788 -0
- package/src/execution/__tests__/subprocess-agent-runner-routing.test.ts +310 -0
- package/src/execution/__tests__/subprocess-agent-runner.test.ts +612 -0
- package/src/execution/__tests__/temp-prompt.test.ts +53 -0
- package/src/execution/__tests__/timeout-integration.test.ts +615 -0
- package/src/execution/__tests__/tombstone-store.test.ts +73 -0
- package/src/execution/__tests__/turn-limiter-semantics.test.ts +194 -0
- package/src/execution/__tests__/turn-limiter.test.ts +65 -0
- package/src/execution/__tests__/ui-channels.test.ts +187 -0
- package/src/execution/__tests__/ui-interaction-model.test.ts +67 -0
- package/src/execution/__tests__/ui-request-handler-factory.test.ts +369 -0
- package/src/execution/__tests__/ui-request-handler.test.ts +204 -0
- package/src/execution/__tests__/ui-request-observability.test.ts +113 -0
- package/src/execution/__tests__/ui-request-queue.test.ts +133 -0
- package/src/execution/__tests__/worktree-manager.test.ts +633 -0
- package/src/execution/__tests__/worktree-pid-registration.integration.test.ts +229 -0
- package/src/execution/__tests__/worktree-reconcile.integration.test.ts +181 -0
- package/src/execution/__tests__/worktree-registry.test.ts +199 -0
- package/src/execution/agent-registry.ts +199 -0
- package/src/execution/agent-result-mapper.ts +90 -0
- package/src/execution/alive-store.ts +119 -0
- package/src/execution/argv-mirror.ts +113 -0
- package/src/execution/best-effort.ts +38 -0
- package/src/execution/channel-registry-access.ts +144 -0
- package/src/execution/concurrency-pool.ts +116 -0
- package/src/execution/config.ts +165 -0
- package/src/execution/dialog-queue.ts +330 -0
- package/src/execution/engine/__tests__/common/data-dir.test.ts +78 -0
- package/src/execution/engine/__tests__/common/errors.test.ts +132 -0
- package/src/execution/engine/__tests__/common/event-journal.test.ts +177 -0
- package/src/execution/engine/__tests__/common/kill-chain.test.ts +192 -0
- package/src/execution/engine/__tests__/common/nesting-guard.test.ts +81 -0
- package/src/execution/engine/__tests__/common/persona-router.test.ts +123 -0
- package/src/execution/engine/__tests__/common/pool-manager.test.ts +154 -0
- package/src/execution/engine/__tests__/common/schema-emulation.test.ts +128 -0
- package/src/execution/engine/__tests__/conformance/__fixtures__/pi-golden-events.json +28 -0
- package/src/execution/engine/__tests__/conformance/agent-event-invariants.ts +141 -0
- package/src/execution/engine/__tests__/conformance/contract.abort.test.ts +109 -0
- package/src/execution/engine/__tests__/conformance/contract.agent-events.test.ts +101 -0
- package/src/execution/engine/__tests__/conformance/contract.probe.test.ts +77 -0
- package/src/execution/engine/__tests__/conformance/contract.read-degradation.test.ts +104 -0
- package/src/execution/engine/__tests__/conformance/engine-conformance.live.test.ts +201 -0
- package/src/execution/engine/__tests__/conformance/golden-replay.pi.test.ts +76 -0
- package/src/execution/engine/__tests__/conformance/golden-replay.zcode.test.ts +79 -0
- package/src/execution/engine/__tests__/engine-discovery.test.ts +87 -0
- package/src/execution/engine/__tests__/model-prompt.test.ts +197 -0
- package/src/execution/engine/__tests__/paths.test.ts +39 -0
- package/src/execution/engine/__tests__/registry.test.ts +120 -0
- package/src/execution/engine/__tests__/routing.test.ts +231 -0
- package/src/execution/engine/common/data-dir.ts +66 -0
- package/src/execution/engine/common/errors.ts +183 -0
- package/src/execution/engine/common/event-journal.ts +254 -0
- package/src/execution/engine/common/journal-replay.ts +62 -0
- package/src/execution/engine/common/kill-chain.ts +221 -0
- package/src/execution/engine/common/nesting-guard.ts +50 -0
- package/src/execution/engine/common/persona-router.ts +108 -0
- package/src/execution/engine/common/pool-manager.ts +226 -0
- package/src/execution/engine/common/schema-emulation.ts +189 -0
- package/src/execution/engine/common/session-view-projection.ts +51 -0
- package/src/execution/engine/engine-discovery.ts +65 -0
- package/src/execution/engine/engines/pi/__tests__/pi-engine.test.ts +469 -0
- package/src/execution/engine/engines/pi/__tests__/reader.test.ts +155 -0
- package/src/execution/engine/engines/pi/__tests__/task-spec-mapper.test.ts +164 -0
- package/src/execution/engine/engines/pi/pi-engine.ts +415 -0
- package/src/execution/engine/engines/pi/reader.ts +48 -0
- package/src/execution/engine/engines/pi/registration.ts +35 -0
- package/src/execution/engine/engines/pi/task-spec-mapper.ts +100 -0
- package/src/execution/engine/engines/zcode/__tests__/__fixtures__/zcode-golden-spawn.json +39 -0
- package/src/execution/engine/engines/zcode/__tests__/launcher.test.ts +150 -0
- package/src/execution/engine/engines/zcode/__tests__/parser.test.ts +246 -0
- package/src/execution/engine/engines/zcode/__tests__/preparer.test.ts +228 -0
- package/src/execution/engine/engines/zcode/__tests__/reader.test.ts +210 -0
- package/src/execution/engine/engines/zcode/__tests__/registration.test.ts +71 -0
- package/src/execution/engine/engines/zcode/__tests__/zcode-engine.live.test.ts +127 -0
- package/src/execution/engine/engines/zcode/__tests__/zcode-engine.test.ts +580 -0
- package/src/execution/engine/engines/zcode/constants.ts +43 -0
- package/src/execution/engine/engines/zcode/golden-sample.ts +39 -0
- package/src/execution/engine/engines/zcode/launcher.ts +161 -0
- package/src/execution/engine/engines/zcode/parser.ts +436 -0
- package/src/execution/engine/engines/zcode/preparer.ts +363 -0
- package/src/execution/engine/engines/zcode/reader.ts +381 -0
- package/src/execution/engine/engines/zcode/registration.ts +37 -0
- package/src/execution/engine/engines/zcode/zcode-engine.ts +658 -0
- package/src/execution/engine/host-task-spec.ts +47 -0
- package/src/execution/engine/model-prompt.ts +158 -0
- package/src/execution/engine/paths.ts +42 -0
- package/src/execution/engine/port.ts +153 -0
- package/src/execution/engine/registry.ts +133 -0
- package/src/execution/engine/routing.ts +218 -0
- package/src/execution/engine/types.ts +309 -0
- package/src/execution/execute-options-mapper.ts +115 -0
- package/src/execution/execution-record.ts +984 -0
- package/src/execution/finalize-record.ts +254 -0
- package/src/execution/finalized-marker.ts +70 -0
- package/src/execution/get-state-handshake.ts +104 -0
- package/src/execution/host-mode.ts +56 -0
- package/src/execution/idle-gc.ts +47 -0
- package/src/execution/lifecycle-manager.ts +513 -0
- package/src/execution/lifecycle-predicates.ts +65 -0
- package/src/execution/manifest-store.ts +256 -0
- package/src/execution/model-config-service.ts +250 -0
- package/src/execution/model-resolver.ts +248 -0
- package/src/execution/notifier.ts +372 -0
- package/src/execution/notify-ledger.ts +580 -0
- package/src/execution/output-collector.ts +228 -0
- package/src/execution/path-encoding.ts +60 -0
- package/src/execution/pi-invocation.ts +120 -0
- package/src/execution/record-entry.ts +132 -0
- package/src/execution/record-store.ts +1265 -0
- package/src/execution/relay-env.ts +37 -0
- package/src/execution/session-context-resolver.ts +64 -0
- package/src/execution/session-file-gc.ts +120 -0
- package/src/execution/session-pending.ts +170 -0
- package/src/execution/session-reconstructor.ts +678 -0
- package/src/execution/session-runner.ts +1789 -0
- package/src/execution/sessions-index.ts +304 -0
- package/src/execution/spawn-event-adapter.ts +363 -0
- package/src/execution/stdin-writer.ts +198 -0
- package/src/execution/stream-sink.ts +126 -0
- package/src/execution/subagent-service.ts +2268 -0
- package/src/execution/subprocess-agent-runner.ts +317 -0
- package/src/execution/temp-prompt.ts +62 -0
- package/src/execution/tombstone-store.ts +72 -0
- package/src/execution/turn-limiter.ts +102 -0
- package/src/execution/types.ts +1023 -0
- package/src/execution/ui-channels.ts +216 -0
- package/src/execution/ui-interaction-model.ts +48 -0
- package/src/execution/ui-request-handler-factory.ts +243 -0
- package/src/execution/ui-request-observability.ts +81 -0
- package/src/execution/ui-request-queue.ts +184 -0
- package/src/execution/worktree-manager.ts +688 -0
- package/src/execution/worktree-registry.ts +279 -0
- package/src/index.ts +87 -0
- package/src/orchestration/__tests__/__fixtures__/worker-template.snapshot.txt +349 -0
- package/src/orchestration/__tests__/agent-call-catch-fallback.test.ts +202 -0
- package/src/orchestration/__tests__/agent-call-stream.test.ts +152 -0
- package/src/orchestration/__tests__/args-validator.test.ts +143 -0
- package/src/orchestration/__tests__/config-loader.test.ts +503 -0
- package/src/orchestration/__tests__/error-recovery-handlers.test.ts +809 -0
- package/src/orchestration/__tests__/error-recovery-postmessage-defense.test.ts +404 -0
- package/src/orchestration/__tests__/error-recovery-serialize-failed-result.test.ts +56 -0
- package/src/orchestration/__tests__/error-recovery-workflow-call.test.ts +166 -0
- package/src/orchestration/__tests__/execute-agent-call.test.ts +451 -0
- package/src/orchestration/__tests__/launcher-nested-workflow.test.ts +600 -0
- package/src/orchestration/__tests__/lifecycle-runid-injection.test.ts +96 -0
- package/src/orchestration/__tests__/lifecycle.test.ts +659 -0
- package/src/orchestration/__tests__/non-cloneable-return-e2e.test.ts +95 -0
- package/src/orchestration/__tests__/review-fix-loop-scriptpath-failfast.test.ts +183 -0
- package/src/orchestration/__tests__/script-lint.test.ts +831 -0
- package/src/orchestration/__tests__/skill-discovery.test.ts +210 -0
- package/src/orchestration/__tests__/test-mocks.ts +197 -0
- package/src/orchestration/__tests__/worker-exit-without-result.test.ts +368 -0
- package/src/orchestration/__tests__/worker-host.test.ts +120 -0
- package/src/orchestration/__tests__/worker-returnmeta-passthrough.test.ts +164 -0
- package/src/orchestration/__tests__/worker-script-builder-runtime.test.ts +563 -0
- package/src/orchestration/__tests__/worker-script-builder.test.ts +309 -0
- package/src/orchestration/__tests__/worker-script-template-snapshot.test.ts +129 -0
- package/src/orchestration/__tests__/workflow-nesting-e2e.test.ts +317 -0
- package/src/orchestration/__tests__/workflow-script-lint-memo.test.ts +110 -0
- package/src/orchestration/agent-opts-resolver.ts +174 -0
- package/src/orchestration/args-validator.ts +127 -0
- package/src/orchestration/config-loader.ts +316 -0
- package/src/orchestration/error-recovery.ts +1018 -0
- package/src/orchestration/execute-agent-call.ts +256 -0
- package/src/orchestration/launcher.ts +455 -0
- package/src/orchestration/lifecycle.ts +399 -0
- package/src/orchestration/models/__tests__/budget.test.ts +307 -0
- package/src/orchestration/models/agent-call.ts +83 -0
- package/src/orchestration/models/budget.ts +118 -0
- package/src/orchestration/models/ports.ts +164 -0
- package/src/orchestration/models/run-runtime.ts +104 -0
- package/src/orchestration/models/run-spec.ts +83 -0
- package/src/orchestration/models/run-state.ts +44 -0
- package/src/orchestration/models/trace.ts +183 -0
- package/src/orchestration/models/types.ts +301 -0
- package/src/orchestration/models/workflow-run.ts +254 -0
- package/src/orchestration/models/workflow-script-registry.ts +35 -0
- package/src/orchestration/models/workflow-script.ts +117 -0
- package/src/orchestration/script-lint.ts +766 -0
- package/src/orchestration/skill-discovery.ts +135 -0
- package/src/orchestration/worker-handle.ts +115 -0
- package/src/orchestration/worker-host.ts +98 -0
- package/src/orchestration/worker-script-builder.ts +421 -0
- package/src/orchestration/workflow-files.ts +85 -0
- package/src/orchestration/workflow-script-registry-impl.ts +133 -0
- package/src/shared/__tests__/agent-ref.test.ts +34 -0
- package/src/shared/__tests__/meta-parser.test.ts +304 -0
- package/src/shared/__tests__/model-ref.test.ts +306 -0
- package/src/shared/__tests__/resource-discovery-manifest-cache.test.ts +288 -0
- package/src/shared/__tests__/resource-discovery.test.ts +749 -0
- package/src/shared/__tests__/resource-meta.test.ts +51 -0
- package/src/shared/__tests__/schema-jsonify.test.ts +81 -0
- package/src/shared/__tests__/timer-delay.test.ts +61 -0
- package/src/shared/agent-event.ts +13 -0
- package/src/shared/agent-ref.ts +57 -0
- package/src/shared/meta-parser.ts +261 -0
- package/src/shared/model-ref.ts +286 -0
- package/src/shared/resource-discovery.ts +835 -0
- package/src/shared/resource-meta.ts +65 -0
- package/src/shared/schema-env.ts +44 -0
- package/src/shared/schema-jsonify.ts +58 -0
- package/src/shared/timer-delay.ts +54 -0
- package/src/shared/xml-injection.ts +35 -0
- package/workflows/README.md +81 -0
- package/workflows/_shared/agent-refs.cjs +40 -0
- package/workflows/chain.js +137 -0
- package/workflows/map-reduce.js +180 -0
- package/workflows/parallel.js +154 -0
- package/workflows/review-fix-loop-utils.cjs +1542 -0
- package/workflows/review-fix-loop.js +1605 -0
- package/workflows/scatter-gather.js +184 -0
|
@@ -0,0 +1,369 @@
|
|
|
1
|
+
// src/__tests__/ui-request-handler-factory.test.ts
|
|
2
|
+
//
|
|
3
|
+
// C1 测试:ui-request-handler-factory.ts — createUiRequestHandlerForMode 透传矩阵。
|
|
4
|
+
//
|
|
5
|
+
// 透传矩阵(createUiRequestHandlerForMode 返回的 handler 行为):
|
|
6
|
+
// - headless(json/print/undefined):返回 undefined(不注入 handler)
|
|
7
|
+
// - TUI:fire-and-forget 回 ack 不透传;dialog 进 dialogQueue 串行
|
|
8
|
+
// - GUI(rpc):fire-and-forget 直接调 realHandler;dialog 进 dialogQueue 串行
|
|
9
|
+
// realHandler 路由:channel 命中 → channelHandler(经 coerceUiResponse 形变);未命中 → defaultDialogForward(dialog 转发结果,fire-and-forget 转发 ctx.ui.* 后回 ack,未知 method warn + ack)。
|
|
10
|
+
// 测接口契约,不测实现细节。
|
|
11
|
+
|
|
12
|
+
import type { ExtensionContext, ExtensionMode } from "@earendil-works/pi-coding-agent";
|
|
13
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
14
|
+
|
|
15
|
+
const { loggerMock } = vi.hoisted(() => ({
|
|
16
|
+
loggerMock: {
|
|
17
|
+
debug: vi.fn(),
|
|
18
|
+
warn: vi.fn(),
|
|
19
|
+
error: vi.fn(),
|
|
20
|
+
},
|
|
21
|
+
}));
|
|
22
|
+
vi.mock("../../core/logger.ts", () => ({
|
|
23
|
+
getLogger: () => loggerMock,
|
|
24
|
+
}));
|
|
25
|
+
|
|
26
|
+
import { DialogGlobalQueue, type UiRequest } from "../dialog-queue.ts";
|
|
27
|
+
import { type ChannelHandler,createUiChannelRegistry } from "../ui-channels.ts";
|
|
28
|
+
import { createUiRequestHandlerForMode } from "../ui-request-handler-factory.ts";
|
|
29
|
+
|
|
30
|
+
// mock ExtensionContext 已补 mode 字段(host-mode.ts 读它分流)。最小形状构造。
|
|
31
|
+
function makeCtx(mode: ExtensionMode): ExtensionContext {
|
|
32
|
+
return {
|
|
33
|
+
cwd: "/tmp/test",
|
|
34
|
+
mode,
|
|
35
|
+
sessionManager: {
|
|
36
|
+
getSessionId: () => "s1",
|
|
37
|
+
getSessionFile: () => undefined,
|
|
38
|
+
getSessionDir: () => "/tmp/test/sessions",
|
|
39
|
+
},
|
|
40
|
+
modelRegistry: undefined,
|
|
41
|
+
model: undefined,
|
|
42
|
+
} as ExtensionContext;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** 带 mock ctx.ui 的 ExtensionContext(GUI fire-and-forget 转发测试用)。
|
|
46
|
+
* dialog method(select/confirm/input/editor)返回 undefined/true/"" 兜底,
|
|
47
|
+
* fire-and-forget method(notify/setStatus/setWidget/setTitle/setEditorText)是 void spy。 */
|
|
48
|
+
function makeCtxWithUi(mode: ExtensionMode = "rpc"): ExtensionContext & { ui: Record<string, ReturnType<typeof vi.fn>> } {
|
|
49
|
+
const ui: Record<string, ReturnType<typeof vi.fn>> = {
|
|
50
|
+
select: vi.fn(async () => undefined),
|
|
51
|
+
confirm: vi.fn(async () => true),
|
|
52
|
+
input: vi.fn(async () => ""),
|
|
53
|
+
editor: vi.fn(async () => ""),
|
|
54
|
+
notify: vi.fn(),
|
|
55
|
+
setStatus: vi.fn(),
|
|
56
|
+
setWidget: vi.fn(),
|
|
57
|
+
setTitle: vi.fn(),
|
|
58
|
+
setEditorText: vi.fn(),
|
|
59
|
+
};
|
|
60
|
+
return {
|
|
61
|
+
cwd: "/tmp/test",
|
|
62
|
+
mode,
|
|
63
|
+
sessionManager: {
|
|
64
|
+
getSessionId: () => "s1",
|
|
65
|
+
getSessionFile: () => undefined,
|
|
66
|
+
getSessionDir: () => "/tmp/test/sessions",
|
|
67
|
+
},
|
|
68
|
+
modelRegistry: undefined,
|
|
69
|
+
model: undefined,
|
|
70
|
+
ui,
|
|
71
|
+
} as unknown as ExtensionContext & { ui: Record<string, ReturnType<typeof vi.fn>> };
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function dialogReq(id: string, channel?: string): UiRequest {
|
|
75
|
+
return { method: "select", id, title: `q-${id}`, ...(channel ? { channel } : {}) };
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function fireAndForgetReq(id: string): UiRequest {
|
|
79
|
+
return { method: "notify", id, message: `n-${id}` };
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// dialog 路径用 fake timers 推进 processNext;静默 console.warn/error(stub 故意 warn)。
|
|
83
|
+
beforeEach(() => {
|
|
84
|
+
vi.useFakeTimers();
|
|
85
|
+
vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
86
|
+
vi.spyOn(console, "error").mockImplementation(() => {});
|
|
87
|
+
loggerMock.warn.mockClear();
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
afterEach(() => {
|
|
91
|
+
vi.useRealTimers();
|
|
92
|
+
vi.restoreAllMocks();
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
describe("createUiRequestHandlerForMode — headless 返回 undefined", () => {
|
|
96
|
+
it("mode='json' → undefined(不注入 handler)", () => {
|
|
97
|
+
const queue = new DialogGlobalQueue();
|
|
98
|
+
expect(createUiRequestHandlerForMode(makeCtx("json"), createUiChannelRegistry(), queue))
|
|
99
|
+
.toBeUndefined();
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it("mode='print' → undefined", () => {
|
|
103
|
+
const queue = new DialogGlobalQueue();
|
|
104
|
+
expect(createUiRequestHandlerForMode(makeCtx("print"), createUiChannelRegistry(), queue))
|
|
105
|
+
.toBeUndefined();
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
describe("createUiRequestHandlerForMode — TUI 模式透传", () => {
|
|
110
|
+
it("fire-and-forget(notify)→ {ack:true},不调 realHandler / 不入队", async () => {
|
|
111
|
+
const registry = createUiChannelRegistry();
|
|
112
|
+
const queue = new DialogGlobalQueue();
|
|
113
|
+
const enqueueSpy = vi.spyOn(queue, "enqueue");
|
|
114
|
+
|
|
115
|
+
const handler = createUiRequestHandlerForMode(makeCtx("tui"), registry, queue)!;
|
|
116
|
+
const resp = await handler(fireAndForgetReq("f1"));
|
|
117
|
+
|
|
118
|
+
expect(resp).toEqual({ ack: true });
|
|
119
|
+
expect(console.warn).not.toHaveBeenCalled(); // realHandler(defaultDialogForward)未走
|
|
120
|
+
expect(enqueueSpy).not.toHaveBeenCalled();
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it("dialog(select 无 channel)→ 进 dialogQueue(enqueue 被调,defaultDialogForward stub cancelled)", async () => {
|
|
124
|
+
const registry = createUiChannelRegistry();
|
|
125
|
+
const queue = new DialogGlobalQueue();
|
|
126
|
+
const enqueueSpy = vi.spyOn(queue, "enqueue");
|
|
127
|
+
|
|
128
|
+
const handler = createUiRequestHandlerForMode(makeCtx("tui"), registry, queue)!;
|
|
129
|
+
const pending = handler(dialogReq("d1"));
|
|
130
|
+
|
|
131
|
+
expect(enqueueSpy).toHaveBeenCalledTimes(1);
|
|
132
|
+
await vi.advanceTimersByTimeAsync(0);
|
|
133
|
+
await expect(pending).resolves.toEqual({ cancelled: true });
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
describe("createUiRequestHandlerForMode — GUI(rpc)模式透传", () => {
|
|
138
|
+
it("fire-and-forget(notify)→ 直接调 realHandler,不入队", async () => {
|
|
139
|
+
const registry = createUiChannelRegistry();
|
|
140
|
+
const queue = new DialogGlobalQueue();
|
|
141
|
+
const enqueueSpy = vi.spyOn(queue, "enqueue");
|
|
142
|
+
|
|
143
|
+
const ctx = makeCtxWithUi("rpc");
|
|
144
|
+
const handler = createUiRequestHandlerForMode(ctx, registry, queue)!;
|
|
145
|
+
// notify 无 channel → realHandler → defaultDialogForward → case "notify" → {ack:true}
|
|
146
|
+
const resp = await handler(fireAndForgetReq("f1"));
|
|
147
|
+
|
|
148
|
+
expect(enqueueSpy).not.toHaveBeenCalled();
|
|
149
|
+
expect(resp).toEqual({ ack: true });
|
|
150
|
+
expect(ctx.ui.notify).toHaveBeenCalledWith("n-f1", "info");
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it("dialog(select 无 channel)→ 进 dialogQueue", async () => {
|
|
154
|
+
const registry = createUiChannelRegistry();
|
|
155
|
+
const queue = new DialogGlobalQueue();
|
|
156
|
+
const enqueueSpy = vi.spyOn(queue, "enqueue");
|
|
157
|
+
|
|
158
|
+
const handler = createUiRequestHandlerForMode(makeCtx("rpc"), registry, queue)!;
|
|
159
|
+
const pending = handler(dialogReq("d1"));
|
|
160
|
+
|
|
161
|
+
expect(enqueueSpy).toHaveBeenCalledTimes(1);
|
|
162
|
+
await vi.advanceTimersByTimeAsync(0);
|
|
163
|
+
await expect(pending).resolves.toEqual({ cancelled: true });
|
|
164
|
+
});
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
describe("createUiRequestHandlerForMode — channel 业务路由", () => {
|
|
168
|
+
it("channel 命中 registry → 调注册的 channelHandler(不走 defaultDialogForward)", async () => {
|
|
169
|
+
const registry = createUiChannelRegistry();
|
|
170
|
+
const channelHandler: ChannelHandler = vi.fn(async () => ({ value: "from-channel" }));
|
|
171
|
+
registry.register("ask_user", channelHandler);
|
|
172
|
+
|
|
173
|
+
// GUI fire-and-forget 直接调 realHandler,绕过队列;channel 命中立即生效
|
|
174
|
+
const handler = createUiRequestHandlerForMode(makeCtx("rpc"), registry, new DialogGlobalQueue())!;
|
|
175
|
+
const resp = await handler({ method: "notify", id: "f1", message: "m", channel: "ask_user" });
|
|
176
|
+
|
|
177
|
+
expect(channelHandler).toHaveBeenCalledTimes(1);
|
|
178
|
+
expect(resp).toEqual({ value: "from-channel" });
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
it("channel 未命中 → defaultDialogForward(fire-and-forget 走 ack)", async () => {
|
|
182
|
+
const ctx = makeCtxWithUi("rpc");
|
|
183
|
+
const handler = createUiRequestHandlerForMode(
|
|
184
|
+
ctx, createUiChannelRegistry(), new DialogGlobalQueue())!;
|
|
185
|
+
const resp = await handler({ method: "notify", id: "f1", message: "m", channel: "unknown" });
|
|
186
|
+
// notify 是 fire-and-forget,channel miss 后走 defaultDialogForward 的 notify case → ack
|
|
187
|
+
expect(resp).toEqual({ ack: true });
|
|
188
|
+
expect(ctx.ui.notify).toHaveBeenCalledWith("m", "info");
|
|
189
|
+
});
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
// coerceUiResponse 形变(通过 channelHandler 返回不同 shape 间接测)
|
|
193
|
+
describe("createUiRequestHandlerForMode — coerceUiResponse 形变", () => {
|
|
194
|
+
async function callWithChannel(raw: unknown) {
|
|
195
|
+
const registry = createUiChannelRegistry();
|
|
196
|
+
registry.register("ask_user", (async () => raw) as ChannelHandler);
|
|
197
|
+
const handler = createUiRequestHandlerForMode(makeCtx("rpc"), registry, new DialogGlobalQueue())!;
|
|
198
|
+
return handler({ method: "notify", id: "f1", message: "m", channel: "ask_user" });
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
it("channelHandler 返回 {value:'x'} → {value:'x'}", async () => {
|
|
202
|
+
expect(await callWithChannel({ value: "x" })).toEqual({ value: "x" });
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
it("channelHandler 返回 {confirmed:true} → {confirmed:true}", async () => {
|
|
206
|
+
expect(await callWithChannel({ confirmed: true })).toEqual({ confirmed: true });
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
it("channelHandler 返回 null(非法)→ 降级 {cancelled:true}", async () => {
|
|
210
|
+
expect(await callWithChannel(null)).toEqual({ cancelled: true });
|
|
211
|
+
});
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
// ── P1:GUI fire-and-forget 分类转发(§3.2 映射表 + §3.7 D1/D2) ──
|
|
215
|
+
describe("defaultDialogForward — fire-and-forget 分类转发", () => {
|
|
216
|
+
function makeHandler(ctx: ExtensionContext) {
|
|
217
|
+
return createUiRequestHandlerForMode(
|
|
218
|
+
ctx, createUiChannelRegistry(), new DialogGlobalQueue())!;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// (a) 五个 case 转发调用与 ack 返回
|
|
222
|
+
it("notify → ctx.ui.notify + {ack:true}", async () => {
|
|
223
|
+
const ctx = makeCtxWithUi();
|
|
224
|
+
const handler = makeHandler(ctx);
|
|
225
|
+
const resp = await handler({ method: "notify", id: "n1", message: "hello" });
|
|
226
|
+
expect(resp).toEqual({ ack: true });
|
|
227
|
+
expect(ctx.ui.notify).toHaveBeenCalledTimes(1);
|
|
228
|
+
expect(ctx.ui.notify).toHaveBeenCalledWith("hello", "info");
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
it("setStatus → ctx.ui.setStatus + {ack:true}", async () => {
|
|
232
|
+
const ctx = makeCtxWithUi();
|
|
233
|
+
const handler = makeHandler(ctx);
|
|
234
|
+
const resp = await handler({ method: "setStatus", id: "s1", statusKey: "progress", statusText: "50%" });
|
|
235
|
+
expect(resp).toEqual({ ack: true });
|
|
236
|
+
expect(ctx.ui.setStatus).toHaveBeenCalledTimes(1);
|
|
237
|
+
expect(ctx.ui.setStatus).toHaveBeenCalledWith("progress", "50%");
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
it("setWidget(channel=undefined)→ ctx.ui.setWidget 含 placement + {ack:true}", async () => {
|
|
241
|
+
const ctx = makeCtxWithUi();
|
|
242
|
+
const handler = makeHandler(ctx);
|
|
243
|
+
const resp = await handler({
|
|
244
|
+
method: "setWidget", id: "w1",
|
|
245
|
+
widgetKey: "my-widget", widgetLines: ["line1", "line2"],
|
|
246
|
+
widgetPlacement: "belowEditor",
|
|
247
|
+
});
|
|
248
|
+
expect(resp).toEqual({ ack: true });
|
|
249
|
+
expect(ctx.ui.setWidget).toHaveBeenCalledTimes(1);
|
|
250
|
+
expect(ctx.ui.setWidget).toHaveBeenCalledWith("my-widget", ["line1", "line2"], { placement: "belowEditor" });
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
it("setTitle → ctx.ui.setTitle + {ack:true}", async () => {
|
|
254
|
+
const ctx = makeCtxWithUi();
|
|
255
|
+
const handler = makeHandler(ctx);
|
|
256
|
+
const resp = await handler({ method: "setTitle", id: "t1", title: "My Title" });
|
|
257
|
+
expect(resp).toEqual({ ack: true });
|
|
258
|
+
expect(ctx.ui.setTitle).toHaveBeenCalledTimes(1);
|
|
259
|
+
expect(ctx.ui.setTitle).toHaveBeenCalledWith("My Title");
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
it("set_editor_text → ctx.ui.setEditorText + {ack:true}", async () => {
|
|
263
|
+
const ctx = makeCtxWithUi();
|
|
264
|
+
const handler = makeHandler(ctx);
|
|
265
|
+
const resp = await handler({ method: "set_editor_text", id: "e1", text: "some code" });
|
|
266
|
+
expect(resp).toEqual({ ack: true });
|
|
267
|
+
expect(ctx.ui.setEditorText).toHaveBeenCalledTimes(1);
|
|
268
|
+
expect(ctx.ui.setEditorText).toHaveBeenCalledWith("some code");
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
// (b) notifyType 收窄三档 + 非法值 fallback info
|
|
272
|
+
it.each([
|
|
273
|
+
["info", "info"],
|
|
274
|
+
["warning", "warning"],
|
|
275
|
+
["error", "error"],
|
|
276
|
+
] as const)("notifyType='%s' → 透传 '%s'", async (input, expected) => {
|
|
277
|
+
const ctx = makeCtxWithUi();
|
|
278
|
+
const handler = makeHandler(ctx);
|
|
279
|
+
await handler({ method: "notify", id: "n1", message: "m", notifyType: input });
|
|
280
|
+
expect(ctx.ui.notify).toHaveBeenCalledWith("m", expected);
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
it("notifyType 非法值 → fallback 'info'", async () => {
|
|
284
|
+
const ctx = makeCtxWithUi();
|
|
285
|
+
const handler = makeHandler(ctx);
|
|
286
|
+
await handler({ method: "notify", id: "n1", message: "m", notifyType: "debug" });
|
|
287
|
+
expect(ctx.ui.notify).toHaveBeenCalledWith("m", "info");
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
it("notifyType undefined → fallback 'info'", async () => {
|
|
291
|
+
const ctx = makeCtxWithUi();
|
|
292
|
+
const handler = makeHandler(ctx);
|
|
293
|
+
await handler({ method: "notify", id: "n1", message: "m" });
|
|
294
|
+
expect(ctx.ui.notify).toHaveBeenCalledWith("m", "info");
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
// (c) setWidget 两分支
|
|
298
|
+
it("setWidget channel='gui_widget' → 不调 ctx.ui.setWidget,回 {ack:true}", async () => {
|
|
299
|
+
const ctx = makeCtxWithUi();
|
|
300
|
+
const handler = makeHandler(ctx);
|
|
301
|
+
const resp = await handler({
|
|
302
|
+
method: "setWidget", id: "w1",
|
|
303
|
+
widgetKey: "gui-w", widgetLines: ["\0XYZ_GUI_WIDGET:{...}"],
|
|
304
|
+
channel: "gui_widget",
|
|
305
|
+
});
|
|
306
|
+
expect(resp).toEqual({ ack: true });
|
|
307
|
+
expect(ctx.ui.setWidget).not.toHaveBeenCalled();
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
it("setWidget channel=undefined → 转发含 placement", async () => {
|
|
311
|
+
const ctx = makeCtxWithUi();
|
|
312
|
+
const handler = makeHandler(ctx);
|
|
313
|
+
await handler({
|
|
314
|
+
method: "setWidget", id: "w1",
|
|
315
|
+
widgetKey: "k", widgetLines: ["a"],
|
|
316
|
+
widgetPlacement: "aboveEditor",
|
|
317
|
+
});
|
|
318
|
+
expect(ctx.ui.setWidget).toHaveBeenCalledWith("k", ["a"], { placement: "aboveEditor" });
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
// (d) 未知 method warn + ack
|
|
322
|
+
it("未知 method → logger.warn + {ack:true}(非 cancelled)", async () => {
|
|
323
|
+
const ctx = makeCtxWithUi();
|
|
324
|
+
const handler = makeHandler(ctx);
|
|
325
|
+
const resp = await handler({ method: "futureMethod", id: "x1" });
|
|
326
|
+
expect(resp).toEqual({ ack: true });
|
|
327
|
+
expect(loggerMock.warn).toHaveBeenCalledWith(
|
|
328
|
+
expect.stringContaining("unknown method"),
|
|
329
|
+
expect.objectContaining({ detail: { method: "futureMethod", id: "x1" } }),
|
|
330
|
+
);
|
|
331
|
+
});
|
|
332
|
+
|
|
333
|
+
// (e) dialog 既有 case 不回归(select/confirm 代表)
|
|
334
|
+
it("select 无 channel → 调 ctx.ui.select + 透传 value", async () => {
|
|
335
|
+
const ctx = makeCtxWithUi();
|
|
336
|
+
ctx.ui.select.mockResolvedValueOnce("picked");
|
|
337
|
+
const handler = makeHandler(ctx);
|
|
338
|
+
const resp = await handler({ method: "select", id: "d1", title: "Choose", options: ["a", "b"] });
|
|
339
|
+
expect(resp).toEqual({ value: "picked" });
|
|
340
|
+
expect(ctx.ui.select).toHaveBeenCalledWith("Choose", ["a", "b"]);
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
it("confirm 无 channel → 调 ctx.ui.confirm + 透传 confirmed", async () => {
|
|
344
|
+
const ctx = makeCtxWithUi();
|
|
345
|
+
ctx.ui.confirm.mockResolvedValueOnce(false);
|
|
346
|
+
const handler = makeHandler(ctx);
|
|
347
|
+
const resp = await handler({ method: "confirm", id: "d2", title: "Sure?", message: "Go?" });
|
|
348
|
+
expect(resp).toEqual({ confirmed: false });
|
|
349
|
+
expect(ctx.ui.confirm).toHaveBeenCalledWith("Sure?", "Go?");
|
|
350
|
+
});
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
// ── 确认 createUiRequestHandlerForMode 未改动(TUI 行为零变化) ──
|
|
354
|
+
describe("createUiRequestHandlerForMode — TUI 零回归", () => {
|
|
355
|
+
it("TUI 下所有 fire-and-forget method 均回 ack 且不调 ctx.ui", async () => {
|
|
356
|
+
const methods = ["notify", "setStatus", "setWidget", "setTitle", "set_editor_text"];
|
|
357
|
+
for (const method of methods) {
|
|
358
|
+
const ctx = makeCtxWithUi("tui");
|
|
359
|
+
const handler = createUiRequestHandlerForMode(
|
|
360
|
+
ctx, createUiChannelRegistry(), new DialogGlobalQueue())!;
|
|
361
|
+
const resp = await handler({ method, id: "t1" });
|
|
362
|
+
expect(resp).toEqual({ ack: true });
|
|
363
|
+
// TUI 不透传,ctx.ui 方法不应被调用
|
|
364
|
+
for (const fn of Object.values(ctx.ui)) {
|
|
365
|
+
expect(fn).not.toHaveBeenCalled();
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
});
|
|
369
|
+
});
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
// src/__tests__/ui-request-handler.test.ts
|
|
2
|
+
//
|
|
3
|
+
// W2 测试(TC-W2 / SR-2):UI 请求处理 —— Pi 原生协议格式。
|
|
4
|
+
//
|
|
5
|
+
// 测试对象:session-runner.ts 的 handleUiRequest + spawn-event-adapter.ts 的 parseSpawnLine
|
|
6
|
+
// 契约来源:.fix-plans/00-master-summary.md §二 2.1(ExtensionUiRequest)+ 2.2(UiRequest/channel)
|
|
7
|
+
// + 2.3(stdin 回写 extension_ui_response 格式)+ §一冲突 2(channel 提取在 session-runner 层)
|
|
8
|
+
//
|
|
9
|
+
// 修复原因(FR-12/SR-2):
|
|
10
|
+
// 旧测试用错误的 JSON-RPC 2.0 格式 mock(jsonrpc:"2.0" + params.marker:"ASK_USER"
|
|
11
|
+
// + params.questions),测试绿但生产红(Pi 实际发 {type, method, title, options} 平铺)。
|
|
12
|
+
// 改为 Pi 真实格式 + channel/channelPayload 断言 + extension_ui_response 回写格式。
|
|
13
|
+
//
|
|
14
|
+
// 红灯原因:handleUiRequest 签名仍是旧的 (child, id, params, ctx, signal)
|
|
15
|
+
// + parseChannel 未接入 session-runner,编译失败。W2 改完签名后转绿。
|
|
16
|
+
|
|
17
|
+
import { PassThrough } from "node:stream";
|
|
18
|
+
|
|
19
|
+
import { describe, expect, it, vi } from "vitest";
|
|
20
|
+
|
|
21
|
+
import { ASK_USER_RPC_PROMPT } from "../session-runner.ts";
|
|
22
|
+
import { parseSpawnLine } from "../spawn-event-adapter.ts";
|
|
23
|
+
import { parseChannel } from "../ui-channels.ts";
|
|
24
|
+
import { createUiRequestQueue, type UiRequest, type UiRequestHandler } from "../ui-request-queue.ts";
|
|
25
|
+
|
|
26
|
+
// ── Pi 原生协议样本构造 ────────────────────────────────────
|
|
27
|
+
// 真实格式:{type:"extension_ui_request", id, method:"select",
|
|
28
|
+
// title:"\0XYZ_ASK_USER", options:[JSON.stringify({questions, allowCancel})]}
|
|
29
|
+
// 无 jsonrpc 字段、无 params 包裹、method 在顶层(非 method:"extension_ui_request")。
|
|
30
|
+
|
|
31
|
+
const ASK_USER_MARKER = "\0XYZ_ASK_USER";
|
|
32
|
+
const askUserPayload = {
|
|
33
|
+
questions: [
|
|
34
|
+
{
|
|
35
|
+
question: "What is your preference?",
|
|
36
|
+
options: [{ label: "Option A" }, { label: "Option B" }],
|
|
37
|
+
},
|
|
38
|
+
],
|
|
39
|
+
allowCancel: true,
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
function askUserLine(id: string): string {
|
|
43
|
+
return JSON.stringify({
|
|
44
|
+
type: "extension_ui_request",
|
|
45
|
+
id,
|
|
46
|
+
method: "select",
|
|
47
|
+
title: ASK_USER_MARKER,
|
|
48
|
+
options: [JSON.stringify(askUserPayload)],
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// ── 解析:Pi 原生格式 → ExtensionUiRequest(method 平铺,无 params) ─────
|
|
53
|
+
|
|
54
|
+
describe("parseSpawnLine — ask_user 请求解析(Pi 原生格式)", () => {
|
|
55
|
+
it("ask_user 行被识别为 extension_ui_request kind", () => {
|
|
56
|
+
const result = parseSpawnLine(askUserLine("ui-req-001"));
|
|
57
|
+
expect(result?.kind).toBe("extension_ui_request");
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("request.method === 'select'(ask_user 借道 select dialog 通道)", () => {
|
|
61
|
+
const result = parseSpawnLine(askUserLine("ui-req-001"));
|
|
62
|
+
if (result?.kind !== "extension_ui_request") {
|
|
63
|
+
expect.fail("expected extension_ui_request kind");
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
expect(result.request.method).toBe("select");
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it("request.title === ASK_USER_MARKER(NUL 前缀 marker 原样保留,由 parseChannel 提取)", () => {
|
|
70
|
+
const result = parseSpawnLine(askUserLine("ui-req-001"));
|
|
71
|
+
if (result?.kind !== "extension_ui_request") return;
|
|
72
|
+
expect(result.request.title).toBe(ASK_USER_MARKER);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it("id 被正确提取(用于 response 关联)", () => {
|
|
76
|
+
const result = parseSpawnLine(askUserLine("ui-req-001"));
|
|
77
|
+
if (result?.kind !== "extension_ui_request") return;
|
|
78
|
+
expect(result.id).toBe("ui-req-001");
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
// ── channel 提取(session-runner 层消费 parseChannel) ──────────────
|
|
83
|
+
// adapter 层只做协议解析(method + 字段平铺),channel 提取在 session-runner 层。
|
|
84
|
+
// 对 select.title 的 NUL 前缀解析出 channel='ask_user' + payload={questions, allowCancel}。
|
|
85
|
+
|
|
86
|
+
describe("parseChannel — ask_user channel 提取(session-runner 层)", () => {
|
|
87
|
+
it("select.title 含 ASK_USER_MARKER → channel='ask_user'", () => {
|
|
88
|
+
const result = parseSpawnLine(askUserLine("ui-req-001"));
|
|
89
|
+
if (result?.kind !== "extension_ui_request") return;
|
|
90
|
+
const channel = parseChannel(result.request);
|
|
91
|
+
expect(channel.channel).toBe("ask_user");
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it("channelPayload 已 parse 为 {questions, allowCancel}", () => {
|
|
95
|
+
const result = parseSpawnLine(askUserLine("ui-req-001"));
|
|
96
|
+
if (result?.kind !== "extension_ui_request") return;
|
|
97
|
+
const channel = parseChannel(result.request);
|
|
98
|
+
expect(channel.channelPayload).toEqual(askUserPayload);
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
// ── stdin 回写格式:extension_ui_response(非 JSON-RPC 2.0 result) ─────
|
|
103
|
+
|
|
104
|
+
describe("handleUiRequest — stdin 回写 extension_ui_response(Pi 原生格式)", () => {
|
|
105
|
+
it("handler 返回 {value} → stdin 写入 {type:extension_ui_response, id, value}", async () => {
|
|
106
|
+
const stdin = new PassThrough();
|
|
107
|
+
const written: string[] = [];
|
|
108
|
+
stdin.on("data", (chunk: Buffer) => written.push(chunk.toString()));
|
|
109
|
+
|
|
110
|
+
const child = { stdin, on: vi.fn(), removeListener: vi.fn() } as unknown as Parameters<
|
|
111
|
+
typeof createUiRequestQueue
|
|
112
|
+
>[0];
|
|
113
|
+
|
|
114
|
+
const handler: UiRequestHandler = vi.fn(
|
|
115
|
+
async (req: UiRequest) => ({ value: `answer-for-${req.id}` }),
|
|
116
|
+
);
|
|
117
|
+
const ctx = { uiRequestHandler: handler } as unknown as Parameters<
|
|
118
|
+
typeof createUiRequestQueue
|
|
119
|
+
>[1];
|
|
120
|
+
|
|
121
|
+
const enqueue = createUiRequestQueue(child, ctx);
|
|
122
|
+
// W2 新签名:enqueue(id, request) —— request 是 ExtensionUiRequest(method 平铺)
|
|
123
|
+
enqueue("ui-req-002", {
|
|
124
|
+
method: "select",
|
|
125
|
+
title: ASK_USER_MARKER,
|
|
126
|
+
options: [JSON.stringify(askUserPayload)],
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
// 等微任务 + stdin flush
|
|
130
|
+
await new Promise((r) => setImmediate(r));
|
|
131
|
+
|
|
132
|
+
const raw = written.join("");
|
|
133
|
+
expect(raw).toContain('"type":"extension_ui_response"');
|
|
134
|
+
expect(raw).toContain('"id":"ui-req-002"');
|
|
135
|
+
expect(raw).toContain('"value":"answer-for-ui-req-002"');
|
|
136
|
+
// 旧 JSON-RPC 2.0 格式不应出现
|
|
137
|
+
expect(raw).not.toContain('"jsonrpc"');
|
|
138
|
+
expect(raw).not.toContain('"result"');
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
// ── handler 抛错兜底(M5):catch → 回 cancelled → 写 stdin ─────
|
|
143
|
+
// session-runner.ts:489-494 的 catch 分支:handler reject 时兜底写 cancelled。
|
|
144
|
+
|
|
145
|
+
describe("handleUiRequest — handler 抛错兜底回 cancelled", () => {
|
|
146
|
+
it("handler reject(new Error('boom')) → stdin 写入 cancelled:true", async () => {
|
|
147
|
+
const stdin = new PassThrough();
|
|
148
|
+
const written: string[] = [];
|
|
149
|
+
stdin.on("data", (chunk: Buffer) => written.push(chunk.toString()));
|
|
150
|
+
|
|
151
|
+
const child = { stdin, on: vi.fn(), removeListener: vi.fn() } as unknown as Parameters<
|
|
152
|
+
typeof createUiRequestQueue
|
|
153
|
+
>[0];
|
|
154
|
+
|
|
155
|
+
// handler 抛错(reject)
|
|
156
|
+
const handler: UiRequestHandler = vi.fn(
|
|
157
|
+
async () => Promise.reject(new Error("boom")),
|
|
158
|
+
);
|
|
159
|
+
const ctx = { uiRequestHandler: handler } as unknown as Parameters<
|
|
160
|
+
typeof createUiRequestQueue
|
|
161
|
+
>[1];
|
|
162
|
+
|
|
163
|
+
const enqueue = createUiRequestQueue(child, ctx);
|
|
164
|
+
enqueue("ui-req-err", {
|
|
165
|
+
method: "select",
|
|
166
|
+
title: ASK_USER_MARKER,
|
|
167
|
+
options: [JSON.stringify(askUserPayload)],
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
// 等微任务 + stdin flush
|
|
171
|
+
await new Promise((r) => setImmediate(r));
|
|
172
|
+
|
|
173
|
+
const raw = written.join("");
|
|
174
|
+
expect(raw).toContain('"type":"extension_ui_response"');
|
|
175
|
+
expect(raw).toContain('"id":"ui-req-err"');
|
|
176
|
+
expect(raw).toContain('"cancelled":true');
|
|
177
|
+
});
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
// ── W4 提示词(保留,不受格式修复影响) ──────────────────────────
|
|
181
|
+
|
|
182
|
+
describe("W4: ask_user RPC 系统提示词注入", () => {
|
|
183
|
+
it("ASK_USER_RPC_PROMPT 常量已导出且非空", () => {
|
|
184
|
+
expect(ASK_USER_RPC_PROMPT).toBeDefined();
|
|
185
|
+
expect(typeof ASK_USER_RPC_PROMPT).toBe("string");
|
|
186
|
+
expect(ASK_USER_RPC_PROMPT.length).toBeGreaterThan(0);
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
it("提示词包含 ask_user 工具说明", () => {
|
|
190
|
+
expect(ASK_USER_RPC_PROMPT).toContain("ask_user");
|
|
191
|
+
expect(ASK_USER_RPC_PROMPT).toContain("Tool Availability");
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
it("提示词告知 LLM ask_user 走 RPC 转发", () => {
|
|
195
|
+
expect(ASK_USER_RPC_PROMPT).toContain("RPC");
|
|
196
|
+
expect(ASK_USER_RPC_PROMPT).toContain("main agent");
|
|
197
|
+
expect(ASK_USER_RPC_PROMPT).toContain("forwarded");
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
it("提示词说明用户在主 agent 界面回答", () => {
|
|
201
|
+
expect(ASK_USER_RPC_PROMPT).toContain("user");
|
|
202
|
+
expect(ASK_USER_RPC_PROMPT).toContain("answers");
|
|
203
|
+
});
|
|
204
|
+
});
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
// src/__tests__/ui-request-observability.test.ts
|
|
2
|
+
//
|
|
3
|
+
// M4 测试:ui-request-observability.ts — UiRequestObservability 纯逻辑类。
|
|
4
|
+
//
|
|
5
|
+
// 测试对象:extensions/universal/subagent-workflow/src/execution/ui-request-observability.ts
|
|
6
|
+
// 契约来源:类注释(per-session 去重 + resetMissingHandlerWarnings 清洗 + setMode/getMode 往返)
|
|
7
|
+
//
|
|
8
|
+
// UiRequestObservability 职责:
|
|
9
|
+
// - setMode/getMode:sessionMode 往返存储
|
|
10
|
+
// - notifyMissingHandler(sessionId):per-session 去重,每 session 只 console.warn 一次
|
|
11
|
+
// - resetMissingHandlerWarnings():清去重集合,清洗后可重新 warn
|
|
12
|
+
//
|
|
13
|
+
// 纯逻辑无异步,测试最简单。console.warn spy 验证调用次数。
|
|
14
|
+
|
|
15
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
16
|
+
|
|
17
|
+
// Mock 共享 logger,让 logger.warn 可被 spy(源码已从 console.warn 改为 logger.warn)
|
|
18
|
+
const { loggerMock } = vi.hoisted(() => ({
|
|
19
|
+
loggerMock: {
|
|
20
|
+
debug: vi.fn(),
|
|
21
|
+
warn: vi.fn(),
|
|
22
|
+
error: vi.fn(),
|
|
23
|
+
},
|
|
24
|
+
}));
|
|
25
|
+
vi.mock("../../core/logger.ts", () => ({
|
|
26
|
+
getLogger: () => loggerMock,
|
|
27
|
+
}));
|
|
28
|
+
|
|
29
|
+
import { UiRequestObservability } from "../ui-request-observability.ts";
|
|
30
|
+
|
|
31
|
+
// ── 公共 fixture ──────────────────────────────────────────────
|
|
32
|
+
|
|
33
|
+
beforeEach(() => {
|
|
34
|
+
loggerMock.warn.mockClear();
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
afterEach(() => {
|
|
38
|
+
vi.restoreAllMocks();
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
describe("UiRequestObservability — setMode/getMode 往返", () => {
|
|
42
|
+
it("setMode('tui') → getMode() === 'tui'", () => {
|
|
43
|
+
const obs = new UiRequestObservability();
|
|
44
|
+
obs.setMode("tui");
|
|
45
|
+
expect(obs.getMode()).toBe("tui");
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("setMode('rpc') → getMode() === 'rpc'", () => {
|
|
49
|
+
const obs = new UiRequestObservability();
|
|
50
|
+
obs.setMode("rpc");
|
|
51
|
+
expect(obs.getMode()).toBe("rpc");
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("初始 getMode() === undefined(未 set)", () => {
|
|
55
|
+
const obs = new UiRequestObservability();
|
|
56
|
+
expect(obs.getMode()).toBeUndefined();
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it("setMode(undefined) → getMode() === undefined(可重置)", () => {
|
|
60
|
+
const obs = new UiRequestObservability();
|
|
61
|
+
obs.setMode("json");
|
|
62
|
+
obs.setMode(undefined);
|
|
63
|
+
expect(obs.getMode()).toBeUndefined();
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
describe("UiRequestObservability — notifyMissingHandler per-session 去重", () => {
|
|
68
|
+
it("同 sessionId 调两次 → 只 warn 一次", () => {
|
|
69
|
+
const obs = new UiRequestObservability();
|
|
70
|
+
obs.notifyMissingHandler("s1");
|
|
71
|
+
obs.notifyMissingHandler("s1");
|
|
72
|
+
expect(loggerMock.warn).toHaveBeenCalledTimes(1);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it("warn 内容含 sessionId 和 mode(可观测性)", () => {
|
|
76
|
+
const obs = new UiRequestObservability();
|
|
77
|
+
obs.setMode("tui");
|
|
78
|
+
obs.notifyMissingHandler("s1");
|
|
79
|
+
expect(loggerMock.warn).toHaveBeenCalledWith(
|
|
80
|
+
expect.stringContaining("session=s1"),
|
|
81
|
+
);
|
|
82
|
+
expect(loggerMock.warn).toHaveBeenCalledWith(
|
|
83
|
+
expect.stringContaining("mode=tui"),
|
|
84
|
+
);
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
describe("UiRequestObservability — resetMissingHandlerWarnings 后可重新 warn", () => {
|
|
89
|
+
it("notify(s1) → reset → notify(s1) → warn 被调 2 次", () => {
|
|
90
|
+
const obs = new UiRequestObservability();
|
|
91
|
+
obs.notifyMissingHandler("s1");
|
|
92
|
+
expect(loggerMock.warn).toHaveBeenCalledTimes(1);
|
|
93
|
+
|
|
94
|
+
obs.resetMissingHandlerWarnings();
|
|
95
|
+
|
|
96
|
+
obs.notifyMissingHandler("s1");
|
|
97
|
+
expect(loggerMock.warn).toHaveBeenCalledTimes(2);
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
describe("UiRequestObservability — 不同 session 各自首次 warn", () => {
|
|
102
|
+
it("notify(s1) + notify(s2) → warn 被调 2 次(互不干扰)", () => {
|
|
103
|
+
const obs = new UiRequestObservability();
|
|
104
|
+
obs.notifyMissingHandler("s1");
|
|
105
|
+
obs.notifyMissingHandler("s2");
|
|
106
|
+
expect(loggerMock.warn).toHaveBeenCalledTimes(2);
|
|
107
|
+
|
|
108
|
+
// 再次各自调用,已被去重,不再 warn
|
|
109
|
+
obs.notifyMissingHandler("s1");
|
|
110
|
+
obs.notifyMissingHandler("s2");
|
|
111
|
+
expect(loggerMock.warn).toHaveBeenCalledTimes(2);
|
|
112
|
+
});
|
|
113
|
+
});
|