forked from AIObjectives/talk-to-the-city-reports
-
Notifications
You must be signed in to change notification settings - Fork 0
/
translate_v0.test.ts
105 lines (92 loc) · 2.63 KB
/
translate_v0.test.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
import { describe, it, vi, expect, beforeEach } from 'vitest';
import TranslateNode, { translate_node_data } from '$lib/compute/translate_v0';
import deepCopy from 'deep-copy';
import { getDataset } from './test/mocks/dataset';
describe('TranslateNode class', () => {
let node;
let inputData;
let dataset;
beforeEach(() => {
vi.mock('$lib/utils', () => ({
readFileFromGCS: vi.fn(() =>
Promise.resolve(
JSON.stringify({
'en-US': { key: 'value' },
'fr-FR': { key: 'translated response' }
})
)
),
uploadJSONToGCS: vi.fn(() => Promise.resolve()),
quickChecksum: vi.fn(() => 123)
}));
node = new TranslateNode(deepCopy(translate_node_data));
inputData = {
open_ai_key: 'test_key',
data: { key: 'value' }
};
dataset = getDataset();
});
it('translates the input data', async () => {
node.data.target_languages = ['fr-FR'];
node.data.language_selector = 'fr-FR';
node.data.keys = ['$.key'];
node.data.gcs_path = 'gs://test_bucket/test_path';
const result = await node.compute(
inputData,
'run',
vi.fn(),
vi.fn(),
vi.fn(),
'test_slug',
vi.fn(),
dataset
);
const { translation, translations } = result;
expect(translation).toEqual({ key: 'translated response' });
expect(translations).toEqual({
'en-US': { key: 'value' },
'fr-FR': { key: 'translated response' }
});
});
it('loads translations from GCS if data has not changed', async () => {
node.data.target_languages = ['fr-FR'];
node.data.language_selector = 'fr-FR';
node.data.keys = ['$.key'];
node.data.gcs_path = 'gs://test_bucket/test_path';
node.data.length = 123; // Same as the checksum returned by quickChecksum
const modes = ['run', 'load'];
for (const mode of modes) {
const result = await node.compute(
inputData,
mode,
vi.fn(),
vi.fn(),
vi.fn(),
'test_slug',
vi.fn(),
dataset
);
const { translation, translations } = result;
expect(translation).toEqual({ key: 'translated response' });
expect(translations).toEqual({
'en-US': { key: 'value' },
'fr-FR': { key: 'translated response' }
});
}
});
it('does not translate if required inputs are missing', async () => {
node.data.target_languages = [];
node.data.keys = [];
const result = await node.compute(
inputData,
'run',
vi.fn(),
vi.fn(),
vi.fn(),
'test_slug',
vi.fn(),
dataset
);
expect(result).toBeUndefined();
});
});