Tooling: - Prettier as the single formatting authority (110 columns, single quotes, measured against the existing style); Markdown excluded because table-cell padding turns the README glossary into 1000-character lines. - Type-aware ESLint ending in eslint-config-prettier, so a rule always means "this is a bug", never "this looks different". - .editorconfig and an `npm run check` that runs typecheck, lint, format and tests in one go. Findings fixed on the first run: three more duplicate imports, four String(unknown) calls (parameter types narrowed instead of casts), two dead initialisers, two redundant casts, a console.log for a real error. Two warnings were false here and are disabled with a reason in place. Tests 132 -> 269: - node-catalog.test.ts covers every node with a run(), with a guard that fails when a new node has no row. - behavior-events.test.ts covers the ten event nodes through a running World, checking timing rather than existence. - Input's state machine (press/release/releaseAll) is reachable without DOM events now; the DOM handlers are thin adapters. Compare fell back to == without a configured operator although the schema default is <; aligned. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
59 lines
2.1 KiB
TypeScript
59 lines
2.1 KiB
TypeScript
/**
|
||
* The viewport transforms used to be private methods of a Pixi widget and could only be
|
||
* checked through the browser. They are plain geometry, so they are pinned here.
|
||
*/
|
||
import { describe, expect, it } from 'vitest';
|
||
import {
|
||
cameraView,
|
||
freeView,
|
||
isInside,
|
||
screenToWorld,
|
||
worldBounds,
|
||
worldToScreen,
|
||
} from '../../src/viewport/view';
|
||
|
||
describe('freeView', () => {
|
||
it('puts the view centre in the middle of the widget', () => {
|
||
const view = freeView(50, -20, 2, 800, 600);
|
||
expect(worldToScreen(view, 50, -20)).toEqual({ x: 400, y: 300 });
|
||
expect(screenToWorld(view, 400, 300)).toEqual({ x: 50, y: -20 });
|
||
});
|
||
|
||
it('scales distances by the zoom', () => {
|
||
const view = freeView(0, 0, 2, 800, 600);
|
||
expect(worldToScreen(view, 100, 0)).toEqual({ x: 600, y: 300 });
|
||
});
|
||
|
||
it('round-trips any point', () => {
|
||
const view = freeView(13, -7, 0.35, 640, 480);
|
||
const back = screenToWorld(view, ...(Object.values(worldToScreen(view, 123, -456)) as [number, number]));
|
||
expect(back.x).toBeCloseTo(123, 9);
|
||
expect(back.y).toBeCloseTo(-456, 9);
|
||
});
|
||
|
||
it('covers the widget in world units', () => {
|
||
const bounds = worldBounds(freeView(0, 0, 2, 800, 600));
|
||
expect(bounds).toEqual({ left: -200, top: -150, right: 200, bottom: 150 });
|
||
});
|
||
});
|
||
|
||
describe('cameraView', () => {
|
||
const camera = { x: 10, y: 20, props: { width: 640, height: 360 } };
|
||
|
||
it('fits the camera frame into the widget and centres it', () => {
|
||
// 640×360 into 800×600: width is the tighter fit (1.25 vs 1.67), so bars top and bottom.
|
||
const view = cameraView(camera, 800, 600);
|
||
expect(view.scale).toBeCloseTo(1.25, 9);
|
||
expect(view.rect).toEqual({ x: 0, y: 75, w: 800, h: 450 });
|
||
// The camera position sits at the centre of the frame, not of the widget.
|
||
expect(worldToScreen(view, 10, 20)).toEqual({ x: 400, y: 300 });
|
||
});
|
||
|
||
it('reports the letterbox as outside', () => {
|
||
const view = cameraView(camera, 800, 600);
|
||
expect(isInside(view, 400, 300)).toBe(true);
|
||
expect(isInside(view, 400, 20)).toBe(false); // top bar
|
||
expect(isInside(view, 400, 580)).toBe(false); // bottom bar
|
||
});
|
||
});
|