/**
 * Run with: node --experimental-strip-types --test src/game/interp.test.ts
 */
import { describe, it } from "node:test";
import assert from "node:assert/strict";
import { createMatch, stepMatch, blankPad } from "./engine.ts";
import { RenderInterp } from "./interp.ts";
import { STEP } from "./constants.ts";

function pads() {
  return { p0: blankPad(), p1: blankPad() };
}

describe("RenderInterp", () => {
  it("blends between the previous and current step, then restores exactly", () => {
    const m = createMatch({ mode: "demo", difficulty: "normal", seed: 7 });
    const it = new RenderInterp();
    for (let i = 0; i < 30; i++) stepMatch(m, pads(), STEP);
    it.capture(m);
    const before = { x: m.ball.x, y: m.ball.y, fx: m.fighters[0].x, fy: m.fighters[0].y };
    stepMatch(m, pads(), STEP);
    const after = { x: m.ball.x, y: m.ball.y, fx: m.fighters[0].x, fy: m.fighters[0].y };
    assert.ok(after.y !== before.y, "la pelota cae, así que cambia de y");

    assert.equal(it.apply(m, 0.5), true);
    assert.ok(Math.abs(m.ball.y - (before.y + after.y) / 2) < 1e-9);
    it.restore(m);
    assert.equal(m.ball.x, after.x);
    assert.equal(m.ball.y, after.y);
    assert.equal(m.fighters[0].x, after.fx);
    assert.equal(m.fighters[0].y, after.fy);
  });

  it("alpha 0 shows the previous step, alpha >= 1 does nothing", () => {
    const m = createMatch({ mode: "demo", difficulty: "normal", seed: 7 });
    const it = new RenderInterp();
    for (let i = 0; i < 10; i++) stepMatch(m, pads(), STEP);
    it.capture(m);
    const prevY = m.ball.y;
    stepMatch(m, pads(), STEP);
    const curY = m.ball.y;
    assert.equal(it.apply(m, 1), false);
    assert.equal(m.ball.y, curY);
    assert.equal(it.apply(m, 0), true);
    assert.equal(m.ball.y, prevY);
    it.restore(m);
    assert.equal(m.ball.y, curY);
  });

  it("does nothing before the first capture or after reset", () => {
    const m = createMatch({ mode: "demo", difficulty: "normal", seed: 7 });
    const it = new RenderInterp();
    assert.equal(it.apply(m, 0.5), false);
    it.capture(m);
    it.reset();
    assert.equal(it.apply(m, 0.5), false);
  });

  it("snaps instead of gliding across a teleport (kickoff reset)", () => {
    const m = createMatch({ mode: "demo", difficulty: "normal", seed: 7 });
    const it = new RenderInterp();
    it.capture(m);
    const oldX = m.ball.x;
    m.ball.x = oldX + 400; // saque: salto enorme en un solo paso
    it.apply(m, 0.5);
    assert.equal(m.ball.x, oldX + 400, "no se dibuja a medio camino");
    it.restore(m);
    assert.equal(m.ball.x, oldX + 400);
  });

  it("never changes the simulation: hash-relevant state is intact after a draw cycle", () => {
    const a = createMatch({ mode: "demo", difficulty: "normal", seed: 11 });
    const b = createMatch({ mode: "demo", difficulty: "normal", seed: 11 });
    const it = new RenderInterp();
    for (let i = 0; i < 240; i++) {
      it.capture(a);
      stepMatch(a, pads(), STEP);
      stepMatch(b, pads(), STEP);
      if (it.apply(a, (i % 7) / 7)) it.restore(a); // dibujar y restaurar
    }
    assert.equal(a.ball.x, b.ball.x);
    assert.equal(a.ball.y, b.ball.y);
    assert.equal(a.fighters[0].x, b.fighters[0].x);
    assert.equal(a.fighters[1].y, b.fighters[1].y);
    assert.equal(a.rng.s, b.rng.s);
  });
});
