|
| 1 | +import { buildDocx, ooxmlText, toBase64, zipEntryNames, zipEntryText } from './lib/ooxml'; |
| 2 | +import { expect, test } from './lib/l0'; |
| 3 | + |
| 4 | +declare function post(type: string, payload?: Record<string, unknown>): Promise<any>; |
| 5 | + |
| 6 | +/** |
| 7 | + * Word features a real document carries and the minimal fixture never did |
| 8 | + * (matrix section B "修订 / 页眉页脚"): tracked changes and header/footer |
| 9 | + * parts must survive open + save. |
| 10 | + */ |
| 11 | +test.describe('docx feature round trips (real editor)', () => { |
| 12 | + test.describe.configure({ timeout: 120_000 }); |
| 13 | + |
| 14 | + const roundTrip = (page: import('@playwright/test').Page, name: string, b64: string) => |
| 15 | + page.evaluate( |
| 16 | + async ({ name, b64 }) => { |
| 17 | + const bin = atob(b64); |
| 18 | + const bytes = new Uint8Array(bin.length); |
| 19 | + for (let i = 0; i < bin.length; i++) bytes[i] = bin.charCodeAt(i); |
| 20 | + await post('document:open-buffer', { fileName: name, buffer: bytes.buffer, readonly: false }); |
| 21 | + const saved = await post('document:save', {}); |
| 22 | + const out = new Uint8Array(await saved.file.arrayBuffer()); |
| 23 | + let s = ''; |
| 24 | + for (let i = 0; i < out.length; i += 0x8000) |
| 25 | + s += String.fromCharCode.apply(null, Array.from(out.subarray(i, i + 0x8000))); |
| 26 | + return btoa(s); |
| 27 | + }, |
| 28 | + { name, b64 }, |
| 29 | + ); |
| 30 | + |
| 31 | + test.beforeEach(async ({ page }) => { |
| 32 | + await page.goto('/embed-demo.html'); |
| 33 | + await expect(page.locator('#status')).toHaveText('ready', { timeout: 60_000 }); |
| 34 | + }); |
| 35 | + |
| 36 | + test('tracked changes (insertion + deletion) survive a save', async ({ page }) => { |
| 37 | + const body = |
| 38 | + '<w:p><w:r><w:t xml:space="preserve">Kept text </w:t></w:r>' + |
| 39 | + '<w:ins w:id="1" w:author="Reviewer" w:date="2026-08-16T00:00:00Z"><w:r><w:t>inserted 插入</w:t></w:r></w:ins>' + |
| 40 | + '<w:del w:id="2" w:author="Reviewer" w:date="2026-08-16T00:00:00Z"><w:r><w:delText>deleted 删除</w:delText></w:r></w:del>' + |
| 41 | + '</w:p>'; |
| 42 | + const out = new Uint8Array( |
| 43 | + Buffer.from(await roundTrip(page, 'revisions.docx', toBase64(buildDocx('', body))), 'base64'), |
| 44 | + ); |
| 45 | + const xml = zipEntryText(out, 'word/document.xml') || ''; |
| 46 | + expect(xml).toMatch(/<w:ins\b[^>]*w:author="Reviewer"/); |
| 47 | + expect(xml).toMatch(/<w:del\b[^>]*w:author="Reviewer"/); |
| 48 | + expect(xml).toContain('<w:delText'); |
| 49 | + expect(ooxmlText(xml)).toContain('inserted 插入'); |
| 50 | + expect(xml).toContain('deleted 删除'); |
| 51 | + }); |
| 52 | + |
| 53 | + test('header and footer parts survive a save', async ({ page }) => { |
| 54 | + const out = new Uint8Array( |
| 55 | + Buffer.from( |
| 56 | + await roundTrip( |
| 57 | + page, |
| 58 | + 'hf.docx', |
| 59 | + toBase64(buildDocx('body text', undefined, { headerText: 'Header 页眉', footerText: 'Footer 页脚' })), |
| 60 | + ), |
| 61 | + 'base64', |
| 62 | + ), |
| 63 | + ); |
| 64 | + const names = zipEntryNames(out); |
| 65 | + const header = names.find((n) => /^word\/header\d+\.xml$/.test(n)); |
| 66 | + const footer = names.find((n) => /^word\/footer\d+\.xml$/.test(n)); |
| 67 | + expect(header, `no header part in ${names.join(',')}`).toBeTruthy(); |
| 68 | + expect(footer, `no footer part in ${names.join(',')}`).toBeTruthy(); |
| 69 | + expect(ooxmlText(zipEntryText(out, header!) || '')).toContain('Header 页眉'); |
| 70 | + expect(ooxmlText(zipEntryText(out, footer!) || '')).toContain('Footer 页脚'); |
| 71 | + expect(ooxmlText(zipEntryText(out, 'word/document.xml') || '')).toContain('body text'); |
| 72 | + }); |
| 73 | +}); |
0 commit comments