youssefreda9 commited on
Commit
3130613
·
1 Parent(s): 014e447

Add test suite for FIX-33 through FIX-37 (29 tests)

Browse files
Files changed (1) hide show
  1. tests/test_recent_fixes.py +279 -0
tests/test_recent_fixes.py ADDED
@@ -0,0 +1,279 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Test suite for FIX-33 through FIX-37.
3
+ Run from project root: python tests/test_recent_fixes.py
4
+
5
+ Tests that need camel-tools (ArabicGrammarGuard) test the regex logic
6
+ directly without instantiating the full class.
7
+ """
8
+ import sys
9
+ import os
10
+ import re
11
+
12
+ # Add src to path
13
+ sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', 'src'))
14
+
15
+ PASS = 0
16
+ FAIL = 0
17
+
18
+ def test(name, condition, detail=""):
19
+ global PASS, FAIL
20
+ if condition:
21
+ PASS += 1
22
+ print(f" ✅ {name}")
23
+ else:
24
+ FAIL += 1
25
+ print(f" ❌ {name} — {detail}")
26
+
27
+
28
+ # ══════════════════════════════════════════════════════════════
29
+ # TEST 1: FIX-33 — Grammar rules don't corrupt الامتحان
30
+ # Test the regex logic directly without camel-tools
31
+ # ══════════════════════════════════════════════════════════════
32
+ print("\n═══ FIX-33: Grammar rules — preposition + root noun protection ═══")
33
+
34
+ # Import the blocklist and test the regex callback logic
35
+ from nlp.grammar import grammar_rules
36
+
37
+ # Get the blocklist from the class (class-level attribute)
38
+ _PREP_BLOCKLIST = grammar_rules.ArabicGrammarGuard._PREP_BLOCKLIST
39
+
40
+ # Simulate the fix_prepositions_advanced regex with callback
41
+ def _prep_replace(m):
42
+ prep = m.group(1)
43
+ stem = m.group(2)
44
+ suffix = m.group(3)
45
+ full_word = stem + suffix
46
+ if full_word in _PREP_BLOCKLIST:
47
+ return m.group(0)
48
+ if stem.startswith('ال') and suffix == 'ان':
49
+ return m.group(0)
50
+ return f'{prep} {stem}ين'
51
+
52
+ def fix_prepositions(text):
53
+ return re.sub(
54
+ r'\b([وف]?(?:في|من|إلى|على|عن|حتى))\s+([أ-ي]{4,})(ون|ان)\b',
55
+ _prep_replace, text
56
+ )
57
+
58
+ # Root nouns — should NOT be corrupted
59
+ root_nouns = [
60
+ ("إلى الامتحان", "إلى الامتحان"),
61
+ ("من الإنسان", "من الإنسان"),
62
+ ("في الميدان", "في الميدان"),
63
+ ("على المكان", "على المكان"),
64
+ ("عن السلطان", "عن السلطان"),
65
+ ("إلى البرلمان", "إلى البرلمان"),
66
+ ("في الحيوان", "في الحيوان"),
67
+ ("من القرآن", "من القرآن"),
68
+ ("في الزمان", "في الزمان"),
69
+ ]
70
+ for input_text, expected in root_nouns:
71
+ result = fix_prepositions(input_text)
72
+ test(f"'{input_text}' → unchanged", result == expected, f"got '{result}'")
73
+
74
+ # Actual plurals — SHOULD be corrected
75
+ plurals = [
76
+ ("في المهندسون", "في المهندسين"),
77
+ ("من المعلمون", "من المعلمين"),
78
+ ]
79
+ for input_text, expected in plurals:
80
+ result = fix_prepositions(input_text)
81
+ test(f"'{input_text}' → '{expected}'", result == expected, f"got '{result}'")
82
+
83
+
84
+ # ══════════════════════════════════════════════════════════════
85
+ # TEST 2: FIX-35 — Spelling doesn't strip conjugation suffixes
86
+ # ══════════════════════════════════════════════════════════════
87
+ print("\n═══ FIX-35: Spelling — conjugation suffix protection ═══")
88
+
89
+ _CONJUGATION_SUFFIXES = {'ن', 'ت', 'ا', 'ي', 'ة', 'و', 'ه'}
90
+
91
+ def simulate_insertion_fix_check(orig_word, corr_word):
92
+ """Simulate the FIX-35 suffix strip check logic from app.py."""
93
+ if len(orig_word) != len(corr_word) + 1:
94
+ return "not_applicable"
95
+
96
+ for di in range(len(orig_word)):
97
+ candidate = orig_word[:di] + orig_word[di + 1:]
98
+ if candidate == corr_word:
99
+ removed_char = orig_word[di]
100
+ removed_pos = di
101
+ if (removed_char in _CONJUGATION_SUFFIXES
102
+ and removed_pos == len(orig_word) - 1
103
+ and len(corr_word) >= 3):
104
+ return "blocked"
105
+ return "allowed"
106
+ return "not_applicable"
107
+
108
+ # BLOCKED cases (suffix stripping)
109
+ test("'ذهبن'→'ذهب' blocked (ن suffix)",
110
+ simulate_insertion_fix_check("ذهبن", "ذهب") == "blocked",
111
+ f"got {simulate_insertion_fix_check('ذهبن', 'ذهب')}")
112
+
113
+ test("'كتبت'→'كتب' blocked (ت suffix)",
114
+ simulate_insertion_fix_check("كتبت", "كتب") == "blocked",
115
+ f"got {simulate_insertion_fix_check('كتبت', 'كتب')}")
116
+
117
+ test("'درسة'→'درس' blocked (ة suffix)",
118
+ simulate_insertion_fix_check("درسة", "درس") == "blocked",
119
+ f"got {simulate_insertion_fix_check('درسة', 'درس')}")
120
+
121
+ test("'جلسوا'→'جلسو' not applicable (len diff ok but وا→و)",
122
+ simulate_insertion_fix_check("جلسوا", "جلسو") == "blocked",
123
+ f"got {simulate_insertion_fix_check('��لسوا', 'جلسو')}")
124
+
125
+ # ALLOWED cases (mid-word insertion fix)
126
+ test("'الكتتاب'→'الكتاب' allowed (mid-word extra ت)",
127
+ simulate_insertion_fix_check("الكتتاب", "الكتاب") == "allowed",
128
+ f"got {simulate_insertion_fix_check('الكتتاب', 'الكتاب')}")
129
+
130
+ test("'الصصف'→'الصف' allowed (mid-word extra ص)",
131
+ simulate_insertion_fix_check("الصصف", "الصف") == "allowed",
132
+ f"got {simulate_insertion_fix_check('الصصف', 'الصف')}")
133
+
134
+
135
+ # ══════════════════════════════════════════════════════════════
136
+ # TEST 3: FIX-36 — Overlap resolver merges grammar+punctuation
137
+ # ══════════════════════════════════════════════════════════════
138
+ print("\n═══ FIX-36: Overlap resolver — grammar+punctuation merge ═══")
139
+
140
+ from nlp.correction_patch import CorrectionPatch, PatchSet, PRIORITY
141
+
142
+ # Case 1: grammar + matching punctuation → merge
143
+ ps = PatchSet()
144
+ grammar_patch = CorrectionPatch(
145
+ stage='grammar',
146
+ start_original=18, end_original=26,
147
+ start_current=18, end_current=26,
148
+ original='المعلمون', replacement='المعلمين',
149
+ priority=PRIORITY['grammar'], confidence=0.9
150
+ )
151
+ punc_patch = CorrectionPatch(
152
+ stage='punctuation',
153
+ start_original=18, end_original=26,
154
+ start_current=18, end_current=26,
155
+ original='المعلمون', replacement='المعلمين.',
156
+ priority=PRIORITY['punctuation'], confidence=0.8
157
+ )
158
+ ps.add(grammar_patch)
159
+ ps.add(punc_patch)
160
+ resolved = ps.resolve_overlaps()
161
+
162
+ test("Case 1: 1 patch after merge", len(resolved) == 1, f"got {len(resolved)}")
163
+ if resolved:
164
+ test("Merged replacement = 'المعلمين.'",
165
+ resolved[0].replacement == 'المعلمين.',
166
+ f"got '{resolved[0].replacement}'")
167
+ test("Merged stage = grammar",
168
+ resolved[0].stage == 'grammar',
169
+ f"got '{resolved[0].stage}'")
170
+
171
+ # Case 2: Non-matching corrections → drop punctuation
172
+ ps2 = PatchSet()
173
+ ps2.add(CorrectionPatch(
174
+ stage='grammar', start_original=0, end_original=5,
175
+ start_current=0, end_current=5,
176
+ original='ذهبوا', replacement='ذهبن',
177
+ priority=PRIORITY['grammar'], confidence=0.9
178
+ ))
179
+ ps2.add(CorrectionPatch(
180
+ stage='punctuation', start_original=0, end_original=5,
181
+ start_current=0, end_current=5,
182
+ original='ذهبوا', replacement='ذهبوا.',
183
+ priority=PRIORITY['punctuation'], confidence=0.8
184
+ ))
185
+ resolved2 = ps2.resolve_overlaps()
186
+ test("Case 2: punc dropped (non-matching)", len(resolved2) == 1, f"got {len(resolved2)}")
187
+ if resolved2:
188
+ test("Kept grammar unchanged 'ذهبن'",
189
+ resolved2[0].replacement == 'ذهبن',
190
+ f"got '{resolved2[0].replacement}'")
191
+
192
+ # Case 3: spelling + punctuation still coexist (Phase 14)
193
+ ps3 = PatchSet()
194
+ ps3.add(CorrectionPatch(
195
+ stage='spelling', start_original=0, end_original=5,
196
+ start_current=0, end_current=5,
197
+ original='برفم', replacement='برغم',
198
+ priority=PRIORITY['spelling'], confidence=0.9
199
+ ))
200
+ ps3.add(CorrectionPatch(
201
+ stage='punctuation', start_original=0, end_original=5,
202
+ start_current=0, end_current=5,
203
+ original='برفم', replacement='برفم.',
204
+ priority=PRIORITY['punctuation'], confidence=0.8
205
+ ))
206
+ resolved3 = ps3.resolve_overlaps()
207
+ test("Case 3: spelling+punc coexist (2 patches)",
208
+ len(resolved3) == 2, f"got {len(resolved3)}")
209
+
210
+
211
+ # ══════════════════════════════════════════════════════════════
212
+ # TEST 4: FIX-34 — No auto re-analysis after Apply All
213
+ # ══════════════════════════════════════════════════════════════
214
+ print("\n═══ FIX-34: Frontend — no auto re-analysis after Apply All ═══")
215
+
216
+ editor_js_path = os.path.join(os.path.dirname(__file__), '..', 'src', 'js', 'editor.js')
217
+ with open(editor_js_path, 'r', encoding='utf-8') as f:
218
+ editor_content = f.read()
219
+
220
+ # Find applyAllSuggestions function body
221
+ fn_match = re.search(
222
+ r'function applyAllSuggestions\b(.*?\n\})',
223
+ editor_content, re.DOTALL
224
+ )
225
+ if fn_match:
226
+ fn_body = fn_match.group(1)
227
+ has_auto_analyze = bool(re.search(r'analyzeText\s*\(', fn_body))
228
+ test("applyAllSuggestions does NOT call analyzeText()",
229
+ not has_auto_analyze,
230
+ "Found analyzeText() call inside applyAllSuggestions!")
231
+
232
+ has_fix34_comment = 'FIX-34' in fn_body
233
+ test("FIX-34 comment present in function",
234
+ has_fix34_comment, "Missing FIX-34 comment")
235
+ else:
236
+ test("applyAllSuggestions function found", False, "Could not find function")
237
+
238
+
239
+ # ════════════════════════��═════════════════════════════════════
240
+ # TEST 5: FIX-37 — Terminal period fallback exists in app.py
241
+ # ══════════════════════════════════════════════════════════════
242
+ print("\n═══ FIX-37: Terminal period fallback in app.py ═══")
243
+
244
+ app_py_path = os.path.join(os.path.dirname(__file__), '..', 'src', 'app.py')
245
+ with open(app_py_path, 'r', encoding='utf-8') as f:
246
+ app_content = f.read()
247
+
248
+ test("FIX-37 comment exists in app.py",
249
+ 'FIX-37' in app_content,
250
+ "Missing FIX-37 marker")
251
+
252
+ test("PUNC-FALLBACK log message exists",
253
+ 'PUNC-FALLBACK' in app_content,
254
+ "Missing PUNC-FALLBACK log")
255
+
256
+ test("Terminal period injection code exists",
257
+ "_lw_text + '.'" in app_content,
258
+ "Missing terminal period injection")
259
+
260
+ # Verify indentation is correct (12-space, not 14)
261
+ for line in app_content.split('\n'):
262
+ if 'FIX-37' in line:
263
+ spaces = len(line) - len(line.lstrip())
264
+ test(f"FIX-37 at correct indent (12 spaces)",
265
+ spaces == 12,
266
+ f"got {spaces} spaces")
267
+ break
268
+
269
+
270
+ # ══════════════════════════════════════════════════════════════
271
+ # SUMMARY
272
+ # ══════════════════════════════════════════════════════════════
273
+ print(f"\n{'═'*60}")
274
+ if FAIL == 0:
275
+ print(f" ✅ ALL {PASS} TESTS PASSED")
276
+ else:
277
+ print(f" ❌ {FAIL} FAILED, {PASS} passed out of {PASS+FAIL} tests")
278
+ print(f"{'═'*60}")
279
+ sys.exit(1 if FAIL > 0 else 0)