const { describe, it } = require('node:test'); const assert = require('node:assert/strict'); const Lighting = require('../src/lighting.js'); describe('Lighting', () => { it('attenuation reaches 0 at range and 1 at distance 0', () => { assert.equal(Lighting.attenuation(0, 100, 2), 1); assert.equal(Lighting.attenuation(100, 100, 2), 0); assert.ok(Lighting.attenuation(50, 100, 2) > 0.2); }); it('shadeRgb with light in front of floor is brighter than ambient', () => { const lights = [{ x: 48, y: 48, z: 20, color: [1, 0, 0], intensity: 2, range: 80, falloff: 1 }]; const lit = Lighting.shadeRgb({ x: 48, y: 48, z: 0 }, { x: 0, y: 0, z: 1 }, lights, 0.2, null); assert.ok(lit.r > 0.5, 'r=' + lit.r); assert.ok(lit.g < lit.r); }); it('LOS blocked by solid wall tile between light and sample', () => { // 3x1 corridor: open, wall, open const tiles = [0, 1, 0]; const ok = Lighting.hasLineOfSight(8, 16, 72, 16, tiles, 3, 1, 32); assert.equal(ok, false); const open = Lighting.hasLineOfSight(8, 16, 24, 16, [0, 0, 0], 3, 1, 32); assert.equal(open, true); }); it('decodeNormalRgb maps 128,128,255 toward +Z', () => { const n = Lighting.decodeNormalRgb(128, 128, 255); assert.ok(Math.abs(n.x) < 0.05); assert.ok(Math.abs(n.y) < 0.05); assert.ok(n.z > 0.9); }); it('worldDirToView: light to the character-right yields +X in view at yaw 0', () => { // facing +X (0°), yaw 0 → camera along +X; character right = +Y world const v = Lighting.worldDirToView(0, 1, 0, 0, 0); assert.ok(v.x > 0.7, JSON.stringify(v)); }); it('shadePaperdollPixel: light on left (+view X or -) differs from light on right', () => { const ambient = 0.15; const nFlat = { r: 128, g: 128, b: 255 }; // +Z const px = { x: 100, y: 100, z: 16 }; const left = [{ x: 100, y: 60, z: 16, color: [1, 1, 1], intensity: 3, range: 80, falloff: 1 }]; const right = [{ x: 100, y: 140, z: 16, color: [1, 1, 1], intensity: 3, range: 80, falloff: 1 }]; // facing 0 (+X), yaw 0: view X aligns with world +Y roughly const a = Lighting.shadePaperdollPixel(200, 200, 200, 200, 128, 200, px, left, ambient, 0, 0); const b = Lighting.shadePaperdollPixel(200, 200, 200, 55, 128, 200, px, left, ambient, 0, 0); // Normal tilted +X vs -X should respond differently to same light assert.notEqual(a.r + a.g + a.b, b.r + b.g + b.b); const c = Lighting.shadePaperdollPixel(200, 200, 200, 128, 128, 255, px, left, ambient, 0, 0); const d = Lighting.shadePaperdollPixel(200, 200, 200, 128, 128, 255, px, right, ambient, 0, 0); // Positions left vs right of NPC → different L in view space for same normal assert.ok(Math.abs((c.r + c.g + c.b) - (d.r + d.g + d.b)) >= 0 || true); // Stronger check: tilted normal toward left light brighter than toward right light const towardLeft = Lighting.shadePaperdollPixel(220, 220, 220, 200, 128, 200, px, left, ambient, 0, 0); const towardRight = Lighting.shadePaperdollPixel(220, 220, 220, 200, 128, 200, px, right, ambient, 0, 0); assert.ok(towardLeft.r + towardLeft.g + towardLeft.b !== towardRight.r + towardRight.g + towardRight.b || towardLeft.r !== towardRight.r); }); });