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