sonicjs/debug-workflow.js
Lane Campbell 46deef39bf feat: implement functional plugin admin system with E2E tests
- Created complete plugin database schema with tables for plugins, hooks, routes, assets, and activity logging
- Implemented PluginService for comprehensive plugin management (install, activate, deactivate, uninstall)
- Built admin routes with proper error handling and permission checks
- Fixed TypeScript compilation errors by adding proper D1Database imports
- Resolved permission system issues by modifying to not query non-existent user_permissions table
- Temporarily bypassed permission checks for admin users to fix access denied errors
- Created comprehensive E2E test suite with 13 tests covering all plugin functionality
- Fixed E2E test selector issues for better reliability
- Integrated plugin admin page with glass morphism UI design
- Added core plugins (Authentication, Media, Workflow) to initial migration

All E2E tests now passing with 100% success rate.

๐Ÿค– Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-06-30 06:27:35 -07:00

324 lines
11 KiB
JavaScript
Executable File

#!/usr/bin/env node
// Debug script for workflow dashboard issues
// This script will help identify why workflow states aren't showing up
const { createClient } = require('@libsql/client');
const path = require('path');
// Configuration
const config = {
// Update these paths according to your setup
databasePath: process.env.DB_PATH || './.wrangler/state/v3/d1/miniflare-D1DatabaseObject/755d5c8bf08a9272238e9b8a4abc2e0aae7d3347653c5decacd3aca91d2a26bf.sqlite',
databaseUrl: process.env.DATABASE_URL || `file:${path.resolve('./.wrangler/state/v3/d1/miniflare-D1DatabaseObject/755d5c8bf08a9272238e9b8a4abc2e0aae7d3347653c5decacd3aca91d2a26bf.sqlite')}`,
};
async function debugWorkflow() {
console.log('๐Ÿ” Starting Workflow Debug Session...\n');
let client;
try {
// Initialize database client
client = createClient({
url: config.databaseUrl,
});
// Test 1: Check if workflow_states table exists and has data
console.log('๐Ÿ“‹ Test 1: Checking workflow_states table...');
try {
const statesResult = await client.execute('SELECT * FROM workflow_states ORDER BY name');
console.log(`โœ… Found ${statesResult.rows.length} workflow states:`);
statesResult.rows.forEach(row => {
console.log(` - ${row.id}: ${row.name} (${row.color}) ${row.is_initial ? '[INITIAL]' : ''} ${row.is_final ? '[FINAL]' : ''}`);
});
} catch (error) {
console.error('โŒ Error querying workflow_states:', error.message);
// Check if table exists
try {
const tableCheck = await client.execute(`
SELECT name FROM sqlite_master
WHERE type='table' AND name='workflow_states'
`);
if (tableCheck.rows.length === 0) {
console.log('โš ๏ธ workflow_states table does not exist!');
console.log(' Run the migration: npm run migrate or apply 005_stage7_workflow_automation.sql');
}
} catch (e) {
console.error('โŒ Error checking table existence:', e.message);
}
}
console.log('\n');
// Test 2: Check workflows table
console.log('๐Ÿ“‹ Test 2: Checking workflows table...');
try {
const workflowsResult = await client.execute(`
SELECT w.*, c.name as collection_name
FROM workflows w
LEFT JOIN collections c ON w.collection_id = c.id
ORDER BY w.name
`);
console.log(`โœ… Found ${workflowsResult.rows.length} workflows:`);
workflowsResult.rows.forEach(row => {
console.log(` - ${row.id}: ${row.name} (Collection: ${row.collection_name || 'Global'}) ${row.is_active ? '[ACTIVE]' : '[INACTIVE]'}`);
});
} catch (error) {
console.error('โŒ Error querying workflows:', error.message);
}
console.log('\n');
// Test 3: Check content with workflow states
console.log('๐Ÿ“‹ Test 3: Checking content workflow status...');
try {
const contentResult = await client.execute(`
SELECT
c.id,
c.title,
c.workflow_state_id,
cws.current_state_id,
ws.name as state_name,
ws.color as state_color
FROM content c
LEFT JOIN content_workflow_status cws ON c.id = cws.content_id
LEFT JOIN workflow_states ws ON (cws.current_state_id = ws.id OR c.workflow_state_id = ws.id)
ORDER BY c.created_at DESC
LIMIT 10
`);
console.log(`โœ… Found ${contentResult.rows.length} content items:`);
contentResult.rows.forEach(row => {
console.log(` - ${row.title}: workflow_state_id="${row.workflow_state_id}", current_state_id="${row.current_state_id}", resolved_state="${row.state_name}"`);
});
} catch (error) {
console.error('โŒ Error querying content workflow status:', error.message);
}
console.log('\n');
// Test 4: Simulate WorkflowEngine.getWorkflowStates()
console.log('๐Ÿ“‹ Test 4: Simulating WorkflowEngine.getWorkflowStates()...');
try {
const statesQuery = `
SELECT * FROM workflow_states
ORDER BY is_initial DESC, name ASC
`;
const statesResult = await client.execute(statesQuery);
console.log(`โœ… Query returned ${statesResult.rows.length} states:`);
console.log('Raw query result:', JSON.stringify(statesResult.rows, null, 2));
} catch (error) {
console.error('โŒ Error simulating getWorkflowStates():', error.message);
}
console.log('\n');
// Test 5: Simulate WorkflowEngine.getContentByState() for each state
console.log('๐Ÿ“‹ Test 5: Simulating WorkflowEngine.getContentByState() for each state...');
try {
const statesResult = await client.execute('SELECT id, name FROM workflow_states');
for (const state of statesResult.rows) {
const contentQuery = `
SELECT
c.*,
cws.assigned_to,
cws.due_date,
ws.name as state_name,
ws.color as state_color,
col.name as collection_name,
u.username as assigned_to_name
FROM content c
JOIN content_workflow_status cws ON c.id = cws.content_id
JOIN workflow_states ws ON cws.current_state_id = ws.id
JOIN collections col ON c.collection_id = col.id
LEFT JOIN users u ON cws.assigned_to = u.id
WHERE cws.current_state_id = ?
ORDER BY c.updated_at DESC
LIMIT 10
`;
const contentResult = await client.execute(contentQuery, [state.id]);
console.log(` - ${state.name} (${state.id}): ${contentResult.rows.length} content items`);
if (contentResult.rows.length > 0) {
contentResult.rows.forEach(content => {
console.log(` * ${content.title} (${content.collection_name})`);
});
}
}
} catch (error) {
console.error('โŒ Error simulating getContentByState():', error.message);
}
console.log('\n');
// Test 6: Check database connection and basic operations
console.log('๐Ÿ“‹ Test 6: Database connection and basic operations...');
try {
const dbVersion = await client.execute('SELECT sqlite_version()');
console.log(`โœ… SQLite version: ${dbVersion.rows[0]['sqlite_version()']}`);
const tableCount = await client.execute(`
SELECT COUNT(*) as count FROM sqlite_master WHERE type='table'
`);
console.log(`โœ… Total tables in database: ${tableCount.rows[0].count}`);
} catch (error) {
console.error('โŒ Database connection error:', error.message);
}
console.log('\n');
// Test 7: Check if migration was applied correctly
console.log('๐Ÿ“‹ Test 7: Checking migration application...');
try {
const expectedTables = [
'workflow_states',
'workflows',
'workflow_transitions',
'content_workflow_status',
'workflow_history'
];
for (const tableName of expectedTables) {
const tableCheck = await client.execute(`
SELECT name FROM sqlite_master
WHERE type='table' AND name='${tableName}'
`);
if (tableCheck.rows.length > 0) {
console.log(`โœ… Table ${tableName} exists`);
} else {
console.log(`โŒ Table ${tableName} is missing!`);
}
}
} catch (error) {
console.error('โŒ Error checking migration:', error.message);
}
console.log('\n');
// Test 8: Check for any errors in the workflow route logic
console.log('๐Ÿ“‹ Test 8: Checking workflow route logic simulation...');
try {
// Simulate the exact query from the workflow dashboard route
const states = await client.execute(`
SELECT * FROM workflow_states
ORDER BY is_initial DESC, name ASC
`);
console.log(`โœ… States query returned ${states.rows.length} results`);
const stateData = [];
for (const state of states.rows) {
const content = await client.execute(`
SELECT
c.*,
cws.assigned_to,
cws.due_date,
ws.name as state_name,
ws.color as state_color,
col.name as collection_name,
u.username as assigned_to_name
FROM content c
JOIN content_workflow_status cws ON c.id = cws.content_id
JOIN workflow_states ws ON cws.current_state_id = ws.id
JOIN collections col ON c.collection_id = col.id
LEFT JOIN users u ON cws.assigned_to = u.id
WHERE cws.current_state_id = ?
ORDER BY c.updated_at DESC
LIMIT 10
`, [state.id]);
stateData.push({
...state,
count: content.rows.length,
content: content.rows.slice(0, 5)
});
}
console.log('โœ… Route simulation completed successfully');
console.log('Final state data structure:', JSON.stringify(stateData, null, 2));
} catch (error) {
console.error('โŒ Error in route simulation:', error.message);
}
} catch (error) {
console.error('โŒ Critical error:', error.message);
} finally {
if (client) {
await client.close();
}
}
console.log('\n๐Ÿ Debug session completed!');
console.log('\n๐Ÿ’ก Recommendations:');
console.log('1. If workflow_states table is empty, run the migration again');
console.log('2. If content_workflow_status table is empty, initialize workflow for existing content');
console.log('3. Check browser console for JavaScript errors');
console.log('4. Verify the database path and permissions');
console.log('5. Test the /admin/workflow/dashboard route directly');
}
// Additional helper function to initialize missing workflow data
async function initializeWorkflowData() {
console.log('\n๐Ÿ”ง Initializing missing workflow data...');
let client;
try {
client = createClient({
url: config.databaseUrl,
});
// Check if content exists without workflow status
const contentWithoutWorkflow = await client.execute(`
SELECT c.id, c.title, c.collection_id
FROM content c
LEFT JOIN content_workflow_status cws ON c.id = cws.content_id
WHERE cws.id IS NULL
LIMIT 10
`);
if (contentWithoutWorkflow.rows.length > 0) {
console.log(`Found ${contentWithoutWorkflow.rows.length} content items without workflow status`);
// Get the default workflow
const defaultWorkflow = await client.execute(`
SELECT id FROM workflows WHERE is_active = 1 LIMIT 1
`);
if (defaultWorkflow.rows.length > 0) {
const workflowId = defaultWorkflow.rows[0].id;
// Initialize workflow status for content without it
for (const content of contentWithoutWorkflow.rows) {
await client.execute(`
INSERT INTO content_workflow_status (content_id, workflow_id, current_state_id)
VALUES (?, ?, 'draft')
`, [content.id, workflowId]);
console.log(`โœ… Initialized workflow for: ${content.title}`);
}
} else {
console.log('โŒ No active workflow found!');
}
} else {
console.log('โœ… All content has workflow status');
}
} catch (error) {
console.error('โŒ Error initializing workflow data:', error.message);
} finally {
if (client) {
await client.close();
}
}
}
// Command line interface
const args = process.argv.slice(2);
if (args.includes('--init')) {
initializeWorkflowData();
} else {
debugWorkflow();
}