const { describe, it } = require('node:test'); const assert = require('node:assert/strict'); const Lighting = require('../src/lighting.js'); const FloorCast = require('../src/floorCast.js'); const LevelFormat = require('../src/levelFormat.js'); const fs = require('fs'); const path = require('path'); 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); }); it('faceNormal: north-ray hit faces outward +Y (south face)', () => { assert.deepEqual(Lighting.faceNormal('north'), { x: 0, y: 1, z: 0 }); assert.deepEqual(Lighting.faceNormal('south'), { x: 0, y: -1, z: 0 }); assert.deepEqual(Lighting.faceNormal('west'), { x: -1, y: 0, z: 0 }); assert.deepEqual(Lighting.faceNormal('east'), { x: 1, y: 0, z: 0 }); }); it('wall facing corridor receives light (N·L > 0) with corrected normals', () => { // Ray went east, hit west face of wall at x≈96; light in corridor to the west const n = Lighting.faceNormal('west'); const lit = Lighting.shadeRgb( { x: 95.4, y: 48, z: 16 }, n, [{ x: 70, y: 48, z: 22, color: [1, 1, 1], intensity: 2, range: 120, falloff: 1 }], 0.15, null ); assert.ok(lit.r > 0.6, 'wall lit r=' + lit.r); }); it('LOS blocked by solid between open cells', () => { assert.equal(Lighting.hasLineOfSight(8, 16, 72, 16, [0, 1, 0], 3, 1, 32), false); assert.equal(Lighting.hasLineOfSight(8, 16, 24, 16, [0, 0, 0], 3, 1, 32), true); }); it('decodeNormalRgb maps 128,128,255 toward +Z', () => { const n = Lighting.decodeNormalRgb(128, 128, 255); assert.ok(n.z > 0.9); }); it('worldDirToView: light on camera-left (−Y at yaw0 facing0) → view −X', () => { const v = Lighting.worldDirToView(0, -1, 0, 0, 0); assert.ok(v.x < -0.7, JSON.stringify(v)); }); it('NPC left half brighter than right with white lamp on camera-left', () => { const ambient = 0.12; const lamp = { x: 100, y: 60, z: 18, color: [1, 1, 1], intensity: 2.5, range: 120, falloff: 1.5 }; const facing = 0, yaw = 0; // Flat front normal + geo tilt const left = Lighting.shadePaperdollPixel( 220, 220, 220, 128, 128, 255, { x: 100, y: 85, z: 16 }, [lamp], ambient, yaw, facing, -0.8 ); const right = Lighting.shadePaperdollPixel( 220, 220, 220, 128, 128, 255, { x: 100, y: 115, z: 16 }, [lamp], ambient, yaw, facing, 0.8 ); assert.ok(left.r > right.r + 25, 'left=' + left.r + ' right=' + right.r); }); it('NPC right half brighter with lamp on camera-right (mirrored)', () => { const ambient = 0.12; const lamp = { x: 100, y: 140, z: 18, color: [1, 1, 1], intensity: 2.5, range: 120, falloff: 1.5 }; const left = Lighting.shadePaperdollPixel( 220, 220, 220, 128, 128, 255, { x: 100, y: 85, z: 16 }, [lamp], ambient, 0, 0, -0.8 ); const right = Lighting.shadePaperdollPixel( 220, 220, 220, 128, 128, 255, { x: 100, y: 115, z: 16 }, [lamp], ambient, 0, 0, 0.8 ); assert.ok(right.r > left.r + 25, 'left=' + left.r + ' right=' + right.r); }); it('floor lighting has no hard column seam (max jump small)', () => { const pack = LevelFormat.parse(fs.readFileSync(path.join(__dirname, '../build/levels.json'), 'utf8')); const level = LevelFormat.toRuntimeLevel(pack.levels[0]); const tex = { w: 1, h: 1, rgba: new Uint8ClampedArray([180, 180, 180, 255]) }; const w = 320, h = 240; const img = { data: new Uint8ClampedArray(w * h * 4) }; global.Lighting = Lighting; FloorCast.render(img, w, h, { x: 48, y: 48, rotation: 0.15, pitch: 0.25 }, level, { stone: tex, wood: tex }, 32); const jump = Lighting.maxColumnJump(function (x) { var s = 0, n = 0; for (var y = Math.floor(h * 0.65); y < h; y++) { s += img.data[(y * w + x) * 4]; n++; } return s / n; }, w); assert.ok(jump < 18, 'floor column jump=' + jump); // No near-black punch in central floor band (wall-null hole regression) var black = 0, total = 0; for (var x = 40; x < w - 40; x++) { for (var y = Math.floor(h * 0.7); y < h - 2; y++) { var o = (y * w + x) * 4; total++; if (img.data[o] + img.data[o + 1] + img.data[o + 2] < 8) { black++; } } } assert.ok(black / total < 0.02, 'black floor fraction=' + (black / total)); }); });