@kya-os/mcp-i
Version:
The TypeScript MCP framework with identity features built-in
141 lines (140 loc) • 4.9 kB
JavaScript
;
/**
* Runtime integration for test infrastructure
*
* This module provides integration points between the test infrastructure
* and the XMCP-I runtime to ensure test mode works seamlessly.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.getTestRuntimeConfig = getTestRuntimeConfig;
exports.interceptKTACall = interceptKTACall;
exports.getTestIdentityForRuntime = getTestIdentityForRuntime;
exports.createTestSessionContext = createTestSessionContext;
exports.validateTestEnvironment = validateTestEnvironment;
exports.testModeOnly = testModeOnly;
const test_environment_1 = require("./test-environment");
const mock_identity_provider_1 = require("./mock-identity-provider");
const test_1 = require("@kya-os/contracts/test");
/**
* Get test runtime configuration
*/
function getTestRuntimeConfig() {
if (!(0, test_environment_1.isTestMode)()) {
return null;
}
return {
skipKTACalls: (0, test_environment_1.shouldSkipKTACalls)(),
useMockIdentities: true,
deterministicKeys: true,
mockKTAResponses: true,
};
}
/**
* Intercept KTA calls in test mode
*/
async function interceptKTACall(operation, originalCall, mockResponse) {
const testConfig = getTestRuntimeConfig();
if (!testConfig || !testConfig.skipKTACalls) {
return originalCall();
}
if (mockResponse) {
return mockResponse();
}
// Default mock responses based on operation
switch (operation) {
case "register":
return {
success: true,
agentURL: "https://test-kta.example.com/agents/test-agent",
agentId: "test-agent",
agentSlug: "test-agent",
verificationEndpoint: "https://test-kta.example.com/verify/test-agent",
conformanceCapabilities: ["handshake", "signing", "verification"],
mirrorStatus: "success",
};
case "checkDelegation":
return {
status: "active",
valid: true,
};
case "claim":
return {
success: true,
claimed: true,
agentURL: "https://test-kta.example.com/agents/test-agent",
};
default:
throw new Error(`${test_1.TEST_ERROR_CODES.INVALID_TEST_CONFIGURATION}: Unknown KTA operation: ${operation}`);
}
}
/**
* Get test identity for runtime use
*/
function getTestIdentityForRuntime(identityKey = "agent1") {
if (!(0, test_environment_1.isTestMode)()) {
throw new Error(`${test_1.TEST_ERROR_CODES.INVALID_TEST_CONFIGURATION}: Test identity can only be used in test mode`);
}
const mockProvider = (0, mock_identity_provider_1.getMockIdentityProvider)();
const identity = mockProvider.getIdentity(identityKey);
if (!identity) {
throw new Error(`${test_1.TEST_ERROR_CODES.INVALID_TEST_CONFIGURATION}: Test identity '${identityKey}' not found`);
}
return identity;
}
/**
* Create test session context
*/
function createTestSessionContext(options = {}) {
if (!(0, test_environment_1.isTestMode)()) {
throw new Error(`${test_1.TEST_ERROR_CODES.INVALID_TEST_CONFIGURATION}: Test session context can only be created in test mode`);
}
const now = Math.floor(Date.now() / 1000);
return {
sessionId: options.sessionId || "sess_test_default",
audience: options.audience || "test.example.com",
nonce: options.nonce || "test_nonce_123",
timestamp: options.timestamp || now,
createdAt: now,
lastActivity: now,
ttlMinutes: 30,
};
}
/**
* Validate test environment setup
*/
function validateTestEnvironment() {
const errors = [];
const warnings = [];
if (!(0, test_environment_1.isTestMode)()) {
errors.push("XMCP_ENV is not set to 'test'");
}
if (!(0, test_environment_1.shouldSkipKTACalls)()) {
warnings.push("KTA calls are not being skipped in test mode");
}
try {
const mockProvider = (0, mock_identity_provider_1.getMockIdentityProvider)();
const identities = mockProvider.getAllIdentities();
if (Object.keys(identities).length === 0) {
warnings.push("No test identities are configured");
}
}
catch (error) {
errors.push(`Failed to access mock identity provider: ${error instanceof Error ? error.message : "Unknown error"}`);
}
return {
valid: errors.length === 0,
errors,
warnings,
};
}
/**
* Test mode guard decorator
*/
function testModeOnly(fn) {
return ((...args) => {
if (!(0, test_environment_1.isTestMode)()) {
throw new Error(`${test_1.TEST_ERROR_CODES.INVALID_TEST_CONFIGURATION}: Function can only be called in test mode`);
}
return fn(...args);
});
}