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。

15.1 KB
Raw Blame
// @vitest-environment jsdom

import { createElement, type ReactNode } from 'react';
import { renderHook, waitFor } from '@testing-library/react';
import { afterEach, describe, expect, it, vi } from 'vitest';

import type { Session } from '@/lib/ccAgent.types';

vi.mock('@/contexts/WorktreeContext', () => ({
  useWorktreeForSession: () => null,
}));
vi.mock('@/features/device-link/stickySessionOrigin', () => ({
  getStickySessionDeviceId: () => undefined,
}));
// PR 缓存统一后 hook 消费 PrRefsProvider;provider 需要 owner id 才开始加载。
vi.mock('@/contexts/AuthContext', () => ({
  useAuth: () => ({ dataOwnerId: 'owner-1' }),
}));

import { useSessionGitContext } from '../useSessionGitContext';
import { PrRefsProvider } from '@/contexts/PrRefsContext';
import { remoteProjectsStore } from '@/features/device-link/remoteProjectsStore';

/** 统一管线经 PrRefsProvider 拉取 PR;hook 测试统一套一层 provider。 */
const wrapper = ({ children }: { children: ReactNode }) =>
  createElement(PrRefsProvider, null, children);

const sessionBase: Session = {
  id: 'session-1',
  userId: 'user-1',
  title: 'Remote task',
  workingDir: '/remote/project',
  workspaceKind: 'project',
  model: 'model',
  effort: 'medium',
  permissionMode: 'default',
  sdkSessionId: null,
  totalTokenUsage: 0,
  totalCostUsd: 0,
  contextTokens: 0,
  contextWindow: 1,
  fastMode: false,
  clearedAt: null,
  pinnedAt: null,
  userSendAt: null,
  status: 'active',
  agentKind: 'codex',
  extraDirs: [],
  remoteHostId: null,
  createdAt: '2026-01-01T00:00:00.000Z',
  updatedAt: '2026-01-01T00:00:00.000Z',
};

function makeGitContext() {
  return {
    getForSession: vi.fn(),
    watch: vi.fn().mockResolvedValue(undefined),
    unwatch: vi.fn().mockResolvedValue(undefined),
    onChanged: vi.fn(() => () => undefined),
    listPrRefs: vi.fn(),
    // PrRefsProvider 挂载即全量加载本地引用(统一缓存管线)。
    listAllPrRefs: vi.fn().mockResolvedValue([]),
    getPrStatuses: vi.fn(),
    onPrRefsChanged: vi.fn(() => () => undefined),
  };
}

afterEach(() => {
  vi.restoreAllMocks();
});

describe('useSessionGitContext remote routing', () => {
  it('SSH 会话在本机 main 侧解析远端 HEAD,不注册本地 watcher', async () => {
    const gitContext = makeGitContext();
    gitContext.getForSession.mockResolvedValue({
      workdir: '/srv/project',
      head: { kind: 'branch', branch: 'feature/ssh', shortSha: null },
      source: 'remote',
    });
    gitContext.listPrRefs.mockResolvedValue([]);
    gitContext.getPrStatuses.mockResolvedValue([]);
    window.electronAPI = {
      gitContext,
      deviceLink: { invoke: vi.fn() },
    } as never;

    const session = { ...sessionBase, remoteHostId: 'ssh-1' };
    const { result, unmount } = renderHook(() => useSessionGitContext(session), { wrapper });

    await waitFor(() => expect(result.current.head?.branch).toBe('feature/ssh'));
    expect(gitContext.getForSession).toHaveBeenCalledWith({
      sessionId: 'session-1',
      workingDir: '/remote/project',
      worktreePath: null,
      remoteHostId: 'ssh-1',
    });
    expect(gitContext.onChanged).not.toHaveBeenCalled();
    expect(gitContext.watch).not.toHaveBeenCalled();
    // SSH 会话的引用在本地 db,由统一缓存的全量加载覆盖(不再逐会话查询)。
    expect(gitContext.listAllPrRefs).toHaveBeenCalled();
    unmount();
    expect(gitContext.unwatch).not.toHaveBeenCalled();
  });

  it('device-link 会话把 HEAD、PR 引用和状态查询全部发往被控端', async () => {
    const gitContext = makeGitContext();
    const invoke = vi.fn(async (_deviceId: string, channel: string) => {
      if (channel === 'git-context:get-for-session') {
        return {
          workdir: '/controlled/repo',
          head: { kind: 'branch', branch: 'feature/device', shortSha: null },
          source: 'remote',
        };
      }
      if (channel === 'git-context:pr-refs:list') {
        return [
          {
            id: 'ref-1',
            sessionId: 'session-1',
            owner: 'octo',
            repo: 'repo',
            prNumber: 42,
            url: 'https://github.com/octo/repo/pull/42',
            firstSeenAt: 1,
            lastSeenAt: 2,
          },
        ];
      }
      return [
        {
          ok: true,
          owner: 'octo',
          repo: 'repo',
          prNumber: 42,
          status: 'open',
          title: 'Remote PR',
          htmlUrl: 'https://github.com/octo/repo/pull/42',
          branch: 'feature/device',
          unresolvedCount: 0,
        },
      ];
    });
    gitContext.getForSession.mockRejectedValue(new Error('must use device-link'));
    gitContext.listPrRefs.mockRejectedValue(new Error('must use device-link'));
    gitContext.getPrStatuses.mockRejectedValue(new Error('must use device-link'));
    window.electronAPI = {
      gitContext,
      deviceLink: { invoke },
    } as never;

    const session = { ...sessionBase, deviceLinkDeviceId: 'device-1', remoteHostId: 'ssh-1' };
    const { result, unmount } = renderHook(() => useSessionGitContext(session), { wrapper });

    await waitFor(() => {
      expect(result.current.head?.branch).toBe('feature/device');
      expect(result.current.prRefs).toHaveLength(1);
      expect(result.current.prStatuses.size).toBe(1);
    });
    expect(invoke).toHaveBeenCalledWith('device-1', 'git-context:get-for-session', [
      expect.objectContaining({ sessionId: 'session-1', remoteHostId: 'ssh-1' }),
    ]);
    expect(invoke).toHaveBeenCalledWith('device-1', 'git-context:pr-refs:list', ['session-1']);
    expect(invoke).toHaveBeenCalledWith('device-1', 'git-context:pr-status', [
      { sessionId: 'session-1', queries: [{ owner: 'octo', repo: 'repo', prNumber: 42 }] },
    ]);
    // 被控端引用/状态不得走本机 gitContext 直查(mockRejected 的三个方法未被吞错误)。
    expect(gitContext.onChanged).not.toHaveBeenCalled();
    // 注:onPrRefsChanged 由 PrRefsProvider 全局订阅一次(本地会话增量刷新),
    // 不再作为「device-link 不订阅本地事件」的判据;device-link 的隔离由
    // 「refs/状态经 deviceLink.invoke 而非本机 gitContext」两条断言保证。
    unmount();
    expect(gitContext.watch).not.toHaveBeenCalled();
    expect(gitContext.unwatch).not.toHaveBeenCalled();
  });

  it('切换任务或被控端断链时清掉旧的 Git / PR 展示', async () => {
    let disconnected = false;
    const gitContext = makeGitContext();
    const invoke = vi.fn(async (_deviceId: string, channel: string, args: unknown[]) => {
      if (disconnected) throw new Error('disconnected');
      if (channel === 'git-context:get-for-session') {
        return {
          workdir: '/controlled/repo',
          head: { kind: 'branch', branch: 'feature/device', shortSha: null },
          source: 'remote',
        };
      }
      if (channel === 'git-context:pr-refs:list') {
        return [
          {
            id: 'ref-1',
            sessionId: (args[0] as string) ?? 'session-1',
            owner: 'octo',
            repo: 'repo',
            prNumber: 42,
            url: 'https://github.com/octo/repo/pull/42',
            firstSeenAt: 1,
            lastSeenAt: 2,
          },
        ];
      }
      return [
        {
          ok: true,
          owner: 'octo',
          repo: 'repo',
          prNumber: 42,
          status: 'open',
          title: 'Remote PR',
          htmlUrl: 'https://github.com/octo/repo/pull/42',
          branch: 'feature/device',
          unresolvedCount: 0,
        },
      ];
    });
    window.electronAPI = {
      gitContext,
      deviceLink: { invoke },
    } as never;

    const first = { ...sessionBase, deviceLinkDeviceId: 'device-1' };
    const { result, rerender, unmount } = renderHook(
      ({ session }: { session: Session }) => useSessionGitContext(session),
      { initialProps: { session: first }, wrapper },
    );
    await waitFor(() => {
      expect(result.current.head?.branch).toBe('feature/device');
      expect(result.current.prRefs).toHaveLength(1);
    });

    disconnected = true;
    rerender({ session: { ...first, id: 'session-2' } });
    await waitFor(() => {
      expect(result.current.head).toBeNull();
      expect(result.current.prRefs).toHaveLength(0);
      expect(result.current.prStatuses.size).toBe(0);
    });
    unmount();
  });

  it.each([
    {
      label: 'Agent 进入仓库时显示分支',
      dir: {
        workdir: '/Users/me/repo',
        head: { kind: 'branch', branch: 'feature/dialogue', shortSha: null },
        source: 'telemetry',
      },
    },
    {
      label: '对话目录不是 git 仓库时分支为空',
      dir: { workdir: null, head: null, source: null },
    },
  ])('对话任务照常返回消息里的 PR:$label', async ({ dir }) => {
    const gitContext = makeGitContext();
    gitContext.getForSession.mockResolvedValue(dir);
    const prRef = {
      id: 'ref-1',
      sessionId: 'session-1',
      owner: 'octo',
      repo: 'repo',
      prNumber: 7,
      url: 'https://github.com/octo/repo/pull/7',
      firstSeenAt: 1,
      lastSeenAt: 2,
    };
    gitContext.listAllPrRefs.mockResolvedValue([prRef]);
    gitContext.listPrRefs.mockResolvedValue([prRef]);
    gitContext.getPrStatuses.mockResolvedValue([
      {
        ok: true,
        owner: 'octo',
        repo: 'repo',
        prNumber: 7,
        status: 'merged',
        title: 'Dialogue PR',
        htmlUrl: 'https://github.com/octo/repo/pull/7',
        branch: 'feature/x',
        unresolvedCount: 0,
      },
    ]);
    window.electronAPI = {
      gitContext,
      deviceLink: { invoke: vi.fn() },
    } as never;

    const session = { ...sessionBase, workspaceKind: 'dialogue' as const };
    const { result, unmount } = renderHook(() => useSessionGitContext(session), { wrapper });

    await waitFor(() => {
      expect(gitContext.getForSession).toHaveBeenCalled();
      expect(result.current.prRefs).toHaveLength(1);
      expect(result.current.prStatuses.size).toBe(1);
    });
    await waitFor(() => expect(result.current.head).toEqual(dir.head));
    if (dir.workdir) {
      expect(gitContext.watch).toHaveBeenCalledWith(dir.workdir);
    } else {
      expect(gitContext.watch).not.toHaveBeenCalled();
    }
    unmount();
  });

  it('切回看过的任务立即显示它自己上次的分支,不借用上一个任务的值', async () => {
    const gitContext = makeGitContext();
    const pending: Array<(value: unknown) => void> = [];
    gitContext.getForSession.mockImplementation(
      () => new Promise((resolve) => pending.push(resolve)),
    );
    gitContext.listPrRefs.mockResolvedValue([]);
    gitContext.getPrStatuses.mockResolvedValue([]);
    window.electronAPI = { gitContext, deviceLink: { invoke: vi.fn() } } as never;
    const branch = (name: string) => ({
      workdir: `/repo/${name}`,
      head: { kind: 'branch', branch: name, shortSha: null },
      source: 'worktree',
    });
    const a = { ...sessionBase, id: 'switch-a' };
    const b = { ...sessionBase, id: 'switch-b' };
    const { result, rerender, unmount } = renderHook(
      ({ session }: { session: Session }) => useSessionGitContext(session),
      { initialProps: { session: a }, wrapper },
    );
    pending.shift()!(branch('feature/a'));
    await waitFor(() => expect(result.current.head?.branch).toBe('feature/a'));

    // 第一次打开 B:解析回来前为空,不显示 A 的分支。
    rerender({ session: b });
    expect(result.current.head).toBeNull();
    pending.shift()!(branch('feature/b'));
    await waitFor(() => expect(result.current.head?.branch).toBe('feature/b'));

    // 切回 A:解析仍在途时已经显示 A 上次的分支。
    rerender({ session: a });
    expect(result.current.head?.branch).toBe('feature/a');
    expect(result.current.branchSource).toBe('worktree');
    pending.shift()!(branch('feature/a2'));
    await waitFor(() => expect(result.current.head?.branch).toBe('feature/a2'));
    unmount();
  });

  it('同一任务的依赖变化在新结果回来前保留当前分支', async () => {
    const gitContext = makeGitContext();
    const pending: Array<(value: unknown) => void> = [];
    gitContext.getForSession.mockImplementation(
      () => new Promise((resolve) => pending.push(resolve)),
    );
    gitContext.listPrRefs.mockResolvedValue([]);
    gitContext.getPrStatuses.mockResolvedValue([]);
    window.electronAPI = { gitContext, deviceLink: { invoke: vi.fn() } } as never;
    const session: Session = { ...sessionBase, id: 'deps-change', workingDir: null };
    const { result, rerender, unmount } = renderHook(
      ({ session }: { session: Session }) => useSessionGitContext(session),
      { initialProps: { session }, wrapper },
    );
    pending.shift()!({
      workdir: '/repo',
      head: { kind: 'branch', branch: 'main', shortSha: null },
      source: 'telemetry',
    });
    await waitFor(() => expect(result.current.head?.branch).toBe('main'));

    rerender({ session: { ...session, workingDir: '/repo' } });
    await waitFor(() => expect(gitContext.getForSession).toHaveBeenCalledTimes(2));
    expect(result.current.head?.branch).toBe('main');
    pending.shift()!({ workdir: '/repo', head: null, source: null });
    await waitFor(() => expect(result.current.head).toBeNull());
    unmount();
  });

  // 2026-08-13 用户裁决:设备明确断线时不发注定失败的 PR 隧道查询(fail-open:
  // shard 缺失照常尝试,见 prRefsRefreshGating.test.ts 的判定语义)。
  it('被控端标记断线时跳过 PR 引用的隧道查询,重连后恢复', async () => {
    const gitContext = makeGitContext();
    const invoke = vi.fn(async (_deviceId: string, channel: string) => {
      if (channel === 'git-context:get-for-session') {
        return {
          workdir: '/controlled/repo',
          head: { kind: 'branch', branch: 'feature/offline', shortSha: null },
          source: 'remote',
        };
      }
      return [];
    });
    window.electronAPI = {
      gitContext,
      deviceLink: { invoke },
    } as never;

    const session = { ...sessionBase, deviceLinkDeviceId: 'device-1' };
    try {
      // 断线快照:shard 仍在(会话行留在侧栏),连接标记为 disconnected。
      remoteProjectsStore.setDeviceSessions('device-1', 'Test Device', [session]);
      remoteProjectsStore.markDeviceDisconnected('device-1');

      const first = renderHook(() => useSessionGitContext(session), { wrapper });
      await waitFor(() => expect(first.result.current.head?.branch).toBe('feature/offline'));
      // HEAD 走独立通道不受门控;PR 引用的隧道查询被跳过。
      expect(invoke).not.toHaveBeenCalledWith('device-1', 'git-context:pr-refs:list', [
        'session-1',
      ]);
      first.unmount();

      // 重连(权威列表再次到达)→ 下一个触发点(这里用重挂载等价周期/聚焦刷新)恢复查询。
      remoteProjectsStore.setDeviceSessions('device-1', 'Test Device', [session]);
      const second = renderHook(() => useSessionGitContext(session), { wrapper });
      await waitFor(() => {
        expect(invoke).toHaveBeenCalledWith('device-1', 'git-context:pr-refs:list', ['session-1']);
      });
      second.unmount();
    } finally {
      // 清掉 shard,别让断线标记漂进同文件其它用例。
      remoteProjectsStore.removeDevice('device-1');
    }
  });
});