|
| 1 | +import { buildPptx, toBase64, zipEntryNames } from './lib/ooxml'; |
| 2 | +import { expect, test } from './lib/l0'; |
| 3 | + |
| 4 | +declare const XLSX: any; |
| 5 | +declare function post(type: string, payload?: Record<string, unknown>): Promise<any>; |
| 6 | + |
| 7 | +/** |
| 8 | + * Image insertion + save for the spreadsheet and presentation editors |
| 9 | + * (matrix section A "插图后保存"; the word editor case lives in |
| 10 | + * embed-regression). The serverless image pipeline (prepareEditorIframe |
| 11 | + * guard 4) must register the image and the saved package must carry the |
| 12 | + * media bytes -- the historical failure was a permanent main-thread hang. |
| 13 | + */ |
| 14 | +test.describe('image insert + save: xlsx / pptx (real editor)', () => { |
| 15 | + test.describe.configure({ timeout: 150_000 }); |
| 16 | + |
| 17 | + test.beforeEach(async ({ page }) => { |
| 18 | + await page.goto('/embed-demo.html'); |
| 19 | + await expect(page.locator('#status')).toHaveText('ready', { timeout: 60_000 }); |
| 20 | + }); |
| 21 | + |
| 22 | + // Installed into the page once per test: insert /img/64.png through the |
| 23 | + // editor API and save; returns the saved bytes as base64. |
| 24 | + const installHelper = (page: import('@playwright/test').Page) => |
| 25 | + page.evaluate(() => { |
| 26 | + (window as any).__insertAndSave = async (kind: string) => { |
| 27 | + const findApi = (): any => { |
| 28 | + const visit = (win: Window): any => { |
| 29 | + try { |
| 30 | + const a = (win as any).Asc?.editor; |
| 31 | + if (a && a.isDocumentLoadComplete && a.isLoadFullApi) return a; |
| 32 | + } catch { |
| 33 | + /* cross-origin */ |
| 34 | + } |
| 35 | + for (let i = 0; i < win.frames.length; i++) { |
| 36 | + const f = visit(win.frames[i]); |
| 37 | + if (f) return f; |
| 38 | + } |
| 39 | + return null; |
| 40 | + }; |
| 41 | + return visit(window); |
| 42 | + }; |
| 43 | + const start = Date.now(); |
| 44 | + let api = findApi(); |
| 45 | + while (!api && Date.now() - start < 60_000) { |
| 46 | + await new Promise((r) => setTimeout(r, 300)); |
| 47 | + api = findApi(); |
| 48 | + } |
| 49 | + if (!api) return { error: 'no api' }; |
| 50 | + const url = location.origin + '/img/64.png'; |
| 51 | + if (kind === 'pptx') api.AddImageUrlAction(url); |
| 52 | + else api.asc_addImageDrawingObject([url]); |
| 53 | + await new Promise((r) => setTimeout(r, 6000)); |
| 54 | + const saved = await post('document:save', {}); |
| 55 | + const out = new Uint8Array(await saved.file.arrayBuffer()); |
| 56 | + let s = ''; |
| 57 | + for (let i = 0; i < out.length; i += 0x8000) |
| 58 | + s += String.fromCharCode.apply(null, Array.from(out.subarray(i, i + 0x8000))); |
| 59 | + return { name: saved.file.name, b64: btoa(s) }; |
| 60 | + }; |
| 61 | + }); |
| 62 | + |
| 63 | + test.beforeEach(async ({ page }) => { |
| 64 | + await installHelper(page); |
| 65 | + }); |
| 66 | + |
| 67 | + test('xlsx: an image inserted by URL is in the saved workbook', async ({ page }) => { |
| 68 | + const result = await page.evaluate(async () => { |
| 69 | + const wb = XLSX.utils.book_new(); |
| 70 | + XLSX.utils.book_append_sheet(wb, XLSX.utils.aoa_to_sheet([['img', 1]]), 'S'); |
| 71 | + const data = XLSX.write(wb, { bookType: 'xlsx', type: 'array' }); |
| 72 | + await post('document:open-buffer', { |
| 73 | + fileName: 'img.xlsx', |
| 74 | + buffer: new Uint8Array(data).buffer, |
| 75 | + readonly: false, |
| 76 | + }); |
| 77 | + return (window as any).__insertAndSave('xlsx'); |
| 78 | + }); |
| 79 | + expect(result.error).toBeUndefined(); |
| 80 | + const names = zipEntryNames(new Uint8Array(Buffer.from(result.b64, 'base64'))); |
| 81 | + expect(names.some((n) => /^xl\/media\//.test(n))).toBe(true); |
| 82 | + expect(names.some((n) => /^xl\/drawings\/drawing\d+\.xml$/.test(n))).toBe(true); |
| 83 | + }); |
| 84 | + |
| 85 | + test('pptx: an image inserted by URL is in the saved deck', async ({ page }) => { |
| 86 | + const result = await page.evaluate( |
| 87 | + async ({ pptxB64 }) => { |
| 88 | + const bin = atob(pptxB64); |
| 89 | + const bytes = new Uint8Array(bin.length); |
| 90 | + for (let i = 0; i < bin.length; i++) bytes[i] = bin.charCodeAt(i); |
| 91 | + await post('document:open-buffer', { fileName: 'img.pptx', buffer: bytes.buffer, readonly: false }); |
| 92 | + return (window as any).__insertAndSave('pptx'); |
| 93 | + }, |
| 94 | + { pptxB64: toBase64(buildPptx('image slide')) }, |
| 95 | + ); |
| 96 | + expect(result.error).toBeUndefined(); |
| 97 | + const names = zipEntryNames(new Uint8Array(Buffer.from(result.b64, 'base64'))); |
| 98 | + expect(names.some((n) => /^ppt\/media\//.test(n))).toBe(true); |
| 99 | + }); |
| 100 | +}); |
0 commit comments