agent-delegation-e2e.test.mjs
sha256:b5f647cb9c409f563d4671fe3fc05ddea01fabfed9b41fc11cb923588e1c1baf
mirror: GitHub Phase A durable MCP OAuth (#270)
Human
minor
⚠ breaking
10 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 | delegation_meta: { record_kind: 'delegation_consent', consent_id: consentBody.consent_id }, |
| 60 | }); |
| 61 | seedDelegationFixtures(dataDir, vaultId, identity); |
| 62 | const precheck = precheckApprovedDelegationProposal(dataDir, proposal); |
| 63 | assert.equal(precheck.ok, true); |
| 64 | applyDelegationProposalToIndex(dataDir, precheck); |
| 65 | |
| 66 | const mint = handleDelegationGrantMintRequest({ |
| 67 | dataDir, |
| 68 | vaultId, |
| 69 | consentId: consentBody.consent_id, |
| 70 | actorAgentId: identity.agent_id, |
| 71 | taskRef: 'task_hw_week3', |
| 72 | runRef: 'run_2026w25', |
| 73 | }); |
| 74 | assert.equal(mint.ok, true); |
| 75 | |
| 76 | const audit = handleDelegationAuditAppendRequest({ |
| 77 | dataDir, |
| 78 | vaultId, |
| 79 | grantId: mint.payload.grant.grant_id, |
| 80 | actorAgentId: identity.agent_id, |
| 81 | principalRef: TEST_PRINCIPAL_REF, |
| 82 | action: 'advance_step', |
| 83 | evidenceRefs: ['proposal:prop_e2e'], |
| 84 | taskRef: 'task_hw_week3', |
| 85 | runRef: 'run_2026w25', |
| 86 | }); |
| 87 | assert.equal(audit.ok, true); |
| 88 | assert.equal(audit.payload.task_ref, 'task_hw_week3'); |
| 89 | assert.equal(audit.payload.run_ref, 'run_2026w25'); |
| 90 | |
| 91 | const revoke = handleDelegationGrantRevokeRequest({ |
| 92 | dataDir, |
| 93 | vaultId, |
| 94 | grantId: mint.payload.grant.grant_id, |
| 95 | }); |
| 96 | assert.equal(revoke.ok, true); |
| 97 | |
| 98 | const auditAfter = handleDelegationAuditAppendRequest({ |
| 99 | dataDir, |
| 100 | vaultId, |
| 101 | grantId: mint.payload.grant.grant_id, |
| 102 | actorAgentId: identity.agent_id, |
| 103 | principalRef: TEST_PRINCIPAL_REF, |
| 104 | action: 'advance_step', |
| 105 | evidenceRefs: ['proposal:prop_e2e2'], |
| 106 | }); |
| 107 | assert.equal(auditAfter.ok, false); |
| 108 | assert.equal(auditAfter.code, 'DELEGATION_GRANT_REVOKED'); |
| 109 | }); |
| 110 | |
| 111 | it('expired consent ⇒ mint fails', () => { |
| 112 | const identity = makeAgentIdentity(); |
| 113 | const consent = makeDelegationConsent({ taskIds: [], flowIds: [] }); |
| 114 | consent.expires_at = '2020-01-01T00:00:00Z'; |
| 115 | seedDelegationFixtures(dataDir, vaultId, identity, consent); |
| 116 | const mint = handleDelegationGrantMintRequest({ |
| 117 | dataDir, |
| 118 | vaultId, |
| 119 | consentId: consent.consent_id, |
| 120 | actorAgentId: identity.agent_id, |
| 121 | }); |
| 122 | assert.equal(mint.ok, false); |
| 123 | assert.equal(mint.code, 'DELEGATION_CONSENT_EXPIRED'); |
| 124 | }); |
| 125 | }); |
File History
2 commits
sha256:b5f647cb9c409f563d4671fe3fc05ddea01fabfed9b41fc11cb923588e1c1baf
mirror: GitHub Phase A durable MCP OAuth (#270)
Human
minor
⚠
10 days ago
sha256:d8c648b20a4d53b2673c5c082ee7edfa7b2fc9b11080832da1f38807b6bf940b
fix(7C-L1b): route hosted delegation proposals through cani…
Human
minor
⚠
30 days ago