Skip to content

Commit 11d5e76

Browse files
committed
fix: streamline JWT token generation tests and enhance user model tests by removing unnecessary imports and simplifying error handling in user service
1 parent 808017b commit 11d5e76

File tree

8 files changed

+80
-23
lines changed

8 files changed

+80
-23
lines changed

backend-nodejs/domains/auth/service/jwt_service.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ describe('JWTService', () => {
4040
});
4141

4242
it('生成的 Token 应该在指定时间后过期', () => {
43-
const { token, expiresAt } = jwtService.generateAccessToken(testUserId, testEmail);
43+
const { expiresAt } = jwtService.generateAccessToken(testUserId, testEmail);
4444
const expectedExpiry = Date.now() + config.accessTokenExpiry * 1000;
4545

4646
// 允许 1 秒的误差
@@ -69,7 +69,7 @@ describe('JWTService', () => {
6969
});
7070

7171
it('生成的 Token 应该在指定时间后过期', () => {
72-
const { token, expiresAt } = jwtService.generateRefreshToken(testUserId);
72+
const { expiresAt } = jwtService.generateRefreshToken(testUserId);
7373
const expectedExpiry = Date.now() + config.refreshTokenExpiry * 1000;
7474

7575
// 允许 1 秒的误差

backend-nodejs/domains/user/model/user.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import { describe, it, expect } from 'vitest';
66
import { User, UserStatuses } from './user.js';
7-
import bcrypt from 'bcryptjs';
87

98
describe('User Model', () => {
109
describe('create', () => {

backend-nodejs/domains/user/model/user.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ function normalizeEmail(email: string): string {
201201
* 验证邮箱格式
202202
*/
203203
function isValidEmail(email: string): boolean {
204-
const emailRegex = /^[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}$/;
204+
const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
205205
return emailRegex.test(email);
206206
}
207207

backend-nodejs/domains/user/service/user_service.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,7 @@ export class UserService {
108108
}
109109

110110
// Step 4: 更新用户字段
111-
try {
112-
user.updateProfile(input.username, input.fullName, input.avatarURL);
113-
} catch (error) {
114-
throw error; // 直接抛出 Model 层的错误
115-
}
111+
user.updateProfile(input.username, input.fullName, input.avatarURL);
116112

117113
// Step 5: 保存用户
118114
await this.userRepo.update(ctx, user);
@@ -156,11 +152,7 @@ export class UserService {
156152
}
157153

158154
// Step 4 & 5: 更新密码(包含哈希)
159-
try {
160-
await user.updatePassword(input.newPassword);
161-
} catch (error) {
162-
throw error; // 直接抛出 Model 层的错误
163-
}
155+
await user.updatePassword(input.newPassword);
164156

165157
// 保存到数据库
166158
await this.userRepo.update(ctx, user);

backend-nodejs/domains/user/tests/change_password.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import { registerUserRoutes } from '../http/router.js';
1111
import { createAuthMiddleware } from '../../../infrastructure/middleware/auth.js';
1212
import { JWTService } from '../../auth/service/jwt_service.js';
1313
import { UserRepositoryImpl } from '../repository/user_repo.js';
14-
import { User } from '../model/user.js';
1514

1615
describe('ChangePassword Handler', () => {
1716
let app: FastifyInstance;

backend-nodejs/infrastructure/monitoring/logger/logger.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,15 @@ export class Logger {
123123
*/
124124
function createOutputStream(config: LoggerConfig): NodeJS.WritableStream {
125125
switch (config.output) {
126-
case 'stdout':
126+
case 'stdout': {
127127
return process.stdout;
128+
}
128129

129-
case 'stderr':
130+
case 'stderr': {
130131
return process.stderr;
132+
}
131133

132-
case 'file':
134+
case 'file': {
133135
if (!config.outputPath) {
134136
throw new Error('outputPath is required when output=file');
135137
}
@@ -171,9 +173,11 @@ function createOutputStream(config: LoggerConfig): NodeJS.WritableStream {
171173
});
172174

173175
return rotatingStream;
176+
}
174177

175-
default:
178+
default: {
176179
return process.stdout;
180+
}
177181
}
178182
}
179183

scripts/pre-commit-format.sh

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ fi
2222

2323
HAS_GO_FILES=false
2424
HAS_FRONTEND_FILES=false
25+
HAS_BACKEND_NODEJS_FILES=false
2526

2627
# 检查是否有 Go 文件
2728
for file in $STAGED_FILES; do
@@ -39,6 +40,14 @@ for file in $STAGED_FILES; do
3940
fi
4041
done
4142

43+
# 检查是否有 backend-nodejs 文件
44+
for file in $STAGED_FILES; do
45+
if [[ "$file" == backend-nodejs/* ]] && [[ "$file" =~ \.(ts)$ ]]; then
46+
HAS_BACKEND_NODEJS_FILES=true
47+
break
48+
fi
49+
done
50+
4251
# 格式化 Go 代码
4352
if [ "$HAS_GO_FILES" = true ]; then
4453
echo "📐 Formatting Go code..."
@@ -121,7 +130,60 @@ if [ "$HAS_FRONTEND_FILES" = true ]; then
121130
echo ""
122131
fi
123132

133+
# 检查 backend-nodejs ESLint
134+
if [ "$HAS_BACKEND_NODEJS_FILES" = true ]; then
135+
echo "🔍 Checking backend-nodejs ESLint..."
136+
137+
# 检查是否有 pnpm
138+
if ! command -v pnpm >/dev/null 2>&1; then
139+
echo "⚠️ pnpm not found, skipping ESLint check"
140+
echo " Install with: npm install -g pnpm"
141+
else
142+
cd "$ROOT_DIR/backend-nodejs"
143+
144+
# 收集需要检查的 TypeScript 文件
145+
TS_FILES=()
146+
for file in $STAGED_FILES; do
147+
if [[ "$file" == backend-nodejs/* ]] && [[ "$file" =~ \.(ts)$ ]]; then
148+
REL_PATH="${file#backend-nodejs/}"
149+
if [ -f "$ROOT_DIR/backend-nodejs/$REL_PATH" ]; then
150+
TS_FILES+=("$REL_PATH")
151+
fi
152+
fi
153+
done
154+
155+
if [ ${#TS_FILES[@]} -gt 0 ]; then
156+
# 运行 ESLint 检查(只检查错误,警告不会阻止提交)
157+
echo " Checking ${#TS_FILES[@]} TypeScript file(s)..."
158+
159+
# 运行 lint 并捕获输出和退出码
160+
LINT_OUTPUT=$(pnpm lint 2>&1)
161+
LINT_EXIT_CODE=$?
162+
163+
# 检查是否有错误(非零退出码表示有错误)
164+
if [ $LINT_EXIT_CODE -eq 0 ]; then
165+
echo "✅ ESLint check passed (warnings are allowed)"
166+
else
167+
echo ""
168+
echo "❌ ESLint check failed! Found errors that must be fixed."
169+
echo ""
170+
echo "$LINT_OUTPUT" | grep -E "(error|✖)" | head -20
171+
echo ""
172+
echo " Please fix the errors before committing."
173+
echo " Run 'cd backend-nodejs && pnpm lint' to see all issues."
174+
echo " Or run 'cd backend-nodejs && pnpm lint:fix' to auto-fix some issues."
175+
echo ""
176+
cd "$ROOT_DIR"
177+
exit 1
178+
fi
179+
fi
180+
181+
cd "$ROOT_DIR"
182+
fi
183+
echo ""
184+
fi
185+
124186
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
125-
echo "✅ Code formatting complete!"
187+
echo "✅ Code formatting and linting complete!"
126188
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
127189

scripts/setup-pre-commit.sh

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,10 @@ chmod +x "$PRE_COMMIT_HOOK"
3737

3838
echo "✅ Pre-commit hook installed at: $PRE_COMMIT_HOOK"
3939
echo ""
40-
echo "📝 The hook will automatically format:"
41-
echo " • Go files (using gofmt/goimports)"
42-
echo " • Frontend files (using prettier)"
40+
echo "📝 The hook will automatically:"
41+
echo " • Format Go files (using gofmt/goimports)"
42+
echo " • Format Frontend files (using prettier)"
43+
echo " • Check backend-nodejs ESLint (errors will block commit)"
4344
echo ""
4445
echo "🎯 To test the hook, try:"
4546
echo " git add <some files>"

0 commit comments

Comments
 (0)