platform.test.mjs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import { test } from 'node:test'
  2. import assert from 'node:assert/strict'
  3. import { desktopPlatform, desktopMenu, loginItemOptions } from './platform.mjs'
  4. import { DesktopOnboarding } from './onboarding.mjs'
  5. import { hostPathInput, pathInside } from '../src/shared/hostPaths.mjs'
  6. test('Windows can only connect to a server, including direct onboarding calls', () => {
  7. const platform = desktopPlatform('win32')
  8. assert.deepEqual(platform.supportedModes, ['client'])
  9. const onboarding = new DesktopOnboarding({ ...platform, remoteServer: () => '' })
  10. assert.equal(onboarding.snapshot().mode, 'remote')
  11. assert.equal(onboarding.snapshot().platform, 'win32')
  12. assert.throws(() => onboarding.select('local'), /不支持/)
  13. assert.throws(() => onboarding.open({ mode: 'local' }), /不支持/)
  14. assert.equal(onboarding.select('remote').mode, 'remote')
  15. assert.deepEqual(desktopPlatform('darwin').supportedModes, ['client', 'server'])
  16. })
  17. test('Windows menus retain useful actions and omit all local service actions', () => {
  18. const template = [{ label: '夭夭', submenu: [{ role: 'hide' }, { id: 'desktop-local-vm' }, { id: 'desktop-login' }, { id: 'desktop-quit', label: '退出夭夭(后台继续运行)' }] },
  19. { label: '执行节点', submenu: [{ id: 'runner-import' }] }, { label: '运行模式', submenu: [{ id: 'desktop-host-use-local' }, { label: '更换服务器…' }] }]
  20. const windows = desktopMenu(template, 'win32')
  21. assert.equal(windows.length, 2)
  22. assert.deepEqual(windows[0].submenu.map(item => item.label), ['登录 Windows 时启动', '退出夭夭'])
  23. assert.deepEqual(windows[1].submenu, [{ label: '更换服务器…' }])
  24. assert.equal(desktopMenu(template, 'darwin'), template)
  25. assert.deepEqual(loginItemOptions('win32', 'C:\\Apps\\Yaoyao.exe'), { path: 'C:\\Apps\\Yaoyao.exe', args: ['--yaoyao-login'] })
  26. })
  27. test('Windows file boundaries handle drive casing, separators, traversal and device paths', () => {
  28. const root = 'C:\\Users\\张三'
  29. for (const path of ['c:\\users\\张三\\Documents\\a.txt', 'C:/Users/张三/Desktop/a.txt', root]) assert.equal(pathInside(root, path, 'win32'), true, path)
  30. for (const path of ['C:\\Users\\张三2\\a.txt', 'D:\\Users\\张三\\a.txt', 'C:\\Users\\张三\\..\\secret']) assert.equal(pathInside(root, path, 'win32'), false, path)
  31. assert.equal(hostPathInput('~\\Documents\\中文.txt', 'win32'), 'Documents\\中文.txt')
  32. for (const path of ['C:secret', '\\\\server\\share', '\\\\?\\C:\\secret', 'a.txt:secret', 'CON', 'x\\NUL.txt', 'x.\\secret', 'a\0b']) assert.throws(() => hostPathInput(path, 'win32'), /路径无效/, path)
  33. assert.equal(hostPathInput('notes:today', 'darwin'), 'notes:today')
  34. assert.equal(pathInside('/home/a', '/home/ab', 'linux'), false)
  35. })