🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
import { act, renderHook } from '@testing-library/react';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { deviceKeys } from '@/libs/swr/keys';
import { usePullRequestActions } from '../usePullRequestActions';
const runPullRequestAction = vi.hoisted(() => vi.fn());
const pushGitBranch = vi.hoisted(() => vi.fn());
const mutate = vi.hoisted(() => vi.fn().mockResolvedValue(undefined));
const toastError = vi.hoisted(() => vi.fn());
vi.mock('@/services/git', () => ({ gitService: { pushGitBranch, runPullRequestAction } }));
const revalidateGitStatus = vi.hoisted(() => vi.fn().mockResolvedValue(undefined));
vi.mock('@/libs/swr', () => ({ mutate }));
vi.mock('@/store/device/gitStatus/store', () => ({
useGitStatusStore: { getState: () => ({ revalidateGitStatus }) },
}));
vi.mock('@lobehub/ui', () => ({ toast: { error: toastError } }));
const params = { deviceId: 'dev-1', number: 7, workingDirectory: '/repo' };
const deferred = <T>() => {
let resolve!: (value: T) => void;
const promise = new Promise<T>((r) => {
resolve = r;
});
return { promise, resolve };
};
describe('usePullRequestActions', () => {
beforeEach(() => {
runPullRequestAction.mockReset();
pushGitBranch.mockReset();
mutate.mockClear();
revalidateGitStatus.mockReset().mockResolvedValue(undefined);
toastError.mockReset();
});
// Regression: the pane enabled close / merge / comment on a PR painted from
// persisted rows, so a click before the live reads settled could mutate a PR
// the branch had already moved off.
it('refuses every mutation while locked, and runs once unlocked', async () => {
runPullRequestAction.mockResolvedValue({ success: true });
pushGitBranch.mockResolvedValue({ success: true });
const mutateDetail = vi.fn().mockResolvedValue(undefined);
const { rerender, result } = renderHook(
({ locked }) => usePullRequestActions({ ...params, locked, mutateDetail }),
{ initialProps: { locked: true } },
);
await act(async () => {
expect(await result.current.run({ type: 'close' })).toBe(false);
expect(await result.current.push()).toBe(false);
});
expect(runPullRequestAction).not.toHaveBeenCalled();
expect(pushGitBranch).not.toHaveBeenCalled();
expect(result.current.busy).toBeUndefined();
rerender({ locked: false });
await act(async () => {
expect(await result.current.run({ type: 'close' })).toBe(true);
});
expect(runPullRequestAction).toHaveBeenCalledTimes(1);
});
it('lets comments through but refuses policy actions while the merge context is stale', async () => {
runPullRequestAction.mockResolvedValue({ success: true });
const mutateDetail = vi.fn().mockResolvedValue(undefined);
const { result } = renderHook(() =>
usePullRequestActions({ ...params, mutateDetail, policyLocked: true }),
);
await act(async () => {
expect(
await result.current.run({ headRefOid: 'a'.repeat(40), method: 'squash', type: 'merge' }),
).toBe(false);
expect(await result.current.run({ body: 'hi', type: 'comment' })).toBe(true);
});
expect(runPullRequestAction).toHaveBeenCalledTimes(1);
expect(runPullRequestAction.mock.calls[0][0].action.type).toBe('comment');
});
it('marks busy during the action and clears it afterwards', async () => {
const pending = deferred<{ success: boolean }>();
runPullRequestAction.mockReturnValue(pending.promise);
const { result } = renderHook(() =>
usePullRequestActions({ ...params, mutateDetail: vi.fn().mockResolvedValue(undefined) }),
);
let run!: Promise<boolean>;
act(() => {
run = result.current.run({
admin: true,
headRefOid: 'a'.repeat(40),
method: 'squash',
type: 'merge',
});
});
expect(result.current.busy).toBe('merge');
expect(runPullRequestAction).toHaveBeenCalledWith({
action: { admin: true, headRefOid: 'a'.repeat(40), method: 'squash', type: 'merge' },
deviceId: 'dev-1',
number: 7,
path: '/repo',
});
await act(async () => {
pending.resolve({ success: true });
await run;
});
expect(result.current.busy).toBeUndefined();
});
it('revalidates detail, linked PR, ahead/behind and working tree on success', async () => {
runPullRequestAction.mockResolvedValue({ success: true });
const mutateDetail = vi.fn().mockResolvedValue(undefined);
const { result } = renderHook(() => usePullRequestActions({ ...params, mutateDetail }));
await act(async () => {
expect(await result.current.run({ type: 'ready' })).toBe(true);
});
expect(mutateDetail).toHaveBeenCalledTimes(1);
expect(mutate).toHaveBeenCalledWith(deviceKeys.gitPullRequestActivity('dev-1', '/repo', 7));
expect(revalidateGitStatus).toHaveBeenCalledWith('dev-1', '/repo', [
'aheadBehind',
'workingTreeStatus',
]);
});
it('targets the confirmed PR and reports success even if the worktree disappeared', async () => {
runPullRequestAction.mockResolvedValue({ success: true });
revalidateGitStatus.mockRejectedValueOnce(new Error('Worktree no longer exists'));
const url = 'https://github.com/owner/repo/pull/7';
const { result } = renderHook(() =>
usePullRequestActions({
...params,
mutateDetail: vi.fn().mockResolvedValue(undefined),
repositoryPath: '/registered-source',
url,
workingDirectory: '/removed-worktree',
}),
);
await act(async () => {
expect(await result.current.run({ type: 'ready' })).toBe(true);
});
expect(result.current.error).toBeUndefined();
expect(runPullRequestAction).toHaveBeenCalledWith({
action: { type: 'ready' },
deviceId: 'dev-1',
number: 7,
path: '/removed-worktree',
repository: { path: '/registered-source', url },
});
expect(mutate).toHaveBeenCalledWith(
deviceKeys.gitPullRequestActivity('dev-1', '/registered-source', 7),
);
});
it('keeps dock-action failures as error and retries the same action', async () => {
runPullRequestAction.mockResolvedValueOnce({ error: 'base moved', success: false });
const { result } = renderHook(() =>
usePullRequestActions({ ...params, mutateDetail: vi.fn().mockResolvedValue(undefined) }),
);
await act(async () => {
expect(
await result.current.run({ headRefOid: 'a'.repeat(40), method: 'rebase', type: 'merge' }),
).toBe(false);
});
expect(result.current.error).toBe('base moved');
expect(mutate).not.toHaveBeenCalled();
expect(toastError).not.toHaveBeenCalled();
runPullRequestAction.mockResolvedValueOnce({ success: true });
await act(async () => {
result.current.retry();
});
expect(runPullRequestAction).toHaveBeenLastCalledWith(
expect.objectContaining({
action: { headRefOid: 'a'.repeat(40), method: 'rebase', type: 'merge' },
}),
);
expect(result.current.error).toBeUndefined();
});
it('toasts non-dock failures without touching the dock error', async () => {
runPullRequestAction.mockRejectedValueOnce(new Error('gh exploded'));
const { result } = renderHook(() =>
usePullRequestActions({ ...params, mutateDetail: vi.fn().mockResolvedValue(undefined) }),
);
await act(async () => {
expect(await result.current.run({ body: 'hi', type: 'comment' })).toBe(false);
});
expect(toastError).toHaveBeenCalledWith('gh exploded');
expect(result.current.error).toBeUndefined();
});
it('ignores re-entry while an action is in flight', async () => {
const pending = deferred<{ success: boolean }>();
runPullRequestAction.mockReturnValue(pending.promise);
pushGitBranch.mockResolvedValue({ success: true });
const { result } = renderHook(() =>
usePullRequestActions({ ...params, mutateDetail: vi.fn().mockResolvedValue(undefined) }),
);
let first!: Promise<boolean>;
let second!: Promise<boolean>;
act(() => {
first = result.current.run({ type: 'ready' });
second = result.current.push();
});
expect(await second).toBe(false);
expect(pushGitBranch).not.toHaveBeenCalled();
await act(async () => {
pending.resolve({ success: true });
await first;
});
expect(result.current.busy).toBeUndefined();
});
});