@kya-os/mcp-i-core 1.2.3-canary.7 → 1.3.0
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.
- package/.claude/settings.local.json +9 -0
- package/.turbo/turbo-build.log +4 -0
- package/.turbo/turbo-test$colon$coverage.log +4514 -0
- package/.turbo/turbo-test.log +2973 -0
- package/COMPLIANCE_IMPROVEMENT_REPORT.md +483 -0
- package/Composer 3.md +615 -0
- package/GPT-5.md +1169 -0
- package/OPUS-plan.md +352 -0
- package/PHASE_3_AND_4.1_SUMMARY.md +585 -0
- package/PHASE_3_SUMMARY.md +317 -0
- package/PHASE_4.1.3_SUMMARY.md +428 -0
- package/PHASE_4.1_COMPLETE.md +525 -0
- package/PHASE_4_USER_DID_IDENTITY_LINKING_PLAN.md +1240 -0
- package/SCHEMA_COMPLIANCE_REPORT.md +275 -0
- package/TEST_PLAN.md +571 -0
- package/coverage/coverage-final.json +57 -0
- package/dist/__tests__/utils/mock-providers.d.ts +1 -2
- package/dist/__tests__/utils/mock-providers.d.ts.map +1 -1
- package/dist/__tests__/utils/mock-providers.js.map +1 -1
- package/dist/cache/oauth-config-cache.d.ts +69 -0
- package/dist/cache/oauth-config-cache.d.ts.map +1 -0
- package/dist/cache/oauth-config-cache.js +76 -0
- package/dist/cache/oauth-config-cache.js.map +1 -0
- package/dist/identity/idp-token-resolver.d.ts +53 -0
- package/dist/identity/idp-token-resolver.d.ts.map +1 -0
- package/dist/identity/idp-token-resolver.js +108 -0
- package/dist/identity/idp-token-resolver.js.map +1 -0
- package/dist/identity/idp-token-storage.interface.d.ts +42 -0
- package/dist/identity/idp-token-storage.interface.d.ts.map +1 -0
- package/dist/identity/idp-token-storage.interface.js +12 -0
- package/dist/identity/idp-token-storage.interface.js.map +1 -0
- package/dist/identity/user-did-manager.d.ts +39 -1
- package/dist/identity/user-did-manager.d.ts.map +1 -1
- package/dist/identity/user-did-manager.js +69 -3
- package/dist/identity/user-did-manager.js.map +1 -1
- package/dist/index.d.ts +22 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +39 -1
- package/dist/index.js.map +1 -1
- package/dist/runtime/audit-logger.d.ts +37 -0
- package/dist/runtime/audit-logger.d.ts.map +1 -0
- package/dist/runtime/audit-logger.js +9 -0
- package/dist/runtime/audit-logger.js.map +1 -0
- package/dist/runtime/base.d.ts +58 -2
- package/dist/runtime/base.d.ts.map +1 -1
- package/dist/runtime/base.js +266 -11
- package/dist/runtime/base.js.map +1 -1
- package/dist/services/access-control.service.d.ts.map +1 -1
- package/dist/services/access-control.service.js +200 -35
- package/dist/services/access-control.service.js.map +1 -1
- package/dist/services/authorization/authorization-registry.d.ts +29 -0
- package/dist/services/authorization/authorization-registry.d.ts.map +1 -0
- package/dist/services/authorization/authorization-registry.js +57 -0
- package/dist/services/authorization/authorization-registry.js.map +1 -0
- package/dist/services/authorization/types.d.ts +53 -0
- package/dist/services/authorization/types.d.ts.map +1 -0
- package/dist/services/authorization/types.js +10 -0
- package/dist/services/authorization/types.js.map +1 -0
- package/dist/services/batch-delegation.service.d.ts +53 -0
- package/dist/services/batch-delegation.service.d.ts.map +1 -0
- package/dist/services/batch-delegation.service.js +95 -0
- package/dist/services/batch-delegation.service.js.map +1 -0
- package/dist/services/oauth-config.service.d.ts +53 -0
- package/dist/services/oauth-config.service.d.ts.map +1 -0
- package/dist/services/oauth-config.service.js +117 -0
- package/dist/services/oauth-config.service.js.map +1 -0
- package/dist/services/oauth-provider-registry.d.ts +77 -0
- package/dist/services/oauth-provider-registry.d.ts.map +1 -0
- package/dist/services/oauth-provider-registry.js +112 -0
- package/dist/services/oauth-provider-registry.js.map +1 -0
- package/dist/services/oauth-service.d.ts +77 -0
- package/dist/services/oauth-service.d.ts.map +1 -0
- package/dist/services/oauth-service.js +348 -0
- package/dist/services/oauth-service.js.map +1 -0
- package/dist/services/oauth-token-retrieval.service.d.ts +49 -0
- package/dist/services/oauth-token-retrieval.service.d.ts.map +1 -0
- package/dist/services/oauth-token-retrieval.service.js +150 -0
- package/dist/services/oauth-token-retrieval.service.js.map +1 -0
- package/dist/services/provider-resolver.d.ts +48 -0
- package/dist/services/provider-resolver.d.ts.map +1 -0
- package/dist/services/provider-resolver.js +120 -0
- package/dist/services/provider-resolver.js.map +1 -0
- package/dist/services/provider-validator.d.ts +55 -0
- package/dist/services/provider-validator.d.ts.map +1 -0
- package/dist/services/provider-validator.js +135 -0
- package/dist/services/provider-validator.js.map +1 -0
- package/dist/services/tool-context-builder.d.ts +57 -0
- package/dist/services/tool-context-builder.d.ts.map +1 -0
- package/dist/services/tool-context-builder.js +125 -0
- package/dist/services/tool-context-builder.js.map +1 -0
- package/dist/services/tool-protection.service.d.ts +87 -10
- package/dist/services/tool-protection.service.d.ts.map +1 -1
- package/dist/services/tool-protection.service.js +282 -112
- package/dist/services/tool-protection.service.js.map +1 -1
- package/dist/types/oauth-required-error.d.ts +40 -0
- package/dist/types/oauth-required-error.d.ts.map +1 -0
- package/dist/types/oauth-required-error.js +40 -0
- package/dist/types/oauth-required-error.js.map +1 -0
- package/dist/utils/did-helpers.d.ts +33 -0
- package/dist/utils/did-helpers.d.ts.map +1 -1
- package/dist/utils/did-helpers.js +40 -0
- package/dist/utils/did-helpers.js.map +1 -1
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/index.js +1 -0
- package/dist/utils/index.js.map +1 -1
- package/docs/API_REFERENCE.md +1362 -0
- package/docs/COMPLIANCE_MATRIX.md +691 -0
- package/docs/STATUSLIST2021_GUIDE.md +696 -0
- package/docs/W3C_VC_DELEGATION_GUIDE.md +710 -0
- package/package.json +24 -50
- package/scripts/audit-compliance.ts +724 -0
- package/src/__tests__/cache/tool-protection-cache.test.ts +640 -0
- package/src/__tests__/config/provider-runtime-config.test.ts +309 -0
- package/src/__tests__/delegation-e2e.test.ts +690 -0
- package/src/__tests__/identity/user-did-manager.test.ts +213 -0
- package/src/__tests__/index.test.ts +56 -0
- package/src/__tests__/integration/full-flow.test.ts +776 -0
- package/src/__tests__/integration.test.ts +281 -0
- package/src/__tests__/providers/base.test.ts +173 -0
- package/src/__tests__/providers/memory.test.ts +319 -0
- package/src/__tests__/regression/phase2-regression.test.ts +427 -0
- package/src/__tests__/runtime/audit-logger.test.ts +154 -0
- package/src/__tests__/runtime/base-extensions.test.ts +593 -0
- package/src/__tests__/runtime/base.test.ts +869 -0
- package/src/__tests__/runtime/delegation-flow.test.ts +164 -0
- package/src/__tests__/runtime/proof-client-did.test.ts +375 -0
- package/src/__tests__/runtime/route-interception.test.ts +686 -0
- package/src/__tests__/runtime/tool-protection-enforcement.test.ts +908 -0
- package/src/__tests__/services/agentshield-integration.test.ts +784 -0
- package/src/__tests__/services/provider-resolver-edge-cases.test.ts +487 -0
- package/src/__tests__/services/tool-protection-oauth-provider.test.ts +480 -0
- package/src/__tests__/services/tool-protection.service.test.ts +1366 -0
- package/src/__tests__/utils/mock-providers.ts +340 -0
- package/src/cache/oauth-config-cache.d.ts +69 -0
- package/src/cache/oauth-config-cache.d.ts.map +1 -0
- package/src/cache/oauth-config-cache.js +71 -0
- package/src/cache/oauth-config-cache.js.map +1 -0
- package/src/cache/oauth-config-cache.ts +123 -0
- package/src/cache/tool-protection-cache.ts +171 -0
- package/src/compliance/EXAMPLE.md +412 -0
- package/src/compliance/__tests__/schema-verifier.test.ts +797 -0
- package/src/compliance/index.ts +8 -0
- package/src/compliance/schema-registry.ts +460 -0
- package/src/compliance/schema-verifier.ts +708 -0
- package/src/config/__tests__/remote-config.spec.ts +268 -0
- package/src/config/remote-config.ts +174 -0
- package/src/config.ts +309 -0
- package/src/delegation/__tests__/audience-validator.test.ts +112 -0
- package/src/delegation/__tests__/bitstring.test.ts +346 -0
- package/src/delegation/__tests__/cascading-revocation.test.ts +628 -0
- package/src/delegation/__tests__/delegation-graph.test.ts +584 -0
- package/src/delegation/__tests__/utils.test.ts +152 -0
- package/src/delegation/__tests__/vc-issuer.test.ts +442 -0
- package/src/delegation/__tests__/vc-verifier.test.ts +922 -0
- package/src/delegation/audience-validator.ts +52 -0
- package/src/delegation/bitstring.ts +278 -0
- package/src/delegation/cascading-revocation.ts +370 -0
- package/src/delegation/delegation-graph.ts +299 -0
- package/src/delegation/index.ts +14 -0
- package/src/delegation/statuslist-manager.ts +353 -0
- package/src/delegation/storage/__tests__/memory-graph-storage.test.ts +366 -0
- package/src/delegation/storage/__tests__/memory-statuslist-storage.test.ts +228 -0
- package/src/delegation/storage/index.ts +9 -0
- package/src/delegation/storage/memory-graph-storage.ts +178 -0
- package/src/delegation/storage/memory-statuslist-storage.ts +77 -0
- package/src/delegation/utils.ts +42 -0
- package/src/delegation/vc-issuer.ts +232 -0
- package/src/delegation/vc-verifier.ts +568 -0
- package/src/identity/idp-token-resolver.ts +147 -0
- package/src/identity/idp-token-storage.interface.ts +59 -0
- package/src/identity/user-did-manager.ts +370 -0
- package/src/index.ts +260 -0
- package/src/providers/base.d.ts +91 -0
- package/src/providers/base.d.ts.map +1 -0
- package/src/providers/base.js +38 -0
- package/src/providers/base.js.map +1 -0
- package/src/providers/base.ts +96 -0
- package/src/providers/memory.ts +142 -0
- package/src/runtime/audit-logger.ts +39 -0
- package/src/runtime/base.ts +1329 -0
- package/src/services/__tests__/access-control.integration.test.ts +443 -0
- package/src/services/__tests__/access-control.proof-response-validation.test.ts +578 -0
- package/src/services/__tests__/access-control.service.test.ts +970 -0
- package/src/services/__tests__/batch-delegation.service.test.ts +351 -0
- package/src/services/__tests__/crypto.service.test.ts +531 -0
- package/src/services/__tests__/oauth-provider-registry.test.ts +142 -0
- package/src/services/__tests__/proof-verifier.integration.test.ts +485 -0
- package/src/services/__tests__/proof-verifier.test.ts +489 -0
- package/src/services/__tests__/provider-resolution.integration.test.ts +198 -0
- package/src/services/__tests__/provider-resolver.test.ts +217 -0
- package/src/services/__tests__/storage.service.test.ts +358 -0
- package/src/services/access-control.service.ts +990 -0
- package/src/services/authorization/authorization-registry.ts +66 -0
- package/src/services/authorization/types.ts +71 -0
- package/src/services/batch-delegation.service.ts +137 -0
- package/src/services/crypto.service.ts +302 -0
- package/src/services/errors.ts +76 -0
- package/src/services/index.ts +9 -0
- package/src/services/oauth-config.service.d.ts +53 -0
- package/src/services/oauth-config.service.d.ts.map +1 -0
- package/src/services/oauth-config.service.js +113 -0
- package/src/services/oauth-config.service.js.map +1 -0
- package/src/services/oauth-config.service.ts +166 -0
- package/src/services/oauth-provider-registry.d.ts +57 -0
- package/src/services/oauth-provider-registry.d.ts.map +1 -0
- package/src/services/oauth-provider-registry.js +73 -0
- package/src/services/oauth-provider-registry.js.map +1 -0
- package/src/services/oauth-provider-registry.ts +123 -0
- package/src/services/oauth-service.ts +510 -0
- package/src/services/oauth-token-retrieval.service.ts +245 -0
- package/src/services/proof-verifier.ts +478 -0
- package/src/services/provider-resolver.d.ts +48 -0
- package/src/services/provider-resolver.d.ts.map +1 -0
- package/src/services/provider-resolver.js +106 -0
- package/src/services/provider-resolver.js.map +1 -0
- package/src/services/provider-resolver.ts +144 -0
- package/src/services/provider-validator.ts +170 -0
- package/src/services/storage.service.ts +566 -0
- package/src/services/tool-context-builder.ts +172 -0
- package/src/services/tool-protection.service.ts +958 -0
- package/src/types/oauth-required-error.ts +63 -0
- package/src/types/tool-protection.ts +155 -0
- package/src/utils/__tests__/did-helpers.test.ts +101 -0
- package/src/utils/base64.ts +148 -0
- package/src/utils/cors.ts +83 -0
- package/src/utils/did-helpers.ts +150 -0
- package/src/utils/index.ts +8 -0
- package/src/utils/storage-keys.ts +278 -0
- package/tsconfig.json +21 -0
- package/vitest.config.ts +56 -0
|
@@ -0,0 +1,970 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unit Tests for AccessControlApiService
|
|
3
|
+
*
|
|
4
|
+
* Comprehensive test coverage for the access control API service.
|
|
5
|
+
* Tests all methods (fetchConfig, verifyDelegation, submitProofs) with
|
|
6
|
+
* success cases, error cases, retry logic, and validation.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
|
10
|
+
import { AccessControlApiService } from '../access-control.service.js';
|
|
11
|
+
import { AgentShieldAPIError } from '@kya-os/contracts/agentshield-api';
|
|
12
|
+
import type { FetchProvider } from '../../providers/base.js';
|
|
13
|
+
import type {
|
|
14
|
+
VerifyDelegationRequest,
|
|
15
|
+
ToolProtectionConfigAPIResponse,
|
|
16
|
+
ProofSubmissionRequest,
|
|
17
|
+
} from '@kya-os/contracts/agentshield-api';
|
|
18
|
+
|
|
19
|
+
describe('AccessControlApiService', () => {
|
|
20
|
+
let service: AccessControlApiService;
|
|
21
|
+
let mockFetchProvider: FetchProvider;
|
|
22
|
+
let mockLogger: ReturnType<typeof vi.fn>;
|
|
23
|
+
let mockSleep: ReturnType<typeof vi.fn>;
|
|
24
|
+
|
|
25
|
+
beforeEach(() => {
|
|
26
|
+
mockLogger = vi.fn();
|
|
27
|
+
mockSleep = vi.fn().mockResolvedValue(undefined);
|
|
28
|
+
|
|
29
|
+
mockFetchProvider = {
|
|
30
|
+
resolveDID: vi.fn(),
|
|
31
|
+
fetchStatusList: vi.fn(),
|
|
32
|
+
fetchDelegationChain: vi.fn(),
|
|
33
|
+
fetch: vi.fn(),
|
|
34
|
+
} as unknown as FetchProvider;
|
|
35
|
+
|
|
36
|
+
service = new AccessControlApiService({
|
|
37
|
+
baseUrl: 'https://api.example.com',
|
|
38
|
+
apiKey: 'test-api-key',
|
|
39
|
+
fetchProvider: mockFetchProvider,
|
|
40
|
+
logger: mockLogger,
|
|
41
|
+
sleepProvider: mockSleep,
|
|
42
|
+
retryConfig: {
|
|
43
|
+
maxRetries: 2,
|
|
44
|
+
initialDelayMs: 10,
|
|
45
|
+
maxDelayMs: 100,
|
|
46
|
+
},
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
describe('fetchConfig', () => {
|
|
51
|
+
it('should fetch config successfully', async () => {
|
|
52
|
+
const mockResponse: ToolProtectionConfigAPIResponse = {
|
|
53
|
+
success: true,
|
|
54
|
+
data: {
|
|
55
|
+
agent_did: 'did:key:z123',
|
|
56
|
+
tools: {
|
|
57
|
+
'tool1': {
|
|
58
|
+
scopes: ['scope1'],
|
|
59
|
+
requires_delegation: true,
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
},
|
|
63
|
+
metadata: {
|
|
64
|
+
requestId: 'test-request-id',
|
|
65
|
+
timestamp: new Date().toISOString(),
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
(mockFetchProvider.fetch as ReturnType<typeof vi.fn>).mockResolvedValue(
|
|
70
|
+
new Response(JSON.stringify(mockResponse), {
|
|
71
|
+
status: 200,
|
|
72
|
+
headers: { 'Content-Type': 'application/json' },
|
|
73
|
+
})
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
const result = await service.fetchConfig({ agentDid: 'did:key:z123' });
|
|
77
|
+
|
|
78
|
+
expect(result).toEqual(mockResponse);
|
|
79
|
+
expect(mockFetchProvider.fetch).toHaveBeenCalledWith(
|
|
80
|
+
'https://api.example.com/api/v1/bouncer/config?agent_did=did%3Akey%3Az123',
|
|
81
|
+
expect.objectContaining({
|
|
82
|
+
method: 'GET',
|
|
83
|
+
headers: expect.objectContaining({
|
|
84
|
+
'Authorization': 'Bearer test-api-key',
|
|
85
|
+
'Content-Type': 'application/json',
|
|
86
|
+
}),
|
|
87
|
+
})
|
|
88
|
+
);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it('should handle 404 error', async () => {
|
|
92
|
+
const errorResponse = {
|
|
93
|
+
success: false,
|
|
94
|
+
error: {
|
|
95
|
+
code: 'config_not_found',
|
|
96
|
+
message: 'Config not found',
|
|
97
|
+
},
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
(mockFetchProvider.fetch as ReturnType<typeof vi.fn>).mockResolvedValue(
|
|
101
|
+
new Response(JSON.stringify(errorResponse), {
|
|
102
|
+
status: 404,
|
|
103
|
+
headers: { 'Content-Type': 'application/json' },
|
|
104
|
+
})
|
|
105
|
+
);
|
|
106
|
+
|
|
107
|
+
await expect(
|
|
108
|
+
service.fetchConfig({ agentDid: 'did:key:z123' })
|
|
109
|
+
).rejects.toThrow(AgentShieldAPIError);
|
|
110
|
+
|
|
111
|
+
const metrics = service.getMetrics();
|
|
112
|
+
expect(metrics.errorCount).toBe(1);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it('should retry on 500 error', async () => {
|
|
116
|
+
const mockResponse: ToolProtectionConfigAPIResponse = {
|
|
117
|
+
success: true,
|
|
118
|
+
data: {
|
|
119
|
+
agent_did: 'did:key:z123',
|
|
120
|
+
tools: {},
|
|
121
|
+
},
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
// First call fails with 500, second succeeds
|
|
125
|
+
(mockFetchProvider.fetch as ReturnType<typeof vi.fn>)
|
|
126
|
+
.mockResolvedValueOnce(
|
|
127
|
+
new Response('Internal Server Error', { status: 500 })
|
|
128
|
+
)
|
|
129
|
+
.mockResolvedValueOnce(
|
|
130
|
+
new Response(JSON.stringify(mockResponse), {
|
|
131
|
+
status: 200,
|
|
132
|
+
headers: { 'Content-Type': 'application/json' },
|
|
133
|
+
})
|
|
134
|
+
);
|
|
135
|
+
|
|
136
|
+
const result = await service.fetchConfig({ agentDid: 'did:key:z123' });
|
|
137
|
+
|
|
138
|
+
expect(result).toEqual(mockResponse);
|
|
139
|
+
expect(mockFetchProvider.fetch).toHaveBeenCalledTimes(2);
|
|
140
|
+
expect(mockSleep).toHaveBeenCalled();
|
|
141
|
+
expect(service.getMetrics().retryCount).toBe(1);
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
it('should handle invalid JSON response', async () => {
|
|
145
|
+
(mockFetchProvider.fetch as ReturnType<typeof vi.fn>)
|
|
146
|
+
.mockResolvedValueOnce(
|
|
147
|
+
new Response('invalid json', {
|
|
148
|
+
status: 200,
|
|
149
|
+
headers: { 'Content-Type': 'application/json' },
|
|
150
|
+
})
|
|
151
|
+
)
|
|
152
|
+
.mockResolvedValueOnce(
|
|
153
|
+
new Response('invalid json', {
|
|
154
|
+
status: 200,
|
|
155
|
+
headers: { 'Content-Type': 'application/json' },
|
|
156
|
+
})
|
|
157
|
+
);
|
|
158
|
+
|
|
159
|
+
await expect(
|
|
160
|
+
service.fetchConfig({ agentDid: 'did:key:z123' })
|
|
161
|
+
).rejects.toThrow(AgentShieldAPIError);
|
|
162
|
+
|
|
163
|
+
const error = await service
|
|
164
|
+
.fetchConfig({ agentDid: 'did:key:z123' })
|
|
165
|
+
.catch((e) => e);
|
|
166
|
+
expect(error).toBeInstanceOf(AgentShieldAPIError);
|
|
167
|
+
expect(error.code).toBe('invalid_response');
|
|
168
|
+
});
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
describe('verifyDelegation', () => {
|
|
172
|
+
it('should verify delegation successfully', async () => {
|
|
173
|
+
const request: VerifyDelegationRequest = {
|
|
174
|
+
agent_did: 'did:key:z123',
|
|
175
|
+
scopes: ['scope1', 'scope2'],
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
const mockResponse = {
|
|
179
|
+
success: true,
|
|
180
|
+
data: {
|
|
181
|
+
valid: true,
|
|
182
|
+
delegation_id: '123e4567-e89b-12d3-a456-426614174000', // Valid UUID format
|
|
183
|
+
credential: {
|
|
184
|
+
agent_did: 'did:key:z123',
|
|
185
|
+
scopes: ['scope1', 'scope2'],
|
|
186
|
+
issued_at: Math.floor(Date.now() / 1000), // Unix timestamp (positive integer)
|
|
187
|
+
created_at: Math.floor(Date.now() / 1000), // Unix timestamp (positive integer)
|
|
188
|
+
},
|
|
189
|
+
},
|
|
190
|
+
metadata: {
|
|
191
|
+
requestId: 'test-request-id',
|
|
192
|
+
timestamp: new Date().toISOString(),
|
|
193
|
+
},
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
(mockFetchProvider.fetch as ReturnType<typeof vi.fn>).mockResolvedValue(
|
|
197
|
+
new Response(JSON.stringify(mockResponse), {
|
|
198
|
+
status: 200,
|
|
199
|
+
headers: { 'Content-Type': 'application/json' },
|
|
200
|
+
})
|
|
201
|
+
);
|
|
202
|
+
|
|
203
|
+
const result = await service.verifyDelegation(request);
|
|
204
|
+
|
|
205
|
+
expect(result.data.valid).toBe(true);
|
|
206
|
+
expect(mockFetchProvider.fetch).toHaveBeenCalledWith(
|
|
207
|
+
'https://api.example.com/api/v1/bouncer/delegations/verify',
|
|
208
|
+
expect.objectContaining({
|
|
209
|
+
method: 'POST',
|
|
210
|
+
body: JSON.stringify({
|
|
211
|
+
agent_did: 'did:key:z123',
|
|
212
|
+
scopes: ['scope1', 'scope2'],
|
|
213
|
+
}),
|
|
214
|
+
})
|
|
215
|
+
);
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
it('should omit scopes field when scopes is undefined (truly optional)', async () => {
|
|
219
|
+
const request: VerifyDelegationRequest = {
|
|
220
|
+
agent_did: 'did:key:z123',
|
|
221
|
+
// scopes is intentionally omitted
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
const mockResponse = {
|
|
225
|
+
success: true,
|
|
226
|
+
data: { valid: true },
|
|
227
|
+
metadata: {
|
|
228
|
+
requestId: 'test-request-id',
|
|
229
|
+
timestamp: new Date().toISOString(),
|
|
230
|
+
},
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
(mockFetchProvider.fetch as ReturnType<typeof vi.fn>).mockResolvedValue(
|
|
234
|
+
new Response(JSON.stringify(mockResponse), {
|
|
235
|
+
status: 200,
|
|
236
|
+
headers: { 'Content-Type': 'application/json' },
|
|
237
|
+
})
|
|
238
|
+
);
|
|
239
|
+
|
|
240
|
+
try {
|
|
241
|
+
await service.verifyDelegation(request);
|
|
242
|
+
} catch (error: any) {
|
|
243
|
+
// Log validation errors and mock logger calls for debugging
|
|
244
|
+
if (error.details?.zodErrors) {
|
|
245
|
+
console.log('Validation errors:', JSON.stringify(error.details.zodErrors, null, 2));
|
|
246
|
+
}
|
|
247
|
+
// Check mock logger calls
|
|
248
|
+
const loggerCalls = mockLogger.mock.calls;
|
|
249
|
+
const debugCall = loggerCalls.find((call: any[]) =>
|
|
250
|
+
call[0]?.includes('Request body debug')
|
|
251
|
+
);
|
|
252
|
+
if (debugCall) {
|
|
253
|
+
console.log('Debug log:', JSON.stringify(debugCall[1], null, 2));
|
|
254
|
+
}
|
|
255
|
+
throw error;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
const callArgs = (mockFetchProvider.fetch as ReturnType<typeof vi.fn>).mock.calls[0];
|
|
259
|
+
const requestBody = JSON.parse(callArgs[1].body as string);
|
|
260
|
+
|
|
261
|
+
// Verify scopes field is not included in request body when undefined
|
|
262
|
+
expect(requestBody).not.toHaveProperty('scopes');
|
|
263
|
+
expect(requestBody.agent_did).toBe('did:key:z123');
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
it('should include scopes field when scopes is provided', async () => {
|
|
267
|
+
const request: VerifyDelegationRequest = {
|
|
268
|
+
agent_did: 'did:key:z123',
|
|
269
|
+
scopes: ['scope1', 'scope2'],
|
|
270
|
+
};
|
|
271
|
+
|
|
272
|
+
const mockResponse = {
|
|
273
|
+
success: true,
|
|
274
|
+
data: { valid: true },
|
|
275
|
+
metadata: {
|
|
276
|
+
requestId: 'test-request-id',
|
|
277
|
+
timestamp: new Date().toISOString(),
|
|
278
|
+
},
|
|
279
|
+
};
|
|
280
|
+
|
|
281
|
+
(mockFetchProvider.fetch as ReturnType<typeof vi.fn>).mockResolvedValue(
|
|
282
|
+
new Response(JSON.stringify(mockResponse), {
|
|
283
|
+
status: 200,
|
|
284
|
+
headers: { 'Content-Type': 'application/json' },
|
|
285
|
+
})
|
|
286
|
+
);
|
|
287
|
+
|
|
288
|
+
await service.verifyDelegation(request);
|
|
289
|
+
|
|
290
|
+
const callArgs = (mockFetchProvider.fetch as ReturnType<typeof vi.fn>).mock.calls[0];
|
|
291
|
+
const requestBody = JSON.parse(callArgs[1].body as string);
|
|
292
|
+
|
|
293
|
+
// Verify scopes field is included when provided
|
|
294
|
+
expect(requestBody.scopes).toEqual(['scope1', 'scope2']);
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
it('should include delegation_token from context', async () => {
|
|
298
|
+
const request: VerifyDelegationRequest = {
|
|
299
|
+
agent_did: 'did:key:z123',
|
|
300
|
+
scopes: ['scope1'],
|
|
301
|
+
};
|
|
302
|
+
|
|
303
|
+
const mockResponse = {
|
|
304
|
+
success: true,
|
|
305
|
+
data: { valid: true },
|
|
306
|
+
metadata: {
|
|
307
|
+
requestId: 'test-request-id',
|
|
308
|
+
timestamp: new Date().toISOString(),
|
|
309
|
+
},
|
|
310
|
+
};
|
|
311
|
+
|
|
312
|
+
(mockFetchProvider.fetch as ReturnType<typeof vi.fn>).mockResolvedValue(
|
|
313
|
+
new Response(JSON.stringify(mockResponse), {
|
|
314
|
+
status: 200,
|
|
315
|
+
headers: { 'Content-Type': 'application/json' },
|
|
316
|
+
})
|
|
317
|
+
);
|
|
318
|
+
|
|
319
|
+
await service.verifyDelegation(request, {
|
|
320
|
+
delegationToken: 'token-123',
|
|
321
|
+
credentialJwt: 'jwt-123',
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
expect(mockFetchProvider.fetch).toHaveBeenCalledWith(
|
|
325
|
+
expect.any(String),
|
|
326
|
+
expect.objectContaining({
|
|
327
|
+
body: expect.stringContaining('delegation_token'),
|
|
328
|
+
})
|
|
329
|
+
);
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
it('should handle validation error', async () => {
|
|
333
|
+
const invalidRequest = {
|
|
334
|
+
agent_did: '', // Invalid: empty string
|
|
335
|
+
scopes: [],
|
|
336
|
+
} as VerifyDelegationRequest;
|
|
337
|
+
|
|
338
|
+
// Mock fetch twice since we call verifyDelegation twice
|
|
339
|
+
(mockFetchProvider.fetch as ReturnType<typeof vi.fn>)
|
|
340
|
+
.mockResolvedValueOnce(
|
|
341
|
+
new Response(
|
|
342
|
+
JSON.stringify({
|
|
343
|
+
success: false,
|
|
344
|
+
error: {
|
|
345
|
+
code: 'validation_error',
|
|
346
|
+
message: 'Invalid request',
|
|
347
|
+
},
|
|
348
|
+
}),
|
|
349
|
+
{
|
|
350
|
+
status: 400,
|
|
351
|
+
headers: { 'Content-Type': 'application/json' },
|
|
352
|
+
}
|
|
353
|
+
)
|
|
354
|
+
)
|
|
355
|
+
.mockResolvedValueOnce(
|
|
356
|
+
new Response(
|
|
357
|
+
JSON.stringify({
|
|
358
|
+
success: false,
|
|
359
|
+
error: {
|
|
360
|
+
code: 'validation_error',
|
|
361
|
+
message: 'Invalid request',
|
|
362
|
+
},
|
|
363
|
+
}),
|
|
364
|
+
{
|
|
365
|
+
status: 400,
|
|
366
|
+
headers: { 'Content-Type': 'application/json' },
|
|
367
|
+
}
|
|
368
|
+
)
|
|
369
|
+
);
|
|
370
|
+
|
|
371
|
+
await expect(
|
|
372
|
+
service.verifyDelegation(invalidRequest)
|
|
373
|
+
).rejects.toThrow(AgentShieldAPIError);
|
|
374
|
+
|
|
375
|
+
const error = await service
|
|
376
|
+
.verifyDelegation(invalidRequest)
|
|
377
|
+
.catch((e) => e);
|
|
378
|
+
expect(error.code).toBe('validation_error');
|
|
379
|
+
});
|
|
380
|
+
|
|
381
|
+
it('should handle 401 authentication error', async () => {
|
|
382
|
+
const request: VerifyDelegationRequest = {
|
|
383
|
+
agent_did: 'did:key:z123',
|
|
384
|
+
scopes: ['scope1'],
|
|
385
|
+
};
|
|
386
|
+
|
|
387
|
+
// Mock fetch twice since we call verifyDelegation twice
|
|
388
|
+
(mockFetchProvider.fetch as ReturnType<typeof vi.fn>)
|
|
389
|
+
.mockResolvedValueOnce(
|
|
390
|
+
new Response(
|
|
391
|
+
JSON.stringify({
|
|
392
|
+
success: false,
|
|
393
|
+
error: {
|
|
394
|
+
code: 'authentication_failed',
|
|
395
|
+
message: 'Invalid API key',
|
|
396
|
+
},
|
|
397
|
+
}),
|
|
398
|
+
{
|
|
399
|
+
status: 401,
|
|
400
|
+
headers: { 'Content-Type': 'application/json' },
|
|
401
|
+
}
|
|
402
|
+
)
|
|
403
|
+
)
|
|
404
|
+
.mockResolvedValueOnce(
|
|
405
|
+
new Response(
|
|
406
|
+
JSON.stringify({
|
|
407
|
+
success: false,
|
|
408
|
+
error: {
|
|
409
|
+
code: 'authentication_failed',
|
|
410
|
+
message: 'Invalid API key',
|
|
411
|
+
},
|
|
412
|
+
}),
|
|
413
|
+
{
|
|
414
|
+
status: 401,
|
|
415
|
+
headers: { 'Content-Type': 'application/json' },
|
|
416
|
+
}
|
|
417
|
+
)
|
|
418
|
+
);
|
|
419
|
+
|
|
420
|
+
await expect(service.verifyDelegation(request)).rejects.toThrow(
|
|
421
|
+
AgentShieldAPIError
|
|
422
|
+
);
|
|
423
|
+
|
|
424
|
+
const error = await service.verifyDelegation(request).catch((e) => e);
|
|
425
|
+
expect(error.code).toBe('authentication_failed');
|
|
426
|
+
});
|
|
427
|
+
});
|
|
428
|
+
|
|
429
|
+
describe('submitProofs', () => {
|
|
430
|
+
it('should submit proofs successfully', async () => {
|
|
431
|
+
const request: ProofSubmissionRequest = {
|
|
432
|
+
session_id: '123e4567-e89b-12d3-a456-426614174000', // Valid UUID
|
|
433
|
+
delegation_id: null, // Explicitly set to null
|
|
434
|
+
proofs: [
|
|
435
|
+
{
|
|
436
|
+
jws: 'header.payload.signature',
|
|
437
|
+
meta: {
|
|
438
|
+
did: 'did:key:z123',
|
|
439
|
+
kid: 'did:key:z123#key-1',
|
|
440
|
+
ts: Math.floor(Date.now() / 1000), // Unix timestamp in seconds
|
|
441
|
+
nonce: 'nonce-123',
|
|
442
|
+
audience: 'mcp-client',
|
|
443
|
+
sessionId: 'session-123',
|
|
444
|
+
requestHash: 'sha256:' + 'a'.repeat(64),
|
|
445
|
+
responseHash: 'sha256:' + 'b'.repeat(64),
|
|
446
|
+
},
|
|
447
|
+
},
|
|
448
|
+
],
|
|
449
|
+
};
|
|
450
|
+
|
|
451
|
+
const mockResponse = {
|
|
452
|
+
// Direct ProofSubmissionResponse (not wrapped)
|
|
453
|
+
success: true,
|
|
454
|
+
accepted: 1,
|
|
455
|
+
rejected: 0,
|
|
456
|
+
outcomes: {
|
|
457
|
+
success: 1,
|
|
458
|
+
failed: 0,
|
|
459
|
+
blocked: 0,
|
|
460
|
+
error: 0,
|
|
461
|
+
},
|
|
462
|
+
};
|
|
463
|
+
|
|
464
|
+
(mockFetchProvider.fetch as ReturnType<typeof vi.fn>).mockResolvedValue(
|
|
465
|
+
new Response(JSON.stringify(mockResponse), {
|
|
466
|
+
status: 200,
|
|
467
|
+
headers: { 'Content-Type': 'application/json' },
|
|
468
|
+
})
|
|
469
|
+
);
|
|
470
|
+
|
|
471
|
+
const result = await service.submitProofs(request);
|
|
472
|
+
|
|
473
|
+
expect(result.accepted).toBe(1);
|
|
474
|
+
expect(result.rejected).toBe(0);
|
|
475
|
+
expect(mockFetchProvider.fetch).toHaveBeenCalledWith(
|
|
476
|
+
'https://api.example.com/api/v1/bouncer/proofs',
|
|
477
|
+
expect.objectContaining({
|
|
478
|
+
method: 'POST',
|
|
479
|
+
body: JSON.stringify(request),
|
|
480
|
+
})
|
|
481
|
+
);
|
|
482
|
+
});
|
|
483
|
+
|
|
484
|
+
it('should handle all_proofs_rejected error gracefully', async () => {
|
|
485
|
+
const request: ProofSubmissionRequest = {
|
|
486
|
+
session_id: '123e4567-e89b-12d3-a456-426614174000', // Valid UUID
|
|
487
|
+
delegation_id: null, // Explicitly set to null
|
|
488
|
+
proofs: [
|
|
489
|
+
{
|
|
490
|
+
jws: 'header.payload.signature',
|
|
491
|
+
meta: {
|
|
492
|
+
did: 'did:key:z123',
|
|
493
|
+
kid: 'did:key:z123#key-1',
|
|
494
|
+
ts: Math.floor(Date.now() / 1000), // Unix timestamp in seconds
|
|
495
|
+
nonce: 'nonce-123',
|
|
496
|
+
audience: 'mcp-client',
|
|
497
|
+
sessionId: 'session-123',
|
|
498
|
+
requestHash: 'sha256:' + 'a'.repeat(64),
|
|
499
|
+
responseHash: 'sha256:' + 'b'.repeat(64),
|
|
500
|
+
},
|
|
501
|
+
},
|
|
502
|
+
],
|
|
503
|
+
};
|
|
504
|
+
|
|
505
|
+
const errorResponse = {
|
|
506
|
+
success: false,
|
|
507
|
+
error: {
|
|
508
|
+
code: 'all_proofs_rejected',
|
|
509
|
+
message: 'All proofs rejected',
|
|
510
|
+
details: {
|
|
511
|
+
rejected: 1,
|
|
512
|
+
errors: [
|
|
513
|
+
{
|
|
514
|
+
proof_index: 0,
|
|
515
|
+
error: {
|
|
516
|
+
code: 'invalid_signature',
|
|
517
|
+
message: 'Invalid signature',
|
|
518
|
+
},
|
|
519
|
+
},
|
|
520
|
+
],
|
|
521
|
+
},
|
|
522
|
+
},
|
|
523
|
+
};
|
|
524
|
+
|
|
525
|
+
(mockFetchProvider.fetch as ReturnType<typeof vi.fn>).mockResolvedValue(
|
|
526
|
+
new Response(JSON.stringify(errorResponse), {
|
|
527
|
+
status: 400,
|
|
528
|
+
headers: { 'Content-Type': 'application/json' },
|
|
529
|
+
})
|
|
530
|
+
);
|
|
531
|
+
|
|
532
|
+
const result = await service.submitProofs(request);
|
|
533
|
+
|
|
534
|
+
// ProofSubmissionResponse has a success field
|
|
535
|
+
expect(result.success).toBe(false);
|
|
536
|
+
expect(result.accepted).toBe(0);
|
|
537
|
+
expect(result.rejected).toBe(1);
|
|
538
|
+
expect(result.errors).toBeDefined();
|
|
539
|
+
});
|
|
540
|
+
|
|
541
|
+
it('should handle wrapped response format', async () => {
|
|
542
|
+
const request: ProofSubmissionRequest = {
|
|
543
|
+
session_id: '123e4567-e89b-12d3-a456-426614174000', // Valid UUID
|
|
544
|
+
delegation_id: null, // Explicitly set to null
|
|
545
|
+
proofs: [
|
|
546
|
+
{
|
|
547
|
+
jws: 'header.payload.signature',
|
|
548
|
+
meta: {
|
|
549
|
+
did: 'did:key:z123',
|
|
550
|
+
kid: 'did:key:z123#key-1',
|
|
551
|
+
ts: Math.floor(Date.now() / 1000), // Unix timestamp in seconds
|
|
552
|
+
nonce: 'nonce-123',
|
|
553
|
+
audience: 'mcp-client',
|
|
554
|
+
sessionId: 'session-123',
|
|
555
|
+
requestHash: 'sha256:' + 'a'.repeat(64),
|
|
556
|
+
responseHash: 'sha256:' + 'b'.repeat(64),
|
|
557
|
+
},
|
|
558
|
+
},
|
|
559
|
+
],
|
|
560
|
+
};
|
|
561
|
+
|
|
562
|
+
const wrappedResponse = {
|
|
563
|
+
success: true,
|
|
564
|
+
data: {
|
|
565
|
+
// ProofSubmissionResponse has a success field
|
|
566
|
+
success: true,
|
|
567
|
+
accepted: 1,
|
|
568
|
+
rejected: 0,
|
|
569
|
+
outcomes: {
|
|
570
|
+
success: 1,
|
|
571
|
+
failed: 0,
|
|
572
|
+
blocked: 0,
|
|
573
|
+
error: 0,
|
|
574
|
+
},
|
|
575
|
+
},
|
|
576
|
+
metadata: {
|
|
577
|
+
requestId: 'test-request-id',
|
|
578
|
+
timestamp: new Date().toISOString(),
|
|
579
|
+
},
|
|
580
|
+
};
|
|
581
|
+
|
|
582
|
+
(mockFetchProvider.fetch as ReturnType<typeof vi.fn>).mockResolvedValue(
|
|
583
|
+
new Response(JSON.stringify(wrappedResponse), {
|
|
584
|
+
status: 200,
|
|
585
|
+
headers: { 'Content-Type': 'application/json' },
|
|
586
|
+
})
|
|
587
|
+
);
|
|
588
|
+
|
|
589
|
+
const result = await service.submitProofs(request);
|
|
590
|
+
|
|
591
|
+
expect(result.accepted).toBe(1);
|
|
592
|
+
});
|
|
593
|
+
|
|
594
|
+
it('should handle invalid response format with detailed error logging', async () => {
|
|
595
|
+
const request: ProofSubmissionRequest = {
|
|
596
|
+
session_id: '123e4567-e89b-12d3-a456-426614174000',
|
|
597
|
+
delegation_id: null,
|
|
598
|
+
proofs: [
|
|
599
|
+
{
|
|
600
|
+
jws: 'header.payload.signature',
|
|
601
|
+
meta: {
|
|
602
|
+
did: 'did:key:z123',
|
|
603
|
+
kid: 'did:key:z123#key-1',
|
|
604
|
+
ts: Math.floor(Date.now() / 1000),
|
|
605
|
+
nonce: 'nonce-123',
|
|
606
|
+
audience: 'mcp-client',
|
|
607
|
+
sessionId: 'session-123',
|
|
608
|
+
requestHash: 'sha256:' + 'a'.repeat(64),
|
|
609
|
+
responseHash: 'sha256:' + 'b'.repeat(64),
|
|
610
|
+
},
|
|
611
|
+
},
|
|
612
|
+
],
|
|
613
|
+
};
|
|
614
|
+
|
|
615
|
+
// Invalid response: missing required fields (accepted, rejected, outcomes)
|
|
616
|
+
const invalidResponse = {
|
|
617
|
+
success: true,
|
|
618
|
+
message: 'Proof submitted', // Wrong format - missing required fields
|
|
619
|
+
};
|
|
620
|
+
|
|
621
|
+
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
|
|
622
|
+
|
|
623
|
+
(mockFetchProvider.fetch as ReturnType<typeof vi.fn>).mockResolvedValue(
|
|
624
|
+
new Response(JSON.stringify(invalidResponse), {
|
|
625
|
+
status: 200,
|
|
626
|
+
headers: { 'Content-Type': 'application/json' },
|
|
627
|
+
})
|
|
628
|
+
);
|
|
629
|
+
|
|
630
|
+
const error = await service.submitProofs(request).catch((e) => e);
|
|
631
|
+
expect(error).toBeInstanceOf(AgentShieldAPIError);
|
|
632
|
+
expect((error as AgentShieldAPIError).code).toBe('invalid_response');
|
|
633
|
+
expect((error as AgentShieldAPIError).message).toBe('Response validation failed');
|
|
634
|
+
|
|
635
|
+
// Verify detailed error logging
|
|
636
|
+
expect(consoleErrorSpy).toHaveBeenCalled();
|
|
637
|
+
const errorCall = consoleErrorSpy.mock.calls.find(call =>
|
|
638
|
+
call[0]?.includes('Response validation failed')
|
|
639
|
+
);
|
|
640
|
+
expect(errorCall).toBeDefined();
|
|
641
|
+
expect(errorCall?.[1]).toHaveProperty('zodErrors');
|
|
642
|
+
expect(errorCall?.[1]).toHaveProperty('responseData');
|
|
643
|
+
|
|
644
|
+
consoleErrorSpy.mockRestore();
|
|
645
|
+
});
|
|
646
|
+
|
|
647
|
+
it('should handle response with missing outcomes field (outcomes is optional)', async () => {
|
|
648
|
+
const request: ProofSubmissionRequest = {
|
|
649
|
+
session_id: '123e4567-e89b-12d3-a456-426614174000',
|
|
650
|
+
delegation_id: null,
|
|
651
|
+
proofs: [
|
|
652
|
+
{
|
|
653
|
+
jws: 'header.payload.signature',
|
|
654
|
+
meta: {
|
|
655
|
+
did: 'did:key:z123',
|
|
656
|
+
kid: 'did:key:z123#key-1',
|
|
657
|
+
ts: Math.floor(Date.now() / 1000),
|
|
658
|
+
nonce: 'nonce-123',
|
|
659
|
+
audience: 'mcp-client',
|
|
660
|
+
sessionId: 'session-123',
|
|
661
|
+
requestHash: 'sha256:' + 'a'.repeat(64),
|
|
662
|
+
responseHash: 'sha256:' + 'b'.repeat(64),
|
|
663
|
+
},
|
|
664
|
+
},
|
|
665
|
+
],
|
|
666
|
+
};
|
|
667
|
+
|
|
668
|
+
// Test 1: Response WITHOUT outcomes field (valid - outcomes is optional)
|
|
669
|
+
const responseWithoutOutcomes = {
|
|
670
|
+
success: true,
|
|
671
|
+
accepted: 1,
|
|
672
|
+
rejected: 0,
|
|
673
|
+
// Missing outcomes field - this is now valid
|
|
674
|
+
};
|
|
675
|
+
|
|
676
|
+
(mockFetchProvider.fetch as ReturnType<typeof vi.fn>).mockResolvedValueOnce(
|
|
677
|
+
new Response(JSON.stringify(responseWithoutOutcomes), {
|
|
678
|
+
status: 200,
|
|
679
|
+
headers: { 'Content-Type': 'application/json' },
|
|
680
|
+
})
|
|
681
|
+
);
|
|
682
|
+
|
|
683
|
+
const result1 = await service.submitProofs(request);
|
|
684
|
+
expect(result1).toEqual({
|
|
685
|
+
success: true,
|
|
686
|
+
accepted: 1,
|
|
687
|
+
rejected: 0,
|
|
688
|
+
outcomes: undefined, // outcomes is optional
|
|
689
|
+
});
|
|
690
|
+
|
|
691
|
+
// Test 2: Response WITH outcomes field (also valid)
|
|
692
|
+
const responseWithOutcomes = {
|
|
693
|
+
success: true,
|
|
694
|
+
accepted: 1,
|
|
695
|
+
rejected: 0,
|
|
696
|
+
outcomes: {
|
|
697
|
+
success: 1,
|
|
698
|
+
failed: 0,
|
|
699
|
+
blocked: 0,
|
|
700
|
+
error: 0,
|
|
701
|
+
},
|
|
702
|
+
};
|
|
703
|
+
|
|
704
|
+
(mockFetchProvider.fetch as ReturnType<typeof vi.fn>).mockResolvedValueOnce(
|
|
705
|
+
new Response(JSON.stringify(responseWithOutcomes), {
|
|
706
|
+
status: 200,
|
|
707
|
+
headers: { 'Content-Type': 'application/json' },
|
|
708
|
+
})
|
|
709
|
+
);
|
|
710
|
+
|
|
711
|
+
const result2 = await service.submitProofs(request);
|
|
712
|
+
expect(result2).toEqual({
|
|
713
|
+
success: true,
|
|
714
|
+
accepted: 1,
|
|
715
|
+
rejected: 0,
|
|
716
|
+
outcomes: {
|
|
717
|
+
success: 1,
|
|
718
|
+
failed: 0,
|
|
719
|
+
blocked: 0,
|
|
720
|
+
error: 0,
|
|
721
|
+
},
|
|
722
|
+
});
|
|
723
|
+
|
|
724
|
+
// Test 3: Response with empty outcomes object (also valid)
|
|
725
|
+
const responseWithEmptyOutcomes = {
|
|
726
|
+
success: true,
|
|
727
|
+
accepted: 1,
|
|
728
|
+
rejected: 0,
|
|
729
|
+
outcomes: {},
|
|
730
|
+
};
|
|
731
|
+
|
|
732
|
+
(mockFetchProvider.fetch as ReturnType<typeof vi.fn>).mockResolvedValueOnce(
|
|
733
|
+
new Response(JSON.stringify(responseWithEmptyOutcomes), {
|
|
734
|
+
status: 200,
|
|
735
|
+
headers: { 'Content-Type': 'application/json' },
|
|
736
|
+
})
|
|
737
|
+
);
|
|
738
|
+
|
|
739
|
+
const result3 = await service.submitProofs(request);
|
|
740
|
+
expect(result3).toEqual({
|
|
741
|
+
success: true,
|
|
742
|
+
accepted: 1,
|
|
743
|
+
rejected: 0,
|
|
744
|
+
outcomes: {},
|
|
745
|
+
});
|
|
746
|
+
});
|
|
747
|
+
|
|
748
|
+
it('should handle wrapped response with invalid data structure', async () => {
|
|
749
|
+
const request: ProofSubmissionRequest = {
|
|
750
|
+
session_id: '123e4567-e89b-12d3-a456-426614174000',
|
|
751
|
+
delegation_id: null,
|
|
752
|
+
proofs: [
|
|
753
|
+
{
|
|
754
|
+
jws: 'header.payload.signature',
|
|
755
|
+
meta: {
|
|
756
|
+
did: 'did:key:z123',
|
|
757
|
+
kid: 'did:key:z123#key-1',
|
|
758
|
+
ts: Math.floor(Date.now() / 1000),
|
|
759
|
+
nonce: 'nonce-123',
|
|
760
|
+
audience: 'mcp-client',
|
|
761
|
+
sessionId: 'session-123',
|
|
762
|
+
requestHash: 'sha256:' + 'a'.repeat(64),
|
|
763
|
+
responseHash: 'sha256:' + 'b'.repeat(64),
|
|
764
|
+
},
|
|
765
|
+
},
|
|
766
|
+
],
|
|
767
|
+
};
|
|
768
|
+
|
|
769
|
+
// Wrapped response but data is invalid
|
|
770
|
+
const invalidWrappedResponse = {
|
|
771
|
+
success: true,
|
|
772
|
+
data: {
|
|
773
|
+
// Missing required fields
|
|
774
|
+
message: 'Invalid format',
|
|
775
|
+
},
|
|
776
|
+
};
|
|
777
|
+
|
|
778
|
+
(mockFetchProvider.fetch as ReturnType<typeof vi.fn>).mockResolvedValue(
|
|
779
|
+
new Response(JSON.stringify(invalidWrappedResponse), {
|
|
780
|
+
status: 200,
|
|
781
|
+
headers: { 'Content-Type': 'application/json' },
|
|
782
|
+
})
|
|
783
|
+
);
|
|
784
|
+
|
|
785
|
+
const error = await service.submitProofs(request).catch((e) => e);
|
|
786
|
+
expect(error).toBeInstanceOf(AgentShieldAPIError);
|
|
787
|
+
expect((error as AgentShieldAPIError).code).toBe('invalid_response');
|
|
788
|
+
});
|
|
789
|
+
|
|
790
|
+
it('should validate request schema', async () => {
|
|
791
|
+
const invalidRequest = {
|
|
792
|
+
session_id: '', // Invalid: empty string
|
|
793
|
+
proofs: [],
|
|
794
|
+
} as ProofSubmissionRequest;
|
|
795
|
+
|
|
796
|
+
await expect(service.submitProofs(invalidRequest)).rejects.toThrow(
|
|
797
|
+
AgentShieldAPIError
|
|
798
|
+
);
|
|
799
|
+
|
|
800
|
+
const error = await service
|
|
801
|
+
.submitProofs(invalidRequest)
|
|
802
|
+
.catch((e) => e);
|
|
803
|
+
expect(error.code).toBe('validation_error');
|
|
804
|
+
});
|
|
805
|
+
});
|
|
806
|
+
|
|
807
|
+
describe('retry logic', () => {
|
|
808
|
+
it('should retry on network errors', async () => {
|
|
809
|
+
const mockResponse = {
|
|
810
|
+
success: true,
|
|
811
|
+
data: {
|
|
812
|
+
agent_did: 'did:key:z123',
|
|
813
|
+
tools: {},
|
|
814
|
+
},
|
|
815
|
+
metadata: {
|
|
816
|
+
requestId: 'test-request-id',
|
|
817
|
+
timestamp: new Date().toISOString(),
|
|
818
|
+
},
|
|
819
|
+
};
|
|
820
|
+
|
|
821
|
+
// First call throws network error, second succeeds
|
|
822
|
+
(mockFetchProvider.fetch as ReturnType<typeof vi.fn>)
|
|
823
|
+
.mockRejectedValueOnce(new TypeError('fetch failed'))
|
|
824
|
+
.mockResolvedValueOnce(
|
|
825
|
+
new Response(JSON.stringify(mockResponse), {
|
|
826
|
+
status: 200,
|
|
827
|
+
headers: { 'Content-Type': 'application/json' },
|
|
828
|
+
})
|
|
829
|
+
);
|
|
830
|
+
|
|
831
|
+
const result = await service.fetchConfig({ agentDid: 'did:key:z123' });
|
|
832
|
+
|
|
833
|
+
expect(result).toBeDefined();
|
|
834
|
+
expect(mockFetchProvider.fetch).toHaveBeenCalledTimes(2);
|
|
835
|
+
expect(service.getMetrics().retryCount).toBe(1);
|
|
836
|
+
});
|
|
837
|
+
|
|
838
|
+
it('should not retry on 400 errors', async () => {
|
|
839
|
+
(mockFetchProvider.fetch as ReturnType<typeof vi.fn>).mockResolvedValue(
|
|
840
|
+
new Response(
|
|
841
|
+
JSON.stringify({
|
|
842
|
+
success: false,
|
|
843
|
+
error: {
|
|
844
|
+
code: 'validation_error',
|
|
845
|
+
message: 'Invalid request',
|
|
846
|
+
},
|
|
847
|
+
}),
|
|
848
|
+
{
|
|
849
|
+
status: 400,
|
|
850
|
+
headers: { 'Content-Type': 'application/json' },
|
|
851
|
+
}
|
|
852
|
+
)
|
|
853
|
+
);
|
|
854
|
+
|
|
855
|
+
await expect(
|
|
856
|
+
service.fetchConfig({ agentDid: 'did:key:z123' })
|
|
857
|
+
).rejects.toThrow(AgentShieldAPIError);
|
|
858
|
+
|
|
859
|
+
expect(mockFetchProvider.fetch).toHaveBeenCalledTimes(1);
|
|
860
|
+
expect(mockSleep).not.toHaveBeenCalled();
|
|
861
|
+
});
|
|
862
|
+
|
|
863
|
+
it('should respect maxRetries limit', async () => {
|
|
864
|
+
// All calls fail with 500 - need to mock multiple times for retries
|
|
865
|
+
(mockFetchProvider.fetch as ReturnType<typeof vi.fn>)
|
|
866
|
+
.mockResolvedValueOnce(new Response('Internal Server Error', { status: 500 }))
|
|
867
|
+
.mockResolvedValueOnce(new Response('Internal Server Error', { status: 500 }))
|
|
868
|
+
.mockResolvedValueOnce(new Response('Internal Server Error', { status: 500 }));
|
|
869
|
+
|
|
870
|
+
await expect(
|
|
871
|
+
service.fetchConfig({ agentDid: 'did:key:z123' })
|
|
872
|
+
).rejects.toThrow();
|
|
873
|
+
|
|
874
|
+
// Should retry maxRetries times (2) + initial attempt = 3 total
|
|
875
|
+
expect(mockFetchProvider.fetch).toHaveBeenCalledTimes(3);
|
|
876
|
+
expect(service.getMetrics().retryCount).toBe(2);
|
|
877
|
+
expect(service.getMetrics().errorCount).toBe(1);
|
|
878
|
+
});
|
|
879
|
+
});
|
|
880
|
+
|
|
881
|
+
describe('metrics', () => {
|
|
882
|
+
it('should track successful requests', async () => {
|
|
883
|
+
const mockResponse = {
|
|
884
|
+
success: true,
|
|
885
|
+
data: {
|
|
886
|
+
agent_did: 'did:key:z123',
|
|
887
|
+
tools: {},
|
|
888
|
+
},
|
|
889
|
+
metadata: {
|
|
890
|
+
requestId: 'test-request-id',
|
|
891
|
+
timestamp: new Date().toISOString(),
|
|
892
|
+
},
|
|
893
|
+
};
|
|
894
|
+
|
|
895
|
+
(mockFetchProvider.fetch as ReturnType<typeof vi.fn>).mockResolvedValue(
|
|
896
|
+
new Response(JSON.stringify(mockResponse), {
|
|
897
|
+
status: 200,
|
|
898
|
+
headers: { 'Content-Type': 'application/json' },
|
|
899
|
+
})
|
|
900
|
+
);
|
|
901
|
+
|
|
902
|
+
await service.fetchConfig({ agentDid: 'did:key:z123' });
|
|
903
|
+
|
|
904
|
+
const metrics = service.getMetrics();
|
|
905
|
+
expect(metrics.successCount).toBe(1);
|
|
906
|
+
expect(metrics.errorCount).toBe(0);
|
|
907
|
+
});
|
|
908
|
+
|
|
909
|
+
it('should reset metrics', async () => {
|
|
910
|
+
const mockResponse = {
|
|
911
|
+
success: true,
|
|
912
|
+
data: {
|
|
913
|
+
agent_did: 'did:key:z123',
|
|
914
|
+
tools: {},
|
|
915
|
+
},
|
|
916
|
+
metadata: {
|
|
917
|
+
requestId: 'test-request-id',
|
|
918
|
+
timestamp: new Date().toISOString(),
|
|
919
|
+
},
|
|
920
|
+
};
|
|
921
|
+
|
|
922
|
+
(mockFetchProvider.fetch as ReturnType<typeof vi.fn>).mockResolvedValue(
|
|
923
|
+
new Response(JSON.stringify(mockResponse), {
|
|
924
|
+
status: 200,
|
|
925
|
+
headers: { 'Content-Type': 'application/json' },
|
|
926
|
+
})
|
|
927
|
+
);
|
|
928
|
+
|
|
929
|
+
await service.fetchConfig({ agentDid: 'did:key:z123' });
|
|
930
|
+
service.resetMetrics();
|
|
931
|
+
|
|
932
|
+
const metrics = service.getMetrics();
|
|
933
|
+
expect(metrics.successCount).toBe(0);
|
|
934
|
+
expect(metrics.errorCount).toBe(0);
|
|
935
|
+
expect(metrics.retryCount).toBe(0);
|
|
936
|
+
});
|
|
937
|
+
});
|
|
938
|
+
|
|
939
|
+
describe('correlation ID', () => {
|
|
940
|
+
it('should include correlation ID in headers', async () => {
|
|
941
|
+
const mockResponse = {
|
|
942
|
+
success: true,
|
|
943
|
+
data: {
|
|
944
|
+
agent_did: 'did:key:z123',
|
|
945
|
+
tools: {},
|
|
946
|
+
},
|
|
947
|
+
metadata: {
|
|
948
|
+
requestId: 'test-request-id',
|
|
949
|
+
timestamp: new Date().toISOString(),
|
|
950
|
+
},
|
|
951
|
+
};
|
|
952
|
+
|
|
953
|
+
(mockFetchProvider.fetch as ReturnType<typeof vi.fn>).mockResolvedValue(
|
|
954
|
+
new Response(JSON.stringify(mockResponse), {
|
|
955
|
+
status: 200,
|
|
956
|
+
headers: { 'Content-Type': 'application/json' },
|
|
957
|
+
})
|
|
958
|
+
);
|
|
959
|
+
|
|
960
|
+
await service.fetchConfig({ agentDid: 'did:key:z123' });
|
|
961
|
+
|
|
962
|
+
const callArgs = (mockFetchProvider.fetch as ReturnType<typeof vi.fn>)
|
|
963
|
+
.mock.calls[0];
|
|
964
|
+
const headers = callArgs[1].headers;
|
|
965
|
+
expect(headers['X-Request-ID']).toBeDefined();
|
|
966
|
+
expect(typeof headers['X-Request-ID']).toBe('string');
|
|
967
|
+
});
|
|
968
|
+
});
|
|
969
|
+
});
|
|
970
|
+
|