|
| 1 | +import { extractSections, supportsSmartMerge, smartMergeContent } from '../utils/template-upgrader'; |
| 2 | + |
| 3 | +describe('template-upgrader', () => { |
| 4 | + describe('extractSections', () => { |
| 5 | + it('should extract marked sections', () => { |
| 6 | + const content = ` |
| 7 | +Some content before |
| 8 | +
|
| 9 | +<!-- codesyncer-section-start:header --> |
| 10 | +# Header |
| 11 | +Some header content |
| 12 | +<!-- codesyncer-section-end:header --> |
| 13 | +
|
| 14 | +User added content here |
| 15 | +
|
| 16 | +<!-- codesyncer-section-start:footer --> |
| 17 | +Footer content |
| 18 | +<!-- codesyncer-section-end:footer --> |
| 19 | +
|
| 20 | +More user content |
| 21 | +`; |
| 22 | + |
| 23 | + const sections = extractSections(content); |
| 24 | + expect(sections).toHaveLength(2); |
| 25 | + expect(sections[0].name).toBe('header'); |
| 26 | + expect(sections[0].content).toContain('# Header'); |
| 27 | + expect(sections[1].name).toBe('footer'); |
| 28 | + expect(sections[1].content).toContain('Footer content'); |
| 29 | + }); |
| 30 | + |
| 31 | + it('should return empty array for content without sections', () => { |
| 32 | + const content = ` |
| 33 | +# Regular Markdown |
| 34 | +No sections here |
| 35 | +`; |
| 36 | + const sections = extractSections(content); |
| 37 | + expect(sections).toHaveLength(0); |
| 38 | + }); |
| 39 | + }); |
| 40 | + |
| 41 | + describe('supportsSmartMerge', () => { |
| 42 | + it('should return true for content with section markers', () => { |
| 43 | + const content = '<!-- codesyncer-section-start:test -->content<!-- codesyncer-section-end:test -->'; |
| 44 | + expect(supportsSmartMerge(content)).toBe(true); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should return false for content without section markers', () => { |
| 48 | + const content = '# Just regular markdown'; |
| 49 | + expect(supportsSmartMerge(content)).toBe(false); |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + describe('smartMergeContent', () => { |
| 54 | + it('should update marked sections while preserving user content', () => { |
| 55 | + const existingContent = ` |
| 56 | +<!-- codesyncer-section-start:header --> |
| 57 | +# Old Header |
| 58 | +Old content |
| 59 | +<!-- codesyncer-section-end:header --> |
| 60 | +
|
| 61 | +## User Added Section |
| 62 | +This is content the user added themselves |
| 63 | +- Custom item 1 |
| 64 | +- Custom item 2 |
| 65 | +
|
| 66 | +<!-- codesyncer-section-start:footer --> |
| 67 | +Old footer |
| 68 | +<!-- codesyncer-section-end:footer --> |
| 69 | +
|
| 70 | +## Another User Section |
| 71 | +More user content here |
| 72 | +
|
| 73 | +<!-- codesyncer-version: 3.0.0 --> |
| 74 | +`; |
| 75 | + |
| 76 | + const newTemplateContent = ` |
| 77 | +<!-- codesyncer-section-start:header --> |
| 78 | +# New Header v3.1 |
| 79 | +Updated header content with new features |
| 80 | +<!-- codesyncer-section-end:header --> |
| 81 | +
|
| 82 | +Some template content |
| 83 | +
|
| 84 | +<!-- codesyncer-section-start:footer --> |
| 85 | +New footer with improvements |
| 86 | +<!-- codesyncer-section-end:footer --> |
| 87 | +
|
| 88 | +<!-- codesyncer-version: 3.1.0 --> |
| 89 | +`; |
| 90 | + |
| 91 | + const result = smartMergeContent(existingContent, newTemplateContent); |
| 92 | + |
| 93 | + // Should update the header section |
| 94 | + expect(result).toContain('# New Header v3.1'); |
| 95 | + expect(result).toContain('Updated header content with new features'); |
| 96 | + expect(result).not.toContain('# Old Header'); |
| 97 | + |
| 98 | + // Should preserve user content between sections |
| 99 | + expect(result).toContain('## User Added Section'); |
| 100 | + expect(result).toContain('This is content the user added themselves'); |
| 101 | + expect(result).toContain('- Custom item 1'); |
| 102 | + expect(result).toContain('- Custom item 2'); |
| 103 | + |
| 104 | + // Should update the footer section |
| 105 | + expect(result).toContain('New footer with improvements'); |
| 106 | + expect(result).not.toContain('Old footer'); |
| 107 | + |
| 108 | + // Should preserve user content after footer |
| 109 | + expect(result).toContain('## Another User Section'); |
| 110 | + expect(result).toContain('More user content here'); |
| 111 | + |
| 112 | + // Should update version |
| 113 | + expect(result).toContain('codesyncer-version: 3.1.0'); |
| 114 | + expect(result).not.toContain('codesyncer-version: 3.0.0'); |
| 115 | + }); |
| 116 | + |
| 117 | + it('should return new template if existing has no sections', () => { |
| 118 | + const existingContent = '# No sections here'; |
| 119 | + const newTemplateContent = ` |
| 120 | +<!-- codesyncer-section-start:header --> |
| 121 | +# Header |
| 122 | +<!-- codesyncer-section-end:header --> |
| 123 | +`; |
| 124 | + |
| 125 | + const result = smartMergeContent(existingContent, newTemplateContent); |
| 126 | + expect(result).toBe(newTemplateContent); |
| 127 | + }); |
| 128 | + |
| 129 | + it('should return new template if new template has no sections', () => { |
| 130 | + const existingContent = ` |
| 131 | +<!-- codesyncer-section-start:header --> |
| 132 | +# Header |
| 133 | +<!-- codesyncer-section-end:header --> |
| 134 | +`; |
| 135 | + const newTemplateContent = '# No sections here'; |
| 136 | + |
| 137 | + const result = smartMergeContent(existingContent, newTemplateContent); |
| 138 | + expect(result).toBe(newTemplateContent); |
| 139 | + }); |
| 140 | + |
| 141 | + it('should handle sections with different sizes', () => { |
| 142 | + const existingContent = ` |
| 143 | +<!-- codesyncer-section-start:short --> |
| 144 | +Short |
| 145 | +<!-- codesyncer-section-end:short --> |
| 146 | +
|
| 147 | +User content |
| 148 | +
|
| 149 | +<!-- codesyncer-section-start:long --> |
| 150 | +Long content here |
| 151 | +With multiple lines |
| 152 | +And more lines |
| 153 | +<!-- codesyncer-section-end:long --> |
| 154 | +`; |
| 155 | + |
| 156 | + const newTemplateContent = ` |
| 157 | +<!-- codesyncer-section-start:short --> |
| 158 | +Now this is a much longer section |
| 159 | +With many more lines |
| 160 | +Line 3 |
| 161 | +Line 4 |
| 162 | +Line 5 |
| 163 | +<!-- codesyncer-section-end:short --> |
| 164 | +
|
| 165 | +Template content |
| 166 | +
|
| 167 | +<!-- codesyncer-section-start:long --> |
| 168 | +Now short |
| 169 | +<!-- codesyncer-section-end:long --> |
| 170 | +`; |
| 171 | + |
| 172 | + const result = smartMergeContent(existingContent, newTemplateContent); |
| 173 | + |
| 174 | + // Short section should now be long |
| 175 | + expect(result).toContain('Now this is a much longer section'); |
| 176 | + expect(result).toContain('Line 5'); |
| 177 | + |
| 178 | + // Long section should now be short |
| 179 | + expect(result).toContain('Now short'); |
| 180 | + expect(result).not.toContain('With multiple lines'); |
| 181 | + |
| 182 | + // User content should be preserved |
| 183 | + expect(result).toContain('User content'); |
| 184 | + }); |
| 185 | + }); |
| 186 | +}); |
0 commit comments