/**
 * One material property that costs 43% of the California board.
 *
 * `MeshPhysicalMaterial.transmission` is not a per-pixel cost. three.js runs a
 * **transmission backdrop pass** whenever any rendered material has a non-zero
 * one: the entire opaque scene is drawn a second time, into a render target, so
 * that the transparent surface has something to refract. It is charged per
 * *scene*, not per material and not per pixel, so a single small mesh switches
 * it on for everything.
 *
 * The electric aircraft's canopy carried `transmission: 0.08` and it made the
 * board draw its terrain, its blocks and every freeway piece twice per frame.
 * Measured with `scripts/performance-budget.mjs`, California desktop:
 *
 *   with it     695,828 triangles   562 draw calls
 *   without it  391,169 triangles   371 draw calls
 *
 * 304,659 triangles and 191 draw calls, for a canopy that is a few dozen pixels
 * of dark glass on a glyph-scale aeroplane and which reads identically without
 * it — the material is already `transparent` at `opacity: 0.86`, and the chase
 * camera shots before and after are indistinguishable.
 *
 * This test exists because that is a **one-word regression**: somebody adding
 * realism to a canopy would be adding it to a material that looks like it is
 * about the aeroplane, and the cost would land on the terrain, silently, on
 * every phone. The performance budget would catch it, eventually, and would say
 * "the California board got slower" rather than "this line did it".
 *
 * The office glazing in `src/assets/materials.ts` keeps `transmission: 0.92`
 * and should: that is a wall of windows a metre from the camera at 1 unit = 1 m,
 * and the office cell has four hundred thousand triangles of headroom to pay the
 * pass with. `materialRoles.test.ts` asserts it is still there. The rule is not
 * "no transmission"; it is "not in the city scene".
 */

import assert from "node:assert/strict";
import { test } from "node:test";
import * as THREE from "three";
import { createElectricAircraftMaterials } from "../../aircraft/asset.ts";

test("the electric aircraft's canopy stays out of the transmission pass", () => {
  const materials = createElectricAircraftMaterials();
  const glass = materials.glass as THREE.MeshPhysicalMaterial;
  assert.ok(glass instanceof THREE.MeshPhysicalMaterial, "the canopy is no longer physical");
  assert.equal(
    glass.transmission,
    0,
    "the canopy is refracting again — that is a second full pass over the whole city scene",
  );
  // What carries the see-through instead, so a future reader can tell that the
  // glass was not simply turned into a painted panel.
  assert.equal(glass.transparent, true, "the canopy stopped being see-through altogether");
  assert.ok(glass.opacity < 1, "the canopy is opaque; it needs the blend to read as glass");
});

test("no material the aircraft ships turns the pass on by another door", () => {
  const materials = createElectricAircraftMaterials();
  for (const [role, material] of Object.entries(materials)) {
    const transmission = (material as THREE.MeshPhysicalMaterial).transmission;
    assert.ok(
      transmission === undefined || transmission === 0,
      `${role} has transmission ${transmission}; the whole scene is now drawn twice`,
    );
  }
});
