|
| 1 | +import { |
| 2 | + BaseProcessor, |
| 3 | + type ExtractStringsResult, |
| 4 | + type TranslatedString, |
| 5 | + type SourceString, |
| 6 | + type ProcessorOptions, |
| 7 | +} from '../../src/core/baseProcessor'; |
| 8 | +import { |
| 9 | + AACTree, |
| 10 | + AACPage, |
| 11 | + AACButton, |
| 12 | + AACSemanticCategory, |
| 13 | + AACSemanticIntent, |
| 14 | +} from '../../src/core/treeStructure'; |
| 15 | + |
| 16 | +class DummyProcessor extends BaseProcessor { |
| 17 | + private tree: AACTree; |
| 18 | + public lastTranslations: Map<string, string> | null = null; |
| 19 | + public lastOutputPath: string | null = null; |
| 20 | + |
| 21 | + constructor(tree: AACTree, options?: ProcessorOptions) { |
| 22 | + super(options); |
| 23 | + this.tree = tree; |
| 24 | + } |
| 25 | + |
| 26 | + async extractTexts(): Promise<string[]> { |
| 27 | + return []; |
| 28 | + } |
| 29 | + |
| 30 | + async loadIntoTree(): Promise<AACTree> { |
| 31 | + return this.tree; |
| 32 | + } |
| 33 | + |
| 34 | + async processTexts( |
| 35 | + _filePathOrBuffer: string, |
| 36 | + translations: Map<string, string>, |
| 37 | + outputPath: string |
| 38 | + ): Promise<Uint8Array> { |
| 39 | + this.lastTranslations = translations; |
| 40 | + this.lastOutputPath = outputPath; |
| 41 | + return new Uint8Array(); |
| 42 | + } |
| 43 | + |
| 44 | + async saveFromTree(): Promise<void> { |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + public filterButtons(buttons: AACButton[]): AACButton[] { |
| 49 | + return this.filterPageButtons(buttons); |
| 50 | + } |
| 51 | + |
| 52 | + public extractStringsGeneric(filePath: string): Promise<ExtractStringsResult> { |
| 53 | + return this.extractStringsWithMetadataGeneric(filePath); |
| 54 | + } |
| 55 | + |
| 56 | + public generateTranslatedGeneric( |
| 57 | + filePath: string, |
| 58 | + translatedStrings: TranslatedString[], |
| 59 | + sourceStrings: SourceString[] |
| 60 | + ): Promise<string> { |
| 61 | + return this.generateTranslatedDownloadGeneric(filePath, translatedStrings, sourceStrings); |
| 62 | + } |
| 63 | + |
| 64 | + public outputPathFor(filePath: string): string { |
| 65 | + return this.generateTranslatedOutputPath(filePath); |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +function createTree(): AACTree { |
| 70 | + const tree = new AACTree(); |
| 71 | + const page = new AACPage({ id: 'page-1', name: 'Home' }); |
| 72 | + const yesButton = new AACButton({ id: 'btn-1', label: 'Yes', message: 'Yes' }); |
| 73 | + const noButton = new AACButton({ id: 'btn-2', label: 'No', message: 'Nope' }); |
| 74 | + page.buttons.push(yesButton, noButton); |
| 75 | + tree.addPage(page); |
| 76 | + tree.rootId = page.id; |
| 77 | + return tree; |
| 78 | +} |
| 79 | + |
| 80 | +describe('BaseProcessor generic helpers', () => { |
| 81 | + it('filters navigation/system buttons by default', () => { |
| 82 | + const tree = createTree(); |
| 83 | + const processor = new DummyProcessor(tree); |
| 84 | + const buttons = [ |
| 85 | + new AACButton({ |
| 86 | + id: 'nav', |
| 87 | + label: 'Back', |
| 88 | + message: '', |
| 89 | + semanticAction: { |
| 90 | + category: AACSemanticCategory.NAVIGATION, |
| 91 | + intent: AACSemanticIntent.GO_BACK, |
| 92 | + }, |
| 93 | + }), |
| 94 | + new AACButton({ |
| 95 | + id: 'sys', |
| 96 | + label: 'Clear', |
| 97 | + message: '', |
| 98 | + semanticAction: { |
| 99 | + category: AACSemanticCategory.TEXT_EDITING, |
| 100 | + intent: AACSemanticIntent.CLEAR_TEXT, |
| 101 | + }, |
| 102 | + }), |
| 103 | + new AACButton({ id: 'keep', label: 'Hello', message: 'Hello' }), |
| 104 | + ]; |
| 105 | + |
| 106 | + const filtered = processor.filterButtons(buttons); |
| 107 | + expect(filtered.map((b) => b.id)).toEqual(['keep']); |
| 108 | + }); |
| 109 | + |
| 110 | + it('preserves all buttons when preserveAllButtons is set', () => { |
| 111 | + const tree = createTree(); |
| 112 | + const processor = new DummyProcessor(tree, { preserveAllButtons: true }); |
| 113 | + const buttons = [ |
| 114 | + new AACButton({ |
| 115 | + id: 'nav', |
| 116 | + label: 'Back', |
| 117 | + message: '', |
| 118 | + semanticAction: { |
| 119 | + category: AACSemanticCategory.NAVIGATION, |
| 120 | + intent: AACSemanticIntent.GO_BACK, |
| 121 | + }, |
| 122 | + }), |
| 123 | + ]; |
| 124 | + |
| 125 | + expect(processor.filterButtons(buttons).length).toBe(1); |
| 126 | + }); |
| 127 | + |
| 128 | + it('applies a custom button filter', () => { |
| 129 | + const tree = createTree(); |
| 130 | + const processor = new DummyProcessor(tree, { |
| 131 | + customButtonFilter: (button) => !button.label?.toLowerCase().includes('skip'), |
| 132 | + }); |
| 133 | + const buttons = [ |
| 134 | + new AACButton({ id: 'skip', label: 'Skip Me', message: '' }), |
| 135 | + new AACButton({ id: 'keep', label: 'Keep', message: '' }), |
| 136 | + ]; |
| 137 | + |
| 138 | + expect(processor.filterButtons(buttons).map((b) => b.id)).toEqual(['keep']); |
| 139 | + }); |
| 140 | + |
| 141 | + it('extracts strings with metadata and deduplicates', async () => { |
| 142 | + const tree = createTree(); |
| 143 | + const processor = new DummyProcessor(tree); |
| 144 | + const result = await processor.extractStringsGeneric('dummy.path'); |
| 145 | + |
| 146 | + const labels = result.extractedStrings.map((entry) => entry.string).sort(); |
| 147 | + expect(labels).toEqual(['Home', 'No', 'Nope', 'Yes']); |
| 148 | + |
| 149 | + const yesEntry = result.extractedStrings.find((entry) => entry.string === 'Yes'); |
| 150 | + expect(yesEntry?.vocabPlacementMeta.vocabLocations.length).toBe(1); |
| 151 | + }); |
| 152 | + |
| 153 | + it('builds translations and output paths for generic downloads', async () => { |
| 154 | + const tree = createTree(); |
| 155 | + const processor = new DummyProcessor(tree); |
| 156 | + const sourceStrings: SourceString[] = [ |
| 157 | + { id: 1, sourcestring: 'Hello', vocabplacementmetadata: { vocabLocations: [] } }, |
| 158 | + ]; |
| 159 | + const translatedStrings: TranslatedString[] = [ |
| 160 | + { sourcestringid: 1, overridestring: 'Hola', translatedstring: 'Bonjour' }, |
| 161 | + ]; |
| 162 | + |
| 163 | + const outputPath = await processor.generateTranslatedGeneric( |
| 164 | + '/tmp/example.obf', |
| 165 | + translatedStrings, |
| 166 | + sourceStrings |
| 167 | + ); |
| 168 | + |
| 169 | + expect(outputPath).toBe('/tmp/example_translated.obf'); |
| 170 | + expect(processor.lastTranslations?.get('Hello')).toBe('Hola'); |
| 171 | + }); |
| 172 | + |
| 173 | + it('generates translated output paths without extensions', () => { |
| 174 | + const tree = createTree(); |
| 175 | + const processor = new DummyProcessor(tree); |
| 176 | + expect(processor.outputPathFor('/tmp/example')).toBe('/tmp/example_translated'); |
| 177 | + }); |
| 178 | +}); |
0 commit comments