agent-delegation-e2e.test.mjs
126 lines 4.1 KB
Raw
sha256:700fafdd1afa490919f9515d660ca6e75456bcd5bb67513abcd8757a634c01f6 docs: record AIP-b SD-21 land (KN #308) Human 11 days ago
1 /**
2 * Tier 3 — E2E: seed identity/consent → mint → audit → revoke; consent expiry path.
3 */
4 import { describe, it, beforeEach, afterEach } from 'node:test';
5 import assert from 'node:assert/strict';
6 import fs from 'node:fs';
7 import path from 'node:path';
8 import { fileURLToPath } from 'node:url';
9
10 import {
11 handleDelegationGrantMintRequest,
12 handleDelegationGrantRevokeRequest,
13 handleDelegationAuditAppendRequest,
14 precheckApprovedDelegationProposal,
15 applyDelegationProposalToIndex,
16 seedDelegationFixtures,
17 DELEGATION_PROPOSAL_SOURCE,
18 } from '../lib/agent/delegation.mjs';
19 import { createProposal } from '../hub/proposals-store.mjs';
20 import {
21 writeDelegationPolicy,
22 makeAgentIdentity,
23 makeDelegationConsent,
24 TEST_USER_ID,
25 TEST_PRINCIPAL_REF,
26 } from './fixtures/agent/delegation-helpers.mjs';
27
28 const __dirname = path.dirname(fileURLToPath(import.meta.url));
29 const tmpRoot = path.join(__dirname, 'fixtures', 'tmp-agent-delegation-e2e');
30
31 describe('Agent delegation — e2e', () => {
32 const dataDir = path.join(tmpRoot, 'data');
33 const vaultId = 'default';
34
35 beforeEach(() => {
36 fs.rmSync(tmpRoot, { recursive: true, force: true });
37 fs.mkdirSync(dataDir, { recursive: true });
38 writeDelegationPolicy(dataDir);
39 process.env.DELEGATION_ENABLED = '1';
40 });
41
42 afterEach(() => {
43 fs.rmSync(tmpRoot, { recursive: true, force: true });
44 delete process.env.DELEGATION_ENABLED;
45 });
46
47 it('proposal approve → mint grant → append audit → revoke denies audit', () => {
48 const identity = makeAgentIdentity({ agentId: 'agent_e2e_test01' });
49 const consentBody = makeDelegationConsent({
50 consentId: 'dcons_e2e_test01',
51 agentId: identity.agent_id,
52 });
53 const proposal = createProposal(dataDir, {
54 path: 'meta/delegation/consents/e2e.md',
55 body: JSON.stringify(consentBody),
56 intent: 'delegation_consent_create',
57 source: DELEGATION_PROPOSAL_SOURCE,
58 vault_id: vaultId,
59 proposed_by: TEST_USER_ID,
60 delegation_meta: { record_kind: 'delegation_consent', consent_id: consentBody.consent_id },
61 });
62 seedDelegationFixtures(dataDir, vaultId, identity);
63 const precheck = precheckApprovedDelegationProposal(dataDir, proposal, { author: TEST_USER_ID });
64 assert.equal(precheck.ok, true);
65 applyDelegationProposalToIndex(dataDir, precheck);
66
67 const mint = handleDelegationGrantMintRequest({
68 dataDir,
69 vaultId,
70 consentId: consentBody.consent_id,
71 actorAgentId: identity.agent_id,
72 taskRef: 'task_hw_week3',
73 runRef: 'run_2026w25',
74 });
75 assert.equal(mint.ok, true);
76
77 const audit = handleDelegationAuditAppendRequest({
78 dataDir,
79 vaultId,
80 grantId: mint.payload.grant.grant_id,
81 actorAgentId: identity.agent_id,
82 principalRef: TEST_PRINCIPAL_REF,
83 action: 'advance_step',
84 evidenceRefs: ['proposal:prop_e2e'],
85 taskRef: 'task_hw_week3',
86 runRef: 'run_2026w25',
87 });
88 assert.equal(audit.ok, true);
89 assert.equal(audit.payload.task_ref, 'task_hw_week3');
90 assert.equal(audit.payload.run_ref, 'run_2026w25');
91
92 const revoke = handleDelegationGrantRevokeRequest({
93 dataDir,
94 vaultId,
95 grantId: mint.payload.grant.grant_id,
96 });
97 assert.equal(revoke.ok, true);
98
99 const auditAfter = handleDelegationAuditAppendRequest({
100 dataDir,
101 vaultId,
102 grantId: mint.payload.grant.grant_id,
103 actorAgentId: identity.agent_id,
104 principalRef: TEST_PRINCIPAL_REF,
105 action: 'advance_step',
106 evidenceRefs: ['proposal:prop_e2e2'],
107 });
108 assert.equal(auditAfter.ok, false);
109 assert.equal(auditAfter.code, 'DELEGATION_GRANT_REVOKED');
110 });
111
112 it('expired consent ⇒ mint fails', () => {
113 const identity = makeAgentIdentity();
114 const consent = makeDelegationConsent({ taskIds: [], flowIds: [] });
115 consent.expires_at = '2020-01-01T00:00:00Z';
116 seedDelegationFixtures(dataDir, vaultId, identity, consent);
117 const mint = handleDelegationGrantMintRequest({
118 dataDir,
119 vaultId,
120 consentId: consent.consent_id,
121 actorAgentId: identity.agent_id,
122 });
123 assert.equal(mint.ok, false);
124 assert.equal(mint.code, 'DELEGATION_CONSENT_EXPIRED');
125 });
126 });
File History 1 commit
sha256:700fafdd1afa490919f9515d660ca6e75456bcd5bb67513abcd8757a634c01f6 docs: record AIP-b SD-21 land (KN #308) Human 11 days ago