-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-globalmessagebus.html
More file actions
303 lines (245 loc) · 12.3 KB
/
test-globalmessagebus.html
File metadata and controls
303 lines (245 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>🧪 GlobalMessageBus テストにゃ</title>
<style>
body {
font-family: 'Courier New', monospace;
background: #1a1a1a;
color: #00ff88;
padding: 20px;
margin: 0;
}
.test-area {
background: rgba(0, 255, 136, 0.1);
border: 1px solid #00ff88;
border-radius: 8px;
padding: 20px;
margin: 10px 0;
}
.log-area {
background: rgba(0, 0, 0, 0.5);
border: 1px solid #4a90e2;
border-radius: 6px;
padding: 15px;
height: 300px;
overflow-y: auto;
font-size: 12px;
white-space: pre-wrap;
margin: 10px 0;
}
button {
background: #4a90e2;
color: white;
border: none;
padding: 10px 20px;
border-radius: 4px;
cursor: pointer;
margin: 5px;
font-family: inherit;
}
button:hover {
background: #357abd;
}
.core-info {
background: rgba(74, 144, 226, 0.1);
border: 1px solid #4a90e2;
border-radius: 6px;
padding: 10px;
margin: 5px 0;
font-size: 11px;
}
.error {
color: #ff6b6b;
}
.success {
color: #00ff88;
}
.warning {
color: #ffaa00;
}
</style>
</head>
<body>
<h1>🧪 nyacore GlobalMessageBus テスト</h1>
<p>ProjectCore と NyaCoreUI 間のメッセージ通信をテストするにゃ</p>
<div class="test-area">
<h3>🎯 テスト環境</h3>
<div id="coreInfo">読み込み中...</div>
<h3>🚀 テスト実行</h3>
<button onclick="testGlobalMessageBus()">📡 GlobalMessageBus テスト</button>
<button onclick="testCoreRegistration()">🔌 コア登録テスト</button>
<button onclick="testDirectSubscription()">🔔 直接購読テスト</button>
<button onclick="testProjectCoreMessage()">🎮 ProjectCore メッセージテスト</button>
<button onclick="clearLog()">🧹 ログクリア</button>
</div>
<div class="test-area">
<h3>📋 テストログ</h3>
<div id="testLog" class="log-area">テスト開始を待機中...\n</div>
</div>
<script type="module">
import { globalMessageBus } from '/src/core/core-communication.js'
import { NyaCore } from '/src/core/nyacore.js'
import { Message } from '/src/messaging/message.js'
// テスト用のグローバル変数
window.globalMessageBus = globalMessageBus
window.NyaCore = NyaCore
window.Message = Message
window.testCoreA = null
window.testCoreB = null
let logElement = document.getElementById('testLog')
function log(message, type = 'info') {
const timestamp = new Date().toLocaleTimeString()
const prefix = type === 'error' ? '❌' : type === 'success' ? '✅' : type === 'warning' ? '⚠️' : 'ℹ️'
const className = type === 'error' ? 'error' : type === 'success' ? 'success' : type === 'warning' ? 'warning' : ''
const logLine = `[${timestamp}] ${prefix} ${message}\n`
logElement.innerHTML += `<span class="${className}">${logLine}</span>`
logElement.scrollTop = logElement.scrollHeight
console.log(`[GlobalMessageBus Test] ${message}`)
}
window.log = log
// 初期化
async function initialize() {
try {
log('🚀 テスト環境初期化開始...')
// テスト用コア作成
window.testCoreA = new NyaCore(null, { debug: true })
window.testCoreB = new NyaCore(null, { debug: true })
await window.testCoreA.initPromise
await window.testCoreB.initPromise
log(`✅ テストコアA作成完了: ID=${window.testCoreA.coreId}`, 'success')
log(`✅ テストコアB作成完了: ID=${window.testCoreB.coreId}`, 'success')
// コア情報表示
updateCoreInfo()
log('🎉 テスト環境初期化完了!', 'success')
} catch (error) {
log(`❌ 初期化失敗: ${error.message}`, 'error')
console.error('Initialization error:', error)
}
}
function updateCoreInfo() {
const coreInfo = document.getElementById('coreInfo')
coreInfo.innerHTML = `
<div class="core-info">
<strong>🌐 GlobalMessageBus:</strong> 登録コア数=${globalMessageBus.getCoreCount()}
</div>
<div class="core-info">
<strong>🎮 TestCoreA:</strong> ID=${window.testCoreA?.coreId || 'なし'}
</div>
<div class="core-info">
<strong>🎮 TestCoreB:</strong> ID=${window.testCoreB?.coreId || 'なし'}
</div>
<div class="core-info">
<strong>📊 登録コア一覧:</strong> ${globalMessageBus.getRegisteredCores().join(', ')}
</div>
`
}
// テスト関数をグローバルに露出
window.testGlobalMessageBus = async function() {
log('📡 GlobalMessageBus 基本テスト開始...')
try {
// 基本情報確認
const coreCount = globalMessageBus.getCoreCount()
const registeredCores = globalMessageBus.getRegisteredCores()
log(`📊 現在の登録コア数: ${coreCount}`)
log(`📋 登録コア一覧: [${registeredCores.join(', ')}]`)
// 通信履歴確認
const history = globalMessageBus.getMessageHistory(10)
log(`📜 メッセージ履歴: ${history.length}件`)
// 統計確認
const stats = globalMessageBus.getCommunicationStats()
log(`📈 通信統計: 総メッセージ${stats.totalMessages}件, 登録コア${stats.registeredCores}個`)
updateCoreInfo()
log('✅ GlobalMessageBus 基本テスト完了', 'success')
} catch (error) {
log(`❌ GlobalMessageBus テスト失敗: ${error.message}`, 'error')
}
}
window.testCoreRegistration = async function() {
log('🔌 コア登録テスト開始...')
try {
// コアA登録
log(`🔌 CoreA登録試行: ${window.testCoreA.coreId}`)
globalMessageBus.registerCore(window.testCoreA.coreId, window.testCoreA)
// コアB登録
log(`🔌 CoreB登録試行: ${window.testCoreB.coreId}`)
globalMessageBus.registerCore(window.testCoreB.coreId, window.testCoreB)
// 登録確認
const coreCount = globalMessageBus.getCoreCount()
const registeredCores = globalMessageBus.getRegisteredCores()
log(`✅ 登録後コア数: ${coreCount}`)
log(`✅ 登録コア一覧: [${registeredCores.join(', ')}]`)
updateCoreInfo()
log('✅ コア登録テスト完了', 'success')
} catch (error) {
log(`❌ コア登録テスト失敗: ${error.message}`, 'error')
}
}
window.testDirectSubscription = async function() {
log('🔔 直接購読テスト開始...')
try {
// 直接購読ハンドラー設定
const unsubscribe = globalMessageBus.subscribe('test.message', (message) => {
log(`🔔 直接購読で受信: ${message.type}`, 'success')
log(`📝 メッセージ構造: ${JSON.stringify(message, null, 2)}`)
log(`📝 メッセージ.data: ${JSON.stringify(message.data)}`)
log(`📝 全プロパティ: ${Object.keys(message).join(', ')}`)
})
// テストメッセージ送信
const testMessage = Message.notice('test.message', {
sender: 'direct-test',
content: 'にゃーテストメッセージ',
timestamp: Date.now()
})
log(`📦 送信前メッセージ確認: ${JSON.stringify(testMessage, null, 2)}`)
log('📤 テストメッセージ送信中...')
const result = await globalMessageBus.broadcast(testMessage)
log(`📊 送信結果: 対象${result.totalCores}コア, 配信${result.delivered}件, 失敗${result.failed}件, 直接購読${result.directSubscribers}件`)
// 購読解除
unsubscribe()
log('🔕 購読解除完了')
log('✅ 直接購読テスト完了', 'success')
} catch (error) {
log(`❌ 直接購読テスト失敗: ${error.message}`, 'error')
}
}
window.testProjectCoreMessage = async function() {
log('🎮 ProjectCore メッセージテスト開始...')
try {
// CoreB にメッセージハンドラー設定
await window.testCoreB.subscribe('ui.stats.update', (message) => {
log(`🎯 CoreB で ui.stats.update 受信!`, 'success')
log(`📝 メッセージ構造: ${JSON.stringify(message, null, 2)}`)
log(`📝 メッセージ.data: ${JSON.stringify(message.data)}`)
log(`📝 全プロパティ: ${Object.keys(message).join(', ')}`)
})
// CoreA から ui.stats.update メッセージ送信
const statsMessage = Message.notice('ui.stats.update', {
pluginCount: 5,
connectionCount: 3,
action: 'plugin.added',
timestamp: Date.now()
})
log(`📦 送信前statsMessage確認: ${JSON.stringify(statsMessage, null, 2)}`)
log('📤 CoreA → ui.stats.update メッセージ送信...')
// 方法1: GlobalMessageBus経由
const broadcastResult = await globalMessageBus.broadcast(statsMessage, window.testCoreA.coreId)
log(`📊 Broadcast結果: 対象${broadcastResult.totalCores}コア, 配信${broadcastResult.delivered}件`)
// 方法2: CoreA のpublish経由
log('📤 CoreA.publish 経由でも送信...')
await window.testCoreA.publish(statsMessage)
log('✅ ProjectCore メッセージテスト完了', 'success')
} catch (error) {
log(`❌ ProjectCore メッセージテスト失敗: ${error.message}`, 'error')
}
}
window.clearLog = function() {
logElement.innerHTML = 'ログクリア完了...\n'
}
// 初期化実行
initialize()
</script>
</body>
</html>