1
0
Fork 0
Code Issues Pull requests Projects Releases Packages Wiki Activity Actions Pages
Worldshaper/src/editorCore.test.ts

107 lines
2.8 KiB
TypeScript
Raw Normal View History

import {
getSpriteRows,
normalizeImageRecordForSave,
normalizeImagesPayloadForSave,
normalizeTileRecordForSave,
resolveUnifiedColorSymbol,
setUnifiedColorEntries,
} from "./editorCore";
import type { JsonObject, JsonValue } from "./editorCore";
describe("editorCore", () => {
it("normalizes tile records with padded rows and compatibility symbols", () => {
const result = normalizeTileRecordForSave({
id: "tile_grass",
rows: ["A", "BC", "DROP"],
width: 2,
height: 2,
});
expect(result).toEqual(expect.objectContaining({
id: "tile_grass",
symbol: "t",
rows: ["A.", "BC"],
width: 2,
height: 2,
}));
});
it("normalizes image records into frame-based storage while preserving tile compatibility", () => {
const result = normalizeImageRecordForSave({
id: "tile_tree",
roles: ["tile", "sprite", "other", "tile"],
rows: ["A", "BC"],
width: 2,
height: 2,
speed: -10,
playback: "invalid",
tileSymbol: "$",
});
expect(result.rows).toBeUndefined();
expect(result.symbol).toBeUndefined();
expect(result.graphicRole).toBeUndefined();
expect(result.roles).toEqual(["tile", "sprite"]);
expect(result.defaultFrame).toBe("frame_0");
expect(result.speed).toBe(0);
expect(result.playback).toBe("normal");
expect(result.tileSymbol).toBe("$");
expect(result.frames).toEqual([
expect.objectContaining({
id: "frame_0",
rows: ["A.", "BC"],
}),
]);
});
it("resolves sprite rows from the default enabled frame", () => {
const record: JsonObject = {
defaultFrame: "walk_1",
frames: [
{ id: "idle", rows: ["II"], enabled: true },
{ id: "walk_1", rows: ["WW"], enabled: true },
{ id: "disabled", rows: ["DD"], enabled: false },
],
};
expect(getSpriteRows(record)).toEqual(["WW"]);
});
it("normalizes image payload arrays without changing non-record entries", () => {
const payload: JsonValue = {
schemaVersion: 1,
images: [
{
id: "sprite_hero",
roles: ["sprite"],
rows: ["X"],
},
"leave-me-alone",
],
};
expect(normalizeImagesPayloadForSave(payload)).toEqual({
schemaVersion: 1,
images: [
expect.objectContaining({
id: "sprite_hero",
roles: ["sprite"],
defaultFrame: "frame_0",
}),
"leave-me-alone",
],
});
});
it("updates the unified color lookup from catalog entries", () => {
setUnifiedColorEntries([
{ key: "A", color: "#123456" },
{ key: "!", color: "#FFFFFF" },
]);
expect(resolveUnifiedColorSymbol("A")).toBe("#123456");
expect(resolveUnifiedColorSymbol(".")).toBe("#00000000");
expect(resolveUnifiedColorSymbol("Z", "#ABCDEF")).toBe("#ABCDEF");
});
});