|
| 1 | +import { beforeEach, describe, expect, test, vi } from "vitest"; |
| 2 | +import { conversationTopics, ToolDependencies } from "./conversationTopics.js"; |
| 3 | +import { MockedObjectDeep } from "@vitest/spy"; |
| 4 | +import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; |
| 5 | +import { Client } from "@modelcontextprotocol/sdk/client/index.js"; |
| 6 | +import { InMemoryTransport } from "@modelcontextprotocol/sdk/inMemory.js"; |
| 7 | +import { randomUUID } from "node:crypto"; |
| 8 | +import { McpError } from "@modelcontextprotocol/sdk/types.js"; |
| 9 | + |
| 10 | +describe("Conversation Topics Tool", () => { |
| 11 | + let toolDeps: MockedObjectDeep<ToolDependencies>; |
| 12 | + let client: Client; |
| 13 | + let toolName: string; |
| 14 | + |
| 15 | + beforeEach(async () => { |
| 16 | + toolDeps = { |
| 17 | + speechTextAnalyticsApi: { |
| 18 | + getSpeechandtextanalyticsTopics: vi.fn(), |
| 19 | + }, |
| 20 | + analyticsApi: { |
| 21 | + getAnalyticsConversationDetails: vi.fn(), |
| 22 | + postAnalyticsTranscriptsAggregatesQuery: vi.fn(), |
| 23 | + }, |
| 24 | + }; |
| 25 | + |
| 26 | + const toolDefinition = conversationTopics(toolDeps); |
| 27 | + toolName = toolDefinition.schema.name; |
| 28 | + |
| 29 | + const server = new McpServer({ name: "TestServer", version: "test" }); |
| 30 | + server.tool( |
| 31 | + toolDefinition.schema.name, |
| 32 | + toolDefinition.schema.description, |
| 33 | + toolDefinition.schema.paramsSchema.shape, |
| 34 | + toolDefinition.call, |
| 35 | + ); |
| 36 | + |
| 37 | + const [serverTransport, clientTransport] = |
| 38 | + InMemoryTransport.createLinkedPair(); |
| 39 | + |
| 40 | + await server.connect(serverTransport); |
| 41 | + |
| 42 | + client = new Client({ name: "test-client", version: "1.0.0" }); |
| 43 | + await client.connect(clientTransport); |
| 44 | + }); |
| 45 | + |
| 46 | + test("schema describes tool", async () => { |
| 47 | + const tools = await client.listTools(); |
| 48 | + expect(tools.tools[0]).toStrictEqual({ |
| 49 | + name: "conversation_topics", |
| 50 | + description: |
| 51 | + "Retrieves Speech and Text Analytics topics detected for a specific conversation. Topics represent business-level intents (e.g. cancellation, billing enquiry) inferred from recognised phrases in the customer-agent interaction.", |
| 52 | + inputSchema: { |
| 53 | + type: "object", |
| 54 | + properties: { |
| 55 | + conversationId: { |
| 56 | + description: |
| 57 | + "A UUID ID for a conversation. (e.g., 00000000-0000-0000-0000-000000000000)", |
| 58 | + format: "uuid", |
| 59 | + type: "string", |
| 60 | + }, |
| 61 | + }, |
| 62 | + required: ["conversationId"], |
| 63 | + additionalProperties: false, |
| 64 | + $schema: "http://json-schema.org/draft-07/schema#", |
| 65 | + }, |
| 66 | + annotations: undefined, |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + test("errors when no conversation ID provided", async () => { |
| 71 | + await expect( |
| 72 | + client.callTool({ |
| 73 | + name: toolName, |
| 74 | + arguments: { |
| 75 | + conversationId: "", |
| 76 | + }, |
| 77 | + }), |
| 78 | + ).rejects.toSatisfy( |
| 79 | + (error: McpError) => |
| 80 | + error.name === "McpError" && |
| 81 | + error.message.includes("conversationId") && |
| 82 | + error.message.includes("Invalid uuid"), |
| 83 | + ); |
| 84 | + }); |
| 85 | + |
| 86 | + test("sentiment returned for single conversation", async () => { |
| 87 | + const conversationId = randomUUID(); |
| 88 | + |
| 89 | + toolDeps.analyticsApi.getAnalyticsConversationDetails.mockResolvedValue({ |
| 90 | + conversationStart: "2025-05-19T20:00:07.395Z", |
| 91 | + conversationEnd: "2025-05-19T21:00:52.686Z", |
| 92 | + }); |
| 93 | + toolDeps.analyticsApi.postAnalyticsTranscriptsAggregatesQuery.mockResolvedValue( |
| 94 | + { |
| 95 | + results: [ |
| 96 | + { group: { topicId: "test-topic-id-1" } }, |
| 97 | + { group: { topicId: "test-topic-id-2" } }, |
| 98 | + ], |
| 99 | + }, |
| 100 | + ); |
| 101 | + toolDeps.speechTextAnalyticsApi.getSpeechandtextanalyticsTopics.mockResolvedValue( |
| 102 | + { |
| 103 | + entities: [ |
| 104 | + { |
| 105 | + name: "Test Topic 1", |
| 106 | + description: "Test Topic 1 Desc", |
| 107 | + }, |
| 108 | + { |
| 109 | + name: "Test Topic 2", |
| 110 | + description: "Test Topic 2 Desc", |
| 111 | + }, |
| 112 | + ], |
| 113 | + }, |
| 114 | + ); |
| 115 | + |
| 116 | + const result = await client.callTool({ |
| 117 | + name: toolName, |
| 118 | + arguments: { |
| 119 | + conversationId: conversationId, |
| 120 | + }, |
| 121 | + }); |
| 122 | + |
| 123 | + expect(result).toStrictEqual({ |
| 124 | + content: [ |
| 125 | + { |
| 126 | + type: "text", |
| 127 | + text: ` |
| 128 | +Conversation ID: ${conversationId} |
| 129 | +Detected Topics: |
| 130 | + • Test Topic 1: Test Topic 1 Desc |
| 131 | + • Test Topic 2: Test Topic 2 Desc |
| 132 | +`.trim(), |
| 133 | + }, |
| 134 | + ], |
| 135 | + }); |
| 136 | + }); |
| 137 | +}); |
0 commit comments