|
| 1 | +// SPDX-FileCopyrightText: © 2025 Phala Network <dstack@phala.network> |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +/** |
| 6 | + * Serde contract tests: verify that TypeScript types correctly |
| 7 | + * parse/produce the same JSON fixtures used by the Rust KMS tests. |
| 8 | + * |
| 9 | + * Direction-aware: |
| 10 | + * - BootInfo: KMS→auth-eth → TS is the consumer → test deserialization |
| 11 | + * - BootResponse: auth-eth→KMS → TS is the producer → test serialization |
| 12 | + * - PolicyResponse: auth-eth→KMS → TS is the producer → test serialization |
| 13 | + */ |
| 14 | + |
| 15 | +import { BootInfo, BootResponse, PolicyResponse } from '../src/types'; |
| 16 | +import * as fs from 'fs'; |
| 17 | +import * as path from 'path'; |
| 18 | + |
| 19 | +const FIXTURES_DIR = path.resolve(__dirname, '../../tests/fixtures'); |
| 20 | + |
| 21 | +describe('Serde contract tests (shared fixtures)', () => { |
| 22 | + describe('BootInfo (KMS→auth-eth: TS deserializes)', () => { |
| 23 | + it('should deserialize the shared fixture produced by Rust', () => { |
| 24 | + const json = JSON.parse(fs.readFileSync(path.join(FIXTURES_DIR, 'boot_info.json'), 'utf8')); |
| 25 | + const info: BootInfo = json; |
| 26 | + // Verify all required fields are accessible after deserialization |
| 27 | + expect(typeof info.mrAggregated).toBe('string'); |
| 28 | + expect(typeof info.osImageHash).toBe('string'); |
| 29 | + expect(typeof info.mrSystem).toBe('string'); |
| 30 | + expect(typeof info.appId).toBe('string'); |
| 31 | + expect(typeof info.composeHash).toBe('string'); |
| 32 | + expect(typeof info.instanceId).toBe('string'); |
| 33 | + expect(typeof info.deviceId).toBe('string'); |
| 34 | + expect(typeof info.tcbStatus).toBe('string'); |
| 35 | + expect(Array.isArray(info.advisoryIds)).toBe(true); |
| 36 | + expect(info.tcbStatus).toBe('UpToDate'); |
| 37 | + expect(info.advisoryIds).toEqual(['INTEL-SA-00001']); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should contain all fields expected by the server schema', () => { |
| 41 | + const json = JSON.parse(fs.readFileSync(path.join(FIXTURES_DIR, 'boot_info.json'), 'utf8')); |
| 42 | + const requiredFields = ['mrAggregated', 'osImageHash', 'appId', 'composeHash', 'instanceId', 'deviceId']; |
| 43 | + for (const field of requiredFields) { |
| 44 | + expect(json).toHaveProperty(field); |
| 45 | + } |
| 46 | + }); |
| 47 | + }); |
| 48 | + |
| 49 | + describe('BootResponse (auth-eth→KMS: TS serializes)', () => { |
| 50 | + it('should produce JSON matching the shared fixture consumed by Rust', () => { |
| 51 | + const fixture = JSON.parse(fs.readFileSync(path.join(FIXTURES_DIR, 'boot_response.json'), 'utf8')); |
| 52 | + // Construct the response as auth-eth would |
| 53 | + const resp: BootResponse = { |
| 54 | + isAllowed: true, |
| 55 | + gatewayAppId: '0x1234567890abcdef1234567890abcdef12345678', |
| 56 | + reason: '', |
| 57 | + }; |
| 58 | + expect(JSON.parse(JSON.stringify(resp))).toEqual(fixture); |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + describe('PolicyResponse (auth-eth→KMS: TS serializes)', () => { |
| 63 | + it('should produce JSON matching the shared fixture consumed by Rust', () => { |
| 64 | + const fixture = JSON.parse(fs.readFileSync(path.join(FIXTURES_DIR, 'policy_response.json'), 'utf8')); |
| 65 | + // Construct the response as auth-eth would |
| 66 | + const resp: PolicyResponse = { |
| 67 | + tcbPolicy: '{"version":1,"intel_qal":[]}', |
| 68 | + }; |
| 69 | + expect(JSON.parse(JSON.stringify(resp))).toEqual(fixture); |
| 70 | + }); |
| 71 | + }); |
| 72 | +}); |
0 commit comments