25 lines
1.4 KiB
JavaScript
25 lines
1.4 KiB
JavaScript
import assert from "node:assert/strict";
|
|
import fs from "node:fs";
|
|
import path from "node:path";
|
|
import test from "node:test";
|
|
import ts from "../../node_modules/typescript/lib/typescript.js";
|
|
|
|
const root = path.resolve(import.meta.dirname, "../../..");
|
|
const sourcePath = path.join(root, "web/protocol/viewport-dpr.ts");
|
|
const output = ts.transpileModule(fs.readFileSync(sourcePath, "utf8"), { compilerOptions: { module: ts.ModuleKind.ES2022, target: ts.ScriptTarget.ES2022 }, fileName: sourcePath, reportDiagnostics: true });
|
|
assert.deepEqual(output.diagnostics, []);
|
|
const dpr = await import(`data:text/javascript;base64,${Buffer.from(output.outputText).toString("base64")}`);
|
|
|
|
test("M14-04B clamps DPR and computes stable backing dimensions", () => {
|
|
for (const [observed, expected] of [[1, 1], [1.5, 1.5], [2, 2], [3, 2], [0, 1], [Number.NaN, 1]]) {
|
|
const metrics = dpr.resolveViewportPixelMetrics(101, 57, observed);
|
|
assert.equal(metrics.pixelRatio, expected);
|
|
assert.deepEqual([metrics.backingWidth, metrics.backingHeight], [Math.floor(101 * expected), Math.floor(57 * expected)]);
|
|
}
|
|
});
|
|
|
|
test("M14-04B uses CSS bounds for DPR-independent NDC", () => {
|
|
assert.deepEqual(dpr.viewportNDC(150, 75, { left: 100, top: 25, width: 100, height: 100 }), { x: 0, y: 0 });
|
|
assert.throws(() => dpr.viewportNDC(0, 0, { left: 0, top: 0, width: 0, height: 1 }), /VIEWPORT_BOUNDS_INVALID/);
|
|
});
|