-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprocessTexts.realworld.test.ts
More file actions
363 lines (300 loc) · 13.2 KB
/
Copy pathprocessTexts.realworld.test.ts
File metadata and controls
363 lines (300 loc) · 13.2 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
// Real-world processTexts tests using actual example files
import fs from 'fs';
import path from 'path';
import { DotProcessor } from '../src/processors/dotProcessor';
import { OpmlProcessor } from '../src/processors/opmlProcessor';
import { ObfProcessor } from '../src/processors/obfProcessor';
import { GridsetProcessor } from '../src/processors/gridsetProcessor';
import { SnapProcessor } from '../src/processors/snapProcessor';
import { TouchChatProcessor } from '../src/processors/touchchatProcessor';
jest.setTimeout(process.platform === 'win32' ? 60000 : 30000);
describe('ProcessTexts with Real-World Data', () => {
const examplesDir = path.join(__dirname, '../examples');
const tempDir = path.join(__dirname, 'temp_realworld');
beforeAll(async () => {
if (!fs.existsSync(tempDir)) {
fs.mkdirSync(tempDir, { recursive: true });
}
});
afterAll(async () => {
if (fs.existsSync(tempDir)) {
try {
fs.rmSync(tempDir, { recursive: true, force: true });
} catch (error) {
// On Windows the file can be locked briefly; ignore cleanup failure in tests
}
}
});
describe('DOT Processor with Real Data', () => {
const dotFile = path.join(examplesDir, 'example.dot');
const communikateDotFile = path.join(examplesDir, 'communikate.dot');
it('should extract and translate texts from example.dot', async () => {
if (!fs.existsSync(dotFile)) {
console.log('Skipping DOT test - example.dot not found');
return;
}
const processor = new DotProcessor();
// First extract all texts to see what we're working with
const originalTexts = await processor.extractTexts(dotFile);
expect(originalTexts.length).toBeGreaterThan(0);
console.log('DOT original texts:', originalTexts.slice(0, 5)); // Show first 5
// Create translations for some common words
const translations = new Map<string, string>();
originalTexts.forEach((text) => {
if (text.toLowerCase().includes('hello')) {
translations.set(text, text.replace(/hello/gi, 'hola'));
}
if (text.toLowerCase().includes('home')) {
translations.set(text, text.replace(/home/gi, 'casa'));
}
if (text.toLowerCase().includes('food')) {
translations.set(text, text.replace(/food/gi, 'comida'));
}
});
if (translations.size > 0) {
const outputPath = path.join(tempDir, 'translated_example.dot');
const result = await processor.processTexts(dotFile, translations, outputPath);
expect(result).toBeInstanceOf(Buffer);
expect(fs.existsSync(outputPath)).toBe(true);
// Verify translations were applied
const translatedContent = Buffer.from(result).toString('utf8');
translations.forEach((translation, original) => {
if (original !== translation) {
expect(translatedContent).toContain(translation);
}
});
}
});
it('should handle communikate.dot file', async () => {
if (!fs.existsSync(communikateDotFile)) {
console.log('Skipping communikate DOT test - file not found');
return;
}
const processor = new DotProcessor();
const texts = await processor.extractTexts(communikateDotFile);
expect(texts.length).toBeGreaterThan(0);
// Test with a simple translation
const translations = new Map([['Core', 'Núcleo']]);
const outputPath = path.join(tempDir, 'translated_communikate.dot');
await expect(
processor.processTexts(communikateDotFile, translations, outputPath)
).resolves.toBeInstanceOf(Uint8Array);
});
});
describe('OPML Processor with Real Data', () => {
const opmlFile = path.join(examplesDir, 'example.opml');
it('should extract and translate texts from example.opml', async () => {
if (!fs.existsSync(opmlFile)) {
console.log('Skipping OPML test - example.opml not found');
return;
}
const processor = new OpmlProcessor();
// Extract texts to see the structure
const originalTexts = await processor.extractTexts(opmlFile);
expect(originalTexts.length).toBeGreaterThan(0);
console.log('OPML original texts:', originalTexts.slice(0, 5));
// Create translations based on actual content
const translations = new Map<string, string>();
originalTexts.forEach((text) => {
if (text.toLowerCase().includes('home')) {
translations.set(text, text.replace(/home/gi, 'casa'));
}
if (text.toLowerCase().includes('food')) {
translations.set(text, text.replace(/food/gi, 'comida'));
}
if (text.toLowerCase().includes('drink')) {
translations.set(text, text.replace(/drink/gi, 'bebida'));
}
});
if (translations.size > 0) {
const outputPath = path.join(tempDir, 'translated_example.opml');
const result = await processor.processTexts(opmlFile, translations, outputPath);
expect(result).toBeInstanceOf(Buffer);
// Verify the XML structure is maintained and translations applied
const translatedContent = Buffer.from(result).toString('utf8');
expect(translatedContent).toContain('<?xml');
expect(translatedContent).toContain('<opml');
translations.forEach((translation, original) => {
if (original !== translation) {
expect(translatedContent).toContain(`text="${translation}"`);
}
});
}
});
});
describe('OBF Processor with Real Data', () => {
const obfFile = path.join(examplesDir, 'example.obf');
const obzFile = path.join(examplesDir, 'example.obz');
it('should extract and translate texts from example.obf', async () => {
if (!fs.existsSync(obfFile)) {
console.log('Skipping OBF test - example.obf not found');
return;
}
const processor = new ObfProcessor();
// Extract texts to understand the content
const originalTexts = await processor.extractTexts(obfFile);
expect(originalTexts.length).toBeGreaterThan(0);
console.log('OBF original texts:', originalTexts.slice(0, 5));
// Create meaningful translations
const translations = new Map<string, string>();
originalTexts.forEach((text) => {
if (text && typeof text === 'string') {
if (text.toLowerCase().includes('hello')) {
translations.set(text, text.replace(/hello/gi, 'hola'));
}
if (text.toLowerCase().includes('yes')) {
translations.set(text, text.replace(/yes/gi, 'sí'));
}
if (text.toLowerCase().includes('no')) {
translations.set(text, text.replace(/no/gi, 'no'));
}
}
});
if (translations.size > 0) {
const outputPath = path.join(tempDir, 'translated_example.obf');
const result = await processor.processTexts(obfFile, translations, outputPath);
expect(result).toBeInstanceOf(Buffer);
expect(fs.existsSync(outputPath)).toBe(true);
// Load the translated file and verify structure
const translatedTree = await processor.loadIntoTree(outputPath);
expect(Object.keys(translatedTree.pages).length).toBeGreaterThan(0);
}
});
it('should handle OBZ (zip) files', async () => {
if (!fs.existsSync(obzFile)) {
console.log('Skipping OBZ test - example.obz not found');
return;
}
const processor = new ObfProcessor();
const texts = await processor.extractTexts(obzFile);
expect(texts.length).toBeGreaterThan(0);
// Test with simple translation
const translations = new Map([['home', 'casa']]);
const outputPath = path.join(tempDir, 'translated_example.obz');
await expect(
processor.processTexts(obzFile, translations, outputPath)
).resolves.toBeInstanceOf(Uint8Array);
});
});
describe('GridSet Processor with Real Data', () => {
const gridsetFile = path.join(examplesDir, 'example.gridset');
it('should extract and translate texts from example.gridset', async () => {
if (!fs.existsSync(gridsetFile)) {
console.log('Skipping GridSet test - example.gridset not found');
return;
}
const processor = new GridsetProcessor();
// Extract texts from the real GridSet file
const fileBuffer = fs.readFileSync(gridsetFile);
const originalTexts = await processor.extractTexts(fileBuffer);
expect(originalTexts.length).toBeGreaterThan(0);
console.log('GridSet original texts:', originalTexts.slice(0, 5));
// Create translations based on Grid3 format expectations
const translations = new Map<string, string>();
originalTexts.forEach((text) => {
if (text && typeof text === 'string') {
// Common AAC words that might be in a gridset
if (text.toLowerCase().includes('i')) {
translations.set(text, text.replace(/\bi\b/gi, 'yo'));
}
if (text.toLowerCase().includes('want')) {
translations.set(text, text.replace(/want/gi, 'quiero'));
}
if (text.toLowerCase().includes('more')) {
translations.set(text, text.replace(/more/gi, 'más'));
}
}
});
if (translations.size > 0) {
const outputPath = path.join(tempDir, 'translated_example.gridset');
const result = await processor.processTexts(fileBuffer, translations, outputPath);
expect(result).toBeInstanceOf(Buffer);
expect(fs.existsSync(outputPath)).toBe(true);
// Verify the translated file can be loaded back
const translatedBuffer = fs.readFileSync(outputPath);
const translatedTree = await processor.loadIntoTree(translatedBuffer);
expect(Object.keys(translatedTree.pages).length).toBeGreaterThan(0);
}
});
});
describe('Snap Processor with Real Data', () => {
const spbFile = path.join(examplesDir, 'example.spb');
const spsFile = path.join(examplesDir, 'example.sps');
it('should extract and translate texts from example.spb', async () => {
if (!fs.existsSync(spbFile)) {
console.log('Skipping SPB test - example.spb not found');
return;
}
const processor = new SnapProcessor();
// Extract texts from real Snap database
const originalTexts = await processor.extractTexts(spbFile);
expect(originalTexts.length).toBeGreaterThan(0);
console.log('Snap SPB original texts:', originalTexts.slice(0, 5));
// Create translations for common AAC vocabulary
const translations = new Map<string, string>();
originalTexts.forEach((text) => {
if (text && typeof text === 'string') {
if (text.toLowerCase().includes('hello')) {
translations.set(text, text.replace(/hello/gi, 'hola'));
}
if (text.toLowerCase().includes('thank')) {
translations.set(text, text.replace(/thank/gi, 'gracias'));
}
}
});
if (translations.size > 0) {
const outputPath = path.join(tempDir, 'translated_example.spb');
const result = await processor.processTexts(spbFile, translations, outputPath);
expect(result).toBeInstanceOf(Buffer);
expect(fs.existsSync(outputPath)).toBe(true);
}
});
it('should handle SPS files', async () => {
if (!fs.existsSync(spsFile)) {
console.log('Skipping SPS test - example.sps not found');
return;
}
const processor = new SnapProcessor();
const texts = await processor.extractTexts(spsFile);
expect(texts.length).toBeGreaterThan(0);
// Test basic translation functionality
const translations = new Map([['home', 'casa']]);
const outputPath = path.join(tempDir, 'translated_example.sps');
await expect(
processor.processTexts(spsFile, translations, outputPath)
).resolves.toBeInstanceOf(Uint8Array);
});
});
describe('TouchChat Processor with Real Data', () => {
const ceFile = path.join(examplesDir, 'example.ce');
it('should extract and translate texts from example.ce', async () => {
if (!fs.existsSync(ceFile)) {
console.log('Skipping TouchChat test - example.ce not found');
return;
}
const processor = new TouchChatProcessor();
// Extract texts from real TouchChat file
const originalTexts = await processor.extractTexts(ceFile);
expect(originalTexts.length).toBeGreaterThan(0);
console.log('TouchChat original texts:', originalTexts.slice(0, 5));
// Create translations for TouchChat vocabulary
const translations = new Map<string, string>();
originalTexts.forEach((text) => {
if (text && typeof text === 'string') {
if (text.toLowerCase().includes('hello')) {
translations.set(text, text.replace(/hello/gi, 'hola'));
}
if (text.toLowerCase().includes('goodbye')) {
translations.set(text, text.replace(/goodbye/gi, 'adiós'));
}
}
});
if (translations.size > 0) {
const outputPath = path.join(tempDir, 'translated_example.ce');
const result = await processor.processTexts(ceFile, translations, outputPath);
expect(result).toBeInstanceOf(Buffer);
expect(fs.existsSync(outputPath)).toBe(true);
}
});
});
});