forked from Ikalus1988/MisakaNet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_quality_scorer.py
More file actions
267 lines (223 loc) 路 8.45 KB
/
Copy pathtest_quality_scorer.py
File metadata and controls
267 lines (223 loc) 路 8.45 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
#!/usr/bin/env python3
"""Tests for scripts/quality_scorer.py -- 100-point rubric implementation."""
import json
import tempfile
import unittest
from pathlib import Path
REPO = Path(__file__).resolve().parent.parent
sys_path_backup = None
import sys
sys.path.insert(0, str(REPO))
from scripts.quality_scorer import (
extract_frontmatter,
get_body,
find_sections,
word_count,
score_metadata,
score_structure,
score_content,
score_dedup,
score_source_trust,
score_lesson,
)
FIXTURES = Path(__file__).resolve().parent / "fixtures"
def load_fixture(name: str) -> str:
return (FIXTURES / name).read_text(encoding="utf-8")
class TestExtractFrontmatter(unittest.TestCase):
def test_json_frontmatter(self):
content = '---\n{"title": "Test", "domain": "ops"}\n---\nBody'
fm, err = extract_frontmatter(content)
self.assertIsNone(err)
self.assertEqual(fm["title"], "Test")
def test_no_frontmatter(self):
fm, err = extract_frontmatter("No frontmatter here")
self.assertIsNone(fm)
self.assertIn("No frontmatter", err)
def test_invalid_json(self):
content = '---\nnot json\n---\nBody'
fm, err = extract_frontmatter(content)
self.assertTrue(fm is not None or err is not None)
class TestScoreMetadata(unittest.TestCase):
def test_good_metadata(self):
fm = {
"title": "Fix database locked error on concurrent writes",
"domain": "devops",
"tags": ["sqlite", "database", "locking"],
"source": "https://github.com/example/repo",
"created": "2026-07-01",
"confidence": "0.85",
}
pts, notes = score_metadata(fm, "")
self.assertEqual(pts, 20)
self.assertEqual(notes, [])
def test_no_frontmatter(self):
pts, notes = score_metadata(None, "")
self.assertEqual(pts, 0)
def test_short_title(self):
fm = {
"title": "Fix",
"domain": "ops",
"tags": ["a", "b", "c"],
"source": "x",
"created": "2026-01-01",
"confidence": "0.5",
}
pts, notes = score_metadata(fm, "")
self.assertLess(pts, 20)
self.assertTrue(any("Title" in n for n in notes))
def test_missing_tags(self):
fm = {
"title": "A long enough title here",
"domain": "ops",
"source": "x",
"created": "2026-01-01",
"confidence": "0.5",
}
pts, notes = score_metadata(fm, "")
self.assertTrue(any("Tags" in n for n in notes))
class TestScoreStructure(unittest.TestCase):
def test_all_sections_present(self):
body = (
"## Problem\n\nX\n\n## Root Cause\n\nY\n\n"
"## Solution\n\nZ\n\n## Verification\n\nV\n\n## Notes\n\nN"
)
pts, notes = score_structure(body)
self.assertEqual(pts, 25)
self.assertEqual(notes, [])
def test_missing_root_cause(self):
body = "## Problem\n\nX\n\n## Solution\n\nZ\n\n## Verification\n\nV"
pts, notes = score_structure(body)
self.assertLess(pts, 25)
self.assertTrue(any("Root Cause" in n for n in notes))
def test_out_of_order(self):
body = (
"## Solution\n\nZ\n\n## Problem\n\nX\n\n## Root Cause\n\nY\n\n"
"## Verification\n\nV\n\n## Notes\n\nN"
)
pts, notes = score_structure(body)
self.assertTrue(any("order" in n.lower() for n in notes))
class TestScoreContent(unittest.TestCase):
def test_rich_content(self):
content = load_fixture("good_lesson.md")
body = get_body(content)
pts, notes = score_content(body)
self.assertGreaterEqual(pts, 25)
def test_no_code_blocks(self):
body = (
"## Problem\n\nSome text without any code.\n\n"
"## Solution\n\nDo the thing step by step.\n\n"
"## Verification\n\nCheck it works."
)
pts, notes = score_content(body)
self.assertTrue(any("code block" in n.lower() for n in notes))
def test_stub_content(self):
body = "## Problem\n\nShort.\n\n## Solution\n\nFix it."
pts, notes = score_content(body)
self.assertLess(pts, 15)
class TestScoreDedup(unittest.TestCase):
def test_clean_content(self):
content = (
"## Problem\n\nGeneric lesson about CI pipelines.\n\n"
"## Solution\n\nUse GitHub Actions."
)
pts, notes = score_dedup(content)
self.assertEqual(pts, 10)
def test_org_sensitive(self):
pts, notes = score_dedup("This uses Xiaomi mify gateway internally")
self.assertLess(pts, 10)
self.assertTrue(any("org-sensitive" in n.lower() for n in notes))
def test_hardcoded_paths(self):
pts, notes = score_dedup("Config is at /Users/eric/.config/app.json")
self.assertTrue(any("hardcoded" in n.lower() for n in notes))
def test_self_similarity_excluded(self):
all_docs = [("test-lesson.md", {"fix", "database", "locked"})]
pts, notes = score_dedup(
"fix database locked error",
all_docs,
current_file="lessons/contrib/test-lesson.md",
)
self.assertEqual(pts, 10)
class TestScoreSourceTrust(unittest.TestCase):
def test_github_source(self):
fm = {"source": "https://github.com/org/repo/issues/1"}
pts, notes = score_source_trust(fm, "resolved in PR #2")
self.assertGreaterEqual(pts, 6)
def test_no_source(self):
fm = {"source": ""}
pts, notes = score_source_trust(fm, "")
self.assertLess(pts, 5)
def test_verified_expert(self):
fm = {
"source": "https://github.com/org/repo",
"verified_date": "2026-07-01",
"domain_expert": "maintainer",
}
pts, notes = score_source_trust(fm, "verified and merged")
self.assertEqual(pts, 10)
class TestScoreLessonIntegration(unittest.TestCase):
def _write_fixture(self, name: str) -> Path:
content = load_fixture(name)
tmp = tempfile.NamedTemporaryFile(suffix=".md", delete=False, mode="w")
tmp.write(content)
tmp.close()
return Path(tmp.name)
def test_good_lesson_passes(self):
path = self._write_fixture("good_lesson.md")
try:
r = score_lesson(path)
self.assertGreaterEqual(r["score"], 75)
self.assertTrue(r["pass"])
self.assertIn(r["grade"], ("A", "B"))
finally:
path.unlink()
def test_bad_lesson_fails(self):
path = self._write_fixture("bad_lesson.md")
try:
r = score_lesson(path)
self.assertLess(r["score"], 60)
self.assertFalse(r["pass"])
finally:
path.unlink()
def test_no_frontmatter_scores_zero_metadata(self):
path = self._write_fixture("no_frontmatter.md")
try:
r = score_lesson(path)
self.assertEqual(r["breakdown"]["metadata"]["score"], 0)
finally:
path.unlink()
def test_placeholder_detected(self):
path = self._write_fixture("placeholder_lesson.md")
try:
r = score_lesson(path)
content_notes = r["breakdown"]["content"]["notes"]
self.assertTrue(
any("TODO" in n or "placeholder" in n.lower() for n in content_notes)
)
finally:
path.unlink()
def test_org_sensitive_flagged(self):
path = self._write_fixture("org_sensitive_lesson.md")
try:
r = score_lesson(path)
dedup_notes = r["breakdown"]["dedup"]["notes"]
self.assertTrue(any("org-sensitive" in n.lower() for n in dedup_notes))
finally:
path.unlink()
def test_json_output_structure(self):
path = self._write_fixture("good_lesson.md")
try:
r = score_lesson(path)
self.assertIn("file", r)
self.assertIn("score", r)
self.assertIn("grade", r)
self.assertIn("pass", r)
self.assertIn("breakdown", r)
for dim in ("metadata", "structure", "content", "dedup", "trust"):
self.assertIn(dim, r["breakdown"])
self.assertIn("score", r["breakdown"][dim])
self.assertIn("max", r["breakdown"][dim])
self.assertIn("notes", r["breakdown"][dim])
finally:
path.unlink()
if __name__ == "__main__":
unittest.main()