import { describe, expect, it } from "vitest"; import { clipsBall, compose, facingSide, findSubject, frameScore, isHandball, isLongPrime, isReportage, isWideZoomOpen, minCropWidth, planCrop, sendBlockedByBall, shotIntent, type Detection } 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 // At the feet: the ball in play for a sport that plays it on the ground, not for handball. 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("does not throw a second player out of the frame", () => { const main = person(0.18, 0.18, 0.4, 0.86); const other = person(0.68, 0.2, 0.9, 0.84); const c = compose({ ...frame, people: [main, other], balls: [] }, null, { framing: "tight", context: "sport" }); if (!c) return; expect(inside(c.rect, main)).toBe(true); expect(inside(c.rect, other)).toBe(true); }); 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; soft foreground is left out when possible, so the crop may sit off-centre. expect(c.rect.x1 - c.rect.x0).toBeGreaterThan(0.55); expect(c.rect.x1 - c.rect.x0).toBeLessThan(0.85); expect(inside(c.rect, referee)).toBe(true); const softShare = Math.max(0, Math.min(c.rect.x1, blurred.x1) - Math.max(c.rect.x0, blurred.x0)) * Math.max(0, Math.min(c.rect.y1, blurred.y1) - Math.max(c.rect.y0, blurred.y0)); expect(softShare / ((blurred.x1 - blurred.x0) * (blurred.y1 - blurred.y0))).toBeLessThan(0.5); }); 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); // A large file may crop down to 3000 px. The old 35% floor held it near 4000. expect(minCropWidth(12000, 8000)).toBeCloseTo(0.25, 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); }); }); describe("tight framing (the agencies' handball look)", () => { it("keeps feet that are whole in the photo, sole inside the frame", () => { const p = { ...person(0.42, 0.12, 0.58, 0.78), sharpness: 1 }; const c = compose({ ...frame, people: [p], balls: [] }, null, { framing: "tight" })!; expect(c.rect.y1).toBeGreaterThan(p.y1); expect(c.rect.y0).toBeLessThanOrEqual(p.y0); expect(((c.rect.x1 - c.rect.x0) * 3000) / ((c.rect.y1 - c.rect.y0) * 2000)).toBeCloseTo(1.5, 2); }); it("cuts mid-limb only when the feet already leave the photo, never at a joint or below the ball", () => { const p = { ...person(0.42, 0.05, 0.56, 1), sharpness: 1 }; const ball = { x0: 0.5, y0: 0.35, x1: 0.54, y1: 0.41, score: 0.8 }; const full = compose({ ...frame, people: [p], balls: [ball] })!; const tight = compose({ ...frame, people: [p], balls: [ball] }, null, { framing: "tight" })!; const hT = tight.rect.y1 - tight.rect.y0; // Both stop on the limb once the foot is gone, so the tight frame is not a looser one. expect(hT).toBeLessThanOrEqual(full.rect.y1 - full.rect.y0); expect(tight.rect.y1).toBeLessThan(p.y1); expect(tight.rect.y0).toBeLessThanOrEqual(p.y0); expect(tight.rect.y1).toBeGreaterThan(ball.y1); const at = (tight.rect.y1 - p.y0) / (p.y1 - p.y0); for (const j of [0.53, 0.74, 0.95]) expect(Math.abs(at - j)).toBeGreaterThanOrEqual(0.035); expect(at).toBeLessThanOrEqual(0.9); expect(((tight.rect.x1 - tight.rect.x0) * 3000) / (hT * 2000)).toBeCloseTo(1.5, 2); }); it("keeps a complete foot on a match photo that has no ball", () => { const p = { ...person(0.42, 0.12, 0.58, 0.78), sharpness: 1 }; const c = compose({ ...frame, people: [p], balls: [] }, null, { framing: "tight", context: "sport" })!; expect(c.rect.y1).toBeGreaterThan(p.y1); expect(c.rect.x0).toBeLessThanOrEqual(p.x0); expect(c.rect.x1).toBeGreaterThanOrEqual(p.x1); }); it("keeps a ball held low, and the feet when they are in the photo", () => { const p = { ...person(0.42, 0.1, 0.56, 0.9), sharpness: 1 }; const ball = { x0: 0.5, y0: 0.7, x1: 0.54, y1: 0.76, score: 0.8 }; const tight = compose({ ...frame, people: [p], balls: [ball] }, null, { framing: "tight" })!; expect(tight.rect.y1).toBeGreaterThan(ball.y1); expect(tight.rect.y1).toBeGreaterThan(p.y1); }); }); describe("the ball follows the sport", () => { it("knows handball by either name", () => { expect(isHandball("Andebol")).toBe(true); expect(isHandball("handball")).toBe(true); expect(isHandball("futebol")).toBe(false); expect(isHandball(null)).toBe(false); }); it("keeps a ball in the outstretched hand inside the crop", () => { const player = person(0.48, 0.15, 0.62, 0.92); const ball = { x0: 0.02, y0: 0.28, x1: 0.08, y1: 0.36, score: 0.9 }; const c = compose({ ...frame, people: [player], balls: [ball] }, null, { framing: "tight", context: "sport", sport: "andebol" })!; expect(c.reason).toBe("ball"); expect(inside(c.rect, player)).toBe(true); expect(inside(c.rect, ball)).toBe(true); }); it("in handball the ball in the hands wins over one on the floor, even when the floor ball overlaps the player and is closer to the body's middle", () => { const player = { ...person(0.4, 0.15, 0.66, 0.996), sharpness: 1 }; const inHand = { x0: 0.48, y0: 0.08, x1: 0.56, y1: 0.18, score: 0.9 }; // Inside the player's box, and closer to the centre than the ball above the head. const onTheFloor = { x0: 0.48, y0: 0.9, x1: 0.56, y1: 0.97, score: 0.95 }; const c = compose({ width: 6000, height: 4000, people: [player], balls: [onTheFloor, inHand] }, null, { context: "reportage", sport: "andebol", minWidth: 0.4 })!; expect(c.reason).toBe("ball"); expect(inside(c.rect, inHand)).toBe(true); expect(c.rect.y1).toBeLessThan(onTheFloor.y0); }); it("in football the ball at the feet is the one in play", () => { const player = person(0.4, 0.2, 0.58, 0.9); const atFeet = { x0: 0.45, y0: 0.82, x1: 0.52, y1: 0.88, score: 0.8 }; const c = compose({ ...frame, people: [player], balls: [atFeet] }, null, { context: "sport", sport: "futebol" })!; expect(c.reason).toBe("ball"); expect(inside(c.rect, atFeet)).toBe(true); }); }); describe("reportage", () => { it("is a generic event, and a match is not", () => { expect(isReportage({ away: "", fixtureId: null })).toBe(true); expect(isReportage({ away: " ", fixtureId: null })).toBe(true); expect(isReportage({ away: "Sporting", fixtureId: null })).toBe(false); expect(isReportage({ away: "", fixtureId: "11111111-1111-4111-8111-111111111111" })).toBe(false); }); it("names the shot from the frame", () => { expect(shotIntent([person(0.35, 0.28, 0.52, 0.72)])).toBe("scene"); expect(shotIntent([person(0.3, 0.12, 0.48, 0.82)])).toBe("figure"); const left = person(0.16, 0.08, 0.36, 0.86); const right = person(0.62, 0.06, 0.84, 0.88); expect(shotIntent([left, right])).toBe("group"); expect(shotIntent([person(0.36, 0.15, 0.66, 0.996)])).toBe("closed"); }); it("crops a single person standing in the frame, and keeps them whole", () => { const speaker = person(0.35, 0.28, 0.52, 0.72); const room = compose({ ...frame, people: [speaker], balls: [] }, null, { context: "reportage" })!; expect(inside(room.rect, speaker)).toBe(true); expect(room.rect.x1 - room.rect.x0).toBeLessThan(0.92); expect(room.rect.y1).toBeGreaterThan(speaker.y1); const match = compose({ ...frame, people: [speaker], balls: [] }, null, { context: "sport" }); expect(match).not.toBeNull(); }); it("knows a long prime from a zoom and from a short prime", () => { expect(isLongPrime({ focalLength: 135, range: [135, 135] })).toBe(true); expect(isLongPrime({ focalLength: 100, range: [100, 100] })).toBe(true); expect(isLongPrime({ focalLength: 85, range: [85, 85] })).toBe(false); expect(isLongPrime({ focalLength: 200, range: [70, 200] })).toBe(false); expect(isLongPrime({ focalLength: 400, name: "RF100-500mm F4.5-7.1" })).toBe(false); expect(isLongPrime({ focalLength: 135, name: "RF135mm F1.8" })).toBe(true); expect(isLongPrime({ focalLength: 200 })).toBe(false); }); it("crops a full-length on a prime and on a zoom, and keeps a suitcase with the person", () => { const p = person(0.3, 0.12, 0.48, 0.82); const photo = { ...frame, people: [p], balls: [] }; const prime = compose(photo, null, { context: "reportage", lens: { focalLength: 135, range: [135, 135] } })!; expect(prime.rect.y0).toBeLessThanOrEqual(p.y0); expect(prime.rect.y1).toBeGreaterThan(p.y1); expect(prime.rect.x0).toBeLessThanOrEqual(p.x0); expect(prime.rect.x1).toBeGreaterThanOrEqual(p.x1); const width = prime.rect.x1 - prime.rect.x0; expect(width).toBeGreaterThan(0.7); expect(width).toBeLessThan(0.92); const zoom = compose(photo, null, { context: "reportage", lens: { focalLength: 135, range: [70, 200] } })!; expect(inside(zoom.rect, p)).toBe(true); expect(zoom.rect.x1 - zoom.rect.x0).toBeLessThan(0.92); const wide = person(0.35, 0.28, 0.52, 0.72); const suitcase = { x0: 0.54, y0: 0.5, x1: 0.64, y1: 0.7, score: 0.8 }; const scene = compose({ ...frame, people: [wide], balls: [], carried: [suitcase] }, null, { context: "reportage", lens: { focalLength: 200, range: [200, 200] } })!; expect(inside(scene.rect, wide)).toBe(true); expect(inside(scene.rect, suitcase)).toBe(true); expect(scene.rect.y1).toBeGreaterThan(suitcase.y1); }); it("crops a reportage full-length without a sports fill, and a match of the same picture still closes", () => { const p = person(0.3, 0.12, 0.48, 0.82); const room = compose({ ...frame, people: [p], balls: [] }, null, { framing: "tight", context: "reportage" })!; expect(inside(room.rect, p)).toBe(true); expect(room.rect.x1 - room.rect.x0).toBeLessThan(0.92); const match = compose({ ...frame, people: [p], balls: [] }, null, { framing: "tight", context: "sport" }); expect(match).not.toBeNull(); if (!match) return; expect(match.rect.x1 - match.rect.x0).toBeLessThan(0.92); }); it("leaves a conversation as it was framed", () => { const left = { ...person(0.16, 0.08, 0.36, 0.86), sharpness: 0.4 }; const center = { ...person(0.38, 0.16, 0.6, 0.92), sharpness: 1 }; const right = { ...person(0.62, 0.06, 0.84, 0.88), sharpness: 0.45 }; const c = compose({ ...frame, people: [left, center, right], balls: [] }, null, { framing: "tight", context: "reportage" }); expect(c).toBeNull(); expect(shotIntent([left, center, right])).toBe("group"); }); it("does not slice the head of a person the crop otherwise keeps", () => { const main = person(0.4, 0.2, 0.58, 0.9); const beside = person(0.62, 0.08, 0.8, 0.88); const c = compose({ ...frame, people: [main, beside], balls: [] }, null, { context: "sport" })!; const headCut = (p: Detection) => { const overlap = Math.max(0, Math.min(c.rect.x1, p.x1) - Math.max(c.rect.x0, p.x0)) * Math.max(0, Math.min(c.rect.y1, p.y1) - Math.max(c.rect.y0, p.y0)); const share = overlap / ((p.x1 - p.x0) * (p.y1 - p.y0)); return share >= 0.35 && c.rect.y0 > p.y0 + 0.03 * (p.y1 - p.y0); }; expect(headCut(main)).toBe(false); expect(headCut(beside)).toBe(false); }); it("leaves out a half person at the frame edge, and does not stop on the sock when the feet are already gone", () => { // PSR17710. The player fills the frame down to the socks. Another player is already cut in half on the left, // and a spare ball sits on the floor beside her. The crop is about the player with the ball. const player = { ...person(0.36, 0.15, 0.66, 0.996), sharpness: 1 }; const half = { ...person(0, 0.22, 0.14, 0.98), sharpness: 0.7 }; const inHand = { x0: 0.48, y0: 0.06, x1: 0.55, y1: 0.16, score: 0.9 }; const onTheFloor = { x0: 0.06, y0: 0.7, x1: 0.12, y1: 0.78, score: 0.8 }; const c = compose({ width: 6000, height: 4000, people: [player, half], balls: [onTheFloor, inHand] }, null, { context: "reportage", sport: "andebol", minWidth: 0.4 })!; expect(c.people).toBe(1); expect(c.rect.x0).toBeGreaterThan(half.x1 - 0.02); expect(c.rect.y0).toBeLessThanOrEqual(player.y0); expect(c.rect.y1).toBeGreaterThan(inHand.y1); const at = (c.rect.y1 - player.y0) / (player.y1 - player.y0); expect(at).toBeGreaterThan(0.55); expect(at).toBeLessThanOrEqual(0.9); for (const j of [0.53, 0.74, 0.95]) expect(Math.abs(at - j)).toBeGreaterThanOrEqual(0.035); }); it("leaves a full-length that already fills the frame, so the foot stays", () => { const p = person(0.4, 0.02, 0.55, 0.984); const c = compose({ ...frame, people: [p], balls: [] }, null, { framing: "tight", context: "reportage" }); expect(c).toBeNull(); expect(shotIntent([p])).toBe("figure"); }); it("does not close a full-length to delete a hand at the edge", () => { const subject = person(0.22, 0.18, 0.4, 0.82); const hand = person(0, 0.35, 0.07, 0.5, 0.8); const c = compose({ ...frame, people: [subject, hand], balls: [] }, null, { context: "reportage" })!; expect(inside(c.rect, subject)).toBe(true); const handIn = Math.max(0, Math.min(c.rect.x1, hand.x1) - Math.max(c.rect.x0, hand.x0)) / (hand.x1 - hand.x0); expect(handIn).toBeLessThan(0.5); }); it("drops a person the frame already cut off, and keeps the full-length around the subject", () => { const subject = person(0.32, 0.18, 0.5, 0.84); const cutOff = person(0, 0.2, 0.16, 0.95); const c = compose({ ...frame, people: [subject, cutOff], balls: [] }, null, { context: "reportage" })!; const cutIn = Math.max(0, Math.min(c.rect.x1, cutOff.x1) - Math.max(c.rect.x0, cutOff.x0)) / (cutOff.x1 - cutOff.x0); expect(cutIn).toBeLessThan(0.05); expect(c.rect.x0).toBeLessThanOrEqual(subject.x0); expect(c.rect.x1).toBeGreaterThanOrEqual(subject.x1); expect(c.rect.y0).toBeLessThanOrEqual(subject.y0); expect(c.rect.y1).toBeGreaterThan(subject.y1); expect(c.rect.y1 - c.rect.y0).toBeGreaterThan(0.7); }); it("crops the whole person at the car, not the sharper face in the window", () => { // PSR15838. The standing woman touches the frame edge and is a little less sharp. The face in the window is // only a bust, and it used to become the crop: a band through the door. const standing = { ...person(0.002, 0.175, 0.215, 0.995, 0.94), sharpness: 0.37 }; const window = { ...person(0.42, 0.315, 0.605, 0.568, 0.76), sharpness: 1 }; const faces = [ { x0: 0.148, y0: 0.214, x1: 0.187, y1: 0.318 }, { x0: 0.43, y0: 0.317, x1: 0.483, y1: 0.422 }, ]; const c = compose({ width: 6000, height: 4000, people: [standing, window], balls: [] }, null, { context: "reportage", faces, minWidth: 0.5 })!; expect(c.rect.y0).toBeLessThanOrEqual(standing.y0 + 1e-6); expect(c.rect.x0).toBeLessThanOrEqual(standing.x0 + 1e-6); // Her box runs off the bottom, so the cut is mid-limb, not the sock and not a band through the door. const at = (c.rect.y1 - standing.y0) / (standing.y1 - standing.y0); expect(at).toBeGreaterThan(0.55); expect(at).toBeLessThanOrEqual(0.9); const widthIn = Math.max(0, Math.min(c.rect.x1, window.x1) - Math.max(c.rect.x0, window.x0)) / (window.x1 - window.x0); expect(widthIn < 0.12 || widthIn > 0.9).toBe(true); if (widthIn > 0.9) { expect(c.rect.y0).toBeLessThanOrEqual(window.y0); expect(c.rect.y1).toBeGreaterThan(window.y1); } }); it("keeps the closer woman whole when the woman behind her is sharper", () => { // PSR39046, the glass entrance. The foreground figure is the full-length. Leaving the frame keeps her whole, // instead of cutting a band on the woman behind. const front = { ...person(0.45, 0.23, 0.636, 0.933, 0.9), sharpness: 0.6 }; const behind = { ...person(0.382, 0.304, 0.473, 0.667, 0.9), sharpness: 1 }; const c = compose({ width: 6000, height: 4000, people: [front, behind], balls: [] }, null, { context: "reportage", minWidth: 0.5, faces: [ { x0: 0.5, y0: 0.25, x1: 0.56, y1: 0.34 }, { x0: 0.4, y0: 0.31, x1: 0.45, y1: 0.4 }, ], })!; expect(c.rect.y0).toBeLessThanOrEqual(front.y0); expect(c.rect.y1).toBeGreaterThan(front.y1 - 1e-6); expect(c.rect.x0).toBeLessThanOrEqual(front.x0); expect(c.rect.x1).toBeGreaterThanOrEqual(front.x1); }); it("leaves a wide shot of small figures as it was framed", () => { // People in the middle of a room, none of them large. The space around them is the photograph. const a = { ...person(0.368, 0.296, 0.463, 0.614), sharpness: 0.84 }; const b = { ...person(0.477, 0.308, 0.547, 0.627), sharpness: 1 }; const c0 = { ...person(0.535, 0.293, 0.615, 0.62), sharpness: 0.85 }; const c = compose({ width: 6000, height: 4000, people: [a, b, c0], balls: [] }, null, { context: "reportage", minWidth: 0.5 }); expect(c).toBeNull(); }); it("makes a three-quarter plan below the hands, and refuses a half that would cut them", () => { const p = person(0.4, 0.1, 0.55, 0.9); const d = { ...frame, people: [p], balls: [] }; expect(planCrop(d, "half")).toBeNull(); const three = planCrop(d, "threeQuarter")!; const at = (y: number) => (y - p.y0) / (p.y1 - p.y0); expect(three.y0).toBeLessThanOrEqual(p.y0); expect(three.x0).toBeLessThanOrEqual(p.x0); expect(three.x1).toBeGreaterThanOrEqual(p.x1); expect(at(three.y1)).toBeGreaterThan(0.58); expect(at(three.y1)).toBeLessThan(0.72); expect(three.y1).toBeLessThan(p.y1); }); it("refuses a sequence crop that would cut a lower neck or the hands of someone leaning", () => { const high = person(0.3, 0.12, 0.48, 0.92); const low = person(0.5, 0.28, 0.68, 0.96); const group = { ...frame, people: [high, low], balls: [] }; expect(planCrop(group, "half", null, { context: "reportage" })).toBeNull(); const leaning = person(0.2, 0.18, 0.72, 0.86); expect(planCrop({ ...frame, people: [leaning], balls: [] }, "threeQuarter", null, { context: "reportage" })).toBeNull(); expect(planCrop({ ...frame, people: [leaning], balls: [] }, "half", null, { context: "reportage" })).toBeNull(); }); }); describe("hands, feet, the ball and an open plan", () => { it("keeps the hands that sit outside a narrow body", () => { const p = person(0.42, 0.12, 0.58, 0.78); const c = compose({ ...frame, people: [p], balls: [] }, null, { framing: "tight", context: "sport" })!; const pad = 0.12 * (p.y1 - p.y0); expect(c.rect.x0).toBeLessThanOrEqual(p.x0 - pad + 1e-6); expect(c.rect.x1).toBeGreaterThanOrEqual(p.x1 + pad - 1e-6); expect(c.rect.y1).toBeGreaterThan(p.y1); }); it("does not cut a whole foot of a person the crop keeps", () => { const a = person(0.3, 0.18, 0.44, 0.8); const b = person(0.46, 0.2, 0.6, 0.78); const c = compose({ ...frame, people: [a, b], balls: [] }, null, { framing: "tight", context: "sport" })!; for (const p of [a, b]) { const widthIn = Math.max(0, Math.min(c.rect.x1, p.x1) - Math.max(c.rect.x0, p.x0)) / (p.x1 - p.x0); if (widthIn > 0.85) expect(c.rect.y1).toBeGreaterThan(p.y1 - 1e-6); } }); it("never leaves a ball sliced by the crop", () => { const p = person(0.3, 0.15, 0.48, 0.85); const ball = { x0: 0.62, y0: 0.4, x1: 0.68, y1: 0.48, score: 0.9 }; const c = compose({ ...frame, people: [p], balls: [ball] }, null, { framing: "tight", context: "sport", sport: "andebol" }); if (c) expect(clipsBall(c.rect, [ball])).toBe(false); }); it("does not send a ball the camera already cut, and does not mind a whole one", () => { const cut = { x0: 0, y0: 0.4, x1: 0.04, y1: 0.48, score: 0.9 }; const whole = { x0: 0.4, y0: 0.4, x1: 0.46, y1: 0.48, score: 0.9 }; expect(sendBlockedByBall(null, [cut], cut)).toBe(true); expect(sendBlockedByBall({ x0: 0.2, y0: 0.2, x1: 0.8, y1: 0.8 }, [whole], whole)).toBe(false); expect(clipsBall({ x0: 0.42, y0: 0.2, x1: 0.8, y1: 0.8 }, [whole])).toBe(true); expect(sendBlockedByBall({ x0: 0.42, y0: 0.2, x1: 0.8, y1: 0.8 }, [whole], whole)).toBe(true); }); it("keeps every player of a team lined up in an open frame", () => { const team = [0.04, 0.18, 0.32, 0.46, 0.6, 0.74].map((x) => person(x, 0.22, x + 0.12, 0.55)); const c = compose({ width: 6000, height: 4000, people: team, balls: [] }, null, { framing: "tight", context: "sport" })!; expect(c.reason).toBe("group"); expect(c.people).toBe(6); for (const p of team) expect(inside(c.rect, p)).toBe(true); expect(planCrop({ width: 6000, height: 4000, people: team, balls: [] }, "half", null, { context: "sport" })).toBeNull(); }); it("keeps a wide frame of many people instead of cutting down to one", () => { const crowd = [ person(0.05, 0.15, 0.16, 0.42), person(0.2, 0.22, 0.31, 0.48), person(0.34, 0.12, 0.45, 0.4), person(0.5, 0.2, 0.61, 0.5), person(0.66, 0.18, 0.77, 0.46), person(0.8, 0.14, 0.91, 0.44), ]; const c = compose({ width: 6000, height: 4000, people: crowd, balls: [] }, null, { framing: "tight", context: "sport" })!; expect(c.people).toBe(6); for (const p of crowd) expect(inside(c.rect, p)).toBe(true); }); }); describe("clean framing, lead room, wide zoom, peak contact", () => { it("does not expand the subject with a soft body near the ball", () => { const sharp = { ...person(0.4, 0.2, 0.52, 0.85), sharpness: 1 }; const soft = { ...person(0.2, 0.05, 0.45, 0.95), sharpness: 0.2 }; const ball = { x0: 0.45, y0: 0.72, x1: 0.5, y1: 0.78, score: 0.9 }; const s = findSubject({ ...frame, people: [sharp, soft], balls: [ball] }, null, { sport: "futebol" })!; expect(s.reason).toBe("ball"); expect(s.people).toEqual([sharp]); expect(s.box.x0).toBeGreaterThan(soft.x0 + 0.05); }); it("leaves a tiny soft scrap out of a sport crop when a cleaner frame fits", () => { const main = { ...person(0.4, 0.15, 0.58, 0.9), sharpness: 1 }; const scrap = { ...person(0.05, 0.7, 0.12, 0.85), sharpness: 0.2 }; const c = compose({ ...frame, people: [main, scrap], balls: [] }, null, { framing: "full", context: "sport" })!; const share = Math.max(0, Math.min(c.rect.x1, scrap.x1) - Math.max(c.rect.x0, scrap.x0)) * Math.max(0, Math.min(c.rect.y1, scrap.y1) - Math.max(c.rect.y0, scrap.y0)); expect(share / ((scrap.x1 - scrap.x0) * (scrap.y1 - scrap.y0))).toBeLessThan(0.05); }); it("puts lead room on the facing side of the subject", () => { const body = { ...person(0.35, 0.15, 0.55, 0.9), sharpness: 1 }; const face = { x0: 0.48, y0: 0.16, x1: 0.54, y1: 0.28 }; expect(facingSide(body, [face], null)).toBe(1); const c = compose({ ...frame, people: [body], balls: [] }, null, { framing: "full", context: "sport", faces: [face] })!; const at = ((body.x0 + body.x1) / 2 - c.rect.x0) / (c.rect.x1 - c.rect.x0); expect(at).toBeLessThan(0.48); }); it("treats a zoom at its wide end as intentional framing", () => { expect(isWideZoomOpen({ focalLength: 15, range: [15, 35] })).toBe(true); expect(isWideZoomOpen({ focalLength: 35, range: [15, 35] })).toBe(false); expect(isWideZoomOpen({ focalLength: 135, range: [70, 200] })).toBe(false); const main = { ...person(0.4, 0.15, 0.58, 0.9), sharpness: 1 }; expect(compose({ ...frame, people: [main], balls: [] }, null, { lens: { focalLength: 15, range: [15, 35] } })).toBeNull(); }); it("scores peak ball contact higher than a distant ball", () => { const body = { ...person(0.4, 0.2, 0.55, 0.85), sharpness: 1, focus: 400 }; const near = { x0: 0.48, y0: 0.7, x1: 0.52, y1: 0.76, score: 0.9 }; const far = { x0: 0.75, y0: 0.3, x1: 0.79, y1: 0.36, score: 0.9 }; const dNear = { ...frame, people: [body], balls: [near] }; const dFar = { ...frame, people: [body], balls: [far] }; const cNear = compose(dNear, null, { sport: "futebol" })!; const cFar = { ...cNear, ball: far, reason: "ball" as const }; expect(frameScore(dNear, cNear)).toBeGreaterThan(frameScore(dFar, cFar)); }); it("refuses a crop that only works at the delivery floor", () => { // Preview-sized file: minWidth is 1 — any crop sits on the floor, so leave as shot. const main = { ...person(0.4, 0.15, 0.6, 0.9), sharpness: 1 }; expect(compose({ width: 2048, height: 1365, people: [main], balls: [] }, null, { minWidth: 1, framing: "full" })).toBeNull(); // High floor with a subject that would need a fill near that floor. const far = { ...person(0.46, 0.35, 0.54, 0.55), sharpness: 1 }; const ball = { x0: 0.49, y0: 0.5, x1: 0.52, y1: 0.54, score: 0.9 }; const tight = compose({ ...frame, people: [far], balls: [ball] }, null, { minWidth: 0.68, framing: "tight", sport: "futebol" }); if (tight) expect(tight.rect.x1 - tight.rect.x0).toBeGreaterThan(0.68 * 1.08); }); it("scores a lightly cropped frame above a heavy reframe of the same subject", () => { const body = { ...person(0.4, 0.15, 0.6, 0.9), sharpness: 1, focus: 400 }; const d = { ...frame, people: [body], balls: [] }; const wide = { rect: { x0: 0.1, y0: 0.05, x1: 0.9, y1: 0.95 }, subject: body, reason: "person" as const, people: 1 }; const tight = { rect: { x0: 0.35, y0: 0.2, x1: 0.65, y1: 0.7 }, subject: body, reason: "person" as const, people: 1 }; expect(frameScore(d, wide)).toBeGreaterThan(frameScore(d, tight)); }); });