Files
williamandClaude Sonnet 5 1c2f002eb3 refactor: move app code into Max Avatar/ subfolder (monorepo prep for backend)
Step 1 of adding a backend proxy server (API keys held server-side instead
of shipped in the installer). Moves everything app-specific into
"Max Avatar/" via git mv (preserves history): src/, tests/, scripts/,
assets/, build/, resources/, package.json/-lock.json, electron-builder.yml,
electron.vite.config.ts, eslint.config.mjs, playwright.config.ts,
vitest.config.mts, sonar-project.properties, tsconfig*.json, Dockerfile,
.editorconfig/.env*/.gitignore/.prettier*.

CLAUDE.md, README.md, docs/, .claude/, .gitea/, .husky/, .vscode/, and the
docker-compose files stay at the repo root, since they'll apply to both the
app and the future backend.

No application code needed changes (all its internal paths are relative to
its own tree). Adjusted what referenced the old root from outside:
- .husky/pre-commit: cd into "Max Avatar" before lint-staged
- .gitea/workflows/*.yml: working-directory + cache-dependency-path +
  artifact paths + SonarQube projectBaseDir
- docker-compose.yml: build context -> ./Max Avatar
- Max Avatar/Dockerfile: node:20-alpine -> node:24-alpine (same node:sqlite
  issue already fixed elsewhere, never applied here before)
- .vscode/launch.json, README.md: updated paths/instructions

Verified from Max Avatar/ after the move: npm ci, typecheck, lint,
test:coverage (473/473, same coverage numbers), build, and dev all work
unchanged.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-31 12:15:30 +01:00

175 lines
5.4 KiB
TypeScript

import { describe, it, expect, vi } from 'vitest'
import {
boxesOverlap,
findFreeLabelPosition,
drawArrowHead,
drawShape,
type LabelBox
} from '../../../src/renderer/src/components/drawRoughShapes'
import type { RoughCanvas } from 'roughjs/bin/canvas'
function fakeCtx(): {
ctx: CanvasRenderingContext2D
fillText: ReturnType<typeof vi.fn>
beginPath: ReturnType<typeof vi.fn>
fill: ReturnType<typeof vi.fn>
} {
const fillText = vi.fn()
const beginPath = vi.fn()
const fill = vi.fn()
const ctx = {
measureText: vi.fn(() => ({ width: 40 })),
save: vi.fn(),
restore: vi.fn(),
beginPath,
moveTo: vi.fn(),
lineTo: vi.fn(),
closePath: vi.fn(),
fill,
fillText,
fillStyle: '',
font: ''
} as unknown as CanvasRenderingContext2D
return { ctx, fillText, beginPath, fill }
}
function fakeRoughCanvas(): {
rc: RoughCanvas
rectangle: ReturnType<typeof vi.fn>
circle: ReturnType<typeof vi.fn>
line: ReturnType<typeof vi.fn>
curve: ReturnType<typeof vi.fn>
arc: ReturnType<typeof vi.fn>
} {
const rectangle = vi.fn()
const circle = vi.fn()
const line = vi.fn()
const curve = vi.fn()
const arc = vi.fn()
const rc = { rectangle, circle, line, curve, arc } as unknown as RoughCanvas
return { rc, rectangle, circle, line, curve, arc }
}
describe('boxesOverlap()', () => {
it('should detect overlapping boxes', () => {
const a: LabelBox = { x: 0, y: 0, width: 10, height: 10 }
const b: LabelBox = { x: 5, y: 5, width: 10, height: 10 }
expect(boxesOverlap(a, b)).toBe(true)
})
it('should detect non-overlapping boxes', () => {
const a: LabelBox = { x: 0, y: 0, width: 10, height: 10 }
const b: LabelBox = { x: 100, y: 100, width: 10, height: 10 }
expect(boxesOverlap(a, b)).toBe(false)
})
})
describe('findFreeLabelPosition()', () => {
it('should return the starting position when nothing is placed yet', () => {
const { ctx } = fakeCtx()
const box = findFreeLabelPosition(ctx, 'Label', 10, 30, [])
expect(box).toEqual({ x: 10, y: 14, width: 40, height: 16 })
})
it('should shift downward to avoid an already-placed overlapping box', () => {
const { ctx } = fakeCtx()
const placed: LabelBox[] = [{ x: 10, y: 14, width: 40, height: 16 }]
const box = findFreeLabelPosition(ctx, 'Label', 10, 30, placed)
expect(box.y).toBeGreaterThan(14)
})
it('should fall back to the last attempted position after 20 attempts if always overlapping', () => {
const { ctx } = fakeCtx()
const placed: LabelBox[] = Array.from({ length: 25 }, (_, i) => ({
x: 10,
y: 14 + i * 16,
width: 40,
height: 16
}))
const box = findFreeLabelPosition(ctx, 'Label', 10, 30, placed)
expect(box).toBeDefined()
})
})
describe('drawArrowHead()', () => {
it('should draw a filled triangle at the arrow tip', () => {
const { ctx, beginPath, fill } = fakeCtx()
drawArrowHead(ctx, 0, 0, 10, 10, '#000')
expect(beginPath).toHaveBeenCalled()
expect(fill).toHaveBeenCalled()
})
})
describe('drawShape()', () => {
it('should draw a rectangle', () => {
const { rc, rectangle } = fakeRoughCanvas()
const { ctx } = fakeCtx()
drawShape(rc, ctx, { type: 'rect', x: 1, y: 2, width: 10, height: 20 }, [])
expect(rectangle).toHaveBeenCalledWith(1, 2, 10, 20, expect.anything())
})
it('should draw a circle', () => {
const { rc, circle } = fakeRoughCanvas()
const { ctx } = fakeCtx()
drawShape(rc, ctx, { type: 'circle', x: 1, y: 2, radius: 5 }, [])
expect(circle).toHaveBeenCalledWith(1, 2, 10, expect.anything())
})
it('should draw a line and position its label at the segment midpoint', () => {
const { rc, line } = fakeRoughCanvas()
const { ctx, fillText } = fakeCtx()
drawShape(rc, ctx, { type: 'line', x: 0, y: 0, x2: 10, y2: 0, label: 'AB' }, [])
expect(line).toHaveBeenCalledWith(0, 0, 10, 0, expect.anything())
expect(fillText).toHaveBeenCalled()
})
it('should draw an arrow with an arrowhead', () => {
const { rc, line } = fakeRoughCanvas()
const { ctx, fill } = fakeCtx()
drawShape(rc, ctx, { type: 'arrow', x: 0, y: 0, x2: 10, y2: 10 }, [])
expect(line).toHaveBeenCalled()
expect(fill).toHaveBeenCalled()
})
it('should draw a stroke curve only when at least 2 points are given', () => {
const { rc, curve } = fakeRoughCanvas()
const { ctx } = fakeCtx()
drawShape(
rc,
ctx,
{
type: 'stroke',
points: [
{ x: 0, y: 0 },
{ x: 1, y: 1 }
]
},
[]
)
expect(curve).toHaveBeenCalled()
})
it('should not draw a stroke with fewer than 2 points', () => {
const { rc, curve } = fakeRoughCanvas()
const { ctx } = fakeCtx()
drawShape(rc, ctx, { type: 'stroke', points: [{ x: 0, y: 0 }] }, [])
expect(curve).not.toHaveBeenCalled()
})
it('should draw an arc', () => {
const { rc, arc } = fakeRoughCanvas()
const { ctx } = fakeCtx()
drawShape(rc, ctx, { type: 'arc', x: 0, y: 0, radius: 10, startAngle: 0, endAngle: 90 }, [])
expect(arc).toHaveBeenCalled()
})
it('should draw text content directly and record its label box', () => {
const { rc } = fakeRoughCanvas()
const { ctx, fillText } = fakeCtx()
const placed: LabelBox[] = []
drawShape(rc, ctx, { type: 'text', x: 0, y: 0, content: 'Bonjour' }, placed)
expect(fillText).toHaveBeenCalledWith('Bonjour', 0, 0)
expect(placed.length).toBe(1)
})
})