|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2026 Autohand AI LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | +import { mkdtemp, rm } from 'node:fs/promises'; |
| 7 | +import os from 'node:os'; |
| 8 | +import path from 'node:path'; |
| 9 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 10 | +import { cloneSession, forkSession, sessionTree } from '../../src/commands/sessionBranching.js'; |
| 11 | +import { setFeatureState } from '../../src/features/featureRegistry.js'; |
| 12 | +import { SessionManager, type Session } from '../../src/session/SessionManager.js'; |
| 13 | +import type { SlashCommandContext } from '../../src/core/slashCommandTypes.js'; |
| 14 | +import type { LoadedConfig } from '../../src/types.js'; |
| 15 | + |
| 16 | +describe('session branching user stories', () => { |
| 17 | + let tempDir: string; |
| 18 | + let manager: SessionManager; |
| 19 | + |
| 20 | + beforeEach(async () => { |
| 21 | + tempDir = await mkdtemp(path.join(os.tmpdir(), 'autohand-session-branching-story-')); |
| 22 | + manager = new SessionManager(tempDir); |
| 23 | + await manager.initialize(); |
| 24 | + }); |
| 25 | + |
| 26 | + afterEach(async () => { |
| 27 | + await rm(tempDir, { recursive: true, force: true }); |
| 28 | + }); |
| 29 | + |
| 30 | + it('lets a user enable fork and clone, branch from a real session, clone the branch, then inspect the tree', async () => { |
| 31 | + const config: LoadedConfig = { |
| 32 | + configPath: path.join(tempDir, 'config.json'), |
| 33 | + provider: 'openrouter', |
| 34 | + }; |
| 35 | + expect(setFeatureState(config, 'experimental_fork', true).ok).toBe(true); |
| 36 | + expect(setFeatureState(config, 'experimental_clone', true).ok).toBe(true); |
| 37 | + |
| 38 | + const source = await manager.createSession('/workspace/project', 'test-model'); |
| 39 | + await source.append({ role: 'user', content: 'Build the first version', timestamp: '2026-01-01T00:00:00.000Z' }); |
| 40 | + await source.append({ role: 'assistant', content: 'First version done', timestamp: '2026-01-01T00:00:01.000Z' }); |
| 41 | + await source.append({ role: 'user', content: 'Try the risky alternative', timestamp: '2026-01-01T00:00:02.000Z' }); |
| 42 | + await source.append({ role: 'assistant', content: 'Alternative done', timestamp: '2026-01-01T00:00:03.000Z' }); |
| 43 | + |
| 44 | + const restoredSessions: string[] = []; |
| 45 | + const makeContext = (currentSession?: Session): SlashCommandContext => ({ |
| 46 | + promptModelSelection: vi.fn(), |
| 47 | + createAgentsFile: vi.fn(), |
| 48 | + resetConversation: vi.fn(), |
| 49 | + sessionManager: manager, |
| 50 | + currentSession, |
| 51 | + memoryManager: {} as SlashCommandContext['memoryManager'], |
| 52 | + permissionManager: {} as SlashCommandContext['permissionManager'], |
| 53 | + llm: {} as SlashCommandContext['llm'], |
| 54 | + workspaceRoot: '/workspace/project', |
| 55 | + model: 'test-model', |
| 56 | + config, |
| 57 | + restoreSession: async (sessionId: string) => { |
| 58 | + restoredSessions.push(sessionId); |
| 59 | + }, |
| 60 | + }); |
| 61 | + |
| 62 | + const forkOutput = await forkSession(makeContext(source), ['2']); |
| 63 | + const forked = manager.getCurrentSession(); |
| 64 | + expect(forked).toBeDefined(); |
| 65 | + expect(forkOutput).toContain('Forked session'); |
| 66 | + expect(forked?.getMessages().map((message) => message.content)).toEqual([ |
| 67 | + 'Build the first version', |
| 68 | + 'First version done', |
| 69 | + 'Try the risky alternative', |
| 70 | + ]); |
| 71 | + |
| 72 | + const cloneOutput = await cloneSession(makeContext(forked)); |
| 73 | + const cloned = manager.getCurrentSession(); |
| 74 | + expect(cloned).toBeDefined(); |
| 75 | + expect(cloneOutput).toContain('Cloned session'); |
| 76 | + expect(cloned?.getMessages()).toEqual(forked?.getMessages()); |
| 77 | + |
| 78 | + const treeOutput = await sessionTree(makeContext(cloned)); |
| 79 | + expect(treeOutput).toContain(source.metadata.sessionId); |
| 80 | + expect(treeOutput).toContain(forked!.metadata.sessionId); |
| 81 | + expect(treeOutput).toContain(cloned!.metadata.sessionId); |
| 82 | + expect(treeOutput).toContain('fork at user message 2'); |
| 83 | + expect(restoredSessions).toEqual([ |
| 84 | + forked!.metadata.sessionId, |
| 85 | + cloned!.metadata.sessionId, |
| 86 | + ]); |
| 87 | + }); |
| 88 | +}); |
0 commit comments