-
Notifications
You must be signed in to change notification settings - Fork 146
/
Copy pathApi.spec.ts
147 lines (138 loc) · 4.5 KB
/
Api.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import { expect } from 'chai'
import Api from '../../../../src/main/translate/Api'
import TranslationManager from '../../../../src/main/translate/TranslationManager'
describe('Api', () => {
before(() => {
(global as any).__baseDir = __dirname
})
it('gets translation from form and parses with regex', (done) => {
const googleCN = new Api({
name: 'googleCN',
url: 'https://translate.google.cn/m',
method: 'POST',
requestBodyFormat: 'X{"q": %TEXT%, "sl": "ja", "hl": "zh-CN"}',
responseBodyPattern: 'Rclass="t0">([^<]*)<'
})
return googleCN.translate(
'悠真くんを攻略すれば210円か。なるほどなぁ…',
(translation) => {
try {
expect(translation).to.equal('如果您捕获尤马坤,它是210日元。我懂了...')
} catch (e) {
return done(e)
}
done()
}
)
}).timeout(5000)
it('combines multiple translations into yuki.Translations object', (done) => {
const apis = [
{
name: 'googleCN',
url: 'https://translate.google.cn/m',
method: 'POST',
requestBodyFormat: 'X{"q": %TEXT%, "sl": "ja", "hl": "zh-CN"}',
responseBodyPattern: 'Rclass="t0">([^<]*)<',
enable: true
},
{
name: 'caiyun',
external: true,
jsFile: '../../../../config/caiyunApi.js',
enable: true
}
]
TranslationManager.getInstance()
.initializeApis(apis)
.translateAll(
'悠真くんを攻略すれば210円か。なるほどなぁ…',
(translations) => {
// tslint:disable-next-line: no-console
console.log(translations)
try {
expect(translations.original).to.equal('悠真くんを攻略すれば210円か。なるほどなぁ…')
expect(translations.translations.googleCN).to.equal('如果您捕获尤马坤,它是210日元。我懂了...')
expect(translations.translations.caiyun).to.equal('攻下悠真的话是210日元吗。原来如此')
} catch (e) {
return done(e)
}
done()
}
)
}).timeout(5000)
it('returns no translation if there is no enabled api', (done) => {
const apis = [
{
name: 'baidu',
url: 'https://fanyi.baidu.com/transapi',
method: 'POST',
requestBodyFormat: 'X{"query": %TEXT%, "from": "jp", "to": "zh"}',
responseBodyPattern: 'J%RESPONSE%.data[0].dst',
enable: false
},
{
name: 'googleCN',
url: 'https://translate.google.cn/m',
method: 'POST',
requestBodyFormat: 'X{"q": %TEXT%, "sl": "ja", "hl": "zh-CN"}',
responseBodyPattern: 'Rclass="t0">([^<]*)<',
enable: false
}
]
TranslationManager.getInstance()
.initializeApis(apis)
.translateAll(
'悠真くんを攻略すれば210円か。なるほどなぁ…',
(translations) => {
try {
// tslint:disable-next-line: no-console
console.log(translations)
expect(translations).to.deep.equal({
original: '悠真くんを攻略すれば210円か。なるほどなぁ…',
translations: {}
})
} catch (e) {
return done(e)
}
done()
}
)
})
it('returns translations for any enabled apis', (done) => {
const apis = [
{
name: 'googleCN',
url: 'https://translate.google.cn/m',
method: 'POST',
requestBodyFormat: 'X{"q": %TEXT%, "sl": "ja", "hl": "zh-CN"}',
responseBodyPattern: 'Rclass="t0">([^<]*)<',
enable: true
},
{
name: 'caiyun',
external: true,
jsFile: '../../../../config/caiyunApi.js',
enable: true
}
]
TranslationManager.getInstance()
.initializeApis(apis)
.translateAll(
'悠真くんを攻略すれば210円か。なるほどなぁ…',
(translations) => {
try {
expect(translations).to.deep.equal({
original: '悠真くんを攻略すれば210円か。なるほどなぁ…',
translations: {
googleCN: '如果您捕获尤马坤,它是210日元。我懂了...',
caiyun: '攻下悠真的话是210日元吗。原来如此'
}
})
} catch (e) {
return done(e)
}
done()
}
)
}).timeout(5000)
})