| 1 | const {test} = require('node:test'); |
| 2 | const assert = require('node:assert/strict'); |
| 3 | const {readFileSync} = require('node:fs'); |
| 4 | const vm = require('node:vm'); |
| 5 | |
| 6 | function page() { |
| 7 | const elements = new Map(); |
| 8 | const handlers = new Map(); |
| 9 | const remoteEvents = new Map(); |
| 10 | const calls = []; |
| 11 | const element = id => { |
| 12 | if (!elements.has(id)) elements.set(id, {id, value:'', hidden:false, textContent:'', |
| 13 | contains: node => node?.remote === true, append(){}, focus(){document.activeElement=this;}, select(){}}); |
| 14 | return elements.get(id); |
| 15 | }; |
| 16 | const canvas = {remote:true}; |
| 17 | const document = {activeElement:canvas, documentElement:{}, body:{append(){}}, head:{append(){}}, |
| 18 | createElement:()=>({style:{},setAttribute(){},focus(){document.activeElement=this;}}), getElementById:element, |
| 19 | addEventListener:(name,fn)=>handlers.set(name,fn)}; |
| 20 | const UI = {connected:true, rfb:{addEventListener:(name,fn)=>remoteEvents.set(name,fn), |
| 21 | sendKey:(...args)=>calls.push(['key',...args]), |
| 22 | cagePasteText:text=>{calls.push(['paste',text]);return Promise.resolve();},focus(){document.activeElement=canvas;}}}; |
| 23 | const clipboard = {writeText:async text=>calls.push(['write-host',text]),readText:async()=>{calls.push(['read-host']);return '本机文本';}}; |
| 24 | const source = readFileSync(__dirname+'/clipboard.js','utf8').replace(/^import .*;$/gm,''); |
| 25 | vm.runInNewContext(source,{getKeycode:e=>e.code || ('Key'+e.key.toUpperCase()),getKeysym:e=>e.key.length===1 ? e.key.codePointAt(0) : ({Shift:0xffe1,Control:0xffe3,Alt:0xffe9}[e.key] ?? null),window:{addEventListener:(name,fn)=>handlers.set('window:'+name,fn)},UI,document,navigator:{clipboard},MutationObserver:class{observe(){}},setTimeout:()=>1,clearTimeout(){}}); |
| 26 | const event = values => ({key:'',metaKey:false,ctrlKey:false,preventDefault(){this.prevented=true;},stopImmediatePropagation(){this.stopped=true;},...values}); |
| 27 | return {UI,document,element,handlers,remoteEvents,calls,clipboard,event,canvas}; |
| 28 | } |
| 29 | |
| 30 | test('receiving remote clipboard never writes to host without a user copy',async()=>{ |
| 31 | const p=page(); |
| 32 | await p.remoteEvents.get('clipboard')({detail:{text:'private guest text'}}); |
| 33 | assert.equal(p.calls.length,0); |
| 34 | }); |
| 35 | test('Mac copy maps to Linux Ctrl+C and exports the subsequent remote clipboard',async()=>{ |
| 36 | const p=page(); |
| 37 | const e=p.event({key:'c',metaKey:true}); p.handlers.get('keydown')(e); |
| 38 | assert.equal(e.prevented,true); assert.equal(e.stopped,true); |
| 39 | assert.deepEqual(p.calls.map(c=>c.slice(0,2)),[['key',0xffe3],['key',99],['key',99],['key',0xffe3]]); |
| 40 | await p.remoteEvents.get('clipboard')({detail:{text:'中文\n✅'}}); |
| 41 | assert.deepEqual(p.calls.at(-1),['write-host','中文\n✅']); |
| 42 | }); |
| 43 | test('native paste transfers Unicode only while the remote canvas is focused',()=>{ |
| 44 | const p=page(); |
| 45 | const e=p.event({clipboardData:{types:['text/plain'],getData:()=> '中文\n✅'}}); |
| 46 | p.handlers.get('paste')(e); |
| 47 | assert.equal(e.prevented,true); assert.deepEqual(p.calls[0],['paste','中文\n✅']); |
| 48 | p.calls.length=0; p.document.activeElement=p.element('cage-transfer-text'); |
| 49 | p.handlers.get('paste')(p.event({clipboardData:{types:['text/plain'],getData:()=> 'local field'}})); |
| 50 | assert.equal(p.calls.length,0); |
| 51 | }); |
| 52 | test('paste shortcut preserves the native paste event; Meta is not sent to Linux',()=>{ |
| 53 | const p=page(); |
| 54 | const e=p.event({key:'v',metaKey:true});p.handlers.get('keydown')(e); |
| 55 | assert.equal(e.stopped,true);assert.equal(e.prevented,undefined);assert.equal(p.calls.length,0); |
| 56 | assert.equal(p.document.activeElement.id,'cage-native-paste'); |
| 57 | p.document.activeElement=p.canvas; |
| 58 | const meta=p.event({key:'Meta',metaKey:true});p.handlers.get('keydown')(meta);assert.equal(meta.stopped,true); |
| 59 | }); |
| 60 | test('denied host clipboard access exposes a manual transfer field',async()=>{ |
| 61 | const p=page();p.clipboard.readText=async()=>{throw Error('denied');}; |
| 62 | await p.element('cage-paste').onclick(); |
| 63 | assert.equal(p.document.activeElement.id,'cage-transfer-text'); |
| 64 | assert.equal(p.calls.length,0); |
| 65 | }); |
| 66 | |
| 67 | test('Command+F and Command+Z become guest Ctrl shortcuts',()=>{ |
| 68 | for (const key of ['f','z']) { |
| 69 | const p=page(), e=p.event({key,code:'Key'+key.toUpperCase(),metaKey:true}); |
| 70 | p.handlers.get('keydown')(e); |
| 71 | assert.equal(e.prevented,true); |
| 72 | assert.deepEqual(p.calls,[['key',0xffe3,'ControlLeft',true],['key',key.charCodeAt(0),e.code,true],['key',key.charCodeAt(0),e.code,false],['key',0xffe3,'ControlLeft',false]]); |
| 73 | const up=p.event({key,code:e.code,metaKey:false}); |
| 74 | p.handlers.get('keyup')(up);assert.equal(up.stopped,true); |
| 75 | } |
| 76 | }); |
| 77 | test('Command+Shift+Z preserves Shift and does not release held modifiers',()=>{ |
| 78 | const p=page();p.handlers.get('keydown')(p.event({key:'Z',code:'KeyZ',metaKey:true,shiftKey:true})); |
| 79 | assert.ok(p.calls.some(c=>c[1]===90 && c[3]===true)); |
| 80 | assert.ok(p.calls.some(c=>c[2]==='ShiftLeft' && c[3]===true)); |
| 81 | assert.ok(p.calls.some(c=>c[2]==='ShiftLeft' && c[3]===false)); |
| 82 | p.calls.length=0; |
| 83 | p.handlers.get('keydown')(p.event({key:'Control',code:'ControlLeft',ctrlKey:true})); |
| 84 | p.handlers.get('keydown')(p.event({key:'f',code:'KeyF',metaKey:true,ctrlKey:true})); |
| 85 | assert.deepEqual(p.calls,[['key',0xffe3,'ControlLeft',true],['key',102,'KeyF',true],['key',102,'KeyF',false]]); |
| 86 | }); |
| 87 | test('local fields and normal Ctrl shortcuts retain their own handlers',()=>{ |
| 88 | const p=page();p.document.activeElement=p.element('cage-transfer-text'); |
| 89 | const local=p.event({key:'f',metaKey:true});p.handlers.get('keydown')(local); |
| 90 | assert.equal(local.prevented,undefined);assert.equal(p.calls.length,0); |
| 91 | p.document.activeElement=p.canvas; |
| 92 | const ctrl=p.event({key:'f',ctrlKey:true});p.handlers.get('keydown')(ctrl); |
| 93 | assert.equal(ctrl.prevented,undefined);assert.equal(p.calls.length,0); |
| 94 | }); |
| 95 | test('Command+Shift+C remains a guest shortcut rather than clipboard export',async()=>{ |
| 96 | const p=page();p.handlers.get('keydown')(p.event({key:'C',code:'KeyC',metaKey:true,shiftKey:true})); |
| 97 | await p.remoteEvents.get('clipboard')({detail:{text:'not requested'}}); |
| 98 | assert.ok(!p.calls.some(c=>c[0]==='write-host')); |
| 99 | }); |
| 100 | |
| 101 | test('modifier pressed after Command stays down until release or focus loss',()=>{ |
| 102 | const p=page(); |
| 103 | const shift=p.event({key:'Shift',code:'ShiftLeft',metaKey:true,shiftKey:true}); |
| 104 | p.handlers.get('keydown')(shift); |
| 105 | assert.equal(shift.stopped,true); |
| 106 | assert.deepEqual(p.calls,[['key',0xffe1,'ShiftLeft',true]]); |
| 107 | p.handlers.get('keyup')(p.event({key:'Shift',code:'ShiftLeft',metaKey:false})); |
| 108 | assert.deepEqual(p.calls.at(-1),['key',0xffe1,'ShiftLeft',false]); |
| 109 | p.handlers.get('keydown')(shift); |
| 110 | p.handlers.get('window:blur')(); |
| 111 | assert.deepEqual(p.calls.at(-1),['key',0xffe1,'ShiftLeft',false]); |
| 112 | }); |
| 113 | |
| 114 | test('right-side held modifiers do not leave synthetic left modifiers pressed',()=>{ |
| 115 | const p=page(); |
| 116 | p.handlers.get('keydown')(p.event({key:'Control',code:'ControlRight',ctrlKey:true})); |
| 117 | p.handlers.get('keydown')(p.event({key:'Shift',code:'ShiftRight',shiftKey:true,ctrlKey:true})); |
| 118 | p.handlers.get('keydown')(p.event({key:'Z',code:'KeyZ',metaKey:true,ctrlKey:true,shiftKey:true})); |
| 119 | assert.deepEqual(p.calls.slice(0,2),[['key',0xffe4,'ControlRight',true],['key',0xffe2,'ShiftRight',true]]); |
| 120 | assert.ok(!p.calls.some(c=>c[2]==='ControlLeft'||c[2]==='ShiftLeft')); |
| 121 | }); |
| 122 | |
| 123 | test('malformed macOS Command letter events recover the letter without losing Shift',()=>{ |
| 124 | const p=page();p.handlers.get('keydown')(p.event({key:'\u0000',code:'KeyZ',metaKey:true,shiftKey:true})); |
| 125 | assert.ok(p.calls.some(c=>c[1]===90 && c[2]==='KeyZ' && c[3]===true)); |
| 126 | assert.ok(!p.calls.some(c=>c[1]===0)); |
| 127 | }); |