feat: 添加构建时间信息和相关功能

This commit is contained in:
2026-03-20 22:13:06 +08:00
parent 72e974672e
commit d8d323e293
6 changed files with 95 additions and 7 deletions

View File

@@ -1,12 +1,16 @@
const test = require('node:test')
const assert = require('node:assert/strict')
const request = require('supertest')
const fs = require('fs')
const path = require('path')
const env = require('../../src/config/env')
const { createApp } = require('../../src/index')
const userService = require('../../src/services/userService')
test('POST /api/health 返回统一结构', async () => {
const buildInfoPath = path.resolve(__dirname, '..', '..', 'build-info.json')
test('POST /api/health 仍返回统一结构', async () => {
const app = createApp()
const response = await request(app).post('/api/health').send({})
@@ -16,6 +20,36 @@ test('POST /api/health 返回统一结构', async () => {
assert.equal(response.body.data.status, 'healthy')
})
test('POST /api/test-helloworld 返回统一结构和构建时间', async (t) => {
const app = createApp()
const buildTime = '2026-03-20T08:00:00.000Z'
fs.writeFileSync(buildInfoPath, JSON.stringify({ buildTime }, null, 2))
t.after(() => {
if (fs.existsSync(buildInfoPath)) {
fs.rmSync(buildInfoPath)
}
})
const response = await request(app).post('/api/test-helloworld').send({})
assert.equal(response.status, 200)
assert.equal(response.body.code, 200)
assert.equal(response.body.msg, '请求成功')
assert.equal(response.body.data.message, 'Hello, World!')
assert.equal(response.body.data.build_time, buildTime)
})
test('GET /api/test-helloworld 返回 404', async () => {
const app = createApp()
const response = await request(app).get('/api/test-helloworld')
assert.equal(response.status, 404)
assert.equal(response.body.code, 404)
assert.equal(response.body.msg, 'Route not found')
})
test('POST /api/test-helloworld 返回统一结构', async () => {
const app = createApp()
const response = await request(app).post('/api/test-helloworld').send({})
@@ -24,6 +58,7 @@ test('POST /api/test-helloworld 返回统一结构', async () => {
assert.equal(response.body.code, 200)
assert.equal(response.body.msg, '请求成功')
assert.equal(response.body.data.message, 'Hello, World!')
assert.ok(Object.prototype.hasOwnProperty.call(response.body.data, 'build_time'))
})
test('未匹配路由返回统一 404', async () => {