torrentclaw-mcp/tests/tools/get-torrent-url.test.ts
Deivid Soto d471c9b695 feat: initial release of torrentclaw-mcp server
MCP (Model Context Protocol) server that wraps the TorrentClaw REST API,
enabling LLMs (Claude Desktop, Claude Code, Cursor, etc.) to search movies
and TV shows with torrent downloads and streaming availability.

## Tools (6)
- search_content: primary search with filters (title, genre, year, rating,
  quality, language, sort). Returns content metadata + torrent magnet links.
- get_popular: trending content ranked by clicks
- get_recent: most recently added content
- get_watch_providers: streaming availability by country (Netflix, Disney+, etc.)
- get_credits: director and top 10 cast members
- get_torrent_url: .torrent file download URL from info_hash

## Resources
- torrentclaw://stats: catalog statistics (content/torrent counts, sources)

## Prompts (4)
- search_movie, search_show, whats_new, where_to_watch

## LLM Usability
- Server description with workflow guidance for tool chaining
- Tool descriptions include trigger phrases, cross-tool references, and
  explicit parameter examples
- Formatted output includes info_hash for tool chaining and call-syntax
  cross-references (e.g. "use with get_watch_providers(content_id=42)")
- Popular/recent output hints to use search_content for torrents
- Error messages include status-specific recovery guidance (400, 404, 429, 5xx)
- Prompts reference tools by name for reliable LLM execution

## Security
- SSRF protection: validates TORRENTCLAW_API_URL against private IPs,
  localhost, link-local, and IPv6 loopback addresses
- Protocol whitelist: only http/https allowed
- Error body sanitization: 4xx truncated to 200 chars, 5xx bodies omitted
- Input validation: control char filter on queries, regex on country codes,
  genre character whitelist, content_id bounds

## Testing
- 85 tests across 13 test files
- Coverage: 99.5% statements, 95.2% branches, 98.1% functions, 99.5% lines
- Vitest v4 with v8 coverage provider, 80% thresholds enforced

## Stack
- TypeScript ESM, Node.js >= 18 (native fetch)
- @modelcontextprotocol/sdk v1.12, zod v3.24
- STDIO transport, runnable via npx torrentclaw-mcp
2026-02-09 17:26:23 +01:00

58 lines
1.9 KiB
TypeScript

import { describe, it, expect, vi } from "vitest";
import { createMockServer } from "../helpers.js";
import { registerGetTorrentUrl } from "../../src/tools/get-torrent-url.js";
import { TorrentClawClient } from "../../src/api-client.js";
function createMockClient(overrides: Partial<TorrentClawClient> = {}) {
return {
search: vi.fn(),
getPopular: vi.fn(),
getRecent: vi.fn(),
getWatchProviders: vi.fn(),
getCredits: vi.fn(),
getStats: vi.fn(),
getTorrentDownloadUrl: vi
.fn()
.mockImplementation(
(hash: string) =>
`https://torrentclaw.com/api/v1/torrent/${hash}`,
),
...overrides,
} as unknown as TorrentClawClient;
}
describe("get_torrent_url tool", () => {
it("returns download URL for valid info hash", async () => {
const client = createMockClient();
const { server, getToolHandler } = createMockServer();
registerGetTorrentUrl(server, client);
const handler = getToolHandler("get_torrent_url");
const hash = "aaf1e71c0a0e3b1c0f1a2b3c4d5e6f7a8b9c0d1e";
const result = await handler({ info_hash: hash });
expect(result.content[0].text).toContain("Download .torrent file:");
expect(result.content[0].text).toContain(hash);
expect(result.isError).toBeUndefined();
});
it("lowercases the info hash", async () => {
const getTorrentDownloadUrlMock = vi
.fn()
.mockReturnValue("https://torrentclaw.com/api/v1/torrent/abc");
const client = createMockClient({
getTorrentDownloadUrl: getTorrentDownloadUrlMock,
});
const { server, getToolHandler } = createMockServer();
registerGetTorrentUrl(server, client);
const handler = getToolHandler("get_torrent_url");
await handler({
info_hash: "AAF1E71C0A0E3B1C0F1A2B3C4D5E6F7A8B9C0D1E",
});
expect(getTorrentDownloadUrlMock).toHaveBeenCalledWith(
"aaf1e71c0a0e3b1c0f1a2b3c4d5e6f7a8b9c0d1e",
);
});
});