attest-icp-client.test.mjs
sha256:65ccb454656ea5acdea0a10e559b78bcde1eb6ff753ecc2911bc99d1c3d7cadd
feat(calendar): enforce agent context tiers in retrieval AP…
Human
minor
⚠ breaking
1 day ago
| 1 | import { test, beforeEach, afterEach, describe } from 'node:test'; |
| 2 | import assert from 'node:assert/strict'; |
| 3 | |
| 4 | let savedCanisterId; |
| 5 | let savedKey; |
| 6 | let savedHost; |
| 7 | |
| 8 | beforeEach(() => { |
| 9 | savedCanisterId = process.env.ICP_ATTESTATION_CANISTER_ID; |
| 10 | savedKey = process.env.ICP_ATTESTATION_KEY; |
| 11 | savedHost = process.env.ICP_ATTESTATION_HOST; |
| 12 | delete process.env.ICP_ATTESTATION_CANISTER_ID; |
| 13 | delete process.env.ICP_ATTESTATION_KEY; |
| 14 | delete process.env.ICP_ATTESTATION_HOST; |
| 15 | }); |
| 16 | |
| 17 | afterEach(() => { |
| 18 | if (savedCanisterId !== undefined) process.env.ICP_ATTESTATION_CANISTER_ID = savedCanisterId; |
| 19 | else delete process.env.ICP_ATTESTATION_CANISTER_ID; |
| 20 | if (savedKey !== undefined) process.env.ICP_ATTESTATION_KEY = savedKey; |
| 21 | else delete process.env.ICP_ATTESTATION_KEY; |
| 22 | if (savedHost !== undefined) process.env.ICP_ATTESTATION_HOST = savedHost; |
| 23 | else delete process.env.ICP_ATTESTATION_HOST; |
| 24 | }); |
| 25 | |
| 26 | describe('isIcpAttestationConfigured', () => { |
| 27 | test('returns false when neither env var is set', async () => { |
| 28 | const { isIcpAttestationConfigured, resetClient } = await import( |
| 29 | '../hub/gateway/icp-attestation-client.mjs' |
| 30 | ); |
| 31 | resetClient(); |
| 32 | assert.equal(isIcpAttestationConfigured(), false); |
| 33 | }); |
| 34 | |
| 35 | test('returns false when only canister ID is set', async () => { |
| 36 | process.env.ICP_ATTESTATION_CANISTER_ID = 'aaaaa-aa'; |
| 37 | const { isIcpAttestationConfigured, resetClient } = await import( |
| 38 | '../hub/gateway/icp-attestation-client.mjs' |
| 39 | ); |
| 40 | resetClient(); |
| 41 | assert.equal(isIcpAttestationConfigured(), false); |
| 42 | }); |
| 43 | |
| 44 | test('returns false when only key is set', async () => { |
| 45 | process.env.ICP_ATTESTATION_KEY = 'a'.repeat(64); |
| 46 | const { isIcpAttestationConfigured, resetClient } = await import( |
| 47 | '../hub/gateway/icp-attestation-client.mjs' |
| 48 | ); |
| 49 | resetClient(); |
| 50 | assert.equal(isIcpAttestationConfigured(), false); |
| 51 | }); |
| 52 | |
| 53 | test('returns true when both env vars are set', async () => { |
| 54 | process.env.ICP_ATTESTATION_CANISTER_ID = 'aaaaa-aa'; |
| 55 | process.env.ICP_ATTESTATION_KEY = 'a'.repeat(64); |
| 56 | const { isIcpAttestationConfigured, resetClient } = await import( |
| 57 | '../hub/gateway/icp-attestation-client.mjs' |
| 58 | ); |
| 59 | resetClient(); |
| 60 | assert.equal(isIcpAttestationConfigured(), true); |
| 61 | }); |
| 62 | |
| 63 | test('returns false when key is too short', async () => { |
| 64 | process.env.ICP_ATTESTATION_CANISTER_ID = 'aaaaa-aa'; |
| 65 | process.env.ICP_ATTESTATION_KEY = 'tooshort'; |
| 66 | const { isIcpAttestationConfigured, resetClient } = await import( |
| 67 | '../hub/gateway/icp-attestation-client.mjs' |
| 68 | ); |
| 69 | resetClient(); |
| 70 | assert.equal(isIcpAttestationConfigured(), false); |
| 71 | }); |
| 72 | }); |
| 73 | |
| 74 | describe('getAttestationCanisterId', () => { |
| 75 | test('returns null when not configured', async () => { |
| 76 | const { getAttestationCanisterId } = await import( |
| 77 | '../hub/gateway/icp-attestation-client.mjs' |
| 78 | ); |
| 79 | assert.equal(getAttestationCanisterId(), null); |
| 80 | }); |
| 81 | |
| 82 | test('returns the canister ID when set', async () => { |
| 83 | process.env.ICP_ATTESTATION_CANISTER_ID = 'rsovz-byaaa-aaaaa-qgira-cai'; |
| 84 | const { getAttestationCanisterId } = await import( |
| 85 | '../hub/gateway/icp-attestation-client.mjs' |
| 86 | ); |
| 87 | assert.equal(getAttestationCanisterId(), 'rsovz-byaaa-aaaaa-qgira-cai'); |
| 88 | }); |
| 89 | |
| 90 | test('trims whitespace from canister ID', async () => { |
| 91 | process.env.ICP_ATTESTATION_CANISTER_ID = ' rsovz-byaaa-aaaaa-qgira-cai '; |
| 92 | const { getAttestationCanisterId } = await import( |
| 93 | '../hub/gateway/icp-attestation-client.mjs' |
| 94 | ); |
| 95 | assert.equal(getAttestationCanisterId(), 'rsovz-byaaa-aaaaa-qgira-cai'); |
| 96 | }); |
| 97 | }); |
| 98 | |
| 99 | describe('anchorAttestation', () => { |
| 100 | test('returns null when not configured', async () => { |
| 101 | const { anchorAttestation, resetClient } = await import( |
| 102 | '../hub/gateway/icp-attestation-client.mjs' |
| 103 | ); |
| 104 | resetClient(); |
| 105 | const result = await anchorAttestation({ |
| 106 | id: 'air-test', |
| 107 | action: 'write', |
| 108 | path: 'notes/test.md', |
| 109 | timestamp: new Date().toISOString(), |
| 110 | content_hash: '', |
| 111 | sig: 'abc', |
| 112 | }); |
| 113 | assert.equal(result, null); |
| 114 | }); |
| 115 | }); |
| 116 | |
| 117 | describe('queryAttestation', () => { |
| 118 | test('returns null when canister ID not set', async () => { |
| 119 | const { queryAttestation, resetClient } = await import( |
| 120 | '../hub/gateway/icp-attestation-client.mjs' |
| 121 | ); |
| 122 | resetClient(); |
| 123 | const result = await queryAttestation('air-test'); |
| 124 | assert.equal(result, null); |
| 125 | }); |
| 126 | }); |
| 127 | |
| 128 | describe('getGatewayPrincipal', () => { |
| 129 | test('returns null when key not set', async () => { |
| 130 | const { getGatewayPrincipal, resetClient } = await import( |
| 131 | '../hub/gateway/icp-attestation-client.mjs' |
| 132 | ); |
| 133 | resetClient(); |
| 134 | const result = await getGatewayPrincipal(); |
| 135 | assert.equal(result, null); |
| 136 | }); |
| 137 | |
| 138 | test('returns a principal string when key is set', async () => { |
| 139 | process.env.ICP_ATTESTATION_KEY = |
| 140 | 'deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef'; |
| 141 | const { getGatewayPrincipal, resetClient } = await import( |
| 142 | '../hub/gateway/icp-attestation-client.mjs' |
| 143 | ); |
| 144 | resetClient(); |
| 145 | const principal = await getGatewayPrincipal(); |
| 146 | assert.ok(principal, 'should return a non-null principal'); |
| 147 | assert.ok(principal.includes('-'), 'principal should contain dashes'); |
| 148 | }); |
| 149 | |
| 150 | test('same key produces same principal deterministically', async () => { |
| 151 | process.env.ICP_ATTESTATION_KEY = |
| 152 | 'cafebabecafebabecafebabecafebabecafebabecafebabecafebabecafebabe'; |
| 153 | const { getGatewayPrincipal, resetClient } = await import( |
| 154 | '../hub/gateway/icp-attestation-client.mjs' |
| 155 | ); |
| 156 | resetClient(); |
| 157 | const p1 = await getGatewayPrincipal(); |
| 158 | resetClient(); |
| 159 | const p2 = await getGatewayPrincipal(); |
| 160 | assert.equal(p1, p2, 'same seed must produce same principal'); |
| 161 | }); |
| 162 | }); |
File History
2 commits
sha256:65ccb454656ea5acdea0a10e559b78bcde1eb6ff753ecc2911bc99d1c3d7cadd
feat(calendar): enforce agent context tiers in retrieval AP…
Human
minor
⚠
1 day ago
sha256:9103f98c89257ed2b01c237cea895dabb3e85ea337dccb1161c175e4422355b6
docs: accept Calendar Events v0 spec with Phase 0 security …
Human
2 days ago