refactor: extract render entrypoint & add cropRender function

This commit is contained in:
Shibo Lyu 2025-12-16 16:41:30 +08:00
parent d97546f47c
commit c703b1870a
16 changed files with 630 additions and 181 deletions

View file

@ -1,14 +1,9 @@
import { describe, it, expect } from "vitest";
import {
createBoard,
getSectionOnBoard,
renderFullBoard,
} from "../src/logic/board.ts";
import type { BoardData } from "../src/types/board.ts";
import { checkFullBoard } from "./checkFullBoard.ts";
import { locateSection } from "../src/logic/board.ts";
import { applyChangeOnBoard } from "../src/logic/board.ts";
import { createBoard, getSectionOnBoard } from "../src/logic/board";
import type { BoardData } from "../src/types/board";
import { locateSection } from "../src/logic/board";
import { applyChangeOnBoard } from "../src/logic/board";
describe("board", () => {
let board: BoardData | undefined;
@ -50,23 +45,23 @@ describe("board", () => {
applyChangeOnBoard({ x: 4, y: 3, ch: "D" }, board!);
applyChangeOnBoard({ x: 5, y: 3, ch: "E" }, board!);
expect(board!.sections[0][0].ch[0][0]).toBe("A");
expect(board!.sections[0][1].ch[0][0]).toBe("B");
expect(board!.sections[1][0].ch[0][0]).toBe("C");
expect(board!.sections[1][1].ch[0]).toEqual(["D", "E", " ", " "]);
expect(board!.sections[0]![0]!.ch[0]![0]).toBe("A");
expect(board!.sections[0]![1]!.ch[0]![0]).toBe("B");
expect(board!.sections[1]![0]!.ch[0]![0]).toBe("C");
expect(board!.sections[1]![1]!.ch[0]).toEqual(["D", "E", " ", " "]);
applyChangeOnBoard({ x: 0, y: 1, ch: "你" }, board!);
applyChangeOnBoard({ x: 4, y: 2, ch: "好" }, board!);
applyChangeOnBoard({ x: 0, y: 4, ch: "嗎" }, board!);
applyChangeOnBoard({ x: 4, y: 4, ch: "嘛" }, board!);
expect(board!.sections[0][0].ch[1][0]).toBe("你");
expect(board!.sections[0][1].ch[2][0]).toBe("好");
expect(board!.sections[1][0].ch[1][0]).toBe("嗎");
expect(board!.sections[1][1].ch[1]).toEqual(["嘛", " ", " ", " "]);
expect(board!.sections[0]![0]!.ch[1]![0]).toBe("你");
expect(board!.sections[0]![1]!.ch[2]![0]).toBe("好");
expect(board!.sections[1]![0]!.ch[1]![0]).toBe("嗎");
expect(board!.sections[1]![1]!.ch[1]).toEqual(["嘛", " ", " ", " "]);
applyChangeOnBoard({ x: 5, y: 4, ch: "啊" }, board!);
expect(board!.sections[1][1].ch[1]).toEqual(["啊", " ", " ", " "]);
expect(board!.sections[1]![1]!.ch[1]).toEqual(["啊", " ", " ", " "]);
});
it("getSectionOnBoard: existing section", () => {
@ -76,8 +71,8 @@ describe("board", () => {
readOnly: true,
});
expect(section.ch[0]).toEqual(["D", "E", " ", " "]);
expect(section.color[0][0]).toBe("F");
expect(section.bgColor[0][0]).toBe("0");
expect(section.color[0]![0]).toBe("F");
expect(section.bgColor[0]![0]).toBe("0");
expect(section.width[0]).toEqual([1, 1, 1, 1]);
});
@ -87,10 +82,10 @@ describe("board", () => {
const section = getSectionOnBoard({ sx: 1, sy: 2 }, board!, {
readOnly: true,
});
expect(section.ch[0][0]).toBe(" ");
expect(section.color[0][0]).toBe("F");
expect(section.bgColor[0][0]).toBe("0");
expect(section.width[0][0]).toBe(1);
expect(section.ch[0]![0]).toBe(" ");
expect(section.color[0]![0]).toBe("F");
expect(section.bgColor[0]![0]).toBe("0");
expect(section.width[0]![0]).toBe(1);
});
it("getSectionOnBoard: non-existing section", () => {
@ -99,23 +94,16 @@ describe("board", () => {
const section = getSectionOnBoard({ sx: 2, sy: 1 }, board!, {
readOnly: true,
});
expect(section.ch[0][0]).toBe(" ");
expect(section.color[0][0]).toBe("F");
expect(section.bgColor[0][0]).toBe("0");
expect(section.width[0][0]).toBe(1);
});
it("renderFullBoard", () => {
expect(board).toBeDefined();
const rendered = renderFullBoard(board!);
checkFullBoard(rendered);
expect(section.ch[0]![0]).toBe(" ");
expect(section.color[0]![0]).toBe("F");
expect(section.bgColor[0]![0]).toBe("0");
expect(section.width[0]![0]).toBe(1);
});
it("on-demand creation: only changed sections are saved", () => {
expect(board).toBeDefined();
expect(board!.sections[2]).toBeUndefined();
expect(board!.sections[0][2]).toBeUndefined();
expect(board!.sections[0]![2]).toBeUndefined();
});
});