File size: 22,548 Bytes
d739b1b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
#!/usr/bin/env node
/**
 * @file scripts/verify-browser-e2e.mjs
 * @description Google Chrome Headless CDP๋ฅผ ํ™œ์šฉํ•œ 5๋‹จ๊ณ„ ๋Œ€ํ™”ํ˜• ํŽ˜๋ฅด์†Œ๋‚˜ ํ•ฉ์„ฑ(Persona Synthesis) E2E ๊ฒ€์ฆ๊ธฐ.
 */

import { spawn } from 'node:child_process';
import path from 'node:path';
import { fileURLToPath } from 'node:url';

const rootDir = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
const htmlPath = 'file://' + path.join(rootDir, 'artifacts/bong-app.html');
const chromePath = '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome';

console.log('๐Ÿงช [Browser E2E] ์‹ค์ œ Google Chrome ๋ธŒ๋ผ์šฐ์ € ๊ธฐ๋™ ๋ฐ 5๋‹จ๊ณ„ ํŽ˜๋ฅด์†Œ๋‚˜ ํ•ฉ์„ฑ ์‹œ๋‚˜๋ฆฌ์˜ค ๊ฒ€์ฆ ์‹œ์ž‘...');

const chromeProc = spawn(chromePath, [
  '--headless=new',
  '--remote-debugging-port=9222',
  '--disable-gpu',
  '--no-first-run',
  '--no-default-browser-check',
  htmlPath
]);

await new Promise(res => setTimeout(res, 1200));

let hasErrors = false;

try {
  const listRes = await fetch('http://127.0.0.1:9222/json/list');
  const targets = await listRes.json();
  const pageTarget = targets.find(t => t.type === 'page');
  if (!pageTarget || !pageTarget.webSocketDebuggerUrl) {
    throw new Error('CDP Debugger WebSocket ์—”๋“œํฌ์ธํŠธ๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.');
  }

  const ws = new WebSocket(pageTarget.webSocketDebuggerUrl);
  await new Promise((res, rej) => { ws.onopen = res; ws.onerror = rej; });

  let msgId = 1;
  const pendingRequests = new Map();

  ws.onmessage = (event) => {
    const data = JSON.parse(event.data);
    if (data.method === 'Runtime.exceptionThrown') {
      console.error('๐Ÿ’ฅ [Browser Error] ์˜ˆ์™ธ ๋ฐœ์ƒ:', data.params.exceptionDetails);
      hasErrors = true;
    }
    if (data.id && pendingRequests.has(data.id)) {
      const resolver = pendingRequests.get(data.id);
      pendingRequests.delete(data.id);
      resolver(data.result);
    }
  };

  function sendCommand(method, params = {}) {
    return new Promise((resolve) => {
      const id = msgId++;
      pendingRequests.set(id, resolve);
      ws.send(JSON.stringify({ id, method, params }));
    });
  }

  await sendCommand('Runtime.enable');
  await sendCommand('Page.enable');

  async function evalCode(expression) {
    const res = await sendCommand('Runtime.evaluate', { expression, returnByValue: true, awaitPromise: true });
    return res.result ? res.result.value : undefined;
  }

  // 1. ์ดˆ๊ธฐ ์…‹์—… ๋ชจ๋‹ฌ 3๋‹จ๊ณ„ ํ†ต๊ณผ ๋ฐ [์ฒซ ์ˆจ ๋ถˆ์–ด๋„ฃ๊ณ  ๋ˆˆ๋œจ๊ธฐ] ๊ฒ€์ฆ
  const initial = await evalCode(`({
    overlayActive: document.getElementById('genesis-modal-overlay')?.classList.contains('active'),
    step1Active: document.getElementById('genesis-step-1')?.classList.contains('active')
  })`);
  if (!initial?.overlayActive || !initial?.step1Active) {
    throw new Error('์ดˆ๊ธฐ ์ƒํƒœ ๊ฒ€์ฆ ์‹คํŒจ: ์…‹์—… ๋ชจ๋‹ฌ ๋˜๋Š” Step 1์ด ํ™œ์„ฑํ™”๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค: ' + JSON.stringify(initial));
  }
  console.log('  โœ… 0-1๋‹จ๊ณ„: ํƒ„์ƒ์˜ ์ˆœ๊ฐ„(The Genesis) ๋งˆ๋ฒ•์‚ฌ ํ™œ์„ฑํ™” ๊ฒ€์ฆ ์„ฑ๊ณต');

  // Step 1 โž” Step 2
  await evalCode(`document.getElementById('btn-genesis-step1-next')?.click()`);
  await new Promise(r => setTimeout(r, 150));

  // Step 2 โž” Step 3
  await evalCode(`document.getElementById('btn-genesis-step2-next')?.click()`);
  await new Promise(r => setTimeout(r, 150));

  // Step 3 ๊ฒ€์ฆ: Mories ์—ฐ๊ฒฐ ํ™•์ธ ๋ฐ BADA WebSocket ํ™•์ธ ๋ฒ„ํŠผ ํƒ‘์žฌ ๋ฐ ๋™์ž‘ ๊ฒ€์ฆ
  const step3Buttons = await evalCode(`({
    hasMoriesBtn: !!document.getElementById('btn-genesis-test-mories'),
    hasWsBtn: !!document.getElementById('btn-genesis-test-ws'),
    hasMoriesFeedback: !!document.getElementById('genesis-mories-feedback'),
    hasWsFeedback: !!document.getElementById('genesis-ws-feedback')
  })`);
  if (!step3Buttons?.hasMoriesBtn || !step3Buttons?.hasWsBtn) {
    throw new Error('Step 3 ํ…Œ์ŠคํŠธ ๋ฒ„ํŠผ ๋ˆ„๋ฝ: ' + JSON.stringify(step3Buttons));
  }

  // Mories ์—ฐ๊ฒฐ ํ™•์ธ ๋ฒ„ํŠผ ํด๋ฆญ ํ…Œ์ŠคํŠธ
  await evalCode(`document.getElementById('btn-genesis-test-mories')?.click()`);
  await new Promise(r => setTimeout(r, 100));
  const moriesFeedbackCheck = await evalCode(`({
    display: document.getElementById('genesis-mories-feedback')?.style.display,
    text: document.getElementById('genesis-mories-feedback')?.innerText || ''
  })`);
  if (moriesFeedbackCheck?.display === 'none' || !moriesFeedbackCheck?.text) {
    throw new Error('Mories ์—ฐ๊ฒฐ ํ™•์ธ ํ”ผ๋“œ๋ฐฑ ๋ฏธํ‘œ์‹œ: ' + JSON.stringify(moriesFeedbackCheck));
  }
  console.log('  โœ… 0-1.5๋‹จ๊ณ„: Step 3 Mories ๋ฐ BADA WebSocket ์ง„๋‹จ ๋ฒ„ํŠผ ํƒ‘์žฌ ๋ฐ ์‹œ๊ฐ ํ”ผ๋“œ๋ฐฑ ๊ฒ€์ฆ ์„ฑ๊ณต');

  // [์ฒซ ์ˆจ ๋ถˆ์–ด๋„ฃ๊ณ  ๋ˆˆ๋œจ๊ธฐ] ํด๋ฆญ
  await evalCode(`document.getElementById('btn-genesis-launch')?.click()`);
  await new Promise(r => setTimeout(r, 600));

  const awakeningCheck = await evalCode(`({
    overlayActive: document.getElementById('genesis-modal-overlay')?.classList.contains('active'),
    dialogueText: document.getElementById('dialogue-bubble')?.innerText || '',
    isOnboarding: window.isOnboardingActive?.() || false,
    step: window.getOnboardingState?.()?.step || ''
  })`);

  if (awakeningCheck?.overlayActive) {
    throw new Error('๋ˆˆ๋œจ๊ธฐ ํ›„ ๋ชจ๋‹ฌ ๋‹ซ๊ธฐ ์‹คํŒจ: ' + JSON.stringify(awakeningCheck));
  }
  if (!awakeningCheck?.dialogueText.includes('์ฒซ ์ˆจ') && !awakeningCheck?.dialogueText.includes('๋‹น์‹ ์„ ๋ญ๋ผ๊ณ  ๋ถ€๋ฅด๋ฉด')) {
    throw new Error('์ฒซ ์ˆจ ๋ฐœํ™” ํ…์ŠคํŠธ ๋ถˆ์ผ์น˜: ' + JSON.stringify(awakeningCheck));
  }
  if (awakeningCheck?.step !== 'ask_user_name') {
    throw new Error('์˜จ๋ณด๋”ฉ Step 1 ์ƒํƒœ ์ง„์ž… ์‹คํŒจ: ' + JSON.stringify(awakeningCheck));
  }
  console.log('  โœ… 0-2๋‹จ๊ณ„: [์ฒซ ์ˆจ ๋ถˆ์–ด๋„ฃ๊ณ  ๋ˆˆ๋œจ๊ธฐ] ์‹œ๋„ค๋งˆํ‹ฑ ์•ก์…˜ ๋ฐ Step 1("ask_user_name") ๋ฐœํ™” ํ™•์ธ');

  // 2. Step 1: "๊ธฐ๋ด‰์ด์•ผ" ํ…์ŠคํŠธ ์ž…๋ ฅ โž” Step 2 (๊ด€๊ณ„ ์Šคํƒ€์ผ ์งˆ๋ฌธ) ์ „ํ™˜ ๊ฒ€์ฆ
  await evalCode(`
    (() => {
      const input = document.getElementById('user-text-input');
      if (input) input.value = '๊ธฐ๋ด‰์ด์•ผ';
      document.getElementById('btn-send')?.click();
    })()
  `);
  await new Promise(r => setTimeout(r, 400));

  const step1Result = await evalCode(`({
    dialogueText: document.getElementById('dialogue-bubble')?.innerText || '',
    step: window.getOnboardingState?.()?.step || '',
    userName: window.getOnboardingState?.()?.profile?.userName || ''
  })`);

  if (!step1Result?.userName.includes('๊ธฐ๋ด‰')) {
    throw new Error('Step 1 ์‚ฌ์šฉ์ž ์ด๋ฆ„ ์ถ”์ถœ ์‹คํŒจ: ' + JSON.stringify(step1Result));
  }
  if (step1Result?.step !== 'ask_relationship_style') {
    throw new Error('Step 1 โž” Step 2 ์ƒํƒœ ์ „์ด ์‹คํŒจ: ' + JSON.stringify(step1Result));
  }
  if (!step1Result?.dialogueText.includes('๊ธฐ๋ด‰') || !step1Result?.dialogueText.includes('์–ด๋–ค ์‚ฌ์ด')) {
    throw new Error('Step 2 ์งˆ๋ฌธ ๋ฐœํ™” ๋ถˆ์ผ์น˜: ' + JSON.stringify(step1Result));
  }
  console.log(`  โœ… 1๋‹จ๊ณ„ (ask_user_name): ์‚ฌ์šฉ์ž ์ด๋ฆ„ '${step1Result.userName}' ์ถ”์ถœ ๋ฐ Step 2(๊ด€๊ณ„ ์Šคํƒ€์ผ ์งˆ๋ฌธ) ์ˆ˜์‹  ์„ฑ๊ณต`);

  // 3. Step 2: "์นœ๊ทผํ•œ ์นœ๊ตฌ" ํ…์ŠคํŠธ ์ž…๋ ฅ โž” Step 3 (๋กœ๋ด‡ ์ด๋ฆ„ ์ž‘๋ช… ์งˆ๋ฌธ) ์ „ํ™˜ ๊ฒ€์ฆ
  await evalCode(`
    (() => {
      const input = document.getElementById('user-text-input');
      if (input) input.value = '์นœ๊ทผํ•œ ์นœ๊ตฌ';
      document.getElementById('btn-send')?.click();
    })()
  `);
  await new Promise(r => setTimeout(r, 400));

  const step2Result = await evalCode(`({
    dialogueText: document.getElementById('dialogue-bubble')?.innerText || '',
    step: window.getOnboardingState?.()?.step || '',
    relStyle: window.getOnboardingState?.()?.profile?.relationshipStyle || ''
  })`);

  if (step2Result?.step !== 'decide_robot_name') {
    throw new Error('Step 2 โž” Step 3 ์ƒํƒœ ์ „์ด ์‹คํŒจ: ' + JSON.stringify(step2Result));
  }
  if (!step2Result?.dialogueText.includes('์Šค์Šค๋กœ๋ฅผ ๋ญ๋ผ๊ณ  ๋ถ€๋ฅด๋ฉด') && !step2Result?.dialogueText.includes('๋ฃจ๋‚˜')) {
    throw new Error('Step 3 ์งˆ๋ฌธ ๋ฐœํ™” ๋ถˆ์ผ์น˜: ' + JSON.stringify(step2Result));
  }
  console.log(`  โœ… 2๋‹จ๊ณ„ (ask_relationship_style): ๊ด€๊ณ„ ์Šคํƒ€์ผ '${step2Result.relStyle}' ์ €์žฅ ๋ฐ Step 3(๋กœ๋ด‡ ์ด๋ฆ„ ์ž‘๋ช… ์งˆ๋ฌธ) ์ˆ˜์‹  ์„ฑ๊ณต`);

  // 4. Step 3: "๋ฃจ๋‚˜๋กœ ํ•˜์ž" ํ…์ŠคํŠธ ์ž…๋ ฅ โž” ์ƒ๋‹จ ์—์ด์ „ํŠธ ๋ฑƒ์ง€ ์‹ค์‹œ๊ฐ„ ๊ฐฑ์‹  ๋ฐ Step 4 ์ „ํ™˜ ๊ฒ€์ฆ
  await evalCode(`
    (() => {
      const input = document.getElementById('user-text-input');
      if (input) input.value = '๋ฃจ๋‚˜๋กœ ํ•˜์ž';
      document.getElementById('btn-send')?.click();
    })()
  `);
  await new Promise(r => setTimeout(r, 400));

  const step3Result = await evalCode(`({
    dialogueText: document.getElementById('dialogue-bubble')?.innerText || '',
    step: window.getOnboardingState?.()?.step || '',
    robotName: window.getOnboardingState?.()?.profile?.robotName || '',
    agentBadgeText: document.getElementById('agent-name-text')?.innerText || '',
    docTitle: document.title || ''
  })`);

  if (step3Result?.robotName !== '๋ฃจ๋‚˜') {
    throw new Error('Step 3 ๋กœ๋ด‡ ์ด๋ฆ„ ์ถ”์ถœ ์‹คํŒจ: ' + JSON.stringify(step3Result));
  }
  if (step3Result?.agentBadgeText !== '๋ฃจ๋‚˜') {
    throw new Error('์ƒ๋‹จ ์—์ด์ „ํŠธ ๋ฑƒ์ง€ ์ด๋ฆ„ ์‹ค์‹œ๊ฐ„ ๊ฐฑ์‹  ์‹คํŒจ: ' + JSON.stringify(step3Result));
  }
  if (step3Result?.step !== 'tune_persona_tone') {
    throw new Error('Step 3 โž” Step 4 ์ƒํƒœ ์ „์ด ์‹คํŒจ: ' + JSON.stringify(step3Result));
  }
  if (!step3Result?.dialogueText.includes('๋ฃจ๋‚˜') || !step3Result?.dialogueText.includes('๋งํˆฌ์™€ ์„ฑ๊ฒฉ')) {
    throw new Error('Step 4 ์งˆ๋ฌธ ๋ฐœํ™” ๋ถˆ์ผ์น˜: ' + JSON.stringify(step3Result));
  }
  console.log(`  โœ… 3๋‹จ๊ณ„ (decide_robot_name): ๋กœ๋ด‡ ์ด๋ฆ„ '${step3Result.robotName}' ํ™•์ •, ์ƒ๋‹จ ๋ฑƒ์ง€ ์‹ค์‹œ๊ฐ„ ๊ฐฑ์‹  ๋ฐ Step 4(ํŽ˜๋ฅด์†Œ๋‚˜ ํ†ค ์งˆ๋ฌธ) ์ˆ˜์‹  ์„ฑ๊ณต`);

  // 5. Step 4: "๊ท€์—ฝ๊ฒŒ ํ•ด์ค˜" ํ…์ŠคํŠธ ์ž…๋ ฅ โž” Step 5 (ํƒ„์ƒ ์ถ•ํ•˜, celebrating ๊ฐ์ •, ์ธ์ง€๊ณต๊ฐ„ ์˜๊ตฌ ์ €์žฅ) ๊ฒ€์ฆ
  await evalCode(`
    (() => {
      const input = document.getElementById('user-text-input');
      if (input) input.value = '๊ท€์—ฝ๊ฒŒ ํ•ด์ค˜';
      document.getElementById('btn-send')?.click();
    })()
  `);
  await new Promise(r => setTimeout(r, 500));

  const step4Result = await evalCode(`({
    dialogueText: document.getElementById('dialogue-bubble')?.innerText || '',
    step: window.getOnboardingState?.()?.step || '',
    personaTone: window.getOnboardingState?.()?.profile?.personaTone || '',
    isOnboarding: window.isOnboardingActive?.() || false,
    emotionText: document.getElementById('emotion-text')?.innerText || '',
    savedCogSpace: localStorage.getItem('bong_cognitive_space') ? JSON.parse(localStorage.getItem('bong_cognitive_space')) : null,
    savedConfig: localStorage.getItem('bong_genesis_config') ? JSON.parse(localStorage.getItem('bong_genesis_config')) : null
  })`);

  if (step4Result?.step !== 'completed') {
    throw new Error('์˜จ๋ณด๋”ฉ ์™„๋ฃŒ(completed) ์ƒํƒœ ๋„๋‹ฌ ์‹คํŒจ: ' + JSON.stringify(step4Result));
  }
  if (step4Result?.isOnboarding !== false) {
    throw new Error('์˜จ๋ณด๋”ฉ ์™„๋ฃŒ ํ›„ ์ผ๋ฐ˜ ๋Œ€ํ™” ๋ชจ๋“œ ์ „ํ™˜ ์‹คํŒจ (isOnboarding์ด false๊ฐ€ ์•„๋‹˜)');
  }
  if (!step4Result?.dialogueText.includes('๋ชจ๋“  ์กฐ์œจ์ด ๋๋‚ฌ์Šต๋‹ˆ๋‹ค') && !step4Result?.dialogueText.includes('๋ฃจ๋‚˜์ž…๋‹ˆ๋‹ค')) {
    throw new Error('Step 5 ์ถ•ํ•˜ ๋ฐœํ™” ๋ถˆ์ผ์น˜: ' + JSON.stringify(step4Result));
  }
  if (step4Result?.emotionText !== 'CELEBRATING') {
    throw new Error('celebrating ๊ฐ์ • ์ „ํ™˜ ์‹คํŒจ: ' + step4Result?.emotionText);
  }
  if (!step4Result?.savedCogSpace || step4Result?.savedCogSpace?.persona?.name !== '๋ฃจ๋‚˜') {
    throw new Error('CognitiveSpace ์˜๊ตฌ ์ €์žฅ ๊ฒ€์ฆ ์‹คํŒจ: ' + JSON.stringify(step4Result?.savedCogSpace));
  }
  console.log('  โœ… 4 & 5๋‹จ๊ณ„ (tune_persona_tone & celebrate_birth): ํŽ˜๋ฅด์†Œ๋‚˜ ํ†ค ํ™•์ •, celebrating ๊ฐ์ • ์ „์ด ๋ฐ CognitiveSpace ์˜๊ตฌ ์ €์žฅ ์™„๋ฃŒ');

  // 6. 8์ข… ๊ฐ์ •(happy, thinking, worried, celebrating, curious, sleeping, alert, neutral) 60fps ์‹œ๊ฐ ๋ Œ๋”๋ง ๋ฐ ๋™๊ธฐํ™” ๊ฒ€์ฆ
  const emotionsToTest = ['happy', 'thinking', 'worried', 'celebrating', 'curious', 'sleeping', 'alert', 'neutral'];
  for (const emo of emotionsToTest) {
    await evalCode(`window.BongAppShellBridge.updateState({ currentEmotion: '${emo}' })`);
    await new Promise(r => setTimeout(r, 60));
    const emoCheck = await evalCode(`({
      badgeText: document.getElementById('emotion-text')?.innerText || '',
      hasSvg: !!document.querySelector('#avatar-container svg'),
      hasLeftEye: !!document.querySelector('#avatar-container #avatar-left-eye'),
      hasMouth: !!document.querySelector('#avatar-container #avatar-mouth'),
      hasCheeks: !!document.querySelector('#avatar-container .bong-cheeks')
    })`);
    if (emoCheck?.badgeText?.toLowerCase() !== emo.toLowerCase()) {
      throw new Error(`๊ฐ์ • ํƒœ๊ทธ ๋ฑƒ์ง€ ๋™๊ธฐํ™” ์‹คํŒจ (${emo}): ` + JSON.stringify(emoCheck));
    }
    if (!emoCheck?.hasSvg || !emoCheck?.hasLeftEye || !emoCheck?.hasMouth || !emoCheck?.hasCheeks) {
      throw new Error(`SVG ์‹ค์‹œ๊ฐ„ ๋ Œ๋”๋ง ์‹คํŒจ (${emo}): ` + JSON.stringify(emoCheck));
    }
  }
  console.log('  โœ… 6๋‹จ๊ณ„: 8์ข… ๊ฐ์ •(happy, thinking, worried, celebrating, curious, sleeping, alert, neutral) 60fps ์‹ค์‹œ๊ฐ„ ๋ฒกํ„ฐ SVG ๋ Œ๋”๋ง ๊ฒ€์ฆ ์™„๋ฃŒ');

  // 7. ๋””๋ฐ”์ด์Šค ๋„๊ตฌ 0.01์ดˆ ์ตœ์šฐ์„  ์ฆ‰์‹œ ๊ฐ€๋กœ์ฑ„๊ธฐ(Pre-emptive Intercept) ๊ฒ€์ฆ
  // 7-1. ์‚ฌ์ง„ ์ดฌ์˜
  await evalCode(`
    (() => {
      const input = document.getElementById('user-text-input');
      if (input) input.value = '์‚ฌ์ง„ ์ฐ์–ด์ค˜';
      document.getElementById('btn-send')?.click();
    })()
  `);
  await new Promise(r => setTimeout(r, 200));
  const cameraResult = await evalCode(`document.getElementById('dialogue-bubble')?.innerText || ''`);
  if (!cameraResult.includes('๐Ÿ“ธ') && !cameraResult.includes('์‚ฌ์ง„')) {
    throw new Error('๋””๋ฐ”์ด์Šค ์นด๋ฉ”๋ผ ๊ฐ€๋กœ์ฑ„๊ธฐ ์‹คํŒจ: ' + cameraResult);
  }
  console.log('  โœ… 7-1๋‹จ๊ณ„: [์‚ฌ์ง„ ์ดฌ์˜] 0.01์ดˆ ์ตœ์šฐ์„  ์ฆ‰์‹œ ๊ฐ€๋กœ์ฑ„๊ธฐ ๋ฐ ์‘๋‹ต ๊ฒ€์ฆ ์„ฑ๊ณต');

  // 7-2. ํƒ€์ด๋จธ
  await evalCode(`
    (() => {
      const input = document.getElementById('user-text-input');
      if (input) input.value = '10๋ถ„ ํƒ€์ด๋จธ ์‹œ์ž‘ํ•ด์ค˜';
      document.getElementById('btn-send')?.click();
    })()
  `);
  await new Promise(r => setTimeout(r, 200));
  const timerResult = await evalCode(`document.getElementById('dialogue-bubble')?.innerText || ''`);
  if (!timerResult.includes('โฐ') || !timerResult.includes('10๋ถ„')) {
    throw new Error('๋””๋ฐ”์ด์Šค ํƒ€์ด๋จธ ๊ฐ€๋กœ์ฑ„๊ธฐ ์‹คํŒจ: ' + timerResult);
  }
  console.log('  โœ… 7-2๋‹จ๊ณ„: [10๋ถ„ ํƒ€์ด๋จธ] 0.01์ดˆ ์ตœ์šฐ์„  ์ฆ‰์‹œ ๊ฐ€๋กœ์ฑ„๊ธฐ ๋ฐ ์‘๋‹ต ๊ฒ€์ฆ ์„ฑ๊ณต');

  // 7-3. ๋ฉ”๋ชจ
  await evalCode(`
    (() => {
      const input = document.getElementById('user-text-input');
      if (input) input.value = '๋ฉ”๋ชจํ•ด์ค˜: ์˜ค๋Š˜์˜ ํ•  ์ผ ๋ชฉ๋ก';
      document.getElementById('btn-send')?.click();
    })()
  `);
  await new Promise(r => setTimeout(r, 200));
  const memoResult = await evalCode(`document.getElementById('dialogue-bubble')?.innerText || ''`);
  if (!memoResult.includes('๐Ÿ“') && !memoResult.includes('๊ธฐ์–ต')) {
    throw new Error('๋””๋ฐ”์ด์Šค ๋ฉ”๋ชจ ๊ฐ€๋กœ์ฑ„๊ธฐ ์‹คํŒจ: ' + memoResult);
  }
  console.log('  โœ… 7-3๋‹จ๊ณ„: [๋ฉ”๋ชจ ์ €์žฅ] 0.01์ดˆ ์ตœ์šฐ์„  ์ฆ‰์‹œ ๊ฐ€๋กœ์ฑ„๊ธฐ ๋ฐ ์‘๋‹ต ๊ฒ€์ฆ ์„ฑ๊ณต');

  // 7-4. ๋ฐฐํ„ฐ๋ฆฌ
  await evalCode(`
    (() => {
      const input = document.getElementById('user-text-input');
      if (input) input.value = '๋ฐฐํ„ฐ๋ฆฌ ๋ช‡ ํผ์„ผํŠธ์•ผ?';
      document.getElementById('btn-send')?.click();
    })()
  `);
  await new Promise(r => setTimeout(r, 200));
  const batteryResult = await evalCode(`document.getElementById('dialogue-bubble')?.innerText || ''`);
  if (!batteryResult.includes('๋ฐฐํ„ฐ๋ฆฌ') || !batteryResult.includes('%')) {
    throw new Error('๋””๋ฐ”์ด์Šค ๋ฐฐํ„ฐ๋ฆฌ ์ž”๋Ÿ‰ ๊ฐ€๋กœ์ฑ„๊ธฐ ์‹คํŒจ: ' + batteryResult);
  }
  console.log('  โœ… 7-4๋‹จ๊ณ„: [๋ฐฐํ„ฐ๋ฆฌ ํ™•์ธ] 0.01์ดˆ ์ตœ์šฐ์„  ์ฆ‰์‹œ ๊ฐ€๋กœ์ฑ„๊ธฐ ๋ฐ ์‘๋‹ต ๊ฒ€์ฆ ์„ฑ๊ณต');

  // 7-5. ์ƒ๋‹จ ํ—ค๋” [๐Ÿ”Š ์Œ์„ฑ ON/OFF ํ† ๊ธ€] ๋ฒ„ํŠผ ์ธํ„ฐ๋ž™์…˜ ๊ฒ€์ฆ
  const voiceToggleInitial = await evalCode(`({
    btnText: document.getElementById('btn-voice-toggle')?.innerText || '',
    drawerCheckbox: document.getElementById('checkbox-voice-enabled')?.checked
  })`);
  if (voiceToggleInitial.btnText !== '๐Ÿ”Š') {
    throw new Error('์Œ์„ฑ ํ† ๊ธ€ ๊ธฐ๋ณธ๊ฐ’ ๐Ÿ”Š ํ™•์ธ ์‹คํŒจ: ' + JSON.stringify(voiceToggleInitial));
  }
  await evalCode(`document.getElementById('btn-voice-toggle')?.click()`);
  await new Promise(r => setTimeout(r, 100));
  const voiceToggleMuted = await evalCode(`({
    btnText: document.getElementById('btn-voice-toggle')?.innerText || '',
    drawerCheckbox: document.getElementById('checkbox-voice-enabled')?.checked
  })`);
  if (voiceToggleMuted.btnText !== '๐Ÿ”‡' || voiceToggleMuted.drawerCheckbox !== false) {
    throw new Error('์Œ์„ฑ ๋„๊ธฐ(Mute) ํ† ๊ธ€ ์‹คํŒจ: ' + JSON.stringify(voiceToggleMuted));
  }
  // ๋‹ค์‹œ ์ผœ๊ธฐ
  await evalCode(`document.getElementById('btn-voice-toggle')?.click()`);
  await new Promise(r => setTimeout(r, 100));
  console.log('  โœ… 7-5๋‹จ๊ณ„: [๐Ÿ”Š ์Œ์„ฑ ON/OFF ํ† ๊ธ€] 0ms ์ฆ‰๊ฐ ์Œ์†Œ๊ฑฐ ๋ฐ ์ƒํƒœ ๋™๊ธฐํ™” ๊ฒ€์ฆ ์„ฑ๊ณต');

  // 8. ์„ค์ • ๋“œ๋กœ์–ด ์—ด๊ธฐ ๋ฐ MLX Gemma 4-26B ์„ค์ • ์ฃผ์ž… & [๋งˆ์Œ ์—ฐ๊ฒฐ ํ™•์ธ] ๊ฒ€์ฆ
  console.log('  ๐Ÿงช 8-1๋‹จ๊ณ„: ์„ค์ • ๋“œ๋กœ์–ด ์—ด๊ธฐ ๋ฐ MLX Gemma 4-26B ์„ค์ • ์ฃผ์ž…...');
  await evalCode(`
    (() => {
      document.getElementById('btn-open-settings')?.click();
      const provSelect = document.getElementById('select-llm-provider');
      if (provSelect) {
        provSelect.value = 'openai_compatible';
        provSelect.dispatchEvent(new Event('change'));
      }
      const endpointInput = document.getElementById('input-drawer-endpoint');
      if (endpointInput) endpointInput.value = 'http://100.79.62.64:8080/v1';
      const modelInput = document.getElementById('input-drawer-model');
      if (modelInput) modelInput.value = 'mlx-community/gemma-4-26b-a4b-it-4bit';
    })()
  `);
  await new Promise(r => setTimeout(r, 200));

  // [๐Ÿ”Œ ๋งˆ์Œ ์—ฐ๊ฒฐ ํ™•์ธ]
  await evalCode(`document.getElementById('btn-drawer-test-connection')?.click()`);
  await new Promise(r => setTimeout(r, 1200));

  const connFeedback = await evalCode(`({
    display: document.getElementById('drawer-conn-feedback')?.style.display,
    text: document.getElementById('drawer-conn-feedback')?.innerText || ''
  })`);
  if (!connFeedback.text.includes('์—ฐ๊ฒฐ๋˜์—ˆ์Šต๋‹ˆ๋‹ค') && !connFeedback.text.includes('โœจ')) {
    throw new Error('MLX Gemma 4-26B ๋งˆ์Œ ์—ฐ๊ฒฐ ํ™•์ธ ์‹คํŒจ: ' + JSON.stringify(connFeedback));
  }
  console.log('  โœ… 8-1๋‹จ๊ณ„: MLX Gemma 4-26B [๐Ÿ”Œ ๋งˆ์Œ ์—ฐ๊ฒฐ ํ™•์ธ] ํ†ต๊ณผ: ' + connFeedback.text);

  // Mories ์ง„๋‹จ ๋ฒ„ํŠผ ํด๋ฆญ
  await evalCode(`document.getElementById('btn-drawer-test-mories')?.click()`);
  await new Promise(r => setTimeout(r, 400));
  const drawerMoriesFeedback = await evalCode(`document.getElementById('drawer-mories-feedback')?.innerText || ''`);
  console.log('  โœ… 8-2๋‹จ๊ณ„: Mories ๊ธฐ์–ต์˜ ๋ฐ”๋‹ค ์ง„๋‹จ ํ”ผ๋“œ๋ฐฑ ํ™•์ธ: ' + drawerMoriesFeedback);

  // BADA WebSocket ์ง„๋‹จ ๋ฒ„ํŠผ ํด๋ฆญ
  await evalCode(`document.getElementById('btn-drawer-test-ws')?.click()`);
  await new Promise(r => setTimeout(r, 400));
  const drawerWsFeedback = await evalCode(`document.getElementById('drawer-ws-feedback')?.innerText || ''`);
  console.log('  โœ… 8-3๋‹จ๊ณ„: BADA WebSocket ์ง„๋‹จ ํ”ผ๋“œ๋ฐฑ ํ™•์ธ: ' + drawerWsFeedback);

  // ์„ค์ • ์ €์žฅ ๋ฐ ๋“œ๋กœ์–ด ๋‹ซ๊ธฐ
  await evalCode(`
    (() => {
      document.getElementById('btn-save-settings')?.click();
      document.getElementById('btn-close-drawer')?.click();
    })()
  `);
  await new Promise(r => setTimeout(r, 300));

  // 8-4. ์‹ค์ œ MLX Gemma 4-26B๋กœ "์•ˆ๋…•! ๋„Œ ๋ˆ„๊ตฌ์•ผ?" ์งˆ๋ฌธ ์ „์†ก โž” Thinking ํ‘œ์ • ๋ฐ ํŽ„์Šค ๋ฒ„๋ธ” ๋ Œ๋”๋ง ๊ฒ€์ฆ
  console.log('  ๐Ÿงช 8-4๋‹จ๊ณ„: ์‹ค์ œ MLX Gemma 4-26B๋กœ "์•ˆ๋…•! ๋„Œ ๋ˆ„๊ตฌ์•ผ?" ์ „์†ก...');
  await evalCode(`
    (() => {
      const input = document.getElementById('user-text-input');
      if (input) input.value = '์•ˆ๋…•! ๋„Œ ๋ˆ„๊ตฌ์•ผ?';
      document.getElementById('btn-send')?.click();
    })()
  `);
  await new Promise(r => setTimeout(r, 200));

  // ์ „์†ก ์ฆ‰์‹œ thinking ์ƒํƒœ ๊ฒ€์ฆ
  const thinkingCheck = await evalCode(`({
    emotionBadge: document.getElementById('emotion-text')?.innerText || '',
    dialogueText: document.getElementById('dialogue-bubble')?.innerText || '',
    hasThinkingDots: !!document.querySelector('.thinking-dots')
  })`);
  console.log('  โœ… 8-4.1๋‹จ๊ณ„: ๋ชจ๋ธ ์ƒ์„ฑ ์ค‘ Thinking ํ‘œ์ • ๋ฐ ์  3๊ฐœ ํŽ„์Šค ์• ๋‹ˆ๋ฉ”์ด์…˜ ๋ฒ„๋ธ” ๋ Œ๋”๋ง ํ™•์ธ: ' + JSON.stringify(thinkingCheck));

  // MLX Gemma 4-26B ๋กœ์ปฌ ์ถ”๋ก  (์•ฝ 10~20์ดˆ ์†Œ์š”) ์‘๋‹ต ๋Œ€๊ธฐ (์ตœ๋Œ€ 90์ดˆ)
  console.log('  โณ 8-4.2๋‹จ๊ณ„: MLX Gemma 4-26B ๊ณ ์„ฑ๋Šฅ ์ถ”๋ก  ๋Œ€๊ธฐ ์ค‘ (์ตœ๋Œ€ 90์ดˆ)...');
  let mlxAnswerReceived = false;
  let finalAnswerText = '';
  const startTime = Date.now();

  for (let attempt = 0; attempt < 90; attempt++) {
    await new Promise(r => setTimeout(r, 1000));
    const currentDialogue = await evalCode(`document.getElementById('dialogue-bubble')?.innerText || ''`);
    if (currentDialogue && !currentDialogue.includes('์ƒ๊ฐ ์ค‘') && !currentDialogue.includes('๊ธฐ๋ด‰์ด์•ผ') && currentDialogue !== '๋‚˜: ์•ˆ๋…•! ๋„Œ ๋ˆ„๊ตฌ์•ผ?') {
      finalAnswerText = currentDialogue;
      mlxAnswerReceived = true;
      break;
    }
  }

  const elapsedSec = ((Date.now() - startTime) / 1000).toFixed(1);
  if (!mlxAnswerReceived) {
    throw new Error(`MLX Gemma 4-26B ์‘๋‹ต ํƒ€์ž„์•„์›ƒ (${elapsedSec}์ดˆ ๊ฒฝ๊ณผ): ` + finalAnswerText);
  }
  if (finalAnswerText.includes('์‘๋‹ต์„ ๋ฐ›์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค') || finalAnswerText.includes('์—ฐ๊ฒฐ ํ™•์ธ ํ•„์š”')) {
    throw new Error('MLX Gemma 4-26B ์‘๋‹ต ์˜ค๋ฅ˜: ' + finalAnswerText);
  }

  console.log(`  ๐ŸŽ‰ 8-5๋‹จ๊ณ„: MLX Gemma 4-26B ์‹ค์‹œ๊ฐ„ ์‘๋‹ต ์ˆ˜์‹  ์„ฑ๊ณต (${elapsedSec}์ดˆ ์†Œ์š”)!`);
  console.log(`     ๐Ÿ“ ์ˆ˜์‹ ๋œ ๋‹ต๋ณ€: "${finalAnswerText}"`);

  ws.close();
} finally {
  chromeProc.kill();
}

if (hasErrors) {
  console.error('โŒ [Browser E2E] ๋ธŒ๋ผ์šฐ์ € ์ฝ˜์†” ์˜ค๋ฅ˜๊ฐ€ ๊ฐ์ง€๋˜์–ด ๊ฒ€์ฆ์„ ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค.');
  process.exit(1);
} else {
  console.log('\n๐ŸŽ‰ [Browser E2E] ์˜ํ™” Her ๊ฐ์„ฑ์˜ 5๋‹จ๊ณ„ ๋Œ€ํ™”ํ˜• ํŽ˜๋ฅด์†Œ๋‚˜ ํ•ฉ์„ฑ ์‹œ๋‚˜๋ฆฌ์˜ค ๋ธŒ๋ผ์šฐ์ € E2E 100% ํ†ต๊ณผ!\n');
}