UNPKG

@rolme/ytscript

Version:

A CLI tool to download YouTube transcripts and generate summaries

91 lines (90 loc) 3.94 kB
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'; import { SummaryService } from '../../../services/summary.js'; import { AIProviderFactory } from '../../../services/providers/factory.js'; vi.mock('../../../services/providers/factory.js'); describe('SummaryService', () => { let service; let mockProvider; beforeEach(() => { mockProvider = { name: 'google', summarize: vi.fn() }; AIProviderFactory.create.mockReturnValue(mockProvider); }); afterEach(() => { vi.clearAllMocks(); }); describe('summarizeTranscript', () => { const mockSegment = { text: 'Test segment content', duration: 60, offset: 0 }; const mockTranscript = { transcript: 'Test transcript content', segments: [mockSegment], videoId: 'test-video-id' }; const mockSummary = 'Test summary'; beforeEach(() => { mockProvider.summarize.mockResolvedValue(mockSummary); }); it('should summarize transcript with default options', async () => { service = new SummaryService(); const result = await service.summarizeTranscript(mockTranscript); expect(result).toEqual({ ...mockTranscript, summary: mockSummary, provider: 'google' }); expect(mockProvider.summarize).toHaveBeenCalledWith(mockTranscript.transcript, {}); }); it('should use specified API key', async () => { const options = { apiKey: 'test-google-key' }; service = new SummaryService(options); const result = await service.summarizeTranscript(mockTranscript); expect(result).toEqual({ ...mockTranscript, summary: mockSummary, provider: 'google' }); expect(AIProviderFactory.create).toHaveBeenCalledWith(options); }); it('should pass style option to provider', async () => { service = new SummaryService(); const options = { style: 'detailed' }; const result = await service.summarizeTranscript(mockTranscript, options); expect(result).toEqual({ ...mockTranscript, summary: mockSummary, provider: 'google' }); expect(mockProvider.summarize).toHaveBeenCalledWith(mockTranscript.transcript, options); }); it('should pass maxLength option to provider', async () => { service = new SummaryService(); const options = { maxLength: 200 }; const result = await service.summarizeTranscript(mockTranscript, options); expect(result).toEqual({ ...mockTranscript, summary: mockSummary, provider: 'google' }); expect(mockProvider.summarize).toHaveBeenCalledWith(mockTranscript.transcript, options); }); it('should handle provider errors', async () => { service = new SummaryService(); const error = new Error('Provider error'); mockProvider.summarize.mockRejectedValue(error); await expect(service.summarizeTranscript(mockTranscript)).rejects.toThrow('Summarization failed: Provider error'); }); it('should handle missing API key', async () => { const options = { apiKey: '' }; AIProviderFactory.create.mockImplementationOnce(() => { throw new Error('Google API key is required. Set GOOGLE_API_KEY environment variable or provide it in options.'); }); expect(() => new SummaryService(options)).toThrow('Google API key is required. Set GOOGLE_API_KEY environment variable or provide it in options.'); }); }); });