|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * Round-trip i18n for import coercion: the localized export and the import |
| 5 | + * template surface *translated* option labels (e.g. 待规划 for `backlog`), so |
| 6 | + * `prepareImportRequest` folds those labels into the field metaMap as matching |
| 7 | + * synonyms — while the authored label and the option code keep working. |
| 8 | + */ |
| 9 | + |
| 10 | +import { describe, it, expect } from 'vitest'; |
| 11 | +import { prepareImportRequest, mergeLocalizedOptionSynonyms } from './import-prepare'; |
| 12 | +import { matchOption } from './import-coerce'; |
| 13 | +import { buildFieldMetaMap } from './export-format'; |
| 14 | + |
| 15 | +const SCHEMA = { |
| 16 | + name: 'task', |
| 17 | + fields: { |
| 18 | + title: { name: 'title', type: 'text', label: 'Title' }, |
| 19 | + status: { |
| 20 | + name: 'status', type: 'select', label: 'Status', |
| 21 | + options: [ |
| 22 | + { label: 'Backlog', value: 'backlog' }, |
| 23 | + { label: 'Done', value: 'done' }, |
| 24 | + ], |
| 25 | + }, |
| 26 | + }, |
| 27 | +}; |
| 28 | + |
| 29 | +/** What `translateMetaItem` returns for SCHEMA under a zh-CN request. */ |
| 30 | +const localizeSchema = (schema: any) => ({ |
| 31 | + ...schema, |
| 32 | + fields: { |
| 33 | + ...schema.fields, |
| 34 | + status: { |
| 35 | + ...schema.fields.status, |
| 36 | + label: '状态', |
| 37 | + options: [ |
| 38 | + { label: '待规划', value: 'backlog' }, |
| 39 | + { label: '已完成', value: 'done' }, |
| 40 | + ], |
| 41 | + }, |
| 42 | + }, |
| 43 | +}); |
| 44 | + |
| 45 | +const p = { getMetaItem: async () => ({ type: 'object', name: 'task', item: SCHEMA }) }; |
| 46 | + |
| 47 | +describe('mergeLocalizedOptionSynonyms', () => { |
| 48 | + it('appends translated labels as extra options without touching authored ones', () => { |
| 49 | + const metaMap = buildFieldMetaMap(SCHEMA); |
| 50 | + mergeLocalizedOptionSynonyms(metaMap, buildFieldMetaMap(localizeSchema(SCHEMA))); |
| 51 | + const options = metaMap.get('status')!.options!; |
| 52 | + expect(matchOption('待规划', options)).toBe('backlog'); |
| 53 | + expect(matchOption('Backlog', options)).toBe('backlog'); |
| 54 | + expect(matchOption('backlog', options)).toBe('backlog'); |
| 55 | + expect(matchOption('已完成', options)).toBe('done'); |
| 56 | + }); |
| 57 | + |
| 58 | + it('is a no-op when the locale leaves labels unchanged (en request)', () => { |
| 59 | + const metaMap = buildFieldMetaMap(SCHEMA); |
| 60 | + const before = metaMap.get('status')!.options!.length; |
| 61 | + mergeLocalizedOptionSynonyms(metaMap, buildFieldMetaMap(SCHEMA)); |
| 62 | + expect(metaMap.get('status')!.options!.length).toBe(before); |
| 63 | + }); |
| 64 | +}); |
| 65 | + |
| 66 | +describe('prepareImportRequest — locale-translated option synonyms', () => { |
| 67 | + it('folds the localizeSchema labels into the prepared metaMap', async () => { |
| 68 | + const prep = await prepareImportRequest( |
| 69 | + { format: 'json', rows: [{ title: 'a', status: '待规划' }] }, |
| 70 | + { p, objectName: 'task', maxRows: 10, localizeSchema }, |
| 71 | + ); |
| 72 | + expect(prep.ok).toBe(true); |
| 73 | + if (!prep.ok) return; |
| 74 | + const options = prep.prepared.metaMap.get('status')!.options!; |
| 75 | + expect(matchOption('待规划', options)).toBe('backlog'); |
| 76 | + expect(matchOption('Backlog', options)).toBe('backlog'); |
| 77 | + }); |
| 78 | + |
| 79 | + it('without localizeSchema the authored-only matching is unchanged', async () => { |
| 80 | + const prep = await prepareImportRequest( |
| 81 | + { format: 'json', rows: [{ title: 'a', status: 'Backlog' }] }, |
| 82 | + { p, objectName: 'task', maxRows: 10 }, |
| 83 | + ); |
| 84 | + expect(prep.ok).toBe(true); |
| 85 | + if (!prep.ok) return; |
| 86 | + const options = prep.prepared.metaMap.get('status')!.options!; |
| 87 | + expect(options).toHaveLength(2); |
| 88 | + // The translated label is unknown to the authored options → no match. |
| 89 | + expect(matchOption('待规划', options)).toBeUndefined(); |
| 90 | + }); |
| 91 | + |
| 92 | + it('a throwing localizeSchema degrades to authored-only matching', async () => { |
| 93 | + const prep = await prepareImportRequest( |
| 94 | + { format: 'json', rows: [{ title: 'a', status: 'Backlog' }] }, |
| 95 | + { p, objectName: 'task', maxRows: 10, localizeSchema: () => { throw new Error('no i18n'); } }, |
| 96 | + ); |
| 97 | + expect(prep.ok).toBe(true); |
| 98 | + if (!prep.ok) return; |
| 99 | + expect(matchOption('Backlog', prep.prepared.metaMap.get('status')!.options!)).toBe('backlog'); |
| 100 | + }); |
| 101 | +}); |
0 commit comments