Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

9.2 KB
Raw Blame
import { readFileSync } from 'node:fs';
import { resolve } from 'node:path';
import { describe, expect, it } from 'vitest';

function source(path: string): string {
  return readFileSync(resolve(process.cwd(), path), 'utf8').replace(/\r\n/g, '\n');
}

describe('任务消息内存治理页面接线', () => {
  const screen = source('app/sessions/[sessionId].tsx');
  const deviceLink = source('src/device-link/DeviceLinkContext.tsx');

  it('只有 focus 与 AppState active 同时成立时 enter,离场立即携 authority 撤权', () => {
    expect(screen).toContain('useFocusEffect(');
    expect(screen).toContain('messageScreenFocusedRef.current = true;');
    expect(screen).toContain("const messageAppActiveRef = useRef(AppState.currentState === 'active');");
    expect(screen).toContain('remoteSessionStore.enterSessionMessageDetail(sessionId)');
    expect(screen).toContain("remoteSessionStore.leaveSessionMessageDetail(sessionId, 'detail-blur', authority)");
    expect(screen).toContain("remoteSessionStore.leaveSessionMessageDetail(sessionId, 'app-background', authority)");
    expect(screen).toContain('setMessageReloadRevision((value) => value + 1);');
    expect(screen).toContain('handledMessageReloadRevisionRef.current === messageReloadRevision');
    expect(screen).toContain('handledMessageReloadRevisionRef.current = messageReloadRevision;');
    expect(screen).toContain('const releasePendingRouteFocusLookup = () => {');
    expect(screen).toContain('releasePendingRouteFocusLookup();');
    expect(screen).toContain('[deviceId, maker, messageReloadRevision, renderItems, requestSync, routeFocusClientId, routeFocusKey, sessionId]');
  });

  it('首次进入详情在取得 authority 后触发同步,不依赖更早的 mount load', () => {
    const authorityStart = screen.indexOf('const messageAuthorityRef = useRef');
    const authorityEnd = screen.indexOf('const auth = useAuth();', authorityStart);
    const authorityBlock = screen.slice(authorityStart, authorityEnd);
    const firstEnter = authorityBlock.indexOf(
      'messageAuthorityRef.current = remoteSessionStore.enterSessionMessageDetail(sessionId);',
    );
    const firstReload = authorityBlock.indexOf(
      'setMessageReloadRevision((value) => value + 1);',
      firstEnter,
    );
    expect(firstEnter).toBeGreaterThanOrEqual(0);
    expect(firstReload).toBeGreaterThan(firstEnter);
    expect(authorityBlock).not.toContain('shouldReload');
    expect(authorityBlock).not.toContain('lastEnteredMessageSessionRef');
    expect(authorityBlock).toContain('}, [deviceId, notificationResponse, sessionId]),');

    const subscriptionStart = screen.indexOf('return startFocusedTopicSubscription({');
    const optimisticOlderStart = screen.indexOf('// 乐观点亮「加载更早」入口', subscriptionStart);
    const subscriptionBlock = screen.slice(subscriptionStart, optimisticOlderStart);
    expect(subscriptionBlock).not.toContain('void load();');
  });

  it('同任务的新通知撤销上次同步与已读资格', () => {
    expect(screen).toContain('JSON.stringify([deviceId, sessionId, notificationResponse])');
    const resetStart = screen.indexOf('if (prevReadAckVisitKey !== readAckVisitKey) {');
    expect(resetStart).toBeGreaterThanOrEqual(0);
    const reset = screen.slice(resetStart, screen.indexOf('\n  }', resetStart));
    expect(reset).toContain('setReadAckSyncedKey(null)');
    expect(reset).toContain('setSessionMetadataSyncedKey(null)');
    expect(reset).toContain('setContentSyncedKey(null)');
    expect(reset).toContain('readAckGateGenRef.current += 1');
  });

  it('同步失败保留页面错误并交给协调器向权威重读调用方传播', () => {
    const syncStart = screen.indexOf('const syncSession = useCallback');
    const syncEnd = screen.indexOf('const remoteSyncContextKey', syncStart);
    const sync = screen.slice(syncStart, syncEnd);
    expect(sync).toMatch(/latchOutboxTransportHold\(formatted\);[\s\S]*?throw err;/);
    expect(sync).not.toContain('syncRun.cancel()');
    expect(screen).toContain("await requestSync({ reason: 'rewind-commit', replaceMessages: true })");
  });

  it('详情读取在请求开始捕获 authority,并在所有消息写入口提交', () => {
    expect(screen).toContain('const messageAuthority = remoteSessionStore.captureSessionMessageAuthority(sessionId);');
    expect(screen).toContain('remoteSessionStore.isSessionMessageAuthorityCurrent(messageAuthority)');
    expect(screen).toContain('remoteSessionStore.setMessages(sessionId, historyPage, { authority: messageAuthority });');
    expect(screen).toMatch(/authority: messageAuthority,\s+moreBeyondWindow,/);
    expect(screen).toContain('{ authority: messageAuthority },\n        );');
    expect(screen).toMatch(/remoteSessionStore\.mergeEarlierMessages\(sessionId, pageList, \{\s*authority: messageAuthority,\s*before,\s*\}\)/);
    expect(screen).toContain('remoteSessionStore.mergeMessages(sessionIdAtStart, rows, { authority: messageAuthority });');
    // First open and reopen now share the same authority-fenced history read.
    expect(screen).toContain('syncSessionMessageWindow({');
    // 第 4 处是重连补读(bridgeLatestMessagePage)沿 before 的往前读,结果仍经同一 authority 的
    // setLatestMessageWindow 提交。
    expect(screen.match(/maker\.listMessages\(/g)).toHaveLength(4);
  });

  it('schedule 关闭翻历史入口,页面工作租约覆盖发送与附件状态', () => {
    expect(screen).toContain('if (isScheduleDetail) return;');
    expect(screen).toContain('hasOlderMessages && !isScheduleDetail');
    expect(screen).toMatch(/const canLoadEarlierHistory = \(historyView\.snapshot\.ready \? historyView\.snapshot\.hasMore : hasOlderMessages && messages\.length > 0\)\s*&& !isScheduleDetail;/);
    expect(screen).toContain('canLoadEarlier={canLoadEarlierHistory}');
    expect(screen).toContain('enabled: appStateActive && remoteHistoryAvailable && !isScheduleDetail,');
    expect(screen).toContain('remoteSessionStore.acquireSessionMessageWork(sessionId, pageHasMessageWork)');
    const bridge = source('src/session/MobileOutboxBridge.tsx');
    // Lease acquisition/release behavior is exercised through the production
    // adapter in newSessionCreation.test.ts. This guard protects effect cleanup.
    expect(bridge).toContain('const messageLeases = createOutboxMessageLeases({');
    expect(bridge).toContain('store: remoteSessionStore,');
    expect(bridge).toContain('messageLeases.refresh();');
    expect(bridge).toContain('messageLeases.dispose();');
    expect(screen).toContain('remoteSessionStore.acquireSessionMessageWork(sessionId, true)');
    expect(screen.match(/messageWorkLease\.release\(\);/g)).toHaveLength(1);
    for (const signal of [
      'outboxItems.length > 0',
      'pendingUploads.length > 0',
      'pastePlaceholderCount > 0',
      'sendInFlightRef.current',
      'sendingQueueClientIds.size > 0',
      'settlingQueueItems.length > 0',
      'attachments.length > 0',
    ]) {
      expect(screen).toContain(signal);
    }
  });

  it('断线补读区分已进入详情与从未打开的 regular,跨代 rewind 不再无条件写正文', () => {
    expect(deviceLink).toContain('remoteSessionStore.hasSessionMessageDetailEntered(sessionId)');
    expect(deviceLink).toContain('? remoteSessionStore.captureSessionMessageAuthority(sessionId)');
    expect(deviceLink).toContain('remoteSessionStore.captureUnenteredSessionMessageAuthority(sessionId)');
    expect(deviceLink).toContain('authority: messageAuthorityAtRequestStart');
    expect(deviceLink).toMatch(/remoteSessionStore.canCommitUnenteredSessionMessageWindow\(\s+unenteredMessageAuthorityAtRequestStart,/);
    expect(deviceLink).toContain('remoteSessionStore.setLatestMessageWindow(sessionId, value, windowOptions);');
    expect(screen).toContain('remoteSessionStore.invalidateSessionMessageWindow(sessionId, deviceId);');
    expect(screen).not.toContain('remoteSessionStore.setMessages(sessionId, Array.isArray(history.messages)');
  });

  it('缓存 hydrate 同时校验页面 authority 与缓存 key epoch', () => {
    const store = source('src/session/remoteSessionStore.ts');
    expect(store).toContain('const cacheAuthority = captureSessionMessageCacheWriteAuthority(deviceId, sessionId);');
    expect(store).toContain('isSessionMessageCacheWriteAuthorityCurrent(cacheAuthority)');
    expect(store).toContain('remoteSessionStore.isSessionMessageAuthorityCurrent(authority)');
  });

  it('只在任务切换或页面卸载时最终释放媒体队列', () => {
    const queueStart = screen.indexOf('const releaseRemoteMediaQueue = useCallback');
    expect(queueStart).toBeGreaterThanOrEqual(0);
    const queueEnd = screen.indexOf('const backfillRunSeqRef', queueStart);
    const queueBlock = screen.slice(queueStart, queueEnd);
    expect(queueBlock).toContain('remoteMediaQueueRef.current?.releaseAll()');
    expect(queueBlock).toContain('remoteMediaQueueRef.current = null;');
    expect(queueBlock).toContain('[releaseRemoteMediaQueue, sessionId]');
    expect(queueBlock).toContain('releaseRemoteMediaQueue();');
    // 失焦或退后台时组件仍挂载且持有 resolved URL,不能提前 DELETE OSS。
    expect(queueBlock).not.toContain('useFocusEffect(');
    expect(queueBlock).not.toContain("AppState.addEventListener('change'");
  });
});