@kya-os/mcp-i-core 1.3.12 → 1.3.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (254) hide show
  1. package/dist/config/remote-config.js +9 -12
  2. package/dist/runtime/base.js +11 -0
  3. package/dist/services/access-control.service.js +5 -0
  4. package/dist/services/tool-protection.service.js +17 -8
  5. package/package.json +2 -2
  6. package/.turbo/turbo-build.log +0 -4
  7. package/.turbo/turbo-test$colon$coverage.log +0 -4586
  8. package/.turbo/turbo-test.log +0 -3169
  9. package/COMPLIANCE_IMPROVEMENT_REPORT.md +0 -483
  10. package/Composer 3.md +0 -615
  11. package/GPT-5.md +0 -1169
  12. package/OPUS-plan.md +0 -352
  13. package/PHASE_3_AND_4.1_SUMMARY.md +0 -585
  14. package/PHASE_3_SUMMARY.md +0 -317
  15. package/PHASE_4.1.3_SUMMARY.md +0 -428
  16. package/PHASE_4.1_COMPLETE.md +0 -525
  17. package/PHASE_4_USER_DID_IDENTITY_LINKING_PLAN.md +0 -1240
  18. package/SCHEMA_COMPLIANCE_REPORT.md +0 -275
  19. package/TEST_PLAN.md +0 -571
  20. package/coverage/coverage-final.json +0 -60
  21. package/dist/cache/oauth-config-cache.d.ts.map +0 -1
  22. package/dist/cache/oauth-config-cache.js.map +0 -1
  23. package/dist/cache/tool-protection-cache.d.ts.map +0 -1
  24. package/dist/cache/tool-protection-cache.js.map +0 -1
  25. package/dist/compliance/index.d.ts.map +0 -1
  26. package/dist/compliance/index.js.map +0 -1
  27. package/dist/compliance/schema-registry.d.ts.map +0 -1
  28. package/dist/compliance/schema-registry.js.map +0 -1
  29. package/dist/compliance/schema-verifier.d.ts.map +0 -1
  30. package/dist/compliance/schema-verifier.js.map +0 -1
  31. package/dist/config/remote-config.d.ts.map +0 -1
  32. package/dist/config/remote-config.js.map +0 -1
  33. package/dist/config.d.ts.map +0 -1
  34. package/dist/config.js.map +0 -1
  35. package/dist/delegation/audience-validator.d.ts.map +0 -1
  36. package/dist/delegation/audience-validator.js.map +0 -1
  37. package/dist/delegation/bitstring.d.ts.map +0 -1
  38. package/dist/delegation/bitstring.js.map +0 -1
  39. package/dist/delegation/cascading-revocation.d.ts.map +0 -1
  40. package/dist/delegation/cascading-revocation.js.map +0 -1
  41. package/dist/delegation/delegation-graph.d.ts.map +0 -1
  42. package/dist/delegation/delegation-graph.js.map +0 -1
  43. package/dist/delegation/did-key-resolver.d.ts.map +0 -1
  44. package/dist/delegation/did-key-resolver.js.map +0 -1
  45. package/dist/delegation/index.d.ts.map +0 -1
  46. package/dist/delegation/index.js.map +0 -1
  47. package/dist/delegation/statuslist-manager.d.ts.map +0 -1
  48. package/dist/delegation/statuslist-manager.js.map +0 -1
  49. package/dist/delegation/storage/index.d.ts.map +0 -1
  50. package/dist/delegation/storage/index.js.map +0 -1
  51. package/dist/delegation/storage/memory-graph-storage.d.ts.map +0 -1
  52. package/dist/delegation/storage/memory-graph-storage.js.map +0 -1
  53. package/dist/delegation/storage/memory-statuslist-storage.d.ts.map +0 -1
  54. package/dist/delegation/storage/memory-statuslist-storage.js.map +0 -1
  55. package/dist/delegation/utils.d.ts.map +0 -1
  56. package/dist/delegation/utils.js.map +0 -1
  57. package/dist/delegation/vc-issuer.d.ts.map +0 -1
  58. package/dist/delegation/vc-issuer.js.map +0 -1
  59. package/dist/delegation/vc-verifier.d.ts.map +0 -1
  60. package/dist/delegation/vc-verifier.js.map +0 -1
  61. package/dist/identity/idp-token-resolver.d.ts.map +0 -1
  62. package/dist/identity/idp-token-resolver.js.map +0 -1
  63. package/dist/identity/idp-token-storage.interface.d.ts.map +0 -1
  64. package/dist/identity/idp-token-storage.interface.js.map +0 -1
  65. package/dist/identity/user-did-manager.d.ts.map +0 -1
  66. package/dist/identity/user-did-manager.js.map +0 -1
  67. package/dist/index.d.ts.map +0 -1
  68. package/dist/index.js.map +0 -1
  69. package/dist/providers/base.d.ts.map +0 -1
  70. package/dist/providers/base.js.map +0 -1
  71. package/dist/providers/memory.d.ts.map +0 -1
  72. package/dist/providers/memory.js.map +0 -1
  73. package/dist/runtime/audit-logger.d.ts.map +0 -1
  74. package/dist/runtime/audit-logger.js.map +0 -1
  75. package/dist/runtime/base.d.ts.map +0 -1
  76. package/dist/runtime/base.js.map +0 -1
  77. package/dist/services/access-control.service.d.ts.map +0 -1
  78. package/dist/services/access-control.service.js.map +0 -1
  79. package/dist/services/authorization/authorization-registry.d.ts.map +0 -1
  80. package/dist/services/authorization/authorization-registry.js.map +0 -1
  81. package/dist/services/authorization/types.d.ts.map +0 -1
  82. package/dist/services/authorization/types.js.map +0 -1
  83. package/dist/services/batch-delegation.service.d.ts.map +0 -1
  84. package/dist/services/batch-delegation.service.js.map +0 -1
  85. package/dist/services/crypto.service.d.ts.map +0 -1
  86. package/dist/services/crypto.service.js.map +0 -1
  87. package/dist/services/errors.d.ts.map +0 -1
  88. package/dist/services/errors.js.map +0 -1
  89. package/dist/services/index.d.ts.map +0 -1
  90. package/dist/services/index.js.map +0 -1
  91. package/dist/services/oauth-config.service.d.ts.map +0 -1
  92. package/dist/services/oauth-config.service.js.map +0 -1
  93. package/dist/services/oauth-provider-registry.d.ts.map +0 -1
  94. package/dist/services/oauth-provider-registry.js.map +0 -1
  95. package/dist/services/oauth-service.d.ts.map +0 -1
  96. package/dist/services/oauth-service.js.map +0 -1
  97. package/dist/services/oauth-token-retrieval.service.d.ts.map +0 -1
  98. package/dist/services/oauth-token-retrieval.service.js.map +0 -1
  99. package/dist/services/proof-verifier.d.ts.map +0 -1
  100. package/dist/services/proof-verifier.js.map +0 -1
  101. package/dist/services/provider-resolver.d.ts.map +0 -1
  102. package/dist/services/provider-resolver.js.map +0 -1
  103. package/dist/services/provider-validator.d.ts.map +0 -1
  104. package/dist/services/provider-validator.js.map +0 -1
  105. package/dist/services/session-registration.service.d.ts.map +0 -1
  106. package/dist/services/session-registration.service.js.map +0 -1
  107. package/dist/services/storage.service.d.ts.map +0 -1
  108. package/dist/services/storage.service.js.map +0 -1
  109. package/dist/services/tool-context-builder.d.ts.map +0 -1
  110. package/dist/services/tool-context-builder.js.map +0 -1
  111. package/dist/services/tool-protection.service.d.ts.map +0 -1
  112. package/dist/services/tool-protection.service.js.map +0 -1
  113. package/dist/types/oauth-required-error.d.ts.map +0 -1
  114. package/dist/types/oauth-required-error.js.map +0 -1
  115. package/dist/types/tool-protection.d.ts.map +0 -1
  116. package/dist/types/tool-protection.js.map +0 -1
  117. package/dist/utils/base58.d.ts.map +0 -1
  118. package/dist/utils/base58.js.map +0 -1
  119. package/dist/utils/base64.d.ts.map +0 -1
  120. package/dist/utils/base64.js.map +0 -1
  121. package/dist/utils/cors.d.ts.map +0 -1
  122. package/dist/utils/cors.js.map +0 -1
  123. package/dist/utils/did-helpers.d.ts.map +0 -1
  124. package/dist/utils/did-helpers.js.map +0 -1
  125. package/dist/utils/index.d.ts.map +0 -1
  126. package/dist/utils/index.js.map +0 -1
  127. package/dist/utils/storage-keys.d.ts.map +0 -1
  128. package/dist/utils/storage-keys.js.map +0 -1
  129. package/docs/API_REFERENCE.md +0 -1362
  130. package/docs/COMPLIANCE_MATRIX.md +0 -691
  131. package/docs/STATUSLIST2021_GUIDE.md +0 -696
  132. package/docs/W3C_VC_DELEGATION_GUIDE.md +0 -710
  133. package/src/__tests__/cache/tool-protection-cache.test.ts +0 -640
  134. package/src/__tests__/config/provider-runtime-config.test.ts +0 -309
  135. package/src/__tests__/delegation-e2e.test.ts +0 -690
  136. package/src/__tests__/identity/user-did-manager.test.ts +0 -232
  137. package/src/__tests__/index.test.ts +0 -56
  138. package/src/__tests__/integration/full-flow.test.ts +0 -789
  139. package/src/__tests__/integration.test.ts +0 -281
  140. package/src/__tests__/providers/base.test.ts +0 -173
  141. package/src/__tests__/providers/memory.test.ts +0 -319
  142. package/src/__tests__/regression/phase2-regression.test.ts +0 -429
  143. package/src/__tests__/runtime/audit-logger.test.ts +0 -154
  144. package/src/__tests__/runtime/base-extensions.test.ts +0 -595
  145. package/src/__tests__/runtime/base.test.ts +0 -869
  146. package/src/__tests__/runtime/delegation-flow.test.ts +0 -164
  147. package/src/__tests__/runtime/proof-client-did.test.ts +0 -376
  148. package/src/__tests__/runtime/route-interception.test.ts +0 -686
  149. package/src/__tests__/runtime/tool-protection-enforcement.test.ts +0 -908
  150. package/src/__tests__/services/agentshield-integration.test.ts +0 -791
  151. package/src/__tests__/services/cache-busting.test.ts +0 -125
  152. package/src/__tests__/services/oauth-service-pkce.test.ts +0 -556
  153. package/src/__tests__/services/provider-resolver-edge-cases.test.ts +0 -591
  154. package/src/__tests__/services/tool-protection-merged-config.test.ts +0 -485
  155. package/src/__tests__/services/tool-protection-oauth-provider.test.ts +0 -480
  156. package/src/__tests__/services/tool-protection.service.test.ts +0 -1373
  157. package/src/__tests__/utils/mock-providers.ts +0 -340
  158. package/src/cache/oauth-config-cache.d.ts +0 -69
  159. package/src/cache/oauth-config-cache.d.ts.map +0 -1
  160. package/src/cache/oauth-config-cache.js.map +0 -1
  161. package/src/cache/oauth-config-cache.ts +0 -123
  162. package/src/cache/tool-protection-cache.ts +0 -171
  163. package/src/compliance/EXAMPLE.md +0 -412
  164. package/src/compliance/__tests__/schema-verifier.test.ts +0 -797
  165. package/src/compliance/index.ts +0 -8
  166. package/src/compliance/schema-registry.ts +0 -460
  167. package/src/compliance/schema-verifier.ts +0 -708
  168. package/src/config/__tests__/merged-config.spec.ts +0 -445
  169. package/src/config/__tests__/remote-config.spec.ts +0 -268
  170. package/src/config/remote-config.ts +0 -264
  171. package/src/config.ts +0 -312
  172. package/src/delegation/__tests__/audience-validator.test.ts +0 -112
  173. package/src/delegation/__tests__/bitstring.test.ts +0 -346
  174. package/src/delegation/__tests__/cascading-revocation.test.ts +0 -628
  175. package/src/delegation/__tests__/delegation-graph.test.ts +0 -584
  176. package/src/delegation/__tests__/did-key-resolver.test.ts +0 -265
  177. package/src/delegation/__tests__/utils.test.ts +0 -152
  178. package/src/delegation/__tests__/vc-issuer.test.ts +0 -442
  179. package/src/delegation/__tests__/vc-verifier.test.ts +0 -922
  180. package/src/delegation/audience-validator.ts +0 -52
  181. package/src/delegation/bitstring.ts +0 -278
  182. package/src/delegation/cascading-revocation.ts +0 -370
  183. package/src/delegation/delegation-graph.ts +0 -299
  184. package/src/delegation/did-key-resolver.ts +0 -179
  185. package/src/delegation/index.ts +0 -14
  186. package/src/delegation/statuslist-manager.ts +0 -353
  187. package/src/delegation/storage/__tests__/memory-graph-storage.test.ts +0 -366
  188. package/src/delegation/storage/__tests__/memory-statuslist-storage.test.ts +0 -228
  189. package/src/delegation/storage/index.ts +0 -9
  190. package/src/delegation/storage/memory-graph-storage.ts +0 -178
  191. package/src/delegation/storage/memory-statuslist-storage.ts +0 -77
  192. package/src/delegation/utils.ts +0 -221
  193. package/src/delegation/vc-issuer.ts +0 -232
  194. package/src/delegation/vc-verifier.ts +0 -568
  195. package/src/identity/idp-token-resolver.ts +0 -181
  196. package/src/identity/idp-token-storage.interface.ts +0 -94
  197. package/src/identity/user-did-manager.ts +0 -526
  198. package/src/index.ts +0 -310
  199. package/src/providers/base.d.ts +0 -91
  200. package/src/providers/base.d.ts.map +0 -1
  201. package/src/providers/base.js.map +0 -1
  202. package/src/providers/base.ts +0 -96
  203. package/src/providers/memory.ts +0 -142
  204. package/src/runtime/audit-logger.ts +0 -39
  205. package/src/runtime/base.ts +0 -1392
  206. package/src/services/__tests__/access-control.integration.test.ts +0 -443
  207. package/src/services/__tests__/access-control.proof-response-validation.test.ts +0 -578
  208. package/src/services/__tests__/access-control.service.test.ts +0 -970
  209. package/src/services/__tests__/batch-delegation.service.test.ts +0 -351
  210. package/src/services/__tests__/crypto.service.test.ts +0 -531
  211. package/src/services/__tests__/oauth-provider-registry.test.ts +0 -142
  212. package/src/services/__tests__/proof-verifier.integration.test.ts +0 -485
  213. package/src/services/__tests__/proof-verifier.test.ts +0 -489
  214. package/src/services/__tests__/provider-resolution.integration.test.ts +0 -202
  215. package/src/services/__tests__/provider-resolver.test.ts +0 -213
  216. package/src/services/__tests__/storage.service.test.ts +0 -358
  217. package/src/services/access-control.service.ts +0 -990
  218. package/src/services/authorization/authorization-registry.ts +0 -66
  219. package/src/services/authorization/types.ts +0 -71
  220. package/src/services/batch-delegation.service.ts +0 -137
  221. package/src/services/crypto.service.ts +0 -302
  222. package/src/services/errors.ts +0 -76
  223. package/src/services/index.ts +0 -18
  224. package/src/services/oauth-config.service.d.ts +0 -53
  225. package/src/services/oauth-config.service.d.ts.map +0 -1
  226. package/src/services/oauth-config.service.js.map +0 -1
  227. package/src/services/oauth-config.service.ts +0 -192
  228. package/src/services/oauth-provider-registry.d.ts +0 -57
  229. package/src/services/oauth-provider-registry.d.ts.map +0 -1
  230. package/src/services/oauth-provider-registry.js.map +0 -1
  231. package/src/services/oauth-provider-registry.ts +0 -141
  232. package/src/services/oauth-service.ts +0 -544
  233. package/src/services/oauth-token-retrieval.service.ts +0 -245
  234. package/src/services/proof-verifier.ts +0 -478
  235. package/src/services/provider-resolver.d.ts +0 -48
  236. package/src/services/provider-resolver.d.ts.map +0 -1
  237. package/src/services/provider-resolver.js.map +0 -1
  238. package/src/services/provider-resolver.ts +0 -146
  239. package/src/services/provider-validator.ts +0 -170
  240. package/src/services/session-registration.service.ts +0 -251
  241. package/src/services/storage.service.ts +0 -566
  242. package/src/services/tool-context-builder.ts +0 -237
  243. package/src/services/tool-protection.service.ts +0 -1070
  244. package/src/types/oauth-required-error.ts +0 -63
  245. package/src/types/tool-protection.ts +0 -155
  246. package/src/utils/__tests__/did-helpers.test.ts +0 -156
  247. package/src/utils/base58.ts +0 -109
  248. package/src/utils/base64.ts +0 -148
  249. package/src/utils/cors.ts +0 -83
  250. package/src/utils/did-helpers.ts +0 -210
  251. package/src/utils/index.ts +0 -8
  252. package/src/utils/storage-keys.ts +0 -278
  253. package/tsconfig.json +0 -21
  254. package/vitest.config.ts +0 -56
@@ -1,791 +0,0 @@
1
- /**
2
- * AgentShield Integration Tests
3
- *
4
- * Tests for AgentShield API integration including:
5
- * - Tool protection config fetching
6
- * - API authentication
7
- * - Error handling
8
- * - Response format compatibility
9
- */
10
-
11
- import { describe, test, expect, beforeEach, vi, afterEach } from 'vitest';
12
- import { ToolProtectionService } from '../../services/tool-protection.service';
13
- import { InMemoryToolProtectionCache } from '../../cache/tool-protection-cache';
14
- import type { ToolProtectionServiceConfig } from '../../types/tool-protection';
15
-
16
- // Mock global fetch
17
- global.fetch = vi.fn();
18
-
19
- describe('AgentShield Integration', () => {
20
- let service: ToolProtectionService;
21
- let cache: InMemoryToolProtectionCache;
22
- let config: ToolProtectionServiceConfig;
23
- const mockAgentDid = 'did:key:z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK';
24
- const baseUrl = 'https://kya.vouched.id';
25
-
26
- beforeEach(() => {
27
- vi.clearAllMocks();
28
- cache = new InMemoryToolProtectionCache();
29
- config = {
30
- apiUrl: baseUrl,
31
- apiKey: 'test-api-key-12345',
32
- projectId: 'test-project-123',
33
- cacheTtl: 300000,
34
- debug: false,
35
- };
36
- service = new ToolProtectionService(config, cache);
37
- });
38
-
39
- afterEach(() => {
40
- vi.restoreAllMocks();
41
- });
42
-
43
- describe('API Authentication', () => {
44
- test('should use X-API-Key header for new endpoint', async () => {
45
- const apiResponse = {
46
- success: true,
47
- data: {
48
- toolProtections: {},
49
- },
50
- metadata: {},
51
- };
52
-
53
- (global.fetch as any).mockResolvedValueOnce({
54
- ok: true,
55
- json: async () => apiResponse,
56
- });
57
-
58
- await service.getToolProtectionConfig(mockAgentDid);
59
-
60
- expect(global.fetch).toHaveBeenCalledWith(
61
- expect.any(String),
62
- expect.objectContaining({
63
- headers: expect.objectContaining({
64
- 'X-API-Key': 'test-api-key-12345',
65
- 'X-Project-Id': 'test-project-123',
66
- }),
67
- })
68
- );
69
- });
70
-
71
- test('should use Authorization Bearer header for old endpoint', async () => {
72
- delete config.projectId;
73
- service = new ToolProtectionService(config, cache);
74
-
75
- const apiResponse = {
76
- success: true,
77
- data: {
78
- tools: [],
79
- },
80
- metadata: {},
81
- };
82
-
83
- (global.fetch as any).mockResolvedValueOnce({
84
- ok: true,
85
- json: async () => apiResponse,
86
- });
87
-
88
- await service.getToolProtectionConfig(mockAgentDid);
89
-
90
- expect(global.fetch).toHaveBeenCalledWith(
91
- expect.any(String),
92
- expect.objectContaining({
93
- headers: expect.objectContaining({
94
- Authorization: 'Bearer test-api-key-12345',
95
- }),
96
- })
97
- );
98
- });
99
-
100
- test('should reject requests without API key', async () => {
101
- config.apiKey = '';
102
- service = new ToolProtectionService(config, cache);
103
-
104
- await expect(service.getToolProtectionConfig(mockAgentDid)).rejects.toThrow(
105
- 'API key is missing or empty'
106
- );
107
- });
108
-
109
- test('should mask API key in debug logs', async () => {
110
- config.debug = true;
111
- config.apiKey = 'sk_live_1234567890abcdef';
112
- service = new ToolProtectionService(config, cache);
113
-
114
- const apiResponse = {
115
- success: true,
116
- data: {
117
- toolProtections: {},
118
- },
119
- metadata: {},
120
- };
121
-
122
- (global.fetch as any).mockResolvedValueOnce({
123
- ok: true,
124
- json: async () => apiResponse,
125
- });
126
-
127
- const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
128
-
129
- await service.getToolProtectionConfig(mockAgentDid);
130
-
131
- const logCalls = consoleSpy.mock.calls;
132
- const debugLog = logCalls.find((call) =>
133
- call[0].includes('Fetching from API')
134
- );
135
-
136
- if (debugLog && debugLog[2]) {
137
- const logData = debugLog[2] as any;
138
- expect(logData.apiKeyMasked).toMatch(/^sk_live\.\.\./);
139
- expect(logData.apiKeyMasked).not.toContain('1234567890abcdef');
140
- }
141
-
142
- consoleSpy.mockRestore();
143
- });
144
- });
145
-
146
- describe('Endpoint Selection', () => {
147
- test('should use project-scoped /config endpoint when projectId is available', async () => {
148
- // Merged config format - tools embedded at config.toolProtection.tools
149
- const apiResponse = {
150
- success: true,
151
- data: {
152
- config: {
153
- toolProtection: {
154
- source: 'agentshield',
155
- tools: {},
156
- },
157
- },
158
- },
159
- metadata: {},
160
- };
161
-
162
- (global.fetch as any).mockResolvedValueOnce({
163
- ok: true,
164
- json: async () => apiResponse,
165
- });
166
-
167
- await service.getToolProtectionConfig(mockAgentDid);
168
-
169
- // Now calls /config endpoint (not /tool-protections)
170
- expect(global.fetch).toHaveBeenCalledWith(
171
- `${baseUrl}/api/v1/bouncer/projects/test-project-123/config`,
172
- expect.any(Object)
173
- );
174
- });
175
-
176
- test('should use agent-scoped endpoint when projectId is not available', async () => {
177
- delete config.projectId;
178
- service = new ToolProtectionService(config, cache);
179
-
180
- const apiResponse = {
181
- success: true,
182
- data: {
183
- tools: [],
184
- },
185
- metadata: {},
186
- };
187
-
188
- (global.fetch as any).mockResolvedValueOnce({
189
- ok: true,
190
- json: async () => apiResponse,
191
- });
192
-
193
- await service.getToolProtectionConfig(mockAgentDid);
194
-
195
- expect(global.fetch).toHaveBeenCalledWith(
196
- `${baseUrl}/api/v1/bouncer/config?agent_did=${encodeURIComponent(mockAgentDid)}`,
197
- expect.any(Object)
198
- );
199
- });
200
-
201
- test('should encode projectId in URL', async () => {
202
- config.projectId = 'project/with/special-chars';
203
- service = new ToolProtectionService(config, cache);
204
-
205
- const apiResponse = {
206
- success: true,
207
- data: {
208
- toolProtections: {},
209
- },
210
- metadata: {},
211
- };
212
-
213
- (global.fetch as any).mockResolvedValueOnce({
214
- ok: true,
215
- json: async () => apiResponse,
216
- });
217
-
218
- await service.getToolProtectionConfig(mockAgentDid);
219
-
220
- expect(global.fetch).toHaveBeenCalledWith(
221
- expect.stringContaining(encodeURIComponent('project/with/special-chars')),
222
- expect.any(Object)
223
- );
224
- });
225
-
226
- test('should encode agent DID in URL', async () => {
227
- delete config.projectId;
228
- service = new ToolProtectionService(config, cache);
229
-
230
- const apiResponse = {
231
- success: true,
232
- data: {
233
- tools: [],
234
- },
235
- metadata: {},
236
- };
237
-
238
- (global.fetch as any).mockResolvedValueOnce({
239
- ok: true,
240
- json: async () => apiResponse,
241
- });
242
-
243
- await service.getToolProtectionConfig(mockAgentDid);
244
-
245
- expect(global.fetch).toHaveBeenCalledWith(
246
- expect.stringContaining(encodeURIComponent(mockAgentDid)),
247
- expect.any(Object)
248
- );
249
- });
250
- });
251
-
252
- describe('Response Format Compatibility', () => {
253
- test('should handle new endpoint format (toolProtections object)', async () => {
254
- const apiResponse = {
255
- success: true,
256
- data: {
257
- toolProtections: {
258
- checkout: {
259
- requiresDelegation: true,
260
- requiredScopes: ['cart:write'],
261
- },
262
- search: {
263
- requiresDelegation: false,
264
- requiredScopes: [],
265
- },
266
- },
267
- },
268
- metadata: {
269
- timestamp: new Date().toISOString(),
270
- cachedUntil: new Date(Date.now() + 300000).toISOString(),
271
- },
272
- };
273
-
274
- (global.fetch as any).mockResolvedValueOnce({
275
- ok: true,
276
- json: async () => apiResponse,
277
- });
278
-
279
- const result = await service.getToolProtectionConfig(mockAgentDid);
280
-
281
- expect(result.toolProtections.checkout).toBeDefined();
282
- expect(result.toolProtections.checkout.requiresDelegation).toBe(true);
283
- expect(result.toolProtections.search.requiresDelegation).toBe(false);
284
- });
285
-
286
- test('should handle old endpoint format (tools array)', async () => {
287
- delete config.projectId;
288
- service = new ToolProtectionService(config, cache);
289
-
290
- const apiResponse = {
291
- success: true,
292
- data: {
293
- tools: [
294
- {
295
- name: 'checkout',
296
- requiresDelegation: true,
297
- scopes: ['cart:write'],
298
- },
299
- {
300
- name: 'search',
301
- requiresDelegation: false,
302
- scopes: [],
303
- },
304
- ],
305
- },
306
- metadata: {},
307
- };
308
-
309
- (global.fetch as any).mockResolvedValueOnce({
310
- ok: true,
311
- json: async () => apiResponse,
312
- });
313
-
314
- const result = await service.getToolProtectionConfig(mockAgentDid);
315
-
316
- expect(result.toolProtections.checkout).toBeDefined();
317
- expect(result.toolProtections.checkout.requiresDelegation).toBe(true);
318
- expect(result.toolProtections.search.requiresDelegation).toBe(false);
319
- });
320
-
321
- test('should handle old endpoint format (tools object)', async () => {
322
- delete config.projectId;
323
- service = new ToolProtectionService(config, cache);
324
-
325
- const apiResponse = {
326
- success: true,
327
- data: {
328
- tools: {
329
- checkout: {
330
- requiresDelegation: true,
331
- scopes: ['cart:write'],
332
- },
333
- search: {
334
- requiresDelegation: false,
335
- scopes: [],
336
- },
337
- },
338
- },
339
- metadata: {},
340
- };
341
-
342
- (global.fetch as any).mockResolvedValueOnce({
343
- ok: true,
344
- json: async () => apiResponse,
345
- });
346
-
347
- const result = await service.getToolProtectionConfig(mockAgentDid);
348
-
349
- expect(result.toolProtections.checkout).toBeDefined();
350
- expect(result.toolProtections.checkout.requiresDelegation).toBe(true);
351
- expect(result.toolProtections.search.requiresDelegation).toBe(false);
352
- });
353
-
354
- test('should handle snake_case field names', async () => {
355
- const apiResponse = {
356
- success: true,
357
- data: {
358
- toolProtections: {
359
- tool1: {
360
- requires_delegation: true,
361
- required_scopes: ['scope1', 'scope2'],
362
- },
363
- },
364
- },
365
- metadata: {},
366
- };
367
-
368
- (global.fetch as any).mockResolvedValueOnce({
369
- ok: true,
370
- json: async () => apiResponse,
371
- });
372
-
373
- const result = await service.getToolProtectionConfig(mockAgentDid);
374
-
375
- expect(result.toolProtections.tool1.requiresDelegation).toBe(true);
376
- expect(result.toolProtections.tool1.requiredScopes).toEqual(['scope1', 'scope2']);
377
- });
378
-
379
- test('should handle camelCase field names', async () => {
380
- const apiResponse = {
381
- success: true,
382
- data: {
383
- toolProtections: {
384
- tool1: {
385
- requiresDelegation: true,
386
- requiredScopes: ['scope1', 'scope2'],
387
- },
388
- },
389
- },
390
- metadata: {},
391
- };
392
-
393
- (global.fetch as any).mockResolvedValueOnce({
394
- ok: true,
395
- json: async () => apiResponse,
396
- });
397
-
398
- const result = await service.getToolProtectionConfig(mockAgentDid);
399
-
400
- expect(result.toolProtections.tool1.requiresDelegation).toBe(true);
401
- expect(result.toolProtections.tool1.requiredScopes).toEqual(['scope1', 'scope2']);
402
- });
403
-
404
- test('should prefer camelCase over snake_case when both present', async () => {
405
- const apiResponse = {
406
- success: true,
407
- data: {
408
- toolProtections: {
409
- tool1: {
410
- requires_delegation: false,
411
- requiresDelegation: true,
412
- required_scopes: ['old'],
413
- requiredScopes: ['new'],
414
- },
415
- },
416
- },
417
- metadata: {},
418
- };
419
-
420
- (global.fetch as any).mockResolvedValueOnce({
421
- ok: true,
422
- json: async () => apiResponse,
423
- });
424
-
425
- const result = await service.getToolProtectionConfig(mockAgentDid);
426
-
427
- expect(result.toolProtections.tool1.requiresDelegation).toBe(true);
428
- expect(result.toolProtections.tool1.requiredScopes).toEqual(['new']);
429
- });
430
- });
431
-
432
- describe('Error Handling', () => {
433
- test('should handle 401 Unauthorized', async () => {
434
- (global.fetch as any).mockResolvedValueOnce({
435
- ok: false,
436
- status: 401,
437
- statusText: 'Unauthorized',
438
- text: async () => 'Invalid API key',
439
- });
440
-
441
- await expect(service.getToolProtectionConfig(mockAgentDid)).rejects.toThrow(
442
- 'Failed to fetch bouncer config: 401 Unauthorized'
443
- );
444
- });
445
-
446
- test('should handle 403 Forbidden', async () => {
447
- (global.fetch as any).mockResolvedValueOnce({
448
- ok: false,
449
- status: 403,
450
- statusText: 'Forbidden',
451
- text: async () => 'Access denied',
452
- });
453
-
454
- await expect(service.getToolProtectionConfig(mockAgentDid)).rejects.toThrow(
455
- 'Failed to fetch bouncer config: 403 Forbidden'
456
- );
457
- });
458
-
459
- test('should handle 404 Not Found', async () => {
460
- (global.fetch as any).mockResolvedValueOnce({
461
- ok: false,
462
- status: 404,
463
- statusText: 'Not Found',
464
- text: async () => 'Project not found',
465
- });
466
-
467
- await expect(service.getToolProtectionConfig(mockAgentDid)).rejects.toThrow(
468
- 'Failed to fetch bouncer config: 404 Not Found'
469
- );
470
- });
471
-
472
- test('should handle 500 Internal Server Error', async () => {
473
- (global.fetch as any).mockResolvedValueOnce({
474
- ok: false,
475
- status: 500,
476
- statusText: 'Internal Server Error',
477
- text: async () => 'Server error',
478
- });
479
-
480
- await expect(service.getToolProtectionConfig(mockAgentDid)).rejects.toThrow(
481
- 'Failed to fetch bouncer config: 500 Internal Server Error'
482
- );
483
- });
484
-
485
- test('should handle network timeout', async () => {
486
- (global.fetch as any).mockRejectedValueOnce(new Error('Network timeout'));
487
-
488
- const fallbackConfig = {
489
- toolProtections: {
490
- fallback: {
491
- requiresDelegation: true,
492
- requiredScopes: ['fallback'],
493
- },
494
- },
495
- };
496
-
497
- config.fallbackConfig = fallbackConfig;
498
- service = new ToolProtectionService(config, cache);
499
-
500
- const result = await service.getToolProtectionConfig(mockAgentDid);
501
-
502
- expect(result).toEqual(fallbackConfig);
503
- });
504
-
505
- test('should handle malformed JSON response', async () => {
506
- (global.fetch as any).mockResolvedValueOnce({
507
- ok: true,
508
- json: async () => {
509
- throw new Error('Unexpected token');
510
- },
511
- });
512
-
513
- await expect(service.getToolProtectionConfig(mockAgentDid)).rejects.toThrow();
514
- });
515
-
516
- test('should handle response with success: false', async () => {
517
- (global.fetch as any).mockResolvedValueOnce({
518
- ok: true,
519
- json: async () => ({
520
- success: false,
521
- error: {
522
- code: 'INVALID_REQUEST',
523
- message: 'Invalid project ID',
524
- },
525
- }),
526
- });
527
-
528
- await expect(service.getToolProtectionConfig(mockAgentDid)).rejects.toThrow(
529
- 'API returned success: false'
530
- );
531
- });
532
-
533
- test('should handle empty response body', async () => {
534
- (global.fetch as any).mockResolvedValueOnce({
535
- ok: false,
536
- status: 500,
537
- statusText: 'Internal Server Error',
538
- text: async () => '',
539
- });
540
-
541
- await expect(service.getToolProtectionConfig(mockAgentDid)).rejects.toThrow(
542
- 'Failed to fetch bouncer config: 500 Internal Server Error - '
543
- );
544
- });
545
- });
546
-
547
- describe('Fallback Behavior', () => {
548
- test('should use fallback config when API is unavailable', async () => {
549
- const fallbackConfig = {
550
- toolProtections: {
551
- fallback_tool: {
552
- requiresDelegation: true,
553
- requiredScopes: ['fallback'],
554
- },
555
- },
556
- };
557
-
558
- config.fallbackConfig = fallbackConfig;
559
- service = new ToolProtectionService(config, cache);
560
-
561
- (global.fetch as any).mockRejectedValueOnce(new Error('Network error'));
562
-
563
- const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
564
-
565
- const result = await service.getToolProtectionConfig(mockAgentDid);
566
-
567
- expect(result).toEqual(fallbackConfig);
568
- expect(consoleSpy).toHaveBeenCalledWith(
569
- expect.stringContaining('using fallback config'),
570
- expect.any(Object)
571
- );
572
-
573
- consoleSpy.mockRestore();
574
- });
575
-
576
- test('should return deny-all config when no fallback is available (fail-safe)', async () => {
577
- (global.fetch as any).mockRejectedValueOnce(new Error('Network error'));
578
-
579
- const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
580
-
581
- const result = await service.getToolProtectionConfig(mockAgentDid);
582
-
583
- expect(result.toolProtections['*']).toBeDefined();
584
- expect(result.toolProtections['*']?.requiresDelegation).toBe(true);
585
- expect(consoleSpy).toHaveBeenCalledWith(
586
- expect.stringContaining('API fetch failed, no fallback, failing closed (deny-all)'),
587
- expect.any(Object)
588
- );
589
-
590
- consoleSpy.mockRestore();
591
- });
592
-
593
- test('should cache fallback config', async () => {
594
- const fallbackConfig = {
595
- toolProtections: {
596
- fallback_tool: {
597
- requiresDelegation: true,
598
- requiredScopes: ['fallback'],
599
- },
600
- },
601
- };
602
-
603
- config.fallbackConfig = fallbackConfig;
604
- service = new ToolProtectionService(config, cache);
605
-
606
- (global.fetch as any).mockRejectedValue(new Error('Network error'));
607
-
608
- // First call - should use fallback
609
- const result1 = await service.getToolProtectionConfig(mockAgentDid);
610
-
611
- // Second call - should use cached fallback (no API call)
612
- const result2 = await service.getToolProtectionConfig(mockAgentDid);
613
-
614
- expect(result1).toEqual(result2);
615
- expect(result1).toEqual(fallbackConfig);
616
- // Should only attempt API call once (both calls use cached fallback)
617
- expect(global.fetch).toHaveBeenCalledTimes(1);
618
- });
619
- });
620
-
621
- describe('Caching Integration', () => {
622
- test('should cache successful API responses', async () => {
623
- const apiResponse = {
624
- success: true,
625
- data: {
626
- toolProtections: {
627
- tool1: {
628
- requiresDelegation: true,
629
- requiredScopes: ['scope1'],
630
- },
631
- },
632
- },
633
- metadata: {},
634
- };
635
-
636
- (global.fetch as any).mockResolvedValueOnce({
637
- ok: true,
638
- json: async () => apiResponse,
639
- });
640
-
641
- // First call - should fetch from API
642
- await service.getToolProtectionConfig(mockAgentDid);
643
-
644
- // Second call - should use cache
645
- const result = await service.getToolProtectionConfig(mockAgentDid);
646
-
647
- expect(global.fetch).toHaveBeenCalledTimes(1);
648
- expect(result.toolProtections.tool1).toBeDefined();
649
- });
650
-
651
- test('should respect cache TTL', async () => {
652
- config.cacheTtl = 1000; // 1 second
653
- service = new ToolProtectionService(config, cache);
654
-
655
- const apiResponse = {
656
- success: true,
657
- data: {
658
- toolProtections: {},
659
- },
660
- metadata: {},
661
- };
662
-
663
- (global.fetch as any).mockResolvedValueOnce({
664
- ok: true,
665
- json: async () => apiResponse,
666
- });
667
-
668
- await service.getToolProtectionConfig(mockAgentDid);
669
-
670
- // Wait for cache to expire
671
- await new Promise((resolve) => setTimeout(resolve, 1100));
672
-
673
- (global.fetch as any).mockResolvedValueOnce({
674
- ok: true,
675
- json: async () => apiResponse,
676
- });
677
-
678
- await service.getToolProtectionConfig(mockAgentDid);
679
-
680
- expect(global.fetch).toHaveBeenCalledTimes(2);
681
- });
682
- });
683
-
684
- describe('Real-world Scenarios', () => {
685
- test('should handle typical e-commerce tool protection config', async () => {
686
- const apiResponse = {
687
- success: true,
688
- data: {
689
- toolProtections: {
690
- search_products: {
691
- requiresDelegation: false,
692
- requiredScopes: [],
693
- },
694
- add_to_cart: {
695
- requiresDelegation: true,
696
- requiredScopes: ['cart:write'],
697
- },
698
- checkout: {
699
- requiresDelegation: true,
700
- requiredScopes: ['cart:write', 'payment:process'],
701
- },
702
- view_order: {
703
- requiresDelegation: false,
704
- requiredScopes: [],
705
- },
706
- },
707
- },
708
- metadata: {
709
- timestamp: new Date().toISOString(),
710
- cachedUntil: new Date(Date.now() + 300000).toISOString(),
711
- },
712
- };
713
-
714
- (global.fetch as any).mockResolvedValueOnce({
715
- ok: true,
716
- json: async () => apiResponse,
717
- });
718
-
719
- const result = await service.getToolProtectionConfig(mockAgentDid);
720
-
721
- expect(result.toolProtections.search_products.requiresDelegation).toBe(false);
722
- expect(result.toolProtections.add_to_cart.requiresDelegation).toBe(true);
723
- expect(result.toolProtections.add_to_cart.requiredScopes).toContain('cart:write');
724
- expect(result.toolProtections.checkout.requiredScopes).toContain('payment:process');
725
- });
726
-
727
- test('should handle API rate limiting gracefully', async () => {
728
- (global.fetch as any).mockResolvedValueOnce({
729
- ok: false,
730
- status: 429,
731
- statusText: 'Too Many Requests',
732
- text: async () => 'Rate limit exceeded',
733
- });
734
-
735
- const fallbackConfig = {
736
- toolProtections: {
737
- tool1: {
738
- requiresDelegation: true,
739
- requiredScopes: ['scope1'],
740
- },
741
- },
742
- };
743
-
744
- config.fallbackConfig = fallbackConfig;
745
- service = new ToolProtectionService(config, cache);
746
-
747
- const result = await service.getToolProtectionConfig(mockAgentDid);
748
-
749
- expect(result).toEqual(fallbackConfig);
750
- });
751
-
752
- test('should handle concurrent requests', async () => {
753
- const apiResponse = {
754
- success: true,
755
- data: {
756
- toolProtections: {
757
- tool1: {
758
- requiresDelegation: true,
759
- requiredScopes: ['scope1'],
760
- },
761
- },
762
- },
763
- metadata: {},
764
- };
765
-
766
- (global.fetch as any).mockResolvedValue({
767
- ok: true,
768
- json: async () => apiResponse,
769
- });
770
-
771
- // Make concurrent requests
772
- const promises = [
773
- service.getToolProtectionConfig(mockAgentDid),
774
- service.getToolProtectionConfig(mockAgentDid),
775
- service.getToolProtectionConfig(mockAgentDid),
776
- ];
777
-
778
- const results = await Promise.all(promises);
779
-
780
- // All should return same config
781
- results.forEach((result) => {
782
- expect(result.toolProtections.tool1).toBeDefined();
783
- });
784
-
785
- // Should only fetch once (cache prevents duplicate fetches)
786
- // Note: Due to race conditions, multiple fetches might occur
787
- expect(global.fetch).toHaveBeenCalled();
788
- });
789
- });
790
- });
791
-