Skip to content

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

21.5 KB
Raw Blame
'use strict';

const assert = require('node:assert/strict');
const test = require('node:test');
const inject = require('../scripts/inject.js');
const compat = require('../scripts/workbuddy-compat.js');
const fs = require('node:fs');
const path = require('node:path');

const injectSource = fs.readFileSync(path.join(__dirname, '..', 'scripts', 'inject.js'), 'utf8');
const { createDirtyIndex } = require('../scripts/session-dirty.js');

test('explicit deletion is sent even without content changes or a baseline, and survives later lifecycle events', () => {
  const sent = [];
  const tracker = inject.createSessionDirtyTracker(payload => sent.push(payload), { delay: 0 });
  tracker.baseline([{ id: 'a', title: 'A', updatedAt: 10 }]);
  assert.equal(tracker.observe({ id: 'a', title: 'A', updatedAt: 10, event: 'sessionDeleted' }), true);
  tracker.observe({ id: 'a', title: 'A', updatedAt: 11, event: 'sessionUpdated' });
  assert.equal(tracker.observe({ id: 'b', event: 'sessionDeleted' }), true);
  tracker.flush();
  assert.deepEqual(sent[1], [{ id: 'a', event: 'sessionDeleted' }, { id: 'b', event: 'sessionDeleted' }]);
  tracker.destroy();
});

test('session dirty index uses the renderer baseline and preserves concurrent changes', () => {
  const index = createDirtyIndex();
  assert.equal(index.shouldSync('account-a', 'session-a'), true, 'uninitialized accounts use the safe fallback');
  assert.equal(index.markBaseline('account-a'), true);
  assert.equal(index.shouldSync('account-a', 'session-a'), false);
  const marker = index.mark('account-a', 'session-a', 'sessionUpdated', 100);
  assert.deepEqual(marker, { at: 100, event: 'sessionUpdated' });
  assert.equal(index.shouldSync('account-a', 'session-a'), true);
  assert.equal(index.clear('account-a', 'session-a', 99), false, 'a newer event must remain dirty');
  assert.equal(index.clear('account-a', 'session-a', 100), true);
  assert.equal(index.shouldSync('account-a', 'session-a'), false);
});

test('renderer dirty tracker ignores the initial snapshot and unchanged lifecycle events', async () => {
  const sent = [];
  const tracker = inject.createSessionDirtyTracker((payload) => sent.push(payload), { delay: 0 });
  tracker.baseline([{ id: 'session-a', status: 'completed', title: 'A' }]);
  assert.deepEqual(sent, [{ ready: true }]);
  assert.equal(tracker.observe({ id: 'session-a', status: 'completed', title: 'A' }), false);
  assert.equal(tracker.observe({ id: 'session-a', status: 'running', title: 'A', updatedAt: 11, event: 'sessionUpdated' }), true);
  tracker.flush();
  assert.deepEqual(sent[1], [{ id: 'session-a', event: 'sessionUpdated' }]);
  tracker.destroy();
});

test('renderer dirty tracker ignores account-switch lifecycle churn', () => {
  const sent = [];
  const tracker = inject.createSessionDirtyTracker((payload) => sent.push(payload), { delay: 0 });
  tracker.baseline([{
    id: 'session-a', title: 'A', status: 'completed', state: 'idle',
    active: false, terminal: true, updatedAt: 10, lastActivityAt: 10,
  }]);
  assert.equal(tracker.observe({
    id: 'session-a', title: 'A', status: 'running', state: 'hydrating',
    active: true, terminal: false, updatedAt: 10, lastActivityAt: 10,
    event: 'sessionUpdated',
  }), false);
  tracker.flush();
  assert.deepEqual(sent, [{ ready: true }]);
  tracker.destroy();
});

test('renderer dirty tracker ignores activation-only last-activity changes', () => {
  const sent = [];
  const tracker = inject.createSessionDirtyTracker((payload) => sent.push(payload), { delay: 0 });
  tracker.baseline([{ id: 'session-a', title: 'A', updatedAt: 10, lastActivityAt: 10 }]);
  assert.equal(tracker.observe({
    id: 'session-a', title: 'A', updatedAt: 10, lastActivityAt: 11,
    status: 'completed', event: 'sessionUpdated',
  }), false);
  tracker.flush();
  assert.deepEqual(sent, [{ ready: true }]);
  tracker.destroy();
});

test('approval gate accepts attached background approval cards without visibility checks', () => {
  const classify = inject.classifyNoDisturbApprovalCandidate;
  assert.equal(classify({
    label: '1允许',
    context: '任务 B 检测到受保护文件修改',
    hasDeny: true,
    hasOnce: true,
    buttonCount: 3,
  }), 'once');
  assert.equal(classify({
    label: '始终允许',
    context: '权限 approval\n拒绝',
    hasDeny: true,
    hasOnce: true,
    buttonCount: 2,
  }), 'session');
});

test('approval gate handles simultaneous cards and rejects unrelated confirmations', () => {
  const classify = inject.classifyNoDisturbApprovalCandidate;
  const cards = [
    { label: '允许', context: '沙箱外写入\n拒绝', hasDeny: true, hasOnce: true, buttonCount: 2 },
    { label: 'Allow', context: '系统级工具\nDeny', hasDeny: true, hasOnce: true, buttonCount: 2 },
    { label: '确认', context: '确认生成图片,消耗 10 积分\n取消', hasDeny: false, buttonCount: 2 },
  ];
  assert.deepEqual(cards.map(classify), ['once', 'once', null]);
  assert.equal(classify({
    label: '允许',
    context: '普通设置确认',
    hasDeny: false,
    buttonCount: 2,
    disabled: true,
  }), null);
});

test('session monitor registry keeps bounded per-session logs in memory', () => {
  const registry = inject.createSessionMonitorRegistry({ maxLogs: 2, maxSessions: 2 });
  registry.ensure('a', { title: '任务 A', status: 'running' });
  registry.append('a', 'state', { status: 'running' });
  registry.append('a', 'approval-clicked', { source: 'api' });
  registry.append('a', 'continue-sent', { source: 'api' });
  assert.equal(registry.list().length, 1);
  assert.equal(registry.get('a').logs.length, 2);
  assert.equal(registry.get('a').logs[0].event, 'approval-clicked');
  assert.equal(registry.get('a').logs[1].event, 'continue-sent');
  registry.ensure('b');
  registry.ensure('c');
  assert.equal(registry.get('a'), null);
  assert.deepEqual(registry.list().map((item) => item.id), ['b', 'c']);
});

test('session monitor progress gate excludes completed and idle snapshots', () => {
  const inProgress = inject.isSessionMonitorInProgress;
  assert.equal(inProgress({ busy: true }), true);
  assert.equal(inProgress({ blocked: true }), true);
  assert.equal(inProgress({ hydrating: true }), true);
  assert.equal(inProgress({ complete: true, busy: false }), false);
  assert.equal(inProgress({ state: 'done', busy: false }), false);
});

test('session monitor interrupted gate accepts exactly what the judge would continue', () => {
  const interrupted = inject.isSessionMonitorInterrupted;
  const decision = inject.controllerAutoContinueDecision;
  // 空闲 + 判定层愿意续跑 = 必须绑定。旧版绑定闸门只按 in-progress 过滤,
  // 这些会话在绑定阶段就被丢掉,判定层一次都走不到。
  const continuable = [
    { assistantId: 'req-a-assistant', error: true },                              // 错误 UI
    { assistantId: 'req-a-assistant', networkFailure: true },                     // 网络类错误
    { assistantId: 'req-a-assistant', complete: false, terminal: false },         // 结构化中断(无错误 UI 也常见)
    { assistantId: 'req-a-assistant', terminalKnown: true, terminal: false, complete: true }, // 明确未终局
  ];
  continuable.forEach((snapshot) => {
    assert.equal(decision(snapshot).trigger, true, 'judge should continue ' + JSON.stringify(snapshot));
    assert.equal(interrupted(snapshot), true, 'gate should bind ' + JSON.stringify(snapshot));
  });
  // 终局的模型限频/额度错误不应自动续跑,但必须绑定一次以记录限频状态。
  assert.equal(interrupted({ assistantId: 'req-rate-limit', terminal: true, terminalKnown: true, rateLimited: true }), true);
  assert.equal(interrupted({ assistantId: 'req-quota', terminal: true, terminalKnown: true, quotaExhausted: true }), true);
  // 仍在运行 / 等决策 / 恢复中:交给 in-progress 分支,不重复绑定
  assert.equal(interrupted({ assistantId: 'a', error: true, busy: true }), false);
  assert.equal(interrupted({ assistantId: 'a', error: true, blocked: true }), false);
  assert.equal(interrupted({ assistantId: 'a', error: true, hydrating: true }), false);
  // 判定层不续跑的空闲会话:闸门同样不绑定,避免对正常历史回复多发一次续跑
  const finished = [
    { assistantId: 'a', manualStop: true, error: true },                 // 用户主动取消
    { assistantId: 'a', complete: true, terminal: true },                // 结构化完成
    { assistantId: 'a', terminalKnown: true, terminal: true, complete: true },
    { assistantId: 'a', complete: true },                                // 旧版无 terminal 字段
    { assistantId: 'a', completionMarker: true, complete: false, terminal: false },
    { assistantId: '', error: true },                                    // 没有助手回复
    null,
  ];
  finished.forEach((snapshot) => {
    assert.equal(decision(snapshot).trigger, false, 'judge should not continue ' + JSON.stringify(snapshot));
    assert.equal(interrupted(snapshot), false, 'gate should not bind ' + JSON.stringify(snapshot));
  });
});

test('interrupted sessions enter auto-continue instead of waiting for a new reply', () => {
  // 判定层对同一份快照确实会触发续跑(error-ui):缺口只在「能否进入判定」
  assert.deepEqual(
    inject.classifyAutoContinueControllerSnapshot({ assistantId: 'req-a-assistant', error: true }),
    { trigger: true, reason: 'error-ui' },
  );
  // 两条绑定路径都必须接纳已中断会话,而不是无限等待不会到来的新回复
  assert.match(
    injectSource,
    /if \(!existing && !isSessionMonitorInProgress\(initialSnapshot\) && !isSessionMonitorInterrupted\(initialSnapshot\)\) return null;/,
  );
  assert.match(injectSource, /session\.awaitingNewReply = false;[\s\S]{0,80}session\.baselineAssistantKey = '';/);
  assert.match(
    injectSource,
    /c\.awaitingNewReply = preserve[\s\S]{0,100}!!carry\.awaitingNewReply && !isSessionMonitorInterrupted\(snap\)[\s\S]{0,60}: !isSessionMonitorInterrupted\(snap\);/,
  );
  assert.match(injectSource, /acLog\('baseline-established', \{ via: 'controller', awaitingNewReply: c\.awaitingNewReply \}\);/);
  // 控制器快照必须自带完成标记,判定与绑定闸门共用同一份证据
  assert.match(injectSource, /completionMarker: acHasMarker\(text\),/);
  // 判定路径与绑定闸门必须走同一个映射,否则两条规则会再次错位
  assert.match(injectSource, /var decision = controllerAutoContinueDecision\(snap\);/);
  assert.match(injectSource, /var decision = controllerAutoContinueDecision\(snapshot\);/);
  assert.equal(
    (injectSource.match(/classifyAutoContinueControllerSnapshot\(\{/g) || []).length,
    1,
    '控制器/多会话判定不得各自内联一份映射',
  );
  assert.match(
    injectSource,
    /function controllerAutoContinueDecision\(snapshot\) \{[\s\S]{0,400}return classifyAutoContinueControllerSnapshot\(\{/,
  );
  assert.match(injectSource, /return controllerAutoContinueDecision\(s\)\.trigger === true;/);
});

test('session resource normalizer treats background terminal updates as final', () => {
  const normalize = inject.normalizeSessionMonitorResourceRecord;
  assert.deepEqual(normalize({
    id: 'task-b',
    status: 'working',
    protocolStatus: 'completed',
    name: '任务 B',
  }, 'sessionUpdated'), {
    id: 'task-b',
    title: '任务 B',
    status: 'completed',
    pendingInputKind: '',
    protocolStatus: 'completed',
    state: '',
    activePromptStartedAt: 0,
    queueRevision: null,
    updatedAt: 0,
    lastActivityAt: 0,
    active: false,
    terminal: true,
    event: 'sessionUpdated',
  });
  assert.equal(normalize({ id: 'task-b', status: 'model_streaming' }, 'sessionUpdated').active, true);
  // 官方 status='pending' = 侧栏「待确认」(等待交互/允许),视为 active 待批准信号
  assert.equal(normalize({ id: 'task-b', status: 'pending' }, 'sessionUpdated').active, true);
  assert.equal(normalize({ id: 'task-b', status: 'pending', activePromptStartedAt: 123 }, 'sessionUpdated').active, true);
  assert.equal(normalize({ id: 'task-b', status: 'pending', pendingInputKind: 'permission' }, 'sessionUpdated').active, true);
  assert.equal(normalize({
    id: 'task-b',
    status: 'pending',
    messageQueueRuntime: { pendingItemCount: 1, paused: false },
  }, 'sessionUpdated').active, true);
  const revision = normalize({ id: 'task-b', status: 'running', updated_at: 10, last_activity_at: 9,
    messageQueueRuntime: { inflightItemId: 'q1', pendingItemCount: 2, paused: false } });
  assert.deepEqual([revision.updatedAt, revision.lastActivityAt, revision.queueRevision], [10, 9, ['q1', 2, 0, false]]);
});

test('session lifecycle adds unknown background work and removes terminal sessions', () => {
  const action = inject.sessionMonitorLifecycleAction;
  assert.equal(action({ id: 'task-b', active: true, terminal: false }, false), 'add');
  assert.equal(action({ id: 'task-b', active: true, terminal: false }, true), 'update');
  assert.equal(action({ id: 'task-b', active: false, terminal: true }, true), 'remove');
  assert.equal(action({ id: 'task-b', active: false, terminal: true }, false), 'ignore');
  assert.equal(action({ id: 'task-b', active: false, terminal: false }, false), 'ignore');
});

test('multi-session monitor subscribes while idle and detaches switched controllers without deleting sessions', () => {
  assert.match(injectSource, /function acStartMultiMonitor\(\)[\s\S]*var hasResource = acMultiBindSessionResource\(\);[\s\S]*if \(!hasResource && !hasSidebar && !Object\.keys\(acMulti\.sessions\)\.length\)/);
  assert.match(injectSource, /function acMultiDetachController\(session\)[\s\S]*session\.controller = null;/);
  assert.match(injectSource, /function acMultiFinishSession\(session\)[\s\S]*delete acMulti\.sessions\[session\.id\];/);
});

test('sync idle probe can scope busy checks to the sessions being copied', () => {
  assert.match(injectSource, /function isAnySessionBusyForIds\(ids\)/);
  assert.match(injectSource, /window\.__wbsSessionsBusy = sessionBusyIdsProbe/);
  assert.match(injectSource, /discoverSleepSessionBusy\(wanted\)/);
});

test('session resource subscription delivers non-active completion without controller changes', () => {
  const handlers = new Map();
  const resource = {
    on(event, handler) { handlers.set(event, handler); },
    off(event, handler) { if (handlers.get(event) === handler) handlers.delete(event); },
  };
  const updates = [];
  const unsubscribe = inject.subscribeSessionMonitorResource(resource, (update) => updates.push(update));

  handlers.get('sessionUpdated')({ id: 'task-b', status: 'completed' });
  handlers.get('sessionsChanged')([
    { id: 'task-a', status: 'working' },
    { id: 'task-c', status: 'failed' },
  ]);

  assert.deepEqual(updates.map((item) => [item.id, item.status, item.terminal]), [
    ['task-b', 'completed', true],
    ['task-a', 'working', false],
    ['task-c', 'failed', true],
  ]);
  unsubscribe();
  assert.equal(handlers.size, 0);
});

test('monitor log card stays hidden until the five-click logo unlock', () => {
  assert.match(injectSource, /id="wbs-monitor-log-card"' \+ \(hiddenToolsUnlocked \? '' : ' style="display:none"'\)/);
  assert.match(injectSource, /if \(count === 5\) options\.unlockDebug\(\)/);
  assert.match(injectSource, /unlockDebug: function \(\) \{[\s\S]*hiddenToolsUnlocked = true;[\s\S]*monitorLogCard\.style\.display = '';/);
});

test('compat discovers multiple capability-shaped controllers and de-duplicates fibers', () => {
  const controllers = [
    { conversationId: 'a', messageStore: { getState() {}, subscribe() {} }, getMessagesViewState() {} },
    { conversationId: 'b', messageStore: { getState() {}, subscribe() {} }, getMessagesViewState() {} },
  ];
  const root = { children: [], __reactFiber$root: { memoizedProps: { value: controllers[0] }, return: null } };
  const child = { children: [], __reactFiber$child: { memoizedProps: { controller: controllers[1] }, return: root.__reactFiber$root } };
  root.children.push(child);
  const doc = { querySelector(selector) { return selector === '#root > div' ? root : null; } };
  assert.deepEqual(compat.findConversationControllers(doc).map((c) => c.conversationId), ['a', 'b']);
});

test('auto-continue controller discovery accepts AI controllers without getMessagesViewState', () => {
  const inject = fs.readFileSync(path.join(__dirname, '../scripts/inject.js'), 'utf8');
  const start = inject.indexOf('function acFindConversationController()');
  const end = inject.indexOf('function acCaptureControllerError', start);
  const source = inject.slice(start, end);
  assert.match(source, /typeof controller\.messageStore\.getState === 'function'/);
  assert.doesNotMatch(source, /typeof controller\.getMessagesViewState === 'function'/);
  assert.match(source, /\[props\.value, props\.controller, props\.adapter\]/);
});

test('quota and model rate limit stops surface one renderer toast', () => {
  const start = injectSource.indexOf('function acNotifyLimit(snapshot)');
  const end = injectSource.indexOf('function acPickBodyBlock', start);
  const source = injectSource.slice(start, end);
  assert.match(source, /snapshot\.rateLimited/);
  assert.match(source, /snapshot\.quotaExhausted/);
  assert.match(source, /acLimitToastSeen/);
  assert.match(source, /toast\(message, true, root\)/);
  assert.match(source, /当前账号积分已耗尽/);
  assert.match(source, /当前模型已触发频率限制,已停止自动发送/);
  assert.doesNotMatch(source, /Auto-Continue 已停止/);
});

test('compat discovers the global session resource by capabilities', () => {
  const resource = { on() {}, off() {}, list() {}, getByIds() {} };
  const adapter = {
    sessionsResource: resource,
    enqueueConversationMessageQueueItem() {},
    pauseConversationMessageQueue() {},
  };
  const root = { children: [], __reactFiber$root: { memoizedProps: { adapter }, return: null } };
  const doc = { querySelector(selector) { return selector === '#root > div' ? root : null; } };
  assert.equal(compat.findSessionsResource(doc), resource);
});

test('compat reads the sidebar conversation status snapshot from a narrow fiber root', () => {
  const records = [
    { id: 'task-a', title: '任务 A', status: 'running' },
    { id: 'task-b', title: '任务 B', status: 'completed' },
  ];
  const ownerFiber = { memoizedProps: { allConversations: records }, return: null };
  const item = { __reactFiber$item: { memoizedProps: {}, return: ownerFiber } };
  const doc = {
    querySelector(selector) {
      if (selector === '.conversation-list .conversation-item') return item;
      return null;
    },
  };
  assert.deepEqual(compat.findConversationListRecords(doc), records);
});


test('dirty monitor polls sidebar revisions when the resource stays empty, and disposes on reinjection', async t => {
  const vm = require('node:vm');
  let records = [{ id: 'old', title: 'fixture', status: 'completed', sortAt: 10 }];
  const sent = [];
  const intervals = new Map();
  const disposers = [];
  const trackers = [];
  const ctx = {
    document: {}, Promise, setTimeout, clearTimeout,
    setInterval(fn) { intervals.set(fn, fn); return fn; },
    clearInterval(id) { intervals.delete(id); },
    registerDisposer(fn) { disposers.push(fn); },
    api: async (route, options) => {
      if (route === '/api/current') return { uid: 'fixture-account' };
      sent.push(JSON.parse(options.body)); return { ok: true };
    },
    WBS_COMPAT: {
      findSessionsResource: () => ({ list: () => ({ agents: [] }), on() {}, off() {} }),
      findConversationListRecords: () => records,
    },
    subscribeSessionMonitorResource: inject.subscribeSessionMonitorResource,
    createSessionDirtyTracker(send) {
      const tracker = inject.createSessionDirtyTracker(send);
      trackers.push(tracker); return tracker;
    },
  };
  t.after(() => { disposers.forEach(fn => fn()); trackers.forEach(tracker => tracker.destroy()); });
  const start = injectSource.indexOf('    var sessionDirtyResource = null;');
  const end = injectSource.indexOf('    /** 状态机可视化日志', start);
  vm.runInNewContext(injectSource.slice(start, end), ctx);
  await new Promise(resolve => setImmediate(resolve));
  const poll = () => { for (const fn of intervals.values()) fn(); trackers.forEach(tracker => tracker.flush()); };
  poll();
  assert.equal(sent.flatMap(body => body.events || []).length, 0, 'initial sidebar data is a clean baseline');
  records = [{ ...records[0], sortAt: 11 }];
  poll();
  assert.deepEqual(sent.flatMap(body => body.events || []), [{ id: 'old', event: 'sessionUpdated' }]);
  assert.equal(sent.at(-1).uid, 'fixture-account');
  poll();
  assert.equal(sent.flatMap(body => body.events || []).length, 1, 'unchanged polls do not repeat events');
  records = [];
  poll();
  records = [{ id: 'old', title: 'fixture', status: 'completed', sortAt: 11 },
    { id: 'new', title: 'new fixture', status: 'running', sortAt: 12 }];
  poll();
  assert.deepEqual(sent.flatMap(body => body.events || []).at(-1), { id: 'new', event: 'sessionCreated' });
  assert.equal(sent.flatMap(body => body.events || []).length, 2, 'temporary missing sidebar does not reset the baseline');
  disposers.forEach(fn => fn());
  assert.equal(intervals.size, 0, 'reinjection removes the polling timer');
});


test('synced-member eligibility survives later renderer events and dirty-index reloads', () => {
  let index = createDirtyIndex();
  index.markBaseline('one');
  index.mark('one', 'a', 'sessionSynced', 100);
  index.mark('one', 'a', 'sessionUpdated', 101);
  index = createDirtyIndex(JSON.parse(JSON.stringify(index.state)));
  assert.deepEqual(index.get('one', 'a'), { at: 101, event: 'sessionSynced' });
  assert.equal(index.clear('one', 'a', 100), false, 'older acknowledgements cannot erase a newer event');
});