| 1 | import { describe, expect, it } from 'vitest';
|
| 2 | import {
|
| 3 | decryptGatewayPayload,
|
| 4 | encryptGatewayPayload,
|
| 5 | issueGatewayGrant,
|
| 6 | verifyGatewayGrant,
|
| 7 | } from '../packages/persistence/src/relay.js';
|
| 8 |
|
| 9 | describe('hosted A2A gateway cryptography', () => {
|
| 10 | const secret = 'a-test-secret-that-is-at-least-thirty-two-bytes';
|
| 11 |
|
| 12 | it('encrypts message bodies and detects tampering', () => {
|
| 13 | const encrypted = encryptGatewayPayload(secret, { message: 'private task' });
|
| 14 | expect(JSON.stringify(encrypted)).not.toContain('private task');
|
| 15 | expect(decryptGatewayPayload(secret, encrypted)).toEqual({ message: 'private task' });
|
| 16 | expect(() =>
|
| 17 | decryptGatewayPayload(secret, { ...encrypted, ciphertext: `${encrypted.ciphertext}x` }),
|
| 18 | ).toThrow();
|
| 19 | });
|
| 20 |
|
| 21 | it('issues scoped, expiring delivery grants', () => {
|
| 22 | const token = issueGatewayGrant(secret, {
|
| 23 | interactionId: 'interaction-1',
|
| 24 | senderAgentId: 'agent-a',
|
| 25 | recipientAgentId: 'agent-b',
|
| 26 | expiresAt: Date.now() + 60_000,
|
| 27 | });
|
| 28 | expect(verifyGatewayGrant(secret, token).recipientAgentId).toBe('agent-b');
|
| 29 | expect(() => verifyGatewayGrant(secret, `${token}x`)).toThrow('Invalid gateway token');
|
| 30 | });
|
| 31 | });
|