Files
Crystal/tests/viewport.test.ts

57 lines
1.9 KiB
TypeScript

import { describe, expect, it } from "vitest";
import {
BASE_VIEWPORT,
MAX_VIEWPORT,
renderViewportForBox,
} from "../src/game/rendering/viewport";
describe("adaptive native viewport", () => {
it("keeps detached fixture canvases at the original resolution", () => {
expect(renderViewportForBox(0, 0)).toEqual({
...BASE_VIEWPORT,
pixelScale: 3,
});
expect(renderViewportForBox(Number.NaN, 500)).toEqual({
...BASE_VIEWPORT,
pixelScale: 3,
});
});
it("uses one integer scale on both axes and reveals additional world", () => {
const phone = renderViewportForBox(390, 720);
expect(phone).toEqual({ width: 195, height: 360, pixelScale: 2 });
const desktop = renderViewportForBox(960, 648);
expect(desktop).toEqual({ width: 320, height: 216, pixelScale: 3 });
expect(desktop.width).toBeGreaterThan(BASE_VIEWPORT.width);
expect(desktop.height).toBeGreaterThan(BASE_VIEWPORT.height);
expect(desktop.width * desktop.pixelScale).toBe(960);
expect(desktop.height * desktop.pixelScale).toBe(648);
});
it("rounds fractional boxes without materially changing their aspect", () => {
const css = { width: 713.5, height: 481.25 };
const viewport = renderViewportForBox(css.width, css.height);
expect(
Math.abs(viewport.width / viewport.height - css.width / css.height),
).toBeLessThan(0.005);
});
it.each([
[304, 400],
[844, 220],
[1920, 600],
[100_000, 100_000],
])(
"preserves the aspect of a %d x %d box while bounding buffers",
(width, height) => {
const viewport = renderViewportForBox(width, height);
expect(viewport.width).toBeLessThanOrEqual(MAX_VIEWPORT.width);
expect(viewport.height).toBeLessThanOrEqual(MAX_VIEWPORT.height);
expect(
Math.abs(viewport.width / viewport.height - width / height),
).toBeLessThan(0.03);
},
);
});