Files
order-notification-automation/tests/storage/storage-preview-ui.test.cjs
T

25 lines
5.5 KiB
JavaScript

'use strict';
const {test}=require('node:test');const assert=require('node:assert/strict');const fs=require('node:fs/promises');const os=require('node:os');const path=require('node:path');
const {Store}=require('../../src/modules/dashboard/store.cjs');const {StorageStore}=require('../../src/modules/storage/store.cjs');const {StorageService}=require('../../src/modules/storage/service.cjs');const {FileArea}=require('../../src/modules/storage/files.cjs');const {createDashboard}=require('../../src/app/dashboard.cjs');const {loadPlaywright,browserOptions}=require('../../src/platform/browser.cjs');const {makePreviewFiles}=require('../fixtures/storage-preview-files.cjs');
test('already-managed storage previews real files locally in an isolated frame',{timeout:240000},async t=>{
const dir=await fs.mkdtemp(path.join(os.tmpdir(),'storage-preview-ui-')),remote=path.join(dir,'remote');await fs.mkdir(remote);await makePreviewFiles(remote);
const store=new Store(path.join(dir,'users'));store.bootstrap('admin','Test-password-123');const storageStore=new StorageStore(path.join(dir,'storage')),storage=new StorageService({store:storageStore,jobs:store,outputDir:dir,client:{area:async()=>new FileArea(remote)}});await storage.saveServer({name:'已托管服务器',host:'storage.company.test',share:'orders',directory:'',username:'archive',password:'Fixture-storage-password',enabled:true});
const server=createDashboard({store,storage,outputDir:dir});await new Promise(r=>server.listen(0,'127.0.0.1',r));let browser;
t.after(async()=>{await browser?.close();await storage.stop();server.closeAllConnections();await new Promise(r=>server.close(r));storageStore.close();await fs.rm(dir,{recursive:true,force:true});});
browser=await loadPlaywright().chromium.launch(browserOptions());const page=await browser.newPage({viewport:{width:1440,height:1000}});page.setDefaultTimeout(15000);const errors=[],external=[],failed=[],consoleErrors=[],previewRequests=[];page.on('console',m=>{if(m.type()==='error'||m.type()==='warning')consoleErrors.push(m.text());});page.on('response',r=>{if(r.url().includes('/vendor/embedpdf/'))previewRequests.push({url:r.url(),status:r.status()});});page.on('pageerror',e=>errors.push(e.message));page.on('request',r=>{if(/^https?:/.test(r.url())&&!r.url().startsWith(base+'/'))external.push(r.url());});page.on('requestfailed',r=>failed.push({url:r.url(),error:r.failure()?.errorText}));const base='http://127.0.0.1:'+server.address().port;
const documentPage=await browser.newPage();await documentPage.setContent('<html><body><h1>PDF preview sample</h1><p>Original PDF content.</p></body></html>');await fs.writeFile(path.join(remote,'sample.pdf'),await documentPage.pdf({format:'A4'}));await documentPage.close();
await page.goto(base+'/#storage');await page.getByLabel('账号',{exact:true}).fill('admin');await page.getByLabel('密码',{exact:true}).fill('Test-password-123');await page.getByRole('button',{name:'登录',exact:true}).click();await page.getByRole('button',{name:'浏览目录',exact:true}).click();
const frame=()=>page.frameLocator('iframe[title="文件内容预览"]');
async function open(filename){await page.locator('#storage-browser .list-group-item').filter({has:page.getByText(filename,{exact:true})}).getByRole('button',{name:'预览',exact:true}).click();}
await open('说明.txt');await frame().locator('#preview-text').waitFor();assert.equal(await frame().locator('#preview-text').textContent(),'<script>window.previewExecuted=true</script>\n第二行原文');assert.equal(await frame().locator('body').evaluate(()=>{try{void parent.document.body;return false;}catch{return window.previewExecuted!==true;}}),true);
await open('sample.docx');await frame().getByText('DOCX preview original text',{exact:true}).waitFor();
await open('sample.xlsx');await frame().getByRole('cell',{name:'42',exact:true}).waitFor();assert.equal(await frame().locator('img[src=x]').count(),0);await frame().getByLabel('工作表',{exact:true}).selectOption('1');await frame().getByRole('cell',{name:'Second sheet',exact:true}).waitFor();
await fs.mkdir(path.resolve('output/verification'),{recursive:true});await page.screenshot({path:path.resolve('output/verification/storage-preview-xlsx.png'),fullPage:true});
await open('sample.png');await frame().getByRole('button',{name:'放大图片',exact:true}).click();await frame().locator('.pswp--open').waitFor();
await open('sample.pdf');try{await frame().locator('#preview-pdf img[src^="blob:"]').first().waitFor({timeout:60000});}catch(error){t.diagnostic(JSON.stringify({failed,errors,external,consoleErrors,previewRequests,body:await frame().locator('body').evaluate(body=>{function read(node){return [...node.childNodes].map(n=>n.nodeType===3?n.textContent:n.nodeType===1&&!['STYLE','SCRIPT'].includes(n.tagName)?read(n.shadowRoot||n):'').join(' ');}return read(body);})}));throw error;}
assert.equal(await frame().locator('#preview-pdf img[src^="blob:"]').first().evaluate(async image=>{await image.decode();return image.naturalWidth>10&&image.naturalHeight>10;}),true);await page.screenshot({path:path.resolve('output/verification/storage-preview-pdf.png'),fullPage:true});
await open('old.doc');await frame().getByText(/旧版 Office 格式/).waitFor();await page.getByRole('button',{name:'关闭预览',exact:true}).click();assert.equal(await page.locator('iframe[title="文件内容预览"]').count(),0);
await page.setViewportSize({width:390,height:844});assert.equal(await page.evaluate(()=>document.documentElement.scrollWidth<=innerWidth),true);assert.deepEqual(external,[]);assert.deepEqual(errors,[]);
});