opencode-config/.opencode/draw-image/tests/fit.test.ts
Sergey 56b67d2d5c
feat(draw-image): SVG template renderer for on-brand covers (#134)
* feat(draw-image): add SVG template render engine

* feat(draw-image): add opencode plugin and tool tests

* test(draw-image): add vitest unit integration e2e suite

* chore(draw-image): wire CI dependabot docs and ADR

* docs(handoff): set PR number

* docs(handoff): fix PR number placeholder in frontmatter

* fix(ci): use tempfile and trailing newline in draw-image tests

---------

Co-authored-by: opencode-agent <agent@opencode.local>
2026-07-29 23:42:50 +03:00

64 lines
No EOL
2 KiB
TypeScript

import { describe, test, expect } from "vitest"
import { computeFit, parseViewBox } from "../src/fit"
describe("fit — contain", () => {
test("square content into wider slot centers horizontally", () => {
const r = computeFit("contain", 100, 100, 400, 200, 100, 100)
expect(r.width).toBe(200)
expect(r.height).toBe(200)
expect(r.x).toBe(200)
expect(r.y).toBe(100)
expect(r.viewBox).toBe("0 0 100 100")
})
test("square content into taller slot centers vertically", () => {
const r = computeFit("contain", 100, 100, 200, 400, 100, 100)
expect(r.width).toBe(200)
expect(r.height).toBe(200)
expect(r.x).toBe(100)
expect(r.y).toBe(200)
})
test("horizontal content into square slot fits by width", () => {
const r = computeFit("contain", 0, 0, 200, 200, 400, 100)
expect(r.width).toBe(200)
expect(r.height).toBe(50)
expect(r.x).toBe(0)
expect(r.y).toBe(75)
})
})
describe("fit — cover", () => {
test("square content into wide slot fills height", () => {
const r = computeFit("cover", 0, 0, 400, 200, 100, 100)
expect(r.width).toBe(400)
expect(r.height).toBe(400)
expect(r.x).toBe(0)
expect(r.y).toBe(-100)
})
})
describe("fit — stretch", () => {
test("stretches to slot dimensions ignoring aspect", () => {
const r = computeFit("stretch", 10, 20, 300, 150, 100, 100)
expect(r.x).toBe(10)
expect(r.y).toBe(20)
expect(r.width).toBe(300)
expect(r.height).toBe(150)
expect(r.viewBox).toBe("0 0 100 100")
})
})
describe("fit — parseViewBox", () => {
test("extracts from viewBox attribute", () => {
expect(parseViewBox('<svg viewBox="0 0 24 24">')).toEqual({ width: 24, height: 24 })
})
test("extracts from width/height attributes", () => {
expect(parseViewBox('<svg width="48" height="48">')).toEqual({ width: 48, height: 48 })
})
test("defaults to 24x24 when nothing found", () => {
expect(parseViewBox("<svg></svg>")).toEqual({ width: 24, height: 24 })
})
})