Theme Persistence Testing Guide

Step-by-step testing guide for theme persistence and MongoDB integration

๐Ÿงช Theme Persistence Testing Guide

๐ŸŽฏ What Was Fixed

1. Explicit Console Logging โœจ

Added console.log() statements (not just logger) to trace the complete flow:

// SimpleThemeStorageService
console.log(`๐ŸŽฏ [SimpleThemeStorage] setActiveThemeId() called!`);
console.log(`๐Ÿ’พ [SimpleThemeStorage] saveTheme() called!`);
console.log(`โœ… Space document updated successfully!`);

2. Add vs Override Mode โœจ

New functionality to control theme saving:

// Save and override existing theme (default)
await themeStorage.saveTheme(theme, { mode: 'override', setAsActive: true });

// Add as new theme (create copy if ID exists)
await themeStorage.saveTheme(theme, { mode: 'add', setAsActive: true });

3. UI Buttons โœจ


๐Ÿงช Test Steps

Test 1: Save Theme (Override Mode) โœ…

  1. Open Theme Designer: Navigate to /space/chrome-space-1/theme-designer

  2. Click "๐Ÿ’พ Save & Apply (Override)"

  3. Expected Console Logs:

    ๐Ÿ’พ [SimpleThemeStorage] saveTheme() called!
       - Theme: my-theme
       - Theme ID: theme-xxx
       - Mode: override
       - Set as active: true
       - Space ID: 69067c087ebb621cd9ff8c3f
    
    โœ… Theme saved locally: theme-xxx
    ๐Ÿ”„ Syncing theme to MongoDB themes collection...
    โœ… Theme synced to MongoDB: theme-xxx
    
    โšก Setting as active theme...
    ๐ŸŽฏ [SimpleThemeStorage] setActiveThemeId() called!
       - Theme ID: theme-xxx
       - Space ID: 69067c087ebb621cd9ff8c3f
    
    ๐ŸŽฏ Calling spaceApi.updateSpace()...
       - Update payload: { themeSystem: { activeThemeId: "theme-xxx", ... } }
    
    โœ… Space document updated successfully!
       - Theme system: { activeThemeId: "theme-xxx", ... }
    
  4. Check MongoDB:

    • โœ… Theme in themes collection
    • โœ… Space document has themeSystem.activeThemeId
  5. Refresh Page (F5):

    • โœ… Theme persists
    • โœ… Console shows: "โœ… Loaded active theme from space document: theme-xxx"

Test 2: Save as New (Add Mode) โœจ

  1. Edit current theme colors

  2. Click "โž• Save as New"

  3. Expected Console Logs:

    ๐Ÿ’พ [SimpleThemeStorage] saveTheme() called!
       - Mode: add
       - Theme exists, creating new one with ID: theme-xxx-1763741332000
    
    โœ… ADD MODE: Creating new theme with ID: theme-xxx-1763741332000
    โœ… Theme saved locally: theme-xxx-1763741332000
    ๐Ÿ”„ Syncing theme to MongoDB themes collection...
    โœ… Theme synced to MongoDB: theme-xxx-1763741332000
    
    โšก Setting as active theme...
    
  4. Check MongoDB:

    • โœ… TWO themes in themes collection (old + new)
    • โœ… Space document has themeSystem.activeThemeId pointing to new theme

Test 3: Override Existing Theme โœ…

  1. Load existing theme

  2. Edit colors

  3. Click "๐Ÿ’พ Save & Apply (Override)"

  4. Expected Behavior:

    • โœ… Theme is updated (same ID)
    • โœ… MongoDB themes collection: theme updated (not duplicated)
    • โœ… Space document: themeSystem.activeThemeId unchanged

Test 4: Persistence After Refresh โœ…

  1. Save a theme

  2. Refresh page (F5)

  3. Expected Console Logs:

    ๐ŸŒŒ [SimpleThemeStorage] Space changed to: 69067c087ebb621cd9ff8c3f - loading theme from space document
    โœ… Loaded active theme from space document: theme-xxx
    ๐ŸŽฏ ThemeStateManager: Active theme loaded: my-theme
    
  4. Expected Behavior:

    • โœ… Theme is applied automatically
    • โœ… NO "No active theme found in space document"

๐Ÿ” Debugging

Issue: No Logs Appearing

Check:

  1. Open DevTools Console
  2. Make sure "Verbose" or "Info" level is enabled
  3. Look for logs starting with ๐ŸŽฏ, ๐Ÿ’พ, โœ…

Issue: Space Document Not Updating

Check Backend Logs:

# In backend-api terminal
# Look for:
[SpacesController] ๐Ÿ“ฅ Update request received
[SpacesService] ๐ŸŽจ Updating themeSystem: {...}
[SpacesService] โœ… Updated space.themeSystem to: {...}

Check Network Tab:

Issue: Theme Not in MongoDB

Check HTTP Sync Adapter:

// In console
console.log('[HTTP Sync] Initialized adapter for collection: themes');

// Should see:
[HTTP Sync] Auto-sync enabled for themes (interval: 3000ms)

๐Ÿ“Š MongoDB Verification

Query Spaces Collection:

db.spaces.findOne({ _id: ObjectId("69067c087ebb621cd9ff8c3f") })

// Expected output:
{
  "_id": ObjectId("69067c087ebb621cd9ff8c3f"),
  "name": "Chrome Space 1",
  "themeSystem": {  // โœ… SHOULD BE HERE!
    "activeThemeId": "theme-testtheme1-1763734768627",
    "updatedAt": "2025-11-21T11:00:32.867Z"
  }
}

Query Themes Collection:

db.themes.find({ spaceId: "69067c087ebb621cd9ff8c3f" })

// Expected: Array of themes
[
  {
    "_id": "theme-xxx",
    "metadata": { "name": "my-theme" },
    "spaceId": "69067c087ebb621cd9ff8c3f",
    "_syncedAt": "2025-11-21T11:00:33.123Z"
  },
  // ... more themes
]

โœ… Success Criteria


๐Ÿšจ If Still Not Working

1. Restart Backend API

# Terminal
nx serve backend-api

Why? MongoDB schema changed (added themeSystem)

2. Clear Browser Cache

# Chrome DevTools Console
localStorage.clear();
window.location.reload();

3. Check Backend API Logs

Look for errors in the backend terminal:

4. Verify HTTP Sync Adapter

// In console
console.log('[HTTP Sync] Initialized adapter for collection: themes');

If NOT present, check app.config.ts:

provideHttpMongoDBSync({
  collections: [
    { name: 'themes', syncInterval: 3000, batchSize: 5 }
  ]
})

๐Ÿ“ Notes


Status: ๐Ÿงช REQUIRES TESTING
Next: Save a theme and check the console logs!
Expected: Full trace from save โ†’ sync โ†’ space update โ†’ success โœจ