tts-mcp
Version:
OpenAI Text to Speech APIを活用したコマンドラインツールとMCPサーバー
168 lines (167 loc) • 6.5 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = require("fs");
const openai_1 = require("openai");
// モジュールをモック化
jest.mock('openai');
jest.mock('fs', () => {
const originalFs = jest.requireActual('fs');
return {
...originalFs,
promises: {
...originalFs.promises,
writeFile: jest.fn().mockResolvedValue(undefined)
}
};
});
// モック化が完了した後にモジュールをインポート
const api = __importStar(require("../src/api"));
describe('api', () => {
let consoleLogSpy;
let consoleErrorSpy;
beforeEach(() => {
// コンソール出力をモック
consoleLogSpy = jest.spyOn(console, 'log').mockImplementation();
consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation();
// モックをリセット
jest.clearAllMocks();
});
afterEach(() => {
// モックをリストア
consoleLogSpy.mockRestore();
consoleErrorSpy.mockRestore();
});
describe('initializeClient', () => {
it('APIキーがない場合、エラーをスロー', async () => {
await expect(api.textToSpeech({
text: 'テスト',
outputPath: 'output.mp3',
model: 'tts-1',
voice: 'alloy',
speed: 1,
format: 'mp3',
apiKey: undefined
})).rejects.toThrow('OpenAI APIキーが設定されていません');
});
});
describe('textToSpeech', () => {
it('正常なリクエストでは音声ファイルを生成する', async () => {
// API呼び出しの成功をモック
const mockCreate = jest.fn().mockResolvedValue({
arrayBuffer: jest.fn().mockResolvedValue(new Uint8Array([1, 2, 3, 4]).buffer)
});
openai_1.OpenAI.mockImplementation(() => ({
audio: {
speech: {
create: mockCreate
}
}
}));
// テスト実行
const options = {
text: 'テスト音声',
outputPath: 'output.mp3',
model: 'tts-1',
voice: 'alloy',
speed: 1,
format: 'mp3',
apiKey: 'test_api_key'
};
await api.textToSpeech(options);
// APIクライアントが作成されたか検証
expect(openai_1.OpenAI).toHaveBeenCalledWith({
apiKey: 'test_api_key'
});
// APIメソッドが呼ばれたか検証
expect(mockCreate).toHaveBeenCalled();
// ファイルが書き込まれたか検証
expect(fs_1.promises.writeFile).toHaveBeenCalled();
// コンソールログが出力されたか検証
expect(consoleLogSpy).toHaveBeenCalledWith(expect.stringContaining('音声ファイルを生成しました'));
});
it('APIエラーが発生した場合、エラーを処理する', async () => {
// APIエラーをモック
const apiError = new Error('API error');
apiError.response = {
data: { error: 'テストエラー' }
};
openai_1.OpenAI.mockImplementation(() => ({
audio: {
speech: {
create: jest.fn().mockRejectedValue(apiError)
}
}
}));
// テスト実行
const options = {
text: 'テスト音声',
outputPath: 'output.mp3',
model: 'tts-1',
voice: 'alloy',
speed: 1,
format: 'mp3',
apiKey: 'test_api_key'
};
await expect(api.textToSpeech(options)).rejects.toThrow();
// エラーログが出力されたことを検証
expect(consoleErrorSpy).toHaveBeenCalled();
});
it('その他のエラーが発生した場合、エラーを処理する', async () => {
// 一般的なエラーをモック
const generalError = new Error('一般的なエラー');
openai_1.OpenAI.mockImplementation(() => ({
audio: {
speech: {
create: jest.fn().mockRejectedValue(generalError)
}
}
}));
// テスト実行
const options = {
text: 'テスト音声',
outputPath: 'output.mp3',
model: 'tts-1',
voice: 'alloy',
speed: 1,
format: 'mp3',
apiKey: 'test_api_key'
};
await expect(api.textToSpeech(options)).rejects.toThrow();
// エラーログが出力されたことを検証
expect(consoleErrorSpy).toHaveBeenCalled();
});
});
});