Skip to content

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

3.2 KB
Raw Blame
import { describe, expect, it } from 'vitest';
import {
  classifyNativeTool,
  fromNativeLayerNodeMeta,
  getNativeMemoryStats,
  isNativeCanvasActive,
  isNativeStrokeTool,
  nativeGetHistory,
  parseCssColorToRgba,
  toNativeLayerNodeMeta,
  vramBudgetMbFromPreset,
} from '../nativeEngineBridge';
import type { LayerItem } from '../../types/editor';

describe('nativeEngineBridge', () => {
  it('stays inactive in non-Tauri browser/jsdom environments', () => {
    expect(isNativeCanvasActive()).toBe(false);
  });

  it('classifies brush and eraser as native stroke tools', () => {
    expect(isNativeStrokeTool('brush')).toBe(true);
    expect(isNativeStrokeTool('eraser')).toBe(true);
    expect(isNativeStrokeTool('fill')).toBe(false);
    expect(isNativeStrokeTool('pen')).toBe(false);

    expect(classifyNativeTool('brush')).toBe('brush');
    expect(classifyNativeTool('eraser')).toBe('eraser');
    expect(classifyNativeTool('liquify')).toBe('liquify');
    expect(classifyNativeTool('frame')).toBe('other');
  });

  it('parses hex and rgb/rgba CSS color strings to normalized RGBA floats', () => {
    const hex6 = parseCssColorToRgba('#161616');
    expect(hex6[0]).toBeCloseTo(22 / 255, 4);
    expect(hex6[1]).toBeCloseTo(22 / 255, 4);
    expect(hex6[2]).toBeCloseTo(22 / 255, 4);
    expect(hex6[3]).toBe(1);

    const hex3 = parseCssColorToRgba('#f0a');
    expect(hex3[0]).toBeCloseTo(1, 4);
    expect(hex3[1]).toBeCloseTo(0, 4);
    expect(hex3[2]).toBeCloseTo(170 / 255, 4);
    expect(hex3[3]).toBe(1);

    const rgba = parseCssColorToRgba('rgba(51, 102, 204, 0.5)');
    expect(rgba[0]).toBeCloseTo(0.2, 4);
    expect(rgba[1]).toBeCloseTo(0.4, 4);
    expect(rgba[2]).toBeCloseTo(0.8, 4);
    expect(rgba[3]).toBeCloseTo(0.5, 4);

    const fallback = parseCssColorToRgba('not-a-color', [0.1, 0.2, 0.3, 1.0]);
    expect(fallback).toEqual([0.1, 0.2, 0.3, 1.0]);
  });

  it('converts compact Phase 2 layer metadata cleanly without DOM canvases and no-ops safely off-desktop', async () => {
    const fakeCanvas = {} as HTMLCanvasElement;
    const layer: LayerItem = {
      id: 'layer-1-doc1',
      name: 'Ink',
      type: 'raster',
      visible: true,
      opacity: 85,
      blendMode: 'multiply',
      locked: false,
      alphaLocked: true,
      clipped: false,
      parentId: null,
      maskCanvas: fakeCanvas,
      maskEnabled: true,
      maskLinked: true,
      expressionColor: 'monochrome',
      monoThreshold: 140,
      thumbnailUrl: 'data:image/png;base64,AA==',
    };

    const nativeMeta = toNativeLayerNodeMeta(layer);
    expect('maskCanvas' in nativeMeta).toBe(false);
    expect(nativeMeta.hasMask).toBe(true);
    expect(nativeMeta.blendMode).toBe('multiply');
    expect(nativeMeta.monoThreshold).toBe(140);

    const hydrated = fromNativeLayerNodeMeta(nativeMeta);
    expect(hydrated.id).toBe('layer-1-doc1');
    expect(hydrated.thumbnailUrl).toBe('data:image/png;base64,AA==');

    expect(vramBudgetMbFromPreset('low')).toBe(128);
    expect(vramBudgetMbFromPreset('medium')).toBe(256);
    expect(vramBudgetMbFromPreset('high')).toBe(512);
    expect(vramBudgetMbFromPreset('ultra')).toBe(1024);

    expect(await nativeGetHistory('doc1')).toBeNull();
    expect(await getNativeMemoryStats()).toBeNull();
  });
});