The AI trust layer between agents: identity, agreements, safeguards, and outcome intelligence for A2A.

Viewing files at commit 3dc3c2c6da63Commit detailsView latest

tests/protocol.test.ts 52 lines · 1.8 KB · typescript
1
import { describe, expect, it } from 'vitest';
2
import { createIdentity, TrustEngine } from '@openclasp/core';
3
import { canonicalHash, signObject, verifyObject } from '@openclasp/protocol';
4
5
describe('protocol cryptography and delegation', () => {
6
  it('canonicalizes object key order and rejects tampering', () => {
7
    expect(canonicalHash({ b: 2, a: 1 })).toBe(canonicalHash({ a: 1, b: 2 }));
8
    const agent = createIdentity({
9
      agentId: 'agent:a',
10
      operatorRef: 'operator:a',
11
      capabilities: ['write'],
12
    });
13
    const signed = signObject({ action: 'write', value: 1 }, agent.keyPair);
14
    expect(verifyObject(signed, agent.identity.publicKey)).toBe(true);
15
    expect(verifyObject({ ...signed, value: 2 }, agent.identity.publicKey)).toBe(false);
16
  });
17
18
  it('prevents authority escalation and rejects expired delegation', () => {
19
    const engine = new TrustEngine();
20
    const parent = createIdentity({
21
      agentId: 'agent:parent',
22
      operatorRef: 'operator:x',
23
      capabilities: ['read'],
24
    });
25
    const child = createIdentity({
26
      agentId: 'agent:child',
27
      operatorRef: 'operator:x',
28
      capabilities: ['read'],
29
      parentAgentId: parent.identity.agentId,
30
      rootControllerId: parent.identity.rootControllerId,
31
    });
32
    engine.registerAgent(parent.identity);
33
    engine.registerAgent(child.identity);
34
    expect(() =>
35
      engine.createDelegation(
36
        parent.identity.agentId,
37
        child.identity.agentId,
38
        ['write'],
39
        new Date(Date.now() + 1000).toISOString(),
40
        parent.keyPair,
41
      ),
42
    ).toThrow('exceeds parent authority');
43
    const expired = engine.createDelegation(
44
      parent.identity.agentId,
45
      child.identity.agentId,
46
      ['read'],
47
      new Date(Date.now() - 1).toISOString(),
48
      parent.keyPair,
49
    );
50
    expect(engine.verifyDelegation(expired.delegationId)).toBe(false);
51
  });
52
});