import json import subprocess from pathlib import Path import pytest ROOT = Path(__file__).resolve().parents[2] def test_delete_plain_row_rejects_document_bookmark_and_preserves_styles(): script = r"""const w=require('./tools/est-test/export-quantity.cjs'),q=require('./tools/est-test/native-quantity.cjs'); const doc='
plain
',sheet=q.fromWpt('N','sheet',doc,[],{}); const out=w.removeRows(doc,sheet,new Set(),new Set([0])); const marked='
title
'; const other=q.fromWpt('N','sheet',marked,[],{});let rejected=false; try{w.removeRows(marked,other,new Set(),new Set([0]))}catch{rejected=true} process.stdout.write(JSON.stringify({style:out.includes(''),removed:!out.includes('plain'),rejected}));""" result = json.loads(subprocess.check_output(["node", "-e", script], cwd=ROOT)) assert result == {"style": True, "removed": True, "rejected": True} @pytest.mark.parametrize("edit", [False, True]) def test_delete_plain_row_in_installed_file_when_sample_exists(edit): sample = Path("C:/Dasansoft/Est Plus 2/Data/ProjectData/main.estx") if not sample.exists(): pytest.skip("installed EST sample unavailable") script = r"""const fs=require('fs'),z=require('zlib'),m=require('./tools/est-test/import-estx.cjs'), e=require('./tools/est-test/export-estx.cjs'),w=require('./tools/est-test/export-quantity.cjs'),c=require('./tools/est-test/quantity-calculate.cjs'); const bytes=fs.readFileSync(process.argv[1]),p=m.importEstx(bytes);let candidate=null; const valid=s=>{const x=c.calculateQuantity(s),n=s.rows.reduce((a,r)=>a+r.markers.length,0); return !x.errors.length&&x.checks.length===n&&x.checks.every(v=>Number.isFinite(v.stored)&&v.stored===v.calculated)&&x.items.every((v,i)=>v.GN===s.items[i].GN)}; for(const s of p.quantitySheets){if(!valid(s))continue;const r=p.tables.MulMainDat.find(x=>x.Code===s.Code); let b=Buffer.from(r.Editor.binary,'base64');if(b[0]===120)b=z.inflateSync(b);const doc=new TextDecoder('euc-kr').decode(b); for(let i=0;iJSON.stringify(x)===JSON.stringify(row)).length!==1)continue; const rows=s.rows.filter((_,j)=>j!==i); if(process.argv[2]==='edit'){ const j=rows.findIndex(row=>row.markers.length===1&&rows.filter(x=>JSON.stringify(x.markers)===JSON.stringify(row.markers)).length===1); if(j<0)continue;rows[j]={...rows[j],LineData:' '+rows[j].LineData}; } try{const source=w.removeRows(doc,s,new Set(),new Set([i]));if(!valid({...s,rows}))continue; w.editDocument(source,{...s,rows:s.rows.filter((_,j)=>j!==i)},{...s,rows}); candidate={sheet:s,rows};break}catch{}} if(candidate)break; } if(!candidate)throw Error('no safe plain-row fixture'); const s=candidate.sheet,before=structuredClone(s);s.rows=candidate.rows; const out=e.exportEstx(p),q=m.importEstx(out),saved=q.quantitySheets.find(x=>x.Code===s.Code),a=m.unzip(bytes),b=m.unzip(out); process.stdout.write(JSON.stringify({rows:JSON.stringify(saved.rows)===JSON.stringify(s.rows), items:JSON.stringify(saved.items)===JSON.stringify(before.items), indexes:[...a].filter(([name])=>name.endsWith('.idx')).every(([name,data])=>data.equals(b.get(name)))}));""" result = json.loads( subprocess.check_output( ["node", "-e", script, str(sample), "edit" if edit else "plain"], cwd=ROOT, timeout=90 ).decode("utf-8") ) assert result == {"rows": True, "items": True, "indexes": True} def test_delete_duplicate_plain_row_is_rejected_when_sample_exists(): sample = Path("C:/Dasansoft/Est Plus 2/Data/ProjectData/main.estx") if not sample.exists(): pytest.skip("installed EST sample unavailable") script = r"""const fs=require('fs'),m=require('./tools/est-test/import-estx.cjs'),e=require('./tools/est-test/export-estx.cjs'); const p=m.importEstx(fs.readFileSync(process.argv[1])),sheet=p.quantitySheets.find(s=>s.Code==='N0000000071'); const index=sheet.rows.findIndex((r,i)=>!r.markers.length&&sheet.rows.some((x,j)=>i!==j&&JSON.stringify(x)===JSON.stringify(r))); if(index<0)throw Error('duplicate fixture unavailable');sheet.rows.splice(index,1); try{e.exportEstx(p);process.stdout.write('false')}catch(error){process.stdout.write(JSON.stringify(error.message.includes('중복')))}""" assert json.loads(subprocess.check_output(["node", "-e", script, str(sample)], cwd=ROOT))