File size: 8,840 Bytes
1f21206 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 | import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
const { branchMock, createMock, listMock } = vi.hoisted(() => ({
branchMock: vi.fn(),
createMock: vi.fn(),
listMock: vi.fn(),
}))
vi.mock('../api/sessions', () => ({
sessionsApi: {
branch: branchMock,
create: createMock,
list: listMock,
delete: vi.fn(),
rename: vi.fn(),
},
}))
import { useSessionStore } from './sessionStore'
import { useTabStore } from './tabStore'
const initialState = useSessionStore.getState()
function delay(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms))
}
function createDeferred<T>() {
let resolve!: (value: T) => void
const promise = new Promise<T>((res) => {
resolve = res
})
return { promise, resolve }
}
describe('sessionStore', () => {
beforeEach(() => {
branchMock.mockReset()
createMock.mockReset()
listMock.mockReset()
useSessionStore.setState({
...initialState,
sessions: [],
activeSessionId: null,
isLoading: false,
error: null,
})
useTabStore.setState({ tabs: [], activeTabId: null })
})
afterEach(() => {
useSessionStore.setState(initialState)
useTabStore.setState({ tabs: [], activeTabId: null })
})
it('returns a new session id before the background refresh completes', async () => {
createMock.mockResolvedValue({ sessionId: 'session-optimistic-1' })
listMock.mockImplementation(() => new Promise(() => {}))
const result = await Promise.race([
useSessionStore.getState().createSession('D:/workspace/code/myself_code/cc-haha'),
delay(100).then(() => 'timed-out'),
])
expect(result).toBe('session-optimistic-1')
expect(useSessionStore.getState().activeSessionId).toBe('session-optimistic-1')
expect(useSessionStore.getState().sessions[0]).toMatchObject({
id: 'session-optimistic-1',
title: 'New Session',
workDir: 'D:/workspace/code/myself_code/cc-haha',
workDirExists: true,
})
expect(createMock).toHaveBeenCalledWith({
workDir: 'D:/workspace/code/myself_code/cc-haha',
})
expect(listMock).toHaveBeenCalledOnce()
})
it('keeps an optimistic local title when a background refresh still returns a placeholder', async () => {
const refresh = createDeferred<{
sessions: Array<{
id: string
title: string
createdAt: string
modifiedAt: string
messageCount: number
projectPath: string
workDir: string | null
workDirExists: boolean
}>
total: number
}>()
createMock.mockResolvedValue({ sessionId: 'session-title-1', workDir: '/workspace/project' })
listMock.mockReturnValue(refresh.promise)
await useSessionStore.getState().createSession('/workspace/project')
useSessionStore.getState().updateSessionTitle('session-title-1', '开始优化UI')
refresh.resolve({
sessions: [{
id: 'session-title-1',
title: 'Untitled Session',
createdAt: '2026-05-07T00:00:00.000Z',
modifiedAt: '2026-05-07T00:00:01.000Z',
messageCount: 0,
projectPath: '',
workDir: '/workspace/project',
workDirExists: true,
}],
total: 1,
})
await refresh.promise
await delay(0)
expect(useSessionStore.getState().sessions[0]?.title).toBe('开始优化UI')
})
it('syncs refreshed session titles into already-open tabs', async () => {
useTabStore.getState().openTab('session-title-2', '```json {"title":')
listMock.mockResolvedValue({
sessions: [{
id: 'session-title-2',
title: '使用bash写一个shell,随便写点什么东西',
createdAt: '2026-05-07T00:00:00.000Z',
modifiedAt: '2026-05-07T00:00:01.000Z',
messageCount: 3,
projectPath: '',
workDir: '/workspace/project',
workDirExists: true,
}],
total: 1,
})
await useSessionStore.getState().fetchSessions()
expect(useTabStore.getState().tabs[0]?.title).toBe('使用bash写一个shell,随便写点什么东西')
})
it('forwards direct branch switch repository options when creating a session', async () => {
createMock.mockResolvedValue({ sessionId: 'session-branch-switch', workDir: '/workspace/repo' })
listMock.mockImplementation(() => new Promise(() => {}))
await useSessionStore.getState().createSession('/workspace/repo', {
repository: { branch: 'feature/rail', worktree: false },
})
expect(createMock).toHaveBeenCalledWith({
workDir: '/workspace/repo',
repository: { branch: 'feature/rail', worktree: false },
})
})
it('forwards isolated worktree repository options when creating a session', async () => {
createMock.mockResolvedValue({
sessionId: 'session-worktree-launch',
workDir: '/workspace/repo/.claude/worktrees/desktop-feature-rail-12345678',
})
listMock.mockImplementation(() => new Promise(() => {}))
await useSessionStore.getState().createSession('/workspace/repo', {
repository: { branch: 'feature/rail', worktree: true },
})
expect(createMock).toHaveBeenCalledWith({
workDir: '/workspace/repo',
repository: { branch: 'feature/rail', worktree: true },
})
expect(useSessionStore.getState().sessions[0]?.workDir)
.toBe('/workspace/repo/.claude/worktrees/desktop-feature-rail-12345678')
})
it('returns the branched session before the background refresh completes', async () => {
branchMock.mockResolvedValue({
sessionId: 'session-branch-1',
title: 'Branch from here',
workDir: '/workspace/repo/branches/session-branch-1',
sourceSessionId: 'session-source-1',
targetMessageId: 'transcript-message-1',
})
listMock.mockImplementation(() => new Promise(() => {}))
useSessionStore.setState({
sessions: [{
id: 'session-source-1',
title: 'Source session',
createdAt: '2026-05-19T00:00:00.000Z',
modifiedAt: '2026-05-19T00:00:00.000Z',
messageCount: 4,
projectPath: '/workspace/repo',
projectRoot: '/workspace/repo',
workDir: '/workspace/repo',
workDirExists: true,
}],
})
const result = await Promise.race([
useSessionStore.getState().branchSession('session-source-1', 'transcript-message-1'),
delay(100).then(() => 'timed-out'),
])
expect(result).toMatchObject({
sessionId: 'session-branch-1',
title: 'Branch from here',
workDir: '/workspace/repo/branches/session-branch-1',
})
expect(branchMock).toHaveBeenCalledWith('session-source-1', {
targetMessageId: 'transcript-message-1',
})
expect(useSessionStore.getState().activeSessionId).toBe('session-branch-1')
expect(useSessionStore.getState().sessions[0]).toMatchObject({
id: 'session-branch-1',
title: 'Branch from here',
projectPath: '/workspace/repo',
workDir: '/workspace/repo/branches/session-branch-1',
projectRoot: '/workspace/repo',
workDirExists: true,
})
expect(listMock).toHaveBeenCalledOnce()
})
it('updates an existing optimistic branch row when the branch session id is already present', async () => {
branchMock.mockResolvedValue({
sessionId: 'session-branch-existing',
title: 'Updated branch',
workDir: '/workspace/repo/branches/session-branch-existing',
sourceSessionId: 'session-source-1',
targetMessageId: 'transcript-message-2',
})
listMock.mockImplementation(() => new Promise(() => {}))
useSessionStore.setState({
sessions: [
{
id: 'session-branch-existing',
title: 'Old branch title',
createdAt: '2026-05-18T00:00:00.000Z',
modifiedAt: '2026-05-18T00:00:00.000Z',
messageCount: 3,
projectPath: '/workspace/old',
projectRoot: '/workspace/old',
workDir: '/workspace/old',
workDirExists: true,
},
{
id: 'session-source-1',
title: 'Source session',
createdAt: '2026-05-19T00:00:00.000Z',
modifiedAt: '2026-05-19T00:00:00.000Z',
messageCount: 4,
projectPath: '/workspace/repo',
projectRoot: '/workspace/repo',
workDir: '/workspace/repo',
workDirExists: true,
},
],
})
await useSessionStore.getState().branchSession('session-source-1', 'transcript-message-2')
expect(useSessionStore.getState().sessions).toHaveLength(2)
expect(useSessionStore.getState().sessions[0]).toMatchObject({
id: 'session-branch-existing',
title: 'Updated branch',
projectPath: '/workspace/repo',
projectRoot: '/workspace/repo',
workDir: '/workspace/repo/branches/session-branch-existing',
})
})
})
|