ceulen 0.5.1 → 0.8.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 +365 -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.test.ts +67 -0
- package/extensions/lib/registry.ts +104 -8
- 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 +235 -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 +288 -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 +649 -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 +108 -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 +186 -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/configPanel.ts +24 -10
- package/extensions/modules/router/lib/client.ts +63 -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 +260 -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/rules/index.ts +120 -0
- package/extensions/modules/rules/lib/rules.ts +348 -0
- package/extensions/modules/rules/test/lib.test.ts +263 -0
- package/extensions/modules/rules/test/module.test.ts +222 -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 +2464 -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 +228 -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 +763 -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 +524 -0
- package/extensions/modules/subagent/test/widget.test.ts +208 -0
- package/extensions/modules/todo/index.ts +478 -0
- package/extensions/modules/todo/test/todo.test.ts +252 -0
- package/extensions/modules/usage/index.ts +70 -16
- package/extensions/modules/usage/test/genrate.test.ts +70 -0
- package/extensions/modules/usage/test/parsers.test.ts +13 -0
- package/extensions/modules/usage/test/session-accumulators.test.ts +85 -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 +218 -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 +157 -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 +198 -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 +154 -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 +721 -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 +230 -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 +364 -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,230 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unit tests for pi-web format module.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { describe, it, beforeEach, afterEach } from "node:test";
|
|
6
|
+
import assert from "node:assert/strict";
|
|
7
|
+
import {
|
|
8
|
+
sanitizeSnippet,
|
|
9
|
+
truncateText,
|
|
10
|
+
formatFirecrawlScrape,
|
|
11
|
+
formatCrawl4aiResult,
|
|
12
|
+
type SearchResultItem,
|
|
13
|
+
} from "../lib/format";
|
|
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 OUTPUT_MAX_BYTES = 50 * 1024;
|
|
51
|
+
const OUTPUT_MAX_LINES = 2_000;
|
|
52
|
+
|
|
53
|
+
describe("sanitizeSnippet", () => {
|
|
54
|
+
it("returns empty string for empty input", () => {
|
|
55
|
+
assert.equal(sanitizeSnippet(), "");
|
|
56
|
+
assert.equal(sanitizeSnippet(""), "");
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it("strips HTML tags", () => {
|
|
60
|
+
assert.equal(sanitizeSnippet("<b>hello</b> world"), "hello world");
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("decodes HTML entities", () => {
|
|
64
|
+
assert.equal(sanitizeSnippet("foo & bar"), "foo & bar");
|
|
65
|
+
assert.equal(sanitizeSnippet("<tag>"), "<tag>");
|
|
66
|
+
assert.equal(sanitizeSnippet(""quoted""), '"quoted"');
|
|
67
|
+
assert.equal(sanitizeSnippet("hello world"), "hello world");
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it("decodes numeric HTML entities", () => {
|
|
71
|
+
assert.equal(sanitizeSnippet("A"), "A");
|
|
72
|
+
assert.equal(sanitizeSnippet("A"), "A");
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it("collapses whitespace", () => {
|
|
76
|
+
assert.equal(sanitizeSnippet("hello world"), "hello world");
|
|
77
|
+
assert.equal(sanitizeSnippet(" hello "), "hello");
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
describe("truncateText", () => {
|
|
82
|
+
it("returns short text as-is", () => {
|
|
83
|
+
const text = "hello world";
|
|
84
|
+
assert.equal(truncateText(text), text);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it("truncates by line count when exceeding OUTPUT_MAX_LINES", () => {
|
|
88
|
+
const lines = Array.from({ length: OUTPUT_MAX_LINES + 10 }, (_, i) => `line ${i}`);
|
|
89
|
+
const text = lines.join("\n");
|
|
90
|
+
const result = truncateText(text);
|
|
91
|
+
assert.ok(includes(result, "[Web output truncated to"));
|
|
92
|
+
assert.ok((result.split("\n").length) < (OUTPUT_MAX_LINES + 15));
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it("truncates by byte count when exceeding OUTPUT_MAX_BYTES", () => {
|
|
96
|
+
const text = "x".repeat(OUTPUT_MAX_BYTES + 50000);
|
|
97
|
+
const result = truncateText(text);
|
|
98
|
+
assert.ok(includes(result, "[Web output truncated to"));
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it("handles empty string", () => {
|
|
102
|
+
assert.equal(truncateText(""), "");
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
describe("formatFirecrawlScrape", () => {
|
|
107
|
+
it("formats scrape data with metadata", () => {
|
|
108
|
+
const data = {
|
|
109
|
+
data: {
|
|
110
|
+
metadata: {
|
|
111
|
+
title: "Test Page",
|
|
112
|
+
sourceURL: "https://example.com",
|
|
113
|
+
statusCode: 200,
|
|
114
|
+
},
|
|
115
|
+
markdown: "# Hello\n\nThis is content.",
|
|
116
|
+
},
|
|
117
|
+
};
|
|
118
|
+
const output = formatFirecrawlScrape(data);
|
|
119
|
+
assert.ok(includes(output, "# Test Page"));
|
|
120
|
+
assert.ok(includes(output, "Source: https://example.com"));
|
|
121
|
+
assert.ok(includes(output, "Status: 200"));
|
|
122
|
+
assert.ok(includes(output, "# Hello"));
|
|
123
|
+
assert.ok(includes(output, "This is content."));
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it("resolves data at top level when no nested data", () => {
|
|
127
|
+
const data = {
|
|
128
|
+
metadata: { title: "Direct" },
|
|
129
|
+
markdown: "Content",
|
|
130
|
+
};
|
|
131
|
+
const output = formatFirecrawlScrape(data);
|
|
132
|
+
assert.ok(includes(output, "# Direct"));
|
|
133
|
+
assert.ok(includes(output, "Content"));
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it("includes warning when present", () => {
|
|
137
|
+
const data = { data: { markdown: "x" }, warning: "Rate limited" };
|
|
138
|
+
const output = formatFirecrawlScrape(data);
|
|
139
|
+
assert.ok(includes(output, "Warning: Rate limited"));
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it("includes links when present", () => {
|
|
143
|
+
const data = {
|
|
144
|
+
data: {
|
|
145
|
+
markdown: "body",
|
|
146
|
+
links: ["https://a.com", "https://b.com"],
|
|
147
|
+
},
|
|
148
|
+
};
|
|
149
|
+
const output = formatFirecrawlScrape(data);
|
|
150
|
+
assert.ok(includes(output, "## Links"));
|
|
151
|
+
assert.ok(includes(output, "- https://a.com"));
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it("returns JSON stringify fallback for empty data", () => {
|
|
155
|
+
const data = { foo: "bar" };
|
|
156
|
+
const output = formatFirecrawlScrape(data);
|
|
157
|
+
assert.ok(includes(output, "foo"));
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
describe("formatCrawl4aiResult", () => {
|
|
162
|
+
it("formats a single successful crawl result with markdown", () => {
|
|
163
|
+
const data = {
|
|
164
|
+
url: "https://example.com",
|
|
165
|
+
success: true,
|
|
166
|
+
status_code: 200,
|
|
167
|
+
markdown: { fit_markdown: "# Hello\n\nWorld content." },
|
|
168
|
+
};
|
|
169
|
+
const output = formatCrawl4aiResult(data);
|
|
170
|
+
assert.ok(includes(output, "URL: https://example.com"));
|
|
171
|
+
assert.ok(includes(output, "Status: 200"));
|
|
172
|
+
assert.ok(includes(output, "# Hello"));
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
it("shows error for failed crawl", () => {
|
|
176
|
+
const data = {
|
|
177
|
+
url: "https://example.com/404",
|
|
178
|
+
success: false,
|
|
179
|
+
error_message: "Not Found",
|
|
180
|
+
};
|
|
181
|
+
const output = formatCrawl4aiResult(data);
|
|
182
|
+
assert.ok(includes(output, "Error: Not Found"));
|
|
183
|
+
assert.ok(includes(output, "URL: https://example.com/404"));
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
it("formats multiple results from wrapped response", () => {
|
|
187
|
+
const data = {
|
|
188
|
+
results: [
|
|
189
|
+
{ url: "https://a.com", success: true, markdown: { raw_markdown: "Page A" } },
|
|
190
|
+
{ url: "https://b.com", success: true, markdown: { raw_markdown: "Page B" } },
|
|
191
|
+
],
|
|
192
|
+
};
|
|
193
|
+
const output = formatCrawl4aiResult(data);
|
|
194
|
+
assert.ok(includes(output, "=== Result 1 ==="));
|
|
195
|
+
assert.ok(includes(output, "=== Result 2 ==="));
|
|
196
|
+
assert.ok(includes(output, "Page A"));
|
|
197
|
+
assert.ok(includes(output, "Page B"));
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
it("falls back to raw_markdown when fit_markdown is absent", () => {
|
|
201
|
+
const data = {
|
|
202
|
+
url: "https://example.com",
|
|
203
|
+
success: true,
|
|
204
|
+
markdown: { raw_markdown: "Raw content only" },
|
|
205
|
+
};
|
|
206
|
+
const output = formatCrawl4aiResult(data);
|
|
207
|
+
assert.ok(includes(output, "Raw content only"));
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
it("includes links when present", () => {
|
|
211
|
+
const data = {
|
|
212
|
+
url: "https://example.com",
|
|
213
|
+
success: true,
|
|
214
|
+
markdown: { fit_markdown: "body" },
|
|
215
|
+
links: {
|
|
216
|
+
internal: [{ href: "https://example.com/about" }],
|
|
217
|
+
external: [{ href: "https://other.com" }],
|
|
218
|
+
},
|
|
219
|
+
};
|
|
220
|
+
const output = formatCrawl4aiResult(data);
|
|
221
|
+
assert.ok(includes(output, "Links"));
|
|
222
|
+
assert.ok(includes(output, "https://example.com/about"));
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
it("returns fallback for empty data", () => {
|
|
226
|
+
assert.equal(formatCrawl4aiResult({}), "(No data)");
|
|
227
|
+
});
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
|
|
@@ -0,0 +1,465 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unit tests for Gemini cookie auto-refresh (lib/gemini-auth.ts) and its
|
|
3
|
+
* integration with lib/gemini.ts. No network — the rotate POST is injected.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { describe, it, beforeEach, afterEach } from "node:test";
|
|
7
|
+
import assert from "node:assert/strict";
|
|
8
|
+
import fs from "node:fs";
|
|
9
|
+
import os from "node:os";
|
|
10
|
+
import path from "node:path";
|
|
11
|
+
import {
|
|
12
|
+
__keepaliveDebug,
|
|
13
|
+
clearCookieStore,
|
|
14
|
+
cookieStoreSnapshot,
|
|
15
|
+
defaultStorePath,
|
|
16
|
+
ensureKeepalive,
|
|
17
|
+
keepaliveOnce,
|
|
18
|
+
loadCookieStore,
|
|
19
|
+
refreshGeminiAuth,
|
|
20
|
+
resolvePsidts,
|
|
21
|
+
rotateCookies,
|
|
22
|
+
saveCookieStore,
|
|
23
|
+
stopKeepalive,
|
|
24
|
+
defaultPost,
|
|
25
|
+
type PostFn,
|
|
26
|
+
} from "../lib/gemini-auth";
|
|
27
|
+
import {
|
|
28
|
+
describeGeminiError,
|
|
29
|
+
withGeminiClient,
|
|
30
|
+
__resetGeminiClientCache,
|
|
31
|
+
type GeminiClientLike,
|
|
32
|
+
} from "../lib/gemini";
|
|
33
|
+
|
|
34
|
+
// chai-compat helpers (assert-based)
|
|
35
|
+
function includes(haystack: unknown, needle: unknown): boolean {
|
|
36
|
+
if (typeof haystack === "string") return haystack.includes(String(needle));
|
|
37
|
+
if (Array.isArray(haystack)) return (haystack as unknown[]).some((v) => {
|
|
38
|
+
if (typeof v === "string" && typeof needle === "string") return v.includes(needle);
|
|
39
|
+
if (v === needle) return true;
|
|
40
|
+
// chai's to.include on an array does DEEP membership for objects.
|
|
41
|
+
if (v && needle && typeof v === "object" && typeof needle === "object") {
|
|
42
|
+
try { assert.deepStrictEqual(v, needle); return true; } catch { return false; }
|
|
43
|
+
}
|
|
44
|
+
return false;
|
|
45
|
+
});
|
|
46
|
+
// chai's to.include on an OBJECT target: needle's properties are a subset.
|
|
47
|
+
if (haystack && typeof haystack === "object" && needle && typeof needle === "object") {
|
|
48
|
+
return deepIncludes(haystack, needle as Record<string, unknown>);
|
|
49
|
+
}
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
function lengthOf(v: unknown): number {
|
|
53
|
+
if (Array.isArray(v)) return v.length;
|
|
54
|
+
if (typeof v === "string") return v.length;
|
|
55
|
+
if (v && typeof v === "object" && "length" in (v as Record<string, unknown>)) return Number((v as Record<string, unknown>).length);
|
|
56
|
+
if (v && typeof v === "object" && "size" in (v as Record<string, unknown>)) return Number((v as Record<string, unknown>).size);
|
|
57
|
+
throw new Error("lengthOf: value has no length/size");
|
|
58
|
+
}
|
|
59
|
+
/** Deep "includes" — every own key of `part` must exist on `obj` with a
|
|
60
|
+
* deep-equal value (chai's to.deep.include for object subjects). */
|
|
61
|
+
function deepIncludes(obj: unknown, part: Record<string, unknown>): boolean {
|
|
62
|
+
if (Array.isArray(obj)) return includes(obj, part); // deep membership
|
|
63
|
+
if (typeof obj !== "object" || obj === null) return false;
|
|
64
|
+
return Object.entries(part).every(([k, v]) => {
|
|
65
|
+
try { assert.deepStrictEqual((obj as Record<string, unknown>)[k], v); return true; } catch { return false; }
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function tmpStore(): string {
|
|
70
|
+
return path.join(fs.mkdtempSync(path.join(os.tmpdir(), "gemini-auth-test-")), "cookies.json");
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function fakePost(
|
|
74
|
+
res: { status: number; setCookie: string[] },
|
|
75
|
+
capture?: { url?: string; opts?: { headers?: Record<string, string>; body?: string } },
|
|
76
|
+
): PostFn {
|
|
77
|
+
return async (url, opts) => {
|
|
78
|
+
if (capture) {
|
|
79
|
+
capture.url = url;
|
|
80
|
+
capture.opts = opts;
|
|
81
|
+
}
|
|
82
|
+
return res;
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const okPost = (ts: string, capture?: Parameters<typeof fakePost>[1]) => fakePost({ status: 200, setCookie: [`__Secure-1PSIDTS=${ts}; Path=/; Secure; HttpOnly`] }, capture);
|
|
87
|
+
const unauthorizedPost = () => fakePost({ status: 401, setCookie: [] });
|
|
88
|
+
|
|
89
|
+
const config = { psid: "psid-test", psidts: "env-ts", psidSource: "test", proxy: undefined };
|
|
90
|
+
|
|
91
|
+
describe("cookie store", () => {
|
|
92
|
+
it("round-trips an entry and reports snapshot freshness", () => {
|
|
93
|
+
const p = tmpStore();
|
|
94
|
+
saveCookieStore({ psid: "a", psidts: "b", updatedAt: Date.now() }, p);
|
|
95
|
+
assert.ok(deepIncludes(loadCookieStore(p), { psid: "a", psidts: "b" }));
|
|
96
|
+
const snap = cookieStoreSnapshot(p);
|
|
97
|
+
assert.equal(snap.present, true);
|
|
98
|
+
assert.ok((snap.ageSeconds ?? 0) >= 0);
|
|
99
|
+
fs.chmodSync(p, 0o600); // saveCookieStore must leave it 0600
|
|
100
|
+
assert.equal(fs.statSync(p).mode & 0o777, 0o600);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it("treats missing and corrupt files as absent, clear removes", () => {
|
|
104
|
+
const p = tmpStore();
|
|
105
|
+
assert.equal(loadCookieStore(p), null);
|
|
106
|
+
fs.writeFileSync(p, "{not json");
|
|
107
|
+
assert.equal(loadCookieStore(p), null);
|
|
108
|
+
saveCookieStore({ psid: "a", psidts: "b", updatedAt: 1 }, p);
|
|
109
|
+
clearCookieStore(p);
|
|
110
|
+
assert.equal(loadCookieStore(p), null);
|
|
111
|
+
assert.equal(cookieStoreSnapshot(p).present, false);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it("honors GEMINI_WEB_COOKIE_STORE for the default path", () => {
|
|
115
|
+
const old = process.env.GEMINI_WEB_COOKIE_STORE;
|
|
116
|
+
process.env.GEMINI_WEB_COOKIE_STORE = "/tmp/xyz/store.json";
|
|
117
|
+
assert.equal(defaultStorePath(), "/tmp/xyz/store.json");
|
|
118
|
+
if (old === undefined) delete process.env.GEMINI_WEB_COOKIE_STORE;
|
|
119
|
+
else process.env.GEMINI_WEB_COOKIE_STORE = old;
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
// ceulen: the module reads process.env ONLY — .env file ingestion is the
|
|
123
|
+
// bundle env.ts's job (agent-dir at import, trusted cwd at session_start).
|
|
124
|
+
it("GEMINI_WEB_COOKIE_STORE follows process.env changes (no module-level caching)", () => {
|
|
125
|
+
const old = process.env.GEMINI_WEB_COOKIE_STORE;
|
|
126
|
+
process.env.GEMINI_WEB_COOKIE_STORE = "/tmp/xyz/store.json";
|
|
127
|
+
assert.equal(defaultStorePath(), "/tmp/xyz/store.json");
|
|
128
|
+
process.env.GEMINI_WEB_COOKIE_STORE = "/tmp/xyz/other.json";
|
|
129
|
+
assert.equal(defaultStorePath(), "/tmp/xyz/other.json");
|
|
130
|
+
if (old === undefined) delete process.env.GEMINI_WEB_COOKIE_STORE;
|
|
131
|
+
else process.env.GEMINI_WEB_COOKIE_STORE = old;
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
describe("resolvePsidts", () => {
|
|
136
|
+
it("env (fresh paste) always wins over the store", () => {
|
|
137
|
+
const p = tmpStore();
|
|
138
|
+
saveCookieStore({ psid: "psid-test", psidts: "store-ts", updatedAt: 1 }, p);
|
|
139
|
+
assert.equal(resolvePsidts("psid-test", "env-ts", p), "env-ts");
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it("falls back to the store only when env has no TS", () => {
|
|
143
|
+
const p = tmpStore();
|
|
144
|
+
saveCookieStore({ psid: "psid-test", psidts: "store-ts", updatedAt: 1 }, p);
|
|
145
|
+
assert.equal(resolvePsidts("psid-test", undefined, p), "store-ts");
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
it("falls back to env with no store, and passes through guest mode", () => {
|
|
149
|
+
assert.equal(resolvePsidts("psid-test", "env-ts", tmpStore()), "env-ts");
|
|
150
|
+
assert.equal(resolvePsidts(undefined, "env-ts", tmpStore()), "env-ts");
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
describe("rotateCookies", () => {
|
|
155
|
+
it("sends the documented request shape and parses the fresh 1PSIDTS", async () => {
|
|
156
|
+
const capture: { url?: string; opts?: { headers?: Record<string, string>; body?: string } } = {};
|
|
157
|
+
const r = await rotateCookies({ psid: "p1", psidts: "t1", post: okPost("fresh", capture) });
|
|
158
|
+
assert.ok(deepIncludes(r, { ok: true, psidts: "fresh" }));
|
|
159
|
+
assert.equal(capture.url, "https://accounts.google.com/RotateCookies");
|
|
160
|
+
assert.equal(capture.opts!.body, '[000,"-0000000000000000000"]');
|
|
161
|
+
assert.equal(capture.opts!.headers!["Content-Type"], "application/json");
|
|
162
|
+
assert.equal(capture.opts!.headers!.Origin, "https://accounts.google.com");
|
|
163
|
+
assert.ok(includes(capture.opts!.headers!["User-Agent"], "Chrome/145")); // non-browser UAs get 400
|
|
164
|
+
assert.equal(capture.opts!.headers!.Cookie, "__Secure-1PSID=p1; __Secure-1PSIDTS=t1");
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it("treats 400/401 as definitive but 200-no-TS and 5xx as non-stale", async () => {
|
|
168
|
+
const r = await rotateCookies({ psid: "p1", post: okPost("f2") });
|
|
169
|
+
assert.equal(r.ok, true);
|
|
170
|
+
const u = await rotateCookies({ psid: "p1", post: unauthorizedPost() });
|
|
171
|
+
assert.equal(u.ok, false);
|
|
172
|
+
assert.equal(u.stale, true);
|
|
173
|
+
assert.match(String(u.reason), /unauthorized/);
|
|
174
|
+
const bad = await rotateCookies({ psid: "p1", post: fakePost({ status: 400, setCookie: [] }) });
|
|
175
|
+
assert.equal(bad.ok, false);
|
|
176
|
+
assert.equal(bad.stale, true);
|
|
177
|
+
const none = await rotateCookies({ psid: "p1", post: fakePost({ status: 200, setCookie: ["NID=x; Path=/"] }) });
|
|
178
|
+
assert.equal(none.ok, false);
|
|
179
|
+
assert.equal(none.stale, undefined);
|
|
180
|
+
const boom = await rotateCookies({ psid: "p1", post: fakePost({ status: 503, setCookie: [] }) });
|
|
181
|
+
assert.equal(boom.ok, false);
|
|
182
|
+
assert.equal(boom.stale, undefined);
|
|
183
|
+
const forbidden = await rotateCookies({ psid: "p1", post: fakePost({ status: 403, setCookie: [] }) });
|
|
184
|
+
assert.equal(forbidden.ok, false);
|
|
185
|
+
assert.equal(forbidden.stale, undefined); // 403 = soft-block, not dead
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
it("marks transport errors as NOT stale", async () => {
|
|
189
|
+
const r = await rotateCookies({ psid: "p1", post: async () => { throw new Error("ECONNRESET"); } });
|
|
190
|
+
assert.equal(r.ok, false);
|
|
191
|
+
assert.equal(r.stale, undefined);
|
|
192
|
+
});
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
describe("refreshGeminiAuth", () => {
|
|
196
|
+
it("persists the fresh 1PSIDTS on success", async () => {
|
|
197
|
+
const p = tmpStore();
|
|
198
|
+
const r = await refreshGeminiAuth(config, { post: okPost("fresh-ts"), storePath: p });
|
|
199
|
+
assert.equal(r.ok, true);
|
|
200
|
+
assert.ok(deepIncludes(loadCookieStore(p), { psid: "psid-test", psidts: "fresh-ts" }));
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it("never destroys the store on rotation failures (400/401/offline/5xx/403)", async () => {
|
|
204
|
+
const p = tmpStore();
|
|
205
|
+
saveCookieStore({ psid: "psid-test", psidts: "old", updatedAt: 1 }, p);
|
|
206
|
+
await refreshGeminiAuth(config, { post: unauthorizedPost(), storePath: p });
|
|
207
|
+
assert.ok(deepIncludes(loadCookieStore(p), { psidts: "old" }));
|
|
208
|
+
|
|
209
|
+
saveCookieStore({ psid: "psid-test", psidts: "old", updatedAt: 1 }, p);
|
|
210
|
+
await refreshGeminiAuth(config, { post: async () => { throw new Error("offline"); }, storePath: p });
|
|
211
|
+
assert.ok(deepIncludes(loadCookieStore(p), { psidts: "old" }));
|
|
212
|
+
|
|
213
|
+
// transient 5xx must also keep the store (reviewer: stale is for definitive rejections only)
|
|
214
|
+
saveCookieStore({ psid: "psid-test", psidts: "old", updatedAt: 1 }, p);
|
|
215
|
+
await refreshGeminiAuth(config, { post: fakePost({ status: 503, setCookie: [] }), storePath: p });
|
|
216
|
+
assert.ok(deepIncludes(loadCookieStore(p), { psidts: "old" }));
|
|
217
|
+
|
|
218
|
+
// 403 is a soft-block — the newest TS must survive it
|
|
219
|
+
saveCookieStore({ psid: "psid-test", psidts: "old", updatedAt: 1 }, p);
|
|
220
|
+
await refreshGeminiAuth(config, { post: fakePost({ status: 403, setCookie: [] }), storePath: p });
|
|
221
|
+
assert.ok(deepIncludes(loadCookieStore(p), { psidts: "old" }));
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
it("is a no-op without a psid", async () => {
|
|
225
|
+
const r = await refreshGeminiAuth({ psid: undefined }, { storePath: tmpStore() });
|
|
226
|
+
assert.equal(r.ok, false);
|
|
227
|
+
assert.match(String(r.reason), /not set/);
|
|
228
|
+
});
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
describe("keepaliveOnce", () => {
|
|
232
|
+
it("skips the network when the store is fresh", async () => {
|
|
233
|
+
const p = tmpStore();
|
|
234
|
+
saveCookieStore({ psid: "psid-test", psidts: "recent", updatedAt: Date.now() }, p);
|
|
235
|
+
let called = 0;
|
|
236
|
+
const post: PostFn = async () => {
|
|
237
|
+
called++;
|
|
238
|
+
return { status: 200, setCookie: [] };
|
|
239
|
+
};
|
|
240
|
+
assert.equal(await keepaliveOnce(config, { storePath: p, post }), true);
|
|
241
|
+
assert.equal(called, 0);
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
it("rotates and persists when stale, fails but keeps the store when dead", async () => {
|
|
245
|
+
const p = tmpStore();
|
|
246
|
+
saveCookieStore({ psid: "psid-test", psidts: "old", updatedAt: 1 }, p);
|
|
247
|
+
assert.equal(await keepaliveOnce(config, { storePath: p, post: okPost("new-ts") }), true);
|
|
248
|
+
assert.ok(deepIncludes(loadCookieStore(p), { psidts: "new-ts" }));
|
|
249
|
+
|
|
250
|
+
saveCookieStore({ psid: "psid-test", psidts: "old", updatedAt: 1 }, p);
|
|
251
|
+
assert.equal(await keepaliveOnce(config, { storePath: p, post: unauthorizedPost() }), false);
|
|
252
|
+
assert.ok(deepIncludes(loadCookieStore(p), { psidts: "old" }));
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
it("does not skip rotation for a fresh store belonging to a DIFFERENT psid", async () => {
|
|
256
|
+
const p = tmpStore();
|
|
257
|
+
saveCookieStore({ psid: "old-psid", psidts: "old-ts", updatedAt: Date.now() }, p);
|
|
258
|
+
let called = 0;
|
|
259
|
+
const post: PostFn = async () => {
|
|
260
|
+
called++;
|
|
261
|
+
return { status: 200, setCookie: ["__Secure-1PSIDTS=fresh-ts; Path=/"] };
|
|
262
|
+
};
|
|
263
|
+
assert.equal(await keepaliveOnce(config, { storePath: p, post }), true);
|
|
264
|
+
assert.equal(called, 1);
|
|
265
|
+
assert.ok(deepIncludes(loadCookieStore(p), { psid: "psid-test", psidts: "fresh-ts" }));
|
|
266
|
+
});
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
describe("withGeminiClient auto-heal + passive persist", () => {
|
|
270
|
+
const cfg = { ...config };
|
|
271
|
+
|
|
272
|
+
beforeEach(() => {
|
|
273
|
+
__resetGeminiClientCache();
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
it("on AuthError: rebuild-retries once (rotation is opt-in, not auto-invoked)", async () => {
|
|
277
|
+
const p = tmpStore();
|
|
278
|
+
const calls: GeminiClientLike[] = [];
|
|
279
|
+
const factory = () => {
|
|
280
|
+
const client: GeminiClientLike = {
|
|
281
|
+
ask: async () => {
|
|
282
|
+
if (calls.length === 1) {
|
|
283
|
+
const e = new Error("Cookies invalid.");
|
|
284
|
+
e.name = "AuthError";
|
|
285
|
+
throw e;
|
|
286
|
+
}
|
|
287
|
+
return { text: "ok" };
|
|
288
|
+
},
|
|
289
|
+
research: async () => ({ text: "report" }),
|
|
290
|
+
};
|
|
291
|
+
calls.push(client);
|
|
292
|
+
return client;
|
|
293
|
+
};
|
|
294
|
+
const result = await withGeminiClient(cfg, (c) => c.ask!("q"), factory, { storePath: p });
|
|
295
|
+
assert.equal(result.text, "ok");
|
|
296
|
+
assert.equal(lengthOf(calls), 2);
|
|
297
|
+
assert.equal(loadCookieStore(p), null); // no rotation → no store write
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
it("on persistent AuthError: rebuilds once, propagates, and never destroys the store", async () => {
|
|
301
|
+
const p = tmpStore();
|
|
302
|
+
saveCookieStore({ psid: "psid-test", psidts: "old", updatedAt: 1 }, p);
|
|
303
|
+
const calls = { n: 0 };
|
|
304
|
+
const factory: Parameters<typeof withGeminiClient>[2] = () => {
|
|
305
|
+
calls.n++;
|
|
306
|
+
return {
|
|
307
|
+
ask: async () => {
|
|
308
|
+
const e = new Error("Cookies invalid.");
|
|
309
|
+
e.name = "AuthError";
|
|
310
|
+
throw e;
|
|
311
|
+
},
|
|
312
|
+
research: async () => ({ text: "report" }),
|
|
313
|
+
};
|
|
314
|
+
};
|
|
315
|
+
let threw: unknown;
|
|
316
|
+
try {
|
|
317
|
+
await withGeminiClient(cfg, (c) => c.ask!("q"), factory, { rotatePost: unauthorizedPost(), storePath: p });
|
|
318
|
+
} catch (e) {
|
|
319
|
+
threw = e;
|
|
320
|
+
}
|
|
321
|
+
assert.equal((threw as Error).name, "AuthError");
|
|
322
|
+
assert.equal(calls.n, 2);
|
|
323
|
+
assert.ok(deepIncludes(loadCookieStore(p), { psid: "psid-test", psidts: "old" })); // store is never destroyed
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
it("persists a rotated 1PSIDTS absorbed from the client jar", async () => {
|
|
327
|
+
const p = tmpStore();
|
|
328
|
+
const client: GeminiClientLike = {
|
|
329
|
+
ask: async () => ({ text: "ok" }),
|
|
330
|
+
research: async () => ({ text: "report" }),
|
|
331
|
+
cookies: { "__Secure-1PSIDTS": "jar-ts" },
|
|
332
|
+
} as GeminiClientLike;
|
|
333
|
+
await withGeminiClient(cfg, (c) => c.ask!("q"), () => client, { storePath: p });
|
|
334
|
+
assert.ok(deepIncludes(loadCookieStore(p), { psid: "psid-test", psidts: "jar-ts" }));
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
it("never writes the store in guest mode", async () => {
|
|
338
|
+
const p = tmpStore();
|
|
339
|
+
const client: GeminiClientLike = {
|
|
340
|
+
ask: async () => ({ text: "ok" }),
|
|
341
|
+
research: async () => ({ text: "report" }),
|
|
342
|
+
cookies: { "__Secure-1PSIDTS": "jar-ts" },
|
|
343
|
+
} as GeminiClientLike;
|
|
344
|
+
await withGeminiClient({ ...cfg, psid: undefined }, (c) => c.ask!("q"), () => client, { storePath: p });
|
|
345
|
+
assert.equal(loadCookieStore(p), null);
|
|
346
|
+
});
|
|
347
|
+
|
|
348
|
+
it("store-write failure never fails the call (best-effort persist)", async () => {
|
|
349
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "gemini-auth-ro-"));
|
|
350
|
+
fs.chmodSync(dir, 0o555);
|
|
351
|
+
const client: GeminiClientLike = {
|
|
352
|
+
ask: async () => ({ text: "ok" }),
|
|
353
|
+
research: async () => ({ text: "report" }),
|
|
354
|
+
cookies: { "__Secure-1PSIDTS": "jar-ts" },
|
|
355
|
+
} as GeminiClientLike;
|
|
356
|
+
try {
|
|
357
|
+
const r = await withGeminiClient(cfg, (c) => c.ask!("q"), () => client, { storePath: path.join(dir, "cookies.json") });
|
|
358
|
+
assert.equal(r.text, "ok");
|
|
359
|
+
} finally {
|
|
360
|
+
fs.chmodSync(dir, 0o755);
|
|
361
|
+
}
|
|
362
|
+
});
|
|
363
|
+
});
|
|
364
|
+
|
|
365
|
+
describe("describeGeminiError AuthError guidance", () => {
|
|
366
|
+
it("points at the incognito re-paste and the keepalive store", () => {
|
|
367
|
+
const e = new Error("Cookies invalid.");
|
|
368
|
+
e.name = "AuthError";
|
|
369
|
+
const msg = describeGeminiError(e);
|
|
370
|
+
assert.match(String(msg), /incognito/);
|
|
371
|
+
assert.match(String(msg), /daily browser/);
|
|
372
|
+
});
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
describe("ensureKeepalive arming", () => {
|
|
376
|
+
const OLD_KEEPALIVE = process.env.GEMINI_WEB_KEEPALIVE;
|
|
377
|
+
const OLD_INTERVAL = process.env.GEMINI_WEB_ROTATE_INTERVAL_MS;
|
|
378
|
+
|
|
379
|
+
afterEach(() => {
|
|
380
|
+
stopKeepalive();
|
|
381
|
+
if (OLD_KEEPALIVE === undefined) delete process.env.GEMINI_WEB_KEEPALIVE;
|
|
382
|
+
else process.env.GEMINI_WEB_KEEPALIVE = OLD_KEEPALIVE;
|
|
383
|
+
if (OLD_INTERVAL === undefined) delete process.env.GEMINI_WEB_ROTATE_INTERVAL_MS;
|
|
384
|
+
else process.env.GEMINI_WEB_ROTATE_INTERVAL_MS = OLD_INTERVAL;
|
|
385
|
+
});
|
|
386
|
+
|
|
387
|
+
it("stays disarmed by default (rotation is opt-in)", () => {
|
|
388
|
+
delete process.env.GEMINI_WEB_KEEPALIVE;
|
|
389
|
+
delete process.env.GEMINI_WEB_ROTATE_INTERVAL_MS;
|
|
390
|
+
ensureKeepalive(config);
|
|
391
|
+
assert.equal(__keepaliveDebug().armed, false);
|
|
392
|
+
});
|
|
393
|
+
|
|
394
|
+
it("stays disarmed when GEMINI_WEB_KEEPALIVE=0", () => {
|
|
395
|
+
process.env.GEMINI_WEB_KEEPALIVE = "0";
|
|
396
|
+
ensureKeepalive(config);
|
|
397
|
+
assert.equal(__keepaliveDebug().armed, false);
|
|
398
|
+
});
|
|
399
|
+
|
|
400
|
+
it("arms when GEMINI_WEB_KEEPALIVE=1: default 600s cadence, honors env interval, clamps below the floor", () => {
|
|
401
|
+
process.env.GEMINI_WEB_KEEPALIVE = "1";
|
|
402
|
+
delete process.env.GEMINI_WEB_ROTATE_INTERVAL_MS;
|
|
403
|
+
ensureKeepalive(config);
|
|
404
|
+
assert.equal(__keepaliveDebug().armed, true);
|
|
405
|
+
assert.equal(__keepaliveDebug().intervalMs, 600_000);
|
|
406
|
+
stopKeepalive();
|
|
407
|
+
process.env.GEMINI_WEB_ROTATE_INTERVAL_MS = "120000";
|
|
408
|
+
ensureKeepalive(config);
|
|
409
|
+
assert.equal(__keepaliveDebug().intervalMs, 120_000);
|
|
410
|
+
stopKeepalive();
|
|
411
|
+
process.env.GEMINI_WEB_ROTATE_INTERVAL_MS = "5000";
|
|
412
|
+
ensureKeepalive(config);
|
|
413
|
+
assert.equal(__keepaliveDebug().intervalMs, 600_000);
|
|
414
|
+
});
|
|
415
|
+
|
|
416
|
+
it("re-arms when the psid changes", () => {
|
|
417
|
+
process.env.GEMINI_WEB_KEEPALIVE = "1";
|
|
418
|
+
ensureKeepalive(config);
|
|
419
|
+
assert.equal(__keepaliveDebug().psid, "psid-test");
|
|
420
|
+
ensureKeepalive({ ...config, psid: "psid-other" });
|
|
421
|
+
assert.equal(__keepaliveDebug().psid, "psid-other");
|
|
422
|
+
});
|
|
423
|
+
|
|
424
|
+
it("tick passes cfg+hooks through (post called, store written)", async () => { // upstream mocha budget: 5000ms
|
|
425
|
+
process.env.GEMINI_WEB_KEEPALIVE = "1";
|
|
426
|
+
const p = tmpStore();
|
|
427
|
+
let called = 0;
|
|
428
|
+
const post: PostFn = async () => {
|
|
429
|
+
called++;
|
|
430
|
+
return { status: 200, setCookie: ["__Secure-1PSIDTS=tick-ts; Path=/"] };
|
|
431
|
+
};
|
|
432
|
+
ensureKeepalive(config, { post, storePath: p, intervalMs: 1000 });
|
|
433
|
+
await new Promise((r) => setTimeout(r, 1300));
|
|
434
|
+
stopKeepalive();
|
|
435
|
+
assert.ok((called) >= (1));
|
|
436
|
+
assert.ok(deepIncludes(loadCookieStore(p), { psidts: "tick-ts" }));
|
|
437
|
+
});
|
|
438
|
+
});
|
|
439
|
+
|
|
440
|
+
describe('defaultPost (rotation transport)', () => {
|
|
441
|
+
it('captures Set-Cookie from a redirect without following it', async () => { // upstream mocha budget: 5000ms
|
|
442
|
+
const { createServer } = await import('node:http');
|
|
443
|
+
let hits = 0;
|
|
444
|
+
const server = createServer((req, res) => {
|
|
445
|
+
hits++;
|
|
446
|
+
res.writeHead(302, { 'set-cookie': ['__Secure-1PSIDTS=redirect-ts; Path=/'], location: '/should-not-follow' });
|
|
447
|
+
res.end();
|
|
448
|
+
});
|
|
449
|
+
await new Promise<void>((r) => server.listen(0, '127.0.0.1', r));
|
|
450
|
+
const port = (server.address() as { port: number }).port;
|
|
451
|
+
try {
|
|
452
|
+
const res = await defaultPost(`http://127.0.0.1:${port}/rotate`, {
|
|
453
|
+
headers: { 'Content-Type': 'application/json' },
|
|
454
|
+
body: '[000,"-0000000000000000000"]',
|
|
455
|
+
timeoutMs: 2000,
|
|
456
|
+
});
|
|
457
|
+
assert.equal(res.status, 302); // redirect NOT followed
|
|
458
|
+
assert.ok(includes(res.setCookie.join(' '), '__Secure-1PSIDTS=redirect-ts'));
|
|
459
|
+
await new Promise((r2) => setTimeout(r2, 100));
|
|
460
|
+
assert.equal(hits, 1); // exactly one request
|
|
461
|
+
} finally {
|
|
462
|
+
server.close();
|
|
463
|
+
}
|
|
464
|
+
});
|
|
465
|
+
});
|