#!/usr/bin/env node
/**
* Minimal JSON Structure Test
* Tests the core JSON structure fix with minimal payload to isolate the issue
*/
import { createComposerFormatter } from '../src/tools/format-for-composer.js';
import { createCompositionAPISaver } from '../src/tools/save-composition-api.js';
import { chromium } from 'playwright';
// Minimal test data - just the essential components that caused the 500 error
const minimalTestData = {
metadata: {
topic: "Teste JSON Structure",
subject: "Ciências",
gradeLevel: "8º ano",
duration: 15,
learningObjectives: ["Testar estrutura JSON"]
},
widgets: [
{
type: "head-1",
content: {
category: "TESTE - ESTRUTURA JSON",
author_name: "Professor Teste",
author_office: "Especialista em Testes"
}
},
{
type: "text-1",
content: {
text: "<h2>Teste de Estrutura JSON</h2><p>Este é um teste mínimo para validar a estrutura JSON.</p>"
}
},
{
type: "quiz-1",
content: {
questions: [
{
question: "Este é um teste de estrutura JSON?",
options: ["Sim", "Não"],
correct_option: 0
}
],
max_attempts: 1
}
}
]
};
async function runMinimalTest() {
console.error('🧪 Minimal JSON Structure Test - Isolating Core Fix\n');
// Step 1: Format the minimal composition
console.error('📋 Step 1: Formatting minimal composition');
const formatter = createComposerFormatter();
const formatResult = await formatter.formatForComposer(minimalTestData);
if (!formatResult.success) {
console.error('❌ Format failed:', formatResult.error);
return;
}
const composerJSON = formatResult.data.composerJSON;
console.error('✅ Formatting successful');
// Step 2: Validate JSON structure matches reference
console.error('\n📋 Step 2: Validating JSON structure');
// Check metadata
const metadata = composerJSON.metadata;
console.error('✅ Metadata category:', metadata.category);
console.error('✅ Metadata duration:', metadata.duration);
// Check header widget
const headerWidget = composerJSON.structure.find(w => w.type === 'head-1');
console.error('✅ Header category HTML:', headerWidget.category);
console.error('✅ Header author HTML:', headerWidget.author_name);
console.error('✅ Header office HTML:', headerWidget.author_office);
// Check quiz widget
const quizWidget = composerJSON.structure.find(w => w.type === 'quiz-1');
const firstQuestion = quizWidget.questions[0];
console.error('✅ Quiz question HTML:', firstQuestion.question);
console.error('✅ Quiz uses choices field:', !!firstQuestion.choices);
console.error('✅ Quiz choice HTML:', firstQuestion.choices[0].text);
// Step 3: Size check
const jsonSize = JSON.stringify(composerJSON).length;
console.error('\n📋 Step 3: Size analysis');
console.error('✅ Composition size:', jsonSize, 'bytes');
console.error('✅ Size is minimal:', jsonSize < 5000);
// Step 4: Attempt API save (the real test)
console.error('\n📋 Step 4: Testing API save with minimal payload');
let browser;
try {
// Launch browser
browser = await chromium.launch({
headless: false,
slowMo: 100
});
const context = await browser.newContext({
viewport: { width: 1920, height: 1080 }
});
const page = await context.newPage();
// Set up console logging
page.on('console', msg => {
console.error(`[BROWSER] ${msg.text()}`);
});
console.error('🔐 Authenticating via JWT redirect...');
// Authenticate via JWT redirect
await page.goto('http://localhost:8080', {
waitUntil: 'networkidle',
timeout: 30000
});
await page.waitForURL('**/composer.euconquisto.com/**', {
timeout: 15000
});
await page.waitForLoadState('networkidle');
await page.waitForTimeout(2000);
console.error('✅ Authentication successful');
// Test API save
console.error('💾 Testing API save...');
const apiSaver = createCompositionAPISaver();
const saveResult = await apiSaver.saveCompositionAPI(composerJSON, page);
if (saveResult.success) {
console.error('✅ API SAVE SUCCESSFUL!');
console.error(' Composition UID:', saveResult.data.compositionUid);
console.error(' File size:', saveResult.data.uploadDetails.fileSize, 'bytes');
if (saveResult.optimizationApplied) {
console.error(' Optimization applied:', saveResult.optimizationDetails.reductionPercent + '%');
} else {
console.error(' No optimization needed');
}
console.error('\n🎯 MINIMAL TEST RESULTS:');
console.error(' ✅ JSON structure fix is working');
console.error(' ✅ HTTP 500 errors resolved');
console.error(' ✅ Minimal payload saves successfully');
console.error(' ✅ Ready for full workflow testing');
} else {
console.error('❌ API SAVE FAILED');
console.error(' Error:', saveResult.error);
// Analyze the failure
if (saveResult.error.details?.httpStatus === 500) {
console.error('\n🔍 ANALYSIS:');
console.error(' ❌ HTTP 500 still occurring');
console.error(' ❌ JSON structure fix incomplete');
console.error(' 🔧 Need to investigate further');
} else {
console.error('\n🔍 ANALYSIS:');
console.error(' ℹ️ Different error type - not JSON structure');
console.error(' 🔧 May need different approach');
}
}
} catch (error) {
console.error('❌ Test failed:', error.message);
console.error('\n🔍 ANALYSIS:');
console.error(' ❌ System error occurred');
console.error(' 🔧 Check authentication or browser setup');
} finally {
if (browser) {
await browser.close();
}
}
}
async function main() {
try {
await runMinimalTest();
} catch (error) {
console.error('💥 Test execution failed:', error);
}
}
main().catch(console.error);