tests/ExtendedSafetyCheck.scala
1package xstream.galaxy
2import java.io._
3import java.nio.file.{Files,Path}
4import java.nio.charset.StandardCharsets.UTF_8
5import com.fasterxml.jackson.databind.ObjectMapper
6import java.util.concurrent.CancellationException
7import scala.jdk.CollectionConverters._
8object ExtendedSafetyCheck {
9 def main(args:Array[String]):Unit={
10 val mapper=new ObjectMapper;val specs=mapper.readTree(Files.readString(Path.of("extra-specs.json")))
11 var total=0
12 specs.fields.asScala.foreach{entry=>
13 val s=entry.getValue;val op=Class.forName(s.path("entrypoint").asText+"$").getField("MODULE$").get(null).asInstanceOf[ExtendedOperator]
14 val f=s.path("fixture");val data=f.path("data").asText.getBytes(UTF_8);val params=f.path("params");val out=new ByteArrayOutputStream
15 def invoke(input:InputStream,output:OutputStream,parameters:com.fasterxml.jackson.databind.JsonNode):Unit={val extra=f.path("inputs").fields.asScala.map(e=>e.getKey->new ByteArrayInputStream(e.getValue.asText.getBytes(UTF_8))).toMap;op.transformInputs(extra+("source"->input),output,parameters)}
16 invoke(new ByteArrayInputStream(data),out,params);assert(out.toString("UTF-8")==f.path("expected").asText,entry.getKey+" actual="+out.toString("UTF-8"))
17 def fails(body: => Unit):Unit={var failed=false;try body catch{case _:IllegalArgumentException=>failed=true};assert(failed,entry.getKey)}
18 fails(invoke(new ByteArrayInputStream(data),new ByteArrayOutputStream,mapper.readTree("{\"unsupported\":1}")))
19 Thread.currentThread.interrupt()
20 try{invoke(new ByteArrayInputStream(data),new ByteArrayOutputStream,params);throw new AssertionError("cancellation ignored")}catch{case _:CancellationException=>()}finally Thread.interrupted()
21 var io=false;try invoke(new InputStream{def read():Int=throw new IOException("input")},new ByteArrayOutputStream,params)catch{case _:IOException=>io=true};assert(io)
22 io=false;try invoke(new ByteArrayInputStream(data),new OutputStream{def write(b:Int):Unit=throw new IOException("output")},params)catch{case _:IOException=>io=true};assert(io)
23 var utf8=false;try invoke(new ByteArrayInputStream(Array(0xff.toByte)),new ByteArrayOutputStream,params)catch{case _:java.nio.charset.MalformedInputException=>utf8=true};assert(utf8)
24 fails(invoke(new ByteArrayInputStream(("A"*1048577).getBytes(UTF_8)),new ByteArrayOutputStream,params))
25 if(entry.getKey=="emboss_shuffleseq"){
26 val input=">shuffle\nACGTACGTTTGGCCAA\n".getBytes(UTF_8);val first=new ByteArrayOutputStream;val second=new ByteArrayOutputStream
27 invoke(new ByteArrayInputStream(input),first,params);invoke(new ByteArrayInputStream(input),second,params)
28 assert(first.toByteArray.toVector==second.toByteArray.toVector,"shuffle seed differs")
29 assert(first.toString("UTF-8").split("\n")(1).sorted=="ACGTACGTTTGGCCAA".sorted,"shuffle changes composition");total+=2
30 }
31 total+=7;println("PASS fixture, validation, cancellation, I/O, encoding and line budget: "+entry.getKey)
32 }
33 val regressions=mapper.readTree(Files.readString(Path.of("extended-regressions.json")))
34 Seq("positive","negative").foreach{kind=>regressions.path(kind).elements.asScala.foreach{c=>
35 val spec=specs.path(c.path("operator").asText);val op=Class.forName(spec.path("entrypoint").asText+"$").getField("MODULE$").get(null).asInstanceOf[ExtendedOperator]
36 val out=new ByteArrayOutputStream;var failed=false
37 val extra=c.path("inputs").fields.asScala.map(e=>e.getKey->new ByteArrayInputStream(e.getValue.asText.getBytes(UTF_8))).toMap
38 try op.transformInputs(extra+("source"->new ByteArrayInputStream(c.path("data").asText.getBytes(UTF_8))),out,c.path("params"))catch{case _:IllegalArgumentException=>failed=true}
39 def equivalent(actual:String,expected:String):Boolean={
40 if(c.has("alignment_score")){
41 val lines=actual.split("\n");val score="score=([-0-9.]+)".r.findFirstMatchIn(actual).map(_.group(1).toDouble)
42 val rows=lines.filterNot(_.startsWith(">"));val originalA=c.path("data").asText.split("\n").filterNot(_.startsWith(">")).mkString;val originalB=c.path("inputs").path("reference").asText.split("\n").filterNot(_.startsWith(">")).mkString
43 if(rows.length!=2||rows(0).length!=rows(1).length||score.isEmpty)false else{
44 val a=rows(0);val b=rows(1);var calculated=0d;var gap=0;var valid=true
45 a.indices.foreach{i=>if(a(i)=='-'&&b(i)=='-')valid=false;val state=if(a(i)=='-')1 else if(b(i)=='-')2 else 0;if(state==0)calculated+= (if(a(i)==b(i))2 else -1)else calculated-= (if(state==gap)1 else 3);gap=state}
46 val local=c.path("operator").asText=="emboss_water";val aa=a.filterNot(_=='-');val bb=b.filterNot(_=='-')
47 valid&&math.abs(calculated-c.path("alignment_score").asDouble)<1e-6&&math.abs(score.get-calculated)<1e-6&&(if(local)originalA.contains(aa)&&originalB.contains(bb)else originalA==aa&&originalB==bb)
48 }
49 }else if(!c.has("numeric_tolerance"))actual==expected else{
50 val a=actual.split("[\t\n]",-1);val e=expected.split("[\t\n]",-1);a.length==e.length&&a.zip(e).forall{case(x,y)=>if(x==y)true else try{val u=x.toDouble;val v=y.toDouble;u.isFinite&&v.isFinite&&math.abs(u-v)<=c.path("numeric_tolerance").asDouble}catch{case _:NumberFormatException=>false}}
51 }
52 }
53 if(kind=="positive")assert(!failed&&equivalent(out.toString("UTF-8"),c.path("expected").asText),c.path("operator").asText+" actual="+out.toString("UTF-8")+" expected="+c.path("expected").asText)
54 else assert(failed,"malformed input accepted: "+c)
55 total+=1
56 }}
57 println(s"PASS $total extended adapter checks, including upstream golden fixtures")
58 }
59}