collavre_translation 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/README.md +83 -0
- data/app/controllers/collavre_translation/creative_translations_controller.rb +48 -0
- data/app/controllers/collavre_translation/translations_controller.rb +52 -0
- data/app/controllers/concerns/collavre_translation/translation_locale.rb +9 -0
- data/app/javascript/collavre_translation.js +1 -0
- data/app/javascript/controllers/__tests__/comment_translation_controller.test.js +377 -0
- data/app/javascript/controllers/__tests__/comment_translation_reader_controller.test.js +106 -0
- data/app/javascript/controllers/__tests__/creative_translations_controller.test.js +667 -0
- data/app/javascript/controllers/__tests__/translation_locale.test.js +14 -0
- data/app/javascript/controllers/__tests__/translation_preference_controller.test.js +29 -0
- data/app/javascript/controllers/__tests__/translation_rendering.test.js +34 -0
- data/app/javascript/controllers/comment_translation_controller.js +163 -0
- data/app/javascript/controllers/comment_translation_reader_controller.js +28 -0
- data/app/javascript/controllers/creative_translations_controller.js +178 -0
- data/app/javascript/controllers/index.js +12 -0
- data/app/javascript/controllers/translation_locale.js +8 -0
- data/app/javascript/controllers/translation_preference_controller.js +13 -0
- data/app/javascript/controllers/workspace_translation.js +46 -0
- data/app/jobs/collavre_translation/translate_job.rb +66 -0
- data/app/models/collavre_translation/translation.rb +54 -0
- data/app/services/collavre_translation/content_translation_policy.rb +18 -0
- data/app/services/collavre_translation/creative_translation_policy.rb +10 -0
- data/app/services/collavre_translation/html_translator.rb +39 -0
- data/app/services/collavre_translation/language_detector.rb +24 -0
- data/app/services/collavre_translation/protected_content.rb +31 -0
- data/app/services/collavre_translation/translator.rb +25 -0
- data/app/views/collavre_translation/comments/_reader.html.erb +5 -0
- data/app/views/collavre_translation/comments/_translation.html.erb +20 -0
- data/app/views/collavre_translation/creatives/_translation.html.erb +7 -0
- data/app/views/collavre_translation/preferences/_settings.html.erb +5 -0
- data/config/locales/en.yml +11 -0
- data/config/locales/ko.yml +11 -0
- data/config/locales/preferences.en.yml +5 -0
- data/config/locales/preferences.ko.yml +5 -0
- data/config/routes.rb +8 -0
- data/db/migrate/20261001000000_create_collavre_translation_translations.rb +18 -0
- data/db/migrate/20261001001000_add_auto_translation_enabled_to_users.rb +5 -0
- data/lib/collavre_translation/engine.rb +39 -0
- data/lib/collavre_translation/version.rb +3 -0
- data/lib/collavre_translation.rb +26 -0
- metadata +118 -0
|
@@ -0,0 +1,667 @@
|
|
|
1
|
+
/** @jest-environment jsdom */
|
|
2
|
+
import { jest } from '@jest/globals'
|
|
3
|
+
import { Application } from '@hotwired/stimulus'
|
|
4
|
+
import { setCreativeTitle } from 'collavre/lib/creative_title'
|
|
5
|
+
|
|
6
|
+
const fetchMock = jest.fn()
|
|
7
|
+
jest.unstable_mockModule('collavre/lib/api/csrf_fetch', () => ({ default: fetchMock }))
|
|
8
|
+
const { addTableDownloadButtons } = await import("collavre/lib/utils/table_download")
|
|
9
|
+
const { default: Controller } = await import('../creative_translations_controller')
|
|
10
|
+
const tick = () => new Promise(resolve => setTimeout(resolve, 0))
|
|
11
|
+
const response = (status, content = null, digest = 'digest') => ({ ok: true,
|
|
12
|
+
json: async () => ({ status, content, source_digest: digest, original_html: row.descriptionHtml }) })
|
|
13
|
+
const pairs = JSON.stringify([{ original: 'English title', translated: '번역 제목' },
|
|
14
|
+
{ original: 'Link label', translated: '<script>safe text</script>' },
|
|
15
|
+
{ original: 'Code', translated: 'must not change' },
|
|
16
|
+
{ original: '@Astra:', translated: 'must not change' }])
|
|
17
|
+
let app, controller, row, intersection
|
|
18
|
+
beforeEach(async () => {
|
|
19
|
+
global.IntersectionObserver = class {
|
|
20
|
+
constructor(callback) { intersection = callback }
|
|
21
|
+
observe = jest.fn()
|
|
22
|
+
unobserve = jest.fn()
|
|
23
|
+
disconnect = jest.fn()
|
|
24
|
+
}
|
|
25
|
+
document.body.innerHTML = `<div id="creative-overflow-menu"></div><creative-tree-row creative-id="1"><div class="creative-content"><h1>English title</h1><a href="/path">Link label</a><pre><code>Code</code></pre><span class="mention">@Astra:</span></div></creative-tree-row>
|
|
26
|
+
<div data-controller="creative-translations" data-creative-translations-base-value="/translation/creatives/__ID__/translation"
|
|
27
|
+
data-creative-translations-original-value="Show original" data-creative-translations-translated-value="Show translation"></div>`
|
|
28
|
+
row = document.querySelector('creative-tree-row')
|
|
29
|
+
row.descriptionHtml = '<h1>English title</h1>'
|
|
30
|
+
app = Application.start()
|
|
31
|
+
app.register('creative-translations', Controller)
|
|
32
|
+
await tick()
|
|
33
|
+
controller = app.getControllerForElementAndIdentifier(document.querySelector('[data-controller]'), 'creative-translations')
|
|
34
|
+
fetchMock.mockReset()
|
|
35
|
+
})
|
|
36
|
+
afterEach(async () => {
|
|
37
|
+
controller.disconnect()
|
|
38
|
+
app.stop()
|
|
39
|
+
document.body.innerHTML = ''
|
|
40
|
+
await tick()
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
test('loads visible creatives, preserves live structure and toggles original text', async () => {
|
|
44
|
+
const link = row.querySelector('a')
|
|
45
|
+
const handler = jest.fn(event => event.preventDefault())
|
|
46
|
+
link.addEventListener('click', handler)
|
|
47
|
+
expect(fetchMock).not.toHaveBeenCalled()
|
|
48
|
+
intersection([{ target: row, isIntersecting: false }])
|
|
49
|
+
expect(fetchMock).not.toHaveBeenCalled()
|
|
50
|
+
fetchMock.mockResolvedValueOnce(response('missing')).mockResolvedValueOnce(response('completed', pairs))
|
|
51
|
+
intersection([{ target: row, isIntersecting: true }])
|
|
52
|
+
await tick()
|
|
53
|
+
expect(fetchMock.mock.calls.map(call => call[1].method)).toEqual(['GET', 'POST'])
|
|
54
|
+
expect(fetchMock.mock.calls[0][0]).toBe('/translation/creatives/1/translation')
|
|
55
|
+
expect(row.querySelector('h1').textContent).toBe('번역 제목')
|
|
56
|
+
expect(row.querySelector('a')).toBe(link)
|
|
57
|
+
expect(link.getAttribute('href')).toBe('/path')
|
|
58
|
+
expect(link.textContent).toBe('<script>safe text</script>')
|
|
59
|
+
expect(row.querySelector('script')).toBeNull()
|
|
60
|
+
link.click()
|
|
61
|
+
expect(handler).toHaveBeenCalledTimes(1)
|
|
62
|
+
expect(row.querySelector('code').textContent).toBe('Code')
|
|
63
|
+
expect(row.querySelector('.mention').textContent).toBe('@Astra:')
|
|
64
|
+
expect(row.descriptionHtml).toBe('<h1>English title</h1>')
|
|
65
|
+
const button = document.querySelector('.creative-translation-toggle')
|
|
66
|
+
button.click()
|
|
67
|
+
expect(row.querySelector('h1').textContent).toBe('English title')
|
|
68
|
+
expect(button.getAttribute('aria-pressed')).toBe('false')
|
|
69
|
+
button.click()
|
|
70
|
+
expect(row.querySelector('h1').textContent).toBe('번역 제목')
|
|
71
|
+
controller.show(row, controller.rows.get(row), JSON.parse(pairs))
|
|
72
|
+
expect(row.querySelectorAll('.creative-translation-toggle')).toHaveLength(0)
|
|
73
|
+
expect(document.querySelectorAll('.creative-translation-toggle')).toHaveLength(1)
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
test('polls pending cache and stops when source digest changes', async () => {
|
|
77
|
+
fetchMock.mockResolvedValueOnce(response('pending')).mockResolvedValueOnce(response('processing'))
|
|
78
|
+
await controller.load(row)
|
|
79
|
+
const state = controller.rows.get(row)
|
|
80
|
+
expect(state.delay).toBe(1000)
|
|
81
|
+
clearTimeout(state.timer)
|
|
82
|
+
fetchMock.mockResolvedValueOnce(response("processing"))
|
|
83
|
+
await new Promise(resolve => { state.timer = setTimeout(async () => { await controller.load(row); resolve() }, 0) })
|
|
84
|
+
clearTimeout(state.timer)
|
|
85
|
+
fetchMock.mockResolvedValueOnce(response('completed', pairs, 'changed'))
|
|
86
|
+
await controller.load(row)
|
|
87
|
+
expect(row.querySelector('h1').textContent).toBe('English title')
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
test.each(['failed', 'skipped'])('%s keeps original without polling', async status => {
|
|
91
|
+
fetchMock.mockResolvedValue(response(status))
|
|
92
|
+
await controller.load(row)
|
|
93
|
+
expect(controller.rows.get(row).timer).toBeUndefined()
|
|
94
|
+
expect(row.querySelector('button')).toBeNull()
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
test('HTTP, network and malformed payload failures preserve original', async () => {
|
|
98
|
+
fetchMock.mockResolvedValueOnce({ ok: false }).mockRejectedValueOnce(new Error('network'))
|
|
99
|
+
.mockResolvedValueOnce(response('completed', 'invalid JSON'))
|
|
100
|
+
for (let i = 0; i < 3; i++) await controller.load(row)
|
|
101
|
+
expect(row.querySelector('h1').textContent).toBe('English title')
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
test('source changes abort old requests and register fresh rows', async () => {
|
|
105
|
+
let resolve
|
|
106
|
+
fetchMock.mockImplementation(() => new Promise(done => { resolve = done }))
|
|
107
|
+
const loading = controller.load(row)
|
|
108
|
+
const old = controller.rows.get(row)
|
|
109
|
+
row.descriptionHtml = 'Changed source'
|
|
110
|
+
controller.scan()
|
|
111
|
+
expect(old.abort.signal.aborted).toBe(true)
|
|
112
|
+
resolve(response('completed', pairs))
|
|
113
|
+
await loading
|
|
114
|
+
expect(row.querySelector('h1').textContent).toBe('English title')
|
|
115
|
+
expect(controller.rows.get(row)).not.toBe(old)
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
test('rerendered and removed rows clean up and restore originals', async () => {
|
|
119
|
+
fetchMock.mockResolvedValue(response('completed', pairs))
|
|
120
|
+
await controller.load(row)
|
|
121
|
+
const old = controller.rows.get(row)
|
|
122
|
+
row.querySelector('.creative-content').outerHTML = '<div class="creative-content"><h1>English title</h1></div>'
|
|
123
|
+
controller.scan()
|
|
124
|
+
expect(old.abort.signal.aborted).toBe(true)
|
|
125
|
+
await controller.load(row)
|
|
126
|
+
const state = controller.rows.get(row)
|
|
127
|
+
row.remove()
|
|
128
|
+
controller.scan()
|
|
129
|
+
expect(state.abort.signal.aborted).toBe(true)
|
|
130
|
+
expect(controller.rows.size).toBe(0)
|
|
131
|
+
await controller.load(row)
|
|
132
|
+
})
|
|
133
|
+
|
|
134
|
+
test('empty, unmatched and missing content never create a toggle', () => {
|
|
135
|
+
const state = controller.rows.get(row)
|
|
136
|
+
controller.show(row, state, [])
|
|
137
|
+
expect(row.querySelector('button')).toBeNull()
|
|
138
|
+
row.querySelector('.creative-content').remove()
|
|
139
|
+
controller.show(row, state, JSON.parse(pairs))
|
|
140
|
+
expect(row.querySelector('button')).toBeNull()
|
|
141
|
+
})
|
|
142
|
+
|
|
143
|
+
test('title content translates and disconnect restores live original nodes', async () => {
|
|
144
|
+
row.querySelector('.creative-content').className = 'creative-title-content'
|
|
145
|
+
fetchMock.mockResolvedValue(response('completed', pairs))
|
|
146
|
+
await controller.load(row)
|
|
147
|
+
expect(row.querySelector('h1').textContent).toBe('번역 제목')
|
|
148
|
+
controller.disconnect()
|
|
149
|
+
expect(row.querySelector('h1').textContent).toBe('English title')
|
|
150
|
+
expect(row.querySelector('button')).toBeNull()
|
|
151
|
+
})
|
|
152
|
+
|
|
153
|
+
test('scheduled poll executes the actual callback and stops at completion', async () => {
|
|
154
|
+
jest.useFakeTimers()
|
|
155
|
+
try {
|
|
156
|
+
fetchMock.mockResolvedValueOnce(response('processing')).mockResolvedValueOnce(response('completed', pairs))
|
|
157
|
+
await controller.load(row)
|
|
158
|
+
await jest.advanceTimersByTimeAsync(1000)
|
|
159
|
+
expect(row.querySelector('h1').textContent).toBe('번역 제목')
|
|
160
|
+
} finally {
|
|
161
|
+
jest.useRealTimers()
|
|
162
|
+
}
|
|
163
|
+
})
|
|
164
|
+
|
|
165
|
+
test('server source mismatch leaves the stale view untranslated', async () => {
|
|
166
|
+
const stale = response('completed', pairs)
|
|
167
|
+
stale.json = async () => ({ status: 'completed', content: pairs, source_digest: 'new', original_html: 'New original' })
|
|
168
|
+
fetchMock.mockResolvedValue(stale)
|
|
169
|
+
await controller.load(row)
|
|
170
|
+
expect(row.querySelector('button')).toBeNull()
|
|
171
|
+
})
|
|
172
|
+
|
|
173
|
+
test('CSV and Excel export the original table while translated display returns', async () => {
|
|
174
|
+
row.querySelector('.creative-content').innerHTML = '<table><tr><th>English title</th></tr><tr><td>Link label</td></tr></table>'
|
|
175
|
+
const content = row.querySelector('.creative-content')
|
|
176
|
+
addTableDownloadButtons(content)
|
|
177
|
+
fetchMock.mockResolvedValue(response('completed', pairs))
|
|
178
|
+
await controller.load(row)
|
|
179
|
+
const exported = []
|
|
180
|
+
URL.createObjectURL = jest.fn(blob => { exported.push(blob); return 'blob:test' })
|
|
181
|
+
URL.revokeObjectURL = jest.fn()
|
|
182
|
+
const click = jest.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(() => {})
|
|
183
|
+
const read = blob => new Promise(resolve => { const reader = new FileReader(); reader.onload = () => resolve(reader.result); reader.readAsText(blob) })
|
|
184
|
+
try {
|
|
185
|
+
for (const button of row.querySelectorAll('.table-download-btn')) {
|
|
186
|
+
button.click()
|
|
187
|
+
expect(content.querySelector('th').textContent).toBe('English title')
|
|
188
|
+
await tick()
|
|
189
|
+
expect(content.querySelector('th').textContent).toBe('번역 제목')
|
|
190
|
+
}
|
|
191
|
+
for (const blob of exported) {
|
|
192
|
+
const text = await read(blob)
|
|
193
|
+
expect(text).toContain('English title')
|
|
194
|
+
expect(text).toContain('Link label')
|
|
195
|
+
expect(text).not.toContain('번역 제목')
|
|
196
|
+
}
|
|
197
|
+
controller.toggle(controller.rows.get(row))
|
|
198
|
+
row.querySelector('.table-download-btn').click()
|
|
199
|
+
expect(content.querySelector('th').textContent).toBe('English title')
|
|
200
|
+
controller.toggle(controller.rows.get(row))
|
|
201
|
+
row.querySelector('.table-download-btn').click()
|
|
202
|
+
controller.cleanup(row, controller.rows.get(row))
|
|
203
|
+
await tick()
|
|
204
|
+
expect(content.querySelector('th').textContent).toBe('English title')
|
|
205
|
+
} finally { click.mockRestore() }
|
|
206
|
+
})
|
|
207
|
+
|
|
208
|
+
test('shared live append and replace use the existing reader controller', async () => {
|
|
209
|
+
const originalRow = row
|
|
210
|
+
const sharedRow = originalRow.cloneNode(true)
|
|
211
|
+
sharedRow.setAttribute('creative-id', '2')
|
|
212
|
+
sharedRow.descriptionHtml = originalRow.descriptionHtml
|
|
213
|
+
document.body.append(sharedRow)
|
|
214
|
+
await tick()
|
|
215
|
+
expect(controller.rows.has(sharedRow)).toBe(true)
|
|
216
|
+
row = sharedRow
|
|
217
|
+
fetchMock.mockResolvedValue(response('completed', pairs))
|
|
218
|
+
intersection([{ target: sharedRow, isIntersecting: true }])
|
|
219
|
+
await tick()
|
|
220
|
+
expect(sharedRow.querySelector('h1').textContent).toBe('번역 제목')
|
|
221
|
+
expect(fetchMock.mock.calls[0][0]).toBe('/translation/creatives/2/translation')
|
|
222
|
+
const replacement = originalRow.cloneNode(true)
|
|
223
|
+
replacement.descriptionHtml = originalRow.descriptionHtml
|
|
224
|
+
sharedRow.replaceWith(replacement)
|
|
225
|
+
row = replacement
|
|
226
|
+
await tick()
|
|
227
|
+
expect(controller.rows.has(sharedRow)).toBe(false)
|
|
228
|
+
expect(controller.rows.has(replacement)).toBe(true)
|
|
229
|
+
intersection([{ target: replacement, isIntersecting: true }])
|
|
230
|
+
await tick()
|
|
231
|
+
expect(replacement.querySelector('h1').textContent).toBe('번역 제목')
|
|
232
|
+
expect(document.querySelectorAll('[data-controller="creative-translations"]')).toHaveLength(1)
|
|
233
|
+
})
|
|
234
|
+
|
|
235
|
+
test('list menu applies original mode to later descendants and completed requests', async () => {
|
|
236
|
+
document.querySelector('.creative-translation-toggle').click()
|
|
237
|
+
fetchMock.mockResolvedValue(response('completed', pairs))
|
|
238
|
+
await controller.load(row)
|
|
239
|
+
expect(row.querySelector('h1').textContent).toBe('English title')
|
|
240
|
+
const child = document.createElement('creative-tree-row')
|
|
241
|
+
child.setAttribute('creative-id', '2')
|
|
242
|
+
child.descriptionHtml = row.descriptionHtml
|
|
243
|
+
child.innerHTML = '<div class="creative-content"><h1>English title</h1></div>'
|
|
244
|
+
document.body.append(child)
|
|
245
|
+
controller.scan()
|
|
246
|
+
await controller.load(child)
|
|
247
|
+
expect(child.querySelector('h1').textContent).toBe('English title')
|
|
248
|
+
document.querySelector('.creative-translation-toggle').click()
|
|
249
|
+
expect(row.querySelector('h1').textContent).toBe('번역 제목')
|
|
250
|
+
expect(child.querySelector('h1').textContent).toBe('번역 제목')
|
|
251
|
+
})
|
|
252
|
+
|
|
253
|
+
test('menu replacement preserves list mode without duplicate controls', () => {
|
|
254
|
+
controller.menuButton.click()
|
|
255
|
+
document.getElementById('creative-overflow-menu').remove()
|
|
256
|
+
controller.scan()
|
|
257
|
+
const menu = document.createElement('div')
|
|
258
|
+
menu.id = 'creative-overflow-menu'
|
|
259
|
+
document.body.append(menu)
|
|
260
|
+
controller.scan()
|
|
261
|
+
controller.scan()
|
|
262
|
+
expect(menu.children).toHaveLength(1)
|
|
263
|
+
expect(menu.textContent).toBe('Show translation')
|
|
264
|
+
})
|
|
265
|
+
|
|
266
|
+
function addTreeLink(label = 'English title') {
|
|
267
|
+
const link = document.createElement('a')
|
|
268
|
+
link.className = 'creative-workspace-tree-link'
|
|
269
|
+
link.dataset.creativeId = '1'
|
|
270
|
+
link.dataset.originalLabel = label
|
|
271
|
+
link.dataset.creativeSnippet = 'Original snippet'
|
|
272
|
+
link.href = '/creatives?id=1'
|
|
273
|
+
link.textContent = label
|
|
274
|
+
document.body.append(link)
|
|
275
|
+
controller.scan()
|
|
276
|
+
return link
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
test('workspace titles translate independently, preserve navigation and follow the list toggle', async () => {
|
|
280
|
+
const link = addTreeLink()
|
|
281
|
+
const clicked = jest.fn(event => event.preventDefault())
|
|
282
|
+
link.addEventListener('click', clicked)
|
|
283
|
+
fetchMock.mockResolvedValue(response('completed', pairs))
|
|
284
|
+
intersection([{ target: link, isIntersecting: true }])
|
|
285
|
+
await tick()
|
|
286
|
+
expect(link.textContent).toBe('번역 제목')
|
|
287
|
+
expect(link.dataset.originalLabel).toBe('English title')
|
|
288
|
+
expect(link.dataset.creativeSnippet).toBe('Original snippet')
|
|
289
|
+
expect(link.getAttribute('href')).toBe('/creatives?id=1')
|
|
290
|
+
link.click()
|
|
291
|
+
expect(clicked).toHaveBeenCalledTimes(1)
|
|
292
|
+
controller.menuButton.click()
|
|
293
|
+
expect(link.textContent).toBe('English title')
|
|
294
|
+
const child = addTreeLink()
|
|
295
|
+
await controller.load(child)
|
|
296
|
+
expect(child.textContent).toBe('English title')
|
|
297
|
+
controller.menuButton.click()
|
|
298
|
+
expect(child.textContent).toBe('번역 제목')
|
|
299
|
+
const state = controller.rows.get(link)
|
|
300
|
+
link.remove()
|
|
301
|
+
controller.scan()
|
|
302
|
+
expect(state.abort.signal.aborted).toBe(true)
|
|
303
|
+
controller.disconnect()
|
|
304
|
+
expect(child.textContent).toBe('English title')
|
|
305
|
+
})
|
|
306
|
+
|
|
307
|
+
test('workspace polling and stale labels keep the original until a matching result completes', async () => {
|
|
308
|
+
const link = addTreeLink()
|
|
309
|
+
fetchMock.mockResolvedValueOnce(response('processing'))
|
|
310
|
+
await controller.load(link)
|
|
311
|
+
const state = controller.rows.get(link)
|
|
312
|
+
expect(state.delay).toBe(1000)
|
|
313
|
+
clearTimeout(state.timer)
|
|
314
|
+
fetchMock.mockResolvedValue(response('completed', pairs))
|
|
315
|
+
await controller.load(link)
|
|
316
|
+
expect(link.textContent).toBe('번역 제목')
|
|
317
|
+
const stale = addTreeLink('Edited title')
|
|
318
|
+
await controller.load(stale)
|
|
319
|
+
expect(stale.textContent).toBe('Edited title')
|
|
320
|
+
})
|
|
321
|
+
|
|
322
|
+
test('workspace labels combine inline text, decode entities and preserve protected content safely', async () => {
|
|
323
|
+
const link = addTreeLink('English title Code @Astra:')
|
|
324
|
+
fetchMock.mockResolvedValue({ ok: true, json: async () => ({ status: 'completed', content: pairs,
|
|
325
|
+
original_html: '<h1>English title</h1> <code>Code</code> <span class="mention">@Astra:</span>', source_digest: 'digest' }) })
|
|
326
|
+
await controller.load(link)
|
|
327
|
+
expect(link.textContent).toBe('English title Code @Astra:')
|
|
328
|
+
const rich = addTreeLink('English titleLink label Code @Astra:')
|
|
329
|
+
fetchMock.mockResolvedValue({ ok: true, json: async () => ({ status: 'completed', content: pairs,
|
|
330
|
+
original_html: '<h1>English title<a href="/">Link label</a></h1> <code>Code</code> <span class="mention">@Astra:</span>', source_digest: 'digest' }) })
|
|
331
|
+
await controller.load(rich)
|
|
332
|
+
expect(rich.textContent).toBe('번역 제목<script>safe text</script> Code @Astra:')
|
|
333
|
+
expect(rich.querySelector('script')).toBeNull()
|
|
334
|
+
})
|
|
335
|
+
|
|
336
|
+
test('workspace labels request the unembedded source so YouTube link text still matches', async () => {
|
|
337
|
+
const link = addTreeLink('English title Link label')
|
|
338
|
+
fetchMock.mockResolvedValue({ ok: true, json: async () => ({ status: 'completed', content: pairs,
|
|
339
|
+
original_html: '<h1>English title</h1> <a href="https://youtu.be/dQw4w9WgXcQ">Link label</a>', source_digest: 'digest' }) })
|
|
340
|
+
await controller.load(link)
|
|
341
|
+
expect(fetchMock).toHaveBeenLastCalledWith('/translation/creatives/1/translation?embed=0', expect.anything())
|
|
342
|
+
expect(link.textContent).toBe('번역 제목 <script>safe text</script>')
|
|
343
|
+
await controller.load(row)
|
|
344
|
+
expect(fetchMock).toHaveBeenLastCalledWith('/translation/creatives/1/translation', expect.anything())
|
|
345
|
+
})
|
|
346
|
+
|
|
347
|
+
test('failed creative retries on reload but failure while polling waits for another reload', async () => {
|
|
348
|
+
fetchMock.mockResolvedValueOnce(response('failed')).mockResolvedValueOnce(response('processing'))
|
|
349
|
+
await controller.load(row)
|
|
350
|
+
expect(fetchMock.mock.calls.map(call => call[1].method)).toEqual(['GET', 'POST'])
|
|
351
|
+
clearTimeout(controller.rows.get(row).timer)
|
|
352
|
+
fetchMock.mockResolvedValueOnce(response('failed'))
|
|
353
|
+
await controller.load(row, false)
|
|
354
|
+
expect(fetchMock.mock.calls.map(call => call[1].method)).toEqual(['GET', 'POST', 'GET'])
|
|
355
|
+
expect(row.querySelector('h1').textContent).toBe('English title')
|
|
356
|
+
controller.cleanup(row, controller.rows.get(row))
|
|
357
|
+
controller.scan()
|
|
358
|
+
fetchMock.mockResolvedValueOnce(response('failed')).mockResolvedValueOnce(response('completed', pairs))
|
|
359
|
+
await controller.load(row)
|
|
360
|
+
expect(fetchMock.mock.calls.map(call => call[1].method)).toEqual(['GET', 'POST', 'GET', 'GET', 'POST'])
|
|
361
|
+
expect(row.querySelector('h1').textContent).toBe('번역 제목')
|
|
362
|
+
})
|
|
363
|
+
|
|
364
|
+
function addNavigationLabel(text, id = '1', length = '24') {
|
|
365
|
+
const title = document.createElement('h3')
|
|
366
|
+
title.dataset.translationLabel = ''
|
|
367
|
+
title.dataset.translationLength = length
|
|
368
|
+
title.dataset.creativeId = id
|
|
369
|
+
title.dataset.originalLabel = text
|
|
370
|
+
title.textContent = text
|
|
371
|
+
document.body.append(title)
|
|
372
|
+
controller.scan()
|
|
373
|
+
return title
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
test('breadcrumb and chat labels translate, truncate after translation and toggle together', async () => {
|
|
377
|
+
const breadcrumb = addNavigationLabel('English title', '1', '20')
|
|
378
|
+
const chat = addNavigationLabel('English title')
|
|
379
|
+
const translated = 'A translated title that is much longer than the original'
|
|
380
|
+
fetchMock.mockResolvedValue(response('completed', JSON.stringify([{ original: 'English title', translated }])))
|
|
381
|
+
await controller.load(breadcrumb)
|
|
382
|
+
await controller.load(chat)
|
|
383
|
+
expect(breadcrumb.textContent).toBe(translated.slice(0, 17) + '...')
|
|
384
|
+
expect(chat.textContent).toBe(translated.slice(0, 21) + '...')
|
|
385
|
+
controller.menuButton.click()
|
|
386
|
+
expect(breadcrumb.textContent).toBe('English title')
|
|
387
|
+
expect(chat.textContent).toBe('English title')
|
|
388
|
+
})
|
|
389
|
+
|
|
390
|
+
test('truncated original labels match stored descriptions and reject stale titles', async () => {
|
|
391
|
+
const title = addNavigationLabel('English...', '1', '10')
|
|
392
|
+
fetchMock.mockResolvedValue(response('completed', pairs))
|
|
393
|
+
await controller.load(title)
|
|
394
|
+
expect(title.textContent).toBe('번역 제목')
|
|
395
|
+
const stale = addNavigationLabel('Different...', '1', '10')
|
|
396
|
+
await controller.load(stale)
|
|
397
|
+
expect(stale.textContent).toBe('Different...')
|
|
398
|
+
})
|
|
399
|
+
|
|
400
|
+
test('chat navigation aborts stale requests and preserves the newly assigned title', async () => {
|
|
401
|
+
const title = addNavigationLabel('English title')
|
|
402
|
+
fetchMock.mockResolvedValue(response('completed', pairs))
|
|
403
|
+
await controller.load(title)
|
|
404
|
+
const previous = controller.rows.get(title)
|
|
405
|
+
title.textContent = 'New title'
|
|
406
|
+
title.dataset.originalLabel = 'New title'
|
|
407
|
+
title.dataset.creativeId = '2'
|
|
408
|
+
await tick()
|
|
409
|
+
expect(previous.abort.signal.aborted).toBe(true)
|
|
410
|
+
expect(title.textContent).toBe('New title')
|
|
411
|
+
expect(controller.rows.get(title).id).toBe('2')
|
|
412
|
+
fetchMock.mockResolvedValue({ ok: true, json: async () => ({ status: 'completed',
|
|
413
|
+
original_html: '<p>New title</p>', content: JSON.stringify([{ original: 'New title', translated: '새 제목' }]) }) })
|
|
414
|
+
await controller.load(title)
|
|
415
|
+
expect(title.textContent).toBe('새 제목')
|
|
416
|
+
expect(fetchMock).toHaveBeenLastCalledWith('/translation/creatives/2/translation?embed=0', expect.anything())
|
|
417
|
+
title.textContent = 'Comments'
|
|
418
|
+
delete title.dataset.creativeId
|
|
419
|
+
delete title.dataset.originalLabel
|
|
420
|
+
await tick()
|
|
421
|
+
expect(controller.rows.has(title)).toBe(false)
|
|
422
|
+
expect(title.textContent).toBe('Comments')
|
|
423
|
+
})
|
|
424
|
+
|
|
425
|
+
test('reopening the same chat preserves translated nodes and original toggling', async () => {
|
|
426
|
+
const title = addNavigationLabel('English title')
|
|
427
|
+
fetchMock.mockResolvedValue(response('completed', pairs))
|
|
428
|
+
await controller.load(title)
|
|
429
|
+
const state = controller.rows.get(title)
|
|
430
|
+
const node = title.firstChild
|
|
431
|
+
expect(title.textContent).toBe('번역 제목')
|
|
432
|
+
title.hidden = true
|
|
433
|
+
setCreativeTitle(title, 'English title', 1)
|
|
434
|
+
title.hidden = false
|
|
435
|
+
await tick()
|
|
436
|
+
expect(controller.rows.get(title)).toBe(state)
|
|
437
|
+
expect(title.firstChild).toBe(node)
|
|
438
|
+
expect(title.textContent).toBe('번역 제목')
|
|
439
|
+
controller.menuButton.click()
|
|
440
|
+
expect(title.textContent).toBe('English title')
|
|
441
|
+
setCreativeTitle(title, 'English title', '1')
|
|
442
|
+
await tick()
|
|
443
|
+
controller.menuButton.click()
|
|
444
|
+
expect(title.textContent).toBe('번역 제목')
|
|
445
|
+
})
|
|
446
|
+
|
|
447
|
+
async function mountManual() {
|
|
448
|
+
controller.disconnect()
|
|
449
|
+
app.stop()
|
|
450
|
+
document.querySelector('[data-controller]').dataset.creativeTranslationsAutoValue = 'false'
|
|
451
|
+
app = Application.start()
|
|
452
|
+
app.register('creative-translations', Controller)
|
|
453
|
+
await tick()
|
|
454
|
+
controller = app.getControllerForElementAndIdentifier(document.querySelector('[data-controller]'), 'creative-translations')
|
|
455
|
+
fetchMock.mockReset()
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
test('automatic translation off offers the translation first and translates only on request', async () => {
|
|
459
|
+
await mountManual()
|
|
460
|
+
const button = document.querySelector('.creative-translation-toggle')
|
|
461
|
+
expect(button.textContent).toBe('Show translation')
|
|
462
|
+
expect(button.getAttribute('aria-pressed')).toBe('false')
|
|
463
|
+
intersection([{ target: row, isIntersecting: true }, { target: document.createElement('div'), isIntersecting: true }])
|
|
464
|
+
await tick()
|
|
465
|
+
expect(fetchMock).not.toHaveBeenCalled()
|
|
466
|
+
expect(row.querySelector('h1').textContent).toBe('English title')
|
|
467
|
+
fetchMock.mockResolvedValueOnce(response('missing')).mockResolvedValueOnce(response('completed', pairs))
|
|
468
|
+
button.click()
|
|
469
|
+
await tick()
|
|
470
|
+
expect(fetchMock.mock.calls.map(call => [call[0], call[1].method])).toEqual([
|
|
471
|
+
['/translation/creatives/1/translation?manual=1', 'GET'], ['/translation/creatives/1/translation?manual=1', 'POST']])
|
|
472
|
+
expect(row.querySelector('h1').textContent).toBe('번역 제목')
|
|
473
|
+
expect(button.textContent).toBe('Show original')
|
|
474
|
+
button.click()
|
|
475
|
+
expect(row.querySelector('h1').textContent).toBe('English title')
|
|
476
|
+
button.click()
|
|
477
|
+
expect(fetchMock).toHaveBeenCalledTimes(2)
|
|
478
|
+
expect(row.querySelector('h1').textContent).toBe('번역 제목')
|
|
479
|
+
const link = addTreeLink()
|
|
480
|
+
fetchMock.mockResolvedValue(response('completed', pairs))
|
|
481
|
+
intersection([{ target: link, isIntersecting: true }])
|
|
482
|
+
await tick()
|
|
483
|
+
expect(fetchMock).toHaveBeenLastCalledWith('/translation/creatives/1/translation?embed=0&manual=1', expect.anything())
|
|
484
|
+
expect(link.textContent).toBe('번역 제목')
|
|
485
|
+
})
|
|
486
|
+
|
|
487
|
+
test('showing translations re-requests missing results without duplicating in-flight loads', async () => {
|
|
488
|
+
fetchMock.mockResolvedValue(response('skipped'))
|
|
489
|
+
intersection([{ target: row, isIntersecting: true }])
|
|
490
|
+
await tick()
|
|
491
|
+
expect(fetchMock).toHaveBeenCalledTimes(1)
|
|
492
|
+
const state = controller.rows.get(row)
|
|
493
|
+
controller.menuButton.click()
|
|
494
|
+
expect(fetchMock).toHaveBeenCalledTimes(1)
|
|
495
|
+
fetchMock.mockReset()
|
|
496
|
+
let resolve
|
|
497
|
+
fetchMock.mockImplementation(() => new Promise(done => { resolve = done }))
|
|
498
|
+
controller.menuButton.click()
|
|
499
|
+
expect(state.loading).toBe(true)
|
|
500
|
+
controller.menuButton.click()
|
|
501
|
+
controller.menuButton.click()
|
|
502
|
+
expect(fetchMock).toHaveBeenCalledTimes(1)
|
|
503
|
+
expect(fetchMock.mock.calls[0][0]).toBe('/translation/creatives/1/translation?manual=1')
|
|
504
|
+
resolve(response('processing'))
|
|
505
|
+
await tick()
|
|
506
|
+
expect(state.loading).toBe(false)
|
|
507
|
+
expect(state.timer).toBeTruthy()
|
|
508
|
+
controller.menuButton.click()
|
|
509
|
+
controller.menuButton.click()
|
|
510
|
+
expect(fetchMock).toHaveBeenCalledTimes(1)
|
|
511
|
+
clearTimeout(state.timer)
|
|
512
|
+
})
|
|
513
|
+
|
|
514
|
+
test('showing translations during an automatic request retries manually when that request fails', async () => {
|
|
515
|
+
let reject
|
|
516
|
+
fetchMock.mockImplementationOnce(() => new Promise((_, fail) => { reject = fail }))
|
|
517
|
+
intersection([{ target: row, isIntersecting: true }])
|
|
518
|
+
const state = controller.rows.get(row)
|
|
519
|
+
expect(fetchMock.mock.calls[0][0]).toBe('/translation/creatives/1/translation')
|
|
520
|
+
controller.menuButton.click()
|
|
521
|
+
controller.menuButton.click()
|
|
522
|
+
expect(state.retry).toBe(true)
|
|
523
|
+
fetchMock.mockResolvedValueOnce(response('failed')).mockResolvedValueOnce(response('completed', pairs))
|
|
524
|
+
reject(new Error('offline'))
|
|
525
|
+
await tick()
|
|
526
|
+
expect(fetchMock.mock.calls.slice(1).map(call => [call[0], call[1].method])).toEqual([
|
|
527
|
+
['/translation/creatives/1/translation?manual=1', 'GET'], ['/translation/creatives/1/translation?manual=1', 'POST']])
|
|
528
|
+
expect(row.querySelector('h1').textContent).toBe('번역 제목')
|
|
529
|
+
expect(state.retry).toBe(false)
|
|
530
|
+
})
|
|
531
|
+
|
|
532
|
+
test('showing translations while polling retries a failed result on the next poll', async () => {
|
|
533
|
+
fetchMock.mockResolvedValueOnce(response('pending')).mockResolvedValueOnce(response('processing'))
|
|
534
|
+
intersection([{ target: row, isIntersecting: true }])
|
|
535
|
+
await tick()
|
|
536
|
+
const state = controller.rows.get(row)
|
|
537
|
+
expect(state.timer).toBeTruthy()
|
|
538
|
+
controller.menuButton.click()
|
|
539
|
+
controller.menuButton.click()
|
|
540
|
+
fetchMock.mockReset()
|
|
541
|
+
fetchMock.mockResolvedValueOnce(response('failed')).mockResolvedValueOnce(response('completed', pairs))
|
|
542
|
+
clearTimeout(state.timer)
|
|
543
|
+
await controller.load(row, false)
|
|
544
|
+
expect(fetchMock.mock.calls.map(call => call[1].method)).toEqual(['GET', 'POST'])
|
|
545
|
+
expect(row.querySelector('h1').textContent).toBe('번역 제목')
|
|
546
|
+
})
|
|
547
|
+
|
|
548
|
+
test('a retry requested mid-flight is dropped once the reader returns to the original', async () => {
|
|
549
|
+
let resolve
|
|
550
|
+
fetchMock.mockImplementationOnce(() => new Promise(done => { resolve = done }))
|
|
551
|
+
intersection([{ target: row, isIntersecting: true }])
|
|
552
|
+
controller.menuButton.click()
|
|
553
|
+
controller.menuButton.click()
|
|
554
|
+
controller.menuButton.click()
|
|
555
|
+
resolve({ ok: false })
|
|
556
|
+
await tick()
|
|
557
|
+
expect(fetchMock).toHaveBeenCalledTimes(1)
|
|
558
|
+
expect(row.querySelector('h1').textContent).toBe('English title')
|
|
559
|
+
})
|
|
560
|
+
|
|
561
|
+
test('policy-rejected automatic loads offer a manual translation on the first click', async () => {
|
|
562
|
+
fetchMock.mockResolvedValueOnce({ ok: false, status: 403 })
|
|
563
|
+
intersection([{ target: row, isIntersecting: true }])
|
|
564
|
+
await tick()
|
|
565
|
+
expect(controller.menuButton.textContent).toBe('Show translation')
|
|
566
|
+
expect(controller.menuButton.getAttribute('aria-pressed')).toBe('false')
|
|
567
|
+
fetchMock.mockResolvedValueOnce(response('missing')).mockResolvedValueOnce(response('completed', pairs))
|
|
568
|
+
controller.menuButton.click()
|
|
569
|
+
await tick()
|
|
570
|
+
expect(fetchMock.mock.calls.slice(1).map(call => [call[0], call[1].method])).toEqual([
|
|
571
|
+
['/translation/creatives/1/translation?manual=1', 'GET'], ['/translation/creatives/1/translation?manual=1', 'POST']])
|
|
572
|
+
expect(row.querySelector('h1').textContent).toBe('번역 제목')
|
|
573
|
+
expect(controller.menuButton.textContent).toBe('Show original')
|
|
574
|
+
})
|
|
575
|
+
|
|
576
|
+
test('manual policy denial preserves translations and allows restoring originals', async () => {
|
|
577
|
+
fetchMock.mockResolvedValueOnce(response('completed', pairs))
|
|
578
|
+
intersection([{ target: row, isIntersecting: true }])
|
|
579
|
+
await tick()
|
|
580
|
+
const link = addTreeLink()
|
|
581
|
+
fetchMock.mockResolvedValueOnce({ ok: false, status: 403 })
|
|
582
|
+
intersection([{ target: link, isIntersecting: true }])
|
|
583
|
+
await tick()
|
|
584
|
+
expect(controller.menuButton.textContent).toBe('Show translation')
|
|
585
|
+
fetchMock.mockResolvedValueOnce({ ok: false, status: 403 })
|
|
586
|
+
controller.menuButton.click()
|
|
587
|
+
await tick()
|
|
588
|
+
expect(row.querySelector('h1').textContent).toBe('번역 제목')
|
|
589
|
+
expect(controller.menuButton.textContent).toBe('Show original')
|
|
590
|
+
controller.menuButton.click()
|
|
591
|
+
expect(row.querySelector('h1').textContent).toBe('English title')
|
|
592
|
+
controller.menuButton.click()
|
|
593
|
+
await tick()
|
|
594
|
+
expect(row.querySelector('h1').textContent).toBe('번역 제목')
|
|
595
|
+
expect(fetchMock).toHaveBeenLastCalledWith('/translation/creatives/1/translation?embed=0&manual=1', expect.anything())
|
|
596
|
+
link.remove()
|
|
597
|
+
controller.scan()
|
|
598
|
+
expect(controller.menuButton.textContent).toBe('Show original')
|
|
599
|
+
})
|
|
600
|
+
|
|
601
|
+
|
|
602
|
+
test('loads can finish before the overflow menu mounts', async () => {
|
|
603
|
+
document.getElementById('creative-overflow-menu').remove()
|
|
604
|
+
controller.menuButton = undefined
|
|
605
|
+
fetchMock.mockResolvedValueOnce(response('completed', pairs))
|
|
606
|
+
await controller.load(row)
|
|
607
|
+
expect(row.querySelector('h1').textContent).toBe('번역 제목')
|
|
608
|
+
document.body.insertAdjacentHTML('beforeend', '<div id="creative-overflow-menu"></div>')
|
|
609
|
+
controller.scan()
|
|
610
|
+
expect(controller.menuButton.textContent).toBe('Show original')
|
|
611
|
+
})
|
|
612
|
+
|
|
613
|
+
test.each(['network', 'GET 500', 'POST 503'])('manual %s failure offers a one-click retry', async failure => {
|
|
614
|
+
controller.showTranslated = false
|
|
615
|
+
controller.autoValue = false
|
|
616
|
+
controller.updateMenu()
|
|
617
|
+
intersection([{ target: row, isIntersecting: true }])
|
|
618
|
+
if (failure === 'network') fetchMock.mockRejectedValueOnce(new Error('offline'))
|
|
619
|
+
else {
|
|
620
|
+
if (failure === 'POST 503') fetchMock.mockResolvedValueOnce(response('missing'))
|
|
621
|
+
fetchMock.mockResolvedValueOnce({ ok: false, status: failure === 'GET 500' ? 500 : 503 })
|
|
622
|
+
}
|
|
623
|
+
controller.menuButton.click()
|
|
624
|
+
await tick()
|
|
625
|
+
expect(controller.menuButton.textContent).toBe('Show translation')
|
|
626
|
+
expect(row.querySelector('h1').textContent).toBe('English title')
|
|
627
|
+
fetchMock.mockResolvedValueOnce(response('completed', pairs))
|
|
628
|
+
controller.menuButton.click()
|
|
629
|
+
await tick()
|
|
630
|
+
expect(row.querySelector('h1').textContent).toBe('번역 제목')
|
|
631
|
+
expect(controller.menuButton.textContent).toBe('Show original')
|
|
632
|
+
})
|
|
633
|
+
|
|
634
|
+
|
|
635
|
+
test.each(['cooldown', 'poll'])('manual failed %s result preserves one-click retry without polling', async failure => {
|
|
636
|
+
controller.showTranslated = false
|
|
637
|
+
controller.autoValue = false
|
|
638
|
+
controller.updateMenu()
|
|
639
|
+
intersection([{ target: row, isIntersecting: true }])
|
|
640
|
+
fetchMock.mockResolvedValueOnce(response('failed'))
|
|
641
|
+
.mockResolvedValueOnce(response(failure === 'cooldown' ? 'failed' : 'processing'))
|
|
642
|
+
controller.menuButton.click()
|
|
643
|
+
await tick()
|
|
644
|
+
const state = controller.rows.get(row)
|
|
645
|
+
if (failure === 'poll') {
|
|
646
|
+
clearTimeout(state.timer)
|
|
647
|
+
fetchMock.mockResolvedValueOnce(response('failed'))
|
|
648
|
+
await controller.load(row, false)
|
|
649
|
+
}
|
|
650
|
+
expect(state.timer).toBeUndefined()
|
|
651
|
+
expect(controller.menuButton.textContent).toBe('Show translation')
|
|
652
|
+
expect(row.querySelector('h1').textContent).toBe('English title')
|
|
653
|
+
const calls = fetchMock.mock.calls.length
|
|
654
|
+
await tick()
|
|
655
|
+
expect(fetchMock).toHaveBeenCalledTimes(calls)
|
|
656
|
+
fetchMock.mockResolvedValueOnce(response('failed')).mockResolvedValueOnce(response('completed', pairs))
|
|
657
|
+
controller.menuButton.click()
|
|
658
|
+
await tick()
|
|
659
|
+
expect(fetchMock.mock.calls.slice(calls).map(([url, options]) => [url, options.method])).toEqual([
|
|
660
|
+
['/translation/creatives/1/translation?manual=1', 'GET'],
|
|
661
|
+
['/translation/creatives/1/translation?manual=1', 'POST']
|
|
662
|
+
])
|
|
663
|
+
expect(row.querySelector('h1').textContent).toBe('번역 제목')
|
|
664
|
+
expect(controller.menuButton.textContent).toBe('Show original')
|
|
665
|
+
controller.menuButton.click()
|
|
666
|
+
expect(row.querySelector('h1').textContent).toBe('English title')
|
|
667
|
+
})
|