-
Notifications
You must be signed in to change notification settings - Fork 66.8k
Expand file tree
/
Copy pathtranslation-error-comments.ts
More file actions
418 lines (342 loc) · 15.1 KB
/
translation-error-comments.ts
File metadata and controls
418 lines (342 loc) · 15.1 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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
import { describe, expect, test, vi, beforeEach, afterEach, type MockedFunction } from 'vitest'
import {
createTranslationFallbackComment,
EmptyTitleError,
renderContentWithFallback,
executeWithFallback,
LiquidError,
} from '../lib/render-with-fallback'
import { TitleFromAutotitleError } from '@/content-render/unified/rewrite-local-links'
import Page from '@/frame/lib/page'
describe('Translation Error Comments', () => {
// Mock renderContent for integration tests
let mockRenderContent: MockedFunction<
(template: string, context: Record<string, unknown>) => string
>
beforeEach(() => {
mockRenderContent = vi.fn()
vi.stubGlobal('renderContent', mockRenderContent)
})
afterEach(() => {
vi.unstubAllGlobals()
})
describe('createTranslationFallbackComment', () => {
describe('Liquid ParseError', () => {
test('includes all fields when token information is available', () => {
const error = new LiquidError("Unknown tag 'badtag', line:1, col:3", 'ParseError')
error.token = {
file: '/content/test/article.md',
getPosition: () => [1, 3],
}
const result = createTranslationFallbackComment(error, 'rawTitle')
expect(result).toContain('<!-- TRANSLATION_FALLBACK')
expect(result).toContain('prop=rawTitle')
expect(result).toContain('type=ParseError')
expect(result).toContain('file=/content/test/article.md')
expect(result).toContain('line=1')
expect(result).toContain('col=3')
expect(result).toContain("msg=\"Unknown tag 'badtag'")
expect(result.endsWith('-->')).toBe(true)
})
})
describe('Liquid RenderError', () => {
test('includes original error message when available', () => {
const error = new LiquidError(
"Unknown variable 'variables.nonexistent.value'",
'RenderError',
)
error.token = {
file: '/content/test/intro.md',
getPosition: () => [3, 15],
}
error.originalError = new Error('Variable not found: variables.nonexistent.value')
const result = createTranslationFallbackComment(error, 'rawIntro')
expect(result).toContain('prop=rawIntro')
expect(result).toContain('type=RenderError')
expect(result).toContain('file=/content/test/intro.md')
expect(result).toContain('line=3')
expect(result).toContain('col=15')
expect(result).toContain('msg="Variable not found: variables.nonexistent.value"')
})
test('falls back to main error message when no originalError', () => {
const error = new LiquidError('Main error message', 'RenderError')
error.token = {
file: '/content/test.md',
getPosition: () => [1, 1],
}
const result = createTranslationFallbackComment(error, 'rawTitle')
expect(result).toContain('msg="Main error message"')
})
})
describe('Liquid TokenizationError', () => {
test('includes tokenization error details', () => {
const error = new LiquidError('Unexpected token, line:1, col:10', 'TokenizationError')
error.token = {
file: '/content/test/page.md',
getPosition: () => [1, 10],
}
const result = createTranslationFallbackComment(error, 'markdown')
expect(result).toContain('prop=markdown')
expect(result).toContain('type=TokenizationError')
expect(result).toContain('file=/content/test/page.md')
expect(result).toContain('line=1')
expect(result).toContain('col=10')
expect(result).toContain('msg="Unexpected token, line:1, col:10"')
})
})
describe('TitleFromAutotitleError', () => {
test('includes AUTOTITLE error message', () => {
const error = new TitleFromAutotitleError(
'Could not find target page for [AUTOTITLE] link to invalid-link',
)
error.name = 'TitleFromAutotitleError'
const result = createTranslationFallbackComment(error, 'rawTitle')
expect(result).toContain('prop=rawTitle')
expect(result).toContain('type=TitleFromAutotitleError')
expect(result).toContain(
'msg="Could not find target page for [AUTOTITLE] link to invalid-link"',
)
// Should not contain file/line/col since AUTOTITLE errors don't have tokens
expect(result).not.toContain('file=')
expect(result).not.toContain('line=')
expect(result).not.toContain('col=')
})
})
describe('EmptyTitleError', () => {
test('includes empty content message', () => {
const error = new EmptyTitleError("output for property 'rawTitle' became empty")
error.name = 'EmptyTitleError'
const result = createTranslationFallbackComment(error, 'rawTitle')
expect(result).toContain('prop=rawTitle')
expect(result).toContain('type=EmptyTitleError')
expect(result).toContain('msg="Content became empty after rendering"')
})
})
describe('Error handling edge cases', () => {
test('handles error with no token information gracefully', () => {
const error = new LiquidError('Generic liquid error without token info', 'RenderError')
// No token property set
const result = createTranslationFallbackComment(error, 'rawIntro')
expect(result).toContain('prop=rawIntro')
expect(result).toContain('type=RenderError')
expect(result).toContain('msg="Generic liquid error without token info"')
// Should not contain file/line/col since no token
expect(result).not.toContain('file=')
expect(result).not.toContain('line=')
expect(result).not.toContain('col=')
})
test('handles error with token but no file', () => {
const error = new LiquidError('Error message', 'ParseError')
error.token = {
// No file property
getPosition: () => [5, 10],
}
const result = createTranslationFallbackComment(error, 'markdown')
expect(result).toContain('line=5')
expect(result).toContain('col=10')
expect(result).not.toContain('file=')
})
test('handles error with token but no getPosition method', () => {
const error = new LiquidError('Error message', 'ParseError')
error.token = {
file: '/content/test.md',
// No getPosition method
}
const result = createTranslationFallbackComment(error, 'title')
expect(result).toContain('file=/content/test.md')
expect(result).not.toContain('line=')
expect(result).not.toContain('col=')
})
test('truncates very long error messages', () => {
const longMessage = 'A'.repeat(300) // Very long error message
const error = new LiquidError(longMessage, 'ParseError')
const result = createTranslationFallbackComment(error, 'rawTitle')
expect(result).toContain('msg="')
expect(result).toContain('...')
// Extract the message part to verify truncation
const msgMatch = result.match(/msg="([^"]*)"/)
expect(msgMatch).toBeTruthy()
if (msgMatch?.[1]) {
expect(msgMatch[1].length).toBeLessThanOrEqual(203) // 200 + '...'
}
})
test('properly escapes quotes in error messages', () => {
const error = new LiquidError('Error with "double quotes" and more', 'RenderError')
const result = createTranslationFallbackComment(error, 'rawTitle')
expect(result).toContain('msg="Error with \'double quotes\' and more"')
expect(result).not.toContain('msg="Error with "double quotes"')
})
test('handles error with unknown type', () => {
const error = new Error('Some error')
// No name property (will default to 'Error')
const result = createTranslationFallbackComment(error, 'content')
expect(result).toContain('type=Error')
expect(result).toContain('prop=content')
// Non-liquid errors without specific handling don't get messages
expect(result).not.toContain('msg=')
})
test('handles error with no message', () => {
const error = new LiquidError('', 'ParseError')
const result = createTranslationFallbackComment(error, 'title')
expect(result).toContain('type=ParseError')
expect(result).toContain('prop=title')
// Should handle gracefully, might not have msg or have empty msg
})
test('cleans up multiline messages', () => {
const error = new LiquidError('Line 1\nLine 2\n Line 3 \n\nLine 5', 'RenderError')
const result = createTranslationFallbackComment(error, 'content')
expect(result).toContain('msg="Line 1 Line 2 Line 3 Line 5"')
expect(result).not.toContain('\n')
})
})
describe('Comment format validation', () => {
test('comment format is valid HTML', () => {
const error = new LiquidError('Test error', 'ParseError')
error.token = {
file: '/content/test.md',
getPosition: () => [1, 1],
}
const result = createTranslationFallbackComment(error, 'rawTitle')
// Should be a proper HTML comment
expect(result.startsWith('<!-- TRANSLATION_FALLBACK')).toBe(true)
expect(result.endsWith('-->')).toBe(true)
// Should be on a single line
expect(result).not.toContain('\n')
})
test('contains all required fields when available', () => {
const error = new LiquidError('Detailed error message', 'RenderError')
error.token = {
file: '/content/detailed-test.md',
getPosition: () => [42, 15],
}
const result = createTranslationFallbackComment(error, 'rawIntro')
expect(result).toContain('TRANSLATION_FALLBACK')
expect(result).toContain('prop=rawIntro')
expect(result).toContain('type=RenderError')
expect(result).toContain('file=/content/detailed-test.md')
expect(result).toContain('line=42')
expect(result).toContain('col=15')
expect(result).toContain('msg="Detailed error message"')
})
test('maintains consistent field order', () => {
const error = new LiquidError('Test message', 'ParseError')
error.token = {
file: '/content/test.md',
getPosition: () => [1, 1],
}
const result = createTranslationFallbackComment(error, 'title')
// Should follow the expected structure with all required fields
expect(result.startsWith('<!-- TRANSLATION_FALLBACK')).toBe(true)
expect(result).toContain('prop=title')
expect(result).toContain('type=ParseError')
expect(result).toContain('file=/content/test.md')
expect(result).toContain('line=1')
expect(result).toContain('col=1')
expect(result).toContain('msg="Test message"')
expect(result.endsWith('-->')).toBe(true)
})
})
})
describe('Integration Tests', () => {
describe('renderContentWithFallback', () => {
test('adds HTML comment when translation fails and fallback succeeds', async () => {
// Mock a simple page object that satisfies instanceof Page check
const mockPage = Object.create(Page.prototype)
mockPage.rawTitle = '{% badtag %}'
const context = {
currentLanguage: 'ja',
getEnglishPage: () => {
const enPage = Object.create(Page.prototype)
enPage.rawTitle = 'English Title'
return enPage
},
}
// Mock renderContent to simulate error for Japanese, success for English
mockRenderContent.mockImplementation(
(template: string, innerContext: Record<string, unknown>) => {
if (innerContext.currentLanguage !== 'en' && template.includes('badtag')) {
const error = new LiquidError("Unknown tag 'badtag'", 'ParseError')
error.token = {
file: '/content/test.md',
getPosition: () => [1, 5],
}
throw error
}
return innerContext.currentLanguage === 'en' ? 'English Title' : template
},
)
const result = await renderContentWithFallback(mockPage, 'rawTitle', context)
expect(result).toContain('<!-- TRANSLATION_FALLBACK')
expect(result).toContain('prop=rawTitle')
expect(result).toContain('type=ParseError')
expect(result).toContain('line=1')
expect(result).toContain('col=1')
expect(result).toContain('msg="tag \'badtag\' not found"')
expect(result).toContain('English Title')
})
test('does not add comment for textOnly rendering', async () => {
const mockPage = Object.create(Page.prototype)
mockPage.rawTitle = '{% badtag %}'
const context = {
currentLanguage: 'ja',
getEnglishPage: () => {
const enPage = Object.create(Page.prototype)
enPage.rawTitle = 'English Title'
return enPage
},
}
mockRenderContent.mockImplementation(
(template: string, innerContext: Record<string, unknown>) => {
if (innerContext.currentLanguage !== 'en' && template.includes('badtag')) {
const error = new LiquidError("Unknown tag 'badtag'", 'ParseError')
throw error
}
return 'English Title'
},
)
const result = await renderContentWithFallback(mockPage, 'rawTitle', context, {
textOnly: true,
})
expect(result).not.toContain('<!-- TRANSLATION_FALLBACK')
expect(result).toBe('English Title')
})
})
describe('executeWithFallback', () => {
test('adds HTML comment for HTML content when callable fails', async () => {
const context = {
currentLanguage: 'es',
}
const failingCallable = async () => {
const error = new LiquidError("Unknown variable 'variables.bad'", 'RenderError')
error.token = {
file: '/content/article.md',
getPosition: () => [10, 20],
}
throw error
}
const fallbackCallable = async () => '<div>English fallback content</div>'
const result = await executeWithFallback(context, failingCallable, fallbackCallable)
expect(result).toContain('<!-- TRANSLATION_FALLBACK')
expect(result).toContain('prop=content')
expect(result).toContain('type=RenderError')
expect(result).toContain('file=/content/article.md')
expect(result).toContain('line=10')
expect(result).toContain('col=20')
expect(result).toContain('<div>English fallback content</div>')
})
test('does not add comment for non-HTML content', async () => {
const context = {
currentLanguage: 'fr',
}
const failingCallable = async () => {
const error = new LiquidError('Test error', 'RenderError')
throw error
}
const fallbackCallable = async () => 'Plain text fallback'
const result = await executeWithFallback(context, failingCallable, fallbackCallable)
expect(result).not.toContain('<!-- TRANSLATION_FALLBACK')
expect(result).toBe('Plain text fallback')
})
})
})
})