feat: support fast worktree-backed session flows
Add a directory-created fast path for worktree creation so session and send flows can continue once the target directory exists while Git attachment and bootstrap finish in the background. Track bootstrap status explicitly in shared UI contracts, including pending, ready, and failed states. Background watchers now surface failures and timeouts, update stored worktree metadata, and keep web and VS Code runtime behavior in parity. Move GitHub issue/PR worktree sessions and assistant-answer fork sessions onto the unified send path so provider, model, agent, and variant selections are preserved. The assistant-answer fork dialog can optionally create a worktree outside VS Code. Make worktree deletion dialogs close after linked-session cleanup while removing the worktree in the background, and clean up failed fast-create artifacts safely without recursively deleting user or agent-written files. Validation: bun test packages/ui/src/lib/worktrees/worktreeBootstrap.test.ts packages/ui/src/lib/worktrees/worktreeManager.test.ts; bun run type-check; bun run lint.
This commit is contained in:
@@ -1,7 +1,27 @@
|
||||
import { beforeEach, describe, expect, mock, test } from 'bun:test';
|
||||
|
||||
const bootstrapStatusCalls: string[] = [];
|
||||
let bootstrapStatusResult = { status: 'ready' as const, error: null, updatedAt: 1 };
|
||||
let bootstrapStatusResult: { status: 'pending' | 'ready' | 'failed'; error: string | null; updatedAt: number } = {
|
||||
status: 'ready',
|
||||
error: null,
|
||||
updatedAt: 1,
|
||||
};
|
||||
const toastErrors: Array<{ title: string; description?: string }> = [];
|
||||
|
||||
mock.module('@/components/ui', () => ({
|
||||
toast: {
|
||||
error: (title: string, options?: { description?: string }) => {
|
||||
toastErrors.push({ title, description: options?.description });
|
||||
},
|
||||
},
|
||||
}));
|
||||
|
||||
mock.module('@/lib/i18n', () => ({
|
||||
formatMessage: (_dictionary: Record<string, string>, key: string) => key,
|
||||
useI18nStore: {
|
||||
getState: () => ({ dictionary: {} }),
|
||||
},
|
||||
}));
|
||||
|
||||
mock.module('@/contexts/runtimeAPIRegistry', () => ({
|
||||
getRegisteredRuntimeAPIs: () => ({
|
||||
@@ -25,13 +45,24 @@ mock.module('@/lib/gitApiHttp', () => ({
|
||||
|
||||
const {
|
||||
clearWorktreeBootstrapState,
|
||||
getWorktreeBootstrapState,
|
||||
markWorktreeBootstrapPending,
|
||||
startWorktreeBootstrapWatcher,
|
||||
waitForWorktreeBootstrap,
|
||||
} = await import('./worktreeBootstrap');
|
||||
|
||||
const waitFor = async (predicate: () => boolean): Promise<void> => {
|
||||
for (let attempt = 0; attempt < 20; attempt += 1) {
|
||||
if (predicate()) return;
|
||||
await new Promise((resolve) => setTimeout(resolve, 0));
|
||||
}
|
||||
throw new Error('Timed out waiting for condition');
|
||||
};
|
||||
|
||||
describe('worktreeBootstrap.waitForWorktreeBootstrap', () => {
|
||||
beforeEach(() => {
|
||||
bootstrapStatusCalls.length = 0;
|
||||
toastErrors.length = 0;
|
||||
bootstrapStatusResult = { status: 'ready', error: null, updatedAt: 1 };
|
||||
clearWorktreeBootstrapState('/repo');
|
||||
clearWorktreeBootstrapState('/repo-wt');
|
||||
@@ -50,4 +81,61 @@ describe('worktreeBootstrap.waitForWorktreeBootstrap', () => {
|
||||
|
||||
expect(bootstrapStatusCalls).toEqual(['/repo-wt']);
|
||||
});
|
||||
|
||||
test('background watcher polls pending worktrees without blocking', async () => {
|
||||
markWorktreeBootstrapPending('/repo-wt');
|
||||
const readyStatuses: Array<{ status: 'pending' | 'ready' | 'failed'; error: string | null; updatedAt: number }> = [];
|
||||
|
||||
startWorktreeBootstrapWatcher('/repo-wt', {
|
||||
pollIntervalMs: 0,
|
||||
onReady: (status) => readyStatuses.push(status),
|
||||
});
|
||||
|
||||
await waitFor(() => readyStatuses.length === 1);
|
||||
expect(bootstrapStatusCalls).toEqual(['/repo-wt']);
|
||||
expect(readyStatuses.map((status) => status.status)).toEqual(['ready']);
|
||||
expect(toastErrors).toEqual([]);
|
||||
});
|
||||
|
||||
test('background watcher shows a toast when bootstrap fails', async () => {
|
||||
bootstrapStatusResult = { status: 'failed', error: 'setup failed', updatedAt: 2 };
|
||||
markWorktreeBootstrapPending('/repo-wt');
|
||||
|
||||
startWorktreeBootstrapWatcher('/repo-wt', { pollIntervalMs: 0 });
|
||||
|
||||
await waitFor(() => toastErrors.length === 1);
|
||||
expect(toastErrors).toEqual([{ title: 'worktree.bootstrap.toast.failed', description: 'setup failed' }]);
|
||||
});
|
||||
|
||||
test('background watcher marks failed and toasts when bootstrap times out', async () => {
|
||||
bootstrapStatusResult = { status: 'pending', error: null, updatedAt: 2 };
|
||||
markWorktreeBootstrapPending('/repo-wt');
|
||||
const failedStatuses: Array<{ status: 'pending' | 'ready' | 'failed'; error: string | null; updatedAt: number }> = [];
|
||||
|
||||
startWorktreeBootstrapWatcher('/repo-wt', {
|
||||
timeoutMs: 0,
|
||||
pollIntervalMs: 0,
|
||||
onFailed: (status) => failedStatuses.push(status),
|
||||
});
|
||||
|
||||
await waitFor(() => toastErrors.length === 1);
|
||||
expect(getWorktreeBootstrapState('/repo-wt')?.status).toBe('failed');
|
||||
expect(failedStatuses.map((status) => status.status)).toEqual(['failed']);
|
||||
expect(toastErrors).toEqual([{
|
||||
title: 'worktree.bootstrap.toast.failed',
|
||||
description: 'worktree.bootstrap.toast.timeoutDescription',
|
||||
}]);
|
||||
});
|
||||
|
||||
test('background watcher is deduped per directory', async () => {
|
||||
bootstrapStatusResult = { status: 'pending', error: null, updatedAt: 2 };
|
||||
markWorktreeBootstrapPending('/repo-wt');
|
||||
|
||||
startWorktreeBootstrapWatcher('/repo-wt', { pollIntervalMs: 1000 });
|
||||
startWorktreeBootstrapWatcher('/repo-wt', { pollIntervalMs: 1000 });
|
||||
|
||||
await waitFor(() => bootstrapStatusCalls.length === 1);
|
||||
expect(bootstrapStatusCalls).toEqual(['/repo-wt']);
|
||||
clearWorktreeBootstrapState('/repo-wt');
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user