// services/api/test/health.e2e-spec.ts // e2e 冒烟:验证 e2e 工装(Nest testing module + supertest + 根 .env 加载)可用。 // 不监听端口:app.init() 后直接对 app.getHttpServer() 发请求。 // 应用级配置与 main-cn.ts 共用 src/shared/configure-app(P1a 终审修缮后收敛为 // 单一来源),故本 spec 对响应头/未知路由的断言即生产引导路径的行为: // · /health 基本形状 + x-request-id 回显/生成/超长替换; // · 未注册路由 → 404 信封 code=NOT_FOUND(终审修缮①:框架级 404 不再冒充 // ALBUM_NOT_FOUND;业务 404 一律经 ApiException 显式携带域码); // · 响应不暴露 x-powered-by 指纹头(终审修缮②)。 import { Test } from '@nestjs/testing'; import { type NestExpressApplication } from '@nestjs/platform-express'; import request from 'supertest'; import { AppCnModule } from '../src/app-cn.module'; import { API_PREFIX } from '../src/shared/constants'; import { configureApp } from '../src/shared/configure-app'; import { resetDb } from './reset'; describe(`e2e smoke: /${API_PREFIX}/health & framework defaults`, () => { let app: NestExpressApplication; beforeAll(async () => { await resetDb(); const moduleRef = await Test.createTestingModule({ imports: [AppCnModule], }).compile(); app = moduleRef.createNestApplication(); configureApp(app); await app.init(); }); afterAll(async () => { await app.close(); }); it('responds 200 {"status":"ok"}', async () => { const res = await request(app.getHttpServer()).get(`/${API_PREFIX}/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(`/${API_PREFIX}/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(`/${API_PREFIX}/health`) .set('x-request-id', 'e2e-fixed-request-id'); expect(res.headers['x-request-id']).toBe('e2e-fixed-request-id'); }); it('replaces an over-long incoming x-request-id (>128 chars) with a fresh one', async () => { const overlong = 'z'.repeat(200); const res = await request(app.getHttpServer()) .get(`/${API_PREFIX}/health`) .set('x-request-id', overlong); const requestId = res.headers['x-request-id']; expect(typeof requestId).toBe('string'); expect(requestId).not.toBe(overlong); expect((requestId as string).length).toBeLessThanOrEqual(128); }); it('responds 404 error envelope with code NOT_FOUND for unknown routes', async () => { const res = await request(app.getHttpServer()).get( `/${API_PREFIX}/nonexistent-route`, ); expect(res.status).toBe(404); expect(res.body).toMatchObject({ code: 'NOT_FOUND', requestId: expect.any(String) as string, }); expect(typeof res.body.message).toBe('string'); }); it('omits the x-powered-by response header', async () => { const res = await request(app.getHttpServer()).get(`/${API_PREFIX}/health`); expect(res.headers['x-powered-by']).toBeUndefined(); }); });