| 1 | import { |
| 2 | ApplyState, |
| 3 | BrowserSerializedContinueConfig, |
| 4 | ToolCallState, |
| 5 | } from "core"; |
| 6 | import { describe, expect, it, vi } from "vitest"; |
| 7 | import { EMPTY_CONFIG } from "../redux/slices/configSlice"; |
| 8 | import { assembleEditOutcomeData } from "./editOutcomeLogger"; |
| 9 | |
| 10 | vi.mock("../redux/store", () => ({ |
| 11 | store: { |
| 12 | getState: vi.fn(() => ({ |
| 13 | session: { history: [] }, |
| 14 | config: { |
| 15 | config: { |
| 16 | selectedModelByRole: { |
| 17 | chat: { provider: "test", model: "test-model" }, |
| 18 | }, |
| 19 | }, |
| 20 | }, |
| 21 | })), |
| 22 | }, |
| 23 | })); |
| 24 | |
| 25 | const EMPTY_CONFIG_WITH_TEST_MODEL_SELECTED: BrowserSerializedContinueConfig = { |
| 26 | ...EMPTY_CONFIG, |
| 27 | selectedModelByRole: { |
| 28 | ...EMPTY_CONFIG.selectedModelByRole, |
| 29 | chat: { |
| 30 | provider: "test", |
| 31 | model: "test-model", |
| 32 | title: "Test model", |
| 33 | underlyingProviderName: "provider", |
| 34 | }, |
| 35 | }, |
| 36 | }; |
| 37 | |
| 38 | describe("assembleEditOutcomeData", () => { |
| 39 | it("should assemble complete edit outcome data correctly", () => { |
| 40 | const toolCallState: ToolCallState = { |
| 41 | status: "generating", |
| 42 | toolCallId: "test-id", |
| 43 | toolCall: { |
| 44 | id: "test-id", |
| 45 | type: "function", |
| 46 | function: { name: "test", arguments: "{}" }, |
| 47 | }, |
| 48 | parsedArgs: {}, |
| 49 | }; |
| 50 | |
| 51 | const applyState: ApplyState = { |
| 52 | streamId: "stream-123", |
| 53 | filepath: "/test/file.ts", |
| 54 | originalFileContent: "const old = 1;", |
| 55 | fileContent: "const new = 2;\nconst another = 3;", |
| 56 | }; |
| 57 | |
| 58 | const result = assembleEditOutcomeData( |
| 59 | [], |
| 60 | EMPTY_CONFIG_WITH_TEST_MODEL_SELECTED, |
| 61 | toolCallState, |
| 62 | applyState, |
| 63 | true, |
| 64 | ); |
| 65 | console.log(result); |
| 66 | |
| 67 | expect(result).toMatchObject({ |
| 68 | streamId: "stream-123", |
| 69 | filepath: "/test/file.ts", |
| 70 | previousCode: "const old = 1;", |
| 71 | newCode: "const new = 2;\nconst another = 3;", |
| 72 | previousCodeLines: 1, |
| 73 | newCodeLines: 2, |
| 74 | lineChange: 1, |
| 75 | accepted: true, |
| 76 | }); |
| 77 | }); |
| 78 | |
| 79 | it("should handle empty code content", () => { |
| 80 | const toolCallState: ToolCallState = { |
| 81 | status: "generating", |
| 82 | toolCallId: "test-id", |
| 83 | toolCall: { |
| 84 | id: "test-id", |
| 85 | type: "function", |
| 86 | function: { name: "test", arguments: "{}" }, |
| 87 | }, |
| 88 | parsedArgs: {}, |
| 89 | }; |
| 90 | |
| 91 | const applyState: ApplyState = { |
| 92 | streamId: "stream-123", |
| 93 | filepath: "/test/empty.ts", |
| 94 | originalFileContent: "", |
| 95 | fileContent: "", |
| 96 | }; |
| 97 | |
| 98 | const result = assembleEditOutcomeData( |
| 99 | [], |
| 100 | EMPTY_CONFIG_WITH_TEST_MODEL_SELECTED, |
| 101 | toolCallState, |
| 102 | applyState, |
| 103 | false, |
| 104 | ); |
| 105 | |
| 106 | expect(result.previousCodeLines).toBe(0); |
| 107 | expect(result.newCodeLines).toBe(0); |
| 108 | expect(result.lineChange).toBe(0); |
| 109 | expect(result.accepted).toBe(false); |
| 110 | }); |
| 111 | }); |