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,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unit tests for pi-web retry module: transient-only policy, clamp bounds.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { describe, it, beforeEach, afterEach } from "node:test";
|
|
6
|
+
import assert from "node:assert/strict";
|
|
7
|
+
import { HttpError, withRetry, signalWithTimeout } from "../lib/retry";
|
|
8
|
+
|
|
9
|
+
describe("withRetry transient policy", () => {
|
|
10
|
+
it("does not retry 401 (surfaces on first attempt)", async () => {
|
|
11
|
+
let calls = 0;
|
|
12
|
+
const fn = async (): Promise<never> => {
|
|
13
|
+
calls++;
|
|
14
|
+
throw new HttpError(401, "Unauthorized", "");
|
|
15
|
+
};
|
|
16
|
+
let caught: unknown;
|
|
17
|
+
try {
|
|
18
|
+
await withRetry(fn, 3);
|
|
19
|
+
} catch (e) {
|
|
20
|
+
caught = e;
|
|
21
|
+
}
|
|
22
|
+
assert.ok(caught instanceof HttpError);
|
|
23
|
+
assert.equal(calls, 1);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("does not retry 403", async () => {
|
|
27
|
+
let calls = 0;
|
|
28
|
+
const fn = async (): Promise<never> => {
|
|
29
|
+
calls++;
|
|
30
|
+
throw new HttpError(403, "Forbidden", "");
|
|
31
|
+
};
|
|
32
|
+
let caught: unknown;
|
|
33
|
+
try {
|
|
34
|
+
await withRetry(fn, 3);
|
|
35
|
+
} catch (e) {
|
|
36
|
+
caught = e;
|
|
37
|
+
}
|
|
38
|
+
assert.ok(caught instanceof HttpError);
|
|
39
|
+
assert.equal(calls, 1);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it("retries 408 (Request Timeout) then succeeds", async () => {
|
|
43
|
+
let calls = 0;
|
|
44
|
+
const fn = async (): Promise<string> => {
|
|
45
|
+
if (calls++ === 0) throw new HttpError(408, "Request Timeout", "");
|
|
46
|
+
return "ok";
|
|
47
|
+
};
|
|
48
|
+
assert.equal(await withRetry(fn, 3), "ok");
|
|
49
|
+
assert.equal(calls, 2);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it("retries 429 then succeeds", async () => {
|
|
53
|
+
let calls = 0;
|
|
54
|
+
const fn = async (): Promise<string> => {
|
|
55
|
+
if (calls++ === 0) throw new HttpError(429, "Too Many Requests", "");
|
|
56
|
+
return "ok";
|
|
57
|
+
};
|
|
58
|
+
assert.equal(await withRetry(fn, 3), "ok");
|
|
59
|
+
assert.equal(calls, 2);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("retries 500 then succeeds", async () => {
|
|
63
|
+
let calls = 0;
|
|
64
|
+
const fn = async (): Promise<string> => {
|
|
65
|
+
if (calls++ === 0) throw new HttpError(500, "Server Error", "");
|
|
66
|
+
return "ok";
|
|
67
|
+
};
|
|
68
|
+
assert.equal(await withRetry(fn, 3), "ok");
|
|
69
|
+
assert.equal(calls, 2);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("retries statusless network errors (generic Error)", async () => {
|
|
73
|
+
let calls = 0;
|
|
74
|
+
const fn = async (): Promise<string> => {
|
|
75
|
+
if (calls++ === 0) throw new Error("fetch failed");
|
|
76
|
+
return "ok";
|
|
77
|
+
};
|
|
78
|
+
assert.equal(await withRetry(fn, 3), "ok");
|
|
79
|
+
assert.equal(calls, 2);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it("throws the last error after exhausting attempts on 500", async () => {
|
|
83
|
+
let calls = 0;
|
|
84
|
+
const fn = async (): Promise<never> => {
|
|
85
|
+
calls++;
|
|
86
|
+
throw new HttpError(500, "Server Error", "");
|
|
87
|
+
};
|
|
88
|
+
let caught: unknown;
|
|
89
|
+
try {
|
|
90
|
+
await withRetry(fn, 3);
|
|
91
|
+
} catch (e) {
|
|
92
|
+
caught = e;
|
|
93
|
+
}
|
|
94
|
+
assert.ok(caught instanceof HttpError);
|
|
95
|
+
assert.equal(calls, 3);
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
describe("signalWithTimeout clamp", () => {
|
|
100
|
+
it("clamps sub-1000ms up to 1000ms (abort fires after >=1s, not instantly)", () => {
|
|
101
|
+
const s = signalWithTimeout(50);
|
|
102
|
+
let abortedAt = 0;
|
|
103
|
+
s.addEventListener("abort", () => (abortedAt = Date.now()));
|
|
104
|
+
const start = Date.now();
|
|
105
|
+
return new Promise<void>((resolve) => setTimeout(resolve, 1100)).then(() => {
|
|
106
|
+
assert.ok((abortedAt) >= (start + 1000));
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it("clamps huge timeouts down to 600000ms (not aborted immediately)", () => {
|
|
111
|
+
const s = signalWithTimeout(3_600_000);
|
|
112
|
+
assert.equal(s.aborted, false);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it("passes in-range values through", () => {
|
|
116
|
+
const s = signalWithTimeout(5000);
|
|
117
|
+
assert.equal(s.aborted, false);
|
|
118
|
+
});
|
|
119
|
+
});
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unit tests for web_screenshot's tool-result shape.
|
|
3
|
+
*
|
|
4
|
+
* The screenshot must come back as a real inline image block (ImageContent)
|
|
5
|
+
* so multimodal models can see it — not as base64 text. Regression guard for
|
|
6
|
+
* pi-web 0.6.2; the HTTP layer is stubbed so no Crawl4AI daemon is needed.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { describe, it, beforeEach, afterEach } from "node:test";
|
|
10
|
+
import assert from "node:assert/strict";
|
|
11
|
+
import piWebExtension from "../index";
|
|
12
|
+
|
|
13
|
+
// The web settings layer reads the agent-dir settings.json — isolate the
|
|
14
|
+
// agent dir so a host `web` section never leaks into these tests.
|
|
15
|
+
process.env.PI_CODING_AGENT_DIR = "/nonexistent-pi-agent-dir-for-web-tests";
|
|
16
|
+
|
|
17
|
+
// chai-compat helpers (assert-based)
|
|
18
|
+
function includes(haystack: unknown, needle: unknown): boolean {
|
|
19
|
+
if (typeof haystack === "string") return haystack.includes(String(needle));
|
|
20
|
+
if (Array.isArray(haystack)) return (haystack as unknown[]).some((v) => {
|
|
21
|
+
if (typeof v === "string" && typeof needle === "string") return v.includes(needle);
|
|
22
|
+
if (v === needle) return true;
|
|
23
|
+
// chai's to.include on an array does DEEP membership for objects.
|
|
24
|
+
if (v && needle && typeof v === "object" && typeof needle === "object") {
|
|
25
|
+
try { assert.deepStrictEqual(v, needle); return true; } catch { return false; }
|
|
26
|
+
}
|
|
27
|
+
return false;
|
|
28
|
+
});
|
|
29
|
+
// chai's to.include on an OBJECT target: needle's properties are a subset.
|
|
30
|
+
if (haystack && typeof haystack === "object" && needle && typeof needle === "object") {
|
|
31
|
+
return deepIncludes(haystack, needle as Record<string, unknown>);
|
|
32
|
+
}
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
function lengthOf(v: unknown): number {
|
|
36
|
+
if (Array.isArray(v)) return v.length;
|
|
37
|
+
if (typeof v === "string") return v.length;
|
|
38
|
+
if (v && typeof v === "object" && "length" in (v as Record<string, unknown>)) return Number((v as Record<string, unknown>).length);
|
|
39
|
+
if (v && typeof v === "object" && "size" in (v as Record<string, unknown>)) return Number((v as Record<string, unknown>).size);
|
|
40
|
+
throw new Error("lengthOf: value has no length/size");
|
|
41
|
+
}
|
|
42
|
+
/** Deep "includes" — every own key of `part` must exist on `obj` with a
|
|
43
|
+
* deep-equal value (chai's to.deep.include for object subjects). */
|
|
44
|
+
function deepIncludes(obj: unknown, part: Record<string, unknown>): boolean {
|
|
45
|
+
if (Array.isArray(obj)) return includes(obj, part); // deep membership
|
|
46
|
+
if (typeof obj !== "object" || obj === null) return false;
|
|
47
|
+
return Object.entries(part).every(([k, v]) => {
|
|
48
|
+
try { assert.deepStrictEqual((obj as Record<string, unknown>)[k], v); return true; } catch { return false; }
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function harness(): Record<string, any> {
|
|
53
|
+
const tools: Record<string, any> = {};
|
|
54
|
+
const pi: any = {
|
|
55
|
+
registerTool(tool: any) { tools[tool.name] = tool; },
|
|
56
|
+
on() {},
|
|
57
|
+
};
|
|
58
|
+
piWebExtension(pi);
|
|
59
|
+
return tools;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const BASE_RESPONSE = { success: true, url: "https://example.com", mime: "image/png", size: 3 };
|
|
63
|
+
|
|
64
|
+
async function runScreenshot(payload: Record<string, unknown>) {
|
|
65
|
+
const orig = globalThis.fetch;
|
|
66
|
+
globalThis.fetch = (async () =>
|
|
67
|
+
({ ok: true, status: 200, text: async () => JSON.stringify(payload) })) as any;
|
|
68
|
+
try {
|
|
69
|
+
const tools = harness();
|
|
70
|
+
return await tools.web_screenshot.execute(
|
|
71
|
+
"t1", { url: "https://example.com" }, new AbortController().signal, undefined, {},
|
|
72
|
+
);
|
|
73
|
+
} finally {
|
|
74
|
+
globalThis.fetch = orig;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
describe("web_screenshot tool result shape", () => {
|
|
79
|
+
it("returns the PNG as an inline image block, not base64 text", async () => {
|
|
80
|
+
const result = await runScreenshot({ ...BASE_RESPONSE, screenshot: "QUJD" });
|
|
81
|
+
const image = result.content.find((c: any) => c.type === "image");
|
|
82
|
+
assert.deepEqual(image, { type: "image", data: "QUJD", mimeType: "image/png" });
|
|
83
|
+
assert.equal(result.content[0].type, "text");
|
|
84
|
+
assert.ok(!includes(result.content[0].text, "base64"));
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it("returns text-only when no screenshot comes back", async () => {
|
|
88
|
+
const result = await runScreenshot({ ...BASE_RESPONSE });
|
|
89
|
+
assert.equal(lengthOf(result.content), 1);
|
|
90
|
+
assert.equal(result.content[0].type, "text");
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it("falls back to image/png when the response has no mime", async () => {
|
|
94
|
+
const result = await runScreenshot({ success: true, url: "https://example.com", screenshot: "QUJD" });
|
|
95
|
+
const image = result.content.find((c: any) => c.type === "image");
|
|
96
|
+
assert.deepEqual(image, { type: "image", data: "QUJD", mimeType: "image/png" });
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it("surfaces daemon success:false as a loud error", async () => {
|
|
100
|
+
let err: any = null;
|
|
101
|
+
try {
|
|
102
|
+
await runScreenshot({ success: false, error_message: "nav failed" });
|
|
103
|
+
} catch (e) {
|
|
104
|
+
err = e;
|
|
105
|
+
}
|
|
106
|
+
assert.ok(err);
|
|
107
|
+
assert.match(String(String(err?.message)), /nav failed/);
|
|
108
|
+
});
|
|
109
|
+
});
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import { describe, it, beforeEach, afterEach } from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
searchWithDiagnostics,
|
|
6
|
+
selectSearchBackendOrder,
|
|
7
|
+
type SearchParams,
|
|
8
|
+
type SearchResult,
|
|
9
|
+
} from "../lib/search";
|
|
10
|
+
|
|
11
|
+
// The web settings layer reads the agent-dir settings.json — isolate the
|
|
12
|
+
// agent dir so a host `web` section never leaks into these tests.
|
|
13
|
+
process.env.PI_CODING_AGENT_DIR = "/nonexistent-pi-agent-dir-for-web-tests";
|
|
14
|
+
|
|
15
|
+
// chai-compat helpers (assert-based)
|
|
16
|
+
function includes(haystack: unknown, needle: unknown): boolean {
|
|
17
|
+
if (typeof haystack === "string") return haystack.includes(String(needle));
|
|
18
|
+
if (Array.isArray(haystack)) return (haystack as unknown[]).some((v) => {
|
|
19
|
+
if (typeof v === "string" && typeof needle === "string") return v.includes(needle);
|
|
20
|
+
if (v === needle) return true;
|
|
21
|
+
// chai's to.include on an array does DEEP membership for objects.
|
|
22
|
+
if (v && needle && typeof v === "object" && typeof needle === "object") {
|
|
23
|
+
try { assert.deepStrictEqual(v, needle); return true; } catch { return false; }
|
|
24
|
+
}
|
|
25
|
+
return false;
|
|
26
|
+
});
|
|
27
|
+
// chai's to.include on an OBJECT target: needle's properties are a subset.
|
|
28
|
+
if (haystack && typeof haystack === "object" && needle && typeof needle === "object") {
|
|
29
|
+
return deepIncludes(haystack, needle as Record<string, unknown>);
|
|
30
|
+
}
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
function lengthOf(v: unknown): number {
|
|
34
|
+
if (Array.isArray(v)) return v.length;
|
|
35
|
+
if (typeof v === "string") return v.length;
|
|
36
|
+
if (v && typeof v === "object" && "length" in (v as Record<string, unknown>)) return Number((v as Record<string, unknown>).length);
|
|
37
|
+
if (v && typeof v === "object" && "size" in (v as Record<string, unknown>)) return Number((v as Record<string, unknown>).size);
|
|
38
|
+
throw new Error("lengthOf: value has no length/size");
|
|
39
|
+
}
|
|
40
|
+
/** Deep "includes" — every own key of `part` must exist on `obj` with a
|
|
41
|
+
* deep-equal value (chai's to.deep.include for object subjects). */
|
|
42
|
+
function deepIncludes(obj: unknown, part: Record<string, unknown>): boolean {
|
|
43
|
+
if (Array.isArray(obj)) return includes(obj, part); // deep membership
|
|
44
|
+
if (typeof obj !== "object" || obj === null) return false;
|
|
45
|
+
return Object.entries(part).every(([k, v]) => {
|
|
46
|
+
try { assert.deepStrictEqual((obj as Record<string, unknown>)[k], v); return true; } catch { return false; }
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const ORIGINAL_ENV = { ...process.env };
|
|
51
|
+
const ORIGINAL_FETCH = globalThis.fetch;
|
|
52
|
+
|
|
53
|
+
function jsonResponse(body: unknown, status = 200): Response {
|
|
54
|
+
return new Response(JSON.stringify(body), {
|
|
55
|
+
status,
|
|
56
|
+
headers: { "content-type": "application/json" },
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function htmlResponse(body: string, status = 200): Response {
|
|
61
|
+
return new Response(body, {
|
|
62
|
+
status,
|
|
63
|
+
headers: { "content-type": "text/html" },
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function installMockFetch(handler: (url: string, init?: RequestInit) => Response | Promise<Response>): string[] {
|
|
68
|
+
const calls: string[] = [];
|
|
69
|
+
globalThis.fetch = (async (input: RequestInfo | URL, init?: RequestInit) => {
|
|
70
|
+
const url = String(input);
|
|
71
|
+
calls.push(url);
|
|
72
|
+
return handler(url, init);
|
|
73
|
+
}) as typeof fetch;
|
|
74
|
+
return calls;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function restoreEnv(): void {
|
|
78
|
+
process.env = { ...ORIGINAL_ENV };
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
describe("SearchParams and SearchResult types", () => {
|
|
82
|
+
it("accept expected fields", () => {
|
|
83
|
+
const params: SearchParams = { query: "test", backend: "auto", signal: new AbortController().signal };
|
|
84
|
+
const result: SearchResult = { title: "T", url: "https://example.com", snippet: "S", age: "", content: "", backend: "brave" };
|
|
85
|
+
assert.equal(params.backend, "auto");
|
|
86
|
+
assert.equal(result.backend, "brave");
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
describe("selectSearchBackendOrder", () => {
|
|
91
|
+
it("selects Brave first for include_content", () => {
|
|
92
|
+
assert.deepEqual(selectSearchBackendOrder({ query: "homelab ansible", include_content: true }), ["brave", "searxng", "firecrawl"]);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it("selects Brave first for site: and precision queries", () => {
|
|
96
|
+
assert.deepEqual(selectSearchBackendOrder({ query: "site:docs.ansible.com podman quadlet" }), ["brave", "searxng", "firecrawl"]);
|
|
97
|
+
assert.deepEqual(selectSearchBackendOrder({ query: '"exact phrase" release notes' }), ["brave", "searxng", "firecrawl"]);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it("keeps SearXNG first for broad discovery and explicit engines", () => {
|
|
101
|
+
assert.deepEqual(selectSearchBackendOrder({ query: "homelab ansible ideas" }), ["searxng", "brave", "firecrawl"]);
|
|
102
|
+
assert.deepEqual(selectSearchBackendOrder({ query: "docs api", engines: "google,github" }), ["searxng", "brave", "firecrawl"]);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it("honors explicit backend", () => {
|
|
106
|
+
assert.deepEqual(selectSearchBackendOrder({ query: "test", backend: "firecrawl" }), ["firecrawl"]);
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
describe("searchWithDiagnostics", () => {
|
|
111
|
+
beforeEach(() => {
|
|
112
|
+
restoreEnv();
|
|
113
|
+
process.env.BRAVE_API_KEY = "test-brave-key";
|
|
114
|
+
process.env.SEARXNG_BASE_URL = "http://searxng.test";
|
|
115
|
+
process.env.FIRECRAWL_API_URL = "http://firecrawl.test/v2";
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
afterEach(() => {
|
|
119
|
+
globalThis.fetch = ORIGINAL_FETCH;
|
|
120
|
+
restoreEnv();
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it("uses Brave first for include_content and fetches inline content", async () => {
|
|
124
|
+
const calls = installMockFetch((url) => {
|
|
125
|
+
if (url.startsWith("https://api.search.brave.com/")) {
|
|
126
|
+
return jsonResponse({ web: { results: [{ title: "Brave", url: "https://example.com/page", description: "Snippet" }] } });
|
|
127
|
+
}
|
|
128
|
+
if (url === "https://example.com/page") {
|
|
129
|
+
return htmlResponse("<html><head><title>Page</title></head><body><main><h1>Page</h1><p>This is long enough readable content for the inline content fetch test.</p></main></body></html>");
|
|
130
|
+
}
|
|
131
|
+
throw new Error(`unexpected fetch ${url}`);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
const result = await searchWithDiagnostics({ query: "homelab ansible", include_content: true });
|
|
135
|
+
assert.equal(result.selectedBackend, "brave");
|
|
136
|
+
assert.equal(result.backendOrder[0], "brave");
|
|
137
|
+
assert.ok(includes(result.results[0].content, "readable content"));
|
|
138
|
+
assert.ok(includes(calls[0], "api.search.brave.com"));
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it("uses SearXNG first for broad discovery", async () => {
|
|
142
|
+
const calls = installMockFetch((url) => {
|
|
143
|
+
if (url.startsWith("http://searxng.test/search")) {
|
|
144
|
+
return jsonResponse({ results: [{ title: "SearXNG", url: "https://example.com", content: "Snippet" }] });
|
|
145
|
+
}
|
|
146
|
+
throw new Error(`unexpected fetch ${url}`);
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
const result = await searchWithDiagnostics({ query: "homelab ansible ideas" });
|
|
150
|
+
assert.equal(result.selectedBackend, "searxng");
|
|
151
|
+
assert.ok(deepIncludes(result.attempts, { backend: "searxng", status: "success", message: "Selected searxng", resultCount: 1 }));
|
|
152
|
+
assert.ok(includes(calls[0], "searxng.test"));
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it("forwards freshness pw/pm/py to SearXNG as time_range", async () => {
|
|
156
|
+
const calls = installMockFetch((url) => {
|
|
157
|
+
if (url.startsWith("http://searxng.test/search")) {
|
|
158
|
+
return jsonResponse({ results: [{ title: "SearXNG", url: "https://example.com", content: "Snippet" }] });
|
|
159
|
+
}
|
|
160
|
+
throw new Error(`unexpected fetch ${url}`);
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
const result = await searchWithDiagnostics({ query: "homelab ansible ideas", freshness: "pw" });
|
|
164
|
+
assert.equal(result.selectedBackend, "searxng");
|
|
165
|
+
assert.ok(includes(calls[0], "time_range=pw"));
|
|
166
|
+
|
|
167
|
+
// Explicit date ranges are Brave-only — no time_range on the SearXNG call.
|
|
168
|
+
const calls2 = installMockFetch((url) => {
|
|
169
|
+
if (url.startsWith("http://searxng.test/search")) {
|
|
170
|
+
return jsonResponse({ results: [] });
|
|
171
|
+
}
|
|
172
|
+
if (url.startsWith("https://api.search.brave.com/")) {
|
|
173
|
+
return jsonResponse({ web: { results: [] } });
|
|
174
|
+
}
|
|
175
|
+
throw new Error(`unexpected fetch ${url}`);
|
|
176
|
+
});
|
|
177
|
+
await searchWithDiagnostics({ query: "homelab ansible ideas", freshness: "2026-01-01to2026-01-31" });
|
|
178
|
+
assert.ok(!includes(calls2[0], "time_range="));
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
it("captures backend errors and falls through to Firecrawl last", async () => {
|
|
182
|
+
installMockFetch((url) => {
|
|
183
|
+
if (url.startsWith("http://searxng.test/search")) return jsonResponse({ results: [] });
|
|
184
|
+
if (url.startsWith("https://api.search.brave.com/")) return jsonResponse({ error: "rate limited" }, 429);
|
|
185
|
+
if (url.startsWith("http://firecrawl.test/v2/search")) {
|
|
186
|
+
return jsonResponse({ data: { web: [{ title: "Fire", url: "https://fire.example", description: "Fallback" }] } });
|
|
187
|
+
}
|
|
188
|
+
throw new Error(`unexpected fetch ${url}`);
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
const result = await searchWithDiagnostics({ query: "homelab ansible ideas" });
|
|
192
|
+
assert.equal(result.selectedBackend, "firecrawl");
|
|
193
|
+
assert.deepEqual(result.attempts.map((a) => a.backend), ["searxng", "brave", "firecrawl"]);
|
|
194
|
+
assert.equal(result.attempts[1].status, "error");
|
|
195
|
+
assert.ok(!includes(result.attempts[1].message, "test-brave-key"));
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
it("explicit backend tries only that backend", async () => {
|
|
199
|
+
const calls = installMockFetch((url) => {
|
|
200
|
+
if (url.startsWith("http://searxng.test/search")) return jsonResponse({ results: [] });
|
|
201
|
+
throw new Error(`unexpected fetch ${url}`);
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
const result = await searchWithDiagnostics({ query: "nothing", backend: "searxng" });
|
|
205
|
+
assert.deepEqual(result.results, []);
|
|
206
|
+
assert.ok(deepIncludes(result.attempts, { backend: "searxng", status: "empty", message: "searxng returned 0 results", resultCount: 0 }));
|
|
207
|
+
assert.equal(lengthOf(calls), 1);
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
});
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
// The web settings layer: env > trusted project > global > default.
|
|
2
|
+
// Uses temp agent dirs / temp settings files; no network.
|
|
3
|
+
|
|
4
|
+
import { describe, it, beforeEach, afterEach } from "node:test";
|
|
5
|
+
import assert from "node:assert/strict";
|
|
6
|
+
import fs from "node:fs";
|
|
7
|
+
import os from "node:os";
|
|
8
|
+
import path from "node:path";
|
|
9
|
+
|
|
10
|
+
import { lookupVar, readWebSettings, writeWebSection, ENV_TO_SETTINGS_KEY } from "../lib/settings";
|
|
11
|
+
|
|
12
|
+
// The settings layer reads agentDirs() from the shared registry, which honors
|
|
13
|
+
// PI_CODING_AGENT_DIR — point it at a temp dir per test.
|
|
14
|
+
let tmpDir = "";
|
|
15
|
+
let agentDir = "";
|
|
16
|
+
let projectDir = "";
|
|
17
|
+
|
|
18
|
+
function setup(): void {
|
|
19
|
+
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "web-settings-"));
|
|
20
|
+
agentDir = path.join(tmpDir, "agent");
|
|
21
|
+
projectDir = path.join(tmpDir, "project");
|
|
22
|
+
fs.mkdirSync(agentDir, { recursive: true });
|
|
23
|
+
fs.mkdirSync(projectDir, { recursive: true });
|
|
24
|
+
process.env.PI_CODING_AGENT_DIR = agentDir;
|
|
25
|
+
// Trust: the real trust.json walk won't see our temp project → untrusted by
|
|
26
|
+
// default. isProjectTrusted(cwd, dirs) is injected nowhere here, so trust
|
|
27
|
+
// tests go through writeWebSection/readWebSettings with explicit trusted
|
|
28
|
+
// flags where the API allows, and lookupVar's project layer is exercised by
|
|
29
|
+
// planting a trust.json for the real walk.
|
|
30
|
+
projectDir = fs.realpathSync(projectDir); // trust.json stores canonical paths
|
|
31
|
+
fs.writeFileSync(
|
|
32
|
+
path.join(agentDir, "trust.json"),
|
|
33
|
+
JSON.stringify({ [projectDir]: true }),
|
|
34
|
+
"utf8",
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function teardown(): void {
|
|
39
|
+
delete process.env.PI_CODING_AGENT_DIR;
|
|
40
|
+
fs.rmSync(tmpDir, { recursive: true, force: true });
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function writeGlobal(web: Record<string, unknown>): void {
|
|
44
|
+
writeWebSection(web, path.join(agentDir, "settings.json"));
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
describe("web settings layer", () => {
|
|
48
|
+
beforeEach(setup);
|
|
49
|
+
afterEach(teardown);
|
|
50
|
+
|
|
51
|
+
it("every mapped env var points at a settings key", () => {
|
|
52
|
+
assert.equal(Object.keys(ENV_TO_SETTINGS_KEY).length, 16);
|
|
53
|
+
for (const key of Object.values(ENV_TO_SETTINGS_KEY)) {
|
|
54
|
+
assert.ok(key.startsWith("searxng.") || key.startsWith("brave.") || key.startsWith("firecrawl.")
|
|
55
|
+
|| key.startsWith("crawl4ai.") || key.startsWith("gemini.") || key.startsWith("image.")
|
|
56
|
+
|| key.startsWith("chat."), key);
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("env wins over settings", () => {
|
|
61
|
+
process.env.BRAVE_API_KEY = "from-env";
|
|
62
|
+
writeGlobal({ brave: { apiKey: "from-settings" } });
|
|
63
|
+
try {
|
|
64
|
+
const r = lookupVar("BRAVE_API_KEY", projectDir, false);
|
|
65
|
+
assert.equal(r.value, "from-env");
|
|
66
|
+
assert.equal(r.source, "env");
|
|
67
|
+
} finally {
|
|
68
|
+
delete process.env.BRAVE_API_KEY;
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("global settings section resolves when env is unset", () => {
|
|
73
|
+
writeGlobal({ brave: { apiKey: "from-settings" } });
|
|
74
|
+
const r = lookupVar("BRAVE_API_KEY", projectDir, false);
|
|
75
|
+
assert.equal(r.value, "from-settings");
|
|
76
|
+
assert.equal(r.source, "global settings");
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it("trusted project settings shadow global per field", () => {
|
|
80
|
+
writeGlobal({ brave: { apiKey: "global" }, chat: { baseUrl: "https://global" } });
|
|
81
|
+
fs.mkdirSync(path.join(projectDir, ".pi"), { recursive: true });
|
|
82
|
+
fs.writeFileSync(
|
|
83
|
+
path.join(projectDir, ".pi", "settings.json"),
|
|
84
|
+
JSON.stringify({ web: { "brave.apiKey": "project" } }),
|
|
85
|
+
"utf8",
|
|
86
|
+
);
|
|
87
|
+
const r = lookupVar("BRAVE_API_KEY", projectDir, true);
|
|
88
|
+
assert.equal(r.value, "project");
|
|
89
|
+
assert.equal(r.source, "project settings");
|
|
90
|
+
// includeCwd=false → project layer skipped, global serves
|
|
91
|
+
const g = lookupVar("BRAVE_API_KEY", projectDir, false);
|
|
92
|
+
assert.equal(g.value, "global");
|
|
93
|
+
// untouched field still comes from global
|
|
94
|
+
const c = lookupVar("WEB_CHAT_API_BASE_URL", projectDir, true);
|
|
95
|
+
assert.equal(c.value, "https://global");
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it("untrusted project file is ignored (fail closed)", () => {
|
|
99
|
+
writeGlobal({ brave: { apiKey: "global" } });
|
|
100
|
+
fs.rmSync(path.join(agentDir, "trust.json"));
|
|
101
|
+
fs.mkdirSync(path.join(projectDir, ".pi"), { recursive: true });
|
|
102
|
+
fs.writeFileSync(
|
|
103
|
+
path.join(projectDir, ".pi", "settings.json"),
|
|
104
|
+
JSON.stringify({ web: { brave: { apiKey: "project" } } }),
|
|
105
|
+
"utf8",
|
|
106
|
+
);
|
|
107
|
+
const r = lookupVar("BRAVE_API_KEY", projectDir, true);
|
|
108
|
+
assert.equal(r.value, "global");
|
|
109
|
+
assert.equal(r.source, "global settings");
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it("unset everywhere → empty source (caller falls through to default)", () => {
|
|
113
|
+
const r = lookupVar("FIRECRAWL_API_KEY", projectDir, true);
|
|
114
|
+
assert.equal(r.value, undefined);
|
|
115
|
+
assert.equal(r.source, "");
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it("vars outside the map are env-only (no settings fallback)", () => {
|
|
119
|
+
writeGlobal({ searxng: { baseUrl: "https://settings" } } as never);
|
|
120
|
+
const r = lookupVar("GEMINI_WEB_COOKIE_STORE", projectDir, true);
|
|
121
|
+
assert.equal(r.value, undefined);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it("readWebSettings merges global ⊕ trusted project", () => {
|
|
125
|
+
writeGlobal({ brave: { apiKey: "g" }, chat: { baseUrl: "https://g" } });
|
|
126
|
+
fs.mkdirSync(path.join(projectDir, ".pi"), { recursive: true });
|
|
127
|
+
fs.writeFileSync(path.join(projectDir, ".pi", "settings.json"), JSON.stringify({ web: { chat: { baseUrl: "https://p" } } }));
|
|
128
|
+
const merged = readWebSettings(projectDir, true);
|
|
129
|
+
assert.equal(merged.brave && (merged.brave as Record<string, unknown>).apiKey, "g");
|
|
130
|
+
assert.equal(merged.chat && (merged.chat as Record<string, unknown>).baseUrl, "https://p");
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it("writeWebSection patches only given keys and clears empty values", () => {
|
|
134
|
+
writeGlobal({ brave: { apiKey: "old", } });
|
|
135
|
+
writeWebSection({ "brave.apiKey": "new", "chat.apiKey": "" }, path.join(agentDir, "settings.json"));
|
|
136
|
+
const raw = JSON.parse(fs.readFileSync(path.join(agentDir, "settings.json"), "utf8"));
|
|
137
|
+
assert.equal(raw.web["brave.apiKey"], "new");
|
|
138
|
+
assert.ok(!("chat.apiKey" in raw.web));
|
|
139
|
+
// key paths are FLAT (panel keys) — writeWebSection stores the panel key as-is
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it("writeWebSection removes the web section when it becomes empty", () => {
|
|
143
|
+
writeGlobal({ "brave.apiKey": "old" });
|
|
144
|
+
writeWebSection({ "brave.apiKey": "" }, path.join(agentDir, "settings.json"));
|
|
145
|
+
const raw = JSON.parse(fs.readFileSync(path.join(agentDir, "settings.json"), "utf8"));
|
|
146
|
+
assert.ok(!("web" in raw));
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it("writeWebSection preserves unrelated settings and other modules' sections", () => {
|
|
150
|
+
const file = path.join(agentDir, "settings.json");
|
|
151
|
+
fs.writeFileSync(file, JSON.stringify({ ceulen: { disabled: [] }, other: 1, web: { "chat.apiKey": "k" } }));
|
|
152
|
+
writeWebSection({ "chat.apiKey": "k2" }, file);
|
|
153
|
+
const raw = JSON.parse(fs.readFileSync(file, "utf8"));
|
|
154
|
+
assert.deepEqual(raw.ceulen, { disabled: [] });
|
|
155
|
+
assert.equal(raw.other, 1);
|
|
156
|
+
assert.equal(raw.web["chat.apiKey"], "k2");
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it("writeWebSection refuses to clobber a corrupt file", () => {
|
|
160
|
+
const file = path.join(agentDir, "settings.json");
|
|
161
|
+
fs.writeFileSync(file, "{ not json");
|
|
162
|
+
assert.throws(() => writeWebSection({ "brave.apiKey": "x" }, file), /not valid JSON/);
|
|
163
|
+
assert.equal(fs.readFileSync(file, "utf8"), "{ not json");
|
|
164
|
+
});
|
|
165
|
+
});
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Regression guard for skill frontmatter (0.16.1 incident): pi refuses to
|
|
3
|
+
* load a skill whose description exceeds 1024 characters, so an oversized
|
|
4
|
+
* description is a load error on every session — not a cosmetic issue.
|
|
5
|
+
* Every skills/<name>/SKILL.md must parse and stay within the limit.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { describe, it } from "node:test";
|
|
9
|
+
import assert from "node:assert/strict";
|
|
10
|
+
import { readFileSync } from "node:fs";
|
|
11
|
+
import path from "node:path";
|
|
12
|
+
import { fileURLToPath } from "node:url";
|
|
13
|
+
|
|
14
|
+
const SKILLS_DIR = path.join(path.dirname(fileURLToPath(import.meta.url)), "..", "..", "..", "..", "skills", "web");
|
|
15
|
+
|
|
16
|
+
/** Minimal frontmatter parser — single-line values plus `>` / `|` folded blocks. */
|
|
17
|
+
function frontmatter(source: string): Record<string, string> {
|
|
18
|
+
const m = /^---\n([\s\S]*?)\n---/.exec(source);
|
|
19
|
+
if (!m) return {};
|
|
20
|
+
const out: Record<string, string> = {};
|
|
21
|
+
const lines = m[1].split("\n");
|
|
22
|
+
for (let i = 0; i < lines.length; i++) {
|
|
23
|
+
const kv = /^([\w][\w-]*):\s?(.*)$/.exec(lines[i]);
|
|
24
|
+
if (!kv) continue;
|
|
25
|
+
const [, key, inline] = kv;
|
|
26
|
+
if (/^[>|][+-]?$/.test(inline.trim())) {
|
|
27
|
+
const body: string[] = [];
|
|
28
|
+
for (i += 1; i < lines.length && (/^\s/.test(lines[i]) || lines[i] === ""); i++) {
|
|
29
|
+
if (lines[i].trim()) body.push(lines[i].trim());
|
|
30
|
+
}
|
|
31
|
+
out[key] = body.join(" ");
|
|
32
|
+
i -= 1;
|
|
33
|
+
} else {
|
|
34
|
+
out[key] = inline.trim();
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return out;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
describe("web skill frontmatter", () => {
|
|
41
|
+
const skillFiles = [path.join(SKILLS_DIR, "SKILL.md")].filter((p) => {
|
|
42
|
+
try {
|
|
43
|
+
readFileSync(p);
|
|
44
|
+
return true;
|
|
45
|
+
} catch {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it("discovers at least one skill", () => {
|
|
51
|
+
assert.ok((skillFiles.length) >= (1));
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
for (const file of skillFiles) {
|
|
55
|
+
const skillName = path.basename(path.dirname(file));
|
|
56
|
+
|
|
57
|
+
it(`${skillName}: has a name and description`, () => {
|
|
58
|
+
const fm = frontmatter(readFileSync(file, "utf8"));
|
|
59
|
+
assert.match(String(fm.name), /^[a-z0-9-]+$/);
|
|
60
|
+
assert.equal(typeof fm.description, 'string');
|
|
61
|
+
assert.ok(fm.description.length >= 20);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it(`${skillName}: description fits pi's 1024-char load limit`, () => {
|
|
65
|
+
// pi folds folded blocks to single-line whitespace before measuring.
|
|
66
|
+
const fm = frontmatter(readFileSync(file, "utf8"));
|
|
67
|
+
const folded = fm.description.split(/\s+/).join(" ").trim();
|
|
68
|
+
// The 0.16.0 incident: 1107 chars => [Skill conflicts] on every session.
|
|
69
|
+
assert.ok(folded.length <= 1024, `${skillName} description is ${folded.length} chars`);
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
});
|