const assert = require('assert');
const fs = require('fs');
const path = require('path');
const {
  CLIPBOARD_COPY_SHORTCUT,
  CLIPBOARD_PASTE_SHORTCUT,
  clipboardShortcutForCode,
  isClipboardTransferShortcut,
} = require('../shortcut');

const root = path.resolve(__dirname, '..');
const appSource = fs.readFileSync(path.join(root, 'app.js'), 'utf8');

assert.strictEqual(
  clipboardShortcutForCode(67, ['command', 'alt']),
  CLIPBOARD_COPY_SHORTCUT
);
assert.strictEqual(
  clipboardShortcutForCode(86, ['alt', 'command']),
  CLIPBOARD_PASTE_SHORTCUT
);
assert.strictEqual(clipboardShortcutForCode(67, ['command']), null);
assert.strictEqual(clipboardShortcutForCode(86, ['alt']), null);
assert.strictEqual(
  clipboardShortcutForCode(67, ['command', 'shift', 'alt']),
  CLIPBOARD_COPY_SHORTCUT
);
assert.strictEqual(isClipboardTransferShortcut(CLIPBOARD_COPY_SHORTCUT), true);
assert.strictEqual(isClipboardTransferShortcut(CLIPBOARD_PASTE_SHORTCUT), true);
assert.strictEqual(isClipboardTransferShortcut('command-tab'), false);

assert.match(
  appSource,
  /syncClipboardToPeer\(activePeerId, 'shortcut-paste', true\);\s*}\s*sendShortcutToPeer/,
  'paste must send clipboard text before the remote paste shortcut'
);
assert.match(
  appSource,
  /performRemoteKeyChord\(senderId, \['command'\], 'c'\);\s*if \(senderId\) \{\s*setTimeout\(\(\) => \{\s*syncClipboardToPeer\(senderId, 'shortcut-copy', true\);/,
  'copy must run remotely before returning clipboard text'
);

console.log('clipboard shortcut tests passed');
