minimal-json-structure-test.jsβ’6.19 kB
#!/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);