import com.gorios.lanvoice.expression.* import com.gorios.lanvoice.audio.* import com.gorios.lanvoice.net.* import android.os.Handler fun presenceCheck() { var checks=0 fun expect(value: Boolean) { check(value) { "presence assertion $checks" };checks++ } val rules=PresenceRules() for(t in 0L..19_900L step 100) expect(rules.sample(false,t,1,true)==null) expect(rules.sample(false,19_999,1,true)==null) expect(rules.sample(false,20_000,1,true)?.kind==PresenceKind.ABSENT) for(t in 20_100L..45_000L step 100) expect(rules.sample(false,t,1,true)==null) expect(rules.sample(true,45_100,1,true)==null) for(t in 45_200L..46_000L step 100) expect(rules.sample(true,t,1,true)==null) expect(rules.sample(true,46_099,1,true)==null) expect(rules.sample(true,46_100,1,true)?.kind==PresenceKind.APPEARED) for(t in 46_200L..47_000L step 100) expect(rules.sample(true,t,1,true)==null) for(t in 47_100L..67_000L step 100) expect(rules.sample(false,t,1,true)==null) expect(rules.sample(false,67_100,1,true)?.kind==PresenceKind.ABSENT) for(t in 67_200L..68_200L step 100) expect(rules.sample(true,t,1,false,absentInFlight=true)==null) expect(rules.sample(true,68_300,1,true)==null) // No late return greeting after departure speech. for(turnChanged in listOf(false,true)) { val r=PresenceRules() for(t in 0L..20_000L step 100) expect(r.sample(false,t,1,false)==null) for(t in 20_100L..25_000L step 100) expect(r.sample(false,t,if(turnChanged) 2 else 1,false)==null) expect(r.sample(false,25_100,if(turnChanged) 2 else 1,true)==null) } val gap=PresenceRules() for(t in 0L..19_900L step 100) expect(gap.sample(false,t,1,true)==null) expect(gap.sample(false,20_400,1,true)==null) expect(gap.sample(null,20_500,1,true)==null) expect(gap.sample(false,20_600,1,true)==null) expect(gap.sample(false,20_600,1,true)==null) gap.reset();expect(gap.sample(false,25_000,1,true)==null) val cool=PresenceRules() for(t in 0L..20_000L step 100) cool.sample(false,t,1,true) for(t in 20_100L..21_100L step 100) expect(cool.sample(true,t,1,true,appearedAllowedAt=600_000)==null) expect(cool.sample(true,21_200,1,true)==null) checkPresenceSessionCallbacks() println("PASS: $checks presence sample-boundary/episode/rearm/TTL/cooldown/UNKNOWN/gap/order assertions; production client callback/turn/cancel replay, device not_run") } private fun checkPresenceSessionCallbacks() { val states=mutableListOf();val session=VoiceSession("localhost",5011,states::add) fun field(name: String,value: Any?) { session.javaClass.getDeclaredField(name).apply { isAccessible=true }.set(session,value) } fun get(name: String)=session.javaClass.getDeclaredField(name).apply { isAccessible=true }.get(session) val dialogue=get("dialogue") as DialogueClient val socket=FakeSocket();dialogue.setField("socket",socket);dialogue.setField("ready",true) field("foreground",true);field("state",VoiceUiState(connection=ConnectionStatus.CONNECTED)) val eventMethod=session.javaClass.getDeclaredMethod("onEvent",ServerEvent::class.java).apply { isAccessible=true } fun event(kind: EventKind,turn: Long,fields: Map = emptyMap(),reason: String="",state: String="") { eventMethod.invoke(session,ServerEvent(if(kind==EventKind.PRESENCE) "presence.status" else kind.name.lowercase(),kind,Payload(turn=turn,reason=reason,state=state,fields=fields))) } session.startVoice();event(EventKind.HELLO,1,mapOf("presence_greeting_version" to 1)) session.setPresence(true,true) fun primeAndSend(): Long { val now=android.os.SystemClock.elapsedRealtime() val rules=get("presenceRules") as PresenceRules rules.reset() rules.javaClass.getDeclaredField("absentSince").apply { isAccessible=true }.set(rules,now-20_000) field("lastSpeechAt",now-1_000) session.observePresence(false,now) val sent=socket.sent.last();check(sent.startsWith("presence_absent/")) return sent.split('/')[1].toLong() } val id=primeAndSend() event(EventKind.ABORT,2,mapOf("request_id" to id),reason="presence_greeting") event(EventKind.PRESENCE,2,mapOf("request_id" to id),state="accepted") check(states.last().turnId==2L && get("presenceRequest")!=null) event(EventKind.TTS,2,state="start") val frames=session.javaClass.getDeclaredMethod("onFrame",DownlinkFrame::class.java).apply { isAccessible=true } frames.invoke(session,DownlinkFrame(2)) check(states.last().phase==VoicePhase.ANSWERING) checkNotNull(AudioPlayback.lastDrained)(2,0);Handler.drain();check(get("presenceRequest")==null) val id2=primeAndSend();session.setPresence(false,true) check(socket.sent.count { it=="presence_cancel/$id2" }==1) val before=AudioPlayback.enqueued event(EventKind.ABORT,3,mapOf("request_id" to id2),reason="presence_greeting") frames.invoke(session,DownlinkFrame(3));check(AudioPlayback.enqueued==before) event(EventKind.ABORT,4,reason="presence_cancelled") check(states.last().turnId==4L) session.setPresence(true,true);val id3=primeAndSend() event(EventKind.VAD,4,mapOf("event" to Payload(fields=mapOf("type" to "speech.start")))) check(get("presenceRequest")==null && socket.sent.any { it=="presence_cancel/$id3" }) session.background();session.release();Handler.drain() }