/** * 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, startLevel, WIDTHS } from "../static/resolution.js"; test("a retina window draws the world at a fixed width, not at its pixels", () => { const size = renderSize(3024, 1915, startLevel(1512)); expect(size.width).toBe(1280); // 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 are the width, and this is 2.4x fewer. expect(3024 / size.width).toBeGreaterThan(2.3); }); test("a narrower window starts narrower still", () => { expect(renderSize(1400, 887, startLevel(700)).width).toBe(960); expect(startLevel(1200)).toBe(0); expect(startLevel(1199)).toBe(2); }); 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, startLevel(360)).width).toBe(960); 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); } });