from pathlib import Path
import subprocess, json, sys
src=Path('/Users/yixiay/Downloads/123/597084-fix-code-2026-11-04/code')
baseline=Path('/Users/yixiay/Documents/codexWorkSpace/issues/output/597084-review/impact-2026-11-04/source')
if '--baseline' in sys.argv: src=baseline
label='red' if '--baseline' in sys.argv else 'green'
out=Path(__file__).resolve().parent
java=out/'src'; classes=out/'classes'; classes.mkdir(exist_ok=True)
def save(path,text):
 p=java/path;p.parent.mkdir(parents=True,exist_ok=True);p.write_text(text)
save(Path('cn/jojo/cc/common/exception/BusinessException.java'),'''package cn.jojo.cc.common.exception; public class BusinessException extends RuntimeException { public BusinessException(String s){super(s);} public static BusinessException paramException(String s){return new BusinessException(s);} }''')
# Execute original complete reader and normalizer; no edits, only exception framework stub.
files=[src/'application/src/main/java/cn/jojo/cc/biz/service/english/dict/EnglishImportFileReader.java',baseline/'common/src/main/java/cn/jojo/cc/common/utils/TextNormalizer.java']
# Extract original Param nested class without behavioral edits to isolate from service dependencies.
body=(src/'application/src/main/java/cn/jojo/cc/biz/service/english/dict/EnglishDictImportBizServiceImpl.java').read_text()
start=body.index('    private static final class Param {')
param=body[start:body.rfind('\n}')]
save(Path('cn/jojo/cc/biz/service/english/dict/ParamProbe.java'),'''package cn.jojo.cc.biz.service.english.dict; import org.apache.commons.lang3.StringUtils; import cn.jojo.cc.common.exception.BusinessException; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; public class ParamProbe {\n'''+param+'''\n public static void probe(String s){ Object raw=JSON.parseObject(s).get("limit"); boolean expected=raw==null ? !JSON.parseObject(s).containsKey("limit") : (raw instanceof Integer || raw instanceof Long || raw instanceof java.math.BigInteger) && raw.toString().matches("[1-9][0-9]*") && new java.math.BigInteger(raw.toString()).compareTo(java.math.BigInteger.valueOf(Integer.MAX_VALUE))<=0; try {Param p=Param.parse(s);if(!expected)throw new AssertionError("invalid limit accepted: "+s);System.out.println("PASS accepted "+s);}catch(RuntimeException e){if(expected)throw e;System.out.println("PASS rejected "+s);} } }''')
save(Path('cn/jojo/cc/biz/service/english/dict/ImportProbe.java'),'''package cn.jojo.cc.biz.service.english.dict;
import java.nio.charset.StandardCharsets;
public class ImportProbe {
 public static void main(String[] args) {
  EnglishImportFileReader r=new EnglishImportFileReader();
  var w=r.readWords("\\uFEFFword,stars,level,raw\\na/an,0,c,x\\nan,0,c,x\\nChina,0,c,x\\nchina,0,c,x\\nbe (am, are, is),0,c,x\\n".getBytes(StandardCharsets.UTF_8));
  System.out.println("VALID_WORDS "+w);
  var p=r.readPhrases("phrase,translation,verb,variants\\nlook up,\\"查找\\n抬头\\",look,looks up|looked up|look up|looks up\\nget up,起床,get,got up|gotten up\\n".getBytes(StandardCharsets.UTF_8));
  System.out.println("VALID_PHRASES "+p);
  try { var malformed=r.readPhrases("phrase,translation\\nlook up,\\"查找\\nget up,起床\\n".getBytes(StandardCharsets.UTF_8));
  if (!malformed.getRows().isEmpty()) throw new AssertionError("FAIL CSV accepted unclosed quote"); } catch(RuntimeException expected) {System.out.println("PASS CSV rejected unclosed quote");}
  var invalidVariant=r.readPhrases("phrase,translation,variants\\nlook up,查找,looks up|looked up\\nget up,起床,got up|起床\\n".getBytes(StandardCharsets.UTF_8));
  if(invalidVariant.getRows().size()!=1) throw new AssertionError("variant not atomic"); System.out.println("PASS variant atomic rejection");
  ParamProbe.probe("{\\"fileUri\\":\\"https://example.invalid/a.csv\\",\\"limit\\":0}");
  ParamProbe.probe("{\\"fileUri\\":\\"https://example.invalid/a.csv\\",\\"limit\\":-1}");
  ParamProbe.probe("{\\"fileUri\\":\\"https://example.invalid/a.csv\\",\\"limit\\":10001}");
  ParamProbe.probe("{\\"fileUri\\":\\"https://example.invalid/a.csv\\",\\"limit\\":20}");
 }
}''')
jars=[Path.home()/'.m2/repository'/p for p in ['org/apache/commons/commons-lang3/3.9/commons-lang3-3.9.jar','org/projectlombok/lombok/1.18.36/lombok-1.18.36.jar','org/springframework/spring-context/6.2.0/spring-context-6.2.0.jar','org/slf4j/slf4j-api/2.0.16/slf4j-api-2.0.16.jar','com/alibaba/fastjson/1.2.83/fastjson-1.2.83.jar']]
cp=':'.join(map(str,jars))
cmd=['javac','-cp',cp,'-processorpath',str(jars[1]),'-d',str(classes),*map(str,files),*map(str,[java/'cn/jojo/cc/common/exception/BusinessException.java',java/'cn/jojo/cc/biz/service/english/dict/ParamProbe.java',java/'cn/jojo/cc/biz/service/english/dict/ImportProbe.java'])]
r=subprocess.run(cmd,capture_output=True,text=True); (out/'compile.txt').write_text(r.stdout+r.stderr); print('javac_exit',r.returncode)
if r.returncode: print(r.stdout+r.stderr);raise SystemExit(r.returncode)
r=subprocess.run(['java','-cp',str(classes)+':'+cp,'cn.jojo.cc.biz.service.english.dict.ImportProbe'],capture_output=True,text=True)
(out/(label+'-import.txt')).write_text(r.stdout+r.stderr+'exit='+str(r.returncode)+'\n');print(r.stdout+r.stderr,'exit=',r.returncode);raise SystemExit(r.returncode)
