-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_index.test.js
More file actions
640 lines (552 loc) · 20.5 KB
/
Copy pathtest_index.test.js
File metadata and controls
640 lines (552 loc) · 20.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
/**
* Unit tests for searchfetch index.js helpers.
*
* Run with: node --test test_index.test.js
*
* These tests verify pure helper functions without starting the MCP server.
* No browser, no network — just logic correctness.
*/
import { describe, it } from "node:test";
import assert from "node:assert/strict";
import { mkdtempSync, realpathSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { fileURLToPath } from "node:url";
import {
loadBuiltinTemplates,
getTemplateByName,
detectTemplateByUrl,
resolveUrlTemplate,
resolveEngineToTemplateName,
mapSearchParams,
isMarkdownContent,
stripSourceMarkdown,
resolveSourceUrl,
parseRetryAfterMs,
isAccessDenied,
applyTransform,
composeSections,
composeSearchResults,
isMainModule,
BUILTIN_TEMPLATES,
TEMPLATE_MAP,
} from "./index.js";
// ---------------------------------------------------------------------------
// 1. Template loading
// ---------------------------------------------------------------------------
describe("Template loading", () => {
it("BUILTIN_TEMPLATES is a non-empty array", () => {
assert.ok(Array.isArray(BUILTIN_TEMPLATES));
assert.ok(BUILTIN_TEMPLATES.length > 0);
});
it("TEMPLATE_MAP has entries matching BUILTIN_TEMPLATES", () => {
assert.equal(TEMPLATE_MAP.size, BUILTIN_TEMPLATES.length);
for (const t of BUILTIN_TEMPLATES) {
assert.ok(TEMPLATE_MAP.has(t.name), `Missing template: ${t.name}`);
}
});
it("every template has a name and order", () => {
for (const t of BUILTIN_TEMPLATES) {
assert.ok(typeof t.name === "string" && t.name.length > 0);
assert.ok(typeof t.order === "number");
}
});
it("templates are sorted by order ascending", () => {
for (let i = 1; i < BUILTIN_TEMPLATES.length; i++) {
assert.ok(
BUILTIN_TEMPLATES[i - 1].order <= BUILTIN_TEMPLATES[i].order,
`Templates not sorted: ${BUILTIN_TEMPLATES[i - 1].name} (${BUILTIN_TEMPLATES[i - 1].order}) vs ${BUILTIN_TEMPLATES[i].name} (${BUILTIN_TEMPLATES[i].order})`,
);
}
});
it("github-repo and github-issue have lowest orders", () => {
const ghRepo = TEMPLATE_MAP.get("github-repo");
const ghIssue = TEMPLATE_MAP.get("github-issue");
assert.ok(ghRepo);
assert.ok(ghIssue);
for (const t of BUILTIN_TEMPLATES) {
if (t.name === "github-repo" || t.name === "github-issue") continue;
assert.ok(ghRepo.order < t.order, `github-repo should precede ${t.name}`);
assert.ok(ghIssue.order < t.order, `github-issue should precede ${t.name}`);
}
});
it("docs-rs comes before docs-page", () => {
const docsRs = TEMPLATE_MAP.get("docs-rs");
const docsPage = TEMPLATE_MAP.get("docs-page");
assert.ok(docsRs);
assert.ok(docsPage);
assert.ok(docsRs.order < docsPage.order, "docs-rs should precede docs-page");
});
it("known templates exist", () => {
const names = [
"docs-rs",
"docker-hub",
"docs-page",
"crates-package",
"npm-package",
"pypi-package",
"github-repo",
"github-issue",
"duckduckgo-search",
"google-search",
"wikipedia",
"reddit",
"mdn-web-docs",
"gitlab",
"youtube",
"devto",
"go-pkg",
"javadoc",
"raw",
];
for (const name of names) {
assert.ok(TEMPLATE_MAP.has(name), `Expected template '${name}' to exist`);
}
});
});
// ---------------------------------------------------------------------------
// 2. Template lookup
// ---------------------------------------------------------------------------
describe("Template lookup", () => {
it("getTemplateByName returns template for known name", () => {
const t = getTemplateByName("docs-rs");
assert.equal(t.name, "docs-rs");
});
it("getTemplateByName throws for unknown name", () => {
assert.throws(() => getTemplateByName("nonexistent-template"), /Unknown template/);
});
});
// ---------------------------------------------------------------------------
// 3. URL template resolution
// ---------------------------------------------------------------------------
describe("URL template resolution", () => {
it("resolves simple placeholders", () => {
const tmpl = {
name: "test",
url_template: "https://example.com/{path}",
url_params: { path: { default: "home" } },
};
const url = resolveUrlTemplate(tmpl, { path: "about" });
assert.equal(url, "https://example.com/about");
});
it("uses default values when param not provided", () => {
const tmpl = {
name: "test",
url_template: "https://example.com/{page}",
url_params: { page: { default: "index" } },
};
const url = resolveUrlTemplate(tmpl, {});
assert.equal(url, "https://example.com/index");
});
it("throws for missing required params", () => {
const tmpl = {
name: "test",
url_template: "https://example.com/{query}",
url_params: { query: { required: true } },
};
assert.throws(() => resolveUrlTemplate(tmpl, {}), /Required URL parameter/);
});
it("URL-encodes when encode is 'url'", () => {
const tmpl = {
name: "test",
url_template: "https://example.com/?q={query}",
url_params: { query: { encode: "url" } },
};
const url = resolveUrlTemplate(tmpl, { query: "hello world" });
assert.equal(url, "https://example.com/?q=hello%20world");
});
});
// ---------------------------------------------------------------------------
// 4. Engine → template name resolution
// ---------------------------------------------------------------------------
describe("Engine-to-template resolution", () => {
it('maps "duckduckgo" to "duckduckgo-search"', () => {
assert.equal(resolveEngineToTemplateName("duckduckgo"), "duckduckgo-search");
});
it('maps "google" to "google-search"', () => {
assert.equal(resolveEngineToTemplateName("google"), "google-search");
});
it("passes through unknown engine names", () => {
assert.equal(resolveEngineToTemplateName("custom-engine"), "custom-engine");
});
});
// ---------------------------------------------------------------------------
// 5. URL pattern matching (auto template detection)
// ---------------------------------------------------------------------------
describe("URL pattern matching", () => {
it("detects docs.rs URLs", () => {
const urls = ["https://docs.rs/tokio/latest/tokio/", "https://docs.rs/serde/latest/serde/"];
for (const url of urls) {
const t = detectTemplateByUrl(url);
assert.ok(t, `URL ${url} should match a template`);
assert.equal(t.name, "docs-rs");
}
});
it("detects GitHub repo URLs", () => {
const t = detectTemplateByUrl("https://github.com/maxylev/searchfetch");
assert.ok(t);
assert.equal(t.name, "github-repo");
});
it("detects GitHub issue/PR URLs", () => {
const t = detectTemplateByUrl("https://github.com/maxylev/searchfetch/issues/1");
assert.ok(t);
assert.equal(t.name, "github-issue");
});
it("detects npm package URLs", () => {
const t = detectTemplateByUrl("https://www.npmjs.com/package/react");
assert.ok(t);
assert.equal(t.name, "npm-package");
});
it("detects PyPI package URLs", () => {
const t = detectTemplateByUrl("https://pypi.org/project/requests/");
assert.ok(t);
assert.equal(t.name, "pypi-package");
});
it("detects crates.io URLs", () => {
const t = detectTemplateByUrl("https://crates.io/crates/serde");
assert.ok(t);
assert.equal(t.name, "crates-package");
});
it("detects Docker Hub URLs", () => {
const t = detectTemplateByUrl("https://hub.docker.com/_/postgres");
assert.ok(t);
assert.equal(t.name, "docker-hub");
});
it("detects docs-page URLs", () => {
const t = detectTemplateByUrl("https://docs.python.org/3/library/os.html");
assert.ok(t);
assert.equal(t.name, "docs-page");
});
it("returns null for unmatched URLs", () => {
const t = detectTemplateByUrl("https://example.com/random-page");
assert.equal(t, null);
});
it("docs.rs matches before docs-page", () => {
// docs.rs URLs also match docs-page patterns, but docs-rs should match first
const t = detectTemplateByUrl("https://docs.rs/solana/latest/solana/");
assert.ok(t);
assert.equal(t.name, "docs-rs", "docs.rs should match docs-rs, not docs-page");
});
});
// ---------------------------------------------------------------------------
// 6. Search parameter mapping
// ---------------------------------------------------------------------------
describe("Search parameter mapping", () => {
it("maps DuckDuckGo region (kl parameter)", () => {
const params = mapSearchParams("duckduckgo", "test query", "us-en", null);
assert.equal(params.query, "test query");
assert.equal(params.kl, "us-en");
});
it("maps DuckDuckGo safe search on (kp=1)", () => {
const params = mapSearchParams("duckduckgo", "test", null, true);
assert.equal(params.kp, "1");
});
it("maps DuckDuckGo safe search off (kp=-2)", () => {
const params = mapSearchParams("duckduckgo", "test", null, false);
assert.equal(params.kp, "-2");
});
it("maps Google region (hl/gl parameters)", () => {
const params = mapSearchParams("google", "test query", "us-en", null);
assert.equal(params.query, "test query");
assert.equal(params.hl, "us");
assert.equal(params.gl, "en");
});
it("maps Google region with single component", () => {
const params = mapSearchParams("google", "test", "de", null);
assert.equal(params.hl, "de");
assert.equal(params.gl, "de");
});
it("no region/safe_search when null", () => {
const params = mapSearchParams("duckduckgo", "test", null, null);
assert.equal(params.query, "test");
assert.equal(params.kl, undefined);
assert.equal(params.kp, undefined);
});
});
// ---------------------------------------------------------------------------
// 7. Transforms
// ---------------------------------------------------------------------------
describe("Transforms", () => {
it("strip transform trims whitespace", () => {
assert.equal(applyTransform(" hello \n", "strip"), "hello");
});
it("decode_google_url decodes /url?q= links", () => {
const input = "/url?q=https://example.com/page&sa=U&ved=...";
const result = applyTransform(input, "decode_google_url");
assert.equal(result, "https://example.com/page");
});
it("decode_google_url leaves non-google URLs unchanged", () => {
const input = "https://example.com/page";
assert.equal(applyTransform(input, "decode_google_url"), input);
});
it("resolve_href resolves relative URLs", () => {
assert.equal(
applyTransform("/docs/api", "resolve_href", "https://example.com"),
"https://example.com/docs/api",
);
});
it("resolve_href leaves absolute URLs unchanged", () => {
assert.equal(
applyTransform("https://other.com/page", "resolve_href", "https://example.com"),
"https://other.com/page",
);
});
it("applyTransform handles array of transforms", () => {
const input = "/url?q=https://example.com/page&sa=U";
const result = applyTransform(input, ["decode_google_url", "strip"]);
assert.equal(result, "https://example.com/page");
});
});
// ---------------------------------------------------------------------------
// 8. Markdown content detection
// ---------------------------------------------------------------------------
describe("Markdown content detection", () => {
it("detects headings", () => {
assert.ok(isMarkdownContent("# Hello World"));
});
it("detects links", () => {
assert.ok(isMarkdownContent("See [docs](https://example.com) for more."));
});
it("detects code fences", () => {
assert.ok(isMarkdownContent("```python\nprint('hello')\n```"));
});
it("detects bold", () => {
assert.ok(isMarkdownContent("This is **bold** text."));
});
it("detects blockquote", () => {
assert.ok(isMarkdownContent("> This is a quote"));
});
it("detects unordered lists", () => {
assert.ok(isMarkdownContent("- item 1\n- item 2"));
});
it("rejects HTML", () => {
assert.ok(!isMarkdownContent("<html><body><h1>Hello</h1></body></html>"));
});
it("rejects empty string", () => {
assert.ok(!isMarkdownContent(""));
});
it("rejects plain text", () => {
assert.ok(!isMarkdownContent("Just some plain text."));
});
});
// ---------------------------------------------------------------------------
// 8.1. Twoslash stripping
// ---------------------------------------------------------------------------
describe("Twoslash stripping", () => {
it("strips twoslash cache lines", () => {
const input =
"# Title\n\nSome text\n@twoslash-cache: abcdef1234567890abcdef1234567890\nMore text";
const result = stripSourceMarkdown(input);
assert.ok(!result.includes("@twoslash-cache:"));
assert.ok(result.includes("# Title"));
assert.ok(result.includes("More text"));
});
it("strips multiple twoslash lines", () => {
const input = [
"# Title",
"@twoslash-cache: hash1",
"content",
"@twoslash-cache: hash2",
"@twoslash-cache: hash3",
"footer",
].join("\n");
const result = stripSourceMarkdown(input);
assert.ok(!result.includes("@twoslash-cache:"));
assert.equal(result, "# Title\n\ncontent\n\nfooter");
});
it("collapses excess blank lines", () => {
const input = "# Title\n\n\n\n\n\nContent\n\n\n\n\n\nFooter";
const result = stripSourceMarkdown(input);
assert.ok(!result.includes("\n\n\n\n"));
assert.equal(result, "# Title\n\nContent\n\nFooter");
});
});
// ---------------------------------------------------------------------------
// 9. Source URL resolution
// ---------------------------------------------------------------------------
describe("Source URL resolution", () => {
it("resolves {url}.md pattern", () => {
const result = resolveSourceUrl("{url}.md", "https://example.com/docs/api");
assert.equal(result, "https://example.com/docs/api.md");
});
it("strips trailing slash before appending .md", () => {
const result = resolveSourceUrl("{url}.md", "https://example.com/docs/api/");
assert.equal(result, "https://example.com/docs/api.md");
});
it("resolves generic {url} replacement", () => {
const result = resolveSourceUrl(
"https://raw.example.com/{url}",
"https://example.com/docs/api",
);
assert.equal(result, "https://raw.example.com/https://example.com/docs/api");
});
});
// ---------------------------------------------------------------------------
// 10. Parse retry-after
// ---------------------------------------------------------------------------
describe("parseRetryAfterMs", () => {
it("parses numeric seconds", () => {
assert.equal(parseRetryAfterMs("5"), 5000);
});
it("defaults to 2000ms for missing header", () => {
assert.equal(parseRetryAfterMs(null), 2000);
assert.equal(parseRetryAfterMs(undefined), 2000);
assert.equal(parseRetryAfterMs(""), 2000);
});
it("caps at 30000ms", () => {
assert.equal(parseRetryAfterMs("999"), 30000);
});
});
describe("Access-denied detection", () => {
it("detects long Google unusual-traffic pages", async () => {
const cheerio = await import("cheerio");
const filler = "ordinary text ".repeat(200);
const $ = cheerio.load(
`<html><body>${filler}Our systems have detected unusual traffic from your computer network.</body></html>`,
);
assert.equal(isAccessDenied($), true);
});
});
// ---------------------------------------------------------------------------
// 11. Composition helpers (no cheerio/HTML needed)
// ---------------------------------------------------------------------------
describe("Composition helpers", () => {
it("composeSections handles empty results", () => {
const result = composeSections([], null, 0, 10000);
assert.ok(result.includes("(No content extracted"));
});
it("composeSections emits value sections as markdown headings", () => {
const extracted = [
{
type: "value",
section: { name: "Title" },
text: "Hello World",
},
];
const result = composeSections(extracted, null, 0, 10000);
assert.ok(result.includes("## Title"));
assert.ok(result.includes("Hello World"));
});
it("composeSections includes pagination metadata", () => {
const extracted = [
{
type: "value",
section: { name: "Content" },
text: "A".repeat(200),
},
];
const result = composeSections(extracted, null, 0, 100);
assert.ok(result.includes("showing characters 0 to"));
assert.ok(result.includes("Use start_index="));
});
it("composeSearchResults falls back to sections when no results", () => {
const extracted = [];
const result = composeSearchResults(extracted);
assert.ok(result.includes("(No content extracted"));
});
it("composeSearchResults filters non-http URLs", () => {
const extracted = [
{
type: "children-multiple",
section: { name: "Results" },
items: [{ Title: "Test 1", URL: "/internal/page", Snippet: "desc" }],
},
];
const result = composeSearchResults(extracted);
// No http URL, so URL line should be absent
assert.ok(!result.includes("URL:"));
assert.ok(result.includes("[1] Test 1"));
});
it("composeSearchResults filters Google internal links", () => {
const extracted = [
{
type: "children-multiple",
section: { name: "Results" },
items: [
{
Title: "Test",
URL: "https://www.google.com/search?q=test",
Snippet: "desc",
},
],
},
];
const result = composeSearchResults(extracted);
assert.ok(!result.includes("URL:"));
});
});
// ---------------------------------------------------------------------------
// 12. Server does NOT start on import (import guard verification)
// ---------------------------------------------------------------------------
describe("Import without run", () => {
it("importing index.js does not start the MCP server", () => {
// If we get here without the server starting (no error about stdio),
// the import guard is working. We can also check that exported symbols exist.
assert.ok(typeof loadBuiltinTemplates === "function");
assert.ok(Array.isArray(BUILTIN_TEMPLATES));
});
it("all expected exports are present", () => {
const expected = [
"loadBuiltinTemplates",
"getTemplateByName",
"detectTemplateByUrl",
"resolveUrlTemplate",
"resolveEngineToTemplateName",
"mapSearchParams",
"isMarkdownContent",
"stripSourceMarkdown",
"resolveSourceUrl",
"parseRetryAfterMs",
"isAccessDenied",
"applyTransform",
"composeSections",
"composeSearchResults",
"genericFallback",
"isMainModule",
"BUILTIN_TEMPLATES",
"TEMPLATE_MAP",
];
for (const name of expected) {
assert.ok(imported[name] !== undefined, `Expected export '${name}' not found`);
}
});
});
// ---------------------------------------------------------------------------
// 13. Main guard: realpath-based argv[1] check for npx / symlink support
// ---------------------------------------------------------------------------
describe("Main guard (realpath-based)", () => {
const indexFile = fileURLToPath(new URL("./index.js", import.meta.url).href);
const indexRealPath = realpathSync(indexFile);
it("returns true for direct execution path", () => {
assert.equal(isMainModule(indexFile, indexRealPath), true);
});
it("returns true for npm/npx bin symlink path", () => {
const tmp = mkdtempSync(join(tmpdir(), "searchfetch-guard-"));
const link = join(tmp, "searchfetch");
try {
symlinkSync(indexFile, link);
assert.equal(isMainModule(link, indexRealPath), true);
} finally {
rmSync(tmp, { recursive: true, force: true });
}
});
it("returns false for a different executable path", () => {
const tmp = mkdtempSync(join(tmpdir(), "searchfetch-guard-"));
const otherFile = join(tmp, "other.js");
try {
writeFileSync(otherFile, "// not the searchfetch entrypoint\n", "utf8");
assert.equal(isMainModule(otherFile, indexRealPath), false);
} finally {
rmSync(tmp, { recursive: true, force: true });
}
});
it("returns false for missing or unresolvable argv[1]", () => {
assert.equal(isMainModule(undefined, indexRealPath), false);
assert.equal(
isMainModule(join(tmpdir(), "definitely-missing-searchfetch-entry"), indexRealPath),
false,
);
});
});
// Re-import everything for the export-check in the test above
import * as imported from "./index.js";