Skip to content

WorkBuddy、CodeBuddy 桌面端增强助手:签到猫猫旅行成长计划查询、多账号独立备份、点切即用;免打扰模式让 AI 无人值守跑长任务;跨账号会话迁移、异常中断自动续接;暂存/快捷提示词;毛玻璃主题与更多实用功能。

8.8 KB
Raw Blame
'use strict';
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const vm = require('node:vm');
const test = require('node:test');
const boundary = require('../scripts/windows-process-boundary.js');
const read = (name) => fs.readFileSync(path.join(__dirname, '..', name), 'utf8');

// Windows APIs and Inno UI need Windows; lock down their integration here.
test('installer obtains explicit consent before cleanup and enables subsequent shortcut launches', () => {
  const src = read('scripts/win/workdaddy.iss');
  assert.match(src, /--desktop-token-status/);
  assert.match(src, /CreateCustomForm\(ScaleX\(500\), ScaleY\(265\), False, False\)/);
  assert.match(src, /Caption := '我已了解风险,继续安装'/);
  assert.match(src, /Button\.Default := False/);
  assert.match(src, /CancelButton\.Default := True/);
  assert.match(src, /WizardSilent[\s\S]*--check-elevated-session/);
  assert.match(src, /--accept-elevated-session --app-dir/);
  assert.match(src, /function ShouldLaunchElevatedSession[\s\S]*Result := ElevatedSessionConfirmed/);
  assert.match(src, /if IsAdmin and not ElevatedSessionConfirmed then\s+exit/);
  assert.match(src, /if ElevatedSessionConfirmed then[\s\S]*Result := Exec\(/);
  const prepare = src.slice(src.indexOf('function PrepareToInstall'));
  assert.ok(prepare.indexOf('ConfirmElevatedInstall') < prepare.indexOf('EnsureWorkBuddyClosed'));
});

test('elevated exception requires unsplit desktop token and persisted user/profile/install consent', () => {
  const src = read('scripts/windows-native/main.go');
  assert.match(src, /func desktopSessionSupportsElevated/);
  assert.match(src, /tokenElevationType/);
  assert.match(src, /elevationType != 1/);
  assert.match(src, /shellSID == currentSID/);
  assert.match(src, /windows-elevated-consent\.json/);
  assert.match(src, /consent\.Version == 1/);
  assert.match(src, /consent\.Profile == profile/);
  assert.match(src, /samePath\(consent\.AppDir, appDir\)/);
  assert.match(src, /consent\.UserSID == sid/);
  assert.match(src, /func elevatedSessionAllowed[\s\S]*desktopSessionSupportsElevated/);
  assert.match(src, /func terminateExactProcess[\s\S]*verifySameProcessSecurity\(handle\)/);
  assert.match(src, /--launch-context/);
  assert.match(src, /elevated && !elevatedSessionAllowed/);
});

test('native privilege query validates real helper response and never defaults errors to standard', () => {
  for (const privilege of ['standard', 'elevated']) {
    const actual = boundary.detectNativeWindowsPrivilege('C:\\Apps\\WorkDaddy', 'workbuddy-cn', (exe, args) => {
      assert.match(exe, /WorkDaddyLauncher\.exe$/);
      assert.deepEqual(args, ['--launch-context', '--profile', 'workbuddy-cn', '--app-dir', 'C:\\Apps\\WorkDaddy']);
      return { status: 0, stdout: JSON.stringify({ profile: 'workbuddy-cn', privilege }) };
    });
    assert.equal(actual, privilege);
  }
  for (const result of [
    { status: 5, stdout: '{"privilege":"elevated","profile":"workbuddy-cn"}' },
    { status: 0, stdout: '{}' },
    { status: 0, stdout: 'broken' },
    { status: 0, stdout: '{"privilege":"unknown","profile":"workbuddy-cn"}' },
    { status: 0, stdout: '{"privilege":"elevated","profile":"workbuddy-ai"}' },
    { status: null, error: new Error('timeout') },
  ]) assert.throws(() => boundary.detectNativeWindowsPrivilege('C:\\Apps\\WorkDaddy', 'workbuddy-cn', () => result));
});

test('native daemon reuse requires matching privilege, profile, build, data and app directories', () => {
  const src = read('scripts/win-launcher.js');
  const fn = src.slice(src.indexOf('function nativeDaemonStatusMatches('), src.indexOf('\nasync function waitForNativeDaemon'));
  const baseline = { ok: true, profile: { id: 'workbuddy-cn' }, dataDir: 'data', appDir: 'app', version: '1', buildId: 'build', privilege: 'elevated' };
  for (const privilege of ['elevated', 'standard']) {
    const context = vm.createContext({ WINDOWS_PRIVILEGE: privilege, readDaemonIdentity: () => ({ version: '1', buildId: 'build' }), PROFILE: { id: 'workbuddy-cn' }, DATA_DIR: 'data', WORKDADDY_APP_DIR: 'app', sameWindowsPath: (a, b) => a === b });
    vm.runInContext(fn, context);
    const status = { ...baseline, privilege };
    assert.equal(context.nativeDaemonStatusMatches(status), true);
    for (const delta of [{ privilege: privilege === 'standard' ? 'elevated' : 'standard' }, { version: 'old' }, { buildId: 'old' }, { dataDir: 'other' }, { appDir: 'other' }, { appDir: '' }, { profile: { id: 'workbuddy-ai' } }]) {
      assert.equal(context.nativeDaemonStatusMatches({ ...status, ...delta }), false);
    }
  }
  assert.match(read('scripts/daemon.js'), /detectNativeWindowsPrivilege/);
  assert.doesNotMatch(read('scripts/daemon.js'), /WBSWITCH_NATIVE_LAUNCHER === '1' \? 'standard'/);
  assert.match(src, /function hasElevatedSessionConsent[\s\S]*--check-elevated-session/);
});

test('native daemon wait allows the watchdog up to 60 seconds to become ready', () => {
  const src = read('scripts/win-launcher.js');
  const start = src.indexOf('async function waitForNativeDaemon(');
  const end = src.indexOf('\n}', start);
  assert.ok(start >= 0 && end > start);
  assert.match(src.slice(start, end), /attempts\s*=\s*240/);
});

test('native daemon startup exposes non-blocking progress notifications', () => {
  const src = read('scripts/win-launcher.js');
  assert.match(src, /function runHiddenPowerShell\(script, options = \{\}\)/);
  assert.match(src, /detached:\s*true/);
  assert.match(src, /runHiddenPowerShell\(ps, \{ detached: true \}\)/);
  assert.match(src, /showWindowsNotification\(WBS_BRAND, ['"]正在启动 WorkDaddy 后台服务,请稍候/);
  assert.match(src, /后台服务仍在启动[\s\S]*请勿重复点击/);
  assert.match(src, /15\s*\*\s*1000/);
  assert.match(src, /30\s*\*\s*1000/);
  assert.match(src, /45\s*\*\s*1000/);
});

test('native consent policy rejects missing, corrupt, foreign and no-longer-eligible consent', (t) => {
  const { spawnSync } = require('node:child_process');
  const os = require('node:os');
  if (spawnSync('go', ['version']).status !== 0) return t.skip('Go compiler unavailable');
  const source = read('scripts/windows-native/main.go');
  const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'workdaddy-consent-policy-'));
  try {
    // Compile the actual policy functions unchanged. Only Win32 probes are
    // substituted, so refusal and persistence can be tested on macOS/Linux.
    const policy = source.slice(source.indexOf('func elevatedSessionAllowed('), source.indexOf('\nfunc helperAppDir('));
    const structure = source.slice(source.indexOf('type elevatedConsent struct'), source.indexOf('\nfunc tokenDword('));
    fs.writeFileSync(path.join(dir, 'policy.go'), `package main
import ("encoding/json"; "errors"; "os"; "path/filepath"; "strings")
var eligible = true
var activeSID = "test-user"
var root string
func desktopSessionSupportsElevated() bool { return eligible }
func currentUserSID() (string, error) { return activeSID, nil }
func dataDir(profile string) (string, error) { return filepath.Join(root, profile), nil }
func samePath(a, b string) bool { return strings.EqualFold(filepath.Clean(a), filepath.Clean(b)) }
${structure}
${policy}`);
    fs.writeFileSync(path.join(dir, 'policy_test.go'), `package main
import ("os"; "path/filepath"; "testing")
func TestConsent(t *testing.T) {
 root = t.TempDir()
 app := filepath.Join(root, "install")
 if elevatedSessionAllowed("cn", app) { t.Fatal("missing consent allowed") }
 if err := saveElevatedConsent("cn", app); err != nil { t.Fatal(err) }
 if !elevatedSessionAllowed("cn", app) { t.Fatal("accepted consent rejected") }
 if elevatedSessionAllowed("ai", app) { t.Fatal("foreign profile allowed") }
 if elevatedSessionAllowed("cn", app+"-other") { t.Fatal("foreign install allowed") }
 if elevatedSessionAllowed("cn", "relative") { t.Fatal("relative path allowed") }
 activeSID = "another-user"
 if elevatedSessionAllowed("cn", app) { t.Fatal("foreign user allowed") }
 activeSID = "test-user"
 eligible = false
 if elevatedSessionAllowed("cn", app) { t.Fatal("ordinary desktop bypassed downgrade") }
 if err := saveElevatedConsent("ai", app); err == nil { t.Fatal("ineligible session saved consent") }
 eligible = true
 name := filepath.Join(root, "cn", "windows-elevated-consent.json")
 for _, contents := range []string{"", "{", "null", "{}", "[]", "{\\\"version\\\":2}"} {
  if err := os.WriteFile(name, []byte(contents), 0600); err != nil { t.Fatal(err) }
  if elevatedSessionAllowed("cn", app) { t.Fatal("invalid consent accepted", contents) }
 }
 if err := saveElevatedConsent("cn", app); err != nil { t.Fatal(err) }
 if !elevatedSessionAllowed("cn", app) { t.Fatal("repaired consent rejected") }
}`);
    const result = spawnSync('go', ['test', 'policy.go', 'policy_test.go'], { cwd: dir, encoding: 'utf8', timeout: 60000 });
    assert.equal(result.status, 0, result.stderr + result.stdout);
  } finally { fs.rmSync(dir, { recursive: true, force: true }); }
});