53 lines
4.9 KiB
Python
53 lines
4.9 KiB
Python
#!/usr/bin/env python3
|
|
"""Host-side Kotlin smoke tests. Uses kotlinc if present; does not require Android SDK."""
|
|
from pathlib import Path
|
|
import shutil, subprocess, tempfile, textwrap, os
|
|
|
|
ROOT=Path(__file__).resolve().parents[1]
|
|
K=ROOT/'android/app/src/main/java/com/qft/optical'
|
|
kotlinc=shutil.which('kotlinc')
|
|
if not kotlinc:
|
|
print('SKIP Kotlin host smoke: kotlinc not installed (Android Gradle tests still cover these files on a dev host)')
|
|
raise SystemExit(0)
|
|
|
|
def run(args):
|
|
env=os.environ.copy(); env.setdefault('TERM','xterm')
|
|
p=subprocess.run(args, text=True, capture_output=True, env=env)
|
|
if p.returncode:
|
|
print(p.stdout); print(p.stderr); raise SystemExit(p.returncode)
|
|
|
|
with tempfile.TemporaryDirectory() as td:
|
|
td=Path(td)
|
|
smoke=td/'Smoke.kt'
|
|
smoke.write_text(textwrap.dedent(r'''
|
|
package com.qft.optical
|
|
import java.nio.ByteBuffer
|
|
import java.nio.ByteOrder
|
|
import java.util.zip.CRC32
|
|
fun b45e(data: ByteArray): String { val a="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:"; val o=StringBuilder(); var i=0; while(i+1<data.size){val v=(data[i].toInt() and 255)*256+(data[i+1].toInt() and 255);o.append(a[v%45]).append(a[(v/45)%45]).append(a[v/2025]);i+=2};if(i<data.size){val v=data[i].toInt() and 255;o.append(a[v%45]).append(a[v/45])};return o.toString() }
|
|
fun fr(p:ByteArray):String { val b=ByteBuffer.allocate(24+p.size+4).order(ByteOrder.BIG_ENDIAN);b.put("QFT2".toByteArray()).put(2).put(2).putLong(7L).putInt(9).putInt(11).putShort(p.size.toShort()).put(p);val n=b.position();val c=CRC32().apply{update(b.array(),0,n)}.value;b.putInt(c.toInt());return "QFT2:"+b45e(b.array()) }
|
|
fun main(){check(Base45.decode("BB8")!!.contentEquals("AB".toByteArray()));check(Base45.decode("%69 VD92EX0")!!.contentEquals("Hello!!".toByteArray()));val p="hello-qft2".toByteArray();val f=QftProtocol.parseText(fr(p))!!;check(f.kind==FrameKind.DATA&&f.sessionId==7L&&f.seq==9L&&f.payload.contentEquals(p));val c=LockRect(100,100,300,300).smoothToward(LockRect(140,130,340,340),.25f);check(c.left in 101..139);val q=mutableListOf("A","B","C");check(moveQueueItem(q,0,2));check(q==listOf("B","C","A"));println("PASS Kotlin QFT2/Base45 protocol + ROI lock + transfer queue")}
|
|
'''))
|
|
jar=td/'smoke.jar'
|
|
run([kotlinc, str(K/'Base45.kt'), str(K/'Base64Url.kt'), str(K/'Protocol.kt'), str(K/'LockRect.kt'), str(K/'TransferQueue.kt'), str(smoke), '-include-runtime', '-d', str(jar)])
|
|
p=subprocess.run(['java','-jar',str(jar)],text=True,capture_output=True,env={**os.environ,'TERM':'xterm'},check=True)
|
|
print(p.stdout.strip())
|
|
|
|
# CameraX/ZXing-shaped compile stubs: catches Kotlin signature/syntax drift in the analyzer without Android SDK.
|
|
for d in ['android/os','androidx/camera/core','com/google/zxing/common','com/google/zxing/qrcode','com/google/zxing']:
|
|
(td/d).mkdir(parents=True,exist_ok=True)
|
|
(td/'android/os/SystemClock.kt').write_text('package android.os\nobject SystemClock { fun elapsedRealtime(): Long = 0L }\n')
|
|
(td/'androidx/camera/core/Core.kt').write_text(textwrap.dedent('''
|
|
package androidx.camera.core
|
|
import java.nio.ByteBuffer
|
|
interface ImageAnalysis { interface Analyzer { fun analyze(image: ImageProxy) } }
|
|
interface ImageProxy { val width:Int; val height:Int; val planes:Array<PlaneProxy>; val imageInfo:ImageInfo; fun close(); interface PlaneProxy { val buffer:ByteBuffer; val rowStride:Int; val pixelStride:Int }; interface ImageInfo { val rotationDegrees:Int } }
|
|
'''))
|
|
(td/'com/google/zxing/Zxing.kt').write_text('package com.google.zxing\nopen class LuminanceSource(val width:Int,val height:Int)\nclass PlanarYUVLuminanceSource(data:ByteArray,dataWidth:Int,dataHeight:Int,left:Int,top:Int,width:Int,height:Int,reverseHorizontal:Boolean):LuminanceSource(width,height)\nclass BinaryBitmap(val binarizer:Any)\ndata class ResultPoint(val x:Float,val y:Float)\nclass Result(val text:String?="",val rawBytes:ByteArray?=null,val resultPoints:Array<ResultPoint>?=null)\n')
|
|
(td/'com/google/zxing/common/Common.kt').write_text('package com.google.zxing.common\nimport com.google.zxing.LuminanceSource\nclass GlobalHistogramBinarizer(source:LuminanceSource)\nclass HybridBinarizer(source:LuminanceSource)\n')
|
|
(td/'com/google/zxing/qrcode/QRCodeReader.kt').write_text('package com.google.zxing.qrcode\nimport com.google.zxing.BinaryBitmap\nimport com.google.zxing.Result\nclass QRCodeReader { fun decode(bitmap:BinaryBitmap):Result=Result(); fun reset(){} }\n')
|
|
out=td/'analyzer.jar'
|
|
sources=[td/'android/os/SystemClock.kt',td/'androidx/camera/core/Core.kt',td/'com/google/zxing/Zxing.kt',td/'com/google/zxing/common/Common.kt',td/'com/google/zxing/qrcode/QRCodeReader.kt',K/'LockRect.kt',K/'ScannerLockState.kt',K/'GrayFrame.kt',K/'QrAnalyzer.kt']
|
|
run([kotlinc,*map(str,sources),'-d',str(out)])
|
|
print('PASS Kotlin QrAnalyzer compile smoke with CameraX/ZXing-shaped stubs')
|