forked from deepseek-ai/deepseek-harness
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranslation-brief.spec.ts
More file actions
287 lines (258 loc) · 11 KB
/
Copy pathtranslation-brief.spec.ts
File metadata and controls
287 lines (258 loc) · 11 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
/** Regression tests for the minimal-update briefing assembly. */
import { describe, expect, it } from 'vitest'
import {
changedSpanIndices,
computeMechanicalUpdate,
firstOccurrenceContext,
markdownUnits,
parseTerminologyRows,
relevantTerminologyRows,
renderTranslationBrief,
sectionSpans,
spansAligned,
termOffsets,
} from './translation-brief.ts'
const DOC = [
'Preamble line.',
'',
'# Title',
'',
'Intro paragraph.',
'',
'## First',
'',
'First body.',
'',
'```ts',
'const value = 1',
'```',
'',
'## Second',
'',
'| A | B |',
'|---|---|',
'| 1 | 2 |',
'',
'- item one',
'- item two',
].join('\n')
describe('markdown spans', () => {
it('lists units with container-scoped kinds in document order', () => {
const kinds = markdownUnits(DOC).map(span => span.kind)
expect(kinds).toEqual([
'root.0:paragraph',
'root.1:heading:1',
'root.2:paragraph',
'root.3:heading:2',
'root.4:paragraph',
'root.5:code',
'root.6:heading:2',
'root.7.0:tableRow',
'root.7.1:tableRow',
'root.8.0:listItem',
'root.8.1:listItem',
])
})
it('lists heading sections with a preamble span and heading labels', () => {
const sections = sectionSpans(DOC)
expect(sections.map(span => span.label)).toEqual([
'(preamble before the first heading)',
'Title',
'First',
'Second',
])
expect(sections[0]).toMatchObject({ startLine: 1, endLine: 2 })
expect(sections[2]).toMatchObject({ startLine: 7, endLine: 14 })
})
it('labels units by their node type', () => {
const units = markdownUnits(DOC)
expect(units[0]!.label).toBe('paragraph')
expect(units[1]!.label).toBe('heading')
expect(units[7]!.label).toBe('tableRow')
})
it('aligns sections by depth only, so translated heading text still maps', () => {
const zh = DOC.replace('## First', '## 第一节').replace('## Second', '## 第二节').replace('# Title', '# 标题')
expect(spansAligned(sectionSpans(DOC), sectionSpans(zh))).toBe(true)
})
it('aligns span lists only on equal non-empty kind sequences', () => {
const zh = DOC.replace('First body.', '第一段。').replace('item one', '第一项').replace('Intro paragraph.', '导语。')
expect(spansAligned(markdownUnits(DOC), markdownUnits(zh))).toBe(true)
const reshaped = DOC.replace('- item one\n- item two', 'merged paragraph')
expect(spansAligned(markdownUnits(DOC), markdownUnits(reshaped))).toBe(false)
expect(spansAligned([], [])).toBe(false)
})
it('reports the indices whose text changed', () => {
const edited = DOC.replace('First body.', 'First body, revised.').replace('| 1 | 2 |', '| 1 | 3 |')
expect(changedSpanIndices(markdownUnits(DOC), markdownUnits(edited))).toEqual([4, 8])
})
})
describe('mechanical code updates', () => {
const en = '# T\n\nProse.\n\n```sh\nrun one\n```\n'
const zh = '# T\n\n中文。\n\n```sh\nrun one\n```\n'
it('splices a fence-only edit into the counterpart', () => {
const edited = en.replace('run one', 'run two')
expect(computeMechanicalUpdate(en, edited, zh)).toBe(zh.replace('run one', 'run two'))
})
it('refuses when prose changed too', () => {
const edited = en.replace('Prose.', 'Prose!').replace('run one', 'run two')
expect(computeMechanicalUpdate(en, edited, zh)).toBeUndefined()
})
it('refuses when the counterpart fences already diverge from last-confirmed', () => {
const edited = en.replace('run one', 'run two')
expect(computeMechanicalUpdate(en, edited, zh.replace('run one', 'run stale'))).toBeUndefined()
})
it('refuses when fence counts differ or nothing changed', () => {
expect(computeMechanicalUpdate(en, `${en}\n\`\`\`sh\nextra\n\`\`\`\n`, zh)).toBeUndefined()
expect(computeMechanicalUpdate(en, en, zh)).toBeUndefined()
})
})
const TERMINOLOGY = [
'| English | 中文 | 首次出现 | 不要译作 | 备注 |',
'|---|---|---|---|---|',
'| agent | agent | agent(智能体) | 智能体 | |',
'| session log | 会话日志 | | 会话记录 | |',
'| gate | 门禁 | | | |',
'| registry | 注册表 | | | |',
].join('\n')
describe('terminology', () => {
it('parses data rows and skips the header and separator', () => {
const rows = parseTerminologyRows(TERMINOLOGY)
expect(rows.map(row => row.english)).toEqual(['agent', 'session log', 'gate', 'registry'])
expect(rows[0]).toMatchObject({ chinese: 'agent', first: 'agent(智能体)' })
})
it('matches English terms on word boundaries with plural inflections', () => {
expect(termOffsets('two agents met', 'agent', true)).toEqual([4])
expect(termOffsets('two registries', 'registry', true)).toEqual([4])
expect(termOffsets('reagents', 'agent', true)).toEqual([])
expect(termOffsets('', 'agent', true)).toEqual([])
})
it('selects rows for the changed text per direction', () => {
expect(relevantTerminologyRows(TERMINOLOGY, 'en-to-zh', 'All agents write a session log.').map(row => row.english))
.toEqual(['agent', 'session log'])
expect(relevantTerminologyRows(TERMINOLOGY, 'zh-to-en', '门禁在提交时运行。').map(row => row.english))
.toEqual(['gate'])
expect(relevantTerminologyRows(TERMINOLOGY, 'en-to-zh', 'delegate the work')).toEqual([])
})
})
describe('first-occurrence tracking', () => {
const before = '# T\n\nAlpha paragraph.\n\nThe agent runs.\n'
const after = '# T\n\nAlpha paragraph with an agent.\n\nThe agent runs.\n'
const rows = parseTerminologyRows(TERMINOLOGY).filter(row => row.english === 'agent')
it('flags a moved first occurrence and pulls the vacated span in', () => {
const context = firstOccurrenceContext(
before, after, markdownUnits(before), markdownUnits(after), rows, new Set([1]),
)
expect(context.notes).toHaveLength(1)
expect(context.notes[0]).toContain('moved from #2 to #1')
expect(context.extraSpanIndices).toEqual([2])
})
it('stays silent when the first occurrence does not move', () => {
const unmoved = before.replace('Alpha paragraph.', 'Alpha paragraph, revised.')
const context = firstOccurrenceContext(
before, unmoved, markdownUnits(before), markdownUnits(unmoved), rows, new Set([1]),
)
expect(context.notes).toEqual([])
expect(context.extraSpanIndices).toEqual([])
})
it('ignores rows without a first-occurrence rendering', () => {
const bare = parseTerminologyRows(TERMINOLOGY).filter(row => row.english === 'gate')
const withGate = after.replace('The agent runs.', 'The gate runs.')
const context = firstOccurrenceContext(
before, withGate, markdownUnits(before), markdownUnits(withGate), bare, new Set([2]),
)
expect(context.notes).toEqual([])
})
})
describe('brief rendering', () => {
const base = {
sourcePath: 'docs/foo.md',
counterpartPath: 'docs/foo.zh.md',
direction: 'en-to-zh' as const,
diff: '@@ -5 +5 @@\n-old text about the agent\n+new text about the agent',
terminology: relevantTerminologyRows(TERMINOLOGY, 'en-to-zh', 'the agent'),
}
const bundle = {
index: 4,
label: 'paragraph',
confirmedSourceText: 'old text about the agent\n',
currentSourceText: 'new text about the agent\n',
counterpartText: '关于 agent 的旧文本\n',
counterpartStartLine: 9,
}
it('renders unit bundles with three-way context and line anchors', () => {
const brief = renderTranslationBrief({
...base,
scope: { kind: 'units', bundles: [bundle], firstOccurrenceNotes: ['agent: the document-wide first occurrence moved from #2 to #1; the agent(智能体) form moves with it (later occurrences drop the annotation).'] },
})
expect(brief).toContain('# Translation update briefing: docs/foo.md')
expect(brief).toContain('## Changed units')
expect(brief).toContain('### #4 paragraph — counterpart at docs/foo.zh.md:9')
expect(brief).toContain('Last-confirmed English:')
expect(brief).toContain('Current Chinese (bring this along):')
expect(brief).toContain('## First-occurrence notes')
expect(brief).toContain('agent(智能体)')
expect(brief).toContain('首次出现 annotations attach to the document-wide first occurrence only')
expect(brief).toContain('targets in the active bilingual corpus use `.zh.md` for Chinese')
expect(brief).toContain('a missing in-scope counterpart is an error')
expect(brief).toContain('verify-translation-pairing --write docs/foo.md')
})
it('marks first-occurrence bundles and omits their unchanged confirmed text', () => {
const brief = renderTranslationBrief({
...base,
scope: {
kind: 'units',
bundles: [{ ...bundle, reason: 'first-occurrence', confirmedSourceText: bundle.currentSourceText }],
firstOccurrenceNotes: [],
},
})
expect(brief).toContain('unchanged; included for a first-occurrence move')
expect(brief).not.toContain('Last-confirmed English:')
})
it('renders the mechanical scope with the --apply command', () => {
const brief = renderTranslationBrief({ ...base, scope: { kind: 'mechanical' } })
expect(brief).toContain('## Mechanical update — no translation judgment involved')
expect(brief).toContain('gen-translation-brief --apply docs/foo.md')
expect(brief).not.toContain('## Changed units')
})
it('renders the section fallback under its own heading', () => {
const brief = renderTranslationBrief({
...base,
scope: { kind: 'sections', bundles: [bundle], firstOccurrenceNotes: [] },
})
expect(brief).toContain('## Changed sections')
expect(brief).toContain('fine-grained units do not align')
})
it('renders the document fallback with its reason and no bundles', () => {
const brief = renderTranslationBrief({
...base,
scope: { kind: 'document', reason: 'BOTH sides changed since the pair was last confirmed consistent, so no side is a trustworthy mapping anchor; decide which side owns each divergence.' },
})
expect(brief).toContain('## Whole-document update required')
expect(brief).toContain('BOTH sides changed')
expect(brief).toContain('locate the affected regions yourself')
})
it('renders the English-target digest for zh-to-en updates', () => {
const brief = renderTranslationBrief({
...base,
direction: 'zh-to-en',
sourcePath: 'docs/foo.zh.md',
counterpartPath: 'docs/foo.md',
scope: { kind: 'units', bundles: [bundle], firstOccurrenceNotes: [] },
})
expect(brief).toContain('exactly what the new Chinese states')
expect(brief).toContain('targets in the active bilingual corpus use `.md` for English')
expect(brief).toContain('targets outside the corpus keep the authored path')
expect(brief).toContain('verify-translation-pairing --write docs/foo.md')
})
it('grows bundle fences past tilde runs in the text', () => {
const brief = renderTranslationBrief({
...base,
scope: {
kind: 'units',
bundles: [{ ...bundle, counterpartText: '~~~~\ninner\n~~~~\n' }],
firstOccurrenceNotes: [],
},
})
expect(brief).toContain('~~~~~markdown')
})
})