import { describe, expect, it } from "vitest"; import { compose, type Detection, findSubject, minCropWidth } from "../src/composition/compose.js"; const person = (x0: number, y0: number, x1: number, y1: number, score = 0.9): Detection => ({ x0, y0, x1, y1, score }); const inside = (r: { x0: number; y0: number; x1: number; y1: number }, b: { x0: number; y0: number; x1: number; y1: number }) => r.x0 <= b.x0 + 1e-9 && r.y0 <= b.y0 + 1e-9 && r.x1 >= b.x1 - 1e-9 && r.y1 >= b.y1 - 1e-9; const frame = { width: 3000, height: 2000 }; describe("automatic crop", () => { it("builds the crop around the ball and the players contesting it", () => { const a = person(0.4, 0.2, 0.5, 0.8); const b = person(0.52, 0.22, 0.62, 0.82); const far = person(0.05, 0.3, 0.1, 0.5); // small, behind const ball = { x0: 0.5, y0: 0.74, x1: 0.53, y1: 0.78, score: 0.8 }; const c = compose({ ...frame, people: [a, b, far], balls: [ball] })!; expect(c.reason).toBe("ball"); expect(c.people).toBe(2); expect(inside(c.rect, a) && inside(c.rect, b) && inside(c.rect, ball)).toBe(true); // Same shape as the photo, tighter than it. expect(((c.rect.x1 - c.rect.x0) * 3000) / ((c.rect.y1 - c.rect.y0) * 2000)).toBeCloseTo(1.5, 2); expect(c.rect.x1 - c.rect.x0).toBeLessThan(0.8); }); it("ignores a ball nobody is playing, and people cut by the photo's edge", () => { const main = person(0.35, 0.15, 0.5, 0.85); const edge = person(0.0, 0.1, 0.06, 0.9); // half out of the photo const s = findSubject({ ...frame, people: [main, edge], balls: [{ x0: 0.9, y0: 0.1, x1: 0.92, y1: 0.13, score: 0.9 }] })!; expect(s.reason).toBe("person"); expect(main).toMatchObject(s.box); }); it("keeps a group together, and doesn't slice a big body at the crop's edge", () => { const a = person(0.3, 0.2, 0.42, 0.8); const b = person(0.4, 0.22, 0.52, 0.82); const passer = person(0.62, 0.15, 0.78, 0.95); // big, next to the group but apart const c = compose({ ...frame, people: [a, b, passer], balls: [] })!; expect(c.reason).toBe("group"); const f = Math.max(0, Math.min(c.rect.x1, passer.x1) - Math.max(c.rect.x0, passer.x0)) / (passer.x1 - passer.x0); expect(f < 0.03 || f > 0.97).toBe(true); }); it("builds around who is in focus, not the biggest blurred player in front", () => { const blurred = { ...person(0.02, 0.05, 0.3, 1.0), sharpness: 0.15 }; const referee = { ...person(0.45, 0.25, 0.55, 0.7), sharpness: 1 }; const c = compose({ ...frame, people: [blurred, referee], balls: [] })!; expect(c.reason).toBe("person"); expect(referee).toMatchObject(c.subject); // About two thirds of the frame, with the referee in the middle. expect(c.rect.x1 - c.rect.x0).toBeGreaterThan(0.55); expect(c.rect.x1 - c.rect.x0).toBeLessThan(0.85); expect(Math.abs((c.rect.x0 + c.rect.x1) / 2 - 0.5)).toBeLessThan(0.05); }); it("never crops across a knee or an ankle, and keeps the delivery's resolution", () => { // A tall player who can't fit whole in a two-thirds crop: the bottom edge must miss the joints. const p = { ...person(0.42, 0.05, 0.58, 1.0), sharpness: 1 }; const c = compose({ ...frame, people: [p], balls: [] }, null, { minWidth: 0.5 })!; const at = (c.rect.y1 - p.y0) / (p.y1 - p.y0); for (const j of [0.74, 0.95]) if (c.rect.y1 < 1) expect(Math.abs(at - j)).toBeGreaterThanOrEqual(0.035); expect(c.rect.x1 - c.rect.x0).toBeGreaterThanOrEqual(0.5 - 1e-9); expect(minCropWidth(6000, 4000)).toBeCloseTo(0.4, 5); expect(minCropWidth(2048, 1365)).toBe(1); }); it("frames the upper body near the middle, with the spare height above and not a strip of pitch below", () => { // A player kneeling in celebration, small in a wide frame (Braga x Sporting, October 2026). const kneeling = { ...person(0.41, 0.19, 0.58, 0.55), sharpness: 1 }; const c = compose({ width: 6000, height: 4000, people: [kneeling], balls: [] }, null, { minWidth: 0.4 })!; const ch = c.rect.y1 - c.rect.y0; const below = (c.rect.y1 - kneeling.y1) / ch; const chest = (kneeling.y0 + 0.3 * (kneeling.y1 - kneeling.y0) - c.rect.y0) / ch; expect(below).toBeLessThanOrEqual(0.12); expect(chest).toBeGreaterThan(0.3); expect(chest).toBeLessThan(0.5); // Heads below the top edge, never touching it. expect((kneeling.y0 - c.rect.y0) / ch).toBeGreaterThanOrEqual(0.03); }); it("proposes nothing for an empty pitch or a distant crowd", () => { expect(compose({ ...frame, people: [person(0.1, 0.4, 0.13, 0.45), person(0.5, 0.4, 0.52, 0.46)], balls: [] })).toBeNull(); }); it("uses the aspect asked for", () => { const c = compose({ ...frame, people: [person(0.4, 0.2, 0.55, 0.85)], balls: [] }, 4 / 5)!; expect(((c.rect.x1 - c.rect.x0) * 3000) / ((c.rect.y1 - c.rect.y0) * 2000)).toBeCloseTo(0.8, 2); }); });