From 7cf0c50921e0e24b15d2333ba01dd1e3a6a5cd3e Mon Sep 17 00:00:00 2001 From: nav Date: Tue, 12 May 2026 12:26:03 +0000 Subject: [PATCH] test(contract): add center-guild registration contract integration test --- .../src/health.integration.spec.ts | 31 ++++++++++++++++++- docs/TODO-backend-center-guild.md | 2 +- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/Fabric.Backend.Center/src/health.integration.spec.ts b/Fabric.Backend.Center/src/health.integration.spec.ts index 022a5a0..1b8586d 100644 --- a/Fabric.Backend.Center/src/health.integration.spec.ts +++ b/Fabric.Backend.Center/src/health.integration.spec.ts @@ -2,13 +2,14 @@ import { INestApplication } from '@nestjs/common'; import { Test } from '@nestjs/testing'; import request from 'supertest'; import { afterAll, beforeAll, describe, expect, it } from 'vitest'; +import { createHmac, randomUUID } from 'crypto'; process.env.DB_HOST = '127.0.0.1'; process.env.DB_PORT = '3307'; process.env.DB_USER = 'fabric'; process.env.DB_PASSWORD = 'fabric'; process.env.DB_NAME = 'fabric_center'; -process.env.DB_SYNC = 'false'; +process.env.DB_SYNC = 'true'; process.env.CENTER_SHARED_SECRET = 'test-center-secret'; process.env.JWT_ACCESS_SECRET = 'test-access-secret'; process.env.JWT_REFRESH_SECRET = 'test-refresh-secret'; @@ -37,4 +38,32 @@ describe('center integration (mysql + api)', () => { expect(res.body.ok).toBe(true); expect(res.body.database).toBe('ready'); }); + + it('POST /api/nodes/register follows center-guild contract (version + hmac)', async () => { + const body = { + nodeId: `guild-node-${Date.now()}`, + name: 'Guild Node Contract Test', + endpoint: `http://guild-${Date.now()}:7002`, + }; + + const timestamp = new Date().toISOString(); + const nonce = randomUUID(); + const canonical = ['POST', '/api/nodes/register', timestamp, nonce, JSON.stringify(body)].join('\n'); + const signature = createHmac('sha256', process.env.CENTER_SHARED_SECRET as string) + .update(canonical) + .digest('hex'); + + const res = await request(app.getHttpServer()) + .post('/api/nodes/register') + .set('x-fabric-version', '1') + .set('x-fabric-timestamp', timestamp) + .set('x-fabric-nonce', nonce) + .set('x-fabric-signature', signature) + .send(body); + + expect(res.status).toBe(201); + expect(res.body.status).toBe('accepted'); + expect(res.body.negotiatedVersion).toBe('1'); + expect(res.body.node.nodeId).toBe(body.nodeId); + }); }); diff --git a/docs/TODO-backend-center-guild.md b/docs/TODO-backend-center-guild.md index cfcf3dc..c275c63 100644 --- a/docs/TODO-backend-center-guild.md +++ b/docs/TODO-backend-center-guild.md @@ -83,7 +83,7 @@ ### 5.1 自动化测试 - [x] 单元测试(auth/service/message/seq) - [x] 集成测试(MySQL + API) -- [ ] 合约测试(Center-Guild 协议) +- [x] 合约测试(Center-Guild 协议) ### 5.2 质量门禁 - [x] lint/typecheck/build 全绿