-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtest.js
188 lines (170 loc) · 5.48 KB
/
test.js
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import test from 'ava';
import nock from 'nock';
import {repeat} from 'lodash';
import Perspective, {TextEmptyError, TextTooLongError, ResponseError} from '.';
test.beforeEach(() => {
nock.disableNetConnect();
});
test.afterEach(() => {
nock.enableNetConnect();
});
const MOCK_RESPONSE = {
attributeScores: {
TOXICITY: {
spanScores: [
{
begin: 0,
end: 56,
score: {
value: 0.8728314,
type: 'PROBABILITY',
},
},
],
summaryScore: {
value: 0.8728314,
type: 'PROBABILITY',
},
},
},
languages: ['en'],
};
const createPerspective = () =>
new Perspective({apiKey: process.env.PERSPECTIVE_API_KEY || 'mock-key'});
test('requires apiKey', t => {
t.throws(() => new Perspective(), Error);
});
test('analyze (mocked)', async t => {
const p = createPerspective();
// pass allowUnmocked: true so that integration tests will work
nock('https://commentanalyzer.googleapis.com', {allowUnmocked: true})
.filteringRequestBody(() => '*')
.post('/v1alpha1/comments:analyze', '*')
.query(true)
.reply(200, MOCK_RESPONSE);
const result = await p.analyze('testing is for dummies');
t.deepEqual(result, MOCK_RESPONSE);
});
test('analyze (mocked) handles errors from API', async t => {
const p = createPerspective();
// pass allowUnmocked: true so that integration tests will work
nock('https://commentanalyzer.googleapis.com', {allowUnmocked: true})
.filteringRequestBody(() => '*')
.post('/v1alpha1/comments:analyze', '*')
.query(true)
.reply(400, {
error: {
message: 'invalid!',
},
});
const error = await t.throws(p.analyze('this will fail'), ResponseError);
t.is(error.message, 'invalid!');
t.is(error.response.data.error.message, 'invalid!');
});
test('analyze (mocked) handles errors with no message', async t => {
const p = createPerspective();
// pass allowUnmocked: true so that integration tests will work
nock('https://commentanalyzer.googleapis.com', {allowUnmocked: true})
.filteringRequestBody(() => '*')
.post('/v1alpha1/comments:analyze', '*')
.query(true)
.reply(400, {
error: {code: 400},
});
const error = await t.throws(p.analyze('this will fail'), ResponseError);
t.regex(error.message, /Request failed with status code 400/);
t.is(error.response.data.error.code, 400);
});
test('strips tags by default', t => {
const p = createPerspective();
const payload = p.getAnalyzeCommentPayload('<p>good test</p>');
t.is(payload.comment.text, 'good test');
});
test('stripHTML false', t => {
const p = createPerspective();
const payload = p.getAnalyzeCommentPayload('<p>good test</p>', {
stripHTML: false,
});
t.is(payload.comment.text, '<p>good test</p>');
});
test('doNotStore is true by default', t => {
const p = createPerspective();
const payload = p.getAnalyzeCommentPayload('good test');
t.true(payload.doNotStore);
});
test('truncate', t => {
const p = createPerspective();
// doesn't truncate by default
const text = repeat('x', 20481);
t.throws(() => p.getAnalyzeCommentPayload(text), Error);
const truncated = p.getAnalyzeCommentPayload(text, {truncate: true});
t.is(truncated.comment.text, repeat('x', 20480));
t.throws(() => p.getAnalyzeCommentPayload(text, {truncate: false}), Error);
});
test('analyze with attributes passed as an array', t => {
const p = createPerspective();
const payload = p.getAnalyzeCommentPayload('good test', {
attributes: ['unsubstantial', 'spam'],
});
t.truthy(payload.requestedAttributes.UNSUBSTANTIAL);
t.truthy(payload.requestedAttributes.SPAM);
});
test('analyze with AnalyzeComment object passed', t => {
const p = createPerspective();
const requestPayload = {
comment: {text: 'hooray for tests'},
requestedAttributes: {
UNSUBSTANTIAL: {},
SPAM: {},
},
clientToken: 'test',
doNotStore: true,
};
const payload = p.getAnalyzeCommentPayload(requestPayload);
t.deepEqual(payload, requestPayload);
});
test('text is required', t => {
const p = createPerspective();
const error = t.throws(
() => p.getAnalyzeCommentPayload(),
/text must not be empty/
);
t.true(error instanceof TextEmptyError);
const error2 = t.throws(
() => p.getAnalyzeCommentPayload(''),
/text must not be empty/
);
t.true(error2 instanceof TextEmptyError);
});
test('> 3000 characters in text is invalid', t => {
const p = createPerspective();
const text = repeat('x', 20481);
// prettier-ignore
const error = t.throws(() => p.getAnalyzeCommentPayload(text), /text must not be greater than/);
t.true(error instanceof TextTooLongError);
});
if (process.env.PERSPECTIVE_API_KEY && process.env.TEST_INTEGRATION) {
test('integration:analyze', async t => {
nock.enableNetConnect();
const p = createPerspective();
const result = await p.analyze('testing is for dummies', {
doNotStore: true,
});
t.log(JSON.stringify(result, null, 2));
t.truthy(result);
t.truthy(result.attributeScores.TOXICITY);
});
// This test will fail if the max length isn't what we expect
test('integration:analyze with text too long', async t => {
nock.enableNetConnect();
const p = createPerspective();
const text = repeat('x', 20481);
const {response} = await t.throws(
p.analyze(text, {doNotStore: true, validate: false}),
Error
);
t.log('Expected error');
t.log(response.data);
t.regex(response.data.error.message, /exceeded limit/);
});
}