Files
order-notification-automation/tests/service/windows-startup.test.cjs
T

21 lines
3.3 KiB
JavaScript

'use strict';
const {test}=require('node:test'),assert=require('node:assert/strict'),fs=require('node:fs/promises'),os=require('node:os'),path=require('node:path');
test('Windows login shortcut uses the current account, supports spaces and can be removed without deleting service state',{skip:process.platform!=='win32',timeout:30000},async t=>{
const {installAutostart,uninstallAutostart,readShortcut}=require('../../src/platform/service/windows-startup.cjs'),root=await fs.mkdtemp(path.join(os.tmpdir(),'order startup ')),startupDir=path.join(root,'登录启动'),stateDir=path.join(root,'状态库');t.after(()=>fs.rm(root,{recursive:true,force:true}));
const projectRoot=path.join(root,'项目 带空格'),marker=path.join(root,'started.txt');await fs.mkdir(path.join(projectRoot,'scripts'),{recursive:true});await fs.writeFile(path.join(projectRoot,'scripts','service.cjs'),'require("node:fs").writeFileSync('+JSON.stringify(marker)+',process.argv[2]);');
const installed=installAutostart({stateDir,startupDir,projectRoot,nodePath:process.execPath});const shortcut=readShortcut(installed.linkPath);
assert.match(shortcut.target,/wscript\.exe$/i);assert.equal(shortcut.args,'//B "'+installed.launcherPath+'"');assert.equal(shortcut.cwd,projectRoot);assert.equal(installed.enabled,true);
require('node:child_process').execFileSync('cscript.exe',['//Nologo','//B',installed.launcherPath],{windowsHide:true,timeout:5000});
const deadline=Date.now()+5000;while(Date.now()<deadline){try{if(await fs.readFile(marker,'utf8')==='run')break;}catch{}await new Promise(r=>setTimeout(r,100));}assert.equal(await fs.readFile(marker,'utf8'),'run');
const twice=installAutostart({stateDir,startupDir,projectRoot,nodePath:process.execPath});assert.equal(twice.linkPath,installed.linkPath);
uninstallAutostart({stateDir,startupDir});await assert.rejects(fs.access(installed.linkPath));assert.ok(await fs.stat(path.join(stateDir,'supervisor','supervisor.sqlite')));
});
test('failed shortcut creation never reports enabled, and foreign shortcuts are preserved',{skip:process.platform!=='win32',timeout:30000},async t=>{
const {installAutostart,uninstallAutostart}=require('../../src/platform/service/windows-startup.cjs'),{readSupervisor}=require('../../src/platform/service/control.cjs'),root=await fs.mkdtemp(path.join(os.tmpdir(),'order-startup-ownership-')),stateDir=path.join(root,'state'),startupDir=path.join(root,'startup');t.after(()=>fs.rm(root,{recursive:true,force:true}));
assert.throws(()=>installAutostart({stateDir,startupDir,shortcutWriter:()=>{}}),/验证失败/);assert.equal(readSupervisor(stateDir).autostart,null);
const record=installAutostart({stateDir,startupDir});
const script='$d=[Console]::In.ReadToEnd()|ConvertFrom-Json; $s=(New-Object -ComObject WScript.Shell).CreateShortcut($d.file); $s.TargetPath=$d.target; $s.Arguments=""; $s.Save()';
require('node:child_process').execFileSync('powershell.exe',['-NoProfile','-NonInteractive','-Command',script],{input:JSON.stringify({file:record.linkPath,target:path.join(process.env.SystemRoot,'System32','notepad.exe')}),encoding:'utf8',windowsHide:true,timeout:10000});
const before=await fs.readFile(record.linkPath);assert.throws(()=>installAutostart({stateDir,startupDir}),/其他程序/);assert.throws(()=>uninstallAutostart({stateDir,startupDir}),/已被更改/);assert.deepEqual(await fs.readFile(record.linkPath),before);
});