|
| 1 | +import { assert, describe, test } from "@rezi-ui/testkit"; |
| 2 | +import type { VNode } from "../../index.js"; |
| 3 | +import type { LayoutTree } from "../layout.js"; |
| 4 | +import { layout } from "../layout.js"; |
| 5 | +import type { Axis } from "../types.js"; |
| 6 | + |
| 7 | +function mustLayout(node: VNode, maxW: number, maxH: number, axis: Axis = "column"): LayoutTree { |
| 8 | + const res = layout(node, 0, 0, maxW, maxH, axis); |
| 9 | + assert.equal(res.ok, true, "layout should succeed"); |
| 10 | + if (!res.ok) { |
| 11 | + throw new Error("layout failed"); |
| 12 | + } |
| 13 | + return res.value; |
| 14 | +} |
| 15 | + |
| 16 | +describe("layout aspect-ratio (deterministic)", () => { |
| 17 | + test("width -> height derives via aspectRatio", () => { |
| 18 | + const node: VNode = { |
| 19 | + kind: "box", |
| 20 | + props: { border: "none", width: 8, aspectRatio: 2 }, |
| 21 | + children: Object.freeze([]), |
| 22 | + }; |
| 23 | + const out = mustLayout(node, 30, 20); |
| 24 | + assert.deepEqual(out.rect, { x: 0, y: 0, w: 8, h: 4 }); |
| 25 | + }); |
| 26 | + |
| 27 | + test("height -> width derives via aspectRatio", () => { |
| 28 | + const node: VNode = { |
| 29 | + kind: "box", |
| 30 | + props: { border: "none", height: 6, aspectRatio: 2 }, |
| 31 | + children: Object.freeze([]), |
| 32 | + }; |
| 33 | + const out = mustLayout(node, 30, 20); |
| 34 | + assert.deepEqual(out.rect, { x: 0, y: 0, w: 12, h: 6 }); |
| 35 | + }); |
| 36 | + |
| 37 | + test("both width and height take precedence over aspectRatio", () => { |
| 38 | + const node: VNode = { |
| 39 | + kind: "box", |
| 40 | + props: { border: "none", width: 10, height: 3, aspectRatio: 2 }, |
| 41 | + children: Object.freeze([]), |
| 42 | + }; |
| 43 | + const out = mustLayout(node, 30, 20); |
| 44 | + assert.deepEqual(out.rect, { x: 0, y: 0, w: 10, h: 3 }); |
| 45 | + }); |
| 46 | + |
| 47 | + test("derived height uses floor rounding", () => { |
| 48 | + const node: VNode = { |
| 49 | + kind: "box", |
| 50 | + props: { border: "none", width: 7, aspectRatio: 2 }, |
| 51 | + children: Object.freeze([]), |
| 52 | + }; |
| 53 | + const out = mustLayout(node, 30, 20); |
| 54 | + assert.deepEqual(out.rect, { x: 0, y: 0, w: 7, h: 3 }); |
| 55 | + }); |
| 56 | + |
| 57 | + test("percent width with aspectRatio resolves against parent width", () => { |
| 58 | + const node: VNode = { |
| 59 | + kind: "box", |
| 60 | + props: { border: "none", width: "50%", aspectRatio: 2 }, |
| 61 | + children: Object.freeze([]), |
| 62 | + }; |
| 63 | + const out = mustLayout(node, 15, 20); |
| 64 | + assert.deepEqual(out.rect, { x: 0, y: 0, w: 7, h: 3 }); |
| 65 | + }); |
| 66 | + |
| 67 | + test("percent height with aspectRatio resolves against parent height", () => { |
| 68 | + const node: VNode = { |
| 69 | + kind: "box", |
| 70 | + props: { border: "none", height: "25%", aspectRatio: 2 }, |
| 71 | + children: Object.freeze([]), |
| 72 | + }; |
| 73 | + const out = mustLayout(node, 20, 13); |
| 74 | + assert.deepEqual(out.rect, { x: 0, y: 0, w: 6, h: 3 }); |
| 75 | + }); |
| 76 | + |
| 77 | + test("minHeight clamp applies after width -> height derivation", () => { |
| 78 | + const node: VNode = { |
| 79 | + kind: "box", |
| 80 | + props: { border: "none", width: 8, aspectRatio: 2, minHeight: 5 }, |
| 81 | + children: Object.freeze([]), |
| 82 | + }; |
| 83 | + const out = mustLayout(node, 30, 20); |
| 84 | + assert.deepEqual(out.rect, { x: 0, y: 0, w: 8, h: 5 }); |
| 85 | + }); |
| 86 | + |
| 87 | + test("maxHeight clamp applies after width -> height derivation", () => { |
| 88 | + const node: VNode = { |
| 89 | + kind: "box", |
| 90 | + props: { border: "none", width: 20, aspectRatio: 2, maxHeight: 6 }, |
| 91 | + children: Object.freeze([]), |
| 92 | + }; |
| 93 | + const out = mustLayout(node, 30, 20); |
| 94 | + assert.deepEqual(out.rect, { x: 0, y: 0, w: 20, h: 6 }); |
| 95 | + }); |
| 96 | + |
| 97 | + test("minWidth clamp applies after height -> width derivation", () => { |
| 98 | + const node: VNode = { |
| 99 | + kind: "box", |
| 100 | + props: { border: "none", height: 4, aspectRatio: 2, minWidth: 10 }, |
| 101 | + children: Object.freeze([]), |
| 102 | + }; |
| 103 | + const out = mustLayout(node, 30, 20); |
| 104 | + assert.deepEqual(out.rect, { x: 0, y: 0, w: 10, h: 4 }); |
| 105 | + }); |
| 106 | + |
| 107 | + test("maxWidth clamp applies after height -> width derivation", () => { |
| 108 | + const node: VNode = { |
| 109 | + kind: "box", |
| 110 | + props: { border: "none", height: 10, aspectRatio: 2, maxWidth: 15 }, |
| 111 | + children: Object.freeze([]), |
| 112 | + }; |
| 113 | + const out = mustLayout(node, 30, 20); |
| 114 | + assert.deepEqual(out.rect, { x: 0, y: 0, w: 15, h: 10 }); |
| 115 | + }); |
| 116 | + |
| 117 | + test("row flex context respects aspect-derived fixed width", () => { |
| 118 | + const row: VNode = { |
| 119 | + kind: "row", |
| 120 | + props: {}, |
| 121 | + children: Object.freeze([ |
| 122 | + { |
| 123 | + kind: "box", |
| 124 | + props: { border: "none", height: 4, aspectRatio: 2 }, |
| 125 | + children: Object.freeze([]), |
| 126 | + }, |
| 127 | + { kind: "box", props: { border: "none", flex: 1 }, children: Object.freeze([]) }, |
| 128 | + ]), |
| 129 | + }; |
| 130 | + const out = mustLayout(row, 20, 6, "row"); |
| 131 | + assert.deepEqual(out.rect, { x: 0, y: 0, w: 20, h: 4 }); |
| 132 | + assert.deepEqual(out.children[0]?.rect, { x: 0, y: 0, w: 8, h: 4 }); |
| 133 | + assert.deepEqual(out.children[1]?.rect, { x: 8, y: 0, w: 12, h: 0 }); |
| 134 | + }); |
| 135 | + |
| 136 | + test("column flex context respects aspect-derived fixed height", () => { |
| 137 | + const column: VNode = { |
| 138 | + kind: "column", |
| 139 | + props: {}, |
| 140 | + children: Object.freeze([ |
| 141 | + { |
| 142 | + kind: "box", |
| 143 | + props: { border: "none", width: 6, aspectRatio: 2 }, |
| 144 | + children: Object.freeze([]), |
| 145 | + }, |
| 146 | + { kind: "box", props: { border: "none", flex: 1 }, children: Object.freeze([]) }, |
| 147 | + ]), |
| 148 | + }; |
| 149 | + const out = mustLayout(column, 8, 12, "column"); |
| 150 | + assert.deepEqual(out.rect, { x: 0, y: 0, w: 6, h: 12 }); |
| 151 | + assert.deepEqual(out.children[0]?.rect, { x: 0, y: 0, w: 6, h: 3 }); |
| 152 | + assert.deepEqual(out.children[1]?.rect, { x: 0, y: 3, w: 0, h: 9 }); |
| 153 | + }); |
| 154 | +}); |
0 commit comments