import { readFileSync } from "node:fs"; import assert from "node:assert/strict"; import { test } from "node:test"; import ts from "typescript"; const exports = {}; new Function("exports", ts.transpileModule(readFileSync(new URL("../src/lib/reticle.ts", import.meta.url), "utf8"), { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022 }, }).outputText)(exports); const { aimPoint, computeLayout } = exports; test("calibrated aim follows the same image point through all four mirror combinations", () => { const geometry = computeLayout({ clientWidth: 800, clientHeight: 600 }, 640, 480, { zoom: 2, x: 40, y: -30 }); const crosshair = { offsetX: 0.1, offsetY: -0.2 }; for (const horizontal of [false, true]) { for (const vertical of [false, true]) { const aim = aimPoint(geometry, crosshair, horizontal, 800, 600, vertical); assert.equal(aim.x, horizontal ? 200 : 600); assert.equal(aim.y, vertical ? 570 : 30); // 校准时向右下拖动 16/12 像素,镜像不改变屏幕上的移动方向。 const moved = aimPoint(geometry, { offsetX: crosshair.offsetX + (horizontal ? -16 : 16) / geometry.drawWidth, offsetY: crosshair.offsetY + (vertical ? -12 : 12) / geometry.drawHeight, }, horizontal, 800, 600, vertical); assert.ok(Math.abs(moved.x - aim.x - 16) < 1e-9); assert.ok(Math.abs(moved.y - aim.y - 12) < 1e-9); } } }); test("missing frames keep the aim centered regardless of mirror or offsets", () => { const empty = computeLayout({ clientWidth: 800, clientHeight: 600 }, 0, 0, { zoom: 2, x: 40, y: -30 }); assert.deepEqual(aimPoint(empty, { offsetX: 0.2, offsetY: -0.1 }, true, 800, 600, true), { x: 400, y: 300 }); });