-
Notifications
You must be signed in to change notification settings - Fork 84
Fix file creation #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/__tests__/CodexACPAgent/data/file-change-add-multiple-files.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| { | ||
| "method": "sessionUpdate", | ||
| "args": [ | ||
| { | ||
| "sessionId": "test-session-id", | ||
| "update": { | ||
| "sessionUpdate": "tool_call", | ||
| "toolCallId": "file-change-2", | ||
| "title": "Editing files", | ||
| "kind": "edit", | ||
| "status": "completed", | ||
| "content": [ | ||
| { | ||
| "type": "diff", | ||
| "oldText": null, | ||
| "newText": "class FileA\n", | ||
| "path": "/test/project/FileA.kt" | ||
| }, | ||
| { | ||
| "type": "diff", | ||
| "oldText": null, | ||
| "newText": "class FileB\n", | ||
| "path": "/test/project/FileB.kt" | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| ] | ||
| } |
23 changes: 23 additions & 0 deletions
23
src/__tests__/CodexACPAgent/data/file-change-add-new-file.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| { | ||
| "method": "sessionUpdate", | ||
| "args": [ | ||
| { | ||
| "sessionId": "test-session-id", | ||
| "update": { | ||
| "sessionUpdate": "tool_call", | ||
| "toolCallId": "file-change-1", | ||
| "title": "Editing files", | ||
| "kind": "edit", | ||
| "status": "completed", | ||
| "content": [ | ||
| { | ||
| "type": "diff", | ||
| "oldText": null, | ||
| "newText": "package test.project\n\nclass NewFile {\n fun hello() = \"Hello\"\n}\n", | ||
| "path": "/test/project/NewFile.kt" | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| ] | ||
| } |
23 changes: 23 additions & 0 deletions
23
src/__tests__/CodexACPAgent/data/file-change-add-raw-content.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| { | ||
| "method": "sessionUpdate", | ||
| "args": [ | ||
| { | ||
| "sessionId": "test-session-id", | ||
| "update": { | ||
| "sessionUpdate": "tool_call", | ||
| "toolCallId": "file-change-raw", | ||
| "title": "Editing files", | ||
| "kind": "edit", | ||
| "status": "completed", | ||
| "content": [ | ||
| { | ||
| "type": "diff", | ||
| "oldText": null, | ||
| "newText": "fun main() {\n println(\"Hello, World!\")\n}\n", | ||
| "path": "/test/project/RawFile.kt" | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| ] | ||
| } |
23 changes: 23 additions & 0 deletions
23
src/__tests__/CodexACPAgent/data/file-change-delete-file.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| { | ||
| "method": "sessionUpdate", | ||
| "args": [ | ||
| { | ||
| "sessionId": "test-session-id", | ||
| "update": { | ||
| "sessionUpdate": "tool_call", | ||
| "toolCallId": "file-change-3", | ||
| "title": "Editing files", | ||
| "kind": "edit", | ||
| "status": "completed", | ||
| "content": [ | ||
| { | ||
| "type": "diff", | ||
| "oldText": "package test.project\n\nclass OldFile {}", | ||
| "newText": "", | ||
| "path": "/test/project/OldFile.kt" | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,204 @@ | ||
| import { describe, it, expect, vi, beforeEach } from 'vitest'; | ||
| import type { SessionState } from '../../CodexAcpServer'; | ||
| import type { ServerNotification } from '../../app-server'; | ||
| import { createCodexMockTestFixture, type CodexMockTestFixture } from '../acp-test-utils'; | ||
|
|
||
| const { mockFiles, mockFileContent, clearMockFiles } = vi.hoisted(() => { | ||
| const files = new Map<string, string>(); | ||
| return { | ||
| mockFiles: files, | ||
| mockFileContent: (path: string, content: string) => files.set(path, content), | ||
| clearMockFiles: () => files.clear(), | ||
| }; | ||
| }); | ||
|
|
||
| vi.mock('node:fs/promises', () => ({ | ||
| readFile: (path: string) => { | ||
| const content = mockFiles.get(path); | ||
| if (content !== undefined) { | ||
| return Promise.resolve(content); | ||
| } | ||
| return Promise.reject(new Error(`ENOENT: no such file or directory, open '${path}'`)); | ||
| }, | ||
| })); | ||
|
|
||
| describe('CodexEventHandler - file change events', () => { | ||
| let mockFixture: CodexMockTestFixture; | ||
| const sessionId = 'test-session-id'; | ||
|
|
||
| beforeEach(() => { | ||
| mockFixture = createCodexMockTestFixture(); | ||
| clearMockFiles(); | ||
| mockFileContent('/test/project/OldFile.kt', 'package test.project\n\nclass OldFile {}'); | ||
| }); | ||
|
|
||
| const sessionState: SessionState = { | ||
| pendingPrompt: null, | ||
| sessionMetadata: { | ||
| sessionId, | ||
| currentModelId: 'model-id', | ||
| models: [], | ||
| }, | ||
| }; | ||
|
|
||
| async function setupAndSendNotifications(notifications: ServerNotification[]) { | ||
| const codexAcpAgent = mockFixture.getCodexAcpAgent(); | ||
|
|
||
| mockFixture.getCodexAppServerClient().turnStart = vi.fn().mockResolvedValue(undefined); | ||
| mockFixture.getCodexAppServerClient().awaitTurnCompleted = vi.fn().mockResolvedValue(undefined); | ||
|
|
||
| vi.spyOn(codexAcpAgent, 'getSessionState').mockReturnValue(sessionState); | ||
|
|
||
| await codexAcpAgent.prompt({ | ||
| sessionId, | ||
| prompt: [{ type: 'text', text: 'test prompt' }], | ||
| }); | ||
|
|
||
| mockFixture.clearAcpConnectionDump(); | ||
|
|
||
| for (const notification of notifications) { | ||
| mockFixture.sendServerNotification(notification); | ||
| } | ||
|
|
||
| await vi.waitFor(() => { | ||
| const dump = mockFixture.getAcpConnectionDump([]); | ||
| expect(dump.length).toBeGreaterThan(0); | ||
| }); | ||
| } | ||
|
|
||
| it('should handle new file creation', async () => { | ||
| const newFileNotification: ServerNotification = { | ||
| method: 'item/started', | ||
| params: { | ||
| threadId: 'thread-1', | ||
| turnId: 'turn-1', | ||
| item: { | ||
| type: 'fileChange', | ||
| id: 'file-change-1', | ||
| changes: [ | ||
| { | ||
| path: '/test/project/NewFile.kt', | ||
| kind: { type: 'add' }, | ||
| diff: `--- /dev/null | ||
| +++ /test/project/NewFile.kt | ||
| @@ -0,0 +1,5 @@ | ||
| +package test.project | ||
| + | ||
| +class NewFile { | ||
| + fun hello() = "Hello" | ||
| +}`, | ||
| }, | ||
| ], | ||
| status: 'completed', | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| await setupAndSendNotifications([newFileNotification]); | ||
|
|
||
| await expect(mockFixture.getAcpConnectionDump(['id'])).toMatchFileSnapshot( | ||
| 'data/file-change-add-new-file.json' | ||
| ); | ||
| }); | ||
|
|
||
| it('should handle multiple new files in single change', async () => { | ||
| const multiFileNotification: ServerNotification = { | ||
| method: 'item/started', | ||
| params: { | ||
| threadId: 'thread-1', | ||
| turnId: 'turn-1', | ||
| item: { | ||
| type: 'fileChange', | ||
| id: 'file-change-2', | ||
| changes: [ | ||
| { | ||
| path: '/test/project/FileA.kt', | ||
| kind: { type: 'add' }, | ||
| diff: `--- /dev/null | ||
| +++ /test/project/FileA.kt | ||
| @@ -0,0 +1 @@ | ||
| +class FileA`, | ||
| }, | ||
| { | ||
| path: '/test/project/FileB.kt', | ||
| kind: { type: 'add' }, | ||
| diff: `--- /dev/null | ||
| +++ /test/project/FileB.kt | ||
| @@ -0,0 +1 @@ | ||
| +class FileB`, | ||
| }, | ||
| ], | ||
| status: 'completed', | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| await setupAndSendNotifications([multiFileNotification]); | ||
|
|
||
| await expect(mockFixture.getAcpConnectionDump(['id'])).toMatchFileSnapshot( | ||
| 'data/file-change-add-multiple-files.json' | ||
| ); | ||
| }); | ||
|
|
||
| it('should handle new file creation with raw content', async () => { | ||
| // Codex sends raw file content (not unified diff) for new files | ||
| const newFileNotification: ServerNotification = { | ||
| method: 'item/started', | ||
| params: { | ||
| threadId: 'thread-1', | ||
| turnId: 'turn-1', | ||
| item: { | ||
| type: 'fileChange', | ||
| id: 'file-change-raw', | ||
| changes: [ | ||
| { | ||
| path: '/test/project/RawFile.kt', | ||
| kind: { type: 'add' }, | ||
| diff: 'fun main() {\n println("Hello, World!")\n}\n', | ||
| }, | ||
| ], | ||
| status: 'completed', | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| await setupAndSendNotifications([newFileNotification]); | ||
|
|
||
| await expect(mockFixture.getAcpConnectionDump(['id'])).toMatchFileSnapshot( | ||
| 'data/file-change-add-raw-content.json' | ||
| ); | ||
| }); | ||
|
|
||
| it('should handle file deletion', async () => { | ||
| const deleteFileNotification: ServerNotification = { | ||
| method: 'item/started', | ||
| params: { | ||
| threadId: 'thread-1', | ||
| turnId: 'turn-1', | ||
| item: { | ||
| type: 'fileChange', | ||
| id: 'file-change-3', | ||
| changes: [ | ||
| { | ||
| path: '/test/project/OldFile.kt', | ||
| kind: { type: 'delete' }, | ||
| diff: `--- /test/project/OldFile.kt | ||
| +++ /dev/null | ||
| @@ -1,3 +0,0 @@ | ||
| -package test.project | ||
| - | ||
| -class OldFile {}`, | ||
| }, | ||
| ], | ||
| status: 'completed', | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| await setupAndSendNotifications([deleteFileNotification]); | ||
|
|
||
| await expect(mockFixture.getAcpConnectionDump(['id'])).toMatchFileSnapshot( | ||
| 'data/file-change-delete-file.json' | ||
| ); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can not remove files using this event?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will handle it later