eztrans-rs 0.2.1

FFI bindings and IPC server for ChangShinSoft's Eztrans software
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
470
471
472
473
474
// EzTransEngine Tests
// Note: These tests require the EzTrans DLL (32-bit) to be present.
// Run with: cargo test --target i686-pc-windows-msvc --test engine_tests -- --include-ignored --test-threads=1

use eztrans_rs::EzTransEngine;
use serial_test::serial;
use std::sync::Mutex;

/// Wrapper to make EzTransEngine usable in static context
/// SAFETY: EzTrans DLL operations are only safe in single-threaded context,
/// which is enforced by #[serial] attribute and --test-threads=1
struct EngineWrapper(EzTransEngine);
unsafe impl Send for EngineWrapper {}
unsafe impl Sync for EngineWrapper {}

/// Global engine instance - initialized once and reused across all tests
/// This is necessary because EzTrans DLL has global state and doesn't handle
/// multiple initialize/terminate cycles well within a single process.
static ENGINE: Mutex<Option<EngineWrapper>> = Mutex::new(None);

fn get_engine_paths() -> (String, String) {
    let manifest_dir = env!("CARGO_MANIFEST_DIR");
    let dll_path = format!("{}/../eztrans_dll/J2KEngine.dll", manifest_dir);
    let dat_path = format!("{}/../eztrans_dll/Dat", manifest_dir);
    (dll_path, dat_path)
}

fn with_engine<F, R>(f: F) -> R
where
    F: FnOnce(&EzTransEngine) -> R,
{
    let mut guard = ENGINE.lock().unwrap();
    if guard.is_none() {
        let (dll_path, dat_path) = get_engine_paths();
        let engine = EzTransEngine::new(&dll_path).expect("Failed to load DLL");
        engine
            .initialize_ex("CSUSER123455", &dat_path)
            .expect("Failed to initialize engine");
        *guard = Some(EngineWrapper(engine));
    }
    f(&guard.as_ref().unwrap().0)
}

// ============================================
// Engine Initialization Tests
// ============================================

#[test]
#[ignore]
#[serial]
fn test_engine_new() {
    with_engine(|engine| {
        // If we got here, the engine was created successfully
        assert!(!engine.module.is_invalid());
    });
}

#[test]
fn test_engine_new_invalid_path() {
    let result = EzTransEngine::new("invalid/path/to/dll.dll");
    assert!(result.is_err());
}

#[test]
#[ignore]
#[serial]
fn test_engine_initialize_ex() {
    with_engine(|engine| {
        // Engine is already initialized via with_engine()
        // Verify engine has initialize_ex function loaded
        assert!(engine.initialize_ex.is_some());
    });
}

#[test]
#[ignore]
#[serial]
fn test_engine_has_required_functions() {
    with_engine(|engine| {
        // Check essential functions are loaded
        assert!(engine.initialize_ex.is_some() || engine.initialize.is_some());
        assert!(engine.terminate.is_some());
        assert!(engine.translate_mmntw.is_some() || engine.translate_mmnt.is_some());
        assert!(engine.free_mem.is_some());
    });
}

// ============================================
// Hangul Encode/Decode Tests
// ============================================

#[test]
#[ignore]
#[serial]
fn test_hangul_encode_basic() {
    with_engine(|engine| {
        // Test Korean character encoding
        let input = "안녕하세요";
        let encoded = engine.hangul_encode(input);

        // Should contain +x prefixes for Korean characters
        assert!(encoded.contains("+x"));
        assert!(!encoded.contains(""));
    });
}

#[test]
#[ignore]
#[serial]
fn test_hangul_encode_mixed() {
    with_engine(|engine| {
        // Test mixed content (Korean + Japanese + ASCII)
        let input = "Hello 안녕 こんにちは";
        let encoded = engine.hangul_encode(input);

        // ASCII and Japanese should remain unchanged
        assert!(encoded.contains("Hello"));
        assert!(encoded.contains("こんにちは"));
        // Korean should be encoded
        assert!(!encoded.contains("안녕"));
    });
}

#[test]
#[ignore]
#[serial]
fn test_hangul_encode_at_symbol() {
    with_engine(|engine| {
        // @ symbol should be encoded
        let input = "test@example.com";
        let encoded = engine.hangul_encode(input);

        assert!(encoded.contains("+x0040")); // @ = U+0040
    });
}

#[test]
#[ignore]
#[serial]
fn test_hangul_encode_special_chars() {
    with_engine(|engine| {
        // Special characters from the special_chars set
        let input = "테스트♥심볼♠";
        let encoded = engine.hangul_encode(input);

        // Should encode both Korean and special symbols
        assert!(encoded.contains("+X")); // Special chars use +X
    });
}

#[test]
#[ignore]
#[serial]
fn test_hangul_decode_basic() {
    with_engine(|engine| {
        // Test decoding
        let encoded = "+xC548+xB155"; // 안녕
        let decoded = engine.hangul_decode(encoded);

        assert_eq!(decoded, "안녕");
    });
}

#[test]
#[ignore]
#[serial]
fn test_hangul_encode_decode_roundtrip() {
    with_engine(|engine| {
        let original = "안녕하세요 Hello 世界";
        let encoded = engine.hangul_encode(original);
        let decoded = engine.hangul_decode(&encoded);

        assert_eq!(decoded, original);
    });
}

#[test]
#[ignore]
#[serial]
fn test_hangul_decode_invalid_hex() {
    with_engine(|engine| {
        // Invalid hex should be preserved
        let input = "+xGGGG test";
        let decoded = engine.hangul_decode(input);

        assert_eq!(decoded, "+xGGGG test");
    });
}

#[test]
#[ignore]
#[serial]
fn test_hangul_decode_incomplete() {
    with_engine(|engine| {
        // Incomplete sequence should be preserved
        let input = "+x12 test";
        let decoded = engine.hangul_decode(input);

        assert_eq!(decoded, "+x12 test");
    });
}

// ============================================
// is_hangul_range Tests
// ============================================

#[test]
#[ignore]
#[serial]
fn test_is_hangul_range_syllables() {
    with_engine(|engine| {
        // Hangul Syllables block (AC00-D7A3)
        assert!(engine.is_hangul_range('' as u32)); // U+AC00
        assert!(engine.is_hangul_range('' as u32)); // U+D7A3
    });
}

#[test]
#[ignore]
#[serial]
fn test_is_hangul_range_jamo() {
    with_engine(|engine| {
        // Hangul Jamo block (1100-11FF)
        assert!(engine.is_hangul_range(0x1100));
        assert!(engine.is_hangul_range(0x11FF));

        // Hangul Compatibility Jamo (3130-318F)
        assert!(engine.is_hangul_range(0x3130));
        assert!(engine.is_hangul_range(0x318F));
    });
}

#[test]
#[ignore]
#[serial]
fn test_is_hangul_range_non_hangul() {
    with_engine(|engine| {
        // ASCII
        assert!(!engine.is_hangul_range('A' as u32));
        assert!(!engine.is_hangul_range('z' as u32));

        // Japanese
        assert!(!engine.is_hangul_range('' as u32));
        assert!(!engine.is_hangul_range('' as u32));
    });
}

// ============================================
// Translation Tests
// ============================================

#[test]
#[ignore]
#[serial]
fn test_translate_mmntw() {
    with_engine(|engine| {
        let input = "おはようございます。";
        let result = engine.translate_mmntw(input);

        assert!(result.is_ok(), "Translation failed: {:?}", result.err());
        let translated = result.unwrap();
        assert!(!translated.is_empty());
        assert_ne!(translated, input);
    });
}

#[test]
#[ignore]
#[serial]
fn test_translate_mmnt() {
    with_engine(|engine| {
        let input = "こんにちは。";
        let result = engine.translate_mmnt(input);

        assert!(result.is_ok(), "Translation failed: {:?}", result.err());
        let translated = result.unwrap();
        assert!(!translated.is_empty());
    });
}

#[test]
#[ignore]
#[serial]
fn test_default_translate() {
    with_engine(|engine| {
        let input = "今日はいい天気ですね。";
        let result = engine.default_translate(input);

        assert!(result.is_ok(), "Translation failed: {:?}", result.err());
        let translated = result.unwrap();
        assert!(!translated.is_empty());
    });
}

#[test]
#[ignore]
#[serial]
fn test_default_translate_with_korean() {
    with_engine(|engine| {
        // Input with Korean that needs encoding
        let input = "가나다라おはようございます。";
        let result = engine.default_translate(input);

        assert!(result.is_ok(), "Translation failed: {:?}", result.err());
        let translated = result.unwrap();

        // Korean should be preserved
        assert!(translated.contains("가나다라"));
    });
}

#[test]
#[ignore]
#[serial]
fn test_default_translate_empty() {
    with_engine(|engine| {
        let input = "";
        let result = engine.default_translate(input);

        assert!(result.is_ok());
    });
}

#[test]
#[ignore]
#[serial]
fn test_translate_multiple_times() {
    with_engine(|engine| {
        let texts = [
            "おはようございます。",
            "こんにちは。",
            "こんばんは。",
            "ありがとうございます。",
            "すみません。",
        ];

        for text in &texts {
            let result = engine.default_translate(text);
            assert!(
                result.is_ok(),
                "Failed to translate '{}': {:?}",
                text,
                result.err()
            );
        }
    });
}

// ============================================
// Reload User Dict Test
// ============================================

#[test]
#[ignore]
#[serial]
fn test_reload_user_dict() {
    with_engine(|engine| {
        let result = engine.reload_user_dict();
        assert!(
            result.is_ok(),
            "Failed to reload user dict: {:?}",
            result.err()
        );
    });
}

// ============================================
// Property Tests
// ============================================

#[test]
#[ignore]
#[serial]
fn test_set_property() {
    with_engine(|engine| {
        // Check that set_property function is loaded
        assert!(engine.set_property.is_some());
    });
}

#[test]
#[ignore]
#[serial]
fn test_get_property() {
    with_engine(|engine| {
        // Check that get_property function is loaded
        assert!(engine.get_property.is_some());
    });
}

// ============================================
// Emoji Test
// ============================================

#[test]
#[ignore]
#[serial]
fn test_emoji_translation() {
    with_engine(|engine| {
        // 이모지가 포함된 일본어 텍스트
        let test_cases = [
            ("こんにちは😀", "단일 이모지"),
            ("ありがとう👨‍👩‍👧ございます", "ZWJ 시퀀스"),
            ("今日🇰🇷天気", "국기 이모지"),
            ("おはよう👋🏻", "피부색 이모지"),
            ("テスト😀😀😀テスト", "다중 이모지"),
        ];

        for (input, desc) in test_cases {
            println!("\n=== {} ===", desc);
            println!("입력: {}", input);

            // 이모지 코드포인트 확인
            print!("코드포인트: ");
            for c in input.chars() {
                if c as u32 >= 0x1F000 || c == '\u{200D}' {
                    print!("U+{:X} ", c as u32);
                }
            }
            println!();

            let result = engine.translate_mmntw(input);
            match result {
                Ok(translated) => {
                    println!("번역 결과: {}", translated);

                    // 이모지가 보존되었는지 확인
                    let input_emojis: Vec<char> = input
                        .chars()
                        .filter(|c| *c as u32 >= 0x1F000 || *c == '\u{200D}')
                        .collect();
                    let output_emojis: Vec<char> = translated
                        .chars()
                        .filter(|c| *c as u32 >= 0x1F000 || *c == '\u{200D}')
                        .collect();

                    println!("입력 이모지: {:?}", input_emojis);
                    println!("출력 이모지: {:?}", output_emojis);

                    if input_emojis == output_emojis {
                        println!("✓ 이모지 보존됨");
                    } else {
                        println!("✗ 이모지 변경/손실됨!");
                    }
                }
                Err(e) => {
                    println!("번역 실패: {:?}", e);
                }
            }
        }
    });
}

#[test]
#[ignore]
#[serial]
fn test_emoji_only() {
    with_engine(|engine| {
        // 이모지만 있는 경우
        let input = "😀";
        println!("입력: {}", input);

        let result = engine.translate_mmntw(input);
        match result {
            Ok(translated) => {
                println!("번역 결과: '{}'", translated);
                println!("번역 결과 바이트: {:?}", translated.as_bytes());
            }
            Err(e) => {
                println!("번역 실패: {:?}", e);
            }
        }
    });
}