Skip to content

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

5.0 KB
Raw Blame
// @ts-expect-error node built-in available in vitest runtime without @types/node
import fs from 'node:fs';
// @ts-expect-error node built-in available in vitest runtime without @types/node
import path from 'node:path';
import { describe, expect, it } from 'vitest';

const REPO_ROOT = path.resolve(
  (globalThis as unknown as { process?: { cwd?: () => string } }).process?.cwd?.() ?? '.'
);

function resolveRepo(...parts: string[]): string {
  return path.join(REPO_ROOT, ...parts);
}

function stripJsComments(code: string): string {
  return code
    .replace(/\/\*[\s\S]*?\*\//g, '')
    .replace(/^\s*\/\/.*$/gm, '');
}

describe('Phase 6 WebView2 Purge, Zero Full-Canvas Allocation & Engine Decoupling Audit', () => {
  it('deletes all replaced legacy JS raster engines and canvas backing stores', () => {
    const purgedLegacyPaths = [
      'src/utils/tileEngine',
      'src/utils/tileEngine.ts',
      'src/utils/liquifyEngine.ts',
      'src/utils/floodFill.ts',
      'src/utils/smartLineFill.ts',
      'src/utils/transformEngine.ts',
      'src/utils/transformResampler.ts',
      'src/utils/puppetWarpEngine.ts',
      'src/utils/cageWarpEngine.ts',
      'src/utils/retouchEngine.ts',
      'src/utils/gradientEngine.ts',
      'src/utils/filterEngine.ts',
      'src/utils/tonalEngine.ts',
      'src/engine/RemoteStrokeRenderer.ts',
      'src/engine/RemoteDraftCanvasManager.ts',
      'src/utils/undoCache.ts',
      'src/utils/transformModeTransition.ts',
      'src/utils/previewRenderer.ts',
      'src/utils/layerEffectsEngine.ts',
      'src/utils/saveManager/cpuComposite.ts',
      'src/utils/saveManager/gpuComposite.ts',
      'src/utils/saveManager/exportGridPrep.ts',
      'src/utils/saveManager/inkParse.ts',
      'src/utils/saveManager/inkSerialize.ts',
      'src/utils/saveManager/inkCodec.ts',
    ];

    for (const rel of purgedLegacyPaths) {
      expect(fs.existsSync(resolveRepo(rel)), `Legacy file still exists: ${rel}`).toBe(false);
    }
  });

  it('removes all dead pixel-IPC Tauri commands from src-tauri/src/lib.rs', () => {
    const libRs = fs.readFileSync(resolveRepo('src-tauri/src/lib.rs'), 'utf8');
    const deadCommands = [
      'native_apply_filter',
      'native_flood_fill',
      'native_liquify_step',
      'native_compress_tiles_lz4',
      'native_decompress_tiles_lz4',
      'save_undo_tile_cache',
      'load_undo_tile_cache',
      'scratch_disk_init',
      'scratch_disk_write_tiles',
      'scratch_disk_read_tile',
      'scratch_disk_clear',
      'parse_clip_file',
      'parse_psd_file',
    ];

    for (const cmd of deadCommands) {
      expect(
        new RegExp(`\\b${cmd}\\b`).test(libRs),
        `Dead pixel-IPC command still registered: ${cmd}`
      ).toBe(false);
    }
  });

  it('enforces strict decoupling: crates/ink-engine has zero Tauri / WebView2 / Wry / Tao dependencies', () => {
    const cargoToml = fs.readFileSync(resolveRepo('crates/ink-engine/Cargo.toml'), 'utf8');
    const forbidden = ['tauri', 'wry', 'tao', 'webview2-com'];
    for (const dep of forbidden) {
      expect(
        new RegExp(`^\\s*${dep}\\s*=`, 'm').test(cargoToml),
        `Forbidden dependency found in crates/ink-engine/Cargo.toml: ${dep}`
      ).toBe(false);
    }
  });

  it('allocates zero HTMLCanvasElement backing stores across core open, draw, undo, zoom, transform, liquify, collab, and save paths', () => {
    const zeroCanvasModules = [
      'src/components/CanvasArea.tsx',
      'src/components/DocumentLifecycleManager.tsx',
      'src/components/TransformOverlay.tsx',
      'src/components/WebtoonExportModal.tsx',
      'src/hooks/useCanvasNavigation.ts',
      'src/hooks/useNativeEngineViewport.ts',
      'src/hooks/useCanvasDrawing.ts',
      'src/hooks/useLayerManager.ts',
      'src/hooks/useCollabSync.ts',
      'src/hooks/useMultiplayer.ts',
      'src/hooks/drawing/useSelectionTools.ts',
      'src/hooks/drawing/useShapeAndFillTools.ts',
      'src/hooks/drawing/useGradientTool.ts',
      'src/hooks/drawing/useRetouchTool.ts',
      'src/hooks/drawing/useVectorDrawing.ts',
      'src/hooks/drawing/usePenTool.ts',
      'src/utils/documentStore.ts',
      'src/utils/selectionUtils.ts',
      'src/utils/saveManager/fileActions.ts',
      'src/utils/saveManager/psdExport.ts',
      'src/utils/imageLoader.ts',
      'src/utils/cspParser.ts',
      'src/utils/psdParser.ts',
      'src/utils/legacySoraMigrator.ts',
      'src/utils/webtoonExporter.ts',
      'src/services/recoveryService.ts',
      'src/services/multiplayerService.ts',
    ];

    const canvasAllocPattern =
      /createElement\s*\(\s*['"]canvas['"]\s*\)|new\s+OffscreenCanvas\s*\(|<\s*canvas\b/i;

    for (const rel of zeroCanvasModules) {
      const fullPath = resolveRepo(rel);
      expect(fs.existsSync(fullPath), `Expected module missing: ${rel}`).toBe(true);
      const src = stripJsComments(fs.readFileSync(fullPath, 'utf8'));
      expect(
        canvasAllocPattern.test(src),
        `Unexpected canvas allocation found in ${rel}`
      ).toBe(false);
    }
  });
});