textui/ui.test.ts

58 lines
1.6 KiB
TypeScript

import { BufferLocation, Char, Color, SPACE } from "./base.ts";
import { describe, expect, it } from "./testing.ts";
import { Display } from "./display.ts";
import { TextUI } from "./ui.ts";
describe(TextUI, () => {
it("maps a full RGB color palette directly", async () => {
const palette = [
{ r: 0.0, g: 0.0, b: 0.0 },
{ r: 1.0, g: 0.0, b: 0.0 },
];
const display = new PaletteTestDisplay(palette);
const ui = new TextUI(display, { palette });
await ui.init();
ui.drawing.color(1, 0).text("x", { x: 0, y: 0 });
await ui.flush();
expect(display.last).toEqual({ ch: "x", bg: 0, fg: 1 });
});
it("finds the best match for a color in a limited palette", async () => {
const app_palette = [
{ r: 0.0, g: 0.0, b: 1.0 },
{ r: 0.0, g: 0.0, b: 0.0 },
];
const display_palette = [
{ r: 1.0, g: 1.0, b: 1.0 },
{ r: 0.1, g: 0.2, b: 0.8 },
{ r: 0.1, g: 0.2, b: 0.1 },
];
const display = new PaletteTestDisplay(display_palette);
const ui = new TextUI(display, { palette: app_palette });
await ui.init();
ui.drawing.color(0, 1).text("x", { x: 0, y: 0 });
await ui.flush();
expect(display.last).toEqual({ ch: "x", bg: 2, fg: 1 });
});
});
class PaletteTestDisplay extends Display {
last = SPACE;
constructor(public palette: Color[]) {
super();
}
async getSize() {
return { w: 1, h: 1 };
}
async setupPalette(colors: readonly Color[]): Promise<readonly Color[]> {
return this.palette;
}
async setChar(at: BufferLocation, char: Char): Promise<void> {
this.last = char;
}
}