feat(api): cn-app skeleton, toolchain, env loading convention

Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
浅念
2026-09-17 19:14:41 +08:00
parent f77ab5ae4b
commit 7d250ec342
21 changed files with 1655 additions and 27 deletions
+47
View File
@@ -0,0 +1,47 @@
// services/api/test/health.e2e-spec.ts
// e2e 冒烟:验证 e2e 工装(Nest testing module + supertest + 根 .env 加载)可用。
// 不监听端口:app.init() 后直接对 app.getHttpServer() 发请求。
// 全局前缀与 main-cn.ts 保持一致('v1'),由 spec 显式设置。
import { type INestApplication } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import request from 'supertest';
import { AppCnModule } from '../src/app-cn.module';
import { resetDb } from './reset';
describe('GET /v1/health (e2e)', () => {
let app: INestApplication;
beforeAll(async () => {
await resetDb();
const moduleRef = await Test.createTestingModule({
imports: [AppCnModule],
}).compile();
app = moduleRef.createNestApplication();
app.setGlobalPrefix('v1');
await app.init();
});
afterAll(async () => {
await app.close();
});
it('responds 200 {"status":"ok"}', async () => {
const res = await request(app.getHttpServer()).get('/v1/health');
expect(res.status).toBe(200);
expect(res.body).toEqual({ status: 'ok' });
});
it('sets x-request-id response header (generated when absent)', async () => {
const res = await request(app.getHttpServer()).get('/v1/health');
const requestId = res.headers['x-request-id'];
expect(typeof requestId).toBe('string');
expect(requestId.length ?? 0).toBeGreaterThan(0);
});
it('echoes incoming x-request-id header', async () => {
const res = await request(app.getHttpServer())
.get('/v1/health')
.set('x-request-id', 'e2e-fixed-request-id');
expect(res.headers['x-request-id']).toBe('e2e-fixed-request-id');
});
});
+7
View File
@@ -0,0 +1,7 @@
// services/api/test/load-env.ts
// 全仓库唯一 env 加载机制(Jest 侧):仓库根 .env。开发/迁移脚本经根 dotenv-cli
// 注入;运行时代码只读 process.env。worker/storage 包的 test/load-env.ts 复制同约定。
import { join } from 'node:path';
import { config } from 'dotenv';
config({ path: join(__dirname, '../../../.env') });
+17
View File
@@ -0,0 +1,17 @@
// services/api/test/reset.ts
// e2e 共用隔离工具:每个 e2e spec 的 beforeAll 调用 resetDb(),防止 spec 间
// 唯一约束冲突与脏数据。
// TODO(Task 8): 用 Prisma 实现 RESET_TABLES 中全部表的 truncate-if-exists
// (Task 8-10 逐步登记表名,届时空列表不再成立)。
// TODO(Task 11): 增加 MinIO `photo-live/dev` 前缀对象清理(同一函数内追加)。
export const RESET_TABLES: readonly string[] = [];
export async function resetDb(): Promise<void> {
if (RESET_TABLES.length === 0) {
// 尚未登记任何表(Task 8 前数据库无表):干净地跳过。
return;
}
throw new Error(
'resetDb: RESET_TABLES is non-empty but truncation is not implemented until Task 8',
);
}