Skip to content

A lightweight Clip Studio Paint alternative with real-time multiplayer collaborative drawing. Built with Tauri and React.

3.1 KB
Raw Blame
import { describe, it, expect } from 'vitest';
import {
  DEFAULT_SHAPE_SETTINGS,
  ShapeSettings,
  computeShapeGeometry,
  buildPolygonVertices,
  buildStarVertices,
  buildShapeSvgPaths,
  resolveShapeFillColor,
} from '../shapeEngine';

describe('shapeEngine', () => {
  it('computes unconstrained and Shift-constrained rectangle / square geometry', () => {
    const base: ShapeSettings = { ...DEFAULT_SHAPE_SETTINGS, shape: 'rectangle' };
    const rect = computeShapeGeometry({ x: 10, y: 20 }, { x: 110, y: 70 }, base, false, false);
    expect(rect.x).toBe(10);
    expect(rect.y).toBe(20);
    expect(rect.width).toBe(100);
    expect(rect.height).toBe(50);

    const square = computeShapeGeometry({ x: 10, y: 20 }, { x: 110, y: 70 }, base, true, false);
    expect(square.width).toBe(100);
    expect(square.height).toBe(100);
  });

  it('supports Alt (from-center) symmetry for closed shapes', () => {
    const base: ShapeSettings = { ...DEFAULT_SHAPE_SETTINGS, shape: 'ellipse' };
    const centered = computeShapeGeometry({ x: 100, y: 100 }, { x: 140, y: 120 }, base, false, true);
    expect(centered.x).toBe(60);
    expect(centered.y).toBe(80);
    expect(centered.width).toBe(80);
    expect(centered.height).toBe(40);
  });

  it('snaps line angles to 15-degree increments when Shift is held', () => {
    const lineSettings: ShapeSettings = { ...DEFAULT_SHAPE_SETTINGS, shape: 'line' };
    const snapped = computeShapeGeometry({ x: 0, y: 0 }, { x: 100, y: 90 }, lineSettings, true, false);
    expect(snapped.angleDeg).toBeCloseTo(45, 3);
    expect(snapped.x2).toBeCloseTo(snapped.y2, 3);
  });

  it('builds valid polygon and star vertices', () => {
    const hex = buildPolygonVertices(0, 0, 100, 100, 6);
    expect(hex).toHaveLength(6);
    expect(hex[0].x).toBeCloseTo(50, 4);
    expect(hex[0].y).toBeCloseTo(0, 4);

    const star = buildStarVertices(0, 0, 100, 100, 5, 0.5);
    expect(star).toHaveLength(10);
  });

  it('generates SVG preview paths for all supported geometric shapes', () => {
    const kinds = [
      'line',
      'arrow',
      'rectangle',
      'rounded-rect',
      'ellipse',
      'triangle',
      'polygon',
      'star',
    ] as const;

    for (const shape of kinds) {
      const res = buildShapeSvgPaths({
        start: { x: 10, y: 10 },
        end: { x: 110, y: 90 },
        shiftKey: false,
        altKey: false,
        settings: { ...DEFAULT_SHAPE_SETTINGS, shape },
      });
      expect(res.mainPath.length).toBeGreaterThan(5);
      if (shape === 'arrow') {
        expect(res.arrowHeadPath).toBeTruthy();
      }
    }
  });

  it('resolves fill color modes accurately', () => {
    expect(
      resolveShapeFillColor({ ...DEFAULT_SHAPE_SETTINGS, fillColorMode: 'primary' }, '#111111', '#eeeeee')
    ).toBe('#111111');
    expect(
      resolveShapeFillColor({ ...DEFAULT_SHAPE_SETTINGS, fillColorMode: 'secondary' }, '#111111', '#eeeeee')
    ).toBe('#eeeeee');
    expect(
      resolveShapeFillColor(
        { ...DEFAULT_SHAPE_SETTINGS, fillColorMode: 'custom', customFillColor: '#ff0055' },
        '#111111',
        '#eeeeee'
      )
    ).toBe('#ff0055');
  });
});