Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 168 additions & 0 deletions demo-syntax-validation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
#!/usr/bin/env node

/**
* Demo script for FML Syntax Validation REST API
*
* This script starts a REST API server and demonstrates the new syntax validation endpoint.
* Run with: node demo-syntax-validation.js
*/

const express = require('express');
const { FmlRunner } = require('./packages/fmlrunner/dist/index.js');
const { FmlRunnerApi } = require('./packages/fmlrunner-rest/dist/api.js');

async function runDemo() {
console.log('πŸš€ Starting FML Runner REST API Demo...\n');

// Create FML Runner instance
const fmlRunner = new FmlRunner({
logLevel: 'info',
validateInputOutput: true
});

// Create and start API server
const api = new FmlRunnerApi(fmlRunner);
const app = api.getApp();

const PORT = process.env.PORT || 3000;
const server = app.listen(PORT, () => {
console.log(`βœ… FML Runner REST API started on http://localhost:${PORT}`);
console.log(`πŸ“š API Documentation: http://localhost:${PORT}/api/v1/health`);
console.log(`πŸ” Syntax Validation: POST http://localhost:${PORT}/api/v1/validate-syntax\n`);

// Run demo tests
setTimeout(() => runDemoTests(PORT), 1000);
});

// Graceful shutdown
process.on('SIGINT', () => {
console.log('\nπŸ›‘ Shutting down server...');
server.close(() => {
console.log('βœ… Server stopped');
process.exit(0);
});
});
}

async function runDemoTests(port) {
console.log('πŸ§ͺ Running Demo Tests...\n');

const baseUrl = `http://localhost:${port}`;

const testCases = [
{
name: 'βœ… Valid FML',
fmlContent: `map "http://example.org/StructureMap/patient" = "PatientMap"

group main(source src : Patient, target tgt : Patient) {
src.name -> tgt.name;
src.birthDate -> tgt.birthDate;
}`
},
{
name: '⚠️ Missing Map Declaration',
fmlContent: `group main(source src : Patient, target tgt : Patient) {
src.name -> tgt.name;
}`
},
{
name: '❌ Empty Content',
fmlContent: ' '
},
{
name: '❌ Invalid Syntax',
fmlContent: `map "http://example.org/StructureMap/test" = "TestMap"

group main(source src : Patient, target tgt : Patient {
src.name -> tgt.name;
// Missing closing parenthesis above
}`
}
];

for (const testCase of testCases) {
console.log(`\nπŸ“ ${testCase.name}:`);
console.log(`Input: ${testCase.fmlContent.substring(0, 50).replace(/\n/g, '\\n')}${testCase.fmlContent.length > 50 ? '...' : ''}`);

try {
const response = await fetch(`${baseUrl}/api/v1/validate-syntax`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ fmlContent: testCase.fmlContent })
});

const result = await response.json();

console.log(`Status: ${response.status} ${response.statusText}`);

if (result.issue && result.issue.length > 0) {
result.issue.forEach(issue => {
const icon = issue.severity === 'error' ? '❌' : '⚠️';
console.log(`${icon} ${issue.severity.toUpperCase()}: ${issue.diagnostics}`);
if (issue.location) {
console.log(` πŸ“ Location: ${issue.location[0]}`);
}
});
} else {
console.log('βœ… No issues found');
}

} catch (error) {
console.error(`❌ Error: ${error.message}`);
}
}

console.log('\nπŸŽ‰ Demo completed! The server is still running.');
console.log('πŸ’‘ Try making your own requests to test the syntax validation:');
console.log(`
curl -X POST ${baseUrl}/api/v1/validate-syntax \\
-H "Content-Type: application/json" \\
-d '{"fmlContent": "map \\"test\\" = \\"TestMap\\""}'
`);
console.log('Press Ctrl+C to stop the server.\n');
}

// Simple fetch polyfill for Node.js
if (typeof fetch === 'undefined') {
global.fetch = async (url, options = {}) => {
const https = require('https');
const http = require('http');
const urlLib = require('url');

return new Promise((resolve, reject) => {
const parsedUrl = urlLib.parse(url);
const lib = parsedUrl.protocol === 'https:' ? https : http;

const req = lib.request({
hostname: parsedUrl.hostname,
port: parsedUrl.port,
path: parsedUrl.path,
method: options.method || 'GET',
headers: options.headers || {}
}, (res) => {
let data = '';
res.on('data', chunk => data += chunk);
res.on('end', () => {
resolve({
status: res.statusCode,
statusText: res.statusMessage,
json: () => Promise.resolve(JSON.parse(data))
});
});
});

req.on('error', reject);

if (options.body) {
req.write(options.body);
}

req.end();
});
};
}

// Run the demo
runDemo().catch(console.error);
Loading