-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathline-comments-sender.test.ts
More file actions
157 lines (137 loc) · 4.81 KB
/
line-comments-sender.test.ts
File metadata and controls
157 lines (137 loc) · 4.81 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
import { describe, it, expect, beforeEach, vi, afterEach } from 'vitest'
import { openaiLineCommentsSender } from '../../src/senders/providers/openai/line-comments-sender.ts'
import { _resetRuntimeConfigCacheForTests } from '../../src/core/utils/runtime-config.ts'
// Mock the OpenAI SDK
const createMock = vi.fn()
vi.mock('openai', () => {
class MockOpenAI {
responses = {
create: createMock
}
}
return { default: MockOpenAI }
})
describe('openaiLineCommentsSender', () => {
beforeEach(async () => {
vi.resetModules()
createMock.mockReset()
_resetRuntimeConfigCacheForTests()
// Ensure env is set for tests
process.env.OPENAI_API_KEY = 'test-openai-key'
delete process.env.OPENAI_MODEL
})
afterEach(() => {
delete process.env.OPENAI_API_KEY
delete process.env.OPENAI_MODEL
})
it('returns structured JSON string when Responses API returns valid json_schema output', async () => {
const expected = { summary: 'Looks good', comments: [] }
createMock.mockResolvedValue({
output_text: JSON.stringify(expected)
})
const result = await openaiLineCommentsSender('test prompt')
expect(result).toEqual(JSON.stringify(expected))
// Ensure SDK called with default model and structured outputs config
expect(createMock).toHaveBeenCalledWith(
expect.objectContaining({
model: 'gpt-5',
input: 'test prompt',
text: {
format: expect.objectContaining({
type: 'json_schema',
name: 'code_review'
})
}
})
)
})
it('uses OPENAI_MODEL when provided', async () => {
process.env.OPENAI_MODEL = 'gpt-5'
const expected = { summary: 'ok', comments: [] }
createMock.mockResolvedValue({
output_text: JSON.stringify(expected)
})
const result = await openaiLineCommentsSender('prompt')
expect(JSON.parse(result)).toEqual(expected)
expect(createMock).toHaveBeenCalledWith(
expect.objectContaining({
model: 'gpt-5'
})
)
})
it('throws when output_text is missing or empty', async () => {
createMock.mockResolvedValue({})
await expect(openaiLineCommentsSender('test prompt')).rejects.toThrow(
'OpenAI Responses API did not return output_text for line comments'
)
})
it('throws when output_text has invalid JSON', async () => {
createMock.mockResolvedValue({
output_text: '{ invalid json'
})
await expect(openaiLineCommentsSender('test prompt')).rejects.toThrow(
'OpenAI Responses API returned invalid JSON for line comments'
)
})
it('uses temperature=1 for GPT-5 regardless of thinking mode (disabled)', async () => {
process.env.OPENAI_MODEL = 'gpt-5'
const expected = { summary: 'ok', comments: [] }
createMock.mockResolvedValue({
output_text: JSON.stringify(expected)
})
// thinking disabled, but GPT-5 requires temperature=1
await openaiLineCommentsSender('prompt', false)
expect(createMock).toHaveBeenCalledWith(
expect.objectContaining({
model: 'gpt-5',
temperature: 1 // Should be 1 for GPT-5, not 0
})
)
})
it('uses temperature=1 for GPT-5 when thinking is enabled', async () => {
process.env.OPENAI_MODEL = 'gpt-5'
const expected = { summary: 'ok', comments: [] }
createMock.mockResolvedValue({
output_text: JSON.stringify(expected)
})
// thinking enabled, GPT-5 gets temperature=1
await openaiLineCommentsSender('prompt', true)
expect(createMock).toHaveBeenCalledWith(
expect.objectContaining({
model: 'gpt-5',
temperature: 1
})
)
})
it('uses temperature=0 for non-GPT-5 models when thinking is disabled', async () => {
process.env.OPENAI_MODEL = 'gpt-4o'
const expected = { summary: 'ok', comments: [] }
createMock.mockResolvedValue({
output_text: JSON.stringify(expected)
})
// Other models can use temperature=0 when thinking disabled
await openaiLineCommentsSender('prompt', false)
expect(createMock).toHaveBeenCalledWith(
expect.objectContaining({
model: 'gpt-4o',
temperature: 0 // Should be 0 for non-GPT-5 models when thinking disabled
})
)
})
it('respects model-specific temperature overrides from registry', async () => {
// This test demonstrates that the system is provider-agnostic
// Any model can be added to the override registry
process.env.OPENAI_MODEL = 'gpt-5'
const expected = { summary: 'ok', comments: [] }
createMock.mockResolvedValue({
output_text: JSON.stringify(expected)
})
// The override system forces temperature=1 for GPT-5
await openaiLineCommentsSender('prompt', false)
expect(createMock).toHaveBeenCalledWith(
expect.objectContaining({
temperature: 1 // Forced by MODEL_PARAMETER_OVERRIDES registry
})
)
})
})