| 1 | import type { ChatHistoryItem } from "core/index.js"; |
| 2 | |
| 3 | import { convertFromUnifiedHistory } from "./messageConversion.js"; |
| 4 | |
| 5 | describe("convertFromUnifiedHistory", () => { |
| 6 | it("should expand contextItems into user message content", () => { |
| 7 | const historyItems: ChatHistoryItem[] = [ |
| 8 | { |
| 9 | message: { |
| 10 | role: "user", |
| 11 | content: "Please review @test-file.txt", |
| 12 | }, |
| 13 | contextItems: [ |
| 14 | { |
| 15 | id: { |
| 16 | providerTitle: "file", |
| 17 | itemId: "550e8400-e29b-41d4-a716-446655440000", |
| 18 | }, |
| 19 | content: "console.log('Hello, world!');", |
| 20 | name: "test-file.txt", |
| 21 | description: "File: test-file.txt", |
| 22 | uri: { |
| 23 | type: "file", |
| 24 | value: "file:///Users/user/test-file.txt", |
| 25 | }, |
| 26 | }, |
| 27 | ], |
| 28 | }, |
| 29 | ]; |
| 30 | |
| 31 | const result = convertFromUnifiedHistory(historyItems); |
| 32 | |
| 33 | expect(result).toHaveLength(1); |
| 34 | expect(result[0]).toEqual({ |
| 35 | role: "user", |
| 36 | content: |
| 37 | "<context name=\"test-file.txt\">\nconsole.log('Hello, world!');\n</context>\n\nPlease review @test-file.txt", |
| 38 | }); |
| 39 | }); |
| 40 | |
| 41 | it("should handle user messages without contextItems", () => { |
| 42 | const historyItems: ChatHistoryItem[] = [ |
| 43 | { |
| 44 | message: { |
| 45 | role: "user", |
| 46 | content: "Hello", |
| 47 | }, |
| 48 | contextItems: [], |
| 49 | }, |
| 50 | ]; |
| 51 | |
| 52 | const result = convertFromUnifiedHistory(historyItems); |
| 53 | |
| 54 | expect(result).toHaveLength(1); |
| 55 | expect(result[0]).toEqual({ |
| 56 | role: "user", |
| 57 | content: "Hello", |
| 58 | }); |
| 59 | }); |
| 60 | |
| 61 | it("should handle multiple contextItems", () => { |
| 62 | const historyItems: ChatHistoryItem[] = [ |
| 63 | { |
| 64 | message: { |
| 65 | role: "user", |
| 66 | content: "Compare @file1.js and @file2.js", |
| 67 | }, |
| 68 | contextItems: [ |
| 69 | { |
| 70 | id: { |
| 71 | providerTitle: "file", |
| 72 | itemId: "550e8400-e29b-41d4-a716-446655440000", |
| 73 | }, |
| 74 | content: "const a = 1;", |
| 75 | name: "file1.js", |
| 76 | description: "File: file1.js", |
| 77 | uri: { |
| 78 | type: "file", |
| 79 | value: "file:///Users/user/file1.js", |
| 80 | }, |
| 81 | }, |
| 82 | { |
| 83 | id: { |
| 84 | providerTitle: "file", |
| 85 | itemId: "550e8400-e29b-41d4-a716-446655440001", |
| 86 | }, |
| 87 | content: "const b = 2;", |
| 88 | name: "file2.js", |
| 89 | description: "File: file2.js", |
| 90 | uri: { |
| 91 | type: "file", |
| 92 | value: "file:///Users/user/file2.js", |
| 93 | }, |
| 94 | }, |
| 95 | ], |
| 96 | }, |
| 97 | ]; |
| 98 | |
| 99 | const result = convertFromUnifiedHistory(historyItems); |
| 100 | |
| 101 | expect(result).toHaveLength(1); |
| 102 | expect(result[0]).toEqual({ |
| 103 | role: "user", |
| 104 | content: |
| 105 | '<context name="file1.js">\nconst a = 1;\n</context>\n\n<context name="file2.js">\nconst b = 2;\n</context>\n\nCompare @file1.js and @file2.js', |
| 106 | }); |
| 107 | }); |
| 108 | |
| 109 | it("should not expand contextItems for non-user messages", () => { |
| 110 | const historyItems: ChatHistoryItem[] = [ |
| 111 | { |
| 112 | message: { |
| 113 | role: "assistant", |
| 114 | content: "Here's the analysis", |
| 115 | }, |
| 116 | contextItems: [ |
| 117 | { |
| 118 | id: { |
| 119 | providerTitle: "file", |
| 120 | itemId: "550e8400-e29b-41d4-a716-446655440000", |
| 121 | }, |
| 122 | content: "some content", |
| 123 | name: "file.txt", |
| 124 | description: "File: file.txt", |
| 125 | uri: { |
| 126 | type: "file", |
| 127 | value: "file:///Users/user/file.txt", |
| 128 | }, |
| 129 | }, |
| 130 | ], |
| 131 | }, |
| 132 | ]; |
| 133 | |
| 134 | const result = convertFromUnifiedHistory(historyItems); |
| 135 | |
| 136 | expect(result).toHaveLength(1); |
| 137 | expect(result[0]).toEqual({ |
| 138 | role: "assistant", |
| 139 | content: "Here's the analysis", |
| 140 | }); |
| 141 | }); |
| 142 | }); |