import test from "node:test";
import assert from "node:assert/strict";
import { beginPull, finishPull, movePull } from "./pull-to-refresh.js";

test("downward pull arms only after crossing the refresh threshold", () => {
  let state = beginPull({ direction: "down", coordinate: 100, atBoundary: true, refreshing: false });
  state = movePull(state, 170);

  assert.equal(state.distance, 35);
  assert.equal(state.armed, false);

  state = movePull(state, 240);
  assert.equal(state.distance, 52);
  assert.equal(state.armed, true);
  assert.equal(finishPull(state).shouldRefresh, true);
});

test("upward pull arms from the bottom boundary", () => {
  let state = beginPull({ direction: "up", coordinate: 300, atBoundary: true, refreshing: false });
  state = movePull(state, 160);

  assert.equal(state.distance, 52);
  assert.equal(state.armed, true);
});

test("pull stays inactive away from the required boundary or during refresh", () => {
  const away = beginPull({ direction: "down", coordinate: 100, atBoundary: false, refreshing: false });
  const busy = beginPull({ direction: "up", coordinate: 100, atBoundary: true, refreshing: true });

  assert.equal(movePull(away, 240).active, false);
  assert.equal(movePull(busy, 0).active, false);
  assert.equal(finishPull(away).shouldRefresh, false);
});

test("movement in the normal scroll direction cancels pull progress", () => {
  let state = beginPull({ direction: "down", coordinate: 100, atBoundary: true, refreshing: false });
  state = movePull(state, 60);

  assert.equal(state.distance, 0);
  assert.equal(state.armed, false);
  assert.equal(finishPull(state).shouldRefresh, false);
});

test("pull indicator stays within the projects heading row at 52 pixels", () => {
  let down = beginPull({ direction: "down", coordinate: 0, atBoundary: true, refreshing: false });
  let up = beginPull({ direction: "up", coordinate: 1000, atBoundary: true, refreshing: false });

  down = movePull(down, 1000);
  up = movePull(up, 0);

  assert.equal(down.distance, 52);
  assert.equal(up.distance, 52);
  assert.equal(down.armed, true);
  assert.equal(up.armed, true);
});
