@audc/salute-tts
Version:
Synthesis client for Node.js
322 lines (321 loc) • 15.8 kB
JavaScript
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// ** This file is automatically generated by gapic-generator-typescript. **
// ** https://github.com/googleapis/gapic-generator-typescript **
// ** All changes to this file may be overwritten. **
// @ts-ignore
import * as protos from '../../protos/protos.js';
import assert from 'assert';
import * as sinon from 'sinon';
import { describe, it } from 'mocha';
import * as smartspeechModule from '../src/index.js';
import { PassThrough } from 'stream';
import { protobuf } from 'google-gax';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
// @ts-ignore
const dirname = path.dirname(fileURLToPath(import.meta.url));
// Dynamically loaded proto JSON is needed to get the type information
// to fill in default values for request objects
const root = protobuf.Root.fromJSON(JSON.parse(fs.readFileSync(path.join(dirname, '..', '..', 'protos/protos.json'), 'utf8')));
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function getTypeDefaultValue(typeName, fields) {
var _a, _b, _c;
let type = root.lookupType(typeName);
for (const field of fields.slice(0, -1)) {
type = (_a = type === null || type === void 0 ? void 0 : type.fields[field]) === null || _a === void 0 ? void 0 : _a.resolvedType;
}
return (_c = (_b = type === null || type === void 0 ? void 0 : type.fields[fields[fields.length - 1]]) === null || _b === void 0 ? void 0 : _b.defaultValue) !== null && _c !== void 0 ? _c : null;
}
function generateSampleMessage(instance) {
const filledObject = instance.constructor
.toObject(instance, { defaults: true });
return instance.constructor.fromObject(filledObject);
}
function stubSimpleCall(response, error) {
return error ? sinon.stub().rejects(error) : sinon.stub().resolves([response]);
}
function stubSimpleCallWithCallback(response, error) {
return error ? sinon.stub().callsArgWith(2, error) : sinon.stub().callsArgWith(2, null, response);
}
function stubServerStreamingCall(response, error) {
const transformStub = error ? sinon.stub().callsArgWith(2, error) : sinon.stub().callsArgWith(2, null, response);
const mockStream = new PassThrough({
objectMode: true,
transform: transformStub,
});
// write something to the stream to trigger transformStub and send the response back to the client
setImmediate(() => { mockStream.write({}); });
setImmediate(() => { mockStream.end(); });
return sinon.stub().returns(mockStream);
}
describe('v1.SmartSpeechClient', () => {
describe('Common methods', () => {
it('has apiEndpoint', () => {
const client = new smartspeechModule.v1.SmartSpeechClient();
const apiEndpoint = client.apiEndpoint;
assert.strictEqual(apiEndpoint, 'sber.ru');
});
it('has universeDomain', () => {
const client = new smartspeechModule.v1.SmartSpeechClient();
const universeDomain = client.universeDomain;
assert.strictEqual(universeDomain, "googleapis.com");
});
if (typeof process === 'object' && typeof process.emitWarning === 'function') {
it('throws DeprecationWarning if static servicePath is used', () => {
const stub = sinon.stub(process, 'emitWarning');
const servicePath = smartspeechModule.v1.SmartSpeechClient.servicePath;
assert.strictEqual(servicePath, 'sber.ru');
assert(stub.called);
stub.restore();
});
it('throws DeprecationWarning if static apiEndpoint is used', () => {
const stub = sinon.stub(process, 'emitWarning');
const apiEndpoint = smartspeechModule.v1.SmartSpeechClient.apiEndpoint;
assert.strictEqual(apiEndpoint, 'sber.ru');
assert(stub.called);
stub.restore();
});
}
it('does not allow setting both universeDomain and universe_domain', () => {
assert.throws(() => { new smartspeechModule.v1.SmartSpeechClient({ universe_domain: 'example.com', universeDomain: 'example.net' }); });
});
it('has port', () => {
const port = smartspeechModule.v1.SmartSpeechClient.port;
assert(port);
assert(typeof port === 'number');
});
it('should create a client with no option', () => {
const client = new smartspeechModule.v1.SmartSpeechClient();
assert(client);
});
it('should create a client with gRPC fallback', () => {
const client = new smartspeechModule.v1.SmartSpeechClient({
fallback: true,
});
assert(client);
});
it('has initialize method and supports deferred initialization', async () => {
const client = new smartspeechModule.v1.SmartSpeechClient({
credentials: { client_email: 'bogus', private_key: 'bogus' },
projectId: 'bogus',
});
assert.strictEqual(client.smartSpeechStub, undefined);
await client.initialize();
assert(client.smartSpeechStub);
});
it('has close method for the initialized client', done => {
const client = new smartspeechModule.v1.SmartSpeechClient({
credentials: { client_email: 'bogus', private_key: 'bogus' },
projectId: 'bogus',
});
client.initialize().catch(err => { throw err; });
assert(client.smartSpeechStub);
client.close().then(() => {
done();
});
});
it('has close method for the non-initialized client', done => {
const client = new smartspeechModule.v1.SmartSpeechClient({
credentials: { client_email: 'bogus', private_key: 'bogus' },
projectId: 'bogus',
});
assert.strictEqual(client.smartSpeechStub, undefined);
client.close().then(() => {
done();
});
});
it('has getProjectId method', async () => {
const fakeProjectId = 'fake-project-id';
const client = new smartspeechModule.v1.SmartSpeechClient({
credentials: { client_email: 'bogus', private_key: 'bogus' },
projectId: 'bogus',
});
client.auth.getProjectId = sinon.stub().resolves(fakeProjectId);
const result = await client.getProjectId();
assert.strictEqual(result, fakeProjectId);
assert(client.auth.getProjectId.calledWithExactly());
});
it('has getProjectId method with callback', async () => {
const fakeProjectId = 'fake-project-id';
const client = new smartspeechModule.v1.SmartSpeechClient({
credentials: { client_email: 'bogus', private_key: 'bogus' },
projectId: 'bogus',
});
client.auth.getProjectId = sinon.stub().callsArgWith(0, null, fakeProjectId);
const promise = new Promise((resolve, reject) => {
client.getProjectId((err, projectId) => {
if (err) {
reject(err);
}
else {
resolve(projectId);
}
});
});
const result = await promise;
assert.strictEqual(result, fakeProjectId);
});
});
describe('asyncSynthesize', () => {
it('invokes asyncSynthesize without error', async () => {
const client = new smartspeechModule.v1.SmartSpeechClient({
credentials: { client_email: 'bogus', private_key: 'bogus' },
projectId: 'bogus',
});
await client.initialize();
const request = generateSampleMessage(new protos.smartspeech.synthesis.v1.AsyncSynthesisRequest());
const expectedResponse = generateSampleMessage(new protos.smartspeech.task.v1.Task());
client.innerApiCalls.asyncSynthesize = stubSimpleCall(expectedResponse);
const [response] = await client.asyncSynthesize(request);
assert.deepStrictEqual(response, expectedResponse);
});
it('invokes asyncSynthesize without error using callback', async () => {
const client = new smartspeechModule.v1.SmartSpeechClient({
credentials: { client_email: 'bogus', private_key: 'bogus' },
projectId: 'bogus',
});
await client.initialize();
const request = generateSampleMessage(new protos.smartspeech.synthesis.v1.AsyncSynthesisRequest());
const expectedResponse = generateSampleMessage(new protos.smartspeech.task.v1.Task());
client.innerApiCalls.asyncSynthesize = stubSimpleCallWithCallback(expectedResponse);
const promise = new Promise((resolve, reject) => {
client.asyncSynthesize(request, (err, result) => {
if (err) {
reject(err);
}
else {
resolve(result);
}
});
});
const response = await promise;
assert.deepStrictEqual(response, expectedResponse);
});
it('invokes asyncSynthesize with error', async () => {
const client = new smartspeechModule.v1.SmartSpeechClient({
credentials: { client_email: 'bogus', private_key: 'bogus' },
projectId: 'bogus',
});
await client.initialize();
const request = generateSampleMessage(new protos.smartspeech.synthesis.v1.AsyncSynthesisRequest());
const expectedError = new Error('expected');
client.innerApiCalls.asyncSynthesize = stubSimpleCall(undefined, expectedError);
await assert.rejects(client.asyncSynthesize(request), expectedError);
});
it('invokes asyncSynthesize with closed client', async () => {
const client = new smartspeechModule.v1.SmartSpeechClient({
credentials: { client_email: 'bogus', private_key: 'bogus' },
projectId: 'bogus',
});
await client.initialize();
const request = generateSampleMessage(new protos.smartspeech.synthesis.v1.AsyncSynthesisRequest());
const expectedError = new Error('The client has already been closed.');
client.close();
await assert.rejects(client.asyncSynthesize(request), expectedError);
});
});
describe('synthesize', () => {
it('invokes synthesize without error', async () => {
const client = new smartspeechModule.v1.SmartSpeechClient({
credentials: { client_email: 'bogus', private_key: 'bogus' },
projectId: 'bogus',
});
await client.initialize();
const request = generateSampleMessage(new protos.smartspeech.synthesis.v1.SynthesisRequest());
const expectedResponse = generateSampleMessage(new protos.smartspeech.synthesis.v1.SynthesisResponse());
client.innerApiCalls.synthesize = stubServerStreamingCall(expectedResponse);
const stream = client.synthesize(request);
const promise = new Promise((resolve, reject) => {
stream.on('data', (response) => {
resolve(response);
});
stream.on('error', (err) => {
reject(err);
});
});
const response = await promise;
assert.deepStrictEqual(response, expectedResponse);
});
it('invokes synthesize without error and gaxServerStreamingRetries enabled', async () => {
const client = new smartspeechModule.v1.SmartSpeechClient({
credentials: { client_email: 'bogus', private_key: 'bogus' },
projectId: 'bogus',
});
await client.initialize();
const request = generateSampleMessage(new protos.smartspeech.synthesis.v1.SynthesisRequest());
const expectedResponse = generateSampleMessage(new protos.smartspeech.synthesis.v1.SynthesisResponse());
client.innerApiCalls.synthesize = stubServerStreamingCall(expectedResponse);
const stream = client.synthesize(request);
const promise = new Promise((resolve, reject) => {
stream.on('data', (response) => {
resolve(response);
});
stream.on('error', (err) => {
reject(err);
});
});
const response = await promise;
assert.deepStrictEqual(response, expectedResponse);
});
it('invokes synthesize with error', async () => {
const client = new smartspeechModule.v1.SmartSpeechClient({
credentials: { client_email: 'bogus', private_key: 'bogus' },
projectId: 'bogus',
});
await client.initialize();
const request = generateSampleMessage(new protos.smartspeech.synthesis.v1.SynthesisRequest());
const expectedError = new Error('expected');
client.innerApiCalls.synthesize = stubServerStreamingCall(undefined, expectedError);
const stream = client.synthesize(request);
const promise = new Promise((resolve, reject) => {
stream.on('data', (response) => {
resolve(response);
});
stream.on('error', (err) => {
reject(err);
});
});
await assert.rejects(promise, expectedError);
});
it('invokes synthesize with closed client', async () => {
const client = new smartspeechModule.v1.SmartSpeechClient({
credentials: { client_email: 'bogus', private_key: 'bogus' },
projectId: 'bogus',
});
await client.initialize();
const request = generateSampleMessage(new protos.smartspeech.synthesis.v1.SynthesisRequest());
const expectedError = new Error('The client has already been closed.');
client.close();
const stream = client.synthesize(request, { retryRequestOptions: { noResponseRetries: 0 } });
const promise = new Promise((resolve, reject) => {
stream.on('data', (response) => {
resolve(response);
});
stream.on('error', (err) => {
reject(err);
});
});
await assert.rejects(promise, expectedError);
});
it('should create a client with gaxServerStreamingRetries enabled', () => {
const client = new smartspeechModule.v1.SmartSpeechClient({
gaxServerStreamingRetries: true,
});
assert(client);
});
});
});