|
| 1 | +package expo.modules.libghostty |
| 2 | + |
| 3 | +import java.nio.ByteBuffer |
| 4 | +import java.nio.ByteOrder |
| 5 | +import org.junit.Assert.assertEquals |
| 6 | +import org.junit.Assert.assertFalse |
| 7 | +import org.junit.Assert.assertTrue |
| 8 | +import org.junit.Test |
| 9 | + |
| 10 | +class SnapshotFormatTest { |
| 11 | + // Mirrors the writer in ghostty_jni.cpp: 13×i32, little-endian. |
| 12 | + private fun headerBuffer(vararg fields: Int): ByteBuffer { |
| 13 | + val buf = ByteBuffer.allocate(HEADER_BYTES + 8).order(ByteOrder.LITTLE_ENDIAN) |
| 14 | + for (field in fields) buf.putInt(field) |
| 15 | + buf.position(0) |
| 16 | + return buf |
| 17 | + } |
| 18 | + |
| 19 | + @Test |
| 20 | + fun `header size matches the declared v2 layout`() { |
| 21 | + assertEquals(13 * Int.SIZE_BYTES, HEADER_BYTES) |
| 22 | + } |
| 23 | + |
| 24 | + @Test |
| 25 | + fun `read decodes every header field and consumes exactly the header`() { |
| 26 | + val buf = headerBuffer( |
| 27 | + 2, // version |
| 28 | + DIRTY_FULL, |
| 29 | + 80, // cols |
| 30 | + 24, // rows |
| 31 | + 0xFF1E1E2E.toInt(), // background |
| 32 | + 0xFFCDD6F4.toInt(), // foreground |
| 33 | + 12, // cursorX |
| 34 | + 3, // cursorY |
| 35 | + CURSOR_STYLE_BLOCK, |
| 36 | + 1, // cursorVisible |
| 37 | + 24, // rowCount |
| 38 | + 1, // cursorBlinks |
| 39 | + 0xFFF5C2E7.toInt(), // cursorColor |
| 40 | + ) |
| 41 | + val header = SnapshotHeader.read(buf) |
| 42 | + assertEquals(2, header.version) |
| 43 | + assertEquals(DIRTY_FULL, header.dirtyKind) |
| 44 | + assertEquals(80, header.cols) |
| 45 | + assertEquals(24, header.rows) |
| 46 | + assertEquals(0xFF1E1E2E.toInt(), header.background) |
| 47 | + assertEquals(0xFFCDD6F4.toInt(), header.foreground) |
| 48 | + assertEquals(12, header.cursorX) |
| 49 | + assertEquals(3, header.cursorY) |
| 50 | + assertEquals(CURSOR_STYLE_BLOCK, header.cursorStyle) |
| 51 | + assertTrue(header.cursorVisible) |
| 52 | + assertEquals(24, header.rowCount) |
| 53 | + assertTrue(header.cursorBlinks) |
| 54 | + assertEquals(0xFFF5C2E7.toInt(), header.cursorColor) |
| 55 | + assertEquals(HEADER_BYTES, buf.position()) |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + fun `zero int fields decode as false and unconfigured cursor color`() { |
| 60 | + val buf = headerBuffer(2, 0, 80, 24, 0, 0, 0, 0, CURSOR_STYLE_BAR, 0, 0, 0, 0) |
| 61 | + val header = SnapshotHeader.read(buf) |
| 62 | + assertFalse(header.cursorVisible) |
| 63 | + assertFalse(header.cursorBlinks) |
| 64 | + assertEquals(0, header.cursorColor) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +class ResolveCellColorsTest { |
| 69 | + private val fg = 0xFFAA0000.toInt() |
| 70 | + private val bg = 0xFF00AA00.toInt() |
| 71 | + private val defaultFg = 0xFFEEEEEE.toInt() |
| 72 | + private val defaultBg = 0xFF111111.toInt() |
| 73 | + private val themeFg = 0xFF101010.toInt() |
| 74 | + private val themeBg = 0xFFF0F0F0.toInt() |
| 75 | + |
| 76 | + private fun resolve( |
| 77 | + flags: Int = 0, |
| 78 | + selected: Boolean = false, |
| 79 | + selectionFg: Int? = null, |
| 80 | + selectionBg: Int? = null, |
| 81 | + ) = resolveCellColors(fg, bg, flags, selected, defaultFg, defaultBg, selectionFg, selectionBg) |
| 82 | + |
| 83 | + private fun assertColors(expectedFg: Int, expectedBg: Int, packed: Long) { |
| 84 | + assertEquals(expectedFg, packedFg(packed)) |
| 85 | + assertEquals(expectedBg, packedBg(packed)) |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + fun `explicit colors pass through`() { |
| 90 | + assertColors(fg, bg, resolve()) |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + fun `default flags substitute the terminal defaults`() { |
| 95 | + assertColors(defaultFg, bg, resolve(flags = FLAG_FG_DEFAULT)) |
| 96 | + assertColors(fg, defaultBg, resolve(flags = FLAG_BG_NONE)) |
| 97 | + assertColors(defaultFg, defaultBg, resolve(flags = FLAG_FG_DEFAULT or FLAG_BG_NONE)) |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + fun `inverse swaps the resolved colors`() { |
| 102 | + assertColors(bg, fg, resolve(flags = FLAG_INVERSE)) |
| 103 | + // Defaults resolve first, then swap. |
| 104 | + assertColors(defaultBg, defaultFg, resolve(flags = FLAG_FG_DEFAULT or FLAG_BG_NONE or FLAG_INVERSE)) |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + fun `selection without a theme swaps like inverse`() { |
| 109 | + assertColors(bg, fg, resolve(selected = true)) |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + fun `selection on an inverse cell swaps back to the original colors`() { |
| 114 | + assertColors(fg, bg, resolve(flags = FLAG_INVERSE, selected = true)) |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + fun `themed selection background keeps the cell foreground`() { |
| 119 | + assertColors(fg, themeBg, resolve(selected = true, selectionBg = themeBg)) |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + fun `themed selection foreground applies only with a themed background`() { |
| 124 | + assertColors(themeFg, themeBg, resolve(selected = true, selectionFg = themeFg, selectionBg = themeBg)) |
| 125 | + // Foreground alone falls back to the classic swap. |
| 126 | + assertColors(bg, fg, resolve(selected = true, selectionFg = themeFg)) |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + fun `themed selection overrides an inverse cell`() { |
| 131 | + assertColors(themeFg, themeBg, resolve(flags = FLAG_INVERSE, selected = true, selectionFg = themeFg, selectionBg = themeBg)) |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + fun `unselected cells ignore the selection theme`() { |
| 136 | + assertColors(fg, bg, resolve(selectionFg = themeFg, selectionBg = themeBg)) |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + fun `packing round-trips negative ARGB ints`() { |
| 141 | + val packed = resolveCellColors( |
| 142 | + 0x80000000.toInt(), 0xFFFFFFFF.toInt(), 0, false, |
| 143 | + defaultFg, defaultBg, null, null |
| 144 | + ) |
| 145 | + assertColors(0x80000000.toInt(), 0xFFFFFFFF.toInt(), packed) |
| 146 | + } |
| 147 | +} |
0 commit comments