|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import { buildAnnotations, TOOL_KINDS, type ToolName } from "../annotations.js"; |
| 3 | +import { createServer } from "../server.js"; |
| 4 | +import type { SubstackClient } from "../api/client.js"; |
| 5 | + |
| 6 | +// The server only touches the client inside tool handlers, which these tests |
| 7 | +// never invoke — a bare object is enough to register everything. |
| 8 | +const server = createServer({} as SubstackClient); |
| 9 | + |
| 10 | +interface RegisteredToolShape { |
| 11 | + description?: string; |
| 12 | + annotations?: Record<string, unknown>; |
| 13 | +} |
| 14 | + |
| 15 | +// SDK-private registry; the McpServer API has no public tool introspection. |
| 16 | +const registered = ( |
| 17 | + server as unknown as { _registeredTools: Record<string, RegisteredToolShape> } |
| 18 | +)._registeredTools; |
| 19 | + |
| 20 | +const READ_TOOLS: ToolName[] = [ |
| 21 | + "get_subscriber_count", |
| 22 | + "list_published_posts", |
| 23 | + "list_drafts", |
| 24 | + "get_post", |
| 25 | + "get_draft", |
| 26 | + "get_post_comments", |
| 27 | +]; |
| 28 | + |
| 29 | +const ADDITIVE_WRITE_TOOLS: ToolName[] = [ |
| 30 | + "create_draft", |
| 31 | + "update_draft", |
| 32 | + "upload_image", |
| 33 | +]; |
| 34 | + |
| 35 | +const PUBLISH_TOOLS: ToolName[] = ["create_note", "create_note_with_link"]; |
| 36 | + |
| 37 | +describe("buildAnnotations mapping", () => { |
| 38 | + it("read -> { readOnlyHint: true } and nothing else", () => { |
| 39 | + const a = buildAnnotations("get_post"); |
| 40 | + expect(a).toEqual({ readOnlyHint: true }); |
| 41 | + }); |
| 42 | + |
| 43 | + it("additive write -> { readOnlyHint: false } with no destructive or open-world hint", () => { |
| 44 | + const a = buildAnnotations("create_draft"); |
| 45 | + expect(a).toEqual({ readOnlyHint: false }); |
| 46 | + expect(a.destructiveHint).toBeUndefined(); |
| 47 | + expect(a.openWorldHint).toBeUndefined(); |
| 48 | + }); |
| 49 | + |
| 50 | + it("publish (Notes) -> { readOnlyHint: false, openWorldHint: true }", () => { |
| 51 | + const a = buildAnnotations("create_note"); |
| 52 | + expect(a).toEqual({ readOnlyHint: false, openWorldHint: true }); |
| 53 | + }); |
| 54 | +}); |
| 55 | + |
| 56 | +describe("tool annotation classifications", () => { |
| 57 | + it("completeness: registered tools and the classification registry match exactly", () => { |
| 58 | + // Every registered tool must be classified, and every classified tool |
| 59 | + // must exist — new tools cannot slip through unannotated, and stale |
| 60 | + // registry entries cannot linger. |
| 61 | + expect(Object.keys(registered).sort()).toEqual( |
| 62 | + Object.keys(TOOL_KINDS).sort(), |
| 63 | + ); |
| 64 | + }); |
| 65 | + |
| 66 | + it("every registered tool carries the annotations its classification dictates", () => { |
| 67 | + for (const name of Object.keys(TOOL_KINDS) as ToolName[]) { |
| 68 | + expect( |
| 69 | + registered[name].annotations, |
| 70 | + `${name} annotations should match buildAnnotations`, |
| 71 | + ).toEqual(buildAnnotations(name)); |
| 72 | + } |
| 73 | + }); |
| 74 | + |
| 75 | + it("read tools are readOnlyHint:true", () => { |
| 76 | + for (const name of READ_TOOLS) { |
| 77 | + expect(buildAnnotations(name).readOnlyHint, name).toBe(true); |
| 78 | + } |
| 79 | + }); |
| 80 | + |
| 81 | + it("additive writes are readOnlyHint:false with no destructive or open-world hint", () => { |
| 82 | + for (const name of ADDITIVE_WRITE_TOOLS) { |
| 83 | + const a = buildAnnotations(name); |
| 84 | + expect(a.readOnlyHint, name).toBe(false); |
| 85 | + expect(a.destructiveHint, name).toBeUndefined(); |
| 86 | + expect(a.openWorldHint, name).toBeUndefined(); |
| 87 | + } |
| 88 | + }); |
| 89 | + |
| 90 | + it("Note tools (immediate public publish) carry openWorldHint:true", () => { |
| 91 | + for (const name of PUBLISH_TOOLS) { |
| 92 | + const a = buildAnnotations(name); |
| 93 | + expect(a.readOnlyHint, name).toBe(false); |
| 94 | + expect(a.openWorldHint, name).toBe(true); |
| 95 | + } |
| 96 | + }); |
| 97 | + |
| 98 | + it("no tool is destructive (this server has no deletes by design)", () => { |
| 99 | + for (const name of Object.keys(TOOL_KINDS) as ToolName[]) { |
| 100 | + expect(buildAnnotations(name).destructiveHint, name).toBeUndefined(); |
| 101 | + } |
| 102 | + }); |
| 103 | + |
| 104 | + it("Note tool descriptions say loudly that they publish immediately", () => { |
| 105 | + for (const name of PUBLISH_TOOLS) { |
| 106 | + expect(registered[name].description, name).toMatch( |
| 107 | + /publishes immediately/i, |
| 108 | + ); |
| 109 | + } |
| 110 | + }); |
| 111 | + |
| 112 | + it("the classification groups above cover the whole registry", () => { |
| 113 | + const grouped = [...READ_TOOLS, ...ADDITIVE_WRITE_TOOLS, ...PUBLISH_TOOLS]; |
| 114 | + expect(grouped.sort()).toEqual(Object.keys(TOOL_KINDS).sort()); |
| 115 | + }); |
| 116 | +}); |
0 commit comments