@juicesharp/rpiv-web-tools 1.20.0 → 2.1.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.
@@ -1,129 +0,0 @@
1
- import { mkdirSync, rmSync, writeFileSync } from "node:fs";
2
- import { dirname } from "node:path";
3
- import { configPath } from "@juicesharp/rpiv-config";
4
- import { beforeEach, describe, expect, it } from "vitest";
5
- import { getConfigPath, readConfig, WebToolsConfigSchema, writeConfig } from "./config.js";
6
-
7
- const CONFIG_PATH = configPath("rpiv-web-tools");
8
-
9
- beforeEach(() => {
10
- rmSync(CONFIG_PATH, { force: true });
11
- });
12
-
13
- function writeRaw(contents: string): void {
14
- mkdirSync(dirname(CONFIG_PATH), { recursive: true });
15
- writeFileSync(CONFIG_PATH, contents, "utf-8");
16
- }
17
-
18
- describe("getConfigPath", () => {
19
- it("returns the canonical ~/.config/rpiv-web-tools/config.json", () => {
20
- expect(getConfigPath()).toBe(CONFIG_PATH);
21
- });
22
- });
23
-
24
- describe("readConfig — fail-soft posture", () => {
25
- it("returns {} when the file does not exist", () => {
26
- expect(readConfig()).toEqual({});
27
- });
28
-
29
- it("returns {} on malformed JSON (matches loadJsonConfig tolerance)", () => {
30
- writeRaw("{ not valid json");
31
- expect(readConfig()).toEqual({});
32
- });
33
-
34
- it("returns {} when the file is a directory (EISDIR)", () => {
35
- mkdirSync(CONFIG_PATH, { recursive: true });
36
- try {
37
- expect(readConfig()).toEqual({});
38
- } finally {
39
- rmSync(CONFIG_PATH, { recursive: true, force: true });
40
- }
41
- });
42
-
43
- it("returns {} when the schema validation fails hard (e.g. provider is a number)", () => {
44
- writeRaw(JSON.stringify({ provider: 123 }));
45
- expect(readConfig()).toEqual({});
46
- });
47
- });
48
-
49
- describe("readConfig — released-shape compatibility", () => {
50
- it("loads a minimal { provider, apiKeys } config unchanged", () => {
51
- writeRaw(JSON.stringify({ provider: "brave", apiKeys: { brave: "k" } }));
52
- expect(readConfig()).toEqual({ provider: "brave", apiKeys: { brave: "k" } });
53
- });
54
-
55
- it("loads the legacy top-level apiKey field", () => {
56
- writeRaw(JSON.stringify({ apiKey: "legacy" }));
57
- expect(readConfig()).toMatchObject({ apiKey: "legacy" });
58
- });
59
-
60
- it("preserves unknown top-level keys (otherField round-trip contract)", () => {
61
- // The released /web-tools migrate-legacy-apiKey test relies on this:
62
- // unknown keys MUST NOT be stripped by the schema reader.
63
- writeRaw(JSON.stringify({ apiKey: "k", otherField: "keep" }));
64
- const cfg = readConfig() as { otherField?: string };
65
- expect(cfg.otherField).toBe("keep");
66
- });
67
-
68
- it("loads the guidance subtree with web_search + web_fetch", () => {
69
- writeRaw(
70
- JSON.stringify({
71
- guidance: {
72
- web_search: { promptSnippet: "snip", promptGuidelines: ["a", "b"] },
73
- web_fetch: { promptSnippet: "snip2" },
74
- },
75
- }),
76
- );
77
- const cfg = readConfig();
78
- expect(cfg.guidance?.web_search?.promptSnippet).toBe("snip");
79
- expect(cfg.guidance?.web_fetch?.promptSnippet).toBe("snip2");
80
- });
81
- });
82
-
83
- describe("readConfig — interceptors.github union", () => {
84
- it("accepts the boolean true shorthand", () => {
85
- writeRaw(JSON.stringify({ interceptors: { github: true } }));
86
- expect(readConfig().interceptors?.github).toBe(true);
87
- });
88
-
89
- it("accepts the boolean false shorthand", () => {
90
- writeRaw(JSON.stringify({ interceptors: { github: false } }));
91
- expect(readConfig().interceptors?.github).toBe(false);
92
- });
93
-
94
- it("accepts the object override form", () => {
95
- writeRaw(
96
- JSON.stringify({
97
- interceptors: { github: { maxRepoSizeMB: 1000, clonePath: "/x" } },
98
- }),
99
- );
100
- const gh = readConfig().interceptors?.github;
101
- expect(gh).toEqual({ maxRepoSizeMB: 1000, clonePath: "/x" });
102
- });
103
-
104
- it("falls back to {} when interceptors.github has a type-incompatible shape", () => {
105
- // A number is neither boolean nor a GitHubInterceptorOptions object —
106
- // hard schema failure → fail-soft to {}.
107
- writeRaw(JSON.stringify({ interceptors: { github: 42 } }));
108
- expect(readConfig()).toEqual({});
109
- });
110
- });
111
-
112
- describe("writeConfig", () => {
113
- it("round-trips a config through readConfig", () => {
114
- expect(writeConfig({ provider: "brave", apiKeys: { brave: "k" } })).toBe(true);
115
- expect(readConfig()).toEqual({ provider: "brave", apiKeys: { brave: "k" } });
116
- });
117
-
118
- it("preserves the interceptors.github stanza across save+load", () => {
119
- expect(writeConfig({ interceptors: { github: { maxRepoSizeMB: 500 } } })).toBe(true);
120
- expect(readConfig().interceptors?.github).toEqual({ maxRepoSizeMB: 500 });
121
- });
122
- });
123
-
124
- describe("WebToolsConfigSchema — schema-only sanity", () => {
125
- it("exists and is a TypeBox object", () => {
126
- expect(WebToolsConfigSchema).toBeDefined();
127
- expect(WebToolsConfigSchema.type).toBe("object");
128
- });
129
- });
@@ -1,209 +0,0 @@
1
- /**
2
- * Tests the interceptor chain dispatch in registerWebTools: first-match-wins,
3
- * empty-chain fall-through, and the consumer×user-config opt-in resolution.
4
- * Exercises the chain end-to-end via the registered web_fetch tool rather
5
- * than the GitHubInterceptor class directly.
6
- */
7
-
8
- import { mkdirSync, rmSync, writeFileSync } from "node:fs";
9
- import { dirname } from "node:path";
10
- import { configPath } from "@juicesharp/rpiv-config";
11
- import { createMockCtx, createMockPi, stubFetch } from "@juicesharp/rpiv-test-utils";
12
- import { beforeEach, describe, expect, it } from "vitest";
13
- import registerWebTools from "../../index.js";
14
-
15
- const CONFIG_PATH = configPath("rpiv-web-tools");
16
-
17
- function writeConfig(contents: unknown) {
18
- mkdirSync(dirname(CONFIG_PATH), { recursive: true });
19
- writeFileSync(CONFIG_PATH, JSON.stringify(contents), "utf-8");
20
- }
21
-
22
- beforeEach(() => {
23
- delete process.env.BRAVE_SEARCH_API_KEY;
24
- delete process.env.GITHUB_TOKEN;
25
- rmSync(CONFIG_PATH, { force: true });
26
- });
27
-
28
- describe("interceptor chain — opt-in resolution", () => {
29
- it("default OFF: github URL hits provider fetch when neither user nor consumer opts in", async () => {
30
- process.env.BRAVE_SEARCH_API_KEY = "k";
31
- writeConfig({ provider: "brave" });
32
- stubFetch([
33
- {
34
- match: () => true,
35
- response: () =>
36
- new Response("<html><body>plain page</body></html>", {
37
- status: 200,
38
- headers: { "content-type": "text/html" },
39
- }),
40
- },
41
- ]);
42
- const { pi, captured } = createMockPi();
43
- registerWebTools(pi);
44
- const r = await captured.tools
45
- .get("web_fetch")
46
- ?.execute?.(
47
- "tc",
48
- { url: "https://github.com/owner/repo" },
49
- undefined as never,
50
- undefined as never,
51
- createMockCtx(),
52
- );
53
- expect(r?.content[0]).toMatchObject({ text: expect.stringContaining("plain page") });
54
- });
55
-
56
- it("consumer:true enables the interceptor when user config is absent", async () => {
57
- // The interceptor is built; for github.com/owner/repo without gh available
58
- // the interceptor returns null (clone+API both fail) and we fall through
59
- // to provider.fetch. We assert the chain didn't short-circuit the fall-through.
60
- process.env.BRAVE_SEARCH_API_KEY = "k";
61
- writeConfig({ provider: "brave" });
62
- stubFetch([
63
- {
64
- match: () => true,
65
- response: () =>
66
- new Response("<html><body>provider fallback</body></html>", {
67
- status: 200,
68
- headers: { "content-type": "text/html" },
69
- }),
70
- },
71
- ]);
72
- const { pi, captured } = createMockPi();
73
- registerWebTools(pi, { interceptors: { github: true } });
74
- // owner/repo/issues → parseGitHubUrl returns null → intercept null → provider runs.
75
- const r = await captured.tools
76
- .get("web_fetch")
77
- ?.execute?.(
78
- "tc",
79
- { url: "https://github.com/owner/repo/issues" },
80
- undefined as never,
81
- undefined as never,
82
- createMockCtx(),
83
- );
84
- expect(r?.content[0]).toMatchObject({ text: expect.stringContaining("provider fallback") });
85
- });
86
-
87
- it("user config false beats consumer true (explicit user disable)", async () => {
88
- process.env.BRAVE_SEARCH_API_KEY = "k";
89
- writeConfig({ provider: "brave", interceptors: { github: false } });
90
- stubFetch([
91
- {
92
- match: () => true,
93
- response: () =>
94
- new Response("<html><body>provider only</body></html>", {
95
- status: 200,
96
- headers: { "content-type": "text/html" },
97
- }),
98
- },
99
- ]);
100
- const { pi, captured } = createMockPi();
101
- registerWebTools(pi, { interceptors: { github: true } });
102
- const r = await captured.tools
103
- .get("web_fetch")
104
- ?.execute?.(
105
- "tc",
106
- { url: "https://github.com/owner/repo/blob/main/x.ts" },
107
- undefined as never,
108
- undefined as never,
109
- createMockCtx(),
110
- );
111
- // Interceptor is off — provider handled it.
112
- expect(r?.content[0]).toMatchObject({ text: expect.stringContaining("provider only") });
113
- });
114
-
115
- it("user object form implies opt-in even when consumer left it unset", async () => {
116
- // With the interceptor active and gh CLI absent in test env, the interceptor
117
- // tries clone+API, both fail, and intercept() returns null. The chain falls
118
- // through to the provider — i.e. the interceptor *ran* (rather than being
119
- // skipped entirely as it would be when disabled). We verify this indirectly
120
- // via no provider fetch happening when intercept actually succeeds in tests
121
- // that use real network mocks; here we just confirm registration accepts it.
122
- process.env.BRAVE_SEARCH_API_KEY = "k";
123
- writeConfig({ provider: "brave", interceptors: { github: { maxRepoSizeMB: 999 } } });
124
- stubFetch([
125
- {
126
- match: () => true,
127
- response: () =>
128
- new Response("<html><body>provider fallback</body></html>", {
129
- status: 200,
130
- headers: { "content-type": "text/html" },
131
- }),
132
- },
133
- ]);
134
- const { pi, captured } = createMockPi();
135
- registerWebTools(pi);
136
- const r = await captured.tools
137
- .get("web_fetch")
138
- ?.execute?.(
139
- "tc",
140
- { url: "https://github.com/owner/repo/issues" },
141
- undefined as never,
142
- undefined as never,
143
- createMockCtx(),
144
- );
145
- expect(r?.content[0]).toMatchObject({ text: expect.stringContaining("provider fallback") });
146
- });
147
- });
148
-
149
- describe("interceptor chain — fall-through semantics", () => {
150
- it("interceptor returning null falls through to provider fetch", async () => {
151
- // owner/repo/issues → NON_CODE_SEGMENTS → parseGitHubUrl returns null,
152
- // intercept short-circuits to null → provider handles the URL.
153
- process.env.BRAVE_SEARCH_API_KEY = "k";
154
- writeConfig({ provider: "brave", interceptors: { github: true } });
155
- stubFetch([
156
- {
157
- match: () => true,
158
- response: () =>
159
- new Response("<html><body>fallback body</body></html>", {
160
- status: 200,
161
- headers: { "content-type": "text/html" },
162
- }),
163
- },
164
- ]);
165
- const { pi, captured } = createMockPi();
166
- registerWebTools(pi);
167
- const r = await captured.tools
168
- .get("web_fetch")
169
- ?.execute?.(
170
- "tc",
171
- { url: "https://github.com/owner/repo/issues" },
172
- undefined as never,
173
- undefined as never,
174
- createMockCtx(),
175
- );
176
- expect(r?.content[0]).toMatchObject({ text: expect.stringContaining("fallback body") });
177
- });
178
-
179
- it("empty chain (interceptor disabled) is a no-op — every URL hits the provider", async () => {
180
- process.env.BRAVE_SEARCH_API_KEY = "k";
181
- writeConfig({ provider: "brave" });
182
- stubFetch([
183
- {
184
- match: () => true,
185
- response: () =>
186
- new Response("<html><body>vanilla</body></html>", {
187
- status: 200,
188
- headers: { "content-type": "text/html" },
189
- }),
190
- },
191
- ]);
192
- const { pi, captured } = createMockPi();
193
- registerWebTools(pi);
194
- const r1 = await captured.tools
195
- .get("web_fetch")
196
- ?.execute?.("tc", { url: "https://example.com/" }, undefined as never, undefined as never, createMockCtx());
197
- const r2 = await captured.tools
198
- .get("web_fetch")
199
- ?.execute?.(
200
- "tc",
201
- { url: "https://github.com/owner/repo/blob/main/file.ts" },
202
- undefined as never,
203
- undefined as never,
204
- createMockCtx(),
205
- );
206
- expect(r1?.content[0]).toMatchObject({ text: expect.stringContaining("vanilla") });
207
- expect(r2?.content[0]).toMatchObject({ text: expect.stringContaining("vanilla") });
208
- });
209
- });