|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | +import { z } from 'zod'; |
| 3 | +import { registerGraphTools } from '../src/graph-tools.js'; |
| 4 | +import type { GraphClient } from '../src/graph-client.js'; |
| 5 | + |
| 6 | +vi.mock('../src/logger.js', () => ({ |
| 7 | + default: { |
| 8 | + info: vi.fn(), |
| 9 | + error: vi.fn(), |
| 10 | + warn: vi.fn(), |
| 11 | + }, |
| 12 | +})); |
| 13 | + |
| 14 | +vi.mock('../src/generated/client-beta.js', () => ({ api: { endpoints: [] } })); |
| 15 | +vi.mock('../src/generated/client.js', () => ({ |
| 16 | + api: { |
| 17 | + endpoints: [ |
| 18 | + { |
| 19 | + // endpoints.json configures acceptType 'text/vtt' for this tool |
| 20 | + alias: 'get-meeting-transcript-content', |
| 21 | + method: 'get', |
| 22 | + path: '/me/onlineMeetings/:onlineMeetingId/transcripts/:callTranscriptId/content', |
| 23 | + description: 'Transcript content.', |
| 24 | + parameters: [ |
| 25 | + { name: 'onlineMeetingId', type: 'Path', schema: z.string() }, |
| 26 | + { name: 'callTranscriptId', type: 'Path', schema: z.string() }, |
| 27 | + ], |
| 28 | + }, |
| 29 | + ], |
| 30 | + }, |
| 31 | +})); |
| 32 | + |
| 33 | +describe('explicit Accept header', () => { |
| 34 | + let mockServer: { tool: ReturnType<typeof vi.fn>; registerTool: ReturnType<typeof vi.fn> }; |
| 35 | + let mockGraphClient: GraphClient; |
| 36 | + |
| 37 | + beforeEach(() => { |
| 38 | + vi.clearAllMocks(); |
| 39 | + mockServer = { tool: vi.fn(), registerTool: vi.fn() }; |
| 40 | + mockGraphClient = { |
| 41 | + graphRequest: vi.fn().mockResolvedValue({ |
| 42 | + content: [{ type: 'text', text: 'WEBVTT' }], |
| 43 | + }), |
| 44 | + } as unknown as GraphClient; |
| 45 | + }); |
| 46 | + |
| 47 | + function getRegistration(toolName: string) { |
| 48 | + // transcript tools are work-scoped only, so they need org mode to register |
| 49 | + registerGraphTools(mockServer, mockGraphClient, false, undefined, true); |
| 50 | + const call = mockServer.registerTool.mock.calls.find((c: unknown[]) => c[0] === toolName); |
| 51 | + expect(call).toBeDefined(); |
| 52 | + return call!; |
| 53 | + } |
| 54 | + |
| 55 | + function getToolHandler(toolName: string) { |
| 56 | + const call = getRegistration(toolName); |
| 57 | + return call[call.length - 1] as (params: Record<string, unknown>) => Promise<unknown>; |
| 58 | + } |
| 59 | + |
| 60 | + function getParamSchema(toolName: string) { |
| 61 | + const call = getRegistration(toolName); |
| 62 | + const { inputSchema } = call[1] as { inputSchema: z.ZodObject<z.ZodRawShape> }; |
| 63 | + return inputSchema.shape; |
| 64 | + } |
| 65 | + |
| 66 | + function sentHeaders() { |
| 67 | + const call = (mockGraphClient.graphRequest as ReturnType<typeof vi.fn>).mock.calls[0]; |
| 68 | + return (call[1] as { headers: Record<string, string> }).headers; |
| 69 | + } |
| 70 | + |
| 71 | + it('exposes an Accept param for tools with a configured acceptType', () => { |
| 72 | + // The generated client declares no Accept header on any endpoint, so without the |
| 73 | + // synthetic param the caller has no way to send one and the override is unreachable. |
| 74 | + const schema = getParamSchema('get-meeting-transcript-content'); |
| 75 | + |
| 76 | + expect(schema['Accept']).toBeDefined(); |
| 77 | + expect(schema['Accept'].isOptional()).toBe(true); |
| 78 | + }); |
| 79 | + |
| 80 | + it('falls back to the configured acceptType when the caller sends none', async () => { |
| 81 | + const handler = getToolHandler('get-meeting-transcript-content'); |
| 82 | + |
| 83 | + await handler({ onlineMeetingId: 'meeting-1', callTranscriptId: 'transcript-1' }); |
| 84 | + |
| 85 | + expect(sentHeaders()['Accept']).toBe('text/vtt'); |
| 86 | + }); |
| 87 | + |
| 88 | + it('keeps an explicitly provided Accept header instead of overwriting it', async () => { |
| 89 | + const handler = getToolHandler('get-meeting-transcript-content'); |
| 90 | + |
| 91 | + // Graph asks for this format when speaker-attributed transcripts are |
| 92 | + // disabled for the tenant (403 SpeakerAttributionNotAllowed). Overwriting |
| 93 | + // it with the configured acceptType made that retry impossible. |
| 94 | + await handler({ |
| 95 | + onlineMeetingId: 'meeting-1', |
| 96 | + callTranscriptId: 'transcript-1', |
| 97 | + Accept: 'application/vnd.microsoft.graph.transcript+text', |
| 98 | + }); |
| 99 | + |
| 100 | + expect(sentHeaders()['Accept']).toBe('application/vnd.microsoft.graph.transcript+text'); |
| 101 | + }); |
| 102 | +}); |
0 commit comments