-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[test] add unit regarding the error handling
- Loading branch information
Showing
1 changed file
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
const fs = require('fs'); | ||
const tts = require('..'); | ||
const fetch = require('isomorphic-fetch'); | ||
jest.setTimeout(60000); | ||
|
||
jest.mock('isomorphic-fetch'); | ||
const host = 'https://translate.google.com'; | ||
|
||
const html = { | ||
hasTokenKey: fs.readFileSync(`${__dirname}/__mocks__/has-tkk.html`, 'utf8'), | ||
noTokenKey: fs.readFileSync(`${__dirname}/__mocks__/no-tkk.html`, 'utf8'), | ||
}; | ||
|
||
const mockResponse = { | ||
htmlFailed: () => { | ||
fetch.mockImplementationOnce(async (url) => { | ||
if (url !== host) throw new Error('test failed'); | ||
return { status: 400, statusText: 'request failed' }; | ||
}); | ||
}, | ||
htmlSuccessWithTokenKey: () => { | ||
fetch.mockImplementationOnce(async (url) => { | ||
if (url !== host) throw new Error('test failed'); | ||
return { status: 200, text: async () => html.hasTokenKey }; | ||
}); | ||
}, | ||
htmlSuccessButNoTokenKey: () => { | ||
fetch.mockImplementationOnce(async (url) => { | ||
if (url !== host) throw new Error('text failed'); | ||
return { status: 200, text: async () => html.noTokenKey }; | ||
}); | ||
}, | ||
validateSucceed: () => { | ||
fetch.mockImplementationOnce(async (url) => { | ||
if (url === host) throw new Error('test failed'); | ||
return { status: 200 }; | ||
}); | ||
}, | ||
validateFailed: () => { | ||
fetch.mockImplementationOnce(async (url) => { | ||
if (url === host) throw new Error('test failed'); | ||
return { status: 400 }; | ||
}); | ||
}, | ||
}; | ||
|
||
describe('Google TTS', () => { | ||
beforeEach(fetch.mockReset); | ||
|
||
describe('first time using tts function', () => { | ||
it('get html failed', async () => { | ||
mockResponse.htmlFailed(); | ||
await expect(tts('hello')).rejects.toThrow('status code = 400'); | ||
expect(fetch).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('cannot get the token key (retry 10 times)', async () => { | ||
for (let i = 0; i < 10; i++) { | ||
mockResponse.htmlSuccessButNoTokenKey(); | ||
} | ||
await expect(tts('hello')).rejects.toThrow('get token key failed from google'); | ||
expect(fetch).toHaveBeenCalledTimes(10); | ||
}); | ||
|
||
it('retry 3 times to get the token key', async () => { | ||
mockResponse.htmlSuccessButNoTokenKey(); | ||
mockResponse.htmlSuccessButNoTokenKey(); | ||
mockResponse.htmlSuccessButNoTokenKey(); | ||
mockResponse.htmlSuccessWithTokenKey(); | ||
|
||
const url = await tts('hello'); | ||
expect(url).toBe( | ||
'https://translate.google.com/translate_tts?ie=UTF-8&q=hello&tl=en&total=1&idx=0&textlen=5&tk=918197.577527&client=t&prev=input&ttsspeed=1' | ||
); | ||
expect(fetch).toHaveBeenCalledTimes(4); | ||
}); | ||
}); | ||
|
||
describe('non-first time using tts function', () => { | ||
it('validate success', async () => { | ||
mockResponse.validateSucceed(); | ||
const url = await tts('test', 'en', 0.24); | ||
|
||
expect(url).toBe( | ||
'https://translate.google.com/translate_tts?ie=UTF-8&q=test&tl=en&total=1&idx=0&textlen=4&tk=376585.225867&client=t&prev=input&ttsspeed=0.24' | ||
); | ||
expect(fetch).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('validate failed: retry 3 times to get the new token', async () => { | ||
mockResponse.validateFailed(); | ||
mockResponse.htmlSuccessButNoTokenKey(); | ||
mockResponse.htmlSuccessButNoTokenKey(); | ||
mockResponse.htmlSuccessButNoTokenKey(); | ||
fetch.mockImplementationOnce(async (url) => { | ||
if (url !== host) throw new Error('test failed'); | ||
return { status: 200, text: async () => "......tkk:'1234557.1234567'....." }; | ||
}); | ||
|
||
const url = await tts('test'); | ||
expect(url).toBe( | ||
'https://translate.google.com/translate_tts?ie=UTF-8&q=test&tl=en&total=1&idx=0&textlen=4&tk=321164.1454321&client=t&prev=input&ttsspeed=1' | ||
); | ||
expect(fetch).toHaveBeenCalledTimes(5); | ||
}); | ||
}); | ||
}); |