Merge branch 'main' into ring-health-fixes

This commit is contained in:
James Babcock committed 2026-10-25 01:06:38 -07:00
commit 4f8c082b52
368 files changed
+26238 -3401

No files matched your search

@@ -10,9 +10,11 @@ import kotlin.test.*
*/
class PreviewAnimationListenerTest {
// FrameDisplayListTest's menu golden (a 2x2 highlight sliding in to rest at (3, 2)),
// restarted at 0 of its 240 ms so the slide is still in flight at the first pull.
// restarted at 1 of its 240 ms so the slide is still in flight at the first pull. Not 0:
// at exactly 0 the highlight is wholly above this 8x8 screen, so a pull in the same
// millisecond as the submit (a warm JIT) would match the settled frame.
private val menu = "077f0000000300020002000200022a00000096000000010002000200ffffffff0200080000ff24017e30020000000001812c8001812c1611010017328001812c16103001812c3023414031ff24017c30020000000001812c8001812c1611010017328001812c16103001812c30234140310200020001000103100400000000000000001f"
.chunked(2).map { it.toInt(16).toByte() }.toByteArray().also { it.fill(0, 18, 22) }
.chunked(2).map { it.toInt(16).toByte() }.toByteArray().also { it.fill(0, 18, 22); it[18] = 1 }
private fun settledPreview(): ByteArray {
val reader = ArrayByteReader(menu)
@@ -0,0 +1,24 @@
package com.faceclaw.app
import java.io.File
import kotlin.test.Test
import kotlin.test.assertEquals
class WavRecorderRepairTest {
@Test
fun repairsTheHeaderOfARecordingThatWasNeverFinished() {
val file = File.createTempFile("unfinished", ".wav")
try {
// What a process death mid-recording leaves: the empty header from the start, the PCM
// written since, and a torn half-sample at the end.
file.writeBytes(BinaryEncoding.wavHeader(0, 16000, 1) + ByteArray(3201) { 1 })
assertEquals(3200L, FaceclawWavRecorder.repairHeader(file.path, 16000, 1))
val bytes = file.readBytes()
assertEquals(44 + 3200, bytes.size)
assertEquals(BinaryEncoding.wavHeader(3200, 16000, 1).toList(), bytes.copyOf(44).toList())
assertEquals(-1L, FaceclawWavRecorder.repairHeader(file.path + ".missing", 16000, 1))
} finally {
file.delete()
}
}
}
@@ -90,6 +90,7 @@ class CaptionEngineCoreTest {
}
override fun onSpeechStart(startMs: Long) { events.add("start:$startMs"); startLatch.countDown() }
override fun onStatus(status: String?) { events.add("status:$status") }
override fun onStopped(error: String?) { events.add("stopped:$error") }
}
val host = object : CaptionHost {
override val dispatcher = CallbackDispatcher { it() }
@@ -123,5 +124,105 @@ class CaptionEngineCoreTest {
assertEquals("status:Captions listening...", events[1])
assertEquals("start:0", events[2])
assertEquals("utterance:hi:2:0", events[3])
assertEquals("stopped:null", events.last())
}
/** Records listener events; `stopped` opens once the worker reports onStopped. */
private class RecordingListener : FaceclawCaptionEngineListener {
val events = mutableListOf<String>()
val stopped = Latch(1, testPlatform())
var utteranceLatch = Latch(1, testPlatform())
override fun onUtterance(text: String?, embedding: FloatArray?, startMs: Long, endMs: Long, peakRms: Double) {
events.add("utterance:$text")
utteranceLatch.countDown()
}
override fun onSpeechStart(startMs: Long) {}
override fun onStatus(status: String?) {}
override fun onStopped(error: String?) {
events.add("stopped:$error")
stopped.countDown()
}
// Read only after a latch: callbacks run on the worker (the test dispatcher is inline).
fun snapshot(): List<String> = events.toList()
}
private fun captionHost(transcriber: () -> OfflineTranscriber?) = object : CaptionHost {
override val dispatcher = CallbackDispatcher { it() }
override fun loadTranscriber(modelDir: String): OfflineTranscriber? = transcriber()
override fun loadEmbedder(modelPath: String): SpeakerEmbedder? = null
}
private fun speak(session: CaptionSession, loudChunks: Int = 10, quietChunks: Int = 8) {
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) }
repeat(loudChunks) { session.acceptPcm(loud) }
repeat(quietChunks) { session.acceptPcm(quiet) }
}
@Test
fun captionSessionSurvivesAnUtteranceTheModelRejects() {
var calls = 0
val transcriber = object : OfflineTranscriber {
override fun recognize(samples: FloatArray, count: Int): String {
calls++
if (calls == 1) throw IllegalStateException("bad input")
return "second"
}
override fun release() {}
}
val listener = RecordingListener()
val errors = mutableListOf<String>()
val session = CaptionSession(captionHost { transcriber }, testPlatform(), "caption-reject-test") { m, _ -> errors.add(m) }
session.setListener(listener)
session.setAsrModelDir("models")
session.setSilenceMs(300)
session.start()
speak(session)
assertTrue(listener.utteranceLatch.await(5000))
listener.utteranceLatch = Latch(1, testPlatform())
speak(session)
assertTrue(listener.utteranceLatch.await(5000))
session.stop()
// The rejected utterance still arrives (without text), and the worker kept going.
assertEquals(listOf("utterance:", "utterance:second", "stopped:null"), listener.snapshot())
assertEquals(listOf("caption recognition failed"), errors.toList())
}
@Test
fun requestStopDeliversTheTrailingUtteranceBeforeStopped() {
val transcriber = object : OfflineTranscriber {
override fun recognize(samples: FloatArray, count: Int): String = "last words"
override fun release() {}
}
val listener = RecordingListener()
val session = CaptionSession(captionHost { transcriber }, testPlatform(), "caption-flush-test") { m, e -> error("$m: $e") }
session.setListener(listener)
session.setAsrModelDir("models")
session.start()
// Speech with no closing silence: the utterance is still open when the stop lands.
speak(session, loudChunks = 10, quietChunks = 0)
session.requestStop()
assertTrue(listener.stopped.await(5000))
assertEquals(listOf("utterance:last words", "stopped:null"), listener.snapshot())
}
@Test
fun aModelLoadFailureReportsStoppedWithTheError() {
val listener = RecordingListener()
val session = CaptionSession(
captionHost { throw IllegalStateException("model corrupt") },
testPlatform(),
"caption-load-test",
) { _, _ -> }
session.setListener(listener)
session.setAsrModelDir("models")
session.start()
assertTrue(listener.stopped.await(5000))
assertEquals(listOf("stopped:model corrupt"), listener.snapshot())
// The dead worker left the session stopped, so a later stop is a no-op.
session.stop()
session.requestStop()
}
}
@@ -0,0 +1,33 @@
package com.faceclaw.app
import kotlin.test.Test
import kotlin.test.assertEquals
class ConnectionCountersTest {
@Test
fun drainReturnsTheCountsSinceTheLastDrain() {
ConnectionCounters.drain()
ConnectionCounters.increment("g2.connect-attempt")
ConnectionCounters.increment("g2.connect-attempt")
ConnectionCounters.increment("g2.session-ready")
assertEquals("""{"g2.connect-attempt":2,"g2.session-ready":1}""", ConnectionCounters.drain())
assertEquals("{}", ConnectionCounters.drain())
}
@Test
fun keepsCountingKnownNamesPastTheNameCap() {
ConnectionCounters.drain()
for (index in 0 until 250) ConnectionCounters.increment("name-$index")
ConnectionCounters.increment("name-0")
val drained = Json.parseObject(ConnectionCounters.drain())
assertEquals(200, drained.size)
assertEquals(JsonNumber("2"), drained["name-0"])
}
@Test
fun slugsFailureReasons() {
assertEquals("cfw-recovery-retry-limit", ConnectionCounters.slug("CFW recovery retry limit"))
assertEquals("left-arm-not-found", ConnectionCounters.slug("left arm not found"))
assertEquals("evenhub-resume-prelude-failed", ConnectionCounters.slug("EvenHub resume prelude failed"))
}
}
@@ -453,7 +453,7 @@ class GlassesSessionCoreTest {
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.host.hasLog("Transport failure: create layout ack timeout"))
assertTrue(s.link.disconnected.containsAll(listOf(RIGHT, LEFT)))
s.core.disconnect()
assertEquals("disconnected", s.listener.phases.last())
@@ -33,6 +33,41 @@ class GraphicsCoresTest {
assertContentEquals(byteArrayOf(0x2c, 1, 2, 0), wide.copyOfRange(0, 4))
}
@Test
fun lightBackgroundsInvertBeforeTheToneCurve() {
val white = 0xffffffff.toInt()
val black = 0xff000000.toInt()
// A white chart with a black stroke and a fully transparent corner.
val chart = IntArray(10 * 10) { white }
chart[55] = black
chart[0] = 0x00ffffff
assertTrue(GrayPacket.hasLightBackground(chart, 10, 10))
val inverted = GrayPacket.fromArgb(chart, 10, 10, 2.2f, false, true)
assertEquals(0, inverted[4 + 1].toInt() and 0xff) // background goes black
assertEquals(255, inverted[4 + 55].toInt() and 0xff) // stroke goes white
assertEquals(0, inverted[4 + 0].toInt() and 0xff) // transparent stays black
// Mid-gray inverts to its sRGB complement, then takes the tone curve.
chart[56] = 0xff404040.toInt()
val curve = GrayPacket.toneCurve(2.2f)
assertEquals(curve[255 - 0x40], GrayPacket.fromArgb(chart, 10, 10, 2.2f, false, true)[4 + 56].toInt() and 0xff)
// Without the flag the conversion is unchanged.
assertContentEquals(GrayPacket.fromArgb(chart, 10, 10, 2.2f, false), GrayPacket.fromArgb(chart, 10, 10, 2.2f, false, false))
}
@Test
fun darkBackgroundsAndPhotosKeepTheirTones() {
// A black chart with a white stroke: already light-on-dark.
val dark = IntArray(10 * 10) { 0xff000000.toInt() }
dark[55] = 0xffffffff.toInt()
assertTrue(!GrayPacket.hasLightBackground(dark, 10, 10))
assertContentEquals(GrayPacket.fromArgb(dark, 10, 10, 1f, false), GrayPacket.fromArgb(dark, 10, 10, 1f, false, true))
// A photo-like spread of tones, even a bright one, has no background to invert.
val ramp = IntArray(16 * 16) { i -> val v = 64 + (i * 191) / 255; 0xff000000.toInt() or (v shl 16) or (v shl 8) or v }
assertTrue(!GrayPacket.hasLightBackground(ramp, 16, 16))
// A mid-gray background is not light enough to be worth inverting.
assertTrue(!GrayPacket.hasLightBackground(IntArray(8 * 8) { 0xff808080.toInt() }, 8, 8))
}
@Test
fun toneCurveDarkensMidtones() {
val identity = GrayPacket.toneCurve(1f)
@@ -0,0 +1,161 @@
package com.faceclaw.app
import kotlin.math.pow
import kotlin.math.roundToInt
import kotlin.test.*
class NotificationIconArtTest {
private val outputSize = 12
private val size = outputSize * NotificationIconArt.ANALYSIS_SCALE
private val clear = 0
private val white = argb(255, 255, 255, 255)
private val black = argb(255, 0, 0, 0)
private val blue = argb(255, 0, 0, 255)
private val green = argb(255, 20, 160, 70)
private fun argb(a: Int, r: Int, g: Int, b: Int): Int = (a shl 24) or (r shl 16) or (g shl 8) or b
private fun icon(pixel: (x: Int, y: Int) -> Int): IntArray = IntArray(size * size) { pixel(it % size, it / size) }
private fun inCircle(x: Int, y: Int, cx: Double, cy: Double, r: Double): Boolean {
val dx = x + 0.5 - cx
val dy = y + 0.5 - cy
return dx * dx + dy * dy <= r * r
}
/** A rounded square inset from the image edge. */
private fun inRoundedSquare(x: Int, y: Int, inset: Int, radius: Int): Boolean {
val lo = inset
val hi = size - 1 - inset
if (x < lo || x > hi || y < lo || y > hi) return false
val cx = x.coerceIn(lo + radius, hi - radius)
val cy = y.coerceIn(lo + radius, hi - radius)
return inCircle(x, y, cx + 0.5, cy + 0.5, radius + 0.5)
}
/** A horizontal bar across the middle, in analysis pixels aligned to output pixels. */
private fun inBar(x: Int, y: Int): Boolean = y in 10..13 && x in 6..17
private fun gray(art: NotificationIconArt.Art, ox: Int, oy: Int): Int = art.gray[oy * outputSize + ox].toInt() and 0xff
@Test fun emptyIconIsEmpty() {
val art = NotificationIconArt.convert(IntArray(size * size), outputSize)
assertEquals(NotificationIconArt.Kind.EMPTY, art.kind)
assertTrue(art.gray.all { it.toInt() == 0 })
}
@Test fun ringWithNoisyColorsIsATemplate() {
// Discord-style: the alpha is a clean shape with a hole, the colors are garbage.
var seed = 12345
val pixels = icon { x, y ->
seed = seed * 1103515245 + 12345
val inRing = inCircle(x, y, 12.0, 12.0, 11.0) && !inCircle(x, y, 12.0, 12.0, 5.0)
if (inRing) argb(255, (seed ushr 8) and 0xff, (seed ushr 16) and 0xff, (seed ushr 4) and 0xff) else clear
}
val art = NotificationIconArt.convert(pixels, outputSize)
assertEquals(NotificationIconArt.Kind.TEMPLATE, art.kind)
assertContentEquals(NotificationIconArt.template(pixels, outputSize).gray, art.gray)
assertEquals(255, gray(art, 1, 6))
assertEquals(0, gray(art, 6, 6))
}
@Test fun darkColoredTemplateStillLightsFully() {
// A plus sign filled pure blue: by brightness it would all but vanish.
val pixels = icon { x, y -> if ((x in 8..15 && y in 2..21) || (y in 8..15 && x in 2..21)) blue else clear }
val art = NotificationIconArt.convert(pixels, outputSize)
assertEquals(NotificationIconArt.Kind.TEMPLATE, art.kind)
assertEquals(255, gray(art, 6, 2))
assertEquals(0, gray(art, 1, 1))
}
@Test fun faintTemplateIsNormalizedToFullBrightness() {
// A theme tint like #8A000000 leaves the whole shape at 54% alpha.
val pixels = icon { x, y -> if (x in 4..19 && y in 4..19 && !(x in 8..15 && y in 8..15)) argb(138, 0, 0, 0) else clear }
val art = NotificationIconArt.convert(pixels, outputSize)
assertEquals(NotificationIconArt.Kind.TEMPLATE, art.kind)
assertEquals(255, gray(art, 2, 2))
}
@Test fun squareWithCutOutIsATemplate() {
val pixels = icon { x, y -> if (inBar(x, y)) clear else white }
assertEquals(NotificationIconArt.Kind.TEMPLATE, NotificationIconArt.convert(pixels, outputSize).kind)
}
@Test fun oneColorDiscIsSolid() {
val pixels = icon { x, y -> if (inCircle(x, y, 12.0, 12.0, 11.0)) blue else clear }
val art = NotificationIconArt.convert(pixels, outputSize)
assertEquals(NotificationIconArt.Kind.SOLID, art.kind)
assertEquals(255, gray(art, 6, 6))
assertEquals(0, gray(art, 0, 0))
}
@Test fun lightGlyphOnColoredTileIsExtracted() {
val pixels = icon { x, y ->
when {
!inRoundedSquare(x, y, 0, 5) -> clear
inBar(x, y) -> white
else -> green
}
}
val art = NotificationIconArt.convert(pixels, outputSize)
assertEquals(NotificationIconArt.Kind.GLYPH, art.kind)
assertEquals(255, gray(art, 6, 5))
assertEquals(0, gray(art, 6, 2))
assertEquals(0, gray(art, 1, 6))
}
@Test fun darkGlyphOnWhiteCircleIsLitNotTheCircle() {
val pixels = icon { x, y ->
when {
!inCircle(x, y, 12.0, 12.0, 11.5) -> clear
inBar(x, y) -> black
else -> white
}
}
val art = NotificationIconArt.convert(pixels, outputSize)
assertEquals(NotificationIconArt.Kind.GLYPH, art.kind)
assertEquals(255, gray(art, 6, 5))
assertEquals(0, gray(art, 6, 2))
}
@Test fun tileWithTransparentMarginIsStillATile() {
val pixels = icon { x, y ->
when {
!inRoundedSquare(x, y, 2, 3) -> clear
inBar(x, y) -> white
else -> green
}
}
assertEquals(NotificationIconArt.Kind.GLYPH, NotificationIconArt.convert(pixels, outputSize).kind)
}
@Test fun gradientTileIsReadAsAPicture() {
val pixels = icon { x, _ -> val v = x * 10; argb(255, v, v, v) }
val art = NotificationIconArt.convert(pixels, outputSize)
assertEquals(NotificationIconArt.Kind.PICTURE, art.kind)
assertTrue(gray(art, 1, 6) < gray(art, 6, 6))
assertTrue(gray(art, 6, 6) < gray(art, 10, 6))
assertEquals(255, gray(art, 11, 6))
}
@Test fun templateConversionIgnoresColors() {
val pixels = icon { x, y -> if (inBar(x, y)) white else green }
val art = NotificationIconArt.template(pixels, outputSize)
assertEquals(NotificationIconArt.Kind.TEMPLATE, art.kind)
assertTrue(art.gray.all { (it.toInt() and 0xff) == 255 })
}
@Test fun downscaleAveragesCoverageThenAppliesTheToneCurve() {
// Alternate opaque and clear columns: every output pixel is half covered.
val pixels = icon { x, _ -> if (x % 2 == 0) white else clear }
val art = NotificationIconArt.convert(pixels, outputSize)
assertEquals(NotificationIconArt.Kind.TEMPLATE, art.kind)
val expected = (255 * 0.5.pow(NotificationIconArt.GAMMA)).roundToInt()
assertTrue(art.gray.all { (it.toInt() and 0xff) == expected })
}
@Test fun rejectsTheWrongPixelCount() {
assertFailsWith<IllegalArgumentException> { NotificationIconArt.convert(IntArray(10), outputSize) }
}
}
@@ -22,6 +22,21 @@ class IosNativeApiTest {
assertContentEquals(auth.byteArray(), result.payload.byteArray())
}
@Test
fun bgraPacketsConvertWithTheSharedGrayTone() {
// 2x1: opaque orange, half-transparent white (B, G, R, A in memory).
val bgra = byteArrayOf(2, 0, 1, 0, 0x20, 0x80.toByte(), 0xff.toByte(), 0xff.toByte(),
0xff.toByte(), 0xff.toByte(), 0xff.toByte(), 0x80.toByte())
val argb = intArrayOf(0xffff8020.toInt(), 0x80ffffff.toInt())
val protocol = IosProtocol()
for (dither in listOf(false, true)) {
assertContentEquals(GrayPacket.fromArgb(argb, 2, 1, 2.2f, dither),
protocol.grayPacket(bgra.data(), 2.2f, dither).byteArray())
}
assertEquals(0UL, protocol.grayPacket(bgra.copyOf(10).data(), 1f, false).length)
assertEquals(0UL, protocol.grayPacket(byteArrayOf(0, 0, 1, 0).data(), 1f, false).length)
}
@Test
fun compassFacadePreservesTheEndOfTheCrcFreePayload() {
val protocol = IosProtocol()