| @@ -1,13 +1,12 @@ | ||
| 1 | 1 | jQuery(document).ready(function($) { |
| 2 | - // Manual test button click | |
| 3 | 2 | $('#mxchat-test-streaming-btn').on('click', function() { |
| 4 | 3 | const $button = $(this); |
| 5 | 4 | const $result = $('#mxchat-test-streaming-result'); |
| 6 | - | |
| 5 | + | |
| 7 | 6 | $button.prop('disabled', true).text('Testing...'); |
| 8 | 7 | $result.text('Testing streaming environment...'); |
| 9 | - | |
| 8 | + | |
| 10 | 9 | // Test both backend capability AND frontend environment |
| 11 | 10 | testCompleteStreamingEnvironment() |
| 12 | 11 | .then(result => { |
| 13 | 12 | if (result.success) { |
| @@ -19,70 +18,12 @@ | ||
| 19 | 18 | .catch(error => { |
| 20 | 19 | $result.css('color', 'red').html('❌ Test failed: ' + error.message); |
| 21 | 20 | }) |
| 22 | 21 | .finally(() => { |
| 23 | - $button.prop('disabled', false).html('<span class="dashicons dashicons-admin-tools" style="vertical-align: middle; margin-right: 5px;"></span>Test Streaming Compatibility'); | |
| 22 | + $button.prop('disabled', false).text('Test Streaming Compatibility'); | |
| 24 | 23 | }); |
| 25 | 24 | }); |
| 26 | - | |
| 27 | - // Intercept the streaming toggle BEFORE autosave fires | |
| 28 | - // We use a capturing event handler to run first | |
| 29 | - const streamingToggle = document.getElementById('enable_streaming_toggle'); | |
| 30 | - if (streamingToggle) { | |
| 31 | - streamingToggle.addEventListener('change', function(e) { | |
| 32 | - if (this.checked) { | |
| 33 | - // Prevent the default autosave from firing immediately | |
| 34 | - e.stopImmediatePropagation(); | |
| 35 | - | |
| 36 | - const $toggle = $(this); | |
| 37 | - const $result = $('#mxchat-test-streaming-result'); | |
| 38 | - const $button = $('#mxchat-test-streaming-btn'); | |
| 39 | - | |
| 40 | - // User is enabling streaming - run automatic compatibility test first | |
| 41 | - $button.prop('disabled', true); | |
| 42 | - $toggle.prop('disabled', true); | |
| 43 | - $result.css('color', '#666').html('🔄 Testing streaming compatibility before enabling...'); | |
| 44 | - | |
| 45 | - testCompleteStreamingEnvironment() | |
| 46 | - .then(result => { | |
| 47 | - if (result.success) { | |
| 48 | - // Test passed - now save the setting | |
| 49 | - saveStreamingSetting('on'); | |
| 50 | - $result.css('color', 'green').html('✅ Streaming enabled - compatibility verified!'); | |
| 51 | - } else { | |
| 52 | - // Streaming failed - turn it back off (don't save 'on') | |
| 53 | - $toggle.prop('checked', false); | |
| 54 | - $result.css('color', 'red').html(result.message); | |
| 55 | - } | |
| 56 | - }) | |
| 57 | - .catch(error => { | |
| 58 | - // Error during test - turn it back off | |
| 59 | - $toggle.prop('checked', false); | |
| 60 | - $result.css('color', 'red').html('❌ Streaming test failed: ' + error.message + '<br>Streaming has been disabled.'); | |
| 61 | - }) | |
| 62 | - .finally(() => { | |
| 63 | - $button.prop('disabled', false); | |
| 64 | - $toggle.prop('disabled', false); | |
| 65 | - }); | |
| 66 | - } | |
| 67 | - // If turning OFF, let the normal autosave handle it | |
| 68 | - }, true); // 'true' for capturing phase - runs before jQuery handlers | |
| 69 | - } | |
| 70 | - | |
| 71 | - // Helper function to save the streaming setting via AJAX | |
| 72 | - function saveStreamingSetting(value) { | |
| 73 | - $.ajax({ | |
| 74 | - url: mxchatTestStreamingAjax.ajax_url, | |
| 75 | - type: 'POST', | |
| 76 | - data: { | |
| 77 | - action: 'mxchat_save_setting', | |
| 78 | - name: 'enable_streaming_toggle', | |
| 79 | - value: value, | |
| 80 | - _ajax_nonce: mxchatTestStreamingAjax.settings_nonce | |
| 81 | - } | |
| 82 | - }); | |
| 83 | - } | |
| 84 | - | |
| 25 | + | |
| 85 | 26 | function testCompleteStreamingEnvironment() { |
| 86 | 27 | return new Promise((resolve, reject) => { |
| 87 | 28 | let results = { |
| 88 | 29 | backendSupported: false, |
| @@ -110,9 +51,8 @@ | ||
| 110 | 51 | formData.append('action', 'mxchat_stream_chat'); |
| 111 | 52 | formData.append('message', 'Say "test 1", then "test 2", then "test 3" - each on a separate line.'); |
| 112 | 53 | formData.append('session_id', 'streaming_test_' + Date.now()); |
| 113 | 54 | formData.append('nonce', mxchatTestStreamingAjax.nonce); |
| 114 | - formData.append('force_streaming_test', '1'); // Force streaming mode for testing | |
| 115 | 55 | |
| 116 | 56 | fetch(mxchatTestStreamingAjax.ajax_url, { |
| 117 | 57 | method: 'POST', |
| 118 | 58 | body: formData, |