File size: 14,518 Bytes
3130613
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c6dd3fa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3130613
 
 
 
 
 
417db6e
3130613
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
417db6e
3130613
 
 
 
 
 
 
 
417db6e
 
 
3130613
 
417db6e
 
3130613
 
417db6e
3130613
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2702d02
3130613
2702d02
 
3130613
 
2702d02
3130613
 
 
 
 
 
 
 
 
 
 
 
 
 
2702d02
 
 
 
 
 
3130613
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0898117
 
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
"""
Test suite for FIX-33 through FIX-37.
Run from project root: python tests/test_recent_fixes.py

Tests that need camel-tools (ArabicGrammarGuard) test the regex logic
directly without instantiating the full class.
"""
import sys
import os
import re

# Add src to path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', 'src'))

PASS = 0
FAIL = 0

def test(name, condition, detail=""):
    global PASS, FAIL
    if condition:
        PASS += 1
        print(f"  ✅ {name}")
    else:
        FAIL += 1
        print(f"  ❌ {name}{detail}")


# ══════════════════════════════════════════════════════════════
# TEST 1: FIX-33 — Grammar rules don't corrupt الامتحان
# Test the regex logic directly without camel-tools
# ══════════════════════════════════════════════════════════════
print("\n═══ FIX-33: Grammar rules — preposition + root noun protection ═══")

# Import the blocklist and test the regex callback logic
from nlp.grammar import grammar_rules

# Get the blocklist from the class (class-level attribute)
_PREP_BLOCKLIST = grammar_rules.ArabicGrammarGuard._PREP_BLOCKLIST

# Simulate the fix_prepositions_advanced regex with callback
def _prep_replace(m):
    prep = m.group(1)
    stem = m.group(2)
    suffix = m.group(3)
    full_word = stem + suffix
    if full_word in _PREP_BLOCKLIST:
        return m.group(0)
    if stem.startswith('ال') and suffix == 'ان':
        return m.group(0)
    return f'{prep} {stem}ين'

def fix_prepositions(text):
    return re.sub(
        r'\b([وف]?(?:في|من|إلى|على|عن|حتى))\s+([أ-ي]{4,})(ون|ان)\b',
        _prep_replace, text
    )

# Root nouns — should NOT be corrupted
root_nouns = [
    ("إلى الامتحان", "إلى الامتحان"),
    ("من الإنسان", "من الإنسان"),
    ("في الميدان", "في الميدان"),
    ("على المكان", "على المكان"),
    ("عن السلطان", "عن السلطان"),
    ("إلى البرلمان", "إلى البرلمان"),
    ("في الحيوان", "في الحيوان"),
    ("من القرآن", "من القرآن"),
    ("في الزمان", "في الزمان"),
]
for input_text, expected in root_nouns:
    result = fix_prepositions(input_text)
    test(f"'{input_text}' → unchanged", result == expected, f"got '{result}'")

# Actual plurals — SHOULD be corrected
plurals = [
    ("في المهندسون", "في المهندسين"),
    ("من المعلمون", "من المعلمين"),
]
for input_text, expected in plurals:
    result = fix_prepositions(input_text)
    test(f"'{input_text}' → '{expected}'", result == expected, f"got '{result}'")

# ── FIX-33b: Second regex (بال...ون/ان) ──
print("\n═══ FIX-33b: Attached preposition regex protection ═══")

def _attached_prep_replace(m):
    prefix = m.group(1)
    stem = m.group(2)
    suffix = m.group(3)
    full_word = 'ال' + stem + suffix
    if full_word in _PREP_BLOCKLIST:
        return m.group(0)
    if suffix == 'ان':
        return m.group(0)
    return f'{prefix}ال{stem}ين'

def fix_attached_prep(text):
    return re.sub(r'\b([وف]?[بلكف])ال([أ-ي]{4,})(ون|ان)\b', _attached_prep_replace, text)

# Root nouns with بال — should NOT be corrupted
attached_root = [
    ("بالامتحان", "بالامتحان"),       # NOT بالامتحين
    ("بالإنسان", "بالإنسان"),         # NOT بالإنسين
    ("بالميدان", "بالميدان"),         # NOT بالميدين
    ("كالسلطان", "كالسلطان"),         # NOT كالسلطين
    ("فبالبرلمان", "فبالبرلمان"),     # NOT فبالبرلمين
]
for input_text, expected in attached_root:
    result = fix_attached_prep(input_text)
    test(f"'{input_text}' → unchanged", result == expected, f"got '{result}'")

# Actual plurals with بال — SHOULD be corrected
attached_plurals = [
    ("بالمهندسون", "بالمهندسين"),
    ("كالمعلمون", "كالمعلمين"),
]
for input_text, expected in attached_plurals:
    result = fix_attached_prep(input_text)
    test(f"'{input_text}' → '{expected}'", result == expected, f"got '{result}'")

# ── FIX-33b: Third regex (ل...ون/ان) ──
def _lam_prep_replace(m):
    prefix = m.group(1)
    stem = m.group(2)
    suffix = m.group(3)
    if (stem + suffix) in _PREP_BLOCKLIST:
        return m.group(0)
    if suffix == 'ان':
        return m.group(0)
    return f'{prefix}{stem}ين'

def fix_lam_prep(text):
    return re.sub(r'\b([وف]?ل)([أ-ي]{4,})(ون|ان)\b', _lam_prep_replace, text)

# ل-prefixed root nouns
lam_root = [
    ("لامتحان", "لامتحان"),
    ("لإنسان", "لإنسان"),
]
for input_text, expected in lam_root:
    result = fix_lam_prep(input_text)
    test(f"'{input_text}' → unchanged", result == expected, f"got '{result}'")

# ل-prefixed plurals — SHOULD be corrected
test("'لمهندسون'→'لمهندسين'",
     fix_lam_prep("لمهندسون") == "لمهندسين",
     f"got '{fix_lam_prep('لمهندسون')}'")


# ══════════════════════════════════════════════════════════════
# TEST 2: FIX-35 — Spelling doesn't strip conjugation suffixes
# ══════════════════════════════════════════════════════════════
print("\n═══ FIX-35: Spelling — conjugation suffix protection ═══")

_CONJUGATION_SUFFIXES = {'ن', 'ت'}

def simulate_insertion_fix_check(orig_word, corr_word):
    """Simulate the FIX-35 suffix strip check logic from app.py."""
    if len(orig_word) != len(corr_word) + 1:
        return "not_applicable"
    
    for di in range(len(orig_word)):
        candidate = orig_word[:di] + orig_word[di + 1:]
        if candidate == corr_word:
            removed_char = orig_word[di]
            removed_pos = di
            if (removed_char in _CONJUGATION_SUFFIXES
                    and removed_pos == len(orig_word) - 1
                    and len(corr_word) >= 3):
                return "blocked"
            return "allowed"
    return "not_applicable"

# BLOCKED cases (verb conjugation suffix stripping)
test("'ذهبن'→'ذهب' blocked (ن suffix)", 
     simulate_insertion_fix_check("ذهبن", "ذهب") == "blocked",
     f"got {simulate_insertion_fix_check('ذهبن', 'ذهب')}")

test("'كتبت'→'كتب' blocked (ت suffix)",
     simulate_insertion_fix_check("كتبت", "كتب") == "blocked",
     f"got {simulate_insertion_fix_check('كتبت', 'كتب')}")

# NOW ALLOWED cases (narrowed set — ة, و no longer blocked)
test("'درسة'→'درس' allowed (ة not in narrowed set)",
     simulate_insertion_fix_check("درسة", "درس") == "allowed",
     f"got {simulate_insertion_fix_check('درسة', 'درس')}")

test("'جلسوا'→'جلسو' allowed (ا not in narrowed set)",
     simulate_insertion_fix_check("جلسوا", "جلسو") == "allowed",
     f"got {simulate_insertion_fix_check('جلسوا', 'جلسو')}")

# ALLOWED cases (mid-word insertion fix — always allowed)
test("'الكتتاب'→'الكتاب' allowed (mid-word extra ت)",
     simulate_insertion_fix_check("الكتتاب", "الكتاب") == "allowed",
     f"got {simulate_insertion_fix_check('الكتتاب', 'الكتاب')}")

test("'الصصف'→'الصف' allowed (mid-word extra ص)",
     simulate_insertion_fix_check("الصصف", "الصف") == "allowed",
     f"got {simulate_insertion_fix_check('الصصف', 'الصف')}")


# ══════════════════════════════════════════════════════════════
# TEST 3: FIX-36 — Overlap resolver merges grammar+punctuation
# ══════════════════════════════════════════════════════════════
print("\n═══ FIX-36: Overlap resolver — grammar+punctuation merge ═══")

from nlp.correction_patch import CorrectionPatch, PatchSet, PRIORITY

# Case 1: grammar + matching punctuation → merge
ps = PatchSet()
grammar_patch = CorrectionPatch(
    stage='grammar',
    start_original=18, end_original=26,
    start_current=18, end_current=26,
    original='المعلمون', replacement='المعلمين',
    priority=PRIORITY['grammar'], confidence=0.9
)
punc_patch = CorrectionPatch(
    stage='punctuation',
    start_original=18, end_original=26,
    start_current=18, end_current=26,
    original='المعلمون', replacement='المعلمين.',
    priority=PRIORITY['punctuation'], confidence=0.8
)
ps.add(grammar_patch)
ps.add(punc_patch)
resolved = ps.resolve_overlaps()

test("Case 1: 1 patch after merge", len(resolved) == 1, f"got {len(resolved)}")
if resolved:
    test("Merged replacement = 'المعلمين.'",
         resolved[0].replacement == 'المعلمين.',
         f"got '{resolved[0].replacement}'")
    test("Merged stage = grammar",
         resolved[0].stage == 'grammar',
         f"got '{resolved[0].stage}'")

# Case 2: Non-matching corrections → drop punctuation
ps2 = PatchSet()
ps2.add(CorrectionPatch(
    stage='grammar', start_original=0, end_original=5,
    start_current=0, end_current=5,
    original='ذهبوا', replacement='ذهبن',
    priority=PRIORITY['grammar'], confidence=0.9
))
ps2.add(CorrectionPatch(
    stage='punctuation', start_original=0, end_original=5,
    start_current=0, end_current=5,
    original='ذهبوا', replacement='ذهبوا.',
    priority=PRIORITY['punctuation'], confidence=0.8
))
resolved2 = ps2.resolve_overlaps()
test("Case 2: punc merged", len(resolved2) == 1, f"got {len(resolved2)}")
if resolved2:
    test("Appended punc to grammar 'ذهبن.'",
         resolved2[0].replacement == 'ذهبن.',
         f"got '{resolved2[0].replacement}'")

# Case 3: spelling + punctuation merge (Phase 14 fix)
ps3 = PatchSet()
ps3.add(CorrectionPatch(
    stage='spelling', start_original=0, end_original=5,
    start_current=0, end_current=5,
    original='برفم', replacement='برغم',
    priority=PRIORITY['spelling'], confidence=0.9
))
ps3.add(CorrectionPatch(
    stage='punctuation', start_original=0, end_original=5,
    start_current=0, end_current=5,
    original='برفم', replacement='برفم.',
    priority=PRIORITY['punctuation'], confidence=0.8
))
resolved3 = ps3.resolve_overlaps()
test("Case 3: spelling+punc merged (1 patch)",
     len(resolved3) == 1, f"got {len(resolved3)}")
if resolved3:
    test("Appended punc to spelling 'برغم.'",
         resolved3[0].replacement == 'برغم.',
         f"got '{resolved3[0].replacement}'")


# ══════════════════════════════════════════════════════════════
# TEST 4: FIX-34 — No auto re-analysis after Apply All
# ══════════════════════════════════════════════════════════════
print("\n═══ FIX-34: Frontend — no auto re-analysis after Apply All ═══")

editor_js_path = os.path.join(os.path.dirname(__file__), '..', 'src', 'js', 'editor.js')
with open(editor_js_path, 'r', encoding='utf-8') as f:
    editor_content = f.read()

# Find applyAllSuggestions function body
fn_match = re.search(
    r'function applyAllSuggestions\b(.*?\n\})',
    editor_content, re.DOTALL
)
if fn_match:
    fn_body = fn_match.group(1)
    has_auto_analyze = bool(re.search(r'analyzeText\s*\(', fn_body))
    test("applyAllSuggestions does NOT call analyzeText()",
         not has_auto_analyze,
         "Found analyzeText() call inside applyAllSuggestions!")
    
    has_fix34_comment = 'FIX-34' in fn_body
    test("FIX-34 comment present in function",
         has_fix34_comment, "Missing FIX-34 comment")
else:
    test("applyAllSuggestions function found", False, "Could not find function")


# ══════════════════════════════════════════════════════════════
# TEST 5: FIX-37 — Terminal period fallback exists in app.py
# ══════════════════════════════════════════════════════════════
print("\n═══ FIX-37: Terminal period fallback in app.py ═══")

app_py_path = os.path.join(os.path.dirname(__file__), '..', 'src', 'app.py')
with open(app_py_path, 'r', encoding='utf-8') as f:
    app_content = f.read()

test("FIX-37 comment exists in app.py",
     'FIX-37' in app_content,
     "Missing FIX-37 marker")

test("PUNC-FALLBACK log message exists",
     'PUNC-FALLBACK' in app_content,
     "Missing PUNC-FALLBACK log")

test("Terminal period injection code exists",
     "_lw_text + '.'" in app_content,
     "Missing terminal period injection")

# Verify indentation is correct (12-space, not 14)
for line in app_content.split('\n'):
    if 'FIX-37' in line:
        spaces = len(line) - len(line.lstrip())
        test(f"FIX-37 at correct indent (12 spaces)",
             spaces == 12,
             f"got {spaces} spaces")
        break


# ══════════════════════════════════════════════════════════════
# SUMMARY
# ══════════════════════════════════════════════════════════════
print(f"\n{'═'*60}")
if FAIL == 0:
    print(f"  ✅ ALL {PASS} TESTS PASSED")
else:
    print(f"  ❌ {FAIL} FAILED, {PASS} passed out of {PASS+FAIL} tests")
print(f"{'═'*60}")
if __name__ == "__main__":
    sys.exit(1 if FAIL > 0 else 0)