| 1 | import { describe, expect, it } from 'vitest';
|
| 2 | import {
|
| 3 | decryptGatewayPayload,
|
| 4 | encryptGatewayPayload,
|
| 5 | issueGatewayGrant,
|
| 6 | issueSessionGrant,
|
| 7 | verifyGatewayGrant,
|
| 8 | verifySessionGrant,
|
| 9 | } from '../packages/persistence/src/relay.js';
|
| 10 |
|
| 11 | describe('hosted runtime and session cryptography', () => {
|
| 12 | const secret = 'a-test-secret-that-is-at-least-thirty-two-bytes';
|
| 13 |
|
| 14 | it('encrypts message bodies and detects tampering', () => {
|
| 15 | const encrypted = encryptGatewayPayload(secret, { message: 'private task' });
|
| 16 | expect(JSON.stringify(encrypted)).not.toContain('private task');
|
| 17 | expect(decryptGatewayPayload(secret, encrypted)).toEqual({ message: 'private task' });
|
| 18 | expect(() =>
|
| 19 | decryptGatewayPayload(secret, { ...encrypted, ciphertext: `${encrypted.ciphertext}x` }),
|
| 20 | ).toThrow();
|
| 21 | });
|
| 22 |
|
| 23 | it('issues scoped, expiring delivery grants', () => {
|
| 24 | const token = issueGatewayGrant(secret, {
|
| 25 | interactionId: 'interaction-1',
|
| 26 | senderAgentId: 'agent-a',
|
| 27 | recipientAgentId: 'agent-b',
|
| 28 | expiresAt: Date.now() + 60_000,
|
| 29 | });
|
| 30 | expect(verifyGatewayGrant(secret, token).recipientAgentId).toBe('agent-b');
|
| 31 | expect(() => verifyGatewayGrant(secret, `${token}x`)).toThrow('Invalid gateway token');
|
| 32 | });
|
| 33 |
|
| 34 | it('issues platform-signed live-session credentials', () => {
|
| 35 | const token = issueSessionGrant(secret, {
|
| 36 | interactionId: 'interaction-1',
|
| 37 | senderAgentId: 'agent-a',
|
| 38 | recipientAgentId: 'agent-b',
|
| 39 | expiresAt: Date.now() + 60_000,
|
| 40 | });
|
| 41 | expect(verifySessionGrant(secret, token)).toMatchObject({
|
| 42 | interactionId: 'interaction-1',
|
| 43 | senderAgentId: 'agent-a',
|
| 44 | recipientAgentId: 'agent-b',
|
| 45 | });
|
| 46 | expect(() => verifySessionGrant(secret, `${token}x`)).toThrow(
|
| 47 | 'Invalid live-session credential',
|
| 48 | );
|
| 49 | });
|
| 50 | });
|