@jesdi/skills 0.0.101 → 0.0.102

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. package/package.json +1 -1
  2. package/skills/babysit-pr/SKILL.md +9 -2
  3. package/skills/babysit-pr/babysit.py +98 -36
  4. package/skills/babysit-pr/exam.py +156 -0
  5. package/skills/babysit-pr/fixtures/exam_host.py +47 -0
  6. package/skills/babysit-pr/fixtures/gh +107 -11
  7. package/skills/babysit-pr/test_babysit.py +8 -2
  8. package/skills/babysit-pr/test_e2e_setup.py +1 -1
  9. package/skills/babysit-pr/test_exam_merge_gate.py +189 -0
  10. package/skills/babysit-pr/test_exam_request.py +337 -0
  11. package/skills/babysit-pr/test_exam_required_check.py +145 -0
  12. package/skills/babysit-pr/test_exam_wait.py +353 -0
  13. package/skills/babysit-pr/test_merge_and_handoff.py +12 -6
  14. package/skills/babysit-pr/test_trusted_merge_risk.py +2 -2
  15. package/skills/pr-exam/SKILL.md +131 -0
  16. package/skills/pr-exam/fixtures/README.md +20 -0
  17. package/skills/pr-exam/fixtures/delta-new-concept/body.md +9 -0
  18. package/skills/pr-exam/fixtures/delta-new-concept/diff.patch +82 -0
  19. package/skills/pr-exam/fixtures/delta-new-concept/exam.json +46 -0
  20. package/skills/pr-exam/fixtures/delta-new-concept/passed.diff +66 -0
  21. package/skills/pr-exam/fixtures/delta-new-concept/records/0001-single-sync-leader.md +10 -0
  22. package/skills/pr-exam/fixtures/delta-new-concept/records/0002-stale-leader-writes.md +10 -0
  23. package/skills/pr-exam/fixtures/delta-new-concept/records/0003-fencing-token.md +10 -0
  24. package/skills/pr-exam/fixtures/delta-new-concept/records/0004-lease-expiry.md +10 -0
  25. package/skills/pr-exam/fixtures/delta-new-concept/records/0005-injected-monotonic-clock.md +10 -0
  26. package/skills/pr-exam/fixtures/delta-new-concept/records/0006-leader-lifecycle.md +10 -0
  27. package/skills/pr-exam/fixtures/delta-new-concept/records/0007-lease-rollback.md +10 -0
  28. package/skills/pr-exam/fixtures/delta-new-concept/records/0008-sync-blast-radius.md +10 -0
  29. package/skills/pr-exam/fixtures/delta-new-concept/request.json +43 -0
  30. package/skills/pr-exam/fixtures/delta-nothing-new/body.md +9 -0
  31. package/skills/pr-exam/fixtures/delta-nothing-new/diff.patch +66 -0
  32. package/skills/pr-exam/fixtures/delta-nothing-new/exam.json +10 -0
  33. package/skills/pr-exam/fixtures/delta-nothing-new/passed.diff +66 -0
  34. package/skills/pr-exam/fixtures/delta-nothing-new/records/0001-single-sync-leader.md +10 -0
  35. package/skills/pr-exam/fixtures/delta-nothing-new/records/0002-stale-leader-writes.md +10 -0
  36. package/skills/pr-exam/fixtures/delta-nothing-new/records/0003-fencing-token.md +10 -0
  37. package/skills/pr-exam/fixtures/delta-nothing-new/records/0004-lease-expiry.md +10 -0
  38. package/skills/pr-exam/fixtures/delta-nothing-new/records/0005-injected-monotonic-clock.md +10 -0
  39. package/skills/pr-exam/fixtures/delta-nothing-new/records/0006-leader-lifecycle.md +10 -0
  40. package/skills/pr-exam/fixtures/delta-nothing-new/records/0007-lease-rollback.md +10 -0
  41. package/skills/pr-exam/fixtures/delta-nothing-new/records/0008-sync-blast-radius.md +10 -0
  42. package/skills/pr-exam/fixtures/delta-nothing-new/request.json +43 -0
  43. package/skills/pr-exam/fixtures/followup/body.md +9 -0
  44. package/skills/pr-exam/fixtures/followup/diff.patch +66 -0
  45. package/skills/pr-exam/fixtures/followup/exam.json +61 -0
  46. package/skills/pr-exam/fixtures/followup/records/0001-single-sync-leader.md +10 -0
  47. package/skills/pr-exam/fixtures/followup/records/0002-stale-leader-writes.md +10 -0
  48. package/skills/pr-exam/fixtures/followup/records/0003-fencing-token.md +10 -0
  49. package/skills/pr-exam/fixtures/followup/records/0004-lease-expiry.md +10 -0
  50. package/skills/pr-exam/fixtures/followup/records/0005-injected-monotonic-clock.md +10 -0
  51. package/skills/pr-exam/fixtures/followup/records/0006-lease-rollback.md +10 -0
  52. package/skills/pr-exam/fixtures/followup/records/0007-sync-blast-radius.md +10 -0
  53. package/skills/pr-exam/fixtures/followup/request.json +41 -0
  54. package/skills/pr-exam/fixtures/grading/answers.json +5 -0
  55. package/skills/pr-exam/fixtures/grading/attempt.json +10 -0
  56. package/skills/pr-exam/fixtures/grading/grades.json +14 -0
  57. package/skills/pr-exam/fixtures/grading/questions.json +26 -0
  58. package/skills/pr-exam/fixtures/grading/request.json +10 -0
  59. package/skills/pr-exam/fixtures/twelve-concepts/body.md +9 -0
  60. package/skills/pr-exam/fixtures/twelve-concepts/diff.patch +66 -0
  61. package/skills/pr-exam/fixtures/twelve-concepts/exam.json +195 -0
  62. package/skills/pr-exam/fixtures/twelve-concepts/request.json +15 -0
  63. package/skills/pr-exam/fixtures/typo-readme/body.md +1 -0
  64. package/skills/pr-exam/fixtures/typo-readme/diff.patch +7 -0
  65. package/skills/pr-exam/fixtures/typo-readme/exam.json +30 -0
  66. package/skills/pr-exam/fixtures/typo-readme/request.json +15 -0
  67. package/skills/pr-exam/pr_exam.py +705 -0
  68. package/skills/pr-exam/test_config.py +83 -0
  69. package/skills/pr-exam/test_exam_format.py +252 -0
  70. package/skills/pr-exam/test_followup_delta.py +90 -0
  71. package/skills/pr-exam/test_learning_records.py +267 -0
  72. package/skills/pr-exam/test_record_publication.py +45 -0
  73. package/skills/pr-exam/test_scoring.py +121 -0
  74. package/skills/pr-exam/test_served_shapes.py +38 -0
  75. package/skills/pr-exam/test_setup.py +349 -0
  76. package/skills/pr-exam/test_skill_fixtures.py +88 -0
  77. package/skills-manifest.json +9 -3
@@ -0,0 +1,267 @@
1
+ """Acceptance tests: `pr_exam.py learned`, `record` and `validate` with records, run as subprocesses.
2
+
3
+ Interface defined here:
4
+ - `learned --records <dir> --today <date>` prints a JSON object {concept: "skip"|"due"|"misconception"}
5
+ for the active record of each concept; a missing or empty dir prints {}; a bad record exits 1 with the
6
+ file name on stderr.
7
+ - `record --records <dir> --exam <exam.json> --result <result.json> --today <date>` writes
8
+ `NNNN-<concept>.md` (frontmatter concept/state/date/source/status) for each question of 1 point
9
+ (demonstrated) or 0 points (misconception), numbered after the highest file, and marks the concept's
10
+ earlier active record `status: superseded by LR-NNNN`.
11
+ Spec specs/pr-exam: requirement 8; design.md "Learning record".
12
+ """
13
+ import json
14
+ import re
15
+ import subprocess
16
+ import sys
17
+ from pathlib import Path
18
+
19
+ SCRIPT = Path(__file__).parent / "pr_exam.py"
20
+ SHA = "a" * 40
21
+ DIFF = "diff --git a/lease.py b/lease.py\n+def take_lease(): ...\n"
22
+ TODAY = "2026-11-18"
23
+ D29, D30, WEEK = "2026-10-20", "2026-10-19", "2026-11-11"
24
+
25
+
26
+ def cli(*args):
27
+ return subprocess.run([sys.executable, str(SCRIPT), *map(str, args)], capture_output=True, text=True)
28
+
29
+
30
+ def write_record(records, n, concept, state, date, status="active"):
31
+ records.mkdir(exist_ok=True)
32
+ (records / f"{n:04d}-{concept}.md").write_text(
33
+ f"---\nconcept: {concept}\nstate: {state}\ndate: {date}\nsource: jesdi/general-skills#1\n"
34
+ f"status: {status}\n---\n# {concept}\n\nText.\n")
35
+
36
+
37
+ def learned(records, today=TODAY):
38
+ return cli("learned", "--records", records, "--today", today)
39
+
40
+
41
+ def learned_ok(records, today=TODAY):
42
+ r = learned(records, today)
43
+ assert r.returncode == 0, (r.returncode, r.stdout, r.stderr)
44
+ return json.loads(r.stdout)
45
+
46
+
47
+ def exam_with(questions):
48
+ """questions: [(concept, type, kind)]"""
49
+ qs, key = [], {}
50
+ for i, (c, t, _) in enumerate(questions, 1):
51
+ q = {"id": f"q{i}", "concept": c, "type": t, "prompt": f"Prompt {i}?"}
52
+ k = {"explanation": f"Explain {i}.", "hunk": "+def take_lease(): ..."}
53
+ if t == "choice":
54
+ q["options"] = ["alpha beta", "gamma delta", "zeta eta"]
55
+ k["answer"] = 1
56
+ else:
57
+ k["rubric"] = {"pass": "Names it.", "partial": "Mentions it."}
58
+ qs.append(q)
59
+ key[q["id"]] = k
60
+ seen = {}
61
+ for c, _, kind in questions:
62
+ seen.setdefault(c, kind)
63
+ return {"version": 1, "mode": "first", "repo": "jesdi/general-skills", "pr": 240, "head_sha": SHA,
64
+ "concepts": [{"id": c, "title": c, "kind": k} for c, k in seen.items()],
65
+ "questions": qs, "key": key}
66
+
67
+
68
+ def validate(tmp_path, records, exam, extra_concepts=()):
69
+ exam["concepts"] += [{"id": c, "title": c, "kind": k} for c, k in extra_concepts]
70
+ (tmp_path / "diff.patch").write_text(DIFF)
71
+ (tmp_path / "body.md").write_text("body")
72
+ (tmp_path / "exam.json").write_text(json.dumps(exam))
73
+ request = {"mode": "first", "repo": "jesdi/general-skills", "pr": 240, "head_sha": SHA,
74
+ "diff": str(tmp_path / "diff.patch"), "body": str(tmp_path / "body.md"), "glossary": None,
75
+ "records": str(records), "today": TODAY, "only_concepts": [], "prior_concepts": [],
76
+ "prior_prompts": [], "out": str(tmp_path / "exam.json")}
77
+ (tmp_path / "request.json").write_text(json.dumps(request))
78
+ return cli("validate", "--request", tmp_path / "request.json", tmp_path / "exam.json")
79
+
80
+
81
+ def record(tmp_path, records, points, today=TODAY):
82
+ """points: {concept: points}; one text question per concept."""
83
+ exam = exam_with([(c, "text", "concept") for c in points])
84
+ result = {"passed": False, "score": 0, "max": len(points),
85
+ "questions": [{"id": f"q{i}", "concept": c, "points": p}
86
+ for i, (c, p) in enumerate(points.items(), 1)],
87
+ "missed": [c for c, p in points.items() if p < 1]}
88
+ (tmp_path / "rexam.json").write_text(json.dumps(exam))
89
+ (tmp_path / "result.json").write_text(json.dumps(result))
90
+ r = cli("record", "--records", records, "--exam", tmp_path / "rexam.json",
91
+ "--result", tmp_path / "result.json", "--today", today)
92
+ assert r.returncode == 0, (r.returncode, r.stdout, r.stderr)
93
+
94
+
95
+ def field(path, name):
96
+ m = re.search(rf"^{name}: (.*)$", path.read_text().split("---")[1], re.M)
97
+ return m.group(1).strip() if m else None
98
+
99
+
100
+ def test_record_writes_one_file_per_one_or_zero_point_question_after_the_highest(tmp_path):
101
+ records = tmp_path / "records"
102
+ write_record(records, 5, "older", "demonstrated", D29)
103
+ record(tmp_path, records, {"merge-lease": 1, "base-sha-recheck": 0, "half-credit": 0.5})
104
+ assert sorted(p.name for p in records.glob("*.md") if not p.name.startswith("0005")) == [
105
+ "0006-merge-lease.md", "0007-base-sha-recheck.md"]
106
+ lease, base = records / "0006-merge-lease.md", records / "0007-base-sha-recheck.md"
107
+ assert (field(lease, "concept"), field(lease, "state"), field(lease, "date")) == (
108
+ "merge-lease", "demonstrated", TODAY)
109
+ assert (field(base, "state"), field(base, "date")) == ("misconception", TODAY)
110
+
111
+
112
+ def test_second_record_supersedes_the_first_and_learned_reports_the_new_state(tmp_path):
113
+ records = tmp_path / "records"
114
+ record(tmp_path, records, {"merge-lease": 0}, today="2026-09-01")
115
+ assert learned_ok(records) == {"merge-lease": "misconception"}
116
+ record(tmp_path, records, {"merge-lease": 1})
117
+ first = records / "0001-merge-lease.md"
118
+ assert field(first, "status") == "superseded by LR-0002"
119
+ assert field(records / "0002-merge-lease.md", "status") == "active"
120
+ assert learned_ok(records) == {"merge-lease": "skip"}
121
+
122
+
123
+ def test_learned_skip_at_29_days_due_at_30_misconception_at_any_age(tmp_path):
124
+ records = tmp_path / "records"
125
+ write_record(records, 1, "merge-lease", "demonstrated", D29)
126
+ assert learned_ok(records) == {"merge-lease": "skip"}
127
+ write_record(records, 2, "merge-lease", "demonstrated", D30)
128
+ (records / "0001-merge-lease.md").unlink()
129
+ assert learned_ok(records) == {"merge-lease": "due"}
130
+ (records / "0002-merge-lease.md").unlink()
131
+ write_record(records, 3, "merge-lease", "misconception", "2020-01-01")
132
+ assert learned_ok(records) == {"merge-lease": "misconception"}
133
+ (records / "0003-merge-lease.md").unlink()
134
+ write_record(records, 4, "merge-lease", "misconception", TODAY)
135
+ assert learned_ok(records) == {"merge-lease": "misconception"}
136
+
137
+
138
+ def test_exam_on_a_skipped_concept_or_missing_a_misconception_is_refused(tmp_path):
139
+ records = tmp_path / "records"
140
+ write_record(records, 1, "merge-lease", "demonstrated", WEEK)
141
+ write_record(records, 2, "base-sha-recheck", "misconception", WEEK)
142
+ good = exam_with([("base-sha-recheck", "choice", "architecture"), ("other", "text", "why")])
143
+ r = validate(tmp_path, records, good, [("merge-lease", "concept")])
144
+ assert r.returncode == 0, r.stderr
145
+ asks_lease = exam_with([("merge-lease", "choice", "concept"), ("other", "text", "why")])
146
+ r = validate(tmp_path, records, asks_lease, [("base-sha-recheck", "architecture")])
147
+ assert r.returncode == 1 and "merge-lease" in r.stderr, (r.returncode, r.stderr)
148
+ no_base = exam_with([("other", "text", "why")])
149
+ r = validate(tmp_path, records, no_base, [("merge-lease", "concept"), ("base-sha-recheck", "architecture")])
150
+ assert r.returncode == 1 and "base-sha-recheck" in r.stderr, (r.returncode, r.stderr)
151
+
152
+
153
+ def test_due_concept_validates_as_choice_and_is_refused_as_text(tmp_path):
154
+ records = tmp_path / "records"
155
+ write_record(records, 1, "merge-lease", "demonstrated", D30)
156
+ ok = exam_with([("merge-lease", "choice", "concept"), ("other", "text", "why")])
157
+ r = validate(tmp_path, records, ok)
158
+ assert r.returncode == 0, r.stderr
159
+ bad = exam_with([("merge-lease", "text", "concept"), ("other", "choice", "why")])
160
+ r = validate(tmp_path, records, bad)
161
+ assert r.returncode == 1 and "merge-lease" in r.stderr, (r.returncode, r.stderr)
162
+
163
+
164
+ def test_any_question_on_a_concept_demonstrated_29_days_ago_is_refused(tmp_path):
165
+ records = tmp_path / "records"
166
+ write_record(records, 1, "merge-lease", "demonstrated", D29)
167
+ assert validate(tmp_path, records, exam_with([("other", "text", "why")]),
168
+ [("merge-lease", "concept")]).returncode == 0
169
+ for t in ("choice", "text"):
170
+ other = "text" if t == "choice" else "choice"
171
+ r = validate(tmp_path, records, exam_with([("merge-lease", t, "concept"), ("other", other, "why")]))
172
+ assert r.returncode == 1 and "merge-lease" in r.stderr, (t, r.returncode, r.stderr)
173
+
174
+
175
+ def test_missing_or_empty_records_directory_reads_as_no_records(tmp_path):
176
+ assert learned_ok(tmp_path / "nowhere") == {}
177
+ (tmp_path / "empty").mkdir()
178
+ assert learned_ok(tmp_path / "empty") == {}
179
+
180
+
181
+ def test_record_with_missing_or_malformed_field_is_an_error_naming_the_file(tmp_path):
182
+ for i, bad in enumerate(["date: 2026-10-20\n", "state: demonstrated\ndate: yesterday\n",
183
+ "state: learned\ndate: 2026-10-20\n"], 1):
184
+ records = tmp_path / f"r{i}"
185
+ records.mkdir()
186
+ name = "0001-merge-lease.md"
187
+ head = "concept: merge-lease\n" + bad + "source: jesdi/general-skills#1\nstatus: active\n"
188
+ (records / name).write_text(f"---\n{head}---\n# x\n")
189
+ r = learned(records)
190
+ assert r.returncode == 1 and name in r.stderr, (bad, r.returncode, r.stdout, r.stderr)
191
+
192
+
193
+ def concurrent_records(tmp_path, records, concepts):
194
+ """Release ready workers together; each execs the public CLI without instrumenting it."""
195
+ wrapper = ("import os, sys\nprint('ready', flush=True)\nsys.stdin.readline()\n"
196
+ "os.execv(sys.executable, [sys.executable, *sys.argv[1:]])\n")
197
+ procs = []
198
+ for i, c in enumerate(concepts):
199
+ d = tmp_path / f"w{i}"
200
+ d.mkdir()
201
+ (d / "exam.json").write_text(json.dumps(exam_with([(c, "text", "concept")])))
202
+ (d / "result.json").write_text(json.dumps({"passed": False, "score": 1, "max": 1, "missed": [],
203
+ "questions": [{"id": "q1", "concept": c, "points": 1}]}))
204
+ procs.append(subprocess.Popen(
205
+ [sys.executable, "-c", wrapper, str(SCRIPT), "record", "--records", str(records),
206
+ "--exam", str(d / "exam.json"), "--result", str(d / "result.json"), "--today", TODAY],
207
+ stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True))
208
+ for p in procs:
209
+ assert p.stdout.readline() == "ready\n"
210
+ for p in procs:
211
+ p.stdin.write("go\n")
212
+ p.stdin.flush()
213
+ completed = [(p, *p.communicate(timeout=30)) for p in procs]
214
+ for p, out, err in completed:
215
+ assert p.returncode == 0, (p.returncode, out, err)
216
+
217
+
218
+ def test_concurrent_record_calls_in_a_shared_directory_keep_every_write_and_unique_numbers(tmp_path):
219
+ for rnd in range(4):
220
+ base = tmp_path / f"round{rnd}"
221
+ base.mkdir()
222
+ records = base / "records"
223
+ concepts = [f"concept-{i}" for i in range(8)]
224
+ concurrent_records(base, records, concepts)
225
+ files = sorted(p.name for p in records.glob("*.md"))
226
+ assert len(files) == 8, (rnd, files)
227
+ assert len({n.split("-")[0] for n in files}) == 8, (rnd, files)
228
+ assert sorted(learned_ok(records)) == sorted(concepts), (rnd, files)
229
+
230
+
231
+ def test_concurrent_record_calls_on_one_concept_leave_exactly_one_active_record(tmp_path):
232
+ for rnd in range(4):
233
+ base = tmp_path / f"round{rnd}"
234
+ base.mkdir()
235
+ records = base / "records"
236
+ write_record(records, 1, "merge-lease", "misconception", "2026-09-01")
237
+ concurrent_records(base, records, ["merge-lease"] * 6)
238
+ files = sorted(records.glob("*.md"))
239
+ assert len(files) == 7, (rnd, [p.name for p in files])
240
+ assert [field(p, "status") for p in files].count("active") == 1, (rnd, [field(p, "status") for p in files])
241
+
242
+
243
+ def test_record_with_a_malformed_status_is_an_error_naming_the_file(tmp_path):
244
+ records = tmp_path / "records"
245
+ write_record(records, 1, "merge-lease", "demonstrated", WEEK, status="superseded")
246
+ r = learned(records)
247
+ assert r.returncode == 1 and "0001-merge-lease.md" in r.stderr, (r.returncode, r.stdout, r.stderr)
248
+
249
+
250
+ def test_a_markdown_file_that_is_not_a_record_is_ignored(tmp_path):
251
+ records = tmp_path / "records"
252
+ write_record(records, 1, "merge-lease", "demonstrated", WEEK)
253
+ (records / "README.md").write_text("# Notes, not a record\n")
254
+ assert learned_ok(records) == {"merge-lease": "skip"}
255
+
256
+
257
+ def test_validate_and_record_refuse_a_malformed_record_naming_the_file(tmp_path):
258
+ records = tmp_path / "records"
259
+ write_record(records, 1, "merge-lease", "learned", WEEK)
260
+ r = validate(tmp_path, records, exam_with([("other", "text", "why")]))
261
+ assert r.returncode == 1 and "0001-merge-lease.md" in r.stderr, (r.returncode, r.stdout, r.stderr)
262
+ (tmp_path / "rexam.json").write_text(json.dumps(exam_with([("merge-lease", "text", "concept")])))
263
+ (tmp_path / "result.json").write_text(json.dumps({"questions": [{"id": "q1", "concept": "merge-lease", "points": 1}]}))
264
+ r = cli("record", "--records", records, "--exam", tmp_path / "rexam.json",
265
+ "--result", tmp_path / "result.json", "--today", TODAY)
266
+ assert r.returncode == 1 and "0001-merge-lease.md" in r.stderr, (r.returncode, r.stdout, r.stderr)
267
+ assert sorted(p.name for p in records.glob("*.md")) == ["0001-merge-lease.md"]
@@ -0,0 +1,45 @@
1
+ """Regression tests: `record` refuses a concept that is not a slug, publishes a successor before it
2
+ supersedes its predecessor, and `learned` counts the newest of two active records.
3
+ Spec specs/pr-exam: requirement 8; design.md "Learning record".
4
+ """
5
+ import json
6
+ import os
7
+
8
+ from test_learning_records import cli, exam_with, field, learned_ok, write_record, TODAY, WEEK
9
+
10
+
11
+ def run_record(tmp_path, records, concept, points):
12
+ (tmp_path / "rexam.json").write_text(json.dumps(exam_with([(concept, "text", "concept")])))
13
+ (tmp_path / "result.json").write_text(json.dumps(
14
+ {"passed": False, "score": points, "max": 1, "missed": [],
15
+ "questions": [{"id": "q1", "concept": concept, "points": points}]}))
16
+ return cli("record", "--records", records, "--exam", tmp_path / "rexam.json",
17
+ "--result", tmp_path / "result.json", "--today", TODAY)
18
+
19
+
20
+ def test_record_refuses_a_concept_that_is_not_a_slug(tmp_path):
21
+ records = tmp_path / "records"
22
+ r = run_record(tmp_path, records, "../escape", 1)
23
+ assert r.returncode == 1 and "slug" in r.stderr, r.stderr
24
+ assert not (tmp_path / "escape.md").exists()
25
+ assert not any(records.glob("*.md"))
26
+
27
+
28
+ def test_failed_successor_write_keeps_the_previous_record_active(tmp_path):
29
+ records = tmp_path / "records"
30
+ write_record(records, 1, "merge-lease", "demonstrated", WEEK)
31
+ (records / ".lock").touch()
32
+ os.chmod(records, 0o555) # existing files stay writable; no new file can be created
33
+ try:
34
+ assert run_record(tmp_path, records, "merge-lease", 0).returncode != 0
35
+ finally:
36
+ os.chmod(records, 0o755)
37
+ assert field(records / "0001-merge-lease.md", "status") == "active"
38
+ assert learned_ok(records) == {"merge-lease": "skip"}
39
+
40
+
41
+ def test_newest_of_two_active_records_counts(tmp_path):
42
+ records = tmp_path / "records"
43
+ write_record(records, 9, "merge-lease", "demonstrated", WEEK)
44
+ write_record(records, 10, "merge-lease", "misconception", TODAY)
45
+ assert learned_ok(records) == {"merge-lease": "misconception"}
@@ -0,0 +1,121 @@
1
+ """Acceptance tests: `pr_exam.py score --exam --answers --grades`, run as a subprocess.
2
+
3
+ Spec specs/pr-exam: requirement 6; design.md "Served projection, grades.json, result.json".
4
+ Exam: q1 text on a why concept, q2 text, q3-q5 choice (key answer 1).
5
+ """
6
+ import json
7
+ import subprocess
8
+ import sys
9
+ from pathlib import Path
10
+
11
+ SCRIPT = Path(__file__).parent / "pr_exam.py"
12
+ SHA = "a" * 40
13
+ CONCEPTS = [("c-why", "why"), ("lease-ownership", "concept"),
14
+ ("head-sha-recheck", "architecture"), ("c-risk", "risk"), ("c-beh", "behaviour")]
15
+ TYPES = ["text", "text", "choice", "choice", "choice"]
16
+
17
+
18
+ def exam():
19
+ qs, key = [], {}
20
+ for i, ((slug, _), t) in enumerate(zip(CONCEPTS, TYPES), 1):
21
+ q = {"id": f"q{i}", "concept": slug, "type": t, "prompt": f"Prompt {i}?"}
22
+ k = {"explanation": f"Explain {i}.", "hunk": f"+hunk {i}"}
23
+ if t == "choice":
24
+ q["options"] = ["alpha beta", "gamma delta", "zeta eta"]
25
+ k["answer"] = 1
26
+ else:
27
+ k["rubric"] = {"pass": "Names it.", "partial": "Mentions it."}
28
+ qs.append(q)
29
+ key[q["id"]] = k
30
+ return {"version": 1, "mode": "first", "repo": "jesdi/general-skills", "pr": 240, "head_sha": SHA,
31
+ "concepts": [{"id": s, "title": s, "kind": k} for s, k in CONCEPTS],
32
+ "questions": qs, "key": key}
33
+
34
+
35
+ def score(tmp_path, right=(3, 4, 5), why="pass", other="partial", answers=None, grades=None):
36
+ """`right`: choice questions answered correctly; the others are answered 0 (wrong)."""
37
+ a = {"q1": "because", "q2": "text", **{f"q{i}": 1 if i in right else 0 for i in (3, 4, 5)}}
38
+ g = {"q1": {"grade": why, "feedback": "f1"}, "q2": {"grade": other, "feedback": "f2"}}
39
+ a = a if answers is None else answers
40
+ g = g if grades is None else grades
41
+ for name, data in (("exam", exam()), ("answers", a), ("grades", g)):
42
+ (tmp_path / f"{name}.json").write_text(json.dumps(data))
43
+ return subprocess.run([sys.executable, str(SCRIPT), "score", "--exam", str(tmp_path / "exam.json"),
44
+ "--answers", str(tmp_path / "answers.json"), "--grades", str(tmp_path / "grades.json")],
45
+ capture_output=True, text=True)
46
+
47
+
48
+ def ok(r):
49
+ assert r.returncode == 0, (r.returncode, r.stdout, r.stderr)
50
+ return json.loads(r.stdout)
51
+
52
+
53
+ def refused(r, *words):
54
+ assert r.returncode == 1, (r.returncode, r.stdout, r.stderr)
55
+ for w in words:
56
+ assert w in r.stderr, (w, r.stderr)
57
+
58
+
59
+ def test_pass_at_90_percent(tmp_path):
60
+ res = ok(score(tmp_path))
61
+ assert (res["score"], res["max"], res["passed"]) == (4.5, 5, True)
62
+
63
+
64
+ def test_pass_at_exactly_80_percent(tmp_path):
65
+ res = ok(score(tmp_path, other="fail"))
66
+ assert (res["score"], res["passed"]) == (4, True)
67
+
68
+
69
+ def test_fail_at_70_percent(tmp_path):
70
+ res = ok(score(tmp_path, right=(3, 4), why="pass", other="partial"))
71
+ assert (res["score"], res["passed"]) == (3.5, False)
72
+
73
+
74
+ def test_fail_when_why_is_partial_above_80_percent(tmp_path):
75
+ res = ok(score(tmp_path, why="partial", other="pass"))
76
+ assert (res["score"], res["passed"]) == (4.5, False)
77
+
78
+
79
+ def test_points_per_question(tmp_path):
80
+ res = ok(score(tmp_path, right=(3,), why="partial", other="fail"))
81
+ assert {q["id"]: q["points"] for q in res["questions"]} == {"q1": 0.5, "q2": 0, "q3": 1, "q4": 0, "q5": 0}
82
+
83
+
84
+ def test_full_points_carry_no_explanation(tmp_path):
85
+ res = ok(score(tmp_path, right=(3, 5), why="pass", other="pass"))
86
+ assert res["missed"] == ["c-risk"]
87
+ qs = {q["id"]: q for q in res["questions"]}
88
+ for qid in ("q1", "q2", "q3", "q5"):
89
+ assert "explanation" not in qs[qid] and "hunk" not in qs[qid]
90
+ assert (qs["q4"]["explanation"], qs["q4"]["hunk"]) == ("Explain 4.", "+hunk 4")
91
+
92
+
93
+ def test_two_named_misses(tmp_path):
94
+ # q2 (lease-ownership) failed, q3 (head-sha-recheck) wrong; everything else right.
95
+ res = ok(score(tmp_path, right=(4, 5), why="pass", other="fail"))
96
+ assert sorted(res["missed"]) == ["head-sha-recheck", "lease-ownership"]
97
+ qs = {q["id"]: q for q in res["questions"]}
98
+ for qid, n in (("q2", 2), ("q3", 3)):
99
+ assert (qs[qid]["explanation"], qs[qid]["hunk"]) == (f"Explain {n}.", f"+hunk {n}")
100
+ for qid in ("q1", "q4", "q5"):
101
+ assert "explanation" not in qs[qid] and "hunk" not in qs[qid]
102
+
103
+
104
+ def test_refuse_unanswered_question(tmp_path):
105
+ a = {"q1": "x", "q2": "y", "q3": 1, "q4": 1}
106
+ refused(score(tmp_path, answers=a), "q5")
107
+
108
+
109
+ def test_refuse_grade_for_choice_question(tmp_path):
110
+ g = {"q1": {"grade": "pass", "feedback": "f"}, "q2": {"grade": "pass", "feedback": "f"},
111
+ "q3": {"grade": "pass", "feedback": "f"}}
112
+ refused(score(tmp_path, grades=g), "q3")
113
+
114
+
115
+ def test_refuse_text_question_without_grade(tmp_path):
116
+ refused(score(tmp_path, grades={"q1": {"grade": "pass", "feedback": "f"}}), "q2")
117
+
118
+
119
+ def test_refuse_unknown_grade(tmp_path):
120
+ g = {"q1": {"grade": "excellent", "feedback": "f"}, "q2": {"grade": "pass", "feedback": "f"}}
121
+ refused(score(tmp_path, grades=g), "q1", "excellent")
@@ -0,0 +1,38 @@
1
+ """Regression tests: nothing answer-like rides on a served field, and concept ids are slugs.
2
+
3
+ `public` and `validate` refuse an exam whose served fields (`version`, `repo`, `pr`, `head_sha`,
4
+ question `type`) are not their plain shapes, and `validate` refuses a concept id that is not a slug.
5
+ Spec specs/pr-exam: requirement 2; design.md "exam.json" (concept `id`: slug).
6
+ """
7
+ import pytest
8
+
9
+ from test_exam_format import five, refused, run
10
+
11
+ SECRET = {"answer": 1, "rubric": {"pass": "x", "partial": "y"}}
12
+
13
+
14
+ @pytest.mark.parametrize("command", ["validate", "public"])
15
+ def test_question_type_holding_an_object_is_refused(tmp_path, command):
16
+ exam = five()
17
+ exam["questions"][2]["type"] = SECRET
18
+ r = run(tmp_path, exam, command=command)
19
+ refused(r, "q3", "type")
20
+ assert "rubric" not in r.stdout
21
+
22
+
23
+ @pytest.mark.parametrize("field", ["repo", "pr", "head_sha", "version"])
24
+ @pytest.mark.parametrize("command", ["validate", "public"])
25
+ def test_envelope_field_holding_an_object_is_refused_even_when_the_request_mirrors_it(tmp_path, field, command):
26
+ exam = five()
27
+ exam[field] = SECRET
28
+ r = run(tmp_path, exam, {field: SECRET}, command=command)
29
+ refused(r, field)
30
+ assert "rubric" not in r.stdout
31
+
32
+
33
+ @pytest.mark.parametrize("bad", ["../escape", "Merge-Lease", "two\nlines", "", 7])
34
+ def test_concept_id_that_is_not_a_slug_is_refused(tmp_path, bad):
35
+ exam = five()
36
+ exam["concepts"][4]["id"] = bad
37
+ exam["questions"][4]["concept"] = bad
38
+ refused(run(tmp_path, exam), "slug")