Files
Portfolio/tests/unit/traffic.test.ts

43 lines
1.5 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import {
HERO_CAR_HALF_WIDTH,
TRAFFIC_FADE_LENGTH,
TRAFFIC_LAMP_HALF_SPACING,
TRAFFIC_LANES,
TRAFFIC_NEAR_Z,
trafficNearFade,
wrapTrafficZ,
} from '@/scene/world/traffic';
describe('expressway traffic stays clear of the hero car', () => {
it('keeps every lamp outside the car body, with lane jitter', () => {
for (const lane of TRAFFIC_LANES) {
const half = lane < 0 ? TRAFFIC_LAMP_HALF_SPACING.fast : TRAFFIC_LAMP_HALF_SPACING.slow;
const innerLamp = Math.abs(lane) - half - 0.25; // 0.25: max lane jitter
expect(innerLamp).toBeGreaterThan(HERO_CAR_HALF_WIDTH);
}
});
it('is fully dark before the car near field and lit beyond the fade', () => {
expect(trafficNearFade(TRAFFIC_NEAR_Z)).toBe(0);
expect(trafficNearFade(0)).toBe(0);
expect(trafficNearFade(40)).toBe(0);
expect(trafficNearFade(TRAFFIC_NEAR_Z - TRAFFIC_FADE_LENGTH)).toBe(1);
expect(trafficNearFade(-300)).toBe(1);
// The car's nose is at about z = -5.5: the near field starts well ahead.
expect(TRAFFIC_NEAR_Z).toBeLessThan(-18);
});
it('wraps vehicles inside the hidden band at any scroll speed', () => {
// 110 units/s at a 0.1 s frame clamp is 11 units per step; test far more.
for (const step of [0.5, 11, 64, 900, -11, -900]) {
let z = -200;
for (let i = 0; i < 400; i++) {
z = wrapTrafficZ(z + step);
expect(z).toBeLessThanOrEqual(TRAFFIC_NEAR_Z);
expect(z).toBeGreaterThanOrEqual(-660);
}
}
});
});