@@ -0,0 +1,284 @@
1+import { describe, expect, test } from "bun:test";
2+import { CellBuffer, type Cell, EMPTY_CELL } from "./cell-buffer.js";
3+
4+describe("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("zero dimensions", () => {
22+ const buf = new CellBuffer(0, 0);
23+ expect(buf.width).toBe(0);
24+ expect(buf.height).toBe(0);
25+ });
26+ });
27+
28+ describe("get/set", () => {
29+ test("write and read a cell", () => {
30+ const buf = new CellBuffer(10, 5);
31+ const cell: Cell = {
32+ char: "A",
33+ fgColor: "red",
34+ bgColor: null,
35+ bold: true,
36+ italic: false,
37+ underline: false,
38+ };
39+ buf.set(3, 2, cell);
40+ expect(buf.get(3, 2)).toEqual(cell);
41+ });
42+
43+ test("out-of-bounds get returns empty cell", () => {
44+ const buf = new CellBuffer(5, 5);
45+ expect(buf.get(-1, 0)).toEqual(EMPTY_CELL);
46+ expect(buf.get(5, 0)).toEqual(EMPTY_CELL);
47+ expect(buf.get(0, -1)).toEqual(EMPTY_CELL);
48+ expect(buf.get(0, 5)).toEqual(EMPTY_CELL);
49+ });
50+
51+ test("out-of-bounds set is a no-op", () => {
52+ const buf = new CellBuffer(5, 5);
53+ const cell: Cell = {
54+ char: "X",
55+ fgColor: null,
56+ bgColor: null,
57+ bold: false,
58+ italic: false,
59+ underline: false,
60+ };
61+ buf.set(-1, 0, cell);
62+ buf.set(5, 0, cell);
63+ buf.set(0, -1, cell);
64+ buf.set(0, 5, cell);
65+ // Should not throw, buffer unchanged
66+ expect(buf.get(0, 0)).toEqual(EMPTY_CELL);
67+ });
68+
69+ test("overwrite existing cell", () => {
70+ const buf = new CellBuffer(5, 5);
71+ buf.set(0, 0, {
72+ char: "A",
73+ fgColor: null,
74+ bgColor: null,
75+ bold: false,
76+ italic: false,
77+ underline: false,
78+ });
79+ buf.set(0, 0, {
80+ char: "B",
81+ fgColor: "blue",
82+ bgColor: null,
83+ bold: true,
84+ italic: false,
85+ underline: false,
86+ });
87+ expect(buf.get(0, 0).char).toBe("B");
88+ expect(buf.get(0, 0).fgColor).toBe("blue");
89+ });
90+ });
91+
92+ describe("clear", () => {
93+ test("resets all cells to empty", () => {
94+ const buf = new CellBuffer(3, 3);
95+ buf.set(1, 1, {
96+ char: "X",
97+ fgColor: "red",
98+ bgColor: null,
99+ bold: true,
100+ italic: false,
101+ underline: false,
102+ });
103+ buf.clear();
104+ expect(buf.get(1, 1)).toEqual(EMPTY_CELL);
105+ });
106+ });
107+
108+ describe("resize", () => {
109+ test("grows buffer, new cells are empty", () => {
110+ const buf = new CellBuffer(3, 3);
111+ buf.set(1, 1, {
112+ char: "A",
113+ fgColor: null,
114+ bgColor: null,
115+ bold: false,
116+ italic: false,
117+ underline: false,
118+ });
119+ buf.resize(5, 5);
120+ expect(buf.width).toBe(5);
121+ expect(buf.height).toBe(5);
122+ expect(buf.get(1, 1).char).toBe("A");
123+ expect(buf.get(4, 4)).toEqual(EMPTY_CELL);
124+ });
125+
126+ test("shrinks buffer, clips content", () => {
127+ const buf = new CellBuffer(5, 5);
128+ buf.set(4, 4, {
129+ char: "X",
130+ fgColor: null,
131+ bgColor: null,
132+ bold: false,
133+ italic: false,
134+ underline: false,
135+ });
136+ buf.resize(3, 3);
137+ expect(buf.width).toBe(3);
138+ expect(buf.height).toBe(3);
139+ expect(buf.get(4, 4)).toEqual(EMPTY_CELL); // out of bounds now
140+ });
141+ });
142+
143+ describe("fill", () => {
144+ test("fills a rectangular region", () => {
145+ const buf = new CellBuffer(5, 5);
146+ const cell: Cell = {
147+ char: "#",
148+ fgColor: "green",
149+ bgColor: null,
150+ bold: false,
151+ italic: false,
152+ underline: false,
153+ };
154+ buf.fill(1, 1, 3, 2, cell);
155+ expect(buf.get(1, 1)).toEqual(cell);
156+ expect(buf.get(2, 1)).toEqual(cell);
157+ expect(buf.get(3, 1)).toEqual(cell);
158+ expect(buf.get(1, 2)).toEqual(cell);
159+ expect(buf.get(3, 2)).toEqual(cell);
160+ // Outside the fill region
161+ expect(buf.get(0, 0)).toEqual(EMPTY_CELL);
162+ expect(buf.get(4, 3)).toEqual(EMPTY_CELL);
163+ });
164+
165+ test("clips to buffer bounds", () => {
166+ const buf = new CellBuffer(3, 3);
167+ const cell: Cell = {
168+ char: "X",
169+ fgColor: null,
170+ bgColor: null,
171+ bold: false,
172+ italic: false,
173+ underline: false,
174+ };
175+ // Fill extends beyond bounds — should not throw
176+ buf.fill(1, 1, 5, 5, cell);
177+ expect(buf.get(1, 1)).toEqual(cell);
178+ expect(buf.get(2, 2)).toEqual(cell);
179+ });
180+ });
181+
182+ describe("diff", () => {
183+ test("identical buffers produce no changes", () => {
184+ const a = new CellBuffer(3, 3);
185+ const b = new CellBuffer(3, 3);
186+ expect(a.diff(b)).toEqual([]);
187+ });
188+
189+ test("detects changed cells", () => {
190+ const a = new CellBuffer(3, 3);
191+ const b = new CellBuffer(3, 3);
192+ b.set(1, 1, {
193+ char: "X",
194+ fgColor: null,
195+ bgColor: null,
196+ bold: false,
197+ italic: false,
198+ underline: false,
199+ });
200+ const changes = a.diff(b);
201+ expect(changes).toEqual([{ x: 1, y: 1 }]);
202+ });
203+
204+ test("detects multiple changes", () => {
205+ const a = new CellBuffer(3, 3);
206+ const b = new CellBuffer(3, 3);
207+ b.set(0, 0, {
208+ char: "A",
209+ fgColor: null,
210+ bgColor: null,
211+ bold: false,
212+ italic: false,
213+ underline: false,
214+ });
215+ b.set(2, 2, {
216+ char: "B",
217+ fgColor: null,
218+ bgColor: null,
219+ bold: false,
220+ italic: false,
221+ underline: false,
222+ });
223+ const changes = a.diff(b);
224+ expect(changes).toHaveLength(2);
225+ expect(changes).toContainEqual({ x: 0, y: 0 });
226+ expect(changes).toContainEqual({ x: 2, y: 2 });
227+ });
228+
229+ test("detects style-only changes", () => {
230+ const a = new CellBuffer(3, 3);
231+ const b = new CellBuffer(3, 3);
232+ a.set(0, 0, {
233+ char: "X",
234+ fgColor: null,
235+ bgColor: null,
236+ bold: false,
237+ italic: false,
238+ underline: false,
239+ });
240+ b.set(0, 0, {
241+ char: "X",
242+ fgColor: "red",
243+ bgColor: null,
244+ bold: false,
245+ italic: false,
246+ underline: false,
247+ });
248+ expect(a.diff(b)).toEqual([{ x: 0, y: 0 }]);
249+ });
250+ });
251+
252+ describe("isEmpty", () => {
253+ test("empty cell is transparent", () => {
254+ const buf = new CellBuffer(3, 3);
255+ expect(buf.isEmpty(0, 0)).toBe(true);
256+ });
257+
258+ test("non-empty cell is not transparent", () => {
259+ const buf = new CellBuffer(3, 3);
260+ buf.set(1, 1, {
261+ char: "X",
262+ fgColor: null,
263+ bgColor: null,
264+ bold: false,
265+ italic: false,
266+ underline: false,
267+ });
268+ expect(buf.isEmpty(1, 1)).toBe(false);
269+ });
270+
271+ test("space with background is not transparent", () => {
272+ const buf = new CellBuffer(3, 3);
273+ buf.set(0, 0, {
274+ char: " ",
275+ fgColor: null,
276+ bgColor: "blue",
277+ bold: false,
278+ italic: false,
279+ underline: false,
280+ });
281+ expect(buf.isEmpty(0, 0)).toBe(false);
282+ });
283+ });
284+});