const assert = require("assert");

const { placeObjectWithinCanvas, resolveObjectPlacement } = require("../static/object-placement.js");

{
  const placement = placeObjectWithinCanvas({
    canvasWidth: 120,
    canvasHeight: 80,
    objectWidth: 60,
    objectHeight: 20,
  });

  assert.strictEqual(placement.scale, 1);
  assert.strictEqual(placement.left, 30);
  assert.strictEqual(placement.top, 30);
}

{
  const placement = placeObjectWithinCanvas({
    canvasWidth: 76,
    canvasHeight: 38,
    objectWidth: 96,
    objectHeight: 24,
  });

  assert(placement.scale < 1, "small labels should shrink oversized text");
  assert(placement.left >= 0, "placement should stay within canvas horizontally");
  assert(placement.top >= 0, "placement should stay within canvas vertically");
  assert(96 * placement.scale <= 76 * 0.92 + 0.001, "scaled width should fit within canvas");
  assert(24 * placement.scale <= 38 * 0.92 + 0.001, "scaled height should fit within canvas");
}

{
  const placement = resolveObjectPlacement({
    canvasWidth: 76,
    canvasHeight: 38,
    objectWidth: 90,
    objectHeight: 60,
    scaleX: 1,
    scaleY: 1,
  });

  assert(placement.scaleX < 1, "oversized shapes should shrink on small labels");
  assert(placement.scaleY < 1, "oversized shapes should shrink on small labels");
  assert(placement.left >= 0, "shape should stay within canvas horizontally");
  assert(placement.top >= 0, "shape should stay within canvas vertically");
}

{
  const placement = resolveObjectPlacement({
    canvasWidth: 50,
    canvasHeight: 30,
    objectWidth: 100,
    objectHeight: 0,
    scaleX: 1,
    scaleY: 1,
  });

  assert(placement.scaleX < 1, "wide lines should shrink to fit narrow labels");
  assert.strictEqual(placement.scaleX, placement.scaleY, "line scaling should stay uniform");
  assert(placement.top >= 0, "line should still be placed inside the label");
}

console.log("object-placement tests passed");
