/** * The internal render size, as arithmetic. A retina window is the case that * started this: 1512 CSS pixels across is a 3024px canvas, and the renderer * was marching a ray for every other one of them. */ import { expect, test } from "bun:test"; import { renderSize, WIDTHS } from "../../static/engine/resolution.js"; test("a retina window draws the world at a fixed width, not at its pixels", () => { const size = renderSize(3024, 1915, 0); expect(size.width).toBe(960); // The picture keeps the canvas's shape, so the blit only ever scales. expect(size.height / size.width).toBeCloseTo(1915 / 3024, 3); // Which is what the frame costs: rays come out of the width, and this is // three times fewer of them. expect(3024 / size.width).toBeGreaterThan(3); }); test("every window starts at the same width: the canvas size is not the knob", () => { expect(renderSize(1400, 887, 0).width).toBe(960); expect(renderSize(2400, 1520, 0).width).toBe(960); expect(renderSize(5120, 3240, 0).width).toBe(960); }); test("the world is never drawn wider than the canvas has pixels for", () => { // A phone: 360 CSS pixels at dpr 3 is a 1080px canvas, and a level whose // rung is wider than that would be paying for columns nobody can see. expect(renderSize(1080, 684, 0).width).toBe(960); expect(renderSize(720, 456, 0).width).toBe(720); expect(renderSize(600, 380, 0).width).toBe(600); expect(renderSize(320, 203, 0).width).toBe(320); }); test("every rung is coarser than the one before, and the ladder ends", () => { const widths = WIDTHS.map((_, i) => renderSize(4000, 2500, i).width); expect(widths).toEqual(WIDTHS); for (let i = 1; i < widths.length; i++) expect(widths[i]!).toBeLessThan(widths[i - 1]!); // Past the end it holds at the coarsest rung rather than vanishing. expect(renderSize(4000, 2500, 99).width).toBe(WIDTHS[WIDTHS.length - 1]); expect(renderSize(4000, 2500, -3).width).toBe(WIDTHS[0]); }); test("a size is always drawable: no zero, no NaN, whatever it is handed", () => { for (const [w, h] of [[0, 0], [NaN, 100], [-10, -10], [10, 0]] as [number, number][]) { const size = renderSize(w, h, 0); expect(Number.isFinite(size.width)).toBe(true); expect(Number.isFinite(size.height)).toBe(true); expect(size.width).toBeGreaterThan(0); expect(size.height).toBeGreaterThan(0); } });