agent-delegation-parity-integration.test.mjs
sha256:baa800aedf841dbf32081aa7b2befa288ac33dfc7175ac55014c55c4d8c742f9
docs: move durable-auth freeze/evidence to local developmen…
Human
10 days ago
| 1 | /** |
| 2 | * Tier 2 — INTEGRATION: Hub = CLI handler parity; gate off ⇒ DELEGATION_DISABLED. |
| 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 | handleDelegationGrantListRequest, |
| 13 | DELEGATION_POLICY_FILE, |
| 14 | seedDelegationFixtures, |
| 15 | } from '../lib/agent/delegation.mjs'; |
| 16 | import { |
| 17 | writeDelegationPolicy, |
| 18 | makeAgentIdentity, |
| 19 | makeDelegationConsent, |
| 20 | } from './fixtures/agent/delegation-helpers.mjs'; |
| 21 | |
| 22 | const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 23 | const tmpRoot = path.join(__dirname, 'fixtures', 'tmp-agent-delegation-parity'); |
| 24 | |
| 25 | function stripVolatileMint(payload) { |
| 26 | const copy = structuredClone(payload); |
| 27 | delete copy.bearer; |
| 28 | if (copy.grant) { |
| 29 | delete copy.grant.grant_id; |
| 30 | delete copy.grant.expires_at; |
| 31 | delete copy.grant.issued_at; |
| 32 | } |
| 33 | delete copy.expires_at; |
| 34 | return copy; |
| 35 | } |
| 36 | |
| 37 | describe('Agent delegation — parity integration', () => { |
| 38 | const dataDir = path.join(tmpRoot, 'data'); |
| 39 | const vaultId = 'default'; |
| 40 | |
| 41 | beforeEach(() => { |
| 42 | fs.rmSync(tmpRoot, { recursive: true, force: true }); |
| 43 | fs.mkdirSync(dataDir, { recursive: true }); |
| 44 | writeDelegationPolicy(dataDir); |
| 45 | process.env.DELEGATION_ENABLED = '1'; |
| 46 | const identity = makeAgentIdentity(); |
| 47 | const consent = makeDelegationConsent({ agentId: identity.agent_id }); |
| 48 | seedDelegationFixtures(dataDir, vaultId, identity, consent); |
| 49 | }); |
| 50 | |
| 51 | afterEach(() => { |
| 52 | fs.rmSync(tmpRoot, { recursive: true, force: true }); |
| 53 | delete process.env.DELEGATION_ENABLED; |
| 54 | }); |
| 55 | |
| 56 | it('mint parity across Hub-style and CLI-style handler calls', () => { |
| 57 | const consent = makeDelegationConsent(); |
| 58 | const hub = handleDelegationGrantMintRequest({ |
| 59 | dataDir, |
| 60 | vaultId, |
| 61 | consentId: consent.consent_id, |
| 62 | actorAgentId: consent.delegate_agent_id, |
| 63 | taskRef: 'task_hw_week3', |
| 64 | }); |
| 65 | const cli = handleDelegationGrantMintRequest({ |
| 66 | dataDir, |
| 67 | vaultId, |
| 68 | consentId: consent.consent_id, |
| 69 | actorAgentId: consent.delegate_agent_id, |
| 70 | taskRef: 'task_hw_week3', |
| 71 | }); |
| 72 | assert.equal(hub.ok, true); |
| 73 | assert.equal(cli.ok, true); |
| 74 | assert.deepEqual(stripVolatileMint(hub.payload), stripVolatileMint(cli.payload)); |
| 75 | assert.ok(hub.payload.bearer.startsWith('dgrnt_bearer_')); |
| 76 | }); |
| 77 | |
| 78 | it('gate off ⇒ identical DELEGATION_DISABLED', () => { |
| 79 | delete process.env.DELEGATION_ENABLED; |
| 80 | fs.rmSync(path.join(dataDir, DELEGATION_POLICY_FILE)); |
| 81 | const hub = handleDelegationGrantMintRequest({ |
| 82 | dataDir, |
| 83 | vaultId, |
| 84 | consentId: 'dcons_test', |
| 85 | actorAgentId: 'agent_tutor_test01', |
| 86 | }); |
| 87 | const cli = handleDelegationGrantListRequest({ dataDir, vaultId }); |
| 88 | assert.equal(hub.ok, false); |
| 89 | assert.equal(cli.ok, false); |
| 90 | assert.equal(hub.code, 'DELEGATION_DISABLED'); |
| 91 | assert.equal(cli.code, 'DELEGATION_DISABLED'); |
| 92 | }); |
| 93 | |
| 94 | it('list returns grants without bearer', () => { |
| 95 | const consent = makeDelegationConsent(); |
| 96 | handleDelegationGrantMintRequest({ |
| 97 | dataDir, |
| 98 | vaultId, |
| 99 | consentId: consent.consent_id, |
| 100 | actorAgentId: consent.delegate_agent_id, |
| 101 | }); |
| 102 | const list = handleDelegationGrantListRequest({ dataDir, vaultId }); |
| 103 | assert.equal(list.ok, true); |
| 104 | assert.equal(list.payload.grants.length, 1); |
| 105 | assert.equal(JSON.stringify(list.payload).includes('bearer'), false); |
| 106 | }); |
| 107 | }); |
File History
1 commit
sha256:baa800aedf841dbf32081aa7b2befa288ac33dfc7175ac55014c55c4d8c742f9
docs: move durable-auth freeze/evidence to local developmen…
Human
10 days ago