Files
ReceiptGenerator/scripts/package-test.mjs
T

40 lines
3.1 KiB
JavaScript

import { _electron as electron,expect } from '@playwright/test'
import fs from 'node:fs/promises'
import path from 'node:path'
import {PDFDocument} from 'pdf-lib'
const profile=path.join(process.cwd(),'.qa','packaged-profile-1.0.1')
await fs.mkdir(profile,{recursive:true})
const env={...process.env};delete env.ELECTRON_RUN_AS_NODE
let app
try{
app=await electron.launch({executablePath:path.join(process.cwd(),'release','win-unpacked','收据工坊.exe'),args:['--smoke-test','--user-data-dir='+profile],env,timeout:60000})
const page=await app.firstWindow();page.setDefaultTimeout(20000)
await expect(page.getByRole('heading',{name:'收据工作台',exact:true})).toBeVisible({timeout:30000})
await expect(page.getByText('把表格交给收据工坊')).toBeVisible({timeout:30000})
const boot=await page.evaluate(()=>window.receipts.bootstrap())
expect(boot.dataDirectory).toBe(profile)
expect(boot.nextNumber).toBe('SJ00000001')
const sealFolder=path.join(process.cwd(),'.qa','fixtures','seals'),output=path.join(process.cwd(),'.qa','packaged-output')
await fs.mkdir(output,{recursive:true})
await app.evaluate(({shell})=>{shell.openPath=async()=>''})
await page.evaluate(async({output,sealFolder})=>{const b=await window.receipts.bootstrap();await window.receipts.saveSettings({...b.settings,outputDirectory:output,sealDirectory:sealFolder,numberPrefix:'PACK'})},{output,sealFolder})
const seals=await page.evaluate(()=>window.receipts.listSeals());expect(seals.images.length).toBeGreaterThan(0)
await app.evaluate(({dialog},file)=>{dialog.showOpenDialog=async()=>({canceled:false,filePaths:[file]})},path.join(process.cwd(),'.qa','fixtures','收据测试数据.xlsx'))
const draft=await page.evaluate(()=>window.receipts.importExcel())
expect(draft.rows[0].receipt.customer).toBe('某某学校 初二年级 张三')
expect(draft.rows[0].receipt.school).toBe('某某学校')
expect(draft.rows[0].receipt.grade).toBe('初二年级')
expect(draft.rows[0].receipt.studentName).toBe('张三')
await page.evaluate(async({id,sealId})=>window.receipts.exportReceipts({draftId:id,rowIndices:[0],sealId,options:{pdf:true,png:true,print:true,receiptsPerPage:2,cutLine:true}}),{id:draft.id,sealId:seals.images[0].id})
await expect.poll(async()=>page.evaluate(async()=> (await window.receipts.listBatches())[0]?.status),{timeout:90000}).toBe('completed')
const batch=(await page.evaluate(()=>window.receipts.listBatches()))[0]
const files=await fs.readdir(batch.outputDirectory,{recursive:true})
expect(files.filter(n=>n.endsWith('.png'))).toHaveLength(1)
const png=await fs.readFile(path.join(batch.outputDirectory,files.find(n=>n.endsWith('.png'))))
expect(png.readUInt32BE(png.indexOf('pHYs')+4)).toBe(11811)
const pdf=await PDFDocument.load(await fs.readFile(path.join(batch.outputDirectory,files.find(n=>path.basename(n).startsWith('A4')))))
expect(pdf.getPageCount()).toBe(1)
await fs.writeFile('.qa/package-result.json',JSON.stringify({passed:true,platform:'Windows 10 x64',packaged:true,renderedPdf:true,renderedPng:true,dpi:300,profile},null,2))
console.log('Packaged application verified: standalone startup, Excel, seals, PDF and PNG.')
}finally{if(app)await app.close().catch(()=>{})}