feat: add format util unit and add pre-commit unit check (#8427)
Some checks are pending
Build and Push API & Web / build (api, DIFY_API_IMAGE_NAME, linux/amd64, build-api-amd64) (push) Waiting to run
Build and Push API & Web / build (api, DIFY_API_IMAGE_NAME, linux/arm64, build-api-arm64) (push) Waiting to run
Build and Push API & Web / build (web, DIFY_WEB_IMAGE_NAME, linux/amd64, build-web-amd64) (push) Waiting to run
Build and Push API & Web / build (web, DIFY_WEB_IMAGE_NAME, linux/arm64, build-web-arm64) (push) Waiting to run
Build and Push API & Web / create-manifest (api, DIFY_API_IMAGE_NAME, merge-api-images) (push) Blocked by required conditions
Build and Push API & Web / create-manifest (web, DIFY_WEB_IMAGE_NAME, merge-web-images) (push) Blocked by required conditions

This commit is contained in:
HJY 2024-09-19 10:39:27 +08:00 committed by GitHub
parent 41bea4cafa
commit 2721cb8dee
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 95 additions and 3 deletions

View File

@ -36,7 +36,7 @@
| 被团队成员标记为高优先级的功能 | 高优先级 | | 被团队成员标记为高优先级的功能 | 高优先级 |
| 在 [community feedback board](https://github.com/langgenius/dify/discussions/categories/feedbacks) 内反馈的常见功能请求 | 中等优先级 | | 在 [community feedback board](https://github.com/langgenius/dify/discussions/categories/feedbacks) 内反馈的常见功能请求 | 中等优先级 |
| 非核心功能和小幅改进 | 低优先级 | | 非核心功能和小幅改进 | 低优先级 |
| 有价值不紧急 | 未来功能 | | 有价值不紧急 | 未来功能 |
### 其他任何事情(例如 bug 报告、性能优化、拼写错误更正): ### 其他任何事情(例如 bug 报告、性能优化、拼写错误更正):
* 立即开始编码。 * 立即开始编码。

View File

@ -51,5 +51,32 @@ if $web_modified; then
echo "Running ESLint on web module" echo "Running ESLint on web module"
cd ./web || exit 1 cd ./web || exit 1
npx lint-staged npx lint-staged
echo "Running unit tests check"
modified_files=$(git diff --cached --name-only -- utils | grep -v '\.spec\.ts$' || true)
if [ -n "$modified_files" ]; then
for file in $modified_files; do
test_file="${file%.*}.spec.ts"
echo "Checking for test file: $test_file"
# check if the test file exists
if [ -f "../$test_file" ]; then
echo "Detected changes in $file, running corresponding unit tests..."
npm run test "../$test_file"
if [ $? -ne 0 ]; then
echo "Unit tests failed. Please fix the errors before committing."
exit 1
fi
echo "Unit tests for $file passed."
else
echo "Warning: $file does not have a corresponding test file."
fi
done
echo "All unit tests for modified web/utils files have passed."
fi
cd ../ cd ../
fi fi

View File

@ -18,6 +18,10 @@ yarn install --frozen-lockfile
Then, configure the environment variables. Create a file named `.env.local` in the current directory and copy the contents from `.env.example`. Modify the values of these environment variables according to your requirements: Then, configure the environment variables. Create a file named `.env.local` in the current directory and copy the contents from `.env.example`. Modify the values of these environment variables according to your requirements:
```bash
cp .env.example .env.local
```
``` ```
# For production release, change this to PRODUCTION # For production release, change this to PRODUCTION
NEXT_PUBLIC_DEPLOY_ENV=DEVELOPMENT NEXT_PUBLIC_DEPLOY_ENV=DEVELOPMENT

61
web/utils/format.spec.ts Normal file
View File

@ -0,0 +1,61 @@
import { formatFileSize, formatNumber, formatTime } from './format'
describe('formatNumber', () => {
test('should correctly format integers', () => {
expect(formatNumber(1234567)).toBe('1,234,567')
})
test('should correctly format decimals', () => {
expect(formatNumber(1234567.89)).toBe('1,234,567.89')
})
test('should correctly handle string input', () => {
expect(formatNumber('1234567')).toBe('1,234,567')
})
test('should correctly handle zero', () => {
expect(formatNumber(0)).toBe(0)
})
test('should correctly handle negative numbers', () => {
expect(formatNumber(-1234567)).toBe('-1,234,567')
})
test('should correctly handle empty input', () => {
expect(formatNumber('')).toBe('')
})
})
describe('formatFileSize', () => {
test('should return the input if it is falsy', () => {
expect(formatFileSize(0)).toBe(0)
})
test('should format bytes correctly', () => {
expect(formatFileSize(500)).toBe('500.00B')
})
test('should format kilobytes correctly', () => {
expect(formatFileSize(1500)).toBe('1.46KB')
})
test('should format megabytes correctly', () => {
expect(formatFileSize(1500000)).toBe('1.43MB')
})
test('should format gigabytes correctly', () => {
expect(formatFileSize(1500000000)).toBe('1.40GB')
})
test('should format terabytes correctly', () => {
expect(formatFileSize(1500000000000)).toBe('1.36TB')
})
test('should format petabytes correctly', () => {
expect(formatFileSize(1500000000000000)).toBe('1.33PB')
})
})
describe('formatTime', () => {
test('should return the input if it is falsy', () => {
expect(formatTime(0)).toBe(0)
})
test('should format seconds correctly', () => {
expect(formatTime(30)).toBe('30.00 sec')
})
test('should format minutes correctly', () => {
expect(formatTime(90)).toBe('1.50 min')
})
test('should format hours correctly', () => {
expect(formatTime(3600)).toBe('1.00 h')
})
test('should handle large numbers', () => {
expect(formatTime(7200)).toBe('2.00 h')
})
})