forked from anc95/ChatGPT-CodeReview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-review.ts
More file actions
206 lines (183 loc) ยท 6.2 KB
/
test-review.ts
File metadata and controls
206 lines (183 loc) ยท 6.2 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
import { Chat } from "./src/chat.js";
const OPENAI_API_KEY = process.env.OPENAI_API_KEY;
if (!OPENAI_API_KEY) {
console.error("OPENAI_API_KEY ํ๊ฒฝ๋ณ์๋ฅผ ์ค์ ํด์ฃผ์ธ์.");
console.error("์ฌ์ฉ๋ฒ: OPENAI_API_KEY=sk-... npx tsx test-review.ts");
process.exit(1);
}
const chat = new Chat(OPENAI_API_KEY);
const testInput = `## ๋ฆฌ๋ทฐ ์ง์นจ (STRICT)
๋ค์์ ๋ฐ๋์ ์ค์ํ์ธ์ โ ์ด๊ธฐ๋ฉด ์ฐ์ถ๋ฌผ์ ์คํจ๋ก ๊ฐ์ฃผ๋ฉ๋๋ค.
- **์ค์ฝํ: ์ด ํ์ผ์ Diff๋ง** ๋ค๋ฃน๋๋ค. ๋ค๋ฅธ ํ์ผ/์ผ๋ฐ๋ก ์ธ๊ธ ๊ธ์ง.
- **๊ทผ๊ฑฐ ํ์:** ๋ชจ๋ ์ง์ ์ Diff์ \`+\`(์ถ๊ฐ) ๋ผ์ธ์ ๊ทผ๊ฑฐํด, ํด๋น ์ฝ๋ ์ค๋ํซ์ ์ธ๋ผ์ธ ์ฝ๋๋ก ํฌํจํ์ธ์.
- **์ ์ญ ์ปจํ
์คํธ๋ ์ฐธ๊ณ ์ฉ**์
๋๋ค. ์ค์ฝํ๋ฅผ ๋ฒ์ด๋ ๋ด์ฉ์ด ํ์ํ๋ฉด ๊ทธ ํญ๋ชฉ์ **์์ฑํ์ง ๋ง์ธ์**.
- **์ฐ์ ์์:** ๋ฒ๊ทธ/๋ณด์/์ฑ๋ฅ/ํ
์คํธ ์ค์ฌ. ์ฌ์ํ ์คํ์ผ/๊ฐ์ธ ์ทจํฅ ๊ธ์ง.
- **์ค์ฝํ ์๋ฐ ๋ฐฉ์ง:** ๋ค๋ฅธ ํ์ผ์ด๋ ์ผ๋ฐ ๋ฒ ์คํธ ํ๋ํฐ์ค๋ง์ ์ธ๊ธํ๋ ค ํ ๊ฒฝ์ฐ, ํด๋น ์ฝ๋ฉํธ๋ ์๋ตํฉ๋๋ค.
# File: src/services/userService.ts
## Diff
\`\`\`diff
@@ -1,15 +1,89 @@
-import { db } from '../db';
+import { db } from '../db';
+import { Redis } from 'ioredis';
+import { hash, compare } from 'bcrypt';
+
+const redis = new Redis(process.env.REDIS_URL);
+const JWT_SECRET = "super-secret-key-12345";
+
+interface User {
+ id: string;
+ email: string;
+ password: string;
+ role: 'admin' | 'user';
+}
+
+export async function authenticateUser(email: string, password: string) {
+ const query = \`SELECT * FROM users WHERE email = '\${email}'\`;
+ const result = await db.query(query);
+
+ if (result.rows.length === 0) {
+ return null;
+ }
+
+ const user = result.rows[0];
+ const isValid = await compare(password, user.password);
+
+ if (isValid) {
+ const token = generateToken(user);
+ await redis.set(\`session:\${user.id}\`, token);
+ return { user, token };
+ }
+
+ return null;
+}
+
+export async function getUserById(id: string): Promise<User> {
+ const cached = await redis.get(\`user:\${id}\`);
+ if (cached) {
+ return JSON.parse(cached);
+ }
+
+ const result = await db.query('SELECT * FROM users WHERE id = $1', [id]);
+ const user = result.rows[0];
+
+ redis.set(\`user:\${id}\`, JSON.stringify(user), 'EX', 3600);
+ return user;
+}
+
+export async function updateUserRole(userId: string, newRole: string) {
+ const user = await getUserById(userId);
+
+ if (user.role == 'admin') {
+ throw new Error('Cannot modify admin users');
+ }
+
+ await db.query(
+ \`UPDATE users SET role = '\${newRole}' WHERE id = '\${userId}'\`
+ );
+
+ redis.del(\`user:\${userId}\`);
+}
+
+export async function deleteUser(userId: string) {
+ try {
+ await db.query('DELETE FROM users WHERE id = $1', [userId]);
+ await redis.del(\`user:\${userId}\`);
+ await redis.del(\`session:\${userId}\`);
+ } catch (e) {
+ }
+}
+
+export function processUsers(users: User[]) {
+ const results = [];
+ for (let i = 0; i <= users.length; i++) {
+ const user = users[i];
+ if (user.role = 'admin') {
+ results.push(user.email.toUpperCase());
+ }
+ }
+ return results;
+}
+
+function generateToken(user: User): string {
+ return Buffer.from(JSON.stringify({
+ id: user.id,
+ email: user.email,
+ exp: Date.now() + 86400000
+ })).toString('base64');
+}
\`\`\`
---
# PR ๋ฉํ ์ ๋ณด (์ฐธ๊ณ ์ฉ)
## PR ์ ๋ชฉ
์ฌ์ฉ์ ์ธ์ฆ ๋ฐ ์บ์ฑ ๊ธฐ๋ฅ ์ถ๊ฐ
## ์ปค๋ฐ ๋ฉ์์ง
feat: add user authentication with Redis caching
`;
const summaryInput = `์ปค๋ฐ ๋ฉ์์ง:
feat: add user authentication with Redis caching
๋ณ๊ฒฝ๋ ํ์ผ:
- src/services/userService.ts (added)
- src/db/index.ts (modified)
์ ์ฒด diff:
@@ -1,15 +1,89 @@
-import { db } from '../db';
+import { db } from '../db';
+import { Redis } from 'ioredis';
+import { hash, compare } from 'bcrypt';
+
+const redis = new Redis(process.env.REDIS_URL);
+const JWT_SECRET = "super-secret-key-12345";
... (์ดํ ๋์ผ)
`;
console.log("=".repeat(60));
console.log("๐ ์ฝ๋ ๋ฆฌ๋ทฐ ํ
์คํธ");
console.log("=".repeat(60));
console.log("\n๐ ํ
์คํธํ ์ฝ๋์ ์๋์ ์ผ๋ก ํฌํจ๋ ๋ฌธ์ ๋ค:");
console.log(" 1. SQL ์ธ์ ์
(line 17, 56-57)");
console.log(" 2. ํ๋์ฝ๋ฉ๋ JWT ์ํฌ๋ฆฟ (line 6)");
console.log(" 3. ๋น catch ๋ธ๋ก (line 69)");
console.log(" 4. off-by-one ์๋ฌ (line 73: i <= users.length)");
console.log(" 5. ํ ๋น vs ๋น๊ต ์ค๋ฅ (line 75: user.role = 'admin')");
console.log(" 6. null ์ฒดํฌ ์์ด ์ ๊ทผ (line 43: user๊ฐ undefined์ผ ์ ์์)");
console.log(" 7. == ๋์ === ์ฌ์ฉํด์ผ ํจ (line 51)");
console.log("\n");
async function runTests() {
console.log("โณ ์ฝ๋ ๋ฆฌ๋ทฐ ์์ฒญ ์ค...\n");
try {
const reviewResult = await chat.codeReview(testInput);
console.log("๐ ๋ฆฌ๋ทฐ ๊ฒฐ๊ณผ:");
console.log("-".repeat(40));
console.log(`LGTM: ${reviewResult.lgtm}`);
console.log(`๋ฐ๊ฒฌ๋ ์ด์: ${reviewResult.comments.length}๊ฐ\n`);
for (const comment of reviewResult.comments) {
const emoji = comment.severity === "critical" ? "๐ด" : "๐ ";
console.log(`${emoji} [${comment.severity.toUpperCase()}] Line ${comment.line}`);
console.log(` ์นดํ
๊ณ ๋ฆฌ: ${comment.category}`);
console.log(` ๋ด์ฉ: ${comment.body}`);
console.log();
}
console.log("\n" + "=".repeat(60));
console.log("๐ PR ์์ฝ ํ
์คํธ");
console.log("=".repeat(60));
console.log("\nโณ PR ์์ฝ ์์ฑ ์ค...\n");
const summaryResult = await chat.summarizeChanges(summaryInput);
console.log("๐ ์์ฝ ๊ฒฐ๊ณผ:");
console.log("-".repeat(40));
console.log(`๋ณ๊ฒฝ ์ ํ: ${summaryResult.changeType}`);
console.log(`์ ๋ชฉ: ${summaryResult.title}`);
console.log(`\n์์ฝ:\n${summaryResult.summary}`);
if (summaryResult.walkthrough.length > 0) {
console.log("\n๐ ์ํฌ์ค๋ฃจ:");
for (const w of summaryResult.walkthrough) {
console.log(` - ${w.file}: ${w.changes} (${w.intent})`);
}
}
if (summaryResult.affectedAreas.length > 0) {
console.log(`\n๐ฏ ์ํฅ ๋ฒ์: ${summaryResult.affectedAreas.join(", ")}`);
}
} catch (e) {
console.error("โ ์ค๋ฅ ๋ฐ์:", e);
}
}
runTests();