@@ -7,6 +7,11 @@ let createPromptSubmit: typeof import("./submit").createPromptSubmit |
7 | 7 |
|
8 | 8 | const createdClients: string[] = [] |
9 | 9 | const createdSessions: string[] = [] |
| 10 | +const sessionCreateInputs: Array<{ |
| 11 | + agent?: string |
| 12 | + model?: { id: string; providerID: string; variant?: string } |
| 13 | + location?: { directory: string } |
| 14 | +}> = [] |
10 | 15 | const enabledAutoAccept: Array<{ server: string; sessionID: string; directory: string }> = [] |
11 | 16 | const optimistic: Array<{ |
12 | 17 | directory?: string |
@@ -19,11 +24,15 @@ const optimistic: Array<{ |
19 | 24 | }> = [] |
20 | 25 | const optimisticSeeded: boolean[] = [] |
21 | 26 | const storedSessions: Record<string, Array<{ id: string; title?: string }>> = {} |
22 | | -const sessionDirectories: Record<string, string> = {} |
23 | 27 | const promoted: Array<{ directory: string; sessionID: string }> = [] |
24 | | -const sentShell: string[] = [] |
| 28 | +const sentShell: Array<{ sessionID: string; id?: string; command: string }> = [] |
25 | 29 | const syncedDirectories: string[] = [] |
26 | 30 | const promotedDrafts: Array<{ draftID: string; server: string; sessionId: string }> = [] |
| 31 | +const sentPrompts: string[] = [] |
| 32 | +const promptInputs: unknown[] = [] |
| 33 | +const sentCommands: unknown[] = [] |
| 34 | +const commands: Array<{ name: string }> = [] |
| 35 | +let serverSessionSyncs = 0 |
27 | 36 |
|
28 | 37 | let params: { id?: string } = {} |
29 | 38 | let search: { draftId?: string } = {} |
@@ -32,7 +41,7 @@ let variant: string | undefined |
32 | 41 | let permissionServer = "server-a" |
33 | 42 | let createSessionGate: Promise<void> | undefined |
34 | 43 |
|
35 | | -const promptValue: Prompt = [{ type: "text", content: "ls", start: 0, end: 2 }] |
| 44 | +let promptValue: Prompt = [{ type: "text", content: "ls", start: 0, end: 2 }] |
36 | 45 | const [promptStore, setPromptStore] = createStore<PromptStore>({ |
37 | 46 | prompt: promptValue, |
38 | 47 | cursor: 0, |
@@ -64,23 +73,39 @@ const prompt = { |
64 | 73 | const clientFor = (directory: string) => { |
65 | 74 | createdClients.push(directory) |
66 | 75 | return { |
67 | | - session: { |
68 | | - create: async () => { |
69 | | - await createSessionGate |
70 | | - createdSessions.push(directory) |
71 | | - return { |
72 | | - data: { |
| 76 | + api: { |
| 77 | + session: { |
| 78 | + create: async (input: (typeof sessionCreateInputs)[number]) => { |
| 79 | + await createSessionGate |
| 80 | + const location = input.location?.directory ?? directory |
| 81 | + createdSessions.push(location) |
| 82 | + sessionCreateInputs.push(input) |
| 83 | + return { |
73 | 84 | id: `session-${createdSessions.length}`, |
| 85 | + projectID: "project", |
| 86 | + agent: input.agent, |
| 87 | + model: input.model, |
| 88 | + cost: 0, |
| 89 | + tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } }, |
| 90 | + time: { created: 1, updated: 1 }, |
74 | 91 | title: `New session ${createdSessions.length}`, |
75 | | - }, |
76 | | - } |
77 | | - }, |
78 | | - shell: async () => { |
79 | | - sentShell.push(directory) |
80 | | - return { data: undefined } |
| 92 | + location: { directory: location }, |
| 93 | + } |
| 94 | + }, |
| 95 | + prompt: async (input: unknown) => { |
| 96 | + sentPrompts.push(directory) |
| 97 | + promptInputs.push(input) |
| 98 | + return { data: undefined } |
| 99 | + }, |
| 100 | + command: async (input: unknown) => { |
| 101 | + sentCommands.push(input) |
| 102 | + }, |
| 103 | + shell: async (input: { sessionID: string; id?: string; command: string }) => { |
| 104 | + sentShell.push(input) |
| 105 | + }, |
81 | 106 | }, |
82 | | - prompt: async () => ({ data: undefined }), |
83 | | - promptAsync: async () => ({ data: undefined }), |
| 107 | + }, |
| 108 | + session: { |
84 | 109 | command: async () => ({ data: undefined }), |
85 | 110 | abort: async () => ({ data: undefined }), |
86 | 111 | }, |
@@ -90,27 +115,6 @@ const clientFor = (directory: string) => { |
90 | 115 | } |
91 | 116 | } |
92 | 117 |
|
93 | | -const api = { |
94 | | - session: { |
95 | | - async create(input: { location: { directory: string } }) { |
96 | | - await createSessionGate |
97 | | - createdSessions.push(input.location.directory) |
98 | | - const session = { |
99 | | - id: `session-${createdSessions.length}`, |
100 | | - title: `New session ${createdSessions.length}`, |
101 | | - } |
102 | | - sessionDirectories[session.id] = input.location.directory |
103 | | - return session |
104 | | - }, |
105 | | - async shell(input: { sessionID: string }) { |
106 | | - sentShell.push(sessionDirectories[input.sessionID] ?? "/repo/main") |
107 | | - }, |
108 | | - async prompt() {}, |
109 | | - async command() {}, |
110 | | - async interrupt() {}, |
111 | | - }, |
112 | | -} |
113 | | - |
114 | 118 | beforeAll(async () => { |
115 | 119 | const rootClient = clientFor("/repo/main") |
116 | 120 |
|
@@ -193,8 +197,8 @@ beforeAll(async () => { |
193 | 197 | const sdk = { |
194 | 198 | scope: "local", |
195 | 199 | directory: "/repo/main", |
196 | | - api, |
197 | 200 | client: rootClient, |
| 201 | + api: rootClient.api, |
198 | 202 | url: "http://localhost:4096", |
199 | 203 | createClient(opts: any) { |
200 | 204 | return clientFor(opts.directory) |
@@ -206,7 +210,7 @@ beforeAll(async () => { |
206 | 210 |
|
207 | 211 | mock.module("@/context/sync", () => ({ |
208 | 212 | useSync: () => () => ({ |
209 | | - data: { command: [] }, |
| 213 | + data: { command: commands }, |
210 | 214 | session: { |
211 | 215 | optimistic: { |
212 | 216 | add: (value: { |
@@ -233,6 +237,9 @@ beforeAll(async () => { |
233 | 237 | session: { |
234 | 238 | remember: () => undefined, |
235 | 239 | set: () => undefined, |
| 240 | + sync: async () => { |
| 241 | + serverSessionSyncs++ |
| 242 | + }, |
236 | 243 | }, |
237 | 244 | child: (directory: string) => { |
238 | 245 | syncedDirectories.push(directory) |
@@ -274,11 +281,17 @@ beforeAll(async () => { |
274 | 281 | beforeEach(() => { |
275 | 282 | createdClients.length = 0 |
276 | 283 | createdSessions.length = 0 |
| 284 | + sessionCreateInputs.length = 0 |
277 | 285 | enabledAutoAccept.length = 0 |
278 | 286 | optimistic.length = 0 |
279 | 287 | optimisticSeeded.length = 0 |
280 | 288 | promoted.length = 0 |
281 | 289 | promotedDrafts.length = 0 |
| 290 | + sentPrompts.length = 0 |
| 291 | + promptInputs.length = 0 |
| 292 | + sentCommands.length = 0 |
| 293 | + commands.length = 0 |
| 294 | + promptValue = [{ type: "text", content: "ls", start: 0, end: 2 }] |
282 | 295 | params = {} |
283 | 296 | search = {} |
284 | 297 | sentShell.length = 0 |
@@ -287,8 +300,8 @@ beforeEach(() => { |
287 | 300 | variant = undefined |
288 | 301 | permissionServer = "server-a" |
289 | 302 | createSessionGate = undefined |
| 303 | + serverSessionSyncs = 0 |
290 | 304 | for (const key of Object.keys(storedSessions)) delete storedSessions[key] |
291 | | - for (const key of Object.keys(sessionDirectories)) delete sessionDirectories[key] |
292 | 305 | }) |
293 | 306 |
|
294 | 307 | describe("prompt submit worktree selection", () => { |
@@ -321,8 +334,24 @@ describe("prompt submit worktree selection", () => { |
321 | 334 |
|
322 | 335 | expect(createdClients).toEqual(["/repo/worktree-a", "/repo/worktree-b"]) |
323 | 336 | expect(createdSessions).toEqual(["/repo/worktree-a", "/repo/worktree-b"]) |
324 | | - expect(sentShell).toEqual(["/repo/worktree-a", "/repo/worktree-b"]) |
| 337 | + expect(sessionCreateInputs).toEqual([ |
| 338 | + { |
| 339 | + agent: "agent", |
| 340 | + model: { id: "model", providerID: "provider", variant: undefined }, |
| 341 | + location: { directory: "/repo/worktree-a" }, |
| 342 | + }, |
| 343 | + { |
| 344 | + agent: "agent", |
| 345 | + model: { id: "model", providerID: "provider", variant: undefined }, |
| 346 | + location: { directory: "/repo/worktree-b" }, |
| 347 | + }, |
| 348 | + ]) |
| 349 | + expect(sentShell).toEqual([ |
| 350 | + expect.objectContaining({ sessionID: "session-1", id: expect.stringMatching(/^evt_/), command: "ls" }), |
| 351 | + expect.objectContaining({ sessionID: "session-2", id: expect.stringMatching(/^evt_/), command: "ls" }), |
| 352 | + ]) |
325 | 353 | expect(syncedDirectories).toEqual(["/repo/worktree-a", "/repo/worktree-a", "/repo/worktree-b", "/repo/worktree-b"]) |
| 354 | + expect(serverSessionSyncs).toBe(0) |
326 | 355 | expect(promoted).toEqual([ |
327 | 356 | { directory: "/repo/worktree-a", sessionID: "session-1" }, |
328 | 357 | { directory: "/repo/worktree-b", sessionID: "session-2" }, |
@@ -443,6 +472,7 @@ describe("prompt submit worktree selection", () => { |
443 | 472 | const event = { preventDefault: () => undefined } as unknown as Event |
444 | 473 |
|
445 | 474 | await submit.handleSubmit(event) |
| 475 | + await Bun.sleep(0) |
446 | 476 |
|
447 | 477 | expect(optimistic).toHaveLength(1) |
448 | 478 | expect(optimistic[0]).toMatchObject({ |
@@ -451,6 +481,53 @@ describe("prompt submit worktree selection", () => { |
451 | 481 | model: { providerID: "provider", modelID: "model", variant: "high" }, |
452 | 482 | }, |
453 | 483 | }) |
| 484 | + expect(sentPrompts).toEqual(["/repo/main"]) |
| 485 | + expect(promptInputs[0]).toMatchObject({ |
| 486 | + sessionID: "session-1", |
| 487 | + text: "ls", |
| 488 | + files: [], |
| 489 | + agents: [], |
| 490 | + }) |
| 491 | + expect((promptInputs[0] as { id?: string }).id).toStartWith("msg_") |
| 492 | + }) |
| 493 | + |
| 494 | + test("submits slash commands through the current session API", async () => { |
| 495 | + params = { id: "session-1" } |
| 496 | + variant = "high" |
| 497 | + commands.push({ name: "review" }) |
| 498 | + promptValue = [{ type: "text", content: "/review staged changes", start: 0, end: 22 }] |
| 499 | + |
| 500 | + const submit = createPromptSubmit({ |
| 501 | + prompt, |
| 502 | + info: () => ({ id: "session-1" }), |
| 503 | + imageAttachments: () => [], |
| 504 | + commentCount: () => 0, |
| 505 | + autoAccept: () => false, |
| 506 | + mode: () => "normal", |
| 507 | + working: () => false, |
| 508 | + editor: () => undefined, |
| 509 | + queueScroll: () => undefined, |
| 510 | + promptLength: (value) => value.reduce((sum, part) => sum + ("content" in part ? part.content.length : 0), 0), |
| 511 | + addToHistory: () => undefined, |
| 512 | + resetHistoryNavigation: () => undefined, |
| 513 | + setMode: () => undefined, |
| 514 | + setPopover: () => undefined, |
| 515 | + }) |
| 516 | + |
| 517 | + await submit.handleSubmit({ preventDefault: () => undefined } as unknown as Event) |
| 518 | + |
| 519 | + expect(sentCommands).toEqual([ |
| 520 | + { |
| 521 | + sessionID: "session-1", |
| 522 | + id: expect.stringMatching(/^msg_/), |
| 523 | + command: "review", |
| 524 | + arguments: "staged changes", |
| 525 | + agent: "agent", |
| 526 | + model: { id: "model", providerID: "provider", variant: "high" }, |
| 527 | + files: [], |
| 528 | + }, |
| 529 | + ]) |
| 530 | + expect(serverSessionSyncs).toBe(0) |
454 | 531 | }) |
455 | 532 |
|
456 | 533 | test("uses an injected model selection", async () => { |
@@ -511,7 +588,8 @@ describe("prompt submit worktree selection", () => { |
511 | 588 |
|
512 | 589 | await submit.handleSubmit(event) |
513 | 590 |
|
514 | | - expect(storedSessions["/repo/worktree-a"]).toEqual([{ id: "session-1", title: "New session 1" }]) |
| 591 | + expect(storedSessions["/repo/worktree-a"]).toHaveLength(1) |
| 592 | + expect(storedSessions["/repo/worktree-a"]?.[0]).toMatchObject({ id: "session-1", title: "New session 1" }) |
515 | 593 | expect(optimisticSeeded).toEqual([true]) |
516 | 594 | }) |
517 | 595 | }) |