test-sap-api.jsโข3.76 kB
import { config } from 'dotenv';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
import { readFileSync, existsSync } from 'fs';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
config({ path: join(__dirname, '..', '.env') });
console.log('๐งช Testing SAP Notes API with cached token...\n');
// Read cached token
const tokenCacheFile = join(__dirname, '..', 'token-cache.json');
if (!existsSync(tokenCacheFile)) {
console.error('โ No cached token found. Run authentication first with: npm run test-auth');
process.exit(1);
}
const tokenCache = JSON.parse(readFileSync(tokenCacheFile, 'utf8'));
const token = tokenCache.access_token;
console.log('๐ Using cached authentication token');
console.log(` Token length: ${token.length} characters\n`);
// Import and test the SAP Notes API
const { SapNotesApiClient } = await import('../dist/sap-notes-api.js');
const config_obj = {
pfxPath: process.env.PFX_PATH,
pfxPassphrase: process.env.PFX_PASSPHRASE,
coveoOrg: process.env.COVEO_ORG || 'sapamericaproductiontyfzmfz0',
coveoHost: process.env.COVEO_HOST || 'platform.cloud.coveo.com',
maxJwtAgeH: parseInt(process.env.MAX_JWT_AGE_H || '12'),
headful: process.env.HEADFUL === 'true',
logLevel: process.env.LOG_LEVEL || 'info'
};
const sapNotesClient = new SapNotesApiClient(config_obj);
try {
console.log('๐ Testing SAP Notes search...');
// Test search
const searchResult = await sapNotesClient.searchNotes('2744792', token, 5);
console.log(`โ
Search completed!`);
console.log(` Query: "${searchResult.query}"`);
console.log(` Total results: ${searchResult.totalResults}`);
console.log(` Results returned: ${searchResult.results.length}\n`);
if (searchResult.results.length > 0) {
console.log('๐ First result:');
const firstResult = searchResult.results[0];
console.log(` ID: ${firstResult.id}`);
console.log(` Title: ${firstResult.title}`);
console.log(` Summary: ${firstResult.summary.substring(0, 100)}...`);
console.log(` URL: ${firstResult.url}\n`);
// Test get note details
console.log(`๐ Testing get note details for ${firstResult.id}...`);
const noteDetail = await sapNotesClient.getNote(firstResult.id, token);
if (noteDetail) {
console.log(`โ
Note details retrieved!`);
console.log(` ID: ${noteDetail.id}`);
console.log(` Title: ${noteDetail.title}`);
console.log(` Content preview: ${noteDetail.content.substring(0, 150)}...`);
} else {
console.log(`โ Could not retrieve note details for ${firstResult.id}`);
}
} else {
console.log('โน๏ธ No results found. Trying a different search...');
// Try a keyword search
const keywordResult = await sapNotesClient.searchNotes('ABAP', token, 3);
console.log(` Keyword search results: ${keywordResult.totalResults}`);
}
// Test health check
console.log('\n๐ฅ Testing health check...');
const healthOk = await sapNotesClient.healthCheck(token);
console.log(` Health check: ${healthOk ? 'โ
OK' : 'โ Failed'}`);
console.log('\n๐ SAP Notes API test completed successfully!');
console.log('\n๐ก Next steps:');
console.log(' 1. The new SAP Notes API is working');
console.log(' 2. Try it in Cursor - the MCP server should now work');
console.log(' 3. Use sap_note_search and sap_note_get tools');
} catch (error) {
console.error('\nโ SAP Notes API test failed:', error.message);
console.error('\n๐ง This might indicate:');
console.error(' - Authentication token expired');
console.error(' - SAP endpoint access issues');
console.error(' - Network connectivity problems');
}