Skip to content

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

7.6 KB
Raw Blame
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import {
  _resetReloadGuardForTests,
  installReloadGuard,
  isBrowserReloadOrNavShortcut,
  markIntentionalReload,
  shouldBlockDefaultContextMenu,
  shouldConfirmBeforeUnload,
} from '../reloadGuard';
import { shortcutManager } from '../shortcutManager';

function makeKeyEvent(
  overrides: Partial<{
    key: string;
    code: string;
    ctrlKey: boolean;
    metaKey: boolean;
    shiftKey: boolean;
    altKey: boolean;
    repeat: boolean;
  }> = {}
): KeyboardEvent {
  const ev = new Event('keydown', { bubbles: true, cancelable: true }) as KeyboardEvent;
  Object.assign(ev, {
    key: '',
    code: '',
    ctrlKey: false,
    metaKey: false,
    shiftKey: false,
    altKey: false,
    repeat: false,
    ...overrides,
  });
  return ev;
}

describe('Accidental Reload & Page Navigation Protection', () => {
  beforeEach(() => {
    _resetReloadGuardForTests();
  });

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

  it('detects reload and browser history navigation shortcuts across layouts and platforms', () => {
    // Ctrl+R / Cmd+R / Ctrl+Shift+R / Cyrillic Ctrl+К (physical KeyR)
    expect(isBrowserReloadOrNavShortcut({ key: 'r', code: 'KeyR', ctrlKey: true }, false)).toBe(true);
    expect(isBrowserReloadOrNavShortcut({ key: 'R', code: 'KeyR', ctrlKey: true, shiftKey: true }, false)).toBe(true);
    expect(isBrowserReloadOrNavShortcut({ key: 'r', code: 'KeyR', metaKey: true }, true)).toBe(true);
    expect(isBrowserReloadOrNavShortcut({ key: 'к', code: 'KeyR', ctrlKey: true }, false)).toBe(true);

    // F5 variants
    expect(isBrowserReloadOrNavShortcut({ key: 'F5', code: 'F5' }, false)).toBe(true);
    expect(isBrowserReloadOrNavShortcut({ key: 'F5', code: 'F5', ctrlKey: true }, false)).toBe(true);
    expect(isBrowserReloadOrNavShortcut({ key: 'F5', code: 'F5', shiftKey: true }, false)).toBe(true);

    // Hardware browser keys
    expect(isBrowserReloadOrNavShortcut({ key: 'BrowserRefresh', code: 'BrowserRefresh' }, false)).toBe(true);
    expect(isBrowserReloadOrNavShortcut({ key: 'BrowserBack', code: 'BrowserBack' }, false)).toBe(true);
    expect(isBrowserReloadOrNavShortcut({ key: 'BrowserForward', code: 'BrowserForward' }, false)).toBe(true);

    // Alt+Left / Alt+Right blocked on Windows/Linux, allowed on macOS (Option+Arrow word jump)
    expect(isBrowserReloadOrNavShortcut({ key: 'ArrowLeft', code: 'ArrowLeft', altKey: true }, false)).toBe(true);
    expect(isBrowserReloadOrNavShortcut({ key: 'ArrowRight', code: 'ArrowRight', altKey: true }, false)).toBe(true);
    expect(isBrowserReloadOrNavShortcut({ key: 'ArrowLeft', code: 'ArrowLeft', altKey: true }, true)).toBe(false);

    // Legitimate non-reload shortcuts are unaffected
    expect(isBrowserReloadOrNavShortcut({ key: 'r', code: 'KeyR' }, false)).toBe(false);
    expect(isBrowserReloadOrNavShortcut({ key: 'R', code: 'KeyR', shiftKey: true }, false)).toBe(false);
    expect(isBrowserReloadOrNavShortcut({ key: 'z', code: 'KeyZ', ctrlKey: true }, false)).toBe(false);
    expect(isBrowserReloadOrNavShortcut({ key: 's', code: 'KeyS', ctrlKey: true }, false)).toBe(false);
    expect(isBrowserReloadOrNavShortcut({ key: 'r', code: 'KeyR', ctrlKey: true, altKey: true }, false)).toBe(false);
  });

  it('prevents Ctrl+R / F5 reloads without blocking Ink Studio Ctrl+Shift+R or downstream listeners', () => {
    const fakeWin = new EventTarget() as unknown as Window;
    const onBlocked = vi.fn();
    const downstreamListener = vi.fn();

    const uninstall = installReloadGuard({
      target: fakeWin,
      getDocuments: () => [{ isDirty: true }],
      isFinalizingClose: () => false,
      shouldNotifyBlockedKey: (e) => !shortcutManager.matchEvent(e, {}),
      onBlockedReloadShortcut: onBlocked,
    });

    // Downstream app shortcut listener (bubble phase, like GlobalShortcutListener)
    fakeWin.addEventListener('keydown', downstreamListener);

    // 1. Bare Ctrl+R: defaultPrevented = true AND user gets toast notification
    const ctrlR = makeKeyEvent({ key: 'r', code: 'KeyR', ctrlKey: true });
    fakeWin.dispatchEvent(ctrlR);
    expect(ctrlR.defaultPrevented).toBe(true);
    expect(downstreamListener).toHaveBeenCalledTimes(1);
    expect(onBlocked).toHaveBeenCalledTimes(1);

    // 2. Ctrl+Shift+R (Perspective Ruler): browser hard-reload is prevented,
    //    downstream listener STILL runs, and no false "reload blocked" toast fires
    const ctrlShiftR = makeKeyEvent({ key: 'R', code: 'KeyR', ctrlKey: true, shiftKey: true });
    fakeWin.dispatchEvent(ctrlShiftR);
    expect(ctrlShiftR.defaultPrevented).toBe(true);
    expect(downstreamListener).toHaveBeenCalledTimes(2);
    expect(onBlocked).toHaveBeenCalledTimes(1);

    // 3. Normal Ctrl+Z: untouched
    const ctrlZ = makeKeyEvent({ key: 'z', code: 'KeyZ', ctrlKey: true });
    fakeWin.dispatchEvent(ctrlZ);
    expect(ctrlZ.defaultPrevented).toBe(false);
    expect(downstreamListener).toHaveBeenCalledTimes(3);

    uninstall();
    fakeWin.removeEventListener('keydown', downstreamListener);
  });

  it('blocks default context menu on canvas/chrome/sliders while preserving Copy/Paste in text inputs', () => {
    expect(shouldBlockDefaultContextMenu({ tagName: 'CANVAS' } as any, '')).toBe(true);
    expect(shouldBlockDefaultContextMenu({ tagName: 'DIV' } as any, '')).toBe(true);
    expect(shouldBlockDefaultContextMenu({ tagName: 'INPUT', type: 'range' } as any, '')).toBe(true);
    expect(shouldBlockDefaultContextMenu({ tagName: 'INPUT', type: 'checkbox' } as any, '')).toBe(true);

    expect(shouldBlockDefaultContextMenu({ tagName: 'INPUT', type: 'text' } as any, '')).toBe(false);
    expect(shouldBlockDefaultContextMenu({ tagName: 'TEXTAREA' } as any, '')).toBe(false);
    expect(shouldBlockDefaultContextMenu({ tagName: 'DIV', isContentEditable: true } as any, '')).toBe(false);
    expect(shouldBlockDefaultContextMenu({ tagName: 'DIV' } as any, 'selected text')).toBe(false);
  });

  it('guards beforeunload whenever unsaved artwork or an active stroke exists, unless closing/reloading intentionally', () => {
    const fakeWin = new EventTarget() as unknown as Window;
    let docs: Array<{ isDirty: boolean }> = [{ isDirty: false }];
    let finalizing = false;
    let drawingActive = false;

    const uninstall = installReloadGuard({
      target: fakeWin,
      getDocuments: () => docs,
      isFinalizingClose: () => finalizing,
      isDrawingActive: () => drawingActive,
    });

    const fireBeforeUnload = () => {
      const ev = new Event('beforeunload', { cancelable: true }) as BeforeUnloadEvent;
      Object.defineProperty(ev, 'returnValue', { writable: true, value: undefined });
      fakeWin.dispatchEvent(ev);
      return ev;
    };

    // Clean document, not drawing -> does not prompt
    expect(fireBeforeUnload().defaultPrevented).toBe(false);

    // Dirty document -> blocks unload
    docs = [{ isDirty: true }];
    const dirtyEv = fireBeforeUnload();
    expect(dirtyEv.defaultPrevented).toBe(true);
    expect(dirtyEv.returnValue).toBe('');

    // Clean document but mid-stroke -> blocks unload
    docs = [{ isDirty: false }];
    drawingActive = true;
    expect(fireBeforeUnload().defaultPrevented).toBe(true);

    // Finalizing intentional app close -> allows unload even with dirty docs
    docs = [{ isDirty: true }];
    finalizing = true;
    expect(fireBeforeUnload().defaultPrevented).toBe(false);

    // Intentional ErrorBoundary reload -> allows unload even with dirty docs
    finalizing = false;
    expect(shouldConfirmBeforeUnload(docs, false, false)).toBe(true);
    markIntentionalReload();
    expect(fireBeforeUnload().defaultPrevented).toBe(false);

    uninstall();
  });
});