feat(cms): import AI-draft English content from a translation memory (MITHOME-113)
src/content/translations/en.json is a reviewable hu -> en memory built from the live staging content (179 texts). Tests guard every entry: placeholders, HTML, legal markup and e-mail/URL literals must survive. fillEnglishDraft() fills only empty English values whose Hungarian text exactly matches a memory key (lists all-or-nothing) and moves the page status none -> ai-draft; it never sets "approved" or overwrites English. The 20260929_en_ai_draft migration applies it to every global and legal page (published doc + latest version), reports untranslated texts, and wraps legacy values first so it does not depend on migration order. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
32b6643fbc
commit
822646a82a
@@ -0,0 +1,95 @@
|
||||
import { fillEnglishDraft } from './english-draft'
|
||||
import { collectLocalizedPaths } from './localize-paths'
|
||||
|
||||
type Doc = Record<string, unknown>
|
||||
|
||||
const FIELDS = [
|
||||
{ name: 'title', type: 'text', localized: true },
|
||||
{ name: 'paragraphs', type: 'array', localized: true, fields: [{ name: 'value', type: 'text' }] },
|
||||
{ name: 'items', type: 'array', fields: [{ name: 'icon', type: 'text' }, { name: 'label', type: 'text', localized: true }] },
|
||||
]
|
||||
const PATHS = collectLocalizedPaths(FIELDS)
|
||||
const MEMORY = { Cím: 'Title', Egy: 'One', Kettő: 'Two', Címke: 'Label' }
|
||||
|
||||
let seq = 0
|
||||
const newId = () => `new-${++seq}`
|
||||
beforeEach(() => {
|
||||
seq = 0
|
||||
})
|
||||
|
||||
const doc = (): Doc => ({
|
||||
title: { hu: 'Cím' },
|
||||
paragraphs: { hu: [{ value: 'Egy', id: 'p1' }, { value: 'Kettő', id: 'p2' }] },
|
||||
items: [{ id: 'r1', icon: 'star', label: { hu: 'Címke' } }],
|
||||
})
|
||||
|
||||
describe('fillEnglishDraft (MITHOME-113)', () => {
|
||||
it('should fill empty English values from the memory and mark the page as AI draft', () => {
|
||||
const d = doc()
|
||||
const report = fillEnglishDraft(d, PATHS, MEMORY, newId)
|
||||
|
||||
expect(report).toEqual({ filled: 3, unmatched: [] })
|
||||
expect(d.title).toEqual({ hu: 'Cím', en: 'Title' })
|
||||
expect(d.paragraphs).toEqual({
|
||||
hu: [{ value: 'Egy', id: 'p1' }, { value: 'Kettő', id: 'p2' }],
|
||||
en: [{ value: 'One', id: 'new-1' }, { value: 'Two', id: 'new-2' }],
|
||||
})
|
||||
expect(d.items).toEqual([{ id: 'r1', icon: 'star', label: { hu: 'Címke', en: 'Label' } }])
|
||||
expect(d.enTranslationStatus).toBe('ai-draft')
|
||||
})
|
||||
|
||||
it('should leave an edited Hungarian text without English and report it', () => {
|
||||
const d = doc()
|
||||
d.title = { hu: 'Új cím' }
|
||||
const report = fillEnglishDraft(d, PATHS, MEMORY, newId)
|
||||
expect(d.title).toEqual({ hu: 'Új cím' })
|
||||
expect(report.unmatched).toEqual(['Új cím'])
|
||||
})
|
||||
|
||||
it('should never overwrite an existing English value', () => {
|
||||
const d = doc()
|
||||
d.title = { hu: 'Cím', en: 'Human title' }
|
||||
fillEnglishDraft(d, PATHS, MEMORY, newId)
|
||||
expect(d.title).toEqual({ hu: 'Cím', en: 'Human title' })
|
||||
})
|
||||
|
||||
it('should not fill a list partially', () => {
|
||||
const d = doc()
|
||||
d.paragraphs = { hu: [{ value: 'Egy', id: 'p1' }, { value: 'Ismeretlen', id: 'p2' }] }
|
||||
const report = fillEnglishDraft(d, PATHS, MEMORY, newId)
|
||||
expect(d.paragraphs).not.toHaveProperty('en')
|
||||
expect(report.unmatched).toHaveLength(1)
|
||||
})
|
||||
|
||||
it('should treat an empty English list as empty', () => {
|
||||
const d = doc()
|
||||
d.paragraphs = { hu: [{ value: 'Egy', id: 'p1' }], en: [] }
|
||||
fillEnglishDraft(d, PATHS, MEMORY, newId)
|
||||
expect((d.paragraphs as { en: unknown[] }).en).toHaveLength(1)
|
||||
})
|
||||
|
||||
it('should be idempotent', () => {
|
||||
const d = doc()
|
||||
fillEnglishDraft(d, PATHS, MEMORY, newId)
|
||||
const snapshot = JSON.stringify(d)
|
||||
expect(fillEnglishDraft(d, PATHS, MEMORY, newId)).toEqual({ filled: 0, unmatched: [] })
|
||||
expect(JSON.stringify(d)).toBe(snapshot)
|
||||
})
|
||||
|
||||
it.each(['approved', 'ai-draft'])('should keep an existing "%s" status', (status) => {
|
||||
const d = { ...doc(), enTranslationStatus: status }
|
||||
fillEnglishDraft(d, PATHS, MEMORY, newId)
|
||||
expect(d.enTranslationStatus).toBe(status)
|
||||
})
|
||||
|
||||
it('should not change the status when nothing was filled', () => {
|
||||
const d: Doc = { title: { hu: 'Ismeretlen' }, enTranslationStatus: 'none' }
|
||||
fillEnglishDraft(d, PATHS, MEMORY, newId)
|
||||
expect(d.enTranslationStatus).toBe('none')
|
||||
})
|
||||
|
||||
it('should skip empty Hungarian values and legacy (unwrapped) strings', () => {
|
||||
const d: Doc = { title: 'Cím', paragraphs: { hu: [] } }
|
||||
expect(fillEnglishDraft(d, PATHS, MEMORY, newId)).toEqual({ filled: 0, unmatched: [] })
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,54 @@
|
||||
/**
|
||||
* MITHOME-113: fills empty English values from the hu -> en translation
|
||||
* memory (src/content/translations/en.json), in place.
|
||||
*
|
||||
* Rules (design D5):
|
||||
* - only an EMPTY `en` is filled — human English edits are never touched;
|
||||
* - only when the current `hu` text is EXACTLY a memory key — a Hungarian
|
||||
* text edited after translation gets no (stale) English;
|
||||
* - a whole-array localized list is filled all-or-nothing, so a list is
|
||||
* never half English;
|
||||
* - `enTranslationStatus` goes none -> ai-draft when something was filled;
|
||||
* "approved" is never set here.
|
||||
*/
|
||||
import { applyAll, isPlainObject, type LocalizedPath } from './localize-paths'
|
||||
|
||||
type Row = { value?: unknown; id?: unknown }
|
||||
export type DraftReport = { filled: number; unmatched: string[] }
|
||||
|
||||
const isEmpty = (value: unknown) =>
|
||||
value === undefined || value === null || value === '' || (Array.isArray(value) && value.length === 0)
|
||||
|
||||
function translateList(rows: Row[], memory: Record<string, string>, newId: () => string): Row[] | undefined {
|
||||
const values = rows.map((row) => row.value)
|
||||
if (!values.every((v): v is string => typeof v === 'string' && v in memory)) return undefined
|
||||
return values.map((v) => ({ value: memory[v], id: newId() }))
|
||||
}
|
||||
|
||||
export function fillEnglishDraft(
|
||||
doc: Record<string, unknown>,
|
||||
paths: readonly LocalizedPath[],
|
||||
memory: Record<string, string>,
|
||||
newId: () => string,
|
||||
): DraftReport {
|
||||
const report: DraftReport = { filled: 0, unmatched: [] }
|
||||
|
||||
applyAll(doc, paths, (value) => {
|
||||
if (!isPlainObject(value) || !isEmpty(value.en) || isEmpty(value.hu)) return { value, changed: false }
|
||||
const hu = value.hu
|
||||
const en =
|
||||
typeof hu === 'string' ? memory[hu] : Array.isArray(hu) ? translateList(hu as Row[], memory, newId) : undefined
|
||||
if (en === undefined) {
|
||||
report.unmatched.push(typeof hu === 'string' ? hu : JSON.stringify(hu))
|
||||
return { value, changed: false }
|
||||
}
|
||||
value.en = en
|
||||
report.filled += 1
|
||||
return { value, changed: true }
|
||||
})
|
||||
|
||||
if (report.filled > 0 && (isEmpty(doc.enTranslationStatus) || doc.enTranslationStatus === 'none')) {
|
||||
doc.enTranslationStatus = 'ai-draft'
|
||||
}
|
||||
return report
|
||||
}
|
||||
@@ -44,9 +44,9 @@ export function collectLocalizedPaths(
|
||||
return paths
|
||||
}
|
||||
|
||||
type Transform = (value: unknown) => { value: unknown; changed: boolean }
|
||||
export type Transform = (value: unknown) => { value: unknown; changed: boolean }
|
||||
|
||||
function isPlainObject(value: unknown): value is Record<string, unknown> {
|
||||
export function isPlainObject(value: unknown): value is Record<string, unknown> {
|
||||
return typeof value === 'object' && value !== null && !Array.isArray(value)
|
||||
}
|
||||
|
||||
@@ -68,7 +68,8 @@ function applyAtPath(node: unknown, path: LocalizedPath, transform: Transform):
|
||||
// WHY in-place mutation instead of structuredClone: raw Mongo documents
|
||||
// carry BSON class instances (ObjectId) that a structured clone would
|
||||
// silently turn into plain objects. Callers pass freshly read documents.
|
||||
function applyAll(doc: Record<string, unknown>, paths: readonly LocalizedPath[], transform: Transform) {
|
||||
/** Applies `transform` to every value at `paths`, in place; returns the change count. */
|
||||
export function applyAll(doc: Record<string, unknown>, paths: readonly LocalizedPath[], transform: Transform) {
|
||||
return paths.reduce((sum, path) => sum + applyAtPath(doc, path, transform), 0)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user