|
1 | 1 | import { describe, it, expect, vi } from 'vitest'; |
2 | | -import { normalizeDocPartContent, handleDocPartObj, tableOfContentsHandler } from './handle-doc-part-obj.js'; |
| 2 | +import { |
| 3 | + normalizeDocPartContent, |
| 4 | + handleDocPartObj, |
| 5 | + tableOfContentsHandler, |
| 6 | + genericDocPartHandler, |
| 7 | +} from './handle-doc-part-obj.js'; |
3 | 8 |
|
4 | 9 | describe('normalizeDocPartContent', () => { |
5 | 10 | it('wraps inline bookmark nodes in paragraphs', () => { |
@@ -28,6 +33,39 @@ describe('normalizeDocPartContent', () => { |
28 | 33 | const normalized = normalizeDocPartContent(nodes); |
29 | 34 | expect(normalized).toEqual([nodes[0], { type: 'paragraph', content: [nodes[1]] }]); |
30 | 35 | }); |
| 36 | + |
| 37 | + it('wraps commentRangeStart and commentRangeEnd in paragraphs', () => { |
| 38 | + const nodes = [ |
| 39 | + { type: 'commentRangeStart', attrs: { id: '1' } }, |
| 40 | + { type: 'paragraph', content: [{ type: 'text', text: 'text' }] }, |
| 41 | + { type: 'commentRangeEnd', attrs: { id: '1' } }, |
| 42 | + ]; |
| 43 | + const normalized = normalizeDocPartContent(nodes); |
| 44 | + expect(normalized).toEqual([ |
| 45 | + { type: 'paragraph', content: [nodes[0]] }, |
| 46 | + nodes[1], |
| 47 | + { type: 'paragraph', content: [nodes[2]] }, |
| 48 | + ]); |
| 49 | + }); |
| 50 | + |
| 51 | + it('wraps permStart and permEnd in paragraphs', () => { |
| 52 | + const nodes = [ |
| 53 | + { type: 'permStart', attrs: { id: '1' } }, |
| 54 | + { type: 'paragraph', content: [{ type: 'text', text: 'protected' }] }, |
| 55 | + { type: 'permEnd', attrs: { id: '1' } }, |
| 56 | + ]; |
| 57 | + const normalized = normalizeDocPartContent(nodes); |
| 58 | + expect(normalized).toEqual([ |
| 59 | + { type: 'paragraph', content: [nodes[0]] }, |
| 60 | + nodes[1], |
| 61 | + { type: 'paragraph', content: [nodes[2]] }, |
| 62 | + ]); |
| 63 | + }); |
| 64 | + |
| 65 | + it('handles empty input', () => { |
| 66 | + expect(normalizeDocPartContent([])).toEqual([]); |
| 67 | + expect(normalizeDocPartContent()).toEqual([]); |
| 68 | + }); |
31 | 69 | }); |
32 | 70 |
|
33 | 71 | describe('handleDocPartObj', () => { |
@@ -122,6 +160,100 @@ describe('handleDocPartObj', () => { |
122 | 160 | }); |
123 | 161 | }); |
124 | 162 |
|
| 163 | +describe('genericDocPartHandler', () => { |
| 164 | + it('normalizes inline nodes in non-TOC docPartObj content', () => { |
| 165 | + const handler = vi.fn(() => [ |
| 166 | + { type: 'bookmarkStart', attrs: { name: '_GoBack' } }, |
| 167 | + { type: 'paragraph', content: [{ type: 'text', text: 'Page Numbers' }] }, |
| 168 | + { type: 'bookmarkEnd', attrs: { name: '_GoBack' } }, |
| 169 | + ]); |
| 170 | + const sdtPr = { |
| 171 | + name: 'w:sdtPr', |
| 172 | + elements: [ |
| 173 | + { name: 'w:id', attributes: { 'w:val': '100' } }, |
| 174 | + { |
| 175 | + name: 'w:docPartObj', |
| 176 | + elements: [{ name: 'w:docPartGallery', attributes: { 'w:val': 'Page Numbers (Bottom of Page)' } }], |
| 177 | + }, |
| 178 | + ], |
| 179 | + }; |
| 180 | + const contentNode = { name: 'w:sdtContent', elements: [] }; |
| 181 | + const params = { |
| 182 | + nodes: [contentNode], |
| 183 | + nodeListHandler: { handler }, |
| 184 | + extraParams: { sdtPr, docPartGalleryType: 'Page Numbers (Bottom of Page)' }, |
| 185 | + path: [], |
| 186 | + }; |
| 187 | + |
| 188 | + const result = genericDocPartHandler(params); |
| 189 | + |
| 190 | + expect(result.type).toEqual('documentPartObject'); |
| 191 | + expect(result.content).toEqual([ |
| 192 | + { type: 'paragraph', content: [{ type: 'bookmarkStart', attrs: { name: '_GoBack' } }] }, |
| 193 | + { type: 'paragraph', content: [{ type: 'text', text: 'Page Numbers' }] }, |
| 194 | + { type: 'paragraph', content: [{ type: 'bookmarkEnd', attrs: { name: '_GoBack' } }] }, |
| 195 | + ]); |
| 196 | + }); |
| 197 | + |
| 198 | + it('normalizes commentRangeStart/End in non-TOC docPartObj content', () => { |
| 199 | + const handler = vi.fn(() => [ |
| 200 | + { type: 'commentRangeStart', attrs: { id: '5' } }, |
| 201 | + { type: 'paragraph', content: [{ type: 'text', text: 'Bibliography' }] }, |
| 202 | + { type: 'commentRangeEnd', attrs: { id: '5' } }, |
| 203 | + ]); |
| 204 | + const sdtPr = { |
| 205 | + name: 'w:sdtPr', |
| 206 | + elements: [ |
| 207 | + { name: 'w:id', attributes: { 'w:val': '200' } }, |
| 208 | + { |
| 209 | + name: 'w:docPartObj', |
| 210 | + elements: [{ name: 'w:docPartGallery', attributes: { 'w:val': 'Bibliographies' } }], |
| 211 | + }, |
| 212 | + ], |
| 213 | + }; |
| 214 | + const contentNode = { name: 'w:sdtContent', elements: [] }; |
| 215 | + const params = { |
| 216 | + nodes: [contentNode], |
| 217 | + nodeListHandler: { handler }, |
| 218 | + extraParams: { sdtPr, docPartGalleryType: 'Bibliographies' }, |
| 219 | + path: [], |
| 220 | + }; |
| 221 | + |
| 222 | + const result = genericDocPartHandler(params); |
| 223 | + |
| 224 | + expect(result.content).toEqual([ |
| 225 | + { type: 'paragraph', content: [{ type: 'commentRangeStart', attrs: { id: '5' } }] }, |
| 226 | + { type: 'paragraph', content: [{ type: 'text', text: 'Bibliography' }] }, |
| 227 | + { type: 'paragraph', content: [{ type: 'commentRangeEnd', attrs: { id: '5' } }] }, |
| 228 | + ]); |
| 229 | + }); |
| 230 | + |
| 231 | + it('leaves block-only content unchanged', () => { |
| 232 | + const handler = vi.fn(() => [{ type: 'paragraph', content: [{ type: 'text', text: 'Cover Page' }] }]); |
| 233 | + const sdtPr = { |
| 234 | + name: 'w:sdtPr', |
| 235 | + elements: [ |
| 236 | + { name: 'w:id', attributes: { 'w:val': '300' } }, |
| 237 | + { |
| 238 | + name: 'w:docPartObj', |
| 239 | + elements: [{ name: 'w:docPartGallery', attributes: { 'w:val': 'Cover Pages' } }], |
| 240 | + }, |
| 241 | + ], |
| 242 | + }; |
| 243 | + const contentNode = { name: 'w:sdtContent', elements: [] }; |
| 244 | + const params = { |
| 245 | + nodes: [contentNode], |
| 246 | + nodeListHandler: { handler }, |
| 247 | + extraParams: { sdtPr, docPartGalleryType: 'Cover Pages' }, |
| 248 | + path: [], |
| 249 | + }; |
| 250 | + |
| 251 | + const result = genericDocPartHandler(params); |
| 252 | + |
| 253 | + expect(result.content).toEqual([{ type: 'paragraph', content: [{ type: 'text', text: 'Cover Page' }] }]); |
| 254 | + }); |
| 255 | +}); |
| 256 | + |
125 | 257 | describe('tableOfContentsHandler', () => { |
126 | 258 | const mockNodeListHandler = { |
127 | 259 | handler: vi.fn(() => [{ type: 'paragraph', content: [{ type: 'text', text: 'TOC Content' }] }]), |
|
0 commit comments