ceulen 0.5.1 → 0.7.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/CHANGELOG.md +317 -12
- package/README.md +87 -91
- package/extensions/lib/advisor-marker.ts +33 -0
- package/extensions/lib/panel.ts +25 -2
- package/extensions/lib/rate.ts +24 -0
- package/extensions/lib/registry.ts +93 -7
- package/extensions/lib/tools.ts +47 -0
- package/extensions/lib/usage-store.ts +33 -0
- package/extensions/modules/advisor/commands/advisor.ts +244 -0
- package/extensions/modules/advisor/configPanel.ts +194 -0
- package/extensions/modules/advisor/index.ts +217 -0
- package/extensions/modules/advisor/lib/config.ts +248 -0
- package/extensions/modules/advisor/lib/emission-guard.ts +153 -0
- package/extensions/modules/advisor/lib/isolated-model.ts +160 -0
- package/extensions/modules/advisor/lib/model-picker.ts +47 -0
- package/extensions/modules/advisor/lib/watcher.ts +286 -0
- package/extensions/modules/advisor/test/compliance.test.ts +125 -0
- package/extensions/modules/advisor/test/config.test.ts +202 -0
- package/extensions/modules/advisor/test/configPanel.test.ts +223 -0
- package/extensions/modules/advisor/test/emission-guard.test.ts +201 -0
- package/extensions/modules/advisor/test/isolated-model.test.ts +311 -0
- package/extensions/modules/advisor/test/model-picker.test.ts +32 -0
- package/extensions/modules/advisor/test/module.test.ts +586 -0
- package/extensions/modules/advisor/test/watcher.test.ts +478 -0
- package/extensions/modules/classifier/configPanel.ts +104 -0
- package/extensions/modules/classifier/index.ts +297 -0
- package/extensions/modules/classifier/lib/settings.ts +95 -0
- package/extensions/modules/classifier/test/classifier.test.ts +475 -0
- package/extensions/modules/composer/index.ts +152 -3
- package/extensions/modules/composer/lib/footer.ts +119 -0
- package/extensions/modules/composer/lib/shapes.ts +265 -26
- package/extensions/modules/composer/test/editor.test.ts +1 -1
- package/extensions/modules/composer/test/footer.test.ts +96 -0
- package/extensions/modules/composer/test/module.test.ts +113 -11
- package/extensions/modules/composer/test/screen.test.ts +29 -1
- package/extensions/modules/composer/test/shapes.test.ts +224 -8
- package/extensions/modules/config/defaultTools.ts +96 -0
- package/extensions/modules/config/index.ts +147 -7
- package/extensions/modules/config/piSettings.ts +254 -69
- package/extensions/modules/config/test/defaultTools.test.ts +122 -0
- package/extensions/modules/config/test/menu.test.ts +64 -0
- package/extensions/modules/config/test/panel.test.ts +38 -23
- package/extensions/modules/config/test/piSettings.test.ts +130 -3
- package/extensions/modules/config/test/renderTools.test.ts +71 -0
- package/extensions/modules/config/test/tools.test.ts +146 -0
- package/extensions/modules/fff/index.ts +2094 -0
- package/extensions/modules/fff/lib/query.ts +129 -0
- package/extensions/modules/fff/test/index.test.ts +607 -0
- package/extensions/modules/fff/test/query.test.ts +71 -0
- package/extensions/modules/munin/configPanel.ts +105 -0
- package/extensions/modules/munin/index.ts +641 -0
- package/extensions/modules/munin/lib/helpers.ts +471 -0
- package/extensions/modules/munin/lib/retry.ts +20 -0
- package/extensions/modules/munin/lib/sdk.ts +218 -0
- package/extensions/modules/munin/test/config.test.ts +168 -0
- package/extensions/modules/munin/test/configPanel.test.ts +161 -0
- package/extensions/modules/munin/test/extension.test.ts +224 -0
- package/extensions/modules/munin/test/helpers.test.ts +523 -0
- package/extensions/modules/munin/test/retry.test.ts +145 -0
- package/extensions/modules/munin/test/stale-protocol.test.ts +554 -0
- package/extensions/modules/plan/configPanel.ts +152 -0
- package/extensions/modules/plan/index.ts +917 -0
- package/extensions/modules/plan/lib/plan-tools.ts +72 -0
- package/extensions/modules/plan/lib/prompt.ts +65 -0
- package/extensions/modules/plan/lib/settings.ts +166 -0
- package/extensions/modules/plan/lib/shell-gate.ts +200 -0
- package/extensions/modules/plan/test/configPanel.test.ts +96 -0
- package/extensions/modules/plan/test/harness.ts +133 -0
- package/extensions/modules/plan/test/module.test.ts +305 -0
- package/extensions/modules/plan/test/settings.test.ts +114 -0
- package/extensions/modules/plan/test/shell-gate.test.ts +103 -0
- package/extensions/modules/ponytail/index.ts +23 -73
- package/extensions/modules/ponytail/lib/config.ts +1 -6
- package/extensions/modules/ponytail/test/extension.test.ts +1 -85
- package/extensions/modules/repair/configPanel.ts +96 -0
- package/extensions/modules/repair/index.ts +414 -0
- package/extensions/modules/repair/lib/auto-bg.ts +333 -0
- package/extensions/modules/repair/lib/edit-repair.ts +155 -0
- package/extensions/modules/repair/lib/editor.ts +251 -0
- package/extensions/modules/repair/lib/guards.ts +34 -0
- package/extensions/modules/repair/lib/input-repair.ts +324 -0
- package/extensions/modules/repair/lib/patch.ts +481 -0
- package/extensions/modules/repair/lib/settings.ts +114 -0
- package/extensions/modules/repair/test/auto-bg.test.ts +405 -0
- package/extensions/modules/repair/test/edit-repair.test.ts +224 -0
- package/extensions/modules/repair/test/editor.test.ts +141 -0
- package/extensions/modules/repair/test/guards.test.ts +64 -0
- package/extensions/modules/repair/test/input-repair.test.ts +231 -0
- package/extensions/modules/repair/test/patch.test.ts +563 -0
- package/extensions/modules/repair/test/settings.test.ts +167 -0
- package/extensions/modules/repair/test/wrapper.test.ts +210 -0
- package/extensions/modules/router/commands/commands.ts +4 -0
- package/extensions/modules/router/lib/client.ts +61 -0
- package/extensions/modules/router/lib/provider.ts +34 -13
- package/extensions/modules/router/lib/systemone.ts +152 -0
- package/extensions/modules/router/test/unit.test.ts +179 -0
- package/extensions/modules/rtk/findFallback.js +68 -0
- package/extensions/modules/rtk/index.ts +248 -0
- package/extensions/modules/rtk/safe-rewrite.js +74 -0
- package/extensions/modules/rtk/test/findFallback.test.js +66 -0
- package/extensions/modules/rtk/test/index.test.js +131 -0
- package/extensions/modules/rtk/test/safe-rewrite.test.js +96 -0
- package/extensions/modules/rtk/version-gate.js +34 -0
- package/extensions/modules/serena/index.ts +629 -0
- package/extensions/modules/serena/lib/detect.ts +73 -0
- package/extensions/modules/serena/lib/env.ts +76 -0
- package/extensions/modules/serena/lib/guidance.ts +26 -0
- package/extensions/modules/serena/lib/normalize.ts +27 -0
- package/extensions/modules/serena/lib/symbol-key.ts +20 -0
- package/extensions/modules/serena/lib/truncate.ts +20 -0
- package/extensions/modules/serena/test/env.test.ts +122 -0
- package/extensions/modules/serena/test/index.test.ts +347 -0
- package/extensions/modules/serena/test/worker.test.ts +562 -0
- package/extensions/modules/serena/worker.ts +858 -0
- package/extensions/modules/steering/configPanel.ts +122 -0
- package/extensions/modules/steering/index.ts +616 -0
- package/extensions/modules/steering/lib/anchor.ts +175 -0
- package/extensions/modules/steering/lib/errors.ts +253 -0
- package/extensions/modules/steering/lib/family.ts +33 -0
- package/extensions/modules/steering/lib/guidance.ts +172 -0
- package/extensions/modules/steering/lib/history.ts +170 -0
- package/extensions/modules/steering/lib/settings.ts +127 -0
- package/extensions/modules/steering/test/anchor-regressions.test.ts +93 -0
- package/extensions/modules/steering/test/anchor-wiring.test.ts +99 -0
- package/extensions/modules/steering/test/anchor.test.ts +160 -0
- package/extensions/modules/steering/test/cache-stats.test.ts +43 -0
- package/extensions/modules/steering/test/errors.test.ts +309 -0
- package/extensions/modules/steering/test/family.test.ts +43 -0
- package/extensions/modules/steering/test/guidance.test.ts +357 -0
- package/extensions/modules/steering/test/harness.ts +93 -0
- package/extensions/modules/steering/test/history.test.ts +118 -0
- package/extensions/modules/steering/test/settings.test.ts +182 -0
- package/extensions/modules/steering/test/status.test.ts +71 -0
- package/extensions/modules/steering/test/toolCall.test.ts +99 -0
- package/extensions/modules/subagent/agents/planner.md +14 -0
- package/extensions/modules/subagent/agents/reviewer.md +42 -0
- package/extensions/modules/subagent/agents/scout.md +47 -0
- package/extensions/modules/subagent/agents/tester.md +12 -0
- package/extensions/modules/subagent/agents/worker.md +18 -0
- package/extensions/modules/subagent/configPanel.ts +219 -0
- package/extensions/modules/subagent/index.ts +2451 -0
- package/extensions/modules/subagent/lib/agents.ts +408 -0
- package/extensions/modules/subagent/lib/background.ts +383 -0
- package/extensions/modules/subagent/lib/herdr.ts +1019 -0
- package/extensions/modules/subagent/lib/history.ts +126 -0
- package/extensions/modules/subagent/lib/model.ts +156 -0
- package/extensions/modules/subagent/lib/render.ts +335 -0
- package/extensions/modules/subagent/lib/result.ts +110 -0
- package/extensions/modules/subagent/lib/roles.ts +296 -0
- package/extensions/modules/subagent/lib/routing.ts +223 -0
- package/extensions/modules/subagent/lib/runner.ts +559 -0
- package/extensions/modules/subagent/lib/schema.ts +21 -0
- package/extensions/modules/subagent/lib/security.ts +752 -0
- package/extensions/modules/subagent/lib/settings.ts +159 -0
- package/extensions/modules/subagent/lib/thread-viewer.ts +319 -0
- package/extensions/modules/subagent/lib/threads.ts +135 -0
- package/extensions/modules/subagent/lib/types.ts +18 -0
- package/extensions/modules/subagent/lib/widget.ts +475 -0
- package/extensions/modules/subagent/test/configPanel.test.ts +132 -0
- package/extensions/modules/subagent/test/herdr-advisor.test.ts +40 -0
- package/extensions/modules/subagent/test/routing.test.ts +350 -0
- package/extensions/modules/subagent/test/widget.test.ts +208 -0
- package/extensions/modules/usage/index.ts +59 -16
- package/extensions/modules/usage/test/genrate.test.ts +70 -0
- package/extensions/modules/usage/test/parsers.test.ts +13 -0
- package/extensions/modules/ux/UPSTREAM +24 -0
- package/extensions/modules/ux/index.ts +393 -0
- package/extensions/modules/ux/lib/audit.ts +562 -0
- package/extensions/modules/ux/lib/config.ts +136 -0
- package/extensions/modules/ux/lib/instructions.ts +77 -0
- package/extensions/modules/ux/test/audit.test.ts +756 -0
- package/extensions/modules/ux/test/extension.test.ts +394 -0
- package/extensions/modules/ux/test/instructions.test.ts +19 -0
- package/extensions/modules/web/configPanel.ts +193 -0
- package/extensions/modules/web/index.ts +978 -0
- package/extensions/modules/web/lib/agy.ts +209 -0
- package/extensions/modules/web/lib/brave.ts +42 -0
- package/extensions/modules/web/lib/cdp.ts +686 -0
- package/extensions/modules/web/lib/chatapi.ts +101 -0
- package/extensions/modules/web/lib/chatgpt.ts +796 -0
- package/extensions/modules/web/lib/chrome.ts +328 -0
- package/extensions/modules/web/lib/config.ts +136 -0
- package/extensions/modules/web/lib/content.ts +99 -0
- package/extensions/modules/web/lib/crawl4ai.ts +172 -0
- package/extensions/modules/web/lib/extract.ts +235 -0
- package/extensions/modules/web/lib/firecrawl.ts +56 -0
- package/extensions/modules/web/lib/format.ts +197 -0
- package/extensions/modules/web/lib/gemini-auth.ts +286 -0
- package/extensions/modules/web/lib/gemini-dr.ts +338 -0
- package/extensions/modules/web/lib/gemini.ts +551 -0
- package/extensions/modules/web/lib/imageapi.ts +559 -0
- package/extensions/modules/web/lib/retry.ts +71 -0
- package/extensions/modules/web/lib/search.ts +237 -0
- package/extensions/modules/web/lib/searxng.ts +69 -0
- package/extensions/modules/web/lib/settings.ts +148 -0
- package/extensions/modules/web/scripts/cdp-smoke.ts +77 -0
- package/extensions/modules/web/scripts/gemini-smoke.ts +107 -0
- package/extensions/modules/web/test/agy.test.ts +383 -0
- package/extensions/modules/web/test/cdp.test.ts +717 -0
- package/extensions/modules/web/test/chat.test.ts +171 -0
- package/extensions/modules/web/test/chatgpt.test.ts +633 -0
- package/extensions/modules/web/test/chrome.test.ts +282 -0
- package/extensions/modules/web/test/config.test.ts +218 -0
- package/extensions/modules/web/test/configPanel.test.ts +132 -0
- package/extensions/modules/web/test/crawl.test.ts +134 -0
- package/extensions/modules/web/test/crawl4ai.test.ts +105 -0
- package/extensions/modules/web/test/extension.test.ts +118 -0
- package/extensions/modules/web/test/extract.test.ts +436 -0
- package/extensions/modules/web/test/fixtures/dr-confirm.bin +12 -0
- package/extensions/modules/web/test/fixtures/dr-plan.bin +20 -0
- package/extensions/modules/web/test/fixtures/interaction.html +31 -0
- package/extensions/modules/web/test/format.test.ts +230 -0
- package/extensions/modules/web/test/gemini-auth.test.ts +465 -0
- package/extensions/modules/web/test/gemini-dr.test.ts +249 -0
- package/extensions/modules/web/test/gemini.test.ts +472 -0
- package/extensions/modules/web/test/hook.test.ts +101 -0
- package/extensions/modules/web/test/image.test.ts +1089 -0
- package/extensions/modules/web/test/retry.test.ts +119 -0
- package/extensions/modules/web/test/screenshot.test.ts +109 -0
- package/extensions/modules/web/test/search.test.ts +210 -0
- package/extensions/modules/web/test/settings.test.ts +165 -0
- package/extensions/modules/web/test/skill.test.ts +72 -0
- package/extensions/modules/web/types.d.ts +3 -0
- package/extensions/modules/web/vendor/package.json +1 -0
- package/extensions/modules/web/vendor/readability/LICENSE.md +13 -0
- package/extensions/modules/web/vendor/readability/Readability-readerable.js +123 -0
- package/extensions/modules/web/vendor/readability/Readability.js +2787 -0
- package/extensions/modules/web/vendor/readability/index.d.ts +64 -0
- package/extensions/modules/web/vendor/readability/index.js +9 -0
- package/extensions/modules/web/vendor/turndown/LICENSE +21 -0
- package/extensions/modules/web/vendor/turndown/turndown.cjs.js +784 -0
- package/extensions/modules/web/vendor/turndown-plugin-gfm/LICENSE +21 -0
- package/extensions/modules/web/vendor/turndown-plugin-gfm/turndown-plugin-gfm.cjs.js +163 -0
- package/extensions/modules/zai/configPanel.ts +151 -0
- package/extensions/modules/zai/index.ts +186 -0
- package/extensions/modules/zai/lib/anthropic.ts +159 -0
- package/extensions/modules/zai/lib/settings.ts +198 -0
- package/extensions/modules/zai/lib/signing.ts +718 -0
- package/extensions/modules/zai/lib/throttle.ts +198 -0
- package/extensions/modules/zai/test/anthropic.test.ts +132 -0
- package/extensions/modules/zai/test/configPanel.test.ts +207 -0
- package/extensions/modules/zai/test/extension.test.ts +249 -0
- package/extensions/modules/zai/test/settings.test.ts +183 -0
- package/extensions/modules/zai/test/signing-wiring.test.ts +108 -0
- package/extensions/modules/zai/test/signing.test.ts +345 -0
- package/extensions/modules/zai/test/throttle.test.ts +329 -0
- package/package.json +19 -4
- package/scripts/validate-themes.mjs +75 -0
- package/skills/munin/SKILL.md +117 -0
- package/skills/ux-capture/SKILL.md +202 -0
- package/skills/ux-design/SKILL.md +279 -0
- package/skills/ux-presets/SKILL.md +484 -0
- package/skills/ux-routing/SKILL.md +46 -0
- package/skills/web/SKILL.md +153 -0
- package/themes/alabaster.json +93 -0
- package/themes/amethyst.json +96 -0
- package/themes/anthracite.json +93 -0
- package/themes/basalt.json +91 -0
- package/themes/birch.json +95 -0
- package/themes/catppuccin-mocha.json +91 -0
- package/themes/dark-abyss.json +91 -0
- package/themes/dark-arctic.json +104 -0
- package/themes/dark-aurora.json +95 -0
- package/themes/dark-catppuccin.json +107 -0
- package/themes/dark-cavern.json +91 -0
- package/themes/dark-celestial.json +106 -0
- package/themes/dark-copper.json +95 -0
- package/themes/dark-cosmos.json +90 -0
- package/themes/dark-cyberpunk.json +102 -0
- package/themes/dark-dracula.json +98 -0
- package/themes/dark-eclipse.json +91 -0
- package/themes/dark-ember.json +95 -0
- package/themes/dark-equinox.json +90 -0
- package/themes/dark-forest.json +96 -0
- package/themes/dark-github.json +105 -0
- package/themes/dark-gruvbox.json +112 -0
- package/themes/dark-lavender.json +95 -0
- package/themes/dark-lunar.json +89 -0
- package/themes/dark-midnight.json +95 -0
- package/themes/dark-monochrome.json +94 -0
- package/themes/dark-monokai.json +98 -0
- package/themes/dark-nebula.json +90 -0
- package/themes/dark-neon-noir.json +100 -0
- package/themes/dark-nord.json +98 -0
- package/themes/dark-ocean.json +101 -0
- package/themes/dark-one.json +100 -0
- package/themes/dark-poimandres.json +115 -0
- package/themes/dark-rainforest.json +91 -0
- package/themes/dark-reef.json +91 -0
- package/themes/dark-retro.json +92 -0
- package/themes/dark-rose-pine.json +96 -0
- package/themes/dark-sakura.json +95 -0
- package/themes/dark-slate.json +95 -0
- package/themes/dark-solarized.json +97 -0
- package/themes/dark-solstice.json +90 -0
- package/themes/dark-starfall.json +91 -0
- package/themes/dark-sunset.json +99 -0
- package/themes/dark-swamp.json +90 -0
- package/themes/dark-synthwave.json +103 -0
- package/themes/dark-taiga.json +91 -0
- package/themes/dark-terminal.json +95 -0
- package/themes/dark-tokyo-night.json +101 -0
- package/themes/dark-tundra.json +91 -0
- package/themes/dark-twilight.json +91 -0
- package/themes/dark-volcanic.json +91 -0
- package/themes/dark.json +80 -0
- package/themes/graphite.json +92 -0
- package/themes/light-arctic.json +107 -0
- package/themes/light-aurora-day.json +91 -0
- package/themes/light-canyon.json +91 -0
- package/themes/light-catppuccin.json +106 -0
- package/themes/light-cirrus.json +90 -0
- package/themes/light-coral.json +95 -0
- package/themes/light-cyberpunk.json +96 -0
- package/themes/light-dawn.json +90 -0
- package/themes/light-dunes.json +91 -0
- package/themes/light-eucalyptus.json +95 -0
- package/themes/light-forest.json +100 -0
- package/themes/light-frost.json +95 -0
- package/themes/light-github.json +115 -0
- package/themes/light-glacier.json +91 -0
- package/themes/light-gruvbox.json +108 -0
- package/themes/light-haze.json +90 -0
- package/themes/light-honeycomb.json +95 -0
- package/themes/light-lagoon.json +91 -0
- package/themes/light-lavender.json +95 -0
- package/themes/light-meadow.json +91 -0
- package/themes/light-mint.json +95 -0
- package/themes/light-monochrome.json +101 -0
- package/themes/light-ocean.json +99 -0
- package/themes/light-one.json +99 -0
- package/themes/light-opal.json +91 -0
- package/themes/light-orchard.json +91 -0
- package/themes/light-paper.json +95 -0
- package/themes/light-poimandres.json +115 -0
- package/themes/light-prism.json +90 -0
- package/themes/light-retro.json +98 -0
- package/themes/light-sand.json +95 -0
- package/themes/light-savanna.json +91 -0
- package/themes/light-solarized.json +102 -0
- package/themes/light-soleil.json +90 -0
- package/themes/light-sunset.json +99 -0
- package/themes/light-synthwave.json +98 -0
- package/themes/light-tokyo-night.json +111 -0
- package/themes/light-wetland.json +91 -0
- package/themes/light-zenith.json +89 -0
- package/themes/light.json +80 -0
- package/themes/limestone.json +94 -0
- package/themes/mahogany.json +97 -0
- package/themes/marble.json +93 -0
- package/themes/mirage.json +79 -0
- package/themes/obsidian.json +91 -0
- package/themes/onyx.json +91 -0
- package/themes/pearl.json +93 -0
- package/themes/porcelain.json +91 -0
- package/themes/quartz.json +96 -0
- package/themes/sandstone.json +95 -0
- package/themes/titanium.json +90 -0
|
@@ -0,0 +1,562 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unit tests for SerenaWorkerClient protocol parsing and lifecycle.
|
|
3
|
+
* Uses mocked child_process to avoid actual Python dependency.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { EventEmitter } from "node:events";
|
|
7
|
+
import { describe, it } from "node:test";
|
|
8
|
+
import assert from "node:assert/strict";
|
|
9
|
+
import { SerenaWorkerClient, JB_TOOL_MAP, JB_DROP_PARAMS, JB_PARAM_RENAMES } from "../worker";
|
|
10
|
+
|
|
11
|
+
function createMockProcess(onWrite?: (data: string) => void): any {
|
|
12
|
+
const mockStdout = new EventEmitter() as any;
|
|
13
|
+
const mockStderr = new EventEmitter() as any;
|
|
14
|
+
const mockStdin = {
|
|
15
|
+
write: (data: string) => {
|
|
16
|
+
onWrite?.(String(data));
|
|
17
|
+
// Auto-respond to shutdown requests so stop() doesn't hang
|
|
18
|
+
let parsed: any;
|
|
19
|
+
try {
|
|
20
|
+
parsed = JSON.parse(String(data));
|
|
21
|
+
} catch {
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
if (parsed.action === "shutdown") {
|
|
25
|
+
setTimeout(() => {
|
|
26
|
+
mockStdout.emit("data", JSON.stringify({ id: String(parsed.id), ok: true, shutdown: true }) + "\n");
|
|
27
|
+
}, 1);
|
|
28
|
+
}
|
|
29
|
+
return true;
|
|
30
|
+
},
|
|
31
|
+
} as any;
|
|
32
|
+
const proc: any = {
|
|
33
|
+
stdout: mockStdout,
|
|
34
|
+
stderr: mockStderr,
|
|
35
|
+
stdin: mockStdin,
|
|
36
|
+
killed: false,
|
|
37
|
+
pid: 99999,
|
|
38
|
+
kill: () => {
|
|
39
|
+
proc.killed = true;
|
|
40
|
+
},
|
|
41
|
+
on: (event: string, handler: (...args: any[]) => void) => {
|
|
42
|
+
if (event === "exit") proc._exitHandler = handler;
|
|
43
|
+
},
|
|
44
|
+
_exitHandler: null as ((code: number | null, signal: string | null) => void) | null,
|
|
45
|
+
};
|
|
46
|
+
return proc;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Create a worker whose internal process is set to a mock.
|
|
51
|
+
* The onStdout and exit handlers are manually attached because
|
|
52
|
+
* ensureStarted() is bypassed (process already exists).
|
|
53
|
+
*/
|
|
54
|
+
function createMockedWorker(): {
|
|
55
|
+
worker: SerenaWorkerClient;
|
|
56
|
+
mockStdout: EventEmitter;
|
|
57
|
+
mockStdin: { write: (...args: any[]) => boolean };
|
|
58
|
+
mockProcess: any;
|
|
59
|
+
} {
|
|
60
|
+
const worker = new SerenaWorkerClient();
|
|
61
|
+
|
|
62
|
+
function installMockProcess(): any {
|
|
63
|
+
const proc = createMockProcess();
|
|
64
|
+
(worker as any).process = proc;
|
|
65
|
+
const onStdout = (worker as any).onStdout.bind(worker);
|
|
66
|
+
proc.stdout.on("data", (chunk: string) => onStdout(chunk));
|
|
67
|
+
proc.on("exit", (code: number | null, signal: string | null) => {
|
|
68
|
+
if ((worker as any).process === proc) {
|
|
69
|
+
(worker as any).process = undefined;
|
|
70
|
+
}
|
|
71
|
+
(worker as any).failAll(new Error(`Serena worker exited code=${code} signal=${signal}`));
|
|
72
|
+
});
|
|
73
|
+
return proc;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const mockProcess = installMockProcess();
|
|
77
|
+
(worker as any).ensureStarted = () => {
|
|
78
|
+
if (!(worker as any).process || (worker as any).process.killed) installMockProcess();
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
(worker as any).buffer = "";
|
|
82
|
+
(worker as any).nextId = 1;
|
|
83
|
+
(worker as any).pending = new Map();
|
|
84
|
+
(worker as any).generation = 0;
|
|
85
|
+
|
|
86
|
+
return { worker, mockStdout: mockProcess.stdout, mockStdin: mockProcess.stdin, mockProcess };
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Mirrors resolve_jb_tool_call in the Python bridge (worker.ts PYTHON_BRIDGE),
|
|
90
|
+
// which is built from the same exported tables via JSON interpolation.
|
|
91
|
+
function resolveJbToolCall(toolName: string, params: Record<string, unknown>): { name: string; params: Record<string, unknown> } {
|
|
92
|
+
const jbName = JB_TOOL_MAP[toolName] ?? toolName;
|
|
93
|
+
const mapped: Record<string, unknown> = { ...params };
|
|
94
|
+
for (const [renameFrom, renameTo] of Object.entries(JB_PARAM_RENAMES[jbName] ?? {})) {
|
|
95
|
+
if (renameFrom in mapped) {
|
|
96
|
+
mapped[renameTo] = mapped[renameFrom];
|
|
97
|
+
delete mapped[renameFrom];
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
for (const drop of JB_DROP_PARAMS[jbName] ?? []) {
|
|
101
|
+
delete mapped[drop];
|
|
102
|
+
}
|
|
103
|
+
return { name: jbName, params: mapped };
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// Mirrors _jb_pick_unique_symbol in the Python bridge (worker.ts PYTHON_BRIDGE):
|
|
107
|
+
// find_symbol matches name-path *patterns*, so multiple symbols may match; only a
|
|
108
|
+
// single match or exactly one exact name-path match is accepted.
|
|
109
|
+
function jbPickUniqueSymbol(symbols: Array<{ name_path?: string }>, namePath: string): { name_path?: string } | null {
|
|
110
|
+
if (!symbols || symbols.length === 0) return null;
|
|
111
|
+
if (symbols.length === 1) return symbols[0]!;
|
|
112
|
+
const suffix = "/" + namePath;
|
|
113
|
+
const exact = symbols.filter((s) => s.name_path === namePath || (s.name_path ?? "").endsWith(suffix));
|
|
114
|
+
return exact.length === 1 ? exact[0]! : null;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// Mirrors _jb_is_declaration_position_error in the Python bridge: the JetBrains
|
|
118
|
+
// plugin reports declarations-at-position with varying wording; match tolerantly.
|
|
119
|
+
function jbIsDeclarationPositionError(result: string): boolean {
|
|
120
|
+
return /not.*resolvable|may not be on|is.*declaration|declaration.*itself/i.test(result);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// The three regex templates in _jb_declaration_regexes must each contain exactly
|
|
124
|
+
// ONE capturing group — find_text_coordinates raises ValueError otherwise, silently
|
|
125
|
+
// disabling the stage-2 find_declaration fallback. These are fixtures mirroring the
|
|
126
|
+
// Python rf-string templates (name -> {last}, re.escape'd).
|
|
127
|
+
const JB_DECLARATION_REGEX_TEMPLATES = [
|
|
128
|
+
String.raw`\b(?:class|interface|type|function|const|let|var|def|struct|enum|trait|impl)\s+({last})\b`,
|
|
129
|
+
String.raw`(?<![.\w])({last})\s*\(`,
|
|
130
|
+
String.raw`\b({last})\b`,
|
|
131
|
+
];
|
|
132
|
+
|
|
133
|
+
describe("JetBrains tool remapping tables", () => {
|
|
134
|
+
it("maps every LSP-excluded tool to its jet_brains_* variant", () => {
|
|
135
|
+
const excluded = ["get_symbols_overview", "find_symbol", "find_referencing_symbols", "find_declaration", "find_implementations", "rename_symbol", "safe_delete_symbol"];
|
|
136
|
+
for (const tool of excluded) {
|
|
137
|
+
assert.strictEqual(typeof JB_TOOL_MAP[tool], "string", tool);
|
|
138
|
+
assert.ok(JB_TOOL_MAP[tool].startsWith("jet_brains_"), tool);
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it("maps to the exact serena-agent variant names", () => {
|
|
143
|
+
assert.deepStrictEqual(JB_TOOL_MAP, {
|
|
144
|
+
get_symbols_overview: "jet_brains_get_symbols_overview",
|
|
145
|
+
find_symbol: "jet_brains_find_symbol",
|
|
146
|
+
find_referencing_symbols: "jet_brains_find_referencing_symbols",
|
|
147
|
+
find_declaration: "jet_brains_find_declaration",
|
|
148
|
+
find_implementations: "jet_brains_find_implementations",
|
|
149
|
+
rename_symbol: "jet_brains_rename",
|
|
150
|
+
safe_delete_symbol: "jet_brains_safe_delete",
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it("drop/rename tables only reference known jet_brains_* variants", () => {
|
|
155
|
+
const jbNames = new Set(Object.values(JB_TOOL_MAP));
|
|
156
|
+
for (const key of Object.keys(JB_DROP_PARAMS)) {
|
|
157
|
+
assert.ok(jbNames.has(key), `JB_DROP_PARAMS key ${key}`);
|
|
158
|
+
assert.ok(JB_DROP_PARAMS[key].length > 0);
|
|
159
|
+
}
|
|
160
|
+
for (const key of Object.keys(JB_PARAM_RENAMES)) {
|
|
161
|
+
assert.ok(jbNames.has(key), `JB_PARAM_RENAMES key ${key}`);
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it("safe_delete remaps name_path_pattern -> name_path and drops LSP-only params", () => {
|
|
166
|
+
const out = resolveJbToolCall("safe_delete_symbol", {
|
|
167
|
+
name_path_pattern: "Foo",
|
|
168
|
+
relative_path: "a.ts",
|
|
169
|
+
delete_even_if_used: false,
|
|
170
|
+
});
|
|
171
|
+
assert.strictEqual(out.name, "jet_brains_safe_delete");
|
|
172
|
+
assert.deepStrictEqual(out.params, { name_path: "Foo", relative_path: "a.ts", delete_even_if_used: false });
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
it("find_symbol drops include_kinds/exclude_kinds/substring_matching", () => {
|
|
176
|
+
const out = resolveJbToolCall("find_symbol", {
|
|
177
|
+
name_path_pattern: "Foo",
|
|
178
|
+
include_kinds: [5, 6],
|
|
179
|
+
exclude_kinds: [7],
|
|
180
|
+
substring_matching: true,
|
|
181
|
+
relative_path: "a.ts",
|
|
182
|
+
});
|
|
183
|
+
assert.strictEqual(out.name, "jet_brains_find_symbol");
|
|
184
|
+
assert.deepStrictEqual(out.params, { name_path_pattern: "Foo", relative_path: "a.ts" });
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
it("find_referencing_symbols drops include_kinds/exclude_kinds", () => {
|
|
188
|
+
const out = resolveJbToolCall("find_referencing_symbols", {
|
|
189
|
+
name_path: "Foo/bar",
|
|
190
|
+
relative_path: "a.ts",
|
|
191
|
+
include_kinds: [5],
|
|
192
|
+
exclude_kinds: [7],
|
|
193
|
+
});
|
|
194
|
+
assert.strictEqual(out.name, "jet_brains_find_referencing_symbols");
|
|
195
|
+
assert.deepStrictEqual(out.params, { name_path: "Foo/bar", relative_path: "a.ts" });
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
it("rename_symbol maps to jet_brains_rename with unchanged params", () => {
|
|
199
|
+
const out = resolveJbToolCall("rename_symbol", { name_path: "Foo", relative_path: "a.ts", new_name: "Bar" });
|
|
200
|
+
assert.strictEqual(out.name, "jet_brains_rename");
|
|
201
|
+
assert.deepStrictEqual(out.params, { name_path: "Foo", relative_path: "a.ts", new_name: "Bar" });
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
it("is a no-op for shared tools (replace_symbol_body, search_for_pattern)", () => {
|
|
205
|
+
for (const tool of ["replace_symbol_body", "insert_before_symbol", "search_for_pattern", "replace_content"]) {
|
|
206
|
+
const out = resolveJbToolCall(tool, { relative_path: "a.ts" });
|
|
207
|
+
assert.strictEqual(out.name, tool);
|
|
208
|
+
}
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
it("_jb_pick_unique_symbol returns the single match as-is", () => {
|
|
212
|
+
const syms = [{ name_path: "Foo/bar" }];
|
|
213
|
+
assert.deepStrictEqual(jbPickUniqueSymbol(syms, "bar"), { name_path: "Foo/bar" });
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
it("_jb_pick_unique_symbol returns null for no matches", () => {
|
|
217
|
+
assert.strictEqual(jbPickUniqueSymbol([], "bar"), null);
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
it("_jb_pick_unique_symbol disambiguates by exact name-path suffix match", () => {
|
|
221
|
+
const syms = [{ name_path: "Foo/bar" }, { name_path: "Baz/bar" }, { name_path: "Foo/baz" }];
|
|
222
|
+
// "bar" matches Foo/bar and Baz/bar (2 exact suffix matches) -> ambiguous -> null
|
|
223
|
+
assert.strictEqual(jbPickUniqueSymbol(syms, "bar"), null);
|
|
224
|
+
// "Foo/bar" has exactly one exact match -> returned
|
|
225
|
+
assert.deepStrictEqual(jbPickUniqueSymbol(syms, "Foo/bar"), { name_path: "Foo/bar" });
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
it("_jb_pick_unique_symbol returns null for ambiguous bare name", () => {
|
|
229
|
+
const syms = [{ name_path: "Foo/bar" }, { name_path: "Baz/bar" }];
|
|
230
|
+
assert.strictEqual(jbPickUniqueSymbol(syms, "bar"), null);
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
it("_jb_is_declaration_position_error matches the plugin's phrasing variants", () => {
|
|
234
|
+
assert.ok(jbIsDeclarationPositionError("Error executing tool: APIError - No declaration found at line 1. The cursor may not be on a resolvable reference."));
|
|
235
|
+
assert.ok(jbIsDeclarationPositionError("Error: The selected element is a declaration, not a reference"));
|
|
236
|
+
assert.ok(jbIsDeclarationPositionError("Error: Symbol at this position is itself a declaration"));
|
|
237
|
+
assert.ok(!(jbIsDeclarationPositionError("Error executing tool: APIError - No symbol found for NamePathMatcher")));
|
|
238
|
+
assert.ok(!(jbIsDeclarationPositionError("OK")));
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
it("_jb_is_declaration_position_error is case-insensitive", () => {
|
|
242
|
+
assert.ok(jbIsDeclarationPositionError("MAY NOT BE ON a resolvable reference"));
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
it("each _jb_declaration_regexes pattern has exactly one capturing group", () => {
|
|
246
|
+
const sample = "Foo";
|
|
247
|
+
for (const template of JB_DECLARATION_REGEX_TEMPLATES) {
|
|
248
|
+
const pattern = template.replace("{last}", sample);
|
|
249
|
+
const re = new RegExp(pattern);
|
|
250
|
+
// A capturing group count: count unescaped '(' that open a capture group.
|
|
251
|
+
// Non-capturing groups are (?:...) — excluded by this count.
|
|
252
|
+
let groups = 0;
|
|
253
|
+
for (let i = 0; i < pattern.length; i++) {
|
|
254
|
+
if (pattern[i] !== "(") continue;
|
|
255
|
+
// Skip escaped \(
|
|
256
|
+
if (i > 0 && pattern[i - 1] === "\\") continue;
|
|
257
|
+
// Skip non-capturing (?: and lookarounds (?<! / (?=
|
|
258
|
+
const next = pattern[i + 1];
|
|
259
|
+
if (next === "?") continue;
|
|
260
|
+
groups++;
|
|
261
|
+
}
|
|
262
|
+
assert.strictEqual(groups, 1, `pattern ${pattern} must have exactly 1 capturing group`);
|
|
263
|
+
}
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
it("each _jb_declaration_regexes pattern matches its intended declaration shape", () => {
|
|
267
|
+
const sample = "methodOne";
|
|
268
|
+
const [keyword, call, bare] = JB_DECLARATION_REGEX_TEMPLATES.map((t) => new RegExp(t.replace("{last}", sample)));
|
|
269
|
+
assert.ok(keyword.test(" function methodOne(): void {")); // keyword-anchored
|
|
270
|
+
assert.ok(call.test(" methodOne(): void {")); // method definition (no preceding word char/dot)
|
|
271
|
+
assert.ok(!(call.test(" this.methodOne();"))); // call site (preceded by dot) excluded
|
|
272
|
+
assert.ok(bare.test(" methodOne")); // bare fallback
|
|
273
|
+
});
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
describe("SerenaWorkerClient", () => {
|
|
277
|
+
describe("request/response protocol", () => {
|
|
278
|
+
it("resolves a successful response", async () => {
|
|
279
|
+
const { worker, mockStdout } = createMockedWorker();
|
|
280
|
+
const resultPromise = worker.request({ action: "test" });
|
|
281
|
+
mockStdout.emit("data", JSON.stringify({ id: "1", ok: true, result: "hello" }) + "\n");
|
|
282
|
+
const result = await resultPromise;
|
|
283
|
+
assert.ok(result.ok);
|
|
284
|
+
assert.strictEqual(result.result, "hello");
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
it("handles error responses", async () => {
|
|
288
|
+
const { worker, mockStdout } = createMockedWorker();
|
|
289
|
+
const resultPromise = worker.request({ action: "test" });
|
|
290
|
+
mockStdout.emit("data", JSON.stringify({ id: "1", ok: false, error: "something went wrong", errorType: "serena_error" }) + "\n");
|
|
291
|
+
const result = await resultPromise;
|
|
292
|
+
assert.ok(!(result.ok));
|
|
293
|
+
assert.strictEqual(result.error, "something went wrong");
|
|
294
|
+
assert.strictEqual(result.errorType, "serena_error");
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
it("handles chunked stdout data", async () => {
|
|
298
|
+
const { worker, mockStdout } = createMockedWorker();
|
|
299
|
+
const resultPromise = worker.request({ action: "test" });
|
|
300
|
+
mockStdout.emit("data", '{"id":"1","ok":true,');
|
|
301
|
+
mockStdout.emit("data", '"result":"chunked"}\n');
|
|
302
|
+
const result = await resultPromise;
|
|
303
|
+
assert.ok(result.ok);
|
|
304
|
+
assert.strictEqual(result.result, "chunked");
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
it("queues multiple requests and resolves them sequentially", async () => {
|
|
308
|
+
const { worker, mockStdout } = createMockedWorker();
|
|
309
|
+
const p1 = worker.request({ action: "first" });
|
|
310
|
+
const p2 = worker.request({ action: "second" });
|
|
311
|
+
assert.strictEqual((worker as any).queue.length, 1);
|
|
312
|
+
mockStdout.emit("data", JSON.stringify({ id: "1", ok: true, result: "first" }) + "\n");
|
|
313
|
+
const r1 = await p1;
|
|
314
|
+
mockStdout.emit("data", JSON.stringify({ id: "2", ok: true, result: "second" }) + "\n");
|
|
315
|
+
const r2 = await p2;
|
|
316
|
+
assert.strictEqual(r1.result, "first");
|
|
317
|
+
assert.strictEqual(r2.result, "second");
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
it("ignores JSON lines with unknown ids", async () => {
|
|
321
|
+
const { worker, mockStdout } = createMockedWorker();
|
|
322
|
+
const resultPromise = worker.request({ action: "test" });
|
|
323
|
+
mockStdout.emit("data", JSON.stringify({ id: "unknown", ok: true, result: "ignored" }) + "\n");
|
|
324
|
+
mockStdout.emit("data", JSON.stringify({ id: "1", ok: true, result: "real" }) + "\n");
|
|
325
|
+
const result = await resultPromise;
|
|
326
|
+
assert.strictEqual(result.result, "real");
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
it("ignores non-JSON stdout lines", async () => {
|
|
330
|
+
const { worker, mockStdout } = createMockedWorker();
|
|
331
|
+
const resultPromise = worker.request({ action: "test" });
|
|
332
|
+
mockStdout.emit("data", "not json\n");
|
|
333
|
+
mockStdout.emit("data", JSON.stringify({ id: "1", ok: true, result: "after noise" }) + "\n");
|
|
334
|
+
const result = await resultPromise;
|
|
335
|
+
assert.strictEqual(result.result, "after noise");
|
|
336
|
+
});
|
|
337
|
+
|
|
338
|
+
it("handles responses with no 'id' field", async () => {
|
|
339
|
+
const { worker, mockStdout } = createMockedWorker();
|
|
340
|
+
const resultPromise = worker.request({ action: "test" });
|
|
341
|
+
mockStdout.emit("data", JSON.stringify({ ok: true, result: "no id" }) + "\n");
|
|
342
|
+
mockStdout.emit("data", JSON.stringify({ id: "1", ok: true, result: "with id" }) + "\n");
|
|
343
|
+
const result = await resultPromise;
|
|
344
|
+
assert.strictEqual(result.result, "with id");
|
|
345
|
+
});
|
|
346
|
+
|
|
347
|
+
it("handles empty lines between responses", async () => {
|
|
348
|
+
const { worker, mockStdout } = createMockedWorker();
|
|
349
|
+
const resultPromise = worker.request({ action: "test" });
|
|
350
|
+
mockStdout.emit("data", "\n\n");
|
|
351
|
+
mockStdout.emit("data", JSON.stringify({ id: "1", ok: true, result: "after blanks" }) + "\n");
|
|
352
|
+
const result = await resultPromise;
|
|
353
|
+
assert.strictEqual(result.result, "after blanks");
|
|
354
|
+
});
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
describe("timeout handling", () => {
|
|
358
|
+
it("rejects and resets on timeout", async () => {
|
|
359
|
+
const { worker } = createMockedWorker();
|
|
360
|
+
const start = Date.now();
|
|
361
|
+
try {
|
|
362
|
+
await worker.request({ action: "test" }, 20);
|
|
363
|
+
assert.fail("Should have thrown on timeout");
|
|
364
|
+
} catch (err: any) {
|
|
365
|
+
assert.ok((err.message).includes("timed out"));
|
|
366
|
+
assert.ok((err.message).includes("test"));
|
|
367
|
+
}
|
|
368
|
+
// Worker should be reset after timeout
|
|
369
|
+
assert.strictEqual((worker as any).process, undefined);
|
|
370
|
+
assert.ok(Date.now() - start >= 15);
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
it("does not reject later queued requests when the current request times out", async () => {
|
|
374
|
+
const { worker } = createMockedWorker();
|
|
375
|
+
const p1 = worker.request({ action: "first" }, 20);
|
|
376
|
+
const p2 = worker.request({ action: "second" }, 100);
|
|
377
|
+
try {
|
|
378
|
+
await p1;
|
|
379
|
+
assert.fail("Should have thrown");
|
|
380
|
+
} catch (err: any) {
|
|
381
|
+
assert.ok((err.message).includes("timed out"));
|
|
382
|
+
}
|
|
383
|
+
setTimeout(() => {
|
|
384
|
+
const proc = (worker as any).process;
|
|
385
|
+
proc.stdout.emit("data", JSON.stringify({ id: "2", ok: true, result: "second" }) + "\n");
|
|
386
|
+
}, 1);
|
|
387
|
+
const result = await p2;
|
|
388
|
+
assert.strictEqual(result.result, "second");
|
|
389
|
+
assert.strictEqual((worker as any).pending.size, 0);
|
|
390
|
+
});
|
|
391
|
+
});
|
|
392
|
+
|
|
393
|
+
describe("lifecycle", () => {
|
|
394
|
+
it("stop() does not hang behind an active queued request", async () => {
|
|
395
|
+
const { worker, mockProcess, mockStdout } = createMockedWorker();
|
|
396
|
+
// Start a request that will hang
|
|
397
|
+
const p1 = worker.request({ action: "hang" }, 5000);
|
|
398
|
+
p1.catch(() => {}); // prevent unhandled rejection warning
|
|
399
|
+
let writtenToStdin = false;
|
|
400
|
+
mockProcess.stdin.write = (data: string) => {
|
|
401
|
+
if (data.includes("shutdown")) {
|
|
402
|
+
writtenToStdin = true;
|
|
403
|
+
try {
|
|
404
|
+
const parsed = JSON.parse(data);
|
|
405
|
+
setTimeout(() => mockStdout.emit("data", JSON.stringify({ id: parsed.id, ok: true, shutdown: true }) + "\n"), 1);
|
|
406
|
+
} catch {}
|
|
407
|
+
}
|
|
408
|
+
return true;
|
|
409
|
+
};
|
|
410
|
+
// Call stop() while the request is still pending
|
|
411
|
+
const stopPromise = worker.stop();
|
|
412
|
+
await stopPromise;
|
|
413
|
+
assert.ok(writtenToStdin); // Shutdown should be sent immediately bypassing queue
|
|
414
|
+
assert.ok(mockProcess.killed);
|
|
415
|
+
try {
|
|
416
|
+
await p1;
|
|
417
|
+
assert.fail("Should have thrown");
|
|
418
|
+
} catch (err: any) {
|
|
419
|
+
assert.ok((err.message).includes("Serena worker stopped"));
|
|
420
|
+
}
|
|
421
|
+
});
|
|
422
|
+
|
|
423
|
+
it("stop() rejects queued requests promptly", async () => {
|
|
424
|
+
const { worker, mockProcess } = createMockedWorker();
|
|
425
|
+
// One active request
|
|
426
|
+
const p1 = worker.request({ action: "active" }, 5000);
|
|
427
|
+
p1.catch(() => {});
|
|
428
|
+
// One queued request
|
|
429
|
+
const p2 = worker.request({ action: "queued" }, 5000);
|
|
430
|
+
p2.catch(() => {});
|
|
431
|
+
// Should be in queue
|
|
432
|
+
assert.strictEqual((worker as any).queue.length, 1);
|
|
433
|
+
|
|
434
|
+
await worker.stop();
|
|
435
|
+
|
|
436
|
+
try { await p1; assert.fail("expected rejection"); } catch (e: any) { assert.ok((e.message).includes("Serena worker stopped")); }
|
|
437
|
+
try { await p2; assert.fail("expected rejection"); } catch (e: any) { assert.ok((e.message).includes("Serena worker stopped")); }
|
|
438
|
+
});
|
|
439
|
+
|
|
440
|
+
it("stop() rejects a request enqueued during the shutdown-ack wait", async () => {
|
|
441
|
+
const { worker, mockProcess, mockStdout } = createMockedWorker();
|
|
442
|
+
const p1 = worker.request({ action: "active" }, 5000);
|
|
443
|
+
p1.catch(() => {});
|
|
444
|
+
// Hold the shutdown ack open until we enqueue the second request.
|
|
445
|
+
let releaseAck: () => void = () => {};
|
|
446
|
+
mockProcess.stdin.write = (data: string) => {
|
|
447
|
+
if (data.includes("shutdown")) {
|
|
448
|
+
const parsed = JSON.parse(data);
|
|
449
|
+
releaseAck = () => mockStdout.emit("data", JSON.stringify({ id: parsed.id, ok: true, shutdown: true }) + "\n");
|
|
450
|
+
}
|
|
451
|
+
return true;
|
|
452
|
+
};
|
|
453
|
+
const stopPromise = worker.stop();
|
|
454
|
+
const p2 = worker.request({ action: "during-stop" }, 5000);
|
|
455
|
+
p2.catch(() => {});
|
|
456
|
+
releaseAck();
|
|
457
|
+
await stopPromise;
|
|
458
|
+
try { await p2; assert.fail("Should have been rejected"); }
|
|
459
|
+
catch (e: any) { assert.ok((e.message).includes("Serena worker stopped")); }
|
|
460
|
+
});
|
|
461
|
+
|
|
462
|
+
it("stop() kills the process and clears state", async () => {
|
|
463
|
+
const { worker, mockProcess } = createMockedWorker();
|
|
464
|
+
// stop() sends "shutdown", waits 2000ms max, kills the process in finally block.
|
|
465
|
+
await worker.stop();
|
|
466
|
+
assert.strictEqual((worker as any).process, undefined);
|
|
467
|
+
assert.ok(mockProcess.killed);
|
|
468
|
+
});
|
|
469
|
+
|
|
470
|
+
it("restart() destroys pending and starts fresh", () => {
|
|
471
|
+
const { worker, mockProcess } = createMockedWorker();
|
|
472
|
+
assert.ok(!(mockProcess.killed));
|
|
473
|
+
|
|
474
|
+
// restart() kills the current process and calls ensureStarted().
|
|
475
|
+
// ensureStarted() may find Python and spawn a real process if Serena
|
|
476
|
+
// is installed. We handle both cases.
|
|
477
|
+
try {
|
|
478
|
+
worker.restart();
|
|
479
|
+
} catch {
|
|
480
|
+
// No Python found — expected in isolated environments
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
assert.ok(mockProcess.killed);
|
|
484
|
+
assert.strictEqual((worker as any).pending.size, 0);
|
|
485
|
+
});
|
|
486
|
+
|
|
487
|
+
it("exit handler triggers failAll", (_t, done) => {
|
|
488
|
+
const { worker, mockProcess } = createMockedWorker();
|
|
489
|
+
// Set up a pending request
|
|
490
|
+
(worker as any).pending.set("1", {
|
|
491
|
+
resolve: () => done(new Error("Should not resolve")),
|
|
492
|
+
reject: (err: Error) => {
|
|
493
|
+
try {
|
|
494
|
+
assert.ok((err.message).includes("exited"));
|
|
495
|
+
done();
|
|
496
|
+
} catch (assertErr) {
|
|
497
|
+
done(assertErr);
|
|
498
|
+
}
|
|
499
|
+
},
|
|
500
|
+
timer: setTimeout(() => {}, 100_000),
|
|
501
|
+
});
|
|
502
|
+
|
|
503
|
+
// Simulate process exit — the exit handler is in ensureStarted() which
|
|
504
|
+
// was never called (we injected process directly). Fire the handler manually.
|
|
505
|
+
mockProcess._exitHandler?.(1, null);
|
|
506
|
+
});
|
|
507
|
+
|
|
508
|
+
it("stop() cleans up pending entry on timeout", async () => {
|
|
509
|
+
const { worker, mockProcess } = createMockedWorker();
|
|
510
|
+
const originalSetTimeout = global.setTimeout;
|
|
511
|
+
let capturedCallback: (() => void) | undefined;
|
|
512
|
+
(global as any).setTimeout = (cb: () => void, ms: number) => {
|
|
513
|
+
if (ms === 2000) {
|
|
514
|
+
capturedCallback = cb;
|
|
515
|
+
return 123;
|
|
516
|
+
}
|
|
517
|
+
return originalSetTimeout(cb, ms);
|
|
518
|
+
};
|
|
519
|
+
try {
|
|
520
|
+
mockProcess.stdin.write = () => true;
|
|
521
|
+
const stopPromise = worker.stop();
|
|
522
|
+
if (capturedCallback) {
|
|
523
|
+
capturedCallback();
|
|
524
|
+
}
|
|
525
|
+
await stopPromise;
|
|
526
|
+
assert.strictEqual((worker as any).pending.size, 0);
|
|
527
|
+
} finally {
|
|
528
|
+
global.setTimeout = originalSetTimeout;
|
|
529
|
+
}
|
|
530
|
+
});
|
|
531
|
+
|
|
532
|
+
it("stop() unconditionally resets stopping and never kills a replacement process", async () => {
|
|
533
|
+
const { worker, mockProcess } = createMockedWorker();
|
|
534
|
+
const originalSetTimeout = global.setTimeout;
|
|
535
|
+
let capturedCallback: (() => void) | undefined;
|
|
536
|
+
(global as any).setTimeout = (cb: () => void, ms: number) => {
|
|
537
|
+
if (ms === 2000) {
|
|
538
|
+
capturedCallback = cb;
|
|
539
|
+
return 123;
|
|
540
|
+
}
|
|
541
|
+
return originalSetTimeout(cb, ms);
|
|
542
|
+
};
|
|
543
|
+
try {
|
|
544
|
+
mockProcess.stdin.write = () => true;
|
|
545
|
+
const stopPromise = worker.stop();
|
|
546
|
+
const newProc = { kill: () => {} };
|
|
547
|
+
let newProcKilled = false;
|
|
548
|
+
newProc.kill = () => { newProcKilled = true; };
|
|
549
|
+
(worker as any).process = newProc;
|
|
550
|
+
if (capturedCallback) {
|
|
551
|
+
capturedCallback();
|
|
552
|
+
}
|
|
553
|
+
await stopPromise;
|
|
554
|
+
assert.ok(!((worker as any).stopping));
|
|
555
|
+
assert.ok(!(newProcKilled));
|
|
556
|
+
assert.strictEqual((worker as any).process, newProc);
|
|
557
|
+
} finally {
|
|
558
|
+
global.setTimeout = originalSetTimeout;
|
|
559
|
+
}
|
|
560
|
+
});
|
|
561
|
+
});
|
|
562
|
+
});
|