Skip to content

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

2.4 KB
Raw Blame
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import {
  acquireWorkspaceInteractionLock,
  getActiveWorkspaceInteractionReasons,
  isWorkspaceInteractionActive,
  releaseAllWorkspaceInteractionLocks,
} from '../workspacePointerGuard';

describe('Workspace Pointer Guard (zero-rerender UI drag isolation)', () => {
  beforeEach(() => {
    releaseAllWorkspaceInteractionLocks();
  });

  afterEach(() => {
    releaseAllWorkspaceInteractionLocks();
  });

  it('tracks overlapping UI interaction locks, toggles data-workspace-interacting, and releases idempotently', () => {
    const attrs = new Map<string, string>();
    const prevDoc = (globalThis as Record<string, unknown>).document;
    (globalThis as Record<string, unknown>).document = {
      documentElement: {
        setAttribute: (k: string, v: string) => attrs.set(k, v),
        removeAttribute: (k: string) => attrs.delete(k),
      },
      addEventListener: () => {},
    };

    try {
      expect(isWorkspaceInteractionActive()).toBe(false);

      const releaseTab = acquireWorkspaceInteractionLock('pointer-tab-drag');
      expect(isWorkspaceInteractionActive()).toBe(true);
      expect(attrs.get('data-workspace-interacting')).toBe('true');
      expect(getActiveWorkspaceInteractionReasons()).toEqual(['pointer-tab-drag']);

      const releaseResize = acquireWorkspaceInteractionLock('dock-col-resize');
      expect(getActiveWorkspaceInteractionReasons()).toEqual([
        'pointer-tab-drag',
        'dock-col-resize',
      ]);

      // Releasing one lock keeps guard active; double-releasing is a safe no-op
      releaseTab();
      releaseTab();
      expect(isWorkspaceInteractionActive()).toBe(true);
      expect(attrs.get('data-workspace-interacting')).toBe('true');

      releaseResize();
      expect(isWorkspaceInteractionActive()).toBe(false);
      expect(attrs.has('data-workspace-interacting')).toBe(false);
    } finally {
      (globalThis as Record<string, unknown>).document = prevDoc;
    }
  });

  it('releases all locks via releaseAllWorkspaceInteractionLocks so canvas can never stay stuck', () => {
    acquireWorkspaceInteractionLock('floating-panel-drag');
    acquireWorkspaceInteractionLock('chrome-scrollbar-drag');
    expect(isWorkspaceInteractionActive()).toBe(true);

    releaseAllWorkspaceInteractionLocks();
    expect(isWorkspaceInteractionActive()).toBe(false);
    expect(getActiveWorkspaceInteractionReasons()).toEqual([]);
  });
});