forked from Blendman974/faceclaw
Convert most Android-specific kotlin to shared or shareable
This commit is contained in:
1 parent
f9724ddcab
commit
587364e5e6
92 files changed
+14347
-9193
No files matched your search
@@ -0,0 +1,33 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
import java.util.Locale
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
/** The shared export must match what the Java-era String.format("%+7d") produced. */
|
||||
class FrameTimingsFormatTest {
|
||||
private class FakeClock : ProtocolPlatform by AndroidProtocolPlatform {
|
||||
var nowMs = 5_000L
|
||||
override fun elapsedRealtimeMs(): Long = nowMs
|
||||
}
|
||||
|
||||
@Test
|
||||
fun lineOffsetsMatchJavaFormat() {
|
||||
val clock = FakeClock()
|
||||
val core = FrameTimingsCore(clock, wallClockMs = { 0L }, threadName = { "worker" }, formatWallClock = { "w" }, logInfo = {}, logWarn = {})
|
||||
val offsets = listOf(-1_234_567L, -12L, -1L, 0L, 1L, 42L, 999L, 1_000L, 12_345L, 1_234_567L, 12_345_678L)
|
||||
val root = core.startFrame("input:fmt")
|
||||
for (offset in offsets) {
|
||||
clock.nowMs = 5_000L + offset
|
||||
core.log(root, "o=$offset")
|
||||
}
|
||||
clock.nowMs = 5_000L
|
||||
core.finishFrame(root, "sent")
|
||||
val export = core.buildExport()
|
||||
for (offset in offsets) {
|
||||
val javaLine = String.format(Locale.US, " %+7dms [%s] %s", offset, "worker", "o=$offset")
|
||||
assert(export.contains("\n$javaLine\n")) { "missing <$javaLine> in\n$export" }
|
||||
}
|
||||
assertEquals(2, export.split("\n").count { it.contains("finished: sent") })
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
private class FakeScheduler : AlarmScheduler {
|
||||
val pending = LinkedHashMap<Any, Pair<Long, () -> Unit>>()
|
||||
override fun postDelayed(token: Any, delayMs: Long, action: () -> Unit) { pending[token] = delayMs to action }
|
||||
override fun cancel(token: Any) { pending.remove(token) }
|
||||
override fun cancelAll() { pending.clear() }
|
||||
fun fire(token: Any) { val entry = pending.remove(token) ?: error("no timer $token"); entry.second() }
|
||||
}
|
||||
|
||||
private class FakeSink : RingingSink {
|
||||
val events = ArrayList<String>()
|
||||
var sounding = false
|
||||
override fun showNotification(item: RingingItem) { events.add("notify ${item.id} " + (if (item.silenced) "silenced" else if (item.escalated) "ringing" else "glasses")) }
|
||||
override fun itemRemoved(item: RingingItem) { events.add("removed ${item.id}") }
|
||||
override fun ringingStarted() { events.add("wake") }
|
||||
override fun startSound() { if (!sounding) { sounding = true; events.add("sound on") } }
|
||||
override fun stopSound() { if (sounding) { sounding = false; events.add("sound off") } }
|
||||
override fun reschedule(item: RingingItem, atMs: Long, minutes: Int) { events.add("reschedule ${item.id} $atMs $minutes") }
|
||||
override fun cancelSchedule(id: Long) { events.add("cancel $id") }
|
||||
override fun recordPhoneAction(id: Long, action: String, minutes: Int) { events.add("action $id $action $minutes") }
|
||||
override fun log(line: String) { events.add("log $line") }
|
||||
}
|
||||
|
||||
class AlarmRingingPolicyTest {
|
||||
private var now = 5_000_000L
|
||||
private val gate = GlassesStatusGate { now }
|
||||
private val scheduler = FakeScheduler()
|
||||
private val sink = FakeSink()
|
||||
private val policy = AlarmRingingPolicy(gate, { now }, testPlatform()).also { it.attach(scheduler, sink) }
|
||||
|
||||
private fun item(id: Long, kind: String = AlarmSchedule.KIND_ALARM) = RingingItem(id, "Wake", "", kind, 9, now)
|
||||
|
||||
@Test
|
||||
fun glassesUnavailableEscalatesImmediately() {
|
||||
policy.ring(item(1))
|
||||
assertEquals(listOf("log ring alarm 1 (no glasses status)", "wake", "notify 1 glasses", "log phone sound for 1: no glasses status", "notify 1 ringing", "sound on"), sink.events)
|
||||
assertTrue(policy.isRinging(1))
|
||||
assertEquals(setOf<Any>("silence:1"), scheduler.pending.keys)
|
||||
scheduler.fire("silence:1")
|
||||
assertEquals(listOf("log auto-silenced 1", "notify 1 silenced", "sound off"), sink.events.drop(6))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun deliveryAndAcknowledgementTimersEscalate() {
|
||||
gate.set(connected = true, worn = true, charging = false)
|
||||
policy.ring(item(2))
|
||||
assertEquals(listOf("log ring alarm 2 (glasses worn)", "wake", "notify 2 glasses"), sink.events)
|
||||
assertEquals(setOf<Any>("delivery:2"), scheduler.pending.keys)
|
||||
policy.markDelivered(2)
|
||||
assertEquals("log delivered 2 to glasses", sink.events.last())
|
||||
assertEquals(setOf<Any>("delivery:2", "ack:2"), scheduler.pending.keys)
|
||||
scheduler.fire("delivery:2") // delivered in time: no escalation
|
||||
assertFalse(policy.snapshot().single().escalated)
|
||||
scheduler.fire("ack:2")
|
||||
assertTrue(policy.snapshot().single().escalated)
|
||||
assertTrue(sink.sounding)
|
||||
// Acknowledged on the glasses afterwards: quiet stop, sound off.
|
||||
policy.stopQuietly(2, "acknowledged")
|
||||
assertEquals(listOf("log acknowledged 2", "removed 2", "sound off"), sink.events.takeLast(3))
|
||||
assertFalse(policy.isAnythingRinging())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun undeliveredRingEscalatesAfterDeliveryWait() {
|
||||
gate.set(connected = true, worn = true, charging = false)
|
||||
policy.ring(item(3))
|
||||
scheduler.fire("delivery:3")
|
||||
assertEquals("log phone sound for 3: not delivered to the glasses", sink.events[3])
|
||||
assertTrue(sink.sounding)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun snoozeAndDismissRecordActionsAndRearm() {
|
||||
policy.ring(item(4, AlarmSchedule.KIND_TIMER))
|
||||
policy.snooze(4)
|
||||
assertEquals(listOf("action 4 snooze 9", "removed 4", "sound off", "reschedule 4 ${now + 9 * 60_000L} 9"), sink.events.takeLast(4))
|
||||
policy.ring(item(5))
|
||||
policy.dismiss(5)
|
||||
assertEquals(listOf("action 5 dismiss 0", "removed 5", "sound off", "cancel 5"), sink.events.takeLast(4))
|
||||
policy.dismiss(99) // unknown: no-op
|
||||
assertEquals("cancel 5", sink.events.last())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun earlySignalsAreConsumedOrExpire() {
|
||||
gate.set(connected = true, worn = true, charging = false)
|
||||
assertFalse(policy.deliveredOrDefer(6))
|
||||
policy.ring(item(6))
|
||||
assertEquals("log delivered 6 to glasses", sink.events.last())
|
||||
assertEquals(setOf<Any>("ack:6"), scheduler.pending.keys)
|
||||
|
||||
assertFalse(policy.stopOrDefer(7))
|
||||
policy.ring(item(7))
|
||||
assertEquals("log ring 7 already acknowledged", sink.events.last())
|
||||
assertFalse(policy.isRinging(7))
|
||||
|
||||
assertFalse(policy.stopOrDefer(8))
|
||||
now += AlarmRingingPolicy.EARLY_SIGNAL_MAX_AGE_MS + 1
|
||||
policy.ring(item(8))
|
||||
assertTrue(policy.isRinging(8))
|
||||
assertTrue(policy.stopOrDefer(8))
|
||||
assertTrue(policy.deliveredOrDefer(8))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun duplicateRingOnlyRefreshesNotificationAndDetachDropsTimers() {
|
||||
policy.ring(item(9))
|
||||
val before = sink.events.size
|
||||
policy.ring(item(9))
|
||||
assertEquals(listOf("notify 9 ringing"), sink.events.drop(before))
|
||||
assertEquals(1, policy.snapshot().size)
|
||||
policy.detach()
|
||||
assertTrue(scheduler.pending.isEmpty())
|
||||
policy.ring(item(10)) // no host: ignored
|
||||
assertFalse(policy.isRinging(10))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertNotNull
|
||||
import kotlin.test.assertNull
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
private class MemoryStore : AlarmStore {
|
||||
val values = HashMap<String, String?>()
|
||||
override fun read(key: String): String? = values[key]
|
||||
override fun write(key: String, value: String?) { values[key] = value }
|
||||
}
|
||||
|
||||
class AlarmScheduleTest {
|
||||
private val store = MemoryStore()
|
||||
private var now = 1_700_000_000_000L
|
||||
private val schedule = AlarmSchedule(store, { now }, testPlatform())
|
||||
|
||||
@Test
|
||||
fun persistsEntriesAsJsonAndReplacesById() {
|
||||
schedule.put(AlarmEntry(7, now + 60_000, "Tea", "steep", AlarmSchedule.KIND_TIMER, 5))
|
||||
schedule.put(AlarmEntry(8, now + 120_000, null, null, null, 10))
|
||||
assertEquals("""[{"id":7,"at":${now + 60_000},"title":"Tea","text":"steep","kind":"timer","snoozeMinutes":5},{"id":8,"at":${now + 120_000},"title":"","text":"","kind":"alarm","snoozeMinutes":10}]""", store.values[AlarmSchedule.KEY_SCHEDULED])
|
||||
schedule.put(AlarmEntry(7, now + 90_000, "Tea", "steep", AlarmSchedule.KIND_TIMER, 5))
|
||||
assertEquals(listOf(8L, 7L), schedule.entries().map { it.id })
|
||||
assertEquals(now + 90_000, schedule.find(7)?.at)
|
||||
schedule.remove(8)
|
||||
assertNull(schedule.find(8))
|
||||
// A corrupt file reads as empty rather than failing.
|
||||
store.values[AlarmSchedule.KEY_SCHEDULED] = "{nope"
|
||||
assertTrue(schedule.entries().isEmpty())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun replayRingsRecentDropsMissedArmsFuture() {
|
||||
schedule.put(AlarmEntry(1, now - 60_000, "recent", "", null, 10))
|
||||
schedule.put(AlarmEntry(2, now - AlarmSchedule.LATE_GRACE_MS - 1, "old", "", AlarmSchedule.KIND_TIMER, 10))
|
||||
schedule.put(AlarmEntry(3, now + 60_000, "future", "", null, 10))
|
||||
val rang = ArrayList<Long>()
|
||||
val armed = ArrayList<Long>()
|
||||
schedule.replay("boot", { rang.add(it.id) }, { armed.add(it.id) })
|
||||
assertEquals(listOf(1L), rang)
|
||||
assertEquals(listOf(3L), armed)
|
||||
assertEquals(listOf(1L, 3L), schedule.entries().map { it.id })
|
||||
val log = schedule.readLog()
|
||||
assertTrue(log.contains("$now missed timer 2 (boot)"), log)
|
||||
assertTrue(log.endsWith("$now rescheduled 1 (boot)"), log)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun stalenessComparesSoonestPendingWithSystemAlarm() {
|
||||
assertFalse(schedule.looksStale(null))
|
||||
schedule.put(AlarmEntry(1, now - 1, "past", "", null, 10))
|
||||
assertNull(schedule.soonestPendingAt())
|
||||
schedule.put(AlarmEntry(2, now + 50_000, "", "", null, 10))
|
||||
schedule.put(AlarmEntry(3, now + 20_000, "", "", null, 10))
|
||||
assertEquals(now + 20_000, schedule.soonestPendingAt())
|
||||
assertTrue(schedule.looksStale(null))
|
||||
assertTrue(schedule.looksStale(now + 30_000))
|
||||
assertFalse(schedule.looksStale(now + 20_500))
|
||||
assertFalse(schedule.looksStale(now + 5_000))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun journalAppendsAndDrains() {
|
||||
schedule.appendJournal(5, "snooze", 10)
|
||||
now += 1000
|
||||
schedule.appendJournal(6, "dismiss", 0)
|
||||
assertEquals("""[{"id":5,"action":"snooze","minutes":10,"at":${now - 1000}},{"id":6,"action":"dismiss","minutes":0,"at":$now}]""", schedule.drainJournal())
|
||||
assertEquals("[]", schedule.drainJournal())
|
||||
val log = schedule.readLog().lines()
|
||||
assertEquals("${now - 1000} phone snooze 5 (10 min)", log[0])
|
||||
assertEquals("$now phone dismiss 6", log[1])
|
||||
}
|
||||
|
||||
@Test
|
||||
fun logKeepsOnlyTheLastLines() {
|
||||
for (index in 0 until AlarmSchedule.LOG_LINES + 5) schedule.log("line $index")
|
||||
val lines = schedule.readLog().lines()
|
||||
assertEquals(AlarmSchedule.LOG_LINES, lines.size)
|
||||
assertEquals("$now line 5", lines.first())
|
||||
assertEquals("$now line ${AlarmSchedule.LOG_LINES + 4}", lines.last())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun glassesGateRequiresFreshWornConnectedNotCharging() {
|
||||
var clock = 10_000L
|
||||
val gate = GlassesStatusGate { clock }
|
||||
assertFalse(gate.canCarryAlarm())
|
||||
assertEquals("no glasses status", gate.description())
|
||||
gate.set(connected = true, worn = true, charging = false)
|
||||
assertTrue(gate.canCarryAlarm())
|
||||
assertEquals("glasses worn", gate.description())
|
||||
gate.set(connected = true, worn = false, charging = false)
|
||||
assertEquals("glasses not worn", gate.description())
|
||||
gate.set(connected = true, worn = true, charging = true)
|
||||
assertEquals("glasses charging", gate.description())
|
||||
gate.set(connected = false, worn = true, charging = false)
|
||||
assertEquals("glasses not connected", gate.description())
|
||||
gate.set(connected = true, worn = true, charging = false)
|
||||
clock += GlassesStatusGate.MAX_AGE_MS + 1
|
||||
assertFalse(gate.canCarryAlarm())
|
||||
assertEquals("glasses status stale", gate.description())
|
||||
assertEquals(0x7fffffff and (0x1_0000_0002L xor 1L).toInt(), AlarmSchedule.requestCode(0x1_0000_0002L))
|
||||
assertNotNull(AlarmEntry.fromJson(Json.parse("""{"id":1,"at":2}""")))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
import kotlin.math.abs
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertFailsWith
|
||||
import kotlin.test.assertNotNull
|
||||
import kotlin.test.assertNull
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class AudioHelpersTest {
|
||||
@Test
|
||||
fun cutPointLandsInQuietestWindowForFloatsAndShorts() {
|
||||
// 1000 loud samples with a silent gap at 700..760; search the last 500 with a 40 window.
|
||||
val floats = FloatArray(1000) { if (it in 700 until 760) 0f else 0.5f }
|
||||
val cut = AudioSegmentation.quietestCutPoint(floats, floats.size, 500, 40)
|
||||
assertTrue(cut in 700..760, "cut=$cut")
|
||||
val shorts = ShortArray(1100) { if (it - 100 in 700 until 760) 0 else 16000 }
|
||||
assertEquals(cut, AudioSegmentation.quietestCutPoint(shorts, 100, 1000, 500, 40))
|
||||
// Too short to search: returns the length unchanged.
|
||||
assertEquals(30, AudioSegmentation.quietestCutPoint(FloatArray(30) { 0.1f }, 30, 500, 40))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun normalizePeakScalesUpButNeverDown() {
|
||||
val quiet = floatArrayOf(0.01f, -0.02f, 0.005f)
|
||||
AudioSegmentation.normalizePeak(quiet, 0.9f, 30f)
|
||||
assertTrue(abs(quiet[1] + 0.6f) < 1e-6f, "capped at maxGain 30: ${quiet[1]}")
|
||||
val loud = floatArrayOf(0.95f, -0.5f)
|
||||
AudioSegmentation.normalizePeak(loud, 0.9f, 30f)
|
||||
assertEquals(0.95f, loud[0])
|
||||
val silent = FloatArray(4)
|
||||
AudioSegmentation.normalizePeak(silent, 0.9f, 30f)
|
||||
assertEquals(0f, AudioSegmentation.peakAmplitude(silent))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun pcmConversionsRoundTrip() {
|
||||
val samples = shortArrayOf(0, 1, -1, 32767, -32768, 256)
|
||||
val le = AudioSegmentation.pcm16ToLittleEndian(samples, 0, samples.size)
|
||||
assertEquals(listOf(0, 0, 1, 0, 0xff, 0xff, 0xff, 0x7f, 0, 0x80, 0, 1), le.map { it.toInt() and 0xff })
|
||||
val floats = AudioSegmentation.pcm16leToFloat(le)
|
||||
assertEquals(AudioSegmentation.pcm16ToFloat(samples, 0, samples.size).toList(), floats.toList())
|
||||
assertEquals(32767 / 32768.0f, floats[3])
|
||||
assertEquals(-1f, floats[4])
|
||||
assertEquals(2.0, AudioSegmentation.rms(shortArrayOf(2, -2, 2, -2), 0, 4))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun voicePrintMathAndBlobCodec() {
|
||||
val v = floatArrayOf(3f, 4f)
|
||||
VoicePrint.l2Normalize(v)
|
||||
assertEquals(listOf(0.6f, 0.8f), v.toList())
|
||||
assertTrue(abs(VoicePrint.dot(v, v) - 1.0) < 1e-6)
|
||||
assertNull(VoicePrint.l2Normalize(null))
|
||||
assertTrue(VoicePrint.isLongEnoughToEmbed(16000, 16000))
|
||||
assertTrue(!VoicePrint.isLongEnoughToEmbed(15999, 16000))
|
||||
|
||||
val blob = SpeakerEmbeddings.floatsToBlob(floatArrayOf(1.5f, -2f))
|
||||
assertEquals(listOf(0, 0, 0xc0, 0x3f, 0, 0, 0, 0xc0), blob.map { it.toInt() and 0xff })
|
||||
assertEquals(listOf(1.5f, -2f), SpeakerEmbeddings.blobToFloats(blob)!!.toList())
|
||||
assertNull(SpeakerEmbeddings.blobToFloats(byteArrayOf(1, 2, 3)))
|
||||
assertNull(SpeakerEmbeddings.blobToFloats(null))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun centroidUpdatesAndBlends() {
|
||||
val first = SpeakerEmbeddings.runningMean(null, 0, floatArrayOf(1f, 0f), 5)
|
||||
assertEquals(1, first.count)
|
||||
assertEquals(listOf(1f, 0f), first.embedding.toList())
|
||||
val second = SpeakerEmbeddings.runningMean(first.embedding, first.count, floatArrayOf(0f, 1f), 5)
|
||||
assertEquals(2, second.count)
|
||||
assertTrue(abs(second.embedding[0] - second.embedding[1]) < 1e-6f)
|
||||
assertTrue(abs(VoicePrint.dot(second.embedding, second.embedding) - 1.0) < 1e-6)
|
||||
// The cap limits the existing weight: count 100 capped at 1 behaves like an equal blend.
|
||||
val capped = SpeakerEmbeddings.runningMean(floatArrayOf(1f, 0f), 100, floatArrayOf(0f, 1f), 1)
|
||||
assertTrue(abs(capped.embedding[0] - capped.embedding[1]) < 1e-6f)
|
||||
assertEquals(101, capped.count)
|
||||
// Mismatched sizes restart the centroid.
|
||||
assertEquals(1, SpeakerEmbeddings.runningMean(floatArrayOf(1f), 4, floatArrayOf(0f, 1f), 5).count)
|
||||
|
||||
val blended = SpeakerEmbeddings.blend(floatArrayOf(1f, 0f), 1, floatArrayOf(0f, 1f), 3)
|
||||
assertNotNull(blended)
|
||||
assertTrue(blended[1] > blended[0])
|
||||
assertEquals(listOf(0f, 1f), SpeakerEmbeddings.blend(null, 0, floatArrayOf(0f, 1f), 3)!!.toList())
|
||||
assertEquals(listOf(1f, 0f), SpeakerEmbeddings.blend(floatArrayOf(1f, 0f), 2, null, 0)!!.toList())
|
||||
assertNull(SpeakerEmbeddings.blend(null, 0, null, 0))
|
||||
assertTrue(SpeakerEmbeddings.shouldAdoptInsights(targetHasInsights = false, sourceHasInsights = true))
|
||||
assertTrue(!SpeakerEmbeddings.shouldAdoptInsights(targetHasInsights = true, sourceHasInsights = true))
|
||||
}
|
||||
|
||||
private fun wav(dataSize: Int, extraChunk: Boolean = false, channels: Int = 1, bits: Int = 16, format: Int = 1): ByteArray {
|
||||
val out = ByteSink()
|
||||
fun u16(v: Int) { out.write(v); out.write(v ushr 8) }
|
||||
fun u32(v: Int) { u16(v); u16(v ushr 16) }
|
||||
out.write("RIFF".encodeToByteArray()); u32(0); out.write("WAVE".encodeToByteArray())
|
||||
if (extraChunk) { out.write("LIST".encodeToByteArray()); u32(4); u32(0) }
|
||||
out.write("fmt ".encodeToByteArray()); u32(18)
|
||||
u16(format); u16(channels); u32(16000); u32(16000 * channels * 2); u16(channels * 2); u16(bits); u16(0)
|
||||
out.write("data".encodeToByteArray()); u32(dataSize)
|
||||
return out.toByteArray()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun wavHeaderParsesChunksAndRecoversZeroSize() {
|
||||
val header = wav(4000)
|
||||
val info = WavPcmReader.parseHeader(header, header.size + 4000L)
|
||||
assertEquals(16000, info.sampleRate)
|
||||
assertEquals(1, info.channels)
|
||||
assertEquals(16, info.bitsPerSample)
|
||||
assertEquals(header.size.toLong(), info.dataOffset)
|
||||
assertEquals(4000L, info.dataBytes)
|
||||
|
||||
val withList = wav(100, extraChunk = true)
|
||||
assertEquals(withList.size.toLong(), WavPcmReader.parseHeader(withList, withList.size + 100L).dataOffset)
|
||||
|
||||
val unpatched = wav(0)
|
||||
assertEquals(1234L, WavPcmReader.parseHeader(unpatched, unpatched.size + 1234L).dataBytes)
|
||||
|
||||
assertFailsWith<IllegalArgumentException> { WavPcmReader.parseHeader("RIFX".encodeToByteArray() + ByteArray(20), 24) }
|
||||
assertFailsWith<IllegalArgumentException> { WavPcmReader.parseHeader(wav(10, bits = 8), 100) }
|
||||
assertFailsWith<IllegalArgumentException> { WavPcmReader.parseHeader(wav(10, format = 3), 100) }
|
||||
assertFailsWith<IllegalArgumentException> { WavPcmReader.parseHeader(wav(10).copyOf(20), 100) }
|
||||
// The shared header matches the shared writer.
|
||||
val written = BinaryEncoding.wavHeader(640, 16000, 1)
|
||||
val parsed = WavPcmReader.parseHeader(written, 44 + 640L)
|
||||
assertEquals(44L, parsed.dataOffset)
|
||||
assertEquals(640L, parsed.dataBytes)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertNotNull
|
||||
import kotlin.test.assertNull
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class CaptionEngineCoreTest {
|
||||
private class Sink : CaptionSegmentSink {
|
||||
val starts = mutableListOf<Long>()
|
||||
val utterances = mutableListOf<List<Any>>()
|
||||
override fun onSpeechStart(startMs: Long) { starts.add(startMs) }
|
||||
override fun onUtterance(samples: ShortArray, length: Int, startMs: Long, endMs: Long, peakRms: Double) {
|
||||
utterances.add(listOf(length, startMs, endMs, samples[0].toInt()))
|
||||
}
|
||||
}
|
||||
|
||||
private fun chunk(amplitude: Int, samples: Int = 800): ShortArray =
|
||||
ShortArray(samples) { if (it % 2 == 0) amplitude.toShort() else (-amplitude).toShort() }
|
||||
|
||||
@Test
|
||||
fun segmenterUsesPreRollOnsetReleaseAndSilence() {
|
||||
val sink = Sink()
|
||||
val segmenter = CaptionSegmenter(sink)
|
||||
repeat(10) { segmenter.processChunk(chunk(100), 800) } // 500 ms idle, noise floor ~100 -> threshold 220
|
||||
repeat(10) { segmenter.processChunk(chunk(3000), 800) } // 500 ms speech
|
||||
assertEquals(listOf(100L), sink.starts) // 500 ms - 400 ms pre-roll
|
||||
repeat(15) { segmenter.processChunk(chunk(100), 800) } // 750 ms < 800 ms silence: still open
|
||||
assertTrue(sink.utterances.isEmpty())
|
||||
segmenter.processChunk(chunk(100), 800) // 800 ms of silence closes it
|
||||
assertEquals(1, sink.utterances.size)
|
||||
val (length, startMs, endMs, first) = sink.utterances[0]
|
||||
assertEquals(100L, startMs)
|
||||
assertEquals(6400 + 10 * 800 + 16 * 800, length) // pre-roll + speech + trailing silence
|
||||
assertEquals(startMs as Long + (length as Int) * 1000L / 16000, endMs)
|
||||
assertEquals(100, first) // the pre-roll audio leads the utterance
|
||||
assertTrue(segmenter.isInUtterance.not())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun segmenterDropsTooShortUtterancesAndCapsLength() {
|
||||
val sink = Sink()
|
||||
val segmenter = CaptionSegmenter(sink)
|
||||
segmenter.silenceMs = 100 // clamps to 200
|
||||
segmenter.processChunk(chunk(10), 800) // seeds the noise floor; becomes the 50 ms pre-roll
|
||||
segmenter.processChunk(chunk(3000), 800) // onset
|
||||
repeat(4) { segmenter.processChunk(chunk(0), 800) } // 200 ms silence
|
||||
assertEquals(1, sink.starts.size)
|
||||
assertTrue(sink.utterances.isEmpty()) // 300 ms < 350 ms minimum
|
||||
repeat(400) { segmenter.processChunk(chunk(3000), 800) } // 20 s of speech: capped at 15 s
|
||||
assertEquals(1, sink.utterances.size)
|
||||
assertEquals(16000 * 15, sink.utterances[0][0])
|
||||
segmenter.flush()
|
||||
assertEquals(2, sink.utterances.size) // the remainder is flushed on stop
|
||||
}
|
||||
|
||||
@Test
|
||||
fun utteranceDecoderSplitsLongAudioAndEmbedsTenSeconds() {
|
||||
val calls = mutableListOf<Int>()
|
||||
val transcriber = object : OfflineTranscriber {
|
||||
override fun recognize(samples: FloatArray, count: Int): String { calls.add(count); return if (calls.size == 2) ", b" else "a" }
|
||||
override fun release() {}
|
||||
}
|
||||
var embedded = 0
|
||||
val embedder = object : SpeakerEmbedder {
|
||||
override fun embed(pcm16le: ByteArray, sampleRate: Int): FloatArray? { embedded = pcm16le.size; return floatArrayOf(1f) }
|
||||
override fun release() {}
|
||||
}
|
||||
val decoder = UtteranceDecoder(transcriber, embedder)
|
||||
val utterance = ShortArray(16000 * 12) { if (it % 2 == 0) 2000 else -2000 }
|
||||
assertEquals("a, b", decoder.recognize(utterance, utterance.size))
|
||||
assertEquals(2, calls.size)
|
||||
assertTrue(calls[0] <= UtteranceDecoder.DECODE_SEGMENT_MAX_SAMPLES)
|
||||
assertEquals(utterance.size, calls.sum())
|
||||
assertNotNull(decoder.embed(utterance, utterance.size))
|
||||
assertEquals(16000 * 10 * 2, embedded)
|
||||
assertEquals("", UtteranceDecoder(null, null).recognize(utterance, utterance.size))
|
||||
assertNull(UtteranceDecoder(null, null).embed(utterance, utterance.size))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun captionSessionRunsQueueThroughSegmenterAndDecoder() {
|
||||
val startLatch = Latch(1, testPlatform())
|
||||
val utteranceLatch = Latch(1, testPlatform())
|
||||
val events = mutableListOf<String>()
|
||||
val listener = object : FaceclawCaptionEngineListener {
|
||||
override fun onUtterance(text: String?, embedding: FloatArray?, startMs: Long, endMs: Long, peakRms: Double) {
|
||||
events.add("utterance:$text:${embedding?.size}:$startMs"); utteranceLatch.countDown()
|
||||
}
|
||||
override fun onSpeechStart(startMs: Long) { events.add("start:$startMs"); startLatch.countDown() }
|
||||
override fun onStatus(status: String?) { events.add("status:$status") }
|
||||
}
|
||||
val host = object : CaptionHost {
|
||||
override val dispatcher = CallbackDispatcher { it() }
|
||||
override fun loadTranscriber(modelDir: String): OfflineTranscriber? = if (modelDir == "models") object : OfflineTranscriber {
|
||||
override fun recognize(samples: FloatArray, count: Int): String = "hi"
|
||||
override fun release() {}
|
||||
} else null
|
||||
override fun loadEmbedder(modelPath: String): SpeakerEmbedder? = object : SpeakerEmbedder {
|
||||
override fun embed(pcm16le: ByteArray, sampleRate: Int): FloatArray? = floatArrayOf(0.5f, 0.5f)
|
||||
override fun release() {}
|
||||
}
|
||||
}
|
||||
val session = CaptionSession(host, testPlatform(), "caption-test") { m, e -> error("$m: $e") }
|
||||
session.setListener(listener)
|
||||
session.setAsrModelDir("models")
|
||||
session.setSpeakerModelPath("speaker")
|
||||
session.setSilenceMs(300)
|
||||
session.acceptPcm(ByteArray(1600)) // ignored before start
|
||||
session.start()
|
||||
val loud = AudioSegmentation.pcm16ToLittleEndian(chunk(3000), 0, 800)
|
||||
val quiet = AudioSegmentation.pcm16ToLittleEndian(chunk(0), 0, 800)
|
||||
val idle = AudioSegmentation.pcm16ToLittleEndian(chunk(10), 0, 800)
|
||||
repeat(2) { session.acceptPcm(idle) } // seeds the noise floor and fills 100 ms of pre-roll
|
||||
repeat(10) { session.acceptPcm(loud) }
|
||||
assertTrue(startLatch.await(5000))
|
||||
repeat(8) { session.acceptPcm(quiet) }
|
||||
assertTrue(utteranceLatch.await(5000))
|
||||
session.stop()
|
||||
session.stop() // idempotent
|
||||
assertEquals("status:Loading caption model...", events[0])
|
||||
assertEquals("status:Captions listening...", events[1])
|
||||
assertEquals("start:0", events[2])
|
||||
assertEquals("utterance:hi:2:0", events[3])
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertNull
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class EvenHubAssetServerTest {
|
||||
@Test
|
||||
fun resolvesPathsWithIndexDefaultAndEscapeChecks() {
|
||||
assertEquals("index.html", EvenHubAssetServer.resolve(null)!!.relativePath)
|
||||
assertEquals("index.html", EvenHubAssetServer.resolve("")!!.relativePath)
|
||||
assertEquals("index.html", EvenHubAssetServer.resolve("/")!!.relativePath)
|
||||
assertEquals("assets/app.js", EvenHubAssetServer.resolve("/assets/app.js")!!.relativePath)
|
||||
assertEquals("assets/app.js", EvenHubAssetServer.resolve("/assets/./x/../app.js")!!.relativePath)
|
||||
assertNull(EvenHubAssetServer.resolve("/../secret"))
|
||||
assertNull(EvenHubAssetServer.resolve("/a/../../secret"))
|
||||
assertNull(EvenHubAssetServer.resolve("/.."))
|
||||
assertTrue(EvenHubAssetServer.resolve("/index.html")!!.isHtml)
|
||||
assertTrue(EvenHubAssetServer.isInsideRoot("/apps/x", "/apps/x/index.html"))
|
||||
assertTrue(EvenHubAssetServer.isInsideRoot("/apps/x/", "/apps/x/a/b"))
|
||||
assertEquals(false, EvenHubAssetServer.isInsideRoot("/apps/x", "/apps/xy/index.html"))
|
||||
assertEquals(false, EvenHubAssetServer.isInsideRoot("/apps/x", "/apps/index.html"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun mimeTableCoversBothPlatformsTables() {
|
||||
assertEquals("text/html", EvenHubAssetServer.mimeTypeFor("Index.HTM"))
|
||||
assertEquals("text/javascript", EvenHubAssetServer.mimeTypeFor("bundle.mjs"))
|
||||
assertEquals("application/wasm", EvenHubAssetServer.mimeTypeFor("core.wasm"))
|
||||
assertEquals("font/woff2", EvenHubAssetServer.mimeTypeFor("a.woff2"))
|
||||
assertEquals("image/svg+xml", EvenHubAssetServer.mimeTypeFor("icon.svg"))
|
||||
assertEquals("text/plain", EvenHubAssetServer.mimeTypeFor("bundle.js.map"))
|
||||
assertEquals("application/octet-stream", EvenHubAssetServer.mimeTypeFor("blob"))
|
||||
assertEquals("application/octet-stream", EvenHubAssetServer.mimeTypeFor("weird.xyz"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun injectsShimAfterHeadOrAtStart() {
|
||||
assertEquals(
|
||||
"<!doctype html><HEAD lang=\"x\"><script>S</script><title>t</title></HEAD>",
|
||||
EvenHubAssetServer.injectIntoHtml("<!doctype html><HEAD lang=\"x\"><title>t</title></HEAD>", "S"),
|
||||
)
|
||||
assertEquals("<script>S</script><body>x</body>", EvenHubAssetServer.injectIntoHtml("<body>x</body>", "S"))
|
||||
assertEquals("<script>S</script><head", EvenHubAssetServer.injectIntoHtml("<head", "S"), "unterminated head tag")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertFailsWith
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class FirmwareImageTest {
|
||||
private fun putU32(buf: ByteArray, offset: Int, value: Long) {
|
||||
for (i in 0 until 4) buf[offset + i] = ((value ushr (8 * i)) and 0xff).toByte()
|
||||
}
|
||||
|
||||
private class Component(val name: String, val payload: ByteArray)
|
||||
|
||||
/** Builds a checksum-fixed container; [tamper] runs before the CRCs are recorded. */
|
||||
private fun container(components: List<Component>, tamper: (ByteArray) -> Unit = {}): ByteArray {
|
||||
val count = components.size
|
||||
val toc = FirmwareImage.HEADER_SIZE + count * FirmwareImage.TOC_ENTRY_SIZE
|
||||
var offset = toc
|
||||
val offsets = components.map { c -> offset.also { offset += FirmwareImage.SUBHEADER_SIZE + c.payload.size } }
|
||||
val img = ByteArray(offset)
|
||||
putU32(img, 8, count.toLong())
|
||||
components.forEachIndexed { i, c ->
|
||||
val off = offsets[i]
|
||||
putU32(img, FirmwareImage.HEADER_SIZE + i * 16 + 4, off.toLong())
|
||||
putU32(img, off + 8, c.payload.size.toLong())
|
||||
c.name.encodeToByteArray().copyInto(img, off + FirmwareImage.NAME_OFFSET)
|
||||
c.payload.copyInto(img, off + FirmwareImage.SUBHEADER_SIZE)
|
||||
}
|
||||
tamper(img)
|
||||
components.forEachIndexed { i, c ->
|
||||
val off = offsets[i]
|
||||
val crc = FirmwareImage.crc32cUnsigned(img, off + FirmwareImage.SUBHEADER_SIZE, c.payload.size)
|
||||
putU32(img, FirmwareImage.HEADER_SIZE + i * 16 + 12, crc)
|
||||
putU32(img, off + 12, crc)
|
||||
}
|
||||
return img
|
||||
}
|
||||
|
||||
private fun mainApp(size: Int): ByteArray {
|
||||
val payload = ByteArray(size) { (it * 7).toByte() }
|
||||
putU32(payload, 0, size.toLong())
|
||||
putU32(payload, 0x14, FirmwareImage.APP_LOAD_ADDR)
|
||||
return payload
|
||||
}
|
||||
|
||||
private fun validComponents(): List<Component> =
|
||||
listOf(
|
||||
Component("ota/bootloader.bin", ByteArray(300) { it.toByte() }),
|
||||
Component("ota/a.bin", ByteArray(64) { 1 }),
|
||||
Component("ota/b.bin", ByteArray(128) { 2 }),
|
||||
Component("ota/c.bin", ByteArray(32) { 3 }),
|
||||
Component(FirmwareImage.REQUIRED_SEGMENT, mainApp(600)),
|
||||
)
|
||||
|
||||
@Test
|
||||
fun crc32cMatchesKnownVector() {
|
||||
// Castagnoli polynomial, MSB-first, init 0, no reflection or final xor (g2flash.py
|
||||
// crc32c_msb); vectors from an independent Python implementation of that definition.
|
||||
assertEquals(0xc052a8c8L, FirmwareImage.crc32cUnsigned("123456789".encodeToByteArray()))
|
||||
assertEquals(0xd7b91914L, FirmwareImage.crc32cUnsigned(ByteArray(256) { it.toByte() }))
|
||||
assertEquals(0L, FirmwareImage.crc32cUnsigned(ByteArray(0)))
|
||||
assertEquals(FirmwareImage.crc32cUnsigned("23456789".encodeToByteArray()), FirmwareImage.crc32cUnsigned("123456789".encodeToByteArray(), 1, 8))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun validImageParsesAllComponents() {
|
||||
val segs = FirmwareImage.validate(container(validComponents()))
|
||||
assertEquals(5, segs.size)
|
||||
assertEquals(listOf(300, 64, 128, 32, 600), segs.map { it.ps })
|
||||
assertEquals(FirmwareImage.REQUIRED_SEGMENT, segs[4].name)
|
||||
assertTrue(segs.zipWithNext().all { (a, b) -> a.end <= b.off })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun rejectsStaleCrc() {
|
||||
val img = container(validComponents())
|
||||
img[img.size - 1] = (img[img.size - 1] + 1).toByte()
|
||||
assertTrue(assertFailsWith<IllegalStateException> { FirmwareImage.validate(img) }.message!!.contains("CRC32C"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun rejectsOverlappingComponents() {
|
||||
val img = container(validComponents()) { buf ->
|
||||
// Point component 1's TOC entry at component 0, so both claim the same bytes.
|
||||
val off0 = FirmwareImage.readU32(buf, FirmwareImage.HEADER_SIZE + 4)
|
||||
putU32(buf, FirmwareImage.HEADER_SIZE + 16 + 4, off0)
|
||||
}
|
||||
assertTrue(assertFailsWith<IllegalStateException> { FirmwareImage.parseSegments(img) }.message!!.contains("overlap"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun rejectsTruncatedPayload() {
|
||||
val img = container(validComponents())
|
||||
val truncated = img.copyOf(img.size - 10)
|
||||
assertTrue(assertFailsWith<IllegalStateException> { FirmwareImage.parseSegments(truncated) }.message!!.contains("truncated"))
|
||||
assertFailsWith<IllegalStateException> { FirmwareImage.parseSegments(ByteArray(0x10)) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun rejectsWrongComponentCountAndMissingMain() {
|
||||
val four = validComponents().dropLast(1)
|
||||
assertTrue(assertFailsWith<IllegalStateException> { FirmwareImage.validate(container(four)) }.message!!.contains("components"))
|
||||
val noMain = validComponents().dropLast(1) + Component("ota/other.bin", ByteArray(40))
|
||||
assertTrue(assertFailsWith<IllegalStateException> { FirmwareImage.validate(container(noMain)) }.message!!.contains("not found"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun rejectsMainAppBeyondMramCeiling() {
|
||||
val ceiling = (FirmwareImage.APP_MAX_END - FirmwareImage.APP_LOAD_ADDR + FirmwareImage.APP_PREAMBLE).toInt()
|
||||
val fits = validComponents().dropLast(1) + Component(FirmwareImage.REQUIRED_SEGMENT, mainApp(ceiling))
|
||||
FirmwareImage.validate(container(fits))
|
||||
val tooBig = validComponents().dropLast(1) + Component(FirmwareImage.REQUIRED_SEGMENT, mainApp(ceiling + 1))
|
||||
assertTrue(assertFailsWith<IllegalStateException> { FirmwareImage.validate(container(tooBig)) }.message!!.contains("MRAM"))
|
||||
val badAddr = validComponents().dropLast(1) + Component(FirmwareImage.REQUIRED_SEGMENT, mainApp(600).also { putU32(it, 0x14, 0x1234L) })
|
||||
assertTrue(assertFailsWith<IllegalStateException> { FirmwareImage.validate(container(badAddr)) }.message!!.contains("load address"))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertNull
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class FoundationTest {
|
||||
@Test
|
||||
fun jsonRoundTripsObjectsArraysAndNumbers() {
|
||||
val text = """{"a":1,"b":[true,null,"x\ny"],"c":{"d":2.5,"e":-3}}"""
|
||||
val value = Json.parseObject(text)
|
||||
assertEquals(1, value["a"].asInt())
|
||||
assertEquals(listOf(true, null, "x\ny"), value["b"].asArray())
|
||||
assertEquals(2.5, value["c"].asObject()!!["d"].asDouble())
|
||||
assertEquals(-3L, value["c"].asObject()!!["e"].asLong())
|
||||
assertEquals(text, Json.write(value))
|
||||
assertEquals("[1,2,\"q\\\"\"]", Json.write(listOf(1, 2L, "q\"")))
|
||||
assertEquals("1", JsonNumber.of(1.0).text)
|
||||
assertEquals("1.5", JsonNumber.of(1.5).text)
|
||||
assertNull("x".asInt())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun sha256MatchesKnownVector() {
|
||||
assertEquals("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", sha256Hex("abc".encodeToByteArray()))
|
||||
val incremental = Sha256Digest()
|
||||
incremental.update("a".encodeToByteArray())
|
||||
incremental.update("bc".encodeToByteArray())
|
||||
assertEquals(sha256("abc".encodeToByteArray()).toList(), incremental.digest().toList())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun latchAndQueueHandOffAcrossThreads() {
|
||||
val platform = testPlatform()
|
||||
val latch = Latch(1, platform)
|
||||
val queue = BlockingQueue<String>(platform)
|
||||
assertFalse(latch.await(20))
|
||||
startThread("foundation-test", true) {
|
||||
sleepMs(20)
|
||||
queue.put("hello")
|
||||
latch.countDown()
|
||||
}
|
||||
assertEquals("hello", queue.poll(2000))
|
||||
assertTrue(latch.await(2000))
|
||||
assertNull(queue.poll(10))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun interruptibleSleepConsumesEarlyInterrupt() {
|
||||
val platform = testPlatform()
|
||||
val sleep = InterruptibleSleep(platform)
|
||||
sleep.interrupt()
|
||||
val start = platform.elapsedRealtimeMs()
|
||||
assertFalse(sleep.sleep(500))
|
||||
assertTrue(platform.elapsedRealtimeMs() - start < 400)
|
||||
assertTrue(sleep.sleep(5))
|
||||
startThread("foundation-wake", true) { sleepMs(30); sleep.interrupt() }
|
||||
assertFalse(sleep.sleep(5000))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun wallClockFormatsPattern() {
|
||||
val text = formatLocalTime(0L, "yyyy")
|
||||
assertTrue(text == "1970" || text == "1969", text)
|
||||
assertTrue(currentTimeMillis() > 1_600_000_000_000L)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class FrameTimingsCoreTest {
|
||||
private class FakeClock : ProtocolPlatform by testPlatform() {
|
||||
var nowMs = 1_000L
|
||||
override fun elapsedRealtimeMs(): Long = nowMs
|
||||
}
|
||||
|
||||
private fun core(clock: FakeClock, logs: MutableList<String> = ArrayList()) = FrameTimingsCore(
|
||||
platform = clock,
|
||||
wallClockMs = { 1_700_000_000_000L + clock.nowMs },
|
||||
threadName = { "t" },
|
||||
formatWallClock = { "wall@$it" },
|
||||
logInfo = { logs.add("I $it") },
|
||||
logWarn = { logs.add("W $it") },
|
||||
)
|
||||
|
||||
@Test
|
||||
fun exportRendersTreeAgainstRootClockByteForByte() {
|
||||
val clock = FakeClock()
|
||||
val logs = ArrayList<String>()
|
||||
val core = core(clock, logs)
|
||||
val root = core.startFrame("input:sys-event type=0")
|
||||
clock.nowMs += 5
|
||||
core.annotate(root, "fg=terminal")
|
||||
core.log(root, "input event received on JS side")
|
||||
clock.nowMs += 1
|
||||
core.spanStart(root, "handle-input")
|
||||
clock.nowMs += 10
|
||||
val child = core.startFrame("render:shell", root)
|
||||
clock.nowMs += 4
|
||||
core.spanEnd(root, "handle-input")
|
||||
core.spanEnd(root, "missing")
|
||||
core.spanStart(child, "paint")
|
||||
clock.nowMs += 30
|
||||
core.finishFrame(child, "discarded: superseded")
|
||||
clock.nowMs += 50
|
||||
core.finishFrame(root, "sent")
|
||||
core.finishFrame(root, "sent") // ignored: first finish wins
|
||||
core.log(root, "late line is dropped")
|
||||
clock.nowMs += 100
|
||||
|
||||
assertEquals(listOf(
|
||||
"I frame#2 [render:shell] -> discarded: superseded in 34ms",
|
||||
"I frame#1 [input:sys-event type=0 fg=terminal] -> sent in 100ms",
|
||||
), logs)
|
||||
assertEquals("frames started=2 sent=1 discarded=1 timeout=0 | input-to-display p50=100ms p90=100ms p99=100ms max=100ms", core.statsSummary())
|
||||
|
||||
val expected = """
|
||||
|FrameTimings export at wall@1700000001200
|
||||
|frames started=2 sent=1 discarded=1 timeout=0 open=0
|
||||
|input-to-display latency (last 1): p50=100ms p90=100ms p99=100ms max=100ms
|
||||
|
|
||||
|Frames are trees: an input event's own frame is the root, and the renders it
|
||||
|caused are indented under it with offsets measured from the root's start.
|
||||
|
|
||||
|=== slowest frames that reached the glasses ===
|
||||
|frame#1 [input:sys-event type=0 fg=terminal] started wall@1700000001000 duration 100ms (tree 100ms) outcome sent
|
||||
| +5ms [t] input event received on JS side
|
||||
| +6ms [t] span handle-input start
|
||||
| +16ms [t] spawned frame#2 (render:shell)
|
||||
| +20ms [t] span handle-input end (14ms)
|
||||
| +20ms [t] span missing end (start not recorded)
|
||||
| +100ms [t] finished: sent
|
||||
| frame#2 [render:shell] started +16ms duration 34ms outcome discarded: superseded
|
||||
| +20ms [t] span paint start
|
||||
| +50ms [t] span paint never ended (started at +4ms)
|
||||
| +50ms [t] finished: discarded: superseded
|
||||
|=== recent frames (oldest first) ===
|
||||
|frame#1 [input:sys-event type=0 fg=terminal] started wall@1700000001000 duration 100ms (tree 100ms) outcome sent
|
||||
| +5ms [t] input event received on JS side
|
||||
| +6ms [t] span handle-input start
|
||||
| +16ms [t] spawned frame#2 (render:shell)
|
||||
| +20ms [t] span handle-input end (14ms)
|
||||
| +20ms [t] span missing end (start not recorded)
|
||||
| +100ms [t] finished: sent
|
||||
| frame#2 [render:shell] started +16ms duration 34ms outcome discarded: superseded
|
||||
| +20ms [t] span paint start
|
||||
| +50ms [t] span paint never ended (started at +4ms)
|
||||
| +50ms [t] finished: discarded: superseded
|
||||
|""".trimMargin()
|
||||
assertTrue(core.isExportDirty)
|
||||
assertEquals(expected, core.takeExport())
|
||||
assertFalse(core.isExportDirty)
|
||||
assertEquals(null, core.takeExport())
|
||||
assertEquals(expected, core.buildExport())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun sweepFinishesStaleFramesAsTimeoutsAndIgnoresZeroAndUnknownIds() {
|
||||
val clock = FakeClock()
|
||||
val logs = ArrayList<String>()
|
||||
val core = core(clock, logs)
|
||||
val stale = core.startFrame("render:app")
|
||||
clock.nowMs += FrameTimingsCore.FRAME_TIMEOUT_MS - 1
|
||||
val fresh = core.startFrame("render:late")
|
||||
assertEquals(0, core.sweepTimedOut())
|
||||
clock.nowMs += 1
|
||||
assertEquals(1, core.sweepTimedOut())
|
||||
assertEquals(listOf("W frame#$stale [render:app] timed out after 30000ms without being finished"), logs)
|
||||
core.log(0, "ignored")
|
||||
core.log(999, "ignored")
|
||||
core.spanStart(0, "x")
|
||||
core.finishFrame(0, "sent")
|
||||
core.finishFrame(fresh, "sent")
|
||||
assertEquals("frames started=2 sent=1 discarded=0 timeout=1", core.statsSummary())
|
||||
val export = core.buildExport()
|
||||
assertTrue(export.contains("frame#$stale [render:app] started wall@"))
|
||||
assertTrue(export.contains("outcome timeout: never reported finished\n"))
|
||||
assertTrue(export.contains("render-to-display latency (last 1): p50=1ms p90=1ms p99=1ms max=1ms"))
|
||||
assertTrue(export.contains("open=0"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun lineCapAndNegativeOffsetsFormatLikeJava() {
|
||||
val clock = FakeClock()
|
||||
val core = core(clock)
|
||||
val root = core.startFrame("input:x")
|
||||
clock.nowMs -= 12 // a line stamped before the root's start still formats with a sign
|
||||
core.log(root, "early")
|
||||
clock.nowMs += 12
|
||||
repeat(250) { core.log(root, "line $it") }
|
||||
core.finishFrame(root, "sent")
|
||||
val export = core.buildExport()
|
||||
assertTrue(export.contains("\n -12ms [t] early\n"), export)
|
||||
// Printed once under "slowest" and once under "recent".
|
||||
assertEquals(2, Regex("\\.\\.\\. line cap reached").findAll(export).count())
|
||||
assertTrue(export.contains("line 198"))
|
||||
assertFalse(export.contains("line 199"))
|
||||
// Like the Java original, lines past the cap (including the finish line) are dropped.
|
||||
assertFalse(export.contains("finished: sent"))
|
||||
assertTrue(export.contains("outcome sent\n"))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,421 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
import kotlin.concurrent.Volatile
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertContentEquals
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertNotNull
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
private const val RIGHT = "AA:00:00:00:00:01"
|
||||
private const val LEFT = "AA:00:00:00:00:02"
|
||||
|
||||
/** The test platform with a clock the tests can jump forward (the real waits keep real time). */
|
||||
private class ClockPlatform(private val base: ProtocolPlatform) : ProtocolPlatform by base {
|
||||
@Volatile var offsetMs = 0L
|
||||
|
||||
override fun elapsedRealtimeMs(): Long = base.elapsedRealtimeMs() + offsetMs
|
||||
}
|
||||
|
||||
private fun waitUntil(timeoutMs: Long, condition: () -> Boolean): Boolean {
|
||||
val platform = testPlatform()
|
||||
val deadline = platform.elapsedRealtimeMs() + timeoutMs
|
||||
while (platform.elapsedRealtimeMs() < deadline) {
|
||||
if (condition()) return true
|
||||
sleepMs(10)
|
||||
}
|
||||
return condition()
|
||||
}
|
||||
|
||||
private fun ByteArray.containsSequence(needle: ByteArray): Boolean {
|
||||
if (needle.isEmpty() || size < needle.size) return false
|
||||
outer@ for (start in 0..size - needle.size) {
|
||||
for (i in needle.indices) if (this[start + i] != needle[i]) continue@outer
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
/** One logical write as the fake link saw it: the frames, the decoded pb, and the in-flight message (if any). */
|
||||
private class Write(
|
||||
val address: String,
|
||||
val sid: Int,
|
||||
val frames: List<ByteArray>,
|
||||
val pb: ByteArray,
|
||||
val kind: String?,
|
||||
val label: String?,
|
||||
val magic: Int,
|
||||
val message: ByteArray?,
|
||||
)
|
||||
|
||||
/**
|
||||
* A GATT link that records every write and, unless a kind is muted, answers it the way the
|
||||
* firmware would: EvenHub/settings/auth requests get a matching ack frame, CFW stream
|
||||
* messages get one ACK per lens.
|
||||
*/
|
||||
private class FakeLink(private val platform: ProtocolPlatform) : SessionLink {
|
||||
lateinit var core: GlassesSessionCore
|
||||
private val lock = platform.createLock()
|
||||
private val writesList = ArrayList<Write>()
|
||||
val muted = HashSet<String>()
|
||||
val connected = HashSet<String>()
|
||||
val disconnected = ArrayList<String>()
|
||||
@Volatile var closed = false
|
||||
var seq = 0
|
||||
|
||||
val writes: List<Write>
|
||||
get() = lock.withLock { writesList.toList() }
|
||||
|
||||
/** Decoded CFW image messages (the drive loop's debug-flags control rides the same stream and is excluded). */
|
||||
fun cfwMessages(): List<ByteArray> = writes.filter { it.sid == CfwTransport.SID && it.kind == "image" }.mapNotNull { it.message }
|
||||
|
||||
override fun connect(address: String, timeoutMs: Int): Boolean {
|
||||
connected.add(address)
|
||||
return true
|
||||
}
|
||||
|
||||
override fun requestHighPriority(address: String) {}
|
||||
|
||||
override fun requestMtu(address: String, mtu: Int, timeoutMs: Int): Boolean = true
|
||||
|
||||
override fun negotiatedMtu(address: String): Int = 247
|
||||
|
||||
override fun discoverServices(address: String, timeoutMs: Int): Boolean = true
|
||||
|
||||
override fun enableNotifications(address: String, characteristicUuid: String, enable: Boolean, timeoutMs: Int): Boolean = true
|
||||
|
||||
override fun writeFrames(address: String, characteristicUuid: String, frames: List<ByteArray>, mode: GattWriteMode, timeoutMs: Int): Boolean {
|
||||
val sid = frames[0][6].toInt() and 255
|
||||
val magic = if (sid == CfwTransport.SID) frames[0][2].toInt() and 255 else BleProtocol.parseFrame(frames[0]).msgSeq
|
||||
// The in-flight entry gives the label/kind and the decoded CFW message.
|
||||
val inFlight = core.monitor.withLock { core.inFlightMessages.firstOrNull { it.sid == sid && it.magic == magic && magic != 0 } }
|
||||
val pb = if (sid == CfwTransport.SID) ByteArray(0) else {
|
||||
val joined = frames.flatMap { frame -> frame.slice(8 until 8 + (frame[3].toInt() and 255)) }.toByteArray()
|
||||
joined.copyOf(maxOf(0, joined.size - 2))
|
||||
}
|
||||
val write = Write(address, sid, frames, pb, inFlight?.kind, inFlight?.label, magic, inFlight?.message?.copyOf())
|
||||
lock.withLock { writesList.add(write) }
|
||||
if (inFlight != null && inFlight.kind !in muted) {
|
||||
if (sid == CfwTransport.SID) {
|
||||
for (lens in 1..2) core.onNotification(address, BleProtocol.NOTIFY_CHAR_UUID, cfwAck(magic, lens, inFlight.message.size, inFlight.cfwChecksum))
|
||||
} else {
|
||||
core.onNotification(address, BleProtocol.NOTIFY_CHAR_UUID, ackFrame(sid, magic, pb))
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
private fun ackFrame(sid: Int, magic: Int, request: ByteArray): ByteArray {
|
||||
val body = if (sid == BleProtocol.SID_SECURITY_AUTH)
|
||||
BleProtocol.encodeVarintField(1, 4) + BleProtocol.encodeVarintField(2, magic) + byteArrayOf(0x1a, 0x00)
|
||||
else
|
||||
BleProtocol.encodeVarintField(1, 1) + BleProtocol.encodeVarintField(2, magic)
|
||||
return BleProtocol.framePb(body, sid, 0x00, seq++)[0]
|
||||
}
|
||||
|
||||
override fun disconnect(address: String) {
|
||||
disconnected.add(address)
|
||||
connected.remove(address)
|
||||
}
|
||||
|
||||
override fun close() {
|
||||
closed = true
|
||||
}
|
||||
|
||||
override fun isBonded(address: String): Boolean = true
|
||||
|
||||
override fun prepareBenchmarkLink(address: String, mode: Int) {}
|
||||
|
||||
override fun recordDisplayFrameSent() {}
|
||||
}
|
||||
|
||||
private fun cfwAck(streamId: Int, lens: Int, size: Int, checksum: Int): ByteArray {
|
||||
val packet = ByteArray(19)
|
||||
packet[0] = 0xaa.toByte()
|
||||
packet[1] = 0x12
|
||||
packet[3] = 11
|
||||
packet[4] = 1
|
||||
packet[5] = 1
|
||||
packet[6] = CfwTransport.SID.toByte()
|
||||
packet[8] = 1
|
||||
packet[9] = streamId.toByte()
|
||||
packet[12] = lens.toByte()
|
||||
packet[13] = size.toByte()
|
||||
packet[14] = (size ushr 8).toByte()
|
||||
packet[15] = checksum.toByte()
|
||||
packet[16] = (checksum ushr 8).toByte()
|
||||
val crc = CfwTransport.crc(packet.copyOfRange(8, 17))
|
||||
packet[17] = crc.toByte()
|
||||
packet[18] = (crc ushr 8).toByte()
|
||||
return packet
|
||||
}
|
||||
|
||||
private class FakeHost(private val platform: ProtocolPlatform) : SessionHost {
|
||||
private val lock = platform.createLock()
|
||||
private val lines = ArrayList<String>()
|
||||
private var done: Latch? = null
|
||||
var wakeLock = false
|
||||
|
||||
fun logs(): List<String> = lock.withLock { lines.toList() }
|
||||
|
||||
fun hasLog(fragment: String): Boolean = logs().any { it.contains(fragment) }
|
||||
|
||||
override fun postToMain(action: () -> Unit) = action()
|
||||
|
||||
override fun currentThreadDispatcher(): SessionDispatcher = SessionDispatcher { it() }
|
||||
|
||||
override fun isPhoneLocked(): Boolean = false
|
||||
|
||||
override fun setScreenWakeLock(on: Boolean) {
|
||||
wakeLock = on
|
||||
}
|
||||
|
||||
override fun isEvenAppActive(): Boolean = false
|
||||
|
||||
override fun startWorker(body: () -> Unit) {
|
||||
val latch = Latch(1, platform)
|
||||
done = latch
|
||||
startThread("test-session", true) {
|
||||
try {
|
||||
body()
|
||||
} finally {
|
||||
latch.countDown()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun joinWorker(timeoutMs: Long) {
|
||||
done?.await(timeoutMs)
|
||||
}
|
||||
|
||||
override fun log(level: SessionLogLevel, tag: String, message: String, error: Throwable?) {
|
||||
lock.withLock { lines.add("$level $tag: $message") }
|
||||
}
|
||||
}
|
||||
|
||||
private class FakeListener : FaceclawBleCommunicatorListener {
|
||||
val phases = ArrayList<String>()
|
||||
val events = ArrayList<String>()
|
||||
val finished = ArrayList<String>()
|
||||
val batteries = ArrayList<String>()
|
||||
|
||||
override fun onStateChange(phase: String?, status: String?) {
|
||||
phases.add(phase ?: "")
|
||||
}
|
||||
|
||||
override fun onRingEvent(kind: String?, containerName: String?, eventType: Int, eventSource: Int, systemExitReasonCode: Int, frameId: Int, ringTick: Long, ringType: Int, ringAux: Int, ringSpeed: Int) {
|
||||
events.add("$kind:$eventType")
|
||||
}
|
||||
|
||||
override fun onBatteryState(headsetBattery: Int, headsetCharging: Int, ringBattery: Int, ringCharging: Int) {
|
||||
batteries.add("$headsetBattery/$headsetCharging/$ringBattery/$ringCharging")
|
||||
}
|
||||
|
||||
override fun onSilentMode(silent: Boolean) {}
|
||||
|
||||
override fun onWearState(wearing: Boolean) {}
|
||||
|
||||
override fun onPhoneLockState(locked: Boolean) {}
|
||||
|
||||
override fun onEvenAppConflict(message: String?) {}
|
||||
|
||||
override fun onFrameMetrics(paintMs: Int, transmitMs: Int, tileCount: Int) {}
|
||||
|
||||
override fun onFrameFinished(frameId: Int, outcome: String?) {
|
||||
finished.add("$frameId:$outcome")
|
||||
}
|
||||
|
||||
override fun onFirmwareInfo(leftVersion: String?, rightVersion: String?, extension: String?) {}
|
||||
}
|
||||
|
||||
private class Session {
|
||||
val platform = ClockPlatform(testPlatform())
|
||||
val link = FakeLink(platform)
|
||||
val host = FakeHost(platform)
|
||||
val listener = FakeListener()
|
||||
val frameTimings = FrameTimingsCore(platform)
|
||||
val core = GlassesSessionCore(link, host, frameTimings, platform, RIGHT, LEFT, "")
|
||||
|
||||
init {
|
||||
link.core = core
|
||||
core.setListener(listener)
|
||||
}
|
||||
|
||||
fun layoutCreated(): Boolean = core.monitor.withLock { core.fixedLayoutCreated }
|
||||
|
||||
fun startAndAwaitLayout() {
|
||||
assertTrue(core.start())
|
||||
assertTrue(waitUntil(5_000) { layoutCreated() }, "layout never created; logs=${host.logs().takeLast(20)}")
|
||||
}
|
||||
|
||||
/** A 64x32 surface frame with a marker byte at [marker]; returns the expected composite fingerprint. */
|
||||
fun submitFrame(marker: Int, value: Byte, frameId: Int): Unit {
|
||||
val pixels = ByteArray(64 * 32)
|
||||
pixels[marker] = value
|
||||
core.submitSurfaceFrame(ArrayByteReader(pixels), "s", 0, 0, 64, 32, "content-$marker-$value", 0, frameId, null)
|
||||
}
|
||||
|
||||
fun displayedMatchesDesired(): Boolean = core.monitor.withLock {
|
||||
core.desiredTilesLock.locked { core.desiredFingerprint.isNotEmpty() && core.desiredFingerprint == core.displayedFingerprint }
|
||||
}
|
||||
}
|
||||
|
||||
class GlassesSessionCoreTest {
|
||||
@Test
|
||||
fun connectsAuthenticatesPreludesAndCreatesTheLayout() {
|
||||
val s = Session()
|
||||
s.startAndAwaitLayout()
|
||||
val writes = s.link.writes
|
||||
assertEquals(setOf(RIGHT, LEFT), writes.filter { it.sid == BleProtocol.SID_SECURITY_AUTH }.map { it.address }.toSet())
|
||||
assertTrue(s.host.hasLog("security auth R=ok L=ok"), s.host.logs().toString())
|
||||
assertEquals(1, writes.count { it.kind == "prelude" })
|
||||
// The framebuffer lease is acquired on both arms (fire-and-forget settings writes, magic 0).
|
||||
val fbAcquire = byteArrayOf(70, 67, 1, BleProtocol.FACECLAW_FB_OP_ACQUIRE.toByte())
|
||||
assertEquals(setOf(RIGHT, LEFT), writes.filter { it.sid == BleProtocol.SID_UI_SETTING && it.pb.containsSequence(fbAcquire) }.map { it.address }.toSet())
|
||||
assertEquals(1, writes.count { it.kind == "battery" }, "settings query for firmware info")
|
||||
assertEquals(1, writes.count { it.kind == "create-layout" })
|
||||
assertTrue(s.core.isSessionReady())
|
||||
assertEquals(listOf("disconnected", "connecting", "connected"), s.listener.phases.distinct())
|
||||
s.core.disconnect()
|
||||
assertFalse(s.core.isSessionReady())
|
||||
assertEquals("disconnected", s.listener.phases.last())
|
||||
assertTrue(s.listener.phases.contains("disconnecting"))
|
||||
assertTrue(s.link.closed)
|
||||
assertEquals(listOf(RIGHT, LEFT), s.link.disconnected.takeLast(2))
|
||||
assertFalse(s.host.wakeLock)
|
||||
assertTrue(s.host.hasLog("communicator stop"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun imagePipelineSendsAKeyframeThenABoundingBoxDelta() {
|
||||
val s = Session()
|
||||
s.startAndAwaitLayout()
|
||||
s.core.configureCompositorScreen(64, 32)
|
||||
s.core.configureSurface("s", 0, 0, 64, 32, 0, SurfaceCompositor.TRANSPARENCY_OPAQUE)
|
||||
s.submitFrame(100, 0x80.toByte(), 1)
|
||||
assertTrue(waitUntil(5_000) { s.link.cfwMessages().size >= 1 && s.displayedMatchesDesired() }, s.host.logs().takeLast(20).toString())
|
||||
assertTrue(s.listener.finished.contains("1:sent"), s.listener.finished.toString())
|
||||
s.submitFrame(101, 0x80.toByte(), 2)
|
||||
assertTrue(waitUntil(5_000) { s.link.cfwMessages().size >= 2 && s.displayedMatchesDesired() }, s.host.logs().takeLast(20).toString())
|
||||
val messages = s.link.cfwMessages()
|
||||
assertEquals(CFW_MSG_FULL_FRAME, messages[0][0].toInt() and CFW_MSG_TYPE_MASK, "first frame is a keyframe")
|
||||
assertEquals(CFW_MSG_BOUNDING_BOX, messages[1][0].toInt() and CFW_MSG_TYPE_MASK, "second frame is a delta")
|
||||
assertTrue(s.listener.finished.contains("2:sent"))
|
||||
// Identical content again: nothing is sent and the frame is finished as a no-change.
|
||||
s.submitFrame(101, 0x80.toByte(), 3)
|
||||
assertTrue(waitUntil(2_000) { s.listener.finished.any { it.startsWith("3:") } })
|
||||
assertTrue(s.listener.finished.any { it == "3:discarded: no change from displayed image" }, s.listener.finished.toString())
|
||||
assertEquals(2, s.link.cfwMessages().size)
|
||||
s.core.disconnect()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun heartbeatAndLeaseRenewalFollowTheClock() {
|
||||
val s = Session()
|
||||
s.startAndAwaitLayout()
|
||||
// The first heartbeat follows the layout at once (nothing has been acked yet); the
|
||||
// next one waits for HEARTBEAT_READY_MS since that ack.
|
||||
assertTrue(waitUntil(3_000) { s.link.writes.count { it.kind == "heartbeat" } == 1 }, s.host.logs().takeLast(10).toString())
|
||||
assertTrue(s.host.hasLog("Got ACK for heartbeat"))
|
||||
sleepMs(300)
|
||||
assertEquals(1, s.link.writes.count { it.kind == "heartbeat" })
|
||||
s.platform.offsetMs += ConnectionOptions.HEARTBEAT_READY_MS + 100L
|
||||
s.core.interruptibleSleep.interrupt()
|
||||
assertTrue(waitUntil(3_000) { s.link.writes.count { it.kind == "heartbeat" } == 2 }, s.host.logs().takeLast(10).toString())
|
||||
val leaseWritesBefore = s.link.writes.count { it.sid == BleProtocol.SID_UI_SETTING && it.pb.containsSequence(byteArrayOf(70, 67, 1, BleProtocol.FACECLAW_FB_OP_ACQUIRE.toByte())) }
|
||||
s.platform.offsetMs += GlassesSessionCore.FACECLAW_WAKE_LEASE_RENEW_MS + 100L
|
||||
s.core.interruptibleSleep.interrupt()
|
||||
assertTrue(waitUntil(3_000) {
|
||||
s.link.writes.count { it.sid == BleProtocol.SID_UI_SETTING && it.pb.containsSequence(byteArrayOf(70, 67, 1, BleProtocol.FACECLAW_FB_OP_ACQUIRE.toByte())) } == leaseWritesBefore + 2
|
||||
}, "framebuffer lease renewed on both arms")
|
||||
s.core.disconnect()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun deferredWakeIsClaimedAndReadyIsSentAfterResume() {
|
||||
val s = Session()
|
||||
s.startAndAwaitLayout()
|
||||
s.core.configureCompositorScreen(64, 32)
|
||||
s.core.configureSurface("s", 0, 0, 64, 32, 0, SurfaceCompositor.TRANSPARENCY_OPAQUE)
|
||||
s.submitFrame(7, 0x40, 1)
|
||||
assertTrue(waitUntil(5_000) { s.displayedMatchesDesired() })
|
||||
|
||||
assertTrue(s.core.suspendEvenHubSession())
|
||||
assertTrue(s.core.monitor.withLock { s.core.shutdownRequested })
|
||||
assertEquals(1, s.link.writes.count { it.kind == "shutdown" })
|
||||
|
||||
// CFW deferred double-tap wake on the right arm: field 102 [F,C,ver,event=1,nonce LE16].
|
||||
val nonce = 0x1234
|
||||
val event = byteArrayOf(70, 67, 1, 1, (nonce and 0xff).toByte(), (nonce ushr 8).toByte())
|
||||
val pb = BleProtocol.encodeVarintField(1, 1) + BleProtocol.encodeVarintField(2, 0) + BleProtocol.encodeBytesField(BleProtocol.FACECLAW_WAKE_EVENT_FIELD, event)
|
||||
s.core.onNotification(RIGHT, BleProtocol.NOTIFY_CHAR_UUID, BleProtocol.framePb(pb, BleProtocol.SID_UI_SETTING, BleProtocol.FLAG_NOTIFY, 9)[0])
|
||||
assertTrue(s.listener.events.contains("display-wake:${BleProtocol.EVENT_DOUBLE_CLICK}"), s.listener.events.toString())
|
||||
val claim = byteArrayOf(70, 67, 1, BleProtocol.FACECLAW_WAKE_OP_CLAIM.toByte(), (nonce and 0xff).toByte(), (nonce ushr 8).toByte())
|
||||
assertTrue(waitUntil(3_000) { s.link.writes.count { it.pb.containsSequence(claim) } == 2 }, "CLAIM written to both arms")
|
||||
assertEquals(RIGHT, s.link.writes.first { it.pb.containsSequence(claim) }.address, "right arm first")
|
||||
|
||||
assertTrue(s.core.resumeEvenHubSession())
|
||||
assertEquals(2, s.link.writes.count { it.kind == "prelude" })
|
||||
assertTrue(waitUntil(5_000) { s.layoutCreated() && s.displayedMatchesDesired() }, s.host.logs().takeLast(20).toString())
|
||||
assertTrue(s.core.awaitEvenHubSessionReady(5_000))
|
||||
val ready = byteArrayOf(70, 67, 1, BleProtocol.FACECLAW_WAKE_OP_READY.toByte(), (nonce and 0xff).toByte(), (nonce ushr 8).toByte())
|
||||
assertEquals(2, s.link.writes.count { it.pb.containsSequence(ready) }, "READY written to both arms")
|
||||
s.core.disconnect()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun unexpectedAndDuplicateAcksAreLoggedNotApplied() {
|
||||
val s = Session()
|
||||
s.startAndAwaitLayout()
|
||||
val unknown = BleProtocol.framePb(BleProtocol.encodeVarintField(1, 1) + BleProtocol.encodeVarintField(2, 150), BleProtocol.SID_EVENHUB, 0, 5)[0]
|
||||
s.core.onNotification(RIGHT, BleProtocol.NOTIFY_CHAR_UUID, unknown)
|
||||
assertTrue(s.host.hasLog("unexpected ACK sid=${BleProtocol.SID_EVENHUB} magic=150"), s.host.logs().takeLast(5).toString())
|
||||
val layout = s.link.writes.first { it.kind == "create-layout" }
|
||||
val duplicate = BleProtocol.framePb(BleProtocol.encodeVarintField(1, 1) + BleProtocol.encodeVarintField(2, layout.magic), layout.sid, 0, 6)[0]
|
||||
s.core.onNotification(RIGHT, BleProtocol.NOTIFY_CHAR_UUID, duplicate)
|
||||
assertTrue(s.host.hasLog("duplicate ACK for already-acked message sid=${layout.sid} magic=${layout.magic}"), s.host.logs().takeLast(5).toString())
|
||||
assertTrue(s.layoutCreated())
|
||||
s.core.disconnect()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun ackTimeoutOnTheLayoutReconnects() {
|
||||
val s = Session()
|
||||
s.link.muted.add("create-layout")
|
||||
assertTrue(s.core.start())
|
||||
assertTrue(waitUntil(5_000) { s.link.writes.any { it.kind == "create-layout" } })
|
||||
assertFalse(s.layoutCreated())
|
||||
s.platform.offsetMs += ConnectionOptions.ACK_TIMEOUT_MS + ConnectionOptions.WRITE_TIMEOUT_MS + 1_000L
|
||||
s.core.interruptibleSleep.interrupt()
|
||||
assertTrue(waitUntil(3_000) { s.listener.phases.contains("retrying") }, s.host.logs().takeLast(10).toString())
|
||||
assertTrue(s.host.hasLog("message timed out: create-layout"))
|
||||
assertTrue(s.host.hasLog("Transport failure: ack timeout"))
|
||||
assertTrue(s.link.disconnected.containsAll(listOf(RIGHT, LEFT)))
|
||||
s.core.disconnect()
|
||||
assertEquals("disconnected", s.listener.phases.last())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun cfwNackTriggersReplayWithAFreshStreamId() {
|
||||
val s = Session()
|
||||
s.link.muted.add("image")
|
||||
s.startAndAwaitLayout()
|
||||
s.core.configureCompositorScreen(64, 32)
|
||||
s.core.configureSurface("s", 0, 0, 64, 32, 0, SurfaceCompositor.TRANSPARENCY_OPAQUE)
|
||||
s.submitFrame(3, 0x20, 1)
|
||||
assertTrue(waitUntil(5_000) { s.link.cfwMessages().size == 1 })
|
||||
val first = s.link.writes.first { it.sid == CfwTransport.SID && it.kind == "image" }
|
||||
val message = assertNotNull(s.core.monitor.withLock { s.core.inFlightMessages.firstOrNull { it.magic == first.magic } })
|
||||
// A NACK from lens 1 marks the message for replay; the window re-sends it under a new id.
|
||||
val nack = cfwAck(first.magic, 1, 0, 0).also { it[8] = 3; val crc = CfwTransport.crc(it.copyOfRange(8, 17)); it[17] = crc.toByte(); it[18] = (crc ushr 8).toByte() }
|
||||
s.core.onNotification(LEFT, BleProtocol.NOTIFY_CHAR_UUID, nack)
|
||||
assertTrue(waitUntil(3_000) { s.link.cfwMessages().size == 2 }, s.host.logs().takeLast(10).toString())
|
||||
assertTrue(s.host.hasLog("CFW replay id=${first.magic}"))
|
||||
val second = s.link.writes.filter { it.sid == CfwTransport.SID && it.kind == "image" }[1]
|
||||
assertTrue(second.magic != first.magic)
|
||||
assertEquals(1, message.cfwRetries)
|
||||
assertContentEquals(first.message, second.message)
|
||||
s.core.disconnect()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertContentEquals
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertNull
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class GlyphAndIconSourceTest {
|
||||
private fun u16(bytes: ByteArray, at: Int) = (bytes[at].toInt() and 0xff) or ((bytes[at + 1].toInt() and 0xff) shl 8)
|
||||
private fun s16(bytes: ByteArray, at: Int) = u16(bytes, at).toShort().toInt()
|
||||
|
||||
// 6x4 raster: ink in columns 2..4, rows 1..2, one 128 edge pixel.
|
||||
private val raster = byteArrayOf(
|
||||
0, 0, 0, 0, 0, 0,
|
||||
0, 0, -1, -1, 0, 0,
|
||||
0, 0, 0, -1, 0x7f, 0,
|
||||
0, 0, 0, 0, 0, 0,
|
||||
)
|
||||
|
||||
@Test
|
||||
fun gammaLutIsIdentityAtOneAndDarkensAboveOne() {
|
||||
val linear = GlyphPacket.gammaLut(1.0)
|
||||
for (i in 0 until 256) assertEquals(i, linear[i].toInt() and 0xff)
|
||||
assertContentEquals(linear, GlyphPacket.gammaLut(0.0))
|
||||
val squared = GlyphPacket.gammaLut(2.0)
|
||||
assertEquals(64, squared[128].toInt() and 0xff)
|
||||
assertEquals(255, squared[255].toInt() and 0xff)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun inkScansFindPaintedBoxes() {
|
||||
assertContentEquals(intArrayOf(2, 4), GlyphPacket.paintedColumnRange(raster, 6, 4))
|
||||
assertContentEquals(intArrayOf(2, 1, 4, 2), GlyphPacket.inkBounds(raster, 6, 4))
|
||||
assertNull(GlyphPacket.paintedColumnRange(ByteArray(12), 4, 3))
|
||||
assertNull(GlyphPacket.inkBounds(ByteArray(12), 4, 3))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun imagePacketsCarryLittleEndianHeaderAndGammaMappedRegion() {
|
||||
val whole = GlyphPacket.packImage(raster, 6, 4, 1.0)
|
||||
assertEquals(4 + 24, whole.size)
|
||||
assertEquals(6, u16(whole, 0))
|
||||
assertEquals(4, u16(whole, 2))
|
||||
assertContentEquals(raster, whole.copyOfRange(4, whole.size))
|
||||
val region = GlyphPacket.packImageRegion(raster, 6, 2, 1, 3, 2, 2.0)
|
||||
assertEquals(3, u16(region, 0))
|
||||
assertEquals(2, u16(region, 2))
|
||||
assertContentEquals(byteArrayOf(-1, -1, 0, 0, -1, 63), region.copyOfRange(4, region.size))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun glyphCellPacketsCropToInkAndEncodeSignedOffsets() {
|
||||
val cell = GlyphPacket.packGlyphCell(GlyphPacket.advanceFixed(5.5), raster, 6, 4, penX = 3, baselineY = 3, ascent = 10, gamma = 1.0)
|
||||
assertEquals(352, u16(cell, 0))
|
||||
assertEquals(-1, s16(cell, 2)) // ink starts one pixel left of the pen
|
||||
assertEquals(8, s16(cell, 4)) // ascent 10, ink top two rows above the baseline
|
||||
assertEquals(3, u16(cell, 6))
|
||||
assertEquals(2, u16(cell, 8))
|
||||
assertContentEquals(byteArrayOf(-1, -1, 0, 0, -1, 0x7f), cell.copyOfRange(10, cell.size))
|
||||
val empty = GlyphPacket.packGlyphCell(64, ByteArray(12), 4, 3, 0, 0, 10, 1.0)
|
||||
assertContentEquals(byteArrayOf(64, 0, 0, 0, 0, 0, 0, 0, 0, 0), empty)
|
||||
assertEquals(0xffff, GlyphPacket.advanceFixed(5000.0))
|
||||
}
|
||||
|
||||
private fun nameTable(vararg records: Triple<Int, Int, String>): ByteArray {
|
||||
// records: (platformId, nameId, value); platform 1 stored Latin-1, others UTF-16BE
|
||||
val strings = ArrayList<ByteArray>()
|
||||
for ((platform, _, value) in records) {
|
||||
strings.add(if (platform == 1) ByteArray(value.length) { value[it].code.toByte() }
|
||||
else ByteArray(value.length * 2) { if (it % 2 == 0) (value[it / 2].code shr 8).toByte() else value[it / 2].code.toByte() })
|
||||
}
|
||||
val out = ByteSink()
|
||||
fun u16(v: Int) { out.write(v ushr 8); out.write(v) }
|
||||
fun u32(v: Int) { u16(v ushr 16); u16(v and 0xffff) }
|
||||
val nameOffset = 12 + 16
|
||||
val nameLength = 6 + records.size * 12 + strings.sumOf { it.size }
|
||||
u32(0x00010000); u16(1); u16(0); u16(0); u16(0) // offset table, 1 table
|
||||
u32(0x6e616d65); u32(0); u32(nameOffset); u32(nameLength) // 'name' record
|
||||
u16(0); u16(records.size); u16(6 + records.size * 12)
|
||||
var offset = 0
|
||||
for ((i, record) in records.withIndex()) {
|
||||
u16(record.first); u16(0); u16(0); u16(record.second); u16(strings[i].size); u16(offset)
|
||||
offset += strings[i].size
|
||||
}
|
||||
for (s in strings) out.write(s)
|
||||
return out.toByteArray()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun openTypeNamesPreferTypographicFamilyAndStyle() {
|
||||
assertEquals("Roboto\nLight", OpenTypeNames.parse(nameTable(Triple(3, 1, "Roboto Light"), Triple(3, 2, "Regular"), Triple(3, 16, "Roboto"), Triple(1, 17, "Light"))))
|
||||
assertEquals("Mono\n", OpenTypeNames.parse(nameTable(Triple(1, 1, " Mono "))))
|
||||
assertEquals("", OpenTypeNames.parse(nameTable(Triple(3, 2, "Bold"))))
|
||||
assertEquals("", OpenTypeNames.parse(ByteArray(8)))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun svgIconsParseShapesStrokeRuleAndPlacement() {
|
||||
val svg = """<svg viewBox="0 0 24 24" fill="none" stroke="currentColor"><rect x="3" y="3" width="18" height="18" rx="2"/>
|
||||
<circle cx="12" cy="12" r="0"/><circle cx="12" cy="12" r="3"/><path d="M4 4h16"/><polygon points="1,2 3 4 5"/><line x1="0" y1="0" x2="1" y2="1"/></svg>"""
|
||||
val icon = SvgIconSource.parse(svg)!!
|
||||
assertTrue(icon.stroked)
|
||||
assertEquals(listOf("Rect", "Circle", "PathData", "Polyline", "Line"), icon.shapes.map { it::class.simpleName })
|
||||
val rect = icon.shapes[0] as SvgShape.Rect
|
||||
assertEquals(2f, rect.rx); assertEquals(2f, rect.ry)
|
||||
assertEquals(3f, (icon.shapes[1] as SvgShape.Circle).r)
|
||||
assertEquals("M4 4h16", (icon.shapes[2] as SvgShape.PathData).d)
|
||||
val polygon = icon.shapes[3] as SvgShape.Polyline
|
||||
assertContentEquals(floatArrayOf(1f, 2f, 3f, 4f), polygon.points)
|
||||
assertTrue(polygon.closed)
|
||||
assertContentEquals(floatArrayOf(0f, 0f, 2f), icon.placement(48))
|
||||
val filled = SvgIconSource.parse("""<svg viewBox="-2 0 40 20"><rect width="4" height="2"/></svg>""")!!
|
||||
assertTrue(!filled.stroked)
|
||||
assertContentEquals(floatArrayOf(0f, 5f, 0.5f), filled.placement(20))
|
||||
assertEquals(-2f, filled.minX)
|
||||
assertNull(SvgIconSource.parse("""<svg><circle r="0"/><rect width="0" height="3"/></svg>"""))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun coverageQuantizesToThreeLevels() {
|
||||
assertEquals(0, SvgIconSource.quantizeCoverage(63))
|
||||
assertEquals(128, SvgIconSource.quantizeCoverage(64))
|
||||
assertEquals(128, SvgIconSource.quantizeCoverage(191))
|
||||
assertEquals(255, SvgIconSource.quantizeCoverage(192))
|
||||
assertContentEquals(byteArrayOf(0, -128, -1), SvgIconSource.quantizeArgb(intArrayOf(0x10ffffff, 0x80000000.toInt(), 0xff000000.toInt())))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertContentEquals
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertSame
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class GraphicsCoresTest {
|
||||
private fun checksum(bytes: ByteArray): Long {
|
||||
var sum = 7L
|
||||
for (b in bytes) sum = sum * 31 + (b.toInt() and 0xff)
|
||||
return sum
|
||||
}
|
||||
|
||||
@Test
|
||||
fun grayPacketHeaderAndFlatConversion() {
|
||||
// 3x2: opaque white, opaque mid, transparent white, half-alpha red, opaque black, opaque green
|
||||
val pixels = intArrayOf(
|
||||
0xffffffff.toInt(), 0xff808080.toInt(), 0x00ffffff,
|
||||
0x80ff0000.toInt(), 0xff000000.toInt(), 0xff00ff00.toInt(),
|
||||
)
|
||||
val packet = GrayPacket.fromArgb(pixels, 3, 2, 1f, false)
|
||||
assertEquals(4 + 6, packet.size)
|
||||
assertContentEquals(byteArrayOf(3, 0, 2, 0), packet.copyOfRange(0, 4))
|
||||
assertEquals(255, packet[4].toInt() and 0xff)
|
||||
assertEquals(128, packet[5].toInt() and 0xff)
|
||||
assertEquals(0, packet[6].toInt() and 0xff) // alpha 0 darkens to black
|
||||
assertEquals(76 * 128 / 255, packet[7].toInt() and 0xff) // luma 76 at half alpha
|
||||
assertEquals(0, packet[8].toInt() and 0xff)
|
||||
assertEquals(149, packet[9].toInt() and 0xff)
|
||||
val wide = GrayPacket.fromArgb(IntArray(300 * 2), 300, 2, 1f, false)
|
||||
assertContentEquals(byteArrayOf(0x2c, 1, 2, 0), wide.copyOfRange(0, 4))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun toneCurveDarkensMidtones() {
|
||||
val identity = GrayPacket.toneCurve(1f)
|
||||
assertEquals((0..255).toList(), identity.toList())
|
||||
val curve = GrayPacket.toneCurve(2.2f)
|
||||
assertEquals(0, curve[0])
|
||||
assertEquals(255, curve[255])
|
||||
assertEquals(56, curve[128])
|
||||
assertTrue(curve.toList() == curve.sorted())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun ditherIsDeterministicAndQuantized() {
|
||||
// A horizontal ramp over 16x4 so the diffusion has somewhere to go.
|
||||
val pixels = IntArray(16 * 4) { i -> val v = (i % 16) * 17; 0xff000000.toInt() or (v shl 16) or (v shl 8) or v }
|
||||
val a = GrayPacket.fromArgb(pixels, 16, 4, 1f, true)
|
||||
val b = GrayPacket.fromArgb(pixels, 16, 4, 1f, true)
|
||||
assertContentEquals(a, b)
|
||||
for (i in 4 until a.size) {
|
||||
val v = a[i].toInt() and 0xff
|
||||
assertTrue(v == 1 || (v % 16 == 0 && v in 16..240), "byte $i = $v")
|
||||
}
|
||||
// Mid-gray flat area dithers to a mix of two adjacent levels, never a solid.
|
||||
val flat = GrayPacket.fromArgb(IntArray(8 * 8) { 0xff7f7f7f.toInt() }, 8, 8, 1f, true)
|
||||
val levels = flat.drop(4).map { it.toInt() and 0xff }.toSet()
|
||||
assertEquals(setOf(112, 128), levels)
|
||||
assertEquals(2001783152304756847L, checksum(a))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun previewPaletteValues() {
|
||||
assertEquals(0xff000000.toInt(), PreviewPalette.argb(0, 0.7, false))
|
||||
assertEquals(0xffffffff.toInt(), PreviewPalette.argb(255, 0.7, false))
|
||||
assertEquals(0xff00ff00.toInt(), PreviewPalette.argb(255, 0.7, true))
|
||||
val gray = PreviewPalette.argb(128, 0.7, false)
|
||||
val v = gray and 0xff
|
||||
assertEquals(157, v)
|
||||
assertEquals(0xff000000.toInt() or (v shl 16) or (v shl 8) or v, gray)
|
||||
assertEquals(0xff000000.toInt() or (v shl 8), PreviewPalette.argb(128, 0.7, true))
|
||||
assertEquals(0xff808080.toInt(), PreviewPalette.argb(128, 1.0, false))
|
||||
val lut = PreviewPalette.lookupTable(0.7, false)
|
||||
assertSame(lut, PreviewPalette.lookupTable(0.7, false))
|
||||
assertEquals(256, lut.size)
|
||||
val expanded = PreviewPalette.expand(byteArrayOf(0, 0x80.toByte(), 0xff.toByte()), 0, 3, 0.7, false)
|
||||
assertContentEquals(intArrayOf(lut[0], lut[128], lut[255]), expanded)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun statusIconsAreStable() {
|
||||
val wifi = StatusIconArt.wifi(4)
|
||||
val cell = StatusIconArt.cell(2)
|
||||
val hotspot = StatusIconArt.hotspot()
|
||||
for (icon in listOf(wifi, cell, hotspot)) {
|
||||
assertEquals(24 * 24, icon.size)
|
||||
assertTrue(icon.any { it.toInt() != 0 })
|
||||
}
|
||||
assertTrue(StatusIconArt.wifi(0).count { it.toInt() != 0 } < wifi.count { it.toInt() != 0 })
|
||||
assertEquals(StatusIconArt.wifi(2).toList(), StatusIconArt.wifi(2).toList())
|
||||
assertEquals(listOf(-7515428168691714401L, -7643893777543336767L, 7675840471157348143L), listOf(checksum(wifi), checksum(cell), checksum(hotspot)))
|
||||
val scaled = StatusIconArt.scale(wifi, 12)
|
||||
assertEquals(144, scaled.size)
|
||||
assertSame(wifi, StatusIconArt.scale(wifi, 24))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertFailsWith
|
||||
|
||||
class Lc3PacketFramerTest {
|
||||
private class FakeCodec(var fail: Boolean = false) : Lc3Codec {
|
||||
var calls = 0
|
||||
override fun decodeFrames(packet: ByteArray, offset: Int, length: Int, pcmOut: ShortArray): Int {
|
||||
calls++
|
||||
assertEquals(0, offset)
|
||||
assertEquals(Lc3PacketFramer.LC3_BYTES, length)
|
||||
return if (fail) -1 else Lc3PacketFramer.SAMPLES_PER_PACKET
|
||||
}
|
||||
}
|
||||
|
||||
private fun packet(counter: Int, ssr: Int = 0, angle: Int = 0): ByteArray {
|
||||
val p = ByteArray(Lc3PacketFramer.PACKET_BYTES)
|
||||
p[Lc3PacketFramer.COUNTER_OFFSET] = counter.toByte()
|
||||
p[Lc3PacketFramer.SSR_OFFSET] = (ssr and 0xff).toByte()
|
||||
p[Lc3PacketFramer.SSR_OFFSET + 1] = ((ssr shr 8) and 0xff).toByte()
|
||||
p[Lc3PacketFramer.ANGLE_OFFSET] = (angle and 0xff).toByte()
|
||||
p[Lc3PacketFramer.ANGLE_OFFSET + 1] = ((angle shr 8) and 0xff).toByte()
|
||||
return p
|
||||
}
|
||||
|
||||
@Test
|
||||
fun countsRealDuplicateLateAndMissingPackets() {
|
||||
val codec = FakeCodec()
|
||||
val framer = Lc3PacketFramer(codec)
|
||||
val pcm = ShortArray(Lc3PacketFramer.SAMPLES_PER_PACKET)
|
||||
assertEquals(800, framer.decodePacket(packet(250), pcm))
|
||||
assertEquals(800, framer.decodePacket(packet(251), pcm))
|
||||
assertEquals(0, framer.decodePacket(packet(251), pcm)) // relayed by the other arm
|
||||
assertEquals(0, framer.decodePacket(packet(250), pcm)) // late copy (gap 255)
|
||||
assertEquals(800, framer.decodePacket(packet(254), pcm)) // 252, 253 lost
|
||||
assertEquals(800, framer.decodePacket(packet(1), pcm)) // wraps past 255 -> 0; 255, 0 lost
|
||||
assertEquals(0, framer.decodePacket(packet(129), pcm)) // gap 128 is late, not 127 losses
|
||||
assertEquals(4, framer.realPackets)
|
||||
assertEquals(3, framer.duplicatePackets)
|
||||
assertEquals(4, framer.missingPackets)
|
||||
assertEquals(0, framer.decodeErrors)
|
||||
assertEquals(4, codec.calls)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun parsesSignedTrailerAndRejectsBadInput() {
|
||||
val codec = FakeCodec()
|
||||
val framer = Lc3PacketFramer(codec)
|
||||
val pcm = ShortArray(Lc3PacketFramer.SAMPLES_PER_PACKET)
|
||||
framer.decodePacket(packet(7, ssr = -1234, angle = -45), pcm)
|
||||
assertEquals(-1234, framer.lastSsr)
|
||||
assertEquals(-45, framer.lastAngleDegrees)
|
||||
assertEquals(0, framer.decodePacket(ByteArray(10), pcm))
|
||||
assertEquals(0, framer.decodePacket(null, pcm))
|
||||
assertEquals(2, framer.decodeErrors)
|
||||
assertFailsWith<IllegalArgumentException> { framer.decodePacket(packet(8), ShortArray(10)) }
|
||||
codec.fail = true
|
||||
assertEquals(0, framer.decodePacket(packet(8), pcm))
|
||||
assertEquals(3, framer.decodeErrors)
|
||||
// A failed decode does not advance the counter, so 8 is still accepted afterwards.
|
||||
codec.fail = false
|
||||
assertEquals(800, framer.decodePacket(packet(8), pcm))
|
||||
assertEquals(2, framer.realPackets)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,178 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertNull
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
private class FakeConnection(input: String, private val bytesOverride: ByteArray? = null) : RemoteInputConnection {
|
||||
private val bytes = bytesOverride ?: input.encodeToByteArray()
|
||||
private var index = 0
|
||||
private val written = ByteSink()
|
||||
var closed = false
|
||||
val output: String get() = written.toByteArray().decodeToString()
|
||||
|
||||
override fun readByte(timeoutMs: Long): Int = if (index < bytes.size) bytes[index++].toInt() and 255 else -1
|
||||
|
||||
override fun write(bytes: ByteArray) = written.write(bytes)
|
||||
|
||||
override fun close() { closed = true }
|
||||
}
|
||||
|
||||
private class FakePort(platform: ProtocolPlatform) : LocalServerPort {
|
||||
val connections = BlockingQueue<FakeConnection>(platform)
|
||||
var localAddresses = setOf("127.0.0.1", "192.168.1.5")
|
||||
var bindFails = false
|
||||
var listener: FakeListener? = null
|
||||
|
||||
inner class FakeListener : RemoteInputListener {
|
||||
override var isClosed = false
|
||||
override fun accept(): RemoteInputConnection? {
|
||||
while (!isClosed) {
|
||||
val next = connections.poll(20)
|
||||
if (next != null) return next
|
||||
}
|
||||
return null
|
||||
}
|
||||
override fun close() { isClosed = true }
|
||||
}
|
||||
|
||||
override fun isLocalUnicastAddress(address: String): Boolean = address in localAddresses
|
||||
|
||||
override fun bind(address: String, port: Int): RemoteInputListener {
|
||||
if (bindFails) throw IllegalStateException("in use")
|
||||
return FakeListener().also { listener = it }
|
||||
}
|
||||
}
|
||||
|
||||
private fun waitFor(timeoutMs: Long = 3000, condition: () -> Boolean): Boolean {
|
||||
val platform = testPlatform()
|
||||
val deadline = platform.elapsedRealtimeMs() + timeoutMs
|
||||
while (platform.elapsedRealtimeMs() < deadline) {
|
||||
if (condition()) return true
|
||||
sleepMs(5)
|
||||
}
|
||||
return condition()
|
||||
}
|
||||
|
||||
class RemoteInputSessionTest {
|
||||
private val platform = testPlatform()
|
||||
private val random = object : RandomSource { override fun nextBytes(count: Int) = ByteArray(count) { it.toByte() } }
|
||||
private fun dispatchOnThread(action: () -> Unit) = startThread("remote-input-dispatch", true, action)
|
||||
|
||||
private fun session(port: FakePort, requestTimeoutMs: Long = 5000, readTimeoutMs: Long = 5000) =
|
||||
RemoteInputSession(port, random, ::dispatchOnThread, platform, requestTimeoutMs = requestTimeoutMs, readTimeoutMs = readTimeoutMs)
|
||||
|
||||
@Test
|
||||
fun startValidatesAddressesAndBindFailures() {
|
||||
val port = FakePort(platform)
|
||||
val session = session(port)
|
||||
assertEquals("Invalid local address.", session.start(7000, "localhost"))
|
||||
assertEquals("Address is not assigned to a local interface.", session.start(7000, "10.0.0.9"))
|
||||
port.bindFails = true
|
||||
assertEquals("Could not open local input port 7000.", session.start(7000, "127.0.0.1"))
|
||||
port.bindFails = false
|
||||
assertEquals("", session.start(7000, "127.0.0.1"))
|
||||
assertEquals("", session.start(7000, "127.0.0.1"), "second start is a no-op")
|
||||
session.stop()
|
||||
assertTrue(waitFor { port.listener!!.isClosed })
|
||||
assertTrue(RemoteInputSession.isNumericLocalAddress("fe80::1%en0"))
|
||||
assertFalse(RemoteInputSession.isNumericLocalAddress("example.com"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun requestIsPublishedAnsweredAndWrittenBack() {
|
||||
val port = FakePort(platform)
|
||||
val session = session(port)
|
||||
var notified = 0
|
||||
session.setRequestListener { notified++ }
|
||||
assertEquals("", session.start(7000, "127.0.0.1"))
|
||||
val connection = FakeConnection("{\"token\":\"t\",\"text\":\"hi\"}\n")
|
||||
port.connections.put(connection)
|
||||
assertTrue(waitFor { notified == 1 })
|
||||
val pending = Json.parseObject(session.nextRequest()!!)
|
||||
assertEquals(1L, pending["id"].asLong())
|
||||
assertEquals("{\"token\":\"t\",\"text\":\"hi\"}", pending["body"].asString())
|
||||
assertTrue(pending["expiresAt"].asLong()!! > currentTimeMillis())
|
||||
assertNull(session.nextRequest(), "consumed once")
|
||||
session.complete(99L, "{\"ok\":false}")
|
||||
assertFalse(waitFor(100) { connection.output.isNotEmpty() }, "wrong id is ignored")
|
||||
session.complete(1L, "{\"ok\":true}")
|
||||
assertTrue(waitFor { connection.output == "{\"ok\":true}\n" })
|
||||
assertTrue(waitFor { connection.closed })
|
||||
session.stop()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun timeoutRepliesWithTheTimeoutResponse() {
|
||||
val port = FakePort(platform)
|
||||
val session = session(port, requestTimeoutMs = 150)
|
||||
assertEquals("", session.start(7000, "127.0.0.1"))
|
||||
val connection = FakeConnection("slow\n")
|
||||
port.connections.put(connection)
|
||||
assertTrue(waitFor { connection.output == RemoteInputSession.TIMEOUT_RESPONSE + "\n" })
|
||||
assertNull(session.nextRequest(), "expired request is dropped")
|
||||
session.stop()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun oversizedAndIncompleteFramesGetNoReply() {
|
||||
val port = FakePort(platform)
|
||||
val session = session(port)
|
||||
var notified = 0
|
||||
session.setRequestListener { notified++ }
|
||||
assertEquals("", session.start(7000, "127.0.0.1"))
|
||||
val huge = FakeConnection("", ByteArray(RemoteInputSession.MAX_FRAME_BYTES + 2) { 'a'.code.toByte() }.also { it[it.size - 1] = '\n'.code.toByte() })
|
||||
port.connections.put(huge)
|
||||
assertTrue(waitFor { huge.closed })
|
||||
assertEquals("", huge.output)
|
||||
val partial = FakeConnection("no newline")
|
||||
port.connections.put(partial)
|
||||
assertTrue(waitFor { partial.closed })
|
||||
assertEquals("", partial.output)
|
||||
val invalidUtf8 = FakeConnection("", byteArrayOf(0xff.toByte(), 0xfe.toByte(), '\n'.code.toByte()))
|
||||
port.connections.put(invalidUtf8)
|
||||
assertTrue(waitFor { invalidUtf8.closed })
|
||||
assertEquals("", invalidUtf8.output)
|
||||
assertEquals(0, notified)
|
||||
assertNull(session.nextRequest())
|
||||
session.stop()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun stopWhilePendingReleasesTheServingThread() {
|
||||
val port = FakePort(platform)
|
||||
val session = session(port)
|
||||
var notified = 0
|
||||
session.setRequestListener { notified++ }
|
||||
assertEquals("", session.start(7000, "127.0.0.1"))
|
||||
val connection = FakeConnection("pending\n")
|
||||
port.connections.put(connection)
|
||||
assertTrue(waitFor { notified == 1 })
|
||||
session.stop()
|
||||
assertTrue(waitFor { connection.closed })
|
||||
assertNull(session.nextRequest())
|
||||
assertEquals("", session.start(7001, "127.0.0.1"), "restart after stop")
|
||||
session.stop()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun helpersFilterInterfacesAndHashTokens() {
|
||||
val all = listOf(
|
||||
InterfaceAddress("lo0", "127.0.0.1", up = true, pointToPoint = false, loopback = true, linkLocal = false, anyLocal = false),
|
||||
InterfaceAddress("wlan0", "192.168.1.5", up = true, pointToPoint = false, loopback = false, linkLocal = false, anyLocal = false),
|
||||
InterfaceAddress("wlan0", "fe80::1%wlan0", up = true, pointToPoint = false, loopback = false, linkLocal = true, anyLocal = false),
|
||||
InterfaceAddress("tun0", "100.64.0.2", up = true, pointToPoint = true, loopback = false, linkLocal = false, anyLocal = false),
|
||||
InterfaceAddress("eth0", "10.0.0.2", up = false, pointToPoint = false, loopback = false, linkLocal = false, anyLocal = false),
|
||||
InterfaceAddress("any", "0.0.0.0", up = true, pointToPoint = false, loopback = false, linkLocal = false, anyLocal = true),
|
||||
)
|
||||
assertEquals(
|
||||
"[{\"name\":\"wlan0\",\"address\":\"192.168.1.5\",\"pointToPoint\":false},{\"name\":\"tun0\",\"address\":\"100.64.0.2\",\"pointToPoint\":true}]",
|
||||
RemoteInputSession.interfacesJson(all),
|
||||
)
|
||||
assertEquals("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", RemoteInputSession.tokenHash("abc"))
|
||||
val session = session(FakePort(platform))
|
||||
assertEquals("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f", session.randomSecret())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,149 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertNull
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class ResumableDownloadTest {
|
||||
private class Storage : DownloadStorage {
|
||||
val files = HashMap<String, ByteArray>()
|
||||
override fun length(path: String): Long = files[path]?.size?.toLong() ?: -1L
|
||||
override fun prepareParent(path: String) {}
|
||||
override fun openWrite(path: String, append: Boolean): FileSink {
|
||||
if (!append) files[path] = ByteArray(0)
|
||||
return object : FileSink {
|
||||
override fun write(bytes: ByteArray, offset: Int, length: Int) {
|
||||
files[path] = (files[path] ?: ByteArray(0)) + bytes.copyOfRange(offset, offset + length)
|
||||
}
|
||||
override fun close() {}
|
||||
}
|
||||
}
|
||||
override fun readAll(path: String, consumer: (ByteArray, Int) -> Boolean): Long {
|
||||
val data = files[path] ?: return 0
|
||||
var at = 0L
|
||||
for (chunk in data.toList().chunked(7)) {
|
||||
val bytes = chunk.toByteArray()
|
||||
if (!consumer(bytes, bytes.size)) break
|
||||
at += bytes.size
|
||||
}
|
||||
return at
|
||||
}
|
||||
override fun rename(from: String, to: String): Boolean { files[to] = files.remove(from) ?: return false; return true }
|
||||
override fun delete(path: String): Boolean = files.remove(path) != null
|
||||
}
|
||||
|
||||
private class Transport(val content: ByteArray, val honorRange: Boolean = true, val truncateAt: Int = -1) : DownloadTransport {
|
||||
var lastRange = -1L
|
||||
override fun open(url: String, rangeFrom: Long): DownloadResponse {
|
||||
lastRange = rangeFrom
|
||||
val start = if (honorRange) rangeFrom.toInt() else 0
|
||||
val end = if (truncateAt >= 0) minOf(truncateAt, content.size) else content.size
|
||||
val body = content.copyOfRange(start, end)
|
||||
var at = 0
|
||||
return object : DownloadResponse {
|
||||
override val code: Int = if (honorRange && rangeFrom > 0) 206 else 200
|
||||
override val contentLength: Long = body.size.toLong()
|
||||
override fun read(buffer: ByteArray): Int {
|
||||
if (at >= body.size) return -1
|
||||
val n = minOf(5, body.size - at)
|
||||
body.copyInto(buffer, 0, at, at + n)
|
||||
at += n
|
||||
return n
|
||||
}
|
||||
override fun close() {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class Events : FaceclawModelDownloaderListener {
|
||||
val progress = ArrayList<Pair<Long, Long>>()
|
||||
var done: String? = null
|
||||
var error: String? = null
|
||||
override fun onProgress(bytesDownloaded: Long, totalBytes: Long) { progress.add(bytesDownloaded to totalBytes) }
|
||||
override fun onDone(path: String?) { done = path }
|
||||
override fun onError(message: String?) { error = message }
|
||||
}
|
||||
|
||||
private val content = ByteArray(40) { (it * 3).toByte() }
|
||||
private val sha = sha256Hex(content)
|
||||
|
||||
private fun run(storage: Storage, transport: Transport, expectedSha: String? = sha, total: Long = content.size.toLong()): Events {
|
||||
val events = Events()
|
||||
var clock = 0L
|
||||
ResumableDownload("http://x/m", "/dest/model", expectedSha, total, transport, storage, events) { clock += 600; clock }.run()
|
||||
return events
|
||||
}
|
||||
|
||||
@Test
|
||||
fun freshDownloadVerifiesAndRenames() {
|
||||
val storage = Storage()
|
||||
val transport = Transport(content)
|
||||
val events = run(storage, transport)
|
||||
assertNull(events.error)
|
||||
assertEquals("/dest/model", events.done)
|
||||
assertEquals(content.toList(), storage.files["/dest/model"]!!.toList())
|
||||
assertEquals(0L, transport.lastRange)
|
||||
assertTrue(events.progress.isNotEmpty() && events.progress.last().second == 40L)
|
||||
val again = run(storage, transport)
|
||||
assertEquals("/dest/model", again.done)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun resumesFromPartWhenServerHonorsRange() {
|
||||
val storage = Storage()
|
||||
storage.files["/dest/model.part"] = content.copyOfRange(0, 17)
|
||||
val transport = Transport(content)
|
||||
val events = run(storage, transport)
|
||||
assertEquals(17L, transport.lastRange)
|
||||
assertNull(events.error)
|
||||
assertEquals(content.toList(), storage.files["/dest/model"]!!.toList())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun restartsWhenServerIgnoresRange() {
|
||||
val storage = Storage()
|
||||
storage.files["/dest/model.part"] = ByteArray(17) { 1 }
|
||||
val transport = Transport(content, honorRange = false)
|
||||
val events = run(storage, transport)
|
||||
assertEquals(17L, transport.lastRange)
|
||||
assertNull(events.error)
|
||||
assertEquals(content.toList(), storage.files["/dest/model"]!!.toList())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun checksumMismatchDeletesPart() {
|
||||
val storage = Storage()
|
||||
val events = run(storage, Transport(content), expectedSha = "00".repeat(32))
|
||||
assertEquals("Model download was corrupted (checksum mismatch); download it again", events.error)
|
||||
assertNull(storage.files["/dest/model.part"])
|
||||
assertNull(storage.files["/dest/model"])
|
||||
}
|
||||
|
||||
@Test
|
||||
fun shortBodyKeepsPartForResume() {
|
||||
val storage = Storage()
|
||||
val events = run(storage, Transport(content, truncateAt = 30))
|
||||
assertEquals("Model download ended early (30 of 40 bytes); try again to resume", events.error)
|
||||
assertEquals(30, storage.files["/dest/model.part"]!!.size)
|
||||
val resumed = run(storage, Transport(content))
|
||||
assertNull(resumed.error)
|
||||
assertEquals(content.toList(), storage.files["/dest/model"]!!.toList())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun httpErrorIsReported() {
|
||||
val storage = Storage()
|
||||
val transport = object : DownloadTransport {
|
||||
override fun open(url: String, rangeFrom: Long): DownloadResponse = object : DownloadResponse {
|
||||
override val code = 404
|
||||
override val contentLength = -1L
|
||||
override fun read(buffer: ByteArray) = -1
|
||||
override fun close() {}
|
||||
}
|
||||
}
|
||||
val events = Events()
|
||||
ResumableDownload("u", "/dest/model", null, 0, transport, storage, events).run()
|
||||
assertEquals("Model download failed: HTTP 404", events.error)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class SettingsChangeHubTest {
|
||||
private class MemorySettings : SettingsStorage {
|
||||
val values = HashMap<String?, Any?>()
|
||||
override fun getString(key: String?, defaultValue: String?): String? = values[key] as? String ?: defaultValue
|
||||
override fun putString(key: String?, value: String?) { values[key] = value }
|
||||
override fun getBoolean(key: String?, defaultValue: Boolean): Boolean = values[key] as? Boolean ?: defaultValue
|
||||
override fun putBoolean(key: String?, value: Boolean) { values[key] = value }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun writesNotifyEachListenerThroughItsOwnDispatcher() {
|
||||
val warnings = ArrayList<String>()
|
||||
val hub = SettingsChangeHub(MemorySettings(), { warnings.add(it) }, testPlatform())
|
||||
val queued = ArrayList<() -> Unit>()
|
||||
val seen = ArrayList<String?>()
|
||||
val listener = object : FaceclawSettingsListener { override fun onSettingChanged(key: String?) { seen.add(key) } }
|
||||
hub.register(listener) { queued.add(it) }
|
||||
val silent = object : FaceclawSettingsListener { override fun onSettingChanged(key: String?) { seen.add("silent") } }
|
||||
hub.register(silent, null)
|
||||
assertEquals(1, warnings.size)
|
||||
|
||||
hub.setString("font", "Inter")
|
||||
hub.setBoolean("dark", true)
|
||||
assertEquals("Inter", hub.getString("font", null))
|
||||
assertTrue(hub.getBoolean("dark", false))
|
||||
assertTrue(seen.isEmpty()) // delivered only when the dispatcher runs
|
||||
queued.forEach { it() }
|
||||
assertEquals(listOf<String?>("font", "dark"), seen)
|
||||
|
||||
val failing = object : FaceclawSettingsListener { override fun onSettingChanged(key: String?) { throw IllegalStateException("boom") } }
|
||||
hub.register(failing) { it() }
|
||||
hub.setString("x", null)
|
||||
assertTrue(warnings.last().contains("boom"))
|
||||
hub.unregister(listener)
|
||||
hub.unregister(failing)
|
||||
queued.clear()
|
||||
hub.setBoolean("dark", false)
|
||||
assertTrue(queued.isEmpty())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class SseStreamTest {
|
||||
private fun feed(stream: SseStream, vararg chunks: String) {
|
||||
for (chunk in chunks) {
|
||||
val bytes = chunk.encodeToByteArray()
|
||||
stream.onChunk(bytes, 0, bytes.size)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun splitsLinesAcrossChunksWithBomAndCrlf() {
|
||||
val stream = SseStream()
|
||||
stream.onStatus(200)
|
||||
feed(stream, "event: a\r", "\ndata: {\"x\":1}\n\nda", "ta: tail")
|
||||
assertEquals(listOf(SseEvent.Line("event: a"), SseEvent.Line("data: {\"x\":1}"), SseEvent.Line("")), stream.takeEvents())
|
||||
stream.onComplete()
|
||||
assertEquals(listOf(SseEvent.Line("data: tail"), SseEvent.Complete), stream.takeEvents())
|
||||
assertTrue(stream.isFinished)
|
||||
feed(stream, "late\n")
|
||||
assertEquals(emptyList(), stream.takeEvents())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun errorBodyIsBufferedAndCapped() {
|
||||
val stream = SseStream()
|
||||
stream.onStatus(429)
|
||||
feed(stream, "{\"error\":", "\"slow down\"}\n")
|
||||
assertEquals(emptyList(), stream.takeEvents())
|
||||
stream.onComplete()
|
||||
assertEquals(listOf(SseEvent.HttpError(429, "{\"error\":\"slow down\"}\n")), stream.takeEvents())
|
||||
|
||||
val capped = SseStream()
|
||||
capped.onStatus(500)
|
||||
val big = ByteArray(SseStream.ERROR_BODY_LIMIT + 100) { 'x'.code.toByte() }
|
||||
capped.onChunk(big, 0, big.size)
|
||||
capped.onComplete()
|
||||
val event = capped.takeEvents().single() as SseEvent.HttpError
|
||||
assertEquals(SseStream.ERROR_BODY_LIMIT, event.body.length)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun queueOverflowAndOversizedLinesFail() {
|
||||
val stream = SseStream()
|
||||
stream.onStatus(200)
|
||||
val line = "x".repeat(1023) + "\n"
|
||||
val block = line.repeat(64).encodeToByteArray()
|
||||
var fed = 0L
|
||||
while (fed <= SseStream.QUEUE_BYTE_LIMIT + block.size) {
|
||||
stream.onChunk(block, 0, block.size)
|
||||
fed += block.size
|
||||
}
|
||||
val events = stream.takeEvents()
|
||||
assertEquals(SseEvent.Failure("Streaming response queue overflow"), events.single())
|
||||
|
||||
val long = SseStream()
|
||||
long.onStatus(200)
|
||||
val huge = ByteArray(SseStream.LINE_LIMIT + 1) { 'y'.code.toByte() }
|
||||
long.onChunk(huge, 0, huge.size)
|
||||
assertEquals(SseEvent.Failure("Streaming response line too large"), long.takeEvents().single())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun failureAndCancelAreTerminal() {
|
||||
val stream = SseStream()
|
||||
stream.onStatus(200)
|
||||
feed(stream, "a\n")
|
||||
stream.onFailure("boom")
|
||||
assertEquals(listOf(SseEvent.Line("a"), SseEvent.Failure("boom")), stream.takeEvents())
|
||||
val cancelled = SseStream()
|
||||
cancelled.onStatus(200)
|
||||
feed(cancelled, "a\n")
|
||||
cancelled.cancel()
|
||||
cancelled.onComplete()
|
||||
assertEquals(emptyList(), cancelled.takeEvents())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun unpacksFlatHeaderArrays() {
|
||||
assertEquals(listOf("A" to "1", "C" to "3"), SseStream.unpackHeaders(arrayOf("A", "1", "", "2", "C", "3", "D")))
|
||||
assertEquals(emptyList(), SseStream.unpackHeaders(null))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,382 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertContains
|
||||
import kotlin.test.assertContentEquals
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertNotNull
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
/** A stock-firmware lens pair behind the StockLink port; replies are scripted per written message. */
|
||||
private class FakeStockLink : StockLink {
|
||||
class Written(val address: String, val uuid: String, val sid: Int, val flag: Int, val seq: Int, val pb: ByteArray) {
|
||||
/** Protobuf magic (field 2) of a control message; -1 for OTA payloads. */
|
||||
val magic: Int get() = if (sid == 0xc0 || sid == 0xc1) -1 else BleProtocol.readVarintFieldValue(BleProtocol.stripTrailingCrc(pb), 2, -1)
|
||||
}
|
||||
|
||||
var inbound: StockLinkListener? = null
|
||||
val connected = HashSet<String>()
|
||||
val bond = HashMap<String, BondState>()
|
||||
val writes = ArrayList<Written>()
|
||||
var connectResult: (String) -> Boolean = { true }
|
||||
var responder: (Written) -> Unit = {}
|
||||
var closed = false
|
||||
|
||||
override fun setListener(listener: StockLinkListener?) { inbound = listener }
|
||||
override fun connect(address: String, timeoutMs: Int): Boolean = connectResult(address).also { if (it) connected.add(address) }
|
||||
override fun prepareLink(address: String, desiredMtu: Int, timeoutMs: Int) {}
|
||||
override fun discoverServices(address: String, timeoutMs: Int): Boolean = address in connected
|
||||
override fun enableNotifications(address: String, characteristicUuid: String, timeoutMs: Int): Boolean = address in connected
|
||||
override fun writeFrames(address: String, characteristicUuid: String, frames: List<ByteArray>, mode: GattWriteMode, timeoutMs: Int): Boolean {
|
||||
if (address !in connected) throw IllegalStateException("Not connected: $address")
|
||||
val head = frames.first()
|
||||
val body = ByteArray(frames.sumOf { it.size - 8 })
|
||||
var at = 0
|
||||
for (f in frames) { f.copyInto(body, at, 8); at += f.size - 8 }
|
||||
val written = Written(address, characteristicUuid, head[6].toInt() and 0xff, head[7].toInt() and 0xff, head[2].toInt() and 0xff, body)
|
||||
writes.add(written)
|
||||
responder(written)
|
||||
return true
|
||||
}
|
||||
override fun isConnected(address: String): Boolean = address in connected
|
||||
override fun bondState(address: String): BondState = bond[address] ?: BondState.BONDED
|
||||
override fun disconnect(address: String) { connected.remove(address) }
|
||||
override fun close() { closed = true; connected.clear() }
|
||||
|
||||
/** Deliver a lens frame on the control notify characteristic. */
|
||||
fun notify(address: String, pb: ByteArray, sid: Int, flag: Int = 0) {
|
||||
for (frame in BleProtocol.framePb(pb, sid, flag, 7)) inbound!!.onNotification(address, BleProtocol.NOTIFY_CHAR_UUID, frame)
|
||||
}
|
||||
fun otaAck(address: String, op: Int, status: Int) {
|
||||
inbound!!.onNotification(address, BleProtocol.OTA_DATA_NOTIFY_UUID, BleProtocol.framePb(byteArrayOf(op.toByte(), status.toByte()), 0xc1, 0, 1).single())
|
||||
}
|
||||
fun drop(address: String) { connected.remove(address); inbound!!.onConnectionStateChange(address, false) }
|
||||
}
|
||||
|
||||
private fun varint(value: Int): ByteArray { var v = value; val out = ArrayList<Byte>(); do { var b = v and 0x7f; v = v ushr 7; if (v != 0) b = b or 0x80; out.add(b.toByte()) } while (v != 0); return out.toByteArray() }
|
||||
private fun field(number: Int, bytes: ByteArray): ByteArray = varint((number shl 3) or 2) + varint(bytes.size) + bytes
|
||||
private fun field(number: Int, text: String): ByteArray = field(number, text.encodeToByteArray())
|
||||
private fun authSuccess(magic: Int) = BleProtocol.encodeVarintField(1, 4) + BleProtocol.encodeVarintField(2, magic) + field(3, ByteArray(0))
|
||||
private fun authPending(magic: Int) = BleProtocol.encodeVarintField(1, 4) + BleProtocol.encodeVarintField(2, magic) + field(3, BleProtocol.encodeVarintField(1, 1))
|
||||
private fun ack(magic: Int) = BleProtocol.encodeVarintField(1, 1) + BleProtocol.encodeVarintField(2, magic)
|
||||
private fun settingsAck(magic: Int, battery: Int = -1, left: String = "", right: String = "", extension: String = ""): ByteArray {
|
||||
var request = ByteArray(0)
|
||||
if (left.isNotEmpty()) request += field(5, left)
|
||||
if (right.isNotEmpty()) request += field(6, right)
|
||||
if (battery >= 0) request += BleProtocol.encodeVarintField(12, battery)
|
||||
var pb = BleProtocol.encodeVarintField(1, 2) + BleProtocol.encodeVarintField(2, magic) + field(4, request)
|
||||
if (extension.isNotEmpty()) pb += field(100, extension)
|
||||
return pb
|
||||
}
|
||||
private fun listSelection(container: String, item: String, index: Int, eventType: Int): ByteArray =
|
||||
field(13, field(1, field(2, container) + field(3, item) + BleProtocol.encodeVarintField(4, index) + BleProtocol.encodeVarintField(5, eventType)))
|
||||
|
||||
private val fast = StockFlowTimings(
|
||||
securityAuthTimeoutMs = 300, securityAuthSoftTimeoutMs = 100, pairingWaitCapMs = 2_000, postBondGraceMs = 20, pollMs = 5,
|
||||
armRetryDelayMs = 5, queryTimeoutMs = 40, preludeTimeoutMs = 40, heartbeatIntervalMs = 15, selectionTimeoutMs = 80, createAckTimeoutMs = 40,
|
||||
batteryAckTimeoutMs = 40, blockAckTimeoutMs = 60, ctrlAckTimeoutMs = 60, firstLensConnectWindowMs = 200, secondLensConnectWindowMs = 200,
|
||||
rebootSettleMs = 5, notifySettleMs = 5, otaRetryDelayMs = 5, componentRetryDelayMs = 5,
|
||||
)
|
||||
|
||||
private const val R = "AA:00:00:00:00:01"
|
||||
private const val L = "AA:00:00:00:00:02"
|
||||
|
||||
private class ProbeEvents : FaceclawDeviceInfoProbeListener {
|
||||
val logs = ArrayList<String>(); val states = ArrayList<String>(); var result: List<String>? = null; var error: String? = null
|
||||
override fun onLog(line: String?) { logs.add(line ?: "") }
|
||||
override fun onState(state: String?, detail: String?) { states.add("$state:$detail") }
|
||||
override fun onResult(leftVersion: String?, rightVersion: String?, extension: String?) { result = listOf(leftVersion ?: "", rightVersion ?: "", extension ?: "") }
|
||||
override fun onError(message: String?) { error = message }
|
||||
}
|
||||
|
||||
/** Standard lens behaviour: auth succeeds, prelude and settings are acked with the given versions/battery. */
|
||||
private fun stockResponder(link: FakeStockLink, left: String = "2.2.9", right: String = "2.2.9", extension: String = "", battery: Int = 85): (FakeStockLink.Written) -> Unit = { w ->
|
||||
when (w.sid) {
|
||||
BleProtocol.SID_SECURITY_AUTH -> link.notify(w.address, authSuccess(w.magic), w.sid)
|
||||
BleProtocol.PRELUDE_ACK_SID -> link.notify(w.address, ack(BleProtocol.PRELUDE_ACK_MAGIC), w.sid)
|
||||
BleProtocol.SID_UI_SETTING -> link.notify(w.address, settingsAck(w.magic, battery, left, right, extension), w.sid)
|
||||
BleProtocol.SID_EVENHUB -> link.notify(w.address, ack(w.magic), w.sid)
|
||||
}
|
||||
}
|
||||
|
||||
class StockFlowsTest {
|
||||
@Test
|
||||
fun probeAuthenticatesBothArmsThenReadsVersions() {
|
||||
val link = FakeStockLink()
|
||||
link.responder = stockResponder(link, "L1", "R1", "Faceclaw/3")
|
||||
val events = ProbeEvents()
|
||||
DeviceInfoProbeFlow(link, R, L, events, fast, testPlatform()).run()
|
||||
assertEquals(listOf("L1", "R1", "Faceclaw/3"), events.result)
|
||||
assertEquals(null, events.error)
|
||||
assertEquals(listOf("connecting:right", "authenticating:right", "connecting:left", "authenticating:left", "querying:right"), events.states)
|
||||
// Right arm then left arm auth, then prelude + one settings read on the right arm only.
|
||||
assertEquals(listOf(R, L), link.writes.filter { it.sid == BleProtocol.SID_SECURITY_AUTH }.map { it.address })
|
||||
assertEquals(listOf(R), link.writes.filter { it.sid == BleProtocol.SID_UI_SETTING }.map { it.address })
|
||||
assertTrue(link.closed)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun probeUsesUnsolicitedSettingsPushAndFallsBackToLeftLens() {
|
||||
// Right lens: auth ok, but the settings read is never acked; instead a push on a
|
||||
// device-chosen magic carries the versions.
|
||||
val pushed = FakeStockLink()
|
||||
pushed.responder = { w ->
|
||||
when (w.sid) {
|
||||
BleProtocol.SID_SECURITY_AUTH -> pushed.notify(w.address, authSuccess(w.magic), w.sid)
|
||||
BleProtocol.PRELUDE_ACK_SID -> pushed.notify(w.address, ack(BleProtocol.PRELUDE_ACK_MAGIC), w.sid)
|
||||
BleProtocol.SID_UI_SETTING -> pushed.notify(w.address, settingsAck(250, -1, "P1", "P2", ""), w.sid)
|
||||
}
|
||||
}
|
||||
var events = ProbeEvents()
|
||||
DeviceInfoProbeFlow(pushed, R, "", events, fast, testPlatform()).run()
|
||||
assertEquals(listOf("P1", "P2", ""), events.result)
|
||||
assertTrue(events.logs.any { it.contains("unsolicited settings push") })
|
||||
|
||||
// Right lens silent on settings (both attempts); the left lens answers.
|
||||
val silentRight = FakeStockLink()
|
||||
silentRight.responder = { w ->
|
||||
when (w.sid) {
|
||||
BleProtocol.SID_SECURITY_AUTH -> silentRight.notify(w.address, authSuccess(w.magic), w.sid)
|
||||
BleProtocol.PRELUDE_ACK_SID -> silentRight.notify(w.address, ack(BleProtocol.PRELUDE_ACK_MAGIC), w.sid)
|
||||
BleProtocol.SID_UI_SETTING -> if (w.address == L) silentRight.notify(w.address, settingsAck(w.magic, -1, "LL", "RR", ""), w.sid)
|
||||
}
|
||||
}
|
||||
events = ProbeEvents()
|
||||
DeviceInfoProbeFlow(silentRight, R, L, events, fast, testPlatform()).run()
|
||||
assertEquals(listOf("LL", "RR", ""), events.result)
|
||||
assertTrue(events.logs.any { it.contains("probing the left lens") })
|
||||
assertEquals(2, silentRight.writes.count { it.sid == BleProtocol.SID_UI_SETTING && it.address == R })
|
||||
assertContains(events.states, "querying:left")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun probeWaitsThroughPairingAndResendsAfterBond() {
|
||||
// Unbonded phone: the first auth request is answered with the pre-encryption
|
||||
// non-success result, the OS then pairs (no writes while BONDING), and once bonded the
|
||||
// request is re-sent after the grace period and finally succeeds.
|
||||
val link = FakeStockLink()
|
||||
link.bond[R] = BondState.NONE
|
||||
val authReplies = ArrayList<Int>()
|
||||
link.responder = { w ->
|
||||
when (w.sid) {
|
||||
BleProtocol.SID_SECURITY_AUTH -> { authReplies.add(w.magic); link.notify(w.address, if (authReplies.size == 1) authPending(w.magic) else authSuccess(w.magic), w.sid) }
|
||||
BleProtocol.PRELUDE_ACK_SID -> link.notify(w.address, ack(BleProtocol.PRELUDE_ACK_MAGIC), w.sid)
|
||||
BleProtocol.SID_UI_SETTING -> link.notify(w.address, settingsAck(w.magic, -1, "a", "b", ""), w.sid)
|
||||
}
|
||||
}
|
||||
startThread("bond-flip", true) { sleepMs(20); link.bond[R] = BondState.BONDING; sleepMs(40); link.bond[R] = BondState.BONDED }
|
||||
val events = ProbeEvents()
|
||||
DeviceInfoProbeFlow(link, R, "", events, fast, testPlatform()).run()
|
||||
assertEquals(listOf("a", "b", ""), events.result, events.logs.joinToString("\n"))
|
||||
assertTrue(events.logs.any { it.contains("is not the success result") })
|
||||
assertTrue(events.logs.any { it.contains("is pairing with the right lens") })
|
||||
assertTrue(events.logs.any { it.contains("bond established") })
|
||||
assertTrue(events.logs.any { it.contains("re-sending the auth request after pairing") })
|
||||
assertEquals(2, authReplies.size)
|
||||
assertTrue(events.logs.any { it.contains("security auth complete: right lens") })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun probeReportsUnreachableArmAndCancel() {
|
||||
val link = FakeStockLink()
|
||||
link.connectResult = { false }
|
||||
val events = ProbeEvents()
|
||||
DeviceInfoProbeFlow(link, R, L, events, fast, testPlatform()).run()
|
||||
assertNotNull(events.error).let { assertTrue(it.contains("could not connect to the right lens"), it) }
|
||||
assertEquals(3, events.states.count { it == "connecting:right" })
|
||||
|
||||
val cancelled = FakeStockLink()
|
||||
cancelled.responder = { w -> if (w.sid == BleProtocol.SID_SECURITY_AUTH) cancelled.notify(w.address, authPending(w.magic), w.sid) }
|
||||
val slow = StockFlowTimings(securityAuthTimeoutMs = 5_000, pollMs = 5)
|
||||
val flow = DeviceInfoProbeFlow(cancelled, R, "", ProbeEvents().also { events.logs.clear() }, slow, testPlatform())
|
||||
startThread("cancel", true) { sleepMs(30); flow.cancel() }
|
||||
val start = testPlatform().elapsedRealtimeMs()
|
||||
flow.run()
|
||||
assertTrue(testPlatform().elapsedRealtimeMs() - start < 2_000)
|
||||
assertTrue(flow.cancelled)
|
||||
}
|
||||
|
||||
private class PromptEvents : FaceclawFlashPromptListener {
|
||||
val logs = ArrayList<String>(); val states = ArrayList<String>(); var battery: Pair<Int, Int>? = null; var result: Boolean? = null
|
||||
override fun onLog(line: String?) { logs.add(line ?: "") }
|
||||
override fun onState(state: String?, detail: String?) { states.add("$state:$detail") }
|
||||
override fun onBattery(rightPercent: Int, leftPercent: Int) { battery = rightPercent to leftPercent }
|
||||
override fun onResult(approved: Boolean) { result = approved }
|
||||
}
|
||||
|
||||
private fun promptResponder(link: FakeStockLink, selection: Int?, batteries: Map<String, Int>): (FakeStockLink.Written) -> Unit = { w ->
|
||||
when (w.sid) {
|
||||
BleProtocol.SID_SECURITY_AUTH -> link.notify(w.address, authSuccess(w.magic), w.sid)
|
||||
BleProtocol.PRELUDE_ACK_SID -> link.notify(w.address, ack(BleProtocol.PRELUDE_ACK_MAGIC), w.sid)
|
||||
BleProtocol.SID_UI_SETTING -> link.notify(w.address, settingsAck(w.magic, batteries[w.address] ?: -1), w.sid)
|
||||
BleProtocol.SID_EVENHUB -> {
|
||||
val cmd = BleProtocol.readVarintFieldValue(BleProtocol.stripTrailingCrc(w.pb), 1, -1)
|
||||
link.notify(w.address, ack(w.magic), w.sid)
|
||||
// Cmd 0 = create page: the user taps a row right away (a scroll first, which must be ignored).
|
||||
if (cmd == 0 && selection != null) {
|
||||
link.notify(w.address, listSelection(FlashPromptFlow.LIST_NAME, "other", 0, BleProtocol.EVENT_SCROLL_TOP), BleProtocol.SID_EVENHUB, BleProtocol.FLAG_NOTIFY)
|
||||
link.notify(w.address, listSelection(FlashPromptFlow.LIST_NAME, FlashPromptFlow.ITEMS[selection], selection, BleProtocol.EVENT_CLICK), BleProtocol.SID_EVENHUB, BleProtocol.FLAG_NOTIFY)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun promptApprovalReadsBothBatteriesAndShutsDown() {
|
||||
val link = FakeStockLink()
|
||||
link.responder = promptResponder(link, 1, mapOf(R to 85, L to 70))
|
||||
val events = PromptEvents()
|
||||
FlashPromptFlow(link, R, L, "warning", false, events, fast, testPlatform()).run()
|
||||
assertEquals(true, events.result, events.logs.joinToString("\n"))
|
||||
assertEquals(85 to 70, events.battery)
|
||||
assertEquals(listOf("connecting:", "connected:", "prompting:", "battery:", "result:approved"), events.states)
|
||||
val hub = link.writes.filter { it.sid == BleProtocol.SID_EVENHUB }.map { BleProtocol.readVarintFieldValue(BleProtocol.stripTrailingCrc(it.pb), 1, -1) }
|
||||
assertEquals(0, hub.first()) // create page
|
||||
assertEquals(9, hub.last()) // shutdown
|
||||
assertTrue(link.writes.all { it.sid != BleProtocol.SID_EVENHUB || it.address == R })
|
||||
assertTrue(link.closed)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun promptDeclineTimeoutAndSkip() {
|
||||
val declined = FakeStockLink()
|
||||
declined.responder = promptResponder(declined, 0, mapOf())
|
||||
var events = PromptEvents()
|
||||
FlashPromptFlow(declined, R, "", "w", false, events, fast, testPlatform()).run()
|
||||
assertEquals(false, events.result)
|
||||
assertEquals("result:declined", events.states.last())
|
||||
assertEquals(null, events.battery)
|
||||
|
||||
val silent = FakeStockLink()
|
||||
silent.responder = promptResponder(silent, null, mapOf())
|
||||
events = PromptEvents()
|
||||
FlashPromptFlow(silent, R, "", "w", false, events, fast, testPlatform()).run()
|
||||
assertEquals(null, events.result)
|
||||
assertEquals("timeout:No response from the glasses.", events.states.last())
|
||||
// Heartbeats (EvenHub cmd 12) kept the page alive while waiting (interval 15 ms, wait 80 ms).
|
||||
assertTrue(silent.writes.count { it.sid == BleProtocol.SID_EVENHUB && BleProtocol.readVarintFieldValue(BleProtocol.stripTrailingCrc(it.pb), 1, -1) == 12 } >= 2)
|
||||
|
||||
val skipped = FakeStockLink()
|
||||
skipped.responder = promptResponder(skipped, null, mapOf(R to 50))
|
||||
events = PromptEvents()
|
||||
FlashPromptFlow(skipped, R, L, "w", true, events, fast, testPlatform()).run()
|
||||
assertEquals(true, events.result)
|
||||
assertEquals(50 to -1, events.battery)
|
||||
assertFalse(skipped.writes.any { it.sid == BleProtocol.SID_EVENHUB })
|
||||
|
||||
val lost = FakeStockLink()
|
||||
lost.responder = { w ->
|
||||
promptResponder(lost, null, mapOf())(w)
|
||||
if (w.sid == BleProtocol.SID_EVENHUB) lost.drop(R)
|
||||
}
|
||||
events = PromptEvents()
|
||||
FlashPromptFlow(lost, R, "", "w", false, events, fast, testPlatform()).run()
|
||||
assertEquals("disconnected:Lost connection to the glasses.", events.states.last())
|
||||
}
|
||||
|
||||
// ---- OTA ---------------------------------------------------------------
|
||||
|
||||
private fun putU32(buf: ByteArray, offset: Int, value: Long) { for (i in 0 until 4) buf[offset + i] = ((value ushr (8 * i)) and 0xff).toByte() }
|
||||
|
||||
private fun container(): ByteArray {
|
||||
val mainApp = ByteArray(6000) { (it * 7).toByte() }.also { putU32(it, 0, 6000L); putU32(it, 0x14, FirmwareImage.APP_LOAD_ADDR) }
|
||||
val components = listOf("ota/bootloader.bin" to ByteArray(300) { it.toByte() }, "ota/a.bin" to ByteArray(64) { 1 }, "ota/b.bin" to ByteArray(128) { 2 }, "ota/c.bin" to ByteArray(32) { 3 }, FirmwareImage.REQUIRED_SEGMENT to mainApp)
|
||||
val toc = FirmwareImage.HEADER_SIZE + components.size * FirmwareImage.TOC_ENTRY_SIZE
|
||||
var offset = toc
|
||||
val offsets = components.map { c -> offset.also { offset += FirmwareImage.SUBHEADER_SIZE + c.second.size } }
|
||||
val img = ByteArray(offset)
|
||||
putU32(img, 8, components.size.toLong())
|
||||
components.forEachIndexed { i, c ->
|
||||
val off = offsets[i]
|
||||
putU32(img, FirmwareImage.HEADER_SIZE + i * 16 + 4, off.toLong())
|
||||
putU32(img, off + 8, c.second.size.toLong())
|
||||
c.first.encodeToByteArray().copyInto(img, off + FirmwareImage.NAME_OFFSET)
|
||||
c.second.copyInto(img, off + FirmwareImage.SUBHEADER_SIZE)
|
||||
val crc = FirmwareImage.crc32cUnsigned(c.second)
|
||||
putU32(img, FirmwareImage.HEADER_SIZE + i * 16 + 12, crc)
|
||||
putU32(img, off + 12, crc)
|
||||
}
|
||||
return img
|
||||
}
|
||||
|
||||
private class FlashEvents : FaceclawFirmwareFlasherListener {
|
||||
val logs = ArrayList<String>(); val states = ArrayList<String>(); val progress = ArrayList<String>(); var complete: Pair<Boolean, String>? = null
|
||||
override fun onLog(line: String?) { logs.add(line ?: "") }
|
||||
override fun onProgress(lens: String?, componentIndex: Int, componentCount: Int, blockIndex: Int, blockCount: Int, bytesSent: Long, bytesTotal: Long) { progress.add("$lens $componentIndex/$componentCount $blockIndex/$blockCount $bytesSent/$bytesTotal") }
|
||||
override fun onState(state: String?, detail: String?) { states.add("$state:$detail") }
|
||||
override fun onComplete(success: Boolean, detail: String?) { complete = success to (detail ?: "") }
|
||||
}
|
||||
|
||||
/** OTA lens: NAKs one block once and fails one END once, exercising both retry paths. */
|
||||
private class OtaLens(val link: FakeStockLink) {
|
||||
val received = HashMap<String, ByteArray>()
|
||||
var nakOnce = true
|
||||
var failEndOnce = true
|
||||
var component = ByteArray(0)
|
||||
fun respond(w: FakeStockLink.Written) {
|
||||
when (w.sid) {
|
||||
BleProtocol.SID_SECURITY_AUTH -> link.notify(w.address, authSuccess(w.magic), w.sid)
|
||||
OtaFlashFlow.SID_CTRL -> {
|
||||
val pb = BleProtocol.stripTrailingCrc(w.pb)
|
||||
when (pb[0].toInt()) {
|
||||
OtaFlashFlow.OP_BEGIN -> link.otaAck(w.address, OtaFlashFlow.OP_BEGIN, 0)
|
||||
OtaFlashFlow.OP_FILE_CHECK -> { component = ByteArray(0); link.otaAck(w.address, OtaFlashFlow.OP_FILE_CHECK, 0) }
|
||||
OtaFlashFlow.OP_END -> {
|
||||
val status = if (failEndOnce) { failEndOnce = false; 5 } else { received[w.address] = (received[w.address] ?: ByteArray(0)) + component; 9 }
|
||||
link.otaAck(w.address, OtaFlashFlow.OP_END, status)
|
||||
}
|
||||
OtaFlashFlow.OP_BLOCK -> {}
|
||||
}
|
||||
}
|
||||
OtaFlashFlow.SID_DATA -> {
|
||||
val block = BleProtocol.stripTrailingCrc(w.pb)
|
||||
if (nakOnce) { nakOnce = false; link.otaAck(w.address, OtaFlashFlow.OP_BLOCK, 1) } else { component += block; link.otaAck(w.address, OtaFlashFlow.OP_BLOCK, 0) }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun otaFlashesBothLensesWithBlockNakAndEndRetry() {
|
||||
val img = container()
|
||||
val segs = FirmwareImage.validate(img)
|
||||
val link = FakeStockLink()
|
||||
val lens = OtaLens(link)
|
||||
link.responder = lens::respond
|
||||
val events = FlashEvents()
|
||||
OtaFlashFlow(link, R, L, "fw.bin", events, fast, testPlatform()) { img }.run()
|
||||
assertEquals(true to "Both lenses flashed. The glasses are rebooting into the custom firmware.", events.complete, events.logs.joinToString("\n"))
|
||||
assertEquals(listOf("validating:", "connecting:left", "flashing:left", "rebooting:Left lens done. Both lenses reboot briefly; reconnecting for the right lens.", "connecting:right", "flashing:right", "done:"), events.states)
|
||||
// Every component payload arrived intact on both lenses, in order.
|
||||
val expected = segs.fold(ByteArray(0)) { acc, s -> acc + img.copyOfRange(s.payloadStart, s.payloadStart + s.ps) }
|
||||
assertContentEquals(expected, lens.received[L]!!)
|
||||
assertContentEquals(expected, lens.received[R]!!)
|
||||
assertTrue(events.logs.any { it.contains("NAK=1 resend 1/3") })
|
||||
assertTrue(events.logs.any { it.contains("END verify FAILED (status 5)") })
|
||||
assertTrue(events.logs.any { it.contains("re-flash attempt 2/3") })
|
||||
assertEquals("left 5/5 2/2 6524/6524", events.progress.first { it.startsWith("left 5/5 2") })
|
||||
// Marker + data share one envelope seq; the OTA stream restarts at 1 per lens.
|
||||
val leftOta = link.writes.filter { it.address == L && it.uuid == BleProtocol.OTA_DATA_WRITE_UUID }
|
||||
assertEquals(1, leftOta.first().seq)
|
||||
val firstBlock = leftOta.indexOfFirst { it.sid == OtaFlashFlow.SID_DATA }
|
||||
assertEquals(leftOta[firstBlock - 1].seq, leftOta[firstBlock].seq)
|
||||
assertEquals(setOf(BleProtocol.WRITE_CHAR_UUID), link.writes.filter { it.sid == BleProtocol.SID_SECURITY_AUTH }.map { it.uuid }.toSet())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun otaGivesUpWhenLensConnectWindowExpires() {
|
||||
val link = FakeStockLink()
|
||||
link.connectResult = { false }
|
||||
val events = FlashEvents()
|
||||
OtaFlashFlow(link, R, L, "fw.bin", events, fast, testPlatform()) { container() }.run()
|
||||
assertEquals(false to "could not reach left lens: connect/discover incomplete", events.complete)
|
||||
assertEquals("error:could not reach left lens: connect/discover incomplete", events.states.last())
|
||||
assertTrue(events.logs.count { it.contains("left connect attempt") } >= 2)
|
||||
assertTrue(link.closed)
|
||||
|
||||
val unreadable = FlashEvents()
|
||||
OtaFlashFlow(FakeStockLink(), R, L, "/nonexistent/fw.bin", unreadable, fast, testPlatform()).run()
|
||||
assertTrue(unreadable.complete!!.second.startsWith("could not read firmware"), unreadable.complete!!.second)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,273 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertNull
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class VoiceCaptureSessionTest {
|
||||
/** Real clock plus an offset the test can advance to force decode cadences. */
|
||||
private class ShiftedPlatform(private val base: ProtocolPlatform) : ProtocolPlatform by base {
|
||||
var offsetMs = 0L
|
||||
override fun elapsedRealtimeMs(): Long = base.elapsedRealtimeMs() + offsetMs
|
||||
}
|
||||
|
||||
private class Events : FaceclawVoiceControllerListener {
|
||||
val statuses = mutableListOf<String?>()
|
||||
val transcripts = mutableListOf<Pair<String?, Boolean>>()
|
||||
val pcmChunks = mutableListOf<ByteArray>()
|
||||
val frameMeta = mutableListOf<Pair<Int, Int>>()
|
||||
var speechEnds = 0
|
||||
var verified: Pair<Boolean, Float>? = null
|
||||
val stopped = mutableListOf<Int>()
|
||||
val stoppedLatch = Latch(1, testPlatform())
|
||||
override fun onStatus(status: String?) { statuses.add(status) }
|
||||
override fun onTranscript(text: String?, isFinal: Boolean) { transcripts.add(text to isFinal) }
|
||||
override fun onStopped(captureId: Int) { stopped.add(captureId); stoppedLatch.countDown() }
|
||||
override fun onPcm(pcm16le: ByteArray?) { pcmChunks.add(pcm16le!!) }
|
||||
override fun onFrameMeta(angleDegrees: Int, ssr: Int) { frameMeta.add(angleDegrees to ssr) }
|
||||
override fun onSpeechEnd() { speechEnds++ }
|
||||
override fun onSpeakerVerified(isWearer: Boolean, similarity: Float) { verified = isWearer to similarity }
|
||||
}
|
||||
|
||||
/** Scripted phone mic: each entry is a chunk amplitude; after the script the read fails (-1). */
|
||||
private class ScriptedMic(private val amplitudes: List<Int>, private val onRead: () -> Unit = {}) : PcmAudioSource {
|
||||
var index = 0
|
||||
var closed = false
|
||||
override fun read(pcm: ShortArray): Int {
|
||||
onRead()
|
||||
if (index >= amplitudes.size) return -1
|
||||
val amplitude = amplitudes[index++]
|
||||
for (i in pcm.indices) pcm[i] = (if (i % 2 == 0) amplitude else -amplitude).toShort()
|
||||
return pcm.size
|
||||
}
|
||||
override fun close() { closed = true }
|
||||
}
|
||||
|
||||
private class FakeTranscriber(private val text: (Int) -> String) : OfflineTranscriber {
|
||||
val calls = mutableListOf<Int>()
|
||||
var released = false
|
||||
override fun recognize(samples: FloatArray, count: Int): String { calls.add(count); return text(count) }
|
||||
override fun release() { released = true }
|
||||
}
|
||||
|
||||
private class FakeHost(
|
||||
val platform: ProtocolPlatform,
|
||||
var mic: PcmAudioSource? = null,
|
||||
var transcriber: OfflineTranscriber? = null,
|
||||
var hasModel: Boolean = true,
|
||||
var embedding: FloatArray? = null,
|
||||
var g2Ready: Boolean = false,
|
||||
) : VoiceCaptureHost {
|
||||
override val dispatcher = CallbackDispatcher { it() }
|
||||
var packetListener: FaceclawAudioPacketListener? = null
|
||||
var g2Stops = 0
|
||||
val recordings = mutableListOf<ByteArray>()
|
||||
override fun onSessionStarting() {}
|
||||
override fun isG2SessionReady(): Boolean = g2Ready
|
||||
override fun isG2AudioCaptureActive(): Boolean = packetListener != null
|
||||
override fun g2AudioSource(): PacketAudioSource? = if (!g2Ready) null else object : PacketAudioSource {
|
||||
override fun start(listener: FaceclawAudioPacketListener): Boolean { packetListener = listener; return true }
|
||||
override fun stop() { g2Stops++ }
|
||||
override fun isActive(): Boolean = packetListener != null
|
||||
}
|
||||
override fun createPacketDecoder(): AudioPacketDecoder = object : AudioPacketDecoder {
|
||||
override fun decodePacket(packet: ByteArray?, pcmOut: ShortArray?): Int {
|
||||
if (packet == null || packet.size != Lc3PacketFramer.PACKET_BYTES) return 0
|
||||
pcmOut!!.fill(packet[0].toShort())
|
||||
lastAngleDegrees = Lc3PacketFramer.readTrailerS16(packet, Lc3PacketFramer.ANGLE_OFFSET)
|
||||
lastSsr = Lc3PacketFramer.readTrailerS16(packet, Lc3PacketFramer.SSR_OFFSET)
|
||||
realPackets++
|
||||
return Lc3PacketFramer.SAMPLES_PER_PACKET
|
||||
}
|
||||
override var lastAngleDegrees = 0
|
||||
override var lastSsr = 0
|
||||
override var realPackets = 0L
|
||||
override val duplicatePackets = 0L
|
||||
override val missingPackets = 0L
|
||||
override val decodeErrors = 0L
|
||||
override fun close() {}
|
||||
}
|
||||
override fun openPhoneMic(): PcmAudioSource? = mic
|
||||
override fun hasTranscriberModel(kind: VoiceModelKind): Boolean = hasModel
|
||||
override fun loadTranscriber(kind: VoiceModelKind): OfflineTranscriber = transcriber ?: error("no transcriber")
|
||||
override fun speakerEmbedder(modelPath: String): SpeakerEmbedder = object : SpeakerEmbedder {
|
||||
override fun embed(pcm16le: ByteArray, sampleRate: Int): FloatArray? = embedding
|
||||
override fun release() {}
|
||||
}
|
||||
override fun recordingSink(): RecordingSink = RecordingSink { bytes, _ -> recordings.add(bytes) }
|
||||
}
|
||||
|
||||
private val logs = mutableListOf<String>()
|
||||
|
||||
private fun session(host: VoiceCaptureHost, platform: ProtocolPlatform, name: String) =
|
||||
VoiceCaptureSession(host, platform, name, { logs.add("I $it") }, { logs.add("W $it") }, { m, e -> logs.add("E $m ${e.message}") })
|
||||
|
||||
private fun packet(value: Int, angle: Int, ssr: Int): ByteArray {
|
||||
val bytes = ByteArray(Lc3PacketFramer.PACKET_BYTES)
|
||||
bytes[0] = value.toByte()
|
||||
bytes[Lc3PacketFramer.SSR_OFFSET] = (ssr and 0xff).toByte()
|
||||
bytes[Lc3PacketFramer.SSR_OFFSET + 1] = (ssr shr 8).toByte()
|
||||
bytes[Lc3PacketFramer.ANGLE_OFFSET] = (angle and 0xff).toByte()
|
||||
bytes[Lc3PacketFramer.ANGLE_OFFSET + 1] = (angle shr 8).toByte()
|
||||
return bytes
|
||||
}
|
||||
|
||||
@Test
|
||||
fun beamGateWrapsAroundAndClampsHalfWidth() {
|
||||
val session = session(FakeHost(testPlatform()), testPlatform(), "voice-test-beam")
|
||||
session.setBeamFilter(true, 170, 30)
|
||||
assertTrue(session.withinBeam(170))
|
||||
assertTrue(session.withinBeam(-170)) // 20 degrees away across the wrap
|
||||
assertFalse(session.withinBeam(120))
|
||||
session.setBeamFilter(true, 0, 1) // clamps to 5
|
||||
assertTrue(session.withinBeam(5))
|
||||
assertFalse(session.withinBeam(6))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun droppingQueueKeepsNewestAndCountsDrops() {
|
||||
val queue = DroppingPacketQueue<Int>(3, testPlatform())
|
||||
for (i in 1..5) queue.put(i)
|
||||
assertEquals(2L, queue.droppedCount)
|
||||
assertEquals(3, queue.take(10) { true })
|
||||
assertEquals(4, queue.take(10) { true })
|
||||
assertEquals(5, queue.take(10) { true })
|
||||
assertNull(queue.take(10, { true }))
|
||||
assertNull(queue.take(10, { false }))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun g2PacketsAreCountedGatedAndForwarded() {
|
||||
val platform = ShiftedPlatform(testPlatform())
|
||||
val host = FakeHost(platform, g2Ready = true)
|
||||
val events = Events()
|
||||
val session = session(host, platform, "voice-test-g2")
|
||||
session.setListener(events)
|
||||
session.setBeamFilter(true, 0, 30)
|
||||
session.start("cloud", 7)
|
||||
val deadline = platform.elapsedRealtimeMs() + 3000
|
||||
while (host.packetListener == null && platform.elapsedRealtimeMs() < deadline) sleepMs(5)
|
||||
val listener = host.packetListener!!
|
||||
listener.onAudioPacket(packet(1, 10, 5), "L", 1000)
|
||||
listener.onAudioPacket(packet(2, 90, 5), "L", 1050) // outside the beam: meta only
|
||||
listener.onAudioPacket(packet(3, 0, 0), "R", 1200) // wrong arm, late (150 ms gap), ssr 0 passes the gate
|
||||
listener.onAudioPacket(ByteArray(3), "L", 1230) // bad length: dropped by the decoder
|
||||
val waitUntil = platform.elapsedRealtimeMs() + 3000
|
||||
while (events.pcmChunks.size < 2 && platform.elapsedRealtimeMs() < waitUntil) sleepMs(5)
|
||||
session.stop()
|
||||
assertTrue(events.stoppedLatch.await(3000))
|
||||
val stats = session.audioStats()
|
||||
assertEquals(4L, stats.queuedPackets)
|
||||
assertEquals(1L, stats.wrongArmPackets)
|
||||
assertEquals(1L, stats.latePackets)
|
||||
assertEquals(150L, stats.maxInterPacketMs)
|
||||
assertEquals(2, events.pcmChunks.size)
|
||||
assertEquals(Lc3PacketFramer.SAMPLES_PER_PACKET * 2, events.pcmChunks[0].size)
|
||||
assertEquals(listOf(10 to 5, 90 to 5, 0 to 0), events.frameMeta)
|
||||
assertEquals(listOf(7), events.stopped)
|
||||
assertTrue(host.g2Stops >= 1)
|
||||
assertTrue(events.statuses.contains("Listening (cloud)..."))
|
||||
assertTrue(events.transcripts.isEmpty())
|
||||
assertTrue(logs.any { it.startsWith("I G2 mic packets=4 decoded=3") && it.contains("wrongArm=1") }, logs.toString())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun startRefusesWithoutGlassesAndWhenAlreadyRunning() {
|
||||
val host = FakeHost(testPlatform(), g2Ready = false)
|
||||
val events = Events()
|
||||
val session = session(host, testPlatform(), "voice-test-refuse")
|
||||
session.setListener(events)
|
||||
session.start("onboard", 3)
|
||||
assertEquals<List<String?>>(listOf("Voice control needs an active G2 connection."), events.statuses)
|
||||
assertEquals(listOf(3), events.stopped)
|
||||
assertFalse(session.isCapturing())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun onboardTranscriptCommitsSegmentsAndFinalizesWithReplaceSemantics() {
|
||||
val platform = ShiftedPlatform(testPlatform())
|
||||
// 170 chunks of 800 samples = 8.5 s: one 8 s segment commit plus a tail.
|
||||
val mic = ScriptedMic(List(170) { 3000 }) { platform.offsetMs += 1000 }
|
||||
val transcriber = FakeTranscriber { "x" }
|
||||
val host = FakeHost(platform, mic = mic, transcriber = transcriber)
|
||||
val events = Events()
|
||||
val session = session(host, platform, "voice-test-onboard")
|
||||
session.setListener(events)
|
||||
session.setUsePhoneMic(true)
|
||||
session.setSaveRecordings(true)
|
||||
session.start("onboard", 1)
|
||||
assertTrue(events.stoppedLatch.await(10000))
|
||||
assertTrue(mic.closed)
|
||||
assertTrue(transcriber.released)
|
||||
assertEquals("Loading transcription model...", events.statuses[0])
|
||||
assertEquals("Listening...", events.statuses[1])
|
||||
val finals = events.transcripts.filter { it.second }
|
||||
assertEquals(1, finals.size)
|
||||
assertEquals("x x", finals.single().first)
|
||||
assertEquals("x" to false, events.transcripts.first())
|
||||
assertTrue(events.transcripts.any { it == ("x x" to false) })
|
||||
// Every segment handed to the model is at most 8 s and was peak-normalized.
|
||||
assertTrue(transcriber.calls.all { it in 1..VoiceCaptureSession.TRANSCRIPT_SEGMENT_MAX_SAMPLES })
|
||||
assertTrue(logs.any { it.startsWith("I Moonshine decode final=true") }, logs.toString())
|
||||
assertEquals(1, host.recordings.size)
|
||||
assertEquals(170 * 800 * 2, host.recordings[0].size)
|
||||
assertEquals(170, events.pcmChunks.size)
|
||||
assertEquals("x x", VoiceCaptureSession.joinTranscript("x", "x"))
|
||||
assertEquals("x, y", VoiceCaptureSession.joinTranscript("x", ", y"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun endpointingFiresOnceAfterSpeechThenSilence() {
|
||||
val platform = ShiftedPlatform(testPlatform())
|
||||
val script = List(8) { 0 } + List(10) { 3000 } + List(24) { 0 }
|
||||
val host = FakeHost(platform, mic = ScriptedMic(script))
|
||||
val events = Events()
|
||||
val session = session(host, platform, "voice-test-endpoint")
|
||||
session.setListener(events)
|
||||
session.setUsePhoneMic(true)
|
||||
session.setEndpointing(true)
|
||||
session.start("cloud", 2)
|
||||
assertTrue(events.stoppedLatch.await(10000))
|
||||
assertEquals(1, events.speechEnds)
|
||||
assertNull(events.verified)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun speakerVerificationFailsOpenAndComparesEmbeddings() {
|
||||
fun run(embedding: FloatArray?, chunks: Int): Pair<Boolean, Float>? {
|
||||
val platform = ShiftedPlatform(testPlatform())
|
||||
val host = FakeHost(platform, mic = ScriptedMic(List(chunks) { 1000 }), embedding = embedding)
|
||||
val events = Events()
|
||||
val session = session(host, platform, "voice-test-verify")
|
||||
session.setListener(events)
|
||||
session.setUsePhoneMic(true)
|
||||
session.setSpeakerVerification("model", floatArrayOf(1f, 0f), 0.8f)
|
||||
session.start("cloud", 4)
|
||||
assertTrue(events.stoppedLatch.await(10000))
|
||||
return events.verified
|
||||
}
|
||||
assertEquals<Pair<Boolean, Float>?>(true to 0f, run(floatArrayOf(1f, 0f), 10)) // under 1 s: too short, fails open
|
||||
assertEquals<Pair<Boolean, Float>?>(true to 0f, run(null, 30)) // no embedding: fails open
|
||||
assertEquals<Pair<Boolean, Float>?>(true to 0f, run(floatArrayOf(1f), 30)) // dimension mismatch: fails open
|
||||
assertEquals<Pair<Boolean, Float>?>(true to 1f, run(floatArrayOf(1f, 0f), 30))
|
||||
assertEquals<Pair<Boolean, Float>?>(false to 0f, run(floatArrayOf(0f, 1f), 30))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun missingModelReportsAndStops() {
|
||||
val host = FakeHost(testPlatform(), mic = ScriptedMic(emptyList()), hasModel = false)
|
||||
val events = Events()
|
||||
val session = session(host, testPlatform(), "voice-test-nomodel")
|
||||
session.setListener(events)
|
||||
session.setUsePhoneMic(true)
|
||||
session.start("onboard", 9)
|
||||
assertTrue(events.stoppedLatch.await(5000))
|
||||
assertEquals<List<String?>>(listOf("Voice model not downloaded (see Settings > Voice)."), events.statuses)
|
||||
assertEquals(listOf(9), events.stopped)
|
||||
assertEquals("1.50", formatFixed(1.5, 2))
|
||||
assertEquals("-0.333", formatFixed(-1.0 / 3, 3))
|
||||
assertEquals("0", formatFixed(0.4, 0))
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user