-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathpr.test.ts
More file actions
211 lines (186 loc) · 6.04 KB
/
pr.test.ts
File metadata and controls
211 lines (186 loc) · 6.04 KB
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
/**
* @vitest-environment jsdom
*
* GitHub PR Tool Unit Tests
*
* This file contains unit tests for the GitHub Pull Request tool,
* which is used to fetch PR details including diffs and files changed.
*/
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'
import { ToolTester } from '../__test-utils__/test-tools'
import { prTool } from './pr'
describe('GitHub PR Tool', () => {
let tester: ToolTester
// Mock PR response data
const mockPRResponse = {
number: 42,
title: 'Test PR Title',
body: 'Test PR description with details',
state: 'open',
html_url: 'https://github.com/testuser/testrepo/pull/42',
diff_url: 'https://github.com/testuser/testrepo/pull/42.diff',
created_at: '2023-01-01T00:00:00Z',
updated_at: '2023-01-02T00:00:00Z',
base: {
repo: {
name: 'testrepo',
owner: {
login: 'testuser',
},
},
},
}
// Mock PR diff data
const _mockPRDiff = `diff --git a/file.txt b/file.txt
index 1234567..abcdefg 100644
--- a/file.txt
+++ b/file.txt
@@ -1,3 +1,4 @@
Line 1
-Line 2
+Line 2 modified
+Line 3 added
Line 4`
// Mock PR files data
const mockPRFiles = [
{
filename: 'file.txt',
additions: 2,
deletions: 1,
changes: 3,
patch: '@@ -1,3 +1,4 @@\n Line 1\n-Line 2\n+Line 2 modified\n+Line 3 added\n Line 4',
blob_url: 'https://github.com/testuser/testrepo/blob/abc123/file.txt',
raw_url: 'https://github.com/testuser/testrepo/raw/abc123/file.txt',
status: 'modified',
},
]
// Expected content for the PR
const expectedContent = `PR #42: "Test PR Title" (open) - Created: 2023-01-01T00:00:00Z, Updated: 2023-01-02T00:00:00Z
Description: Test PR description with details
Files changed: 1
URL: https://github.com/testuser/testrepo/pull/42`
let originalTransformResponse: any
beforeEach(() => {
tester = new ToolTester(prTool)
// Mock the internal transformResponse method to avoid actual API calls
originalTransformResponse = prTool.transformResponse
prTool.transformResponse = async () => {
return {
success: true,
output: {
content: expectedContent,
metadata: {
number: 42,
title: 'Test PR Title',
state: 'open',
html_url: 'https://github.com/testuser/testrepo/pull/42',
diff_url: 'https://github.com/testuser/testrepo/pull/42.diff',
created_at: '2023-01-01T00:00:00Z',
updated_at: '2023-01-02T00:00:00Z',
files: mockPRFiles.map((file) => ({
filename: file.filename,
additions: file.additions,
deletions: file.deletions,
changes: file.changes,
patch: file.patch,
blob_url: file.blob_url,
raw_url: file.raw_url,
status: file.status,
})),
},
},
}
}
})
afterEach(() => {
// Restore the original transformResponse if it exists
if (originalTransformResponse) {
prTool.transformResponse = originalTransformResponse
}
tester.cleanup()
vi.resetAllMocks()
})
describe('URL Construction', () => {
test('should construct correct GitHub PR API URL', () => {
const params = {
owner: 'testuser',
repo: 'testrepo',
pullNumber: 42,
apiKey: 'test-token',
}
expect(tester.getRequestUrl(params)).toBe(
'https://api.github.com/repos/testuser/testrepo/pulls/42'
)
})
})
describe('Headers Construction', () => {
test('should include correct headers for GitHub API', () => {
const params = {
owner: 'testuser',
repo: 'testrepo',
pullNumber: 42,
apiKey: 'test-token',
}
const headers = tester.getRequestHeaders(params)
expect(headers.Authorization).toBe('Bearer test-token')
expect(headers.Accept).toBe('application/vnd.github.v3+json')
})
})
describe('Data Transformation', () => {
test('should fetch and transform PR data including diff and files', async () => {
// Setup mock response for initial PR data
tester.setup(mockPRResponse)
// Execute the tool
const result = await tester.execute({
owner: 'testuser',
repo: 'testrepo',
pullNumber: 42,
apiKey: 'test-token',
})
// Check the result
expect(result.success).toBe(true)
// Verify content is present and correct
expect(result.output.content).toBe(expectedContent)
// Verify PR basic info in metadata
expect(result.output.metadata.number).toBe(42)
expect(result.output.metadata.title).toBe('Test PR Title')
expect(result.output.metadata.state).toBe('open')
// Verify files were fetched and transformed
expect(result.output.metadata.files).toHaveLength(1)
expect(result.output.metadata.files?.[0].filename).toBe('file.txt')
expect(result.output.metadata.files?.[0].additions).toBe(2)
expect(result.output.metadata.files?.[0].deletions).toBe(1)
expect(result.output.metadata.files?.[0].status).toBe('modified')
})
})
describe('Error Handling', () => {
test('should handle PR not found errors', async () => {
// Setup 404 error response
tester.setup({ message: 'Not Found' }, { ok: false, status: 404 })
// Execute the tool
const result = await tester.execute({
owner: 'testuser',
repo: 'testrepo',
pullNumber: 9999, // non-existent PR
apiKey: 'test-token',
})
// Check error handling
expect(result.success).toBe(false)
expect(result.error).toBeDefined()
})
test('should handle network errors', async () => {
// Setup network error
tester.setupError('Network error')
// Execute the tool
const result = await tester.execute({
owner: 'testuser',
repo: 'testrepo',
pullNumber: 42,
apiKey: 'test-token',
})
// Check error handling
expect(result.success).toBe(false)
expect(result.error).toBeDefined()
})
})
})