70160
Ce
cel-tui / files
cel-tui / packages / core / src / cell-buffer.test.ts
10.5 KB
1import { describe, expect, test } from "bun:test"; 2import { type Cell, CellBuffer, EMPTY_CELL } from "./cell-buffer.js"; 3 4describe("CellBuffer", () => { 5 describe("creation", () => { 6 test("creates buffer with correct dimensions", () => { 7 const buf = new CellBuffer(80, 24); 8 expect(buf.width).toBe(80); 9 expect(buf.height).toBe(24); 10 }); 11 12 test("all cells start empty", () => { 13 const buf = new CellBuffer(3, 2); 14 for (let y = 0; y < 2; y++) { 15 for (let x = 0; x < 3; x++) { 16 expect(buf.get(x, y)).toEqual(EMPTY_CELL); 17 } 18 } 19 }); 20 21 test("the shared empty-cell sentinel is immutable", () => { 22 expect(Object.isFrozen(EMPTY_CELL)).toBe(true); 23 }); 24 25 test("zero dimensions", () => { 26 const buf = new CellBuffer(0, 0); 27 expect(buf.width).toBe(0); 28 expect(buf.height).toBe(0); 29 }); 30 }); 31 32 describe("get/set", () => { 33 test("write and read a cell", () => { 34 const buf = new CellBuffer(10, 5); 35 const cell: Cell = { 36 char: "A", 37 fgColor: "color01", 38 bgColor: null, 39 bold: true, 40 italic: false, 41 underline: false, 42 }; 43 buf.set(3, 2, cell); 44 expect(buf.get(3, 2)).toEqual(cell); 45 }); 46 47 test("out-of-bounds get returns empty cell", () => { 48 const buf = new CellBuffer(5, 5); 49 expect(buf.get(-1, 0)).toEqual(EMPTY_CELL); 50 expect(buf.get(5, 0)).toEqual(EMPTY_CELL); 51 expect(buf.get(0, -1)).toEqual(EMPTY_CELL); 52 expect(buf.get(0, 5)).toEqual(EMPTY_CELL); 53 }); 54 55 test("out-of-bounds set is a no-op", () => { 56 const buf = new CellBuffer(5, 5); 57 const cell: Cell = { 58 char: "X", 59 fgColor: null, 60 bgColor: null, 61 bold: false, 62 italic: false, 63 underline: false, 64 }; 65 buf.set(-1, 0, cell); 66 buf.set(5, 0, cell); 67 buf.set(0, -1, cell); 68 buf.set(0, 5, cell); 69 // Should not throw, buffer unchanged 70 expect(buf.get(0, 0)).toEqual(EMPTY_CELL); 71 }); 72 73 test("overwrite existing cell", () => { 74 const buf = new CellBuffer(5, 5); 75 buf.set(0, 0, { 76 char: "A", 77 fgColor: null, 78 bgColor: null, 79 bold: false, 80 italic: false, 81 underline: false, 82 }); 83 buf.set(0, 0, { 84 char: "B", 85 fgColor: "color04", 86 bgColor: null, 87 bold: true, 88 italic: false, 89 underline: false, 90 }); 91 expect(buf.get(0, 0).char).toBe("B"); 92 expect(buf.get(0, 0).fgColor).toBe("color04"); 93 }); 94 95 test("overwriting a wide glyph lead clears its continuation cells", () => { 96 const buf = new CellBuffer(3, 1); 97 const plain: Cell = { 98 char: "😀", 99 fgColor: null, 100 bgColor: null, 101 bold: false, 102 italic: false, 103 underline: false, 104 }; 105 buf.set(0, 0, plain); 106 buf.set(1, 0, { ...plain, char: "" }); 107 108 buf.set(0, 0, { ...plain, char: "B" }); 109 110 expect(buf.get(0, 0).char).toBe("B"); 111 expect(buf.get(1, 0)).toEqual(EMPTY_CELL); 112 }); 113 114 test("overwriting a wide glyph continuation clears its lead cell", () => { 115 const buf = new CellBuffer(3, 1); 116 const plain: Cell = { 117 char: "😀", 118 fgColor: null, 119 bgColor: null, 120 bold: false, 121 italic: false, 122 underline: false, 123 }; 124 buf.set(0, 0, plain); 125 buf.set(1, 0, { ...plain, char: "" }); 126 127 buf.set(1, 0, { ...plain, char: "B" }); 128 129 expect(buf.get(0, 0)).toEqual(EMPTY_CELL); 130 expect(buf.get(1, 0).char).toBe("B"); 131 }); 132 133 test("restyling a wide glyph preserves and restyles its continuation", () => { 134 const buf = new CellBuffer(3, 1); 135 const wide: Cell = { 136 char: "界", 137 fgColor: null, 138 bgColor: null, 139 bold: false, 140 italic: false, 141 underline: false, 142 }; 143 buf.set(0, 0, wide); 144 buf.set(1, 0, { ...wide, char: "" }); 145 146 buf.set(0, 0, { 147 ...wide, 148 fgColor: "color00", 149 bgColor: "color07", 150 }); 151 152 expect(buf.get(0, 0).char).toBe("界"); 153 expect(buf.get(1, 0).char).toBe(""); 154 expect(buf.get(1, 0).fgColor).toBe("color00"); 155 expect(buf.get(1, 0).bgColor).toBe("color07"); 156 }); 157 158 test("repainting the same wide glyph preserves its lead", () => { 159 const buf = new CellBuffer(3, 1); 160 const wide: Cell = { 161 char: "界", 162 fgColor: null, 163 bgColor: null, 164 bold: false, 165 italic: false, 166 underline: false, 167 }; 168 const continuation = { ...wide, char: "" }; 169 buf.set(0, 0, wide); 170 buf.set(1, 0, continuation); 171 172 buf.set(0, 0, wide); 173 buf.set(1, 0, continuation); 174 175 expect(buf.get(0, 0).char).toBe("界"); 176 expect(buf.get(1, 0).char).toBe(""); 177 }); 178 }); 179 180 describe("clear", () => { 181 test("resets all cells to empty", () => { 182 const buf = new CellBuffer(3, 3); 183 buf.set(1, 1, { 184 char: "X", 185 fgColor: "color01", 186 bgColor: null, 187 bold: true, 188 italic: false, 189 underline: false, 190 }); 191 buf.clear(); 192 expect(buf.get(1, 1)).toEqual(EMPTY_CELL); 193 }); 194 }); 195 196 describe("resize", () => { 197 test("grows buffer, new cells are empty", () => { 198 const buf = new CellBuffer(3, 3); 199 buf.set(1, 1, { 200 char: "A", 201 fgColor: null, 202 bgColor: null, 203 bold: false, 204 italic: false, 205 underline: false, 206 }); 207 buf.resize(5, 5); 208 expect(buf.width).toBe(5); 209 expect(buf.height).toBe(5); 210 expect(buf.get(1, 1).char).toBe("A"); 211 expect(buf.get(4, 4)).toEqual(EMPTY_CELL); 212 }); 213 214 test("shrinks buffer, clips content", () => { 215 const buf = new CellBuffer(5, 5); 216 buf.set(4, 4, { 217 char: "X", 218 fgColor: null, 219 bgColor: null, 220 bold: false, 221 italic: false, 222 underline: false, 223 }); 224 buf.resize(3, 3); 225 expect(buf.width).toBe(3); 226 expect(buf.height).toBe(3); 227 expect(buf.get(4, 4)).toEqual(EMPTY_CELL); // out of bounds now 228 }); 229 230 test("shrinking through a wide glyph removes its clipped lead cell", () => { 231 const buf = new CellBuffer(2, 1); 232 const wide: Cell = { 233 char: "界", 234 fgColor: null, 235 bgColor: null, 236 bold: false, 237 italic: false, 238 underline: false, 239 }; 240 buf.set(0, 0, wide); 241 buf.set(1, 0, { ...wide, char: "" }); 242 243 buf.resize(1, 1); 244 245 expect(buf.get(0, 0)).toEqual(EMPTY_CELL); 246 }); 247 }); 248 249 describe("fill", () => { 250 test("fills a rectangular region", () => { 251 const buf = new CellBuffer(5, 5); 252 const cell: Cell = { 253 char: "#", 254 fgColor: "color02", 255 bgColor: null, 256 bold: false, 257 italic: false, 258 underline: false, 259 }; 260 buf.fill(1, 1, 3, 2, cell); 261 expect(buf.get(1, 1)).toEqual(cell); 262 expect(buf.get(2, 1)).toEqual(cell); 263 expect(buf.get(3, 1)).toEqual(cell); 264 expect(buf.get(1, 2)).toEqual(cell); 265 expect(buf.get(3, 2)).toEqual(cell); 266 // Outside the fill region 267 expect(buf.get(0, 0)).toEqual(EMPTY_CELL); 268 expect(buf.get(4, 3)).toEqual(EMPTY_CELL); 269 }); 270 271 test("clips to buffer bounds", () => { 272 const buf = new CellBuffer(3, 3); 273 const cell: Cell = { 274 char: "X", 275 fgColor: null, 276 bgColor: null, 277 bold: false, 278 italic: false, 279 underline: false, 280 }; 281 // Fill extends beyond bounds — should not throw 282 buf.fill(1, 1, 5, 5, cell); 283 expect(buf.get(1, 1)).toEqual(cell); 284 expect(buf.get(2, 2)).toEqual(cell); 285 }); 286 287 test("filling over part of a wide glyph clears the whole glyph", () => { 288 const buf = new CellBuffer(3, 1); 289 const wide: Cell = { 290 char: "界", 291 fgColor: null, 292 bgColor: null, 293 bold: false, 294 italic: false, 295 underline: false, 296 }; 297 buf.set(0, 0, wide); 298 buf.set(1, 0, { ...wide, char: "" }); 299 300 buf.fill(1, 0, 1, 1, { ...wide, char: " ", bgColor: "color04" }); 301 302 expect(buf.get(0, 0)).toEqual(EMPTY_CELL); 303 expect(buf.get(1, 0).bgColor).toBe("color04"); 304 }); 305 }); 306 307 describe("diff", () => { 308 test("identical buffers produce no changes", () => { 309 const a = new CellBuffer(3, 3); 310 const b = new CellBuffer(3, 3); 311 expect(a.diff(b)).toEqual([]); 312 }); 313 314 test("detects changed cells", () => { 315 const a = new CellBuffer(3, 3); 316 const b = new CellBuffer(3, 3); 317 b.set(1, 1, { 318 char: "X", 319 fgColor: null, 320 bgColor: null, 321 bold: false, 322 italic: false, 323 underline: false, 324 }); 325 const changes = a.diff(b); 326 expect(changes).toEqual([{ x: 1, y: 1 }]); 327 }); 328 329 test("detects multiple changes", () => { 330 const a = new CellBuffer(3, 3); 331 const b = new CellBuffer(3, 3); 332 b.set(0, 0, { 333 char: "A", 334 fgColor: null, 335 bgColor: null, 336 bold: false, 337 italic: false, 338 underline: false, 339 }); 340 b.set(2, 2, { 341 char: "B", 342 fgColor: null, 343 bgColor: null, 344 bold: false, 345 italic: false, 346 underline: false, 347 }); 348 const changes = a.diff(b); 349 expect(changes).toHaveLength(2); 350 expect(changes).toContainEqual({ x: 0, y: 0 }); 351 expect(changes).toContainEqual({ x: 2, y: 2 }); 352 }); 353 354 test("detects style-only changes", () => { 355 const a = new CellBuffer(3, 3); 356 const b = new CellBuffer(3, 3); 357 a.set(0, 0, { 358 char: "X", 359 fgColor: null, 360 bgColor: null, 361 bold: false, 362 italic: false, 363 underline: false, 364 }); 365 b.set(0, 0, { 366 char: "X", 367 fgColor: "color01", 368 bgColor: null, 369 bold: false, 370 italic: false, 371 underline: false, 372 }); 373 expect(a.diff(b)).toEqual([{ x: 0, y: 0 }]); 374 }); 375 }); 376 377 describe("isEmpty", () => { 378 test("empty cell is transparent", () => { 379 const buf = new CellBuffer(3, 3); 380 expect(buf.isEmpty(0, 0)).toBe(true); 381 }); 382 383 test("non-empty cell is not transparent", () => { 384 const buf = new CellBuffer(3, 3); 385 buf.set(1, 1, { 386 char: "X", 387 fgColor: null, 388 bgColor: null, 389 bold: false, 390 italic: false, 391 underline: false, 392 }); 393 expect(buf.isEmpty(1, 1)).toBe(false); 394 }); 395 396 test("space with background is not transparent", () => { 397 const buf = new CellBuffer(3, 3); 398 buf.set(0, 0, { 399 char: " ", 400 fgColor: null, 401 bgColor: "color04", 402 bold: false, 403 italic: false, 404 underline: false, 405 }); 406 expect(buf.isEmpty(0, 0)).toBe(false); 407 }); 408 }); 409});