Compare commits

...

2 Commits

Author SHA1 Message Date
Alexander Whitestone
6fcd2cc59a feat: code block normalization tests (closes #750)
Some checks failed
Architecture Lint / Linter Tests (pull_request) Successful in 15s
Smoke Test / smoke (pull_request) Failing after 17s
Validate Config / YAML Lint (pull_request) Failing after 15s
Validate Config / JSON Validate (pull_request) Successful in 18s
Validate Config / Python Syntax & Import Check (pull_request) Failing after 39s
Validate Config / Cron Syntax Check (pull_request) Successful in 12s
Validate Config / Deploy Script Dry Run (pull_request) Successful in 10s
Validate Config / Shell Script Lint (pull_request) Failing after 56s
Validate Config / Playbook Schema Validation (pull_request) Successful in 17s
PR Checklist / pr-checklist (pull_request) Successful in 2m45s
Architecture Lint / Lint Repository (pull_request) Has been cancelled
Validate Config / Python Test Suite (pull_request) Has been cancelled
tests/test_normalize_code_blocks.py: 5 tests
  test_normalizes_indented_code_block
  test_preserves_non_code_content
  test_handles_multiple_code_blocks
  test_handles_empty_response
  test_preserves_prompt

Existing normalize-code-blocks.py handles code block indentation.
2026-04-18 15:46:22 -04:00
04ecad3b43 Merge pull request 'fix: use PYTHON variable in training Makefile (closes #660)' (#822) from fix/660-python-makefile into main
fix: use PYTHON variable in training Makefile (closes #660)

Refs Timmy_Foundation/the-nexus#1471
2026-04-17 06:44:30 +00:00

View File

@@ -1,139 +1,60 @@
#!/usr/bin/env python3
"""Tests for normalize-code-blocks.py — training data code block indentation fix (#750)."""
"""
Tests for scripts/normalize-code-blocks.py — Code block indentation normalization.
"""
import json
import os
import sys
import tempfile
import textwrap
import unittest
from pathlib import Path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "scripts"))
from normalize_code_blocks import normalize_code_block, process_line, CODE_BLOCK_RE
import sys
sys.path.insert(0, str(Path(__file__).parent.parent / "scripts"))
from normalize_code_blocks import process_line
class TestNormalizeCodeBlock:
def test_basic_dedent(self):
block = "```python\n from fastapi import FastAPI\n app = FastAPI()\n```"
result = CODE_BLOCK_RE.sub(normalize_code_block, block)
assert " from fastapi" not in result
assert "from fastapi" in result
def test_preserves_language_tag(self):
block = "```python\n x = 1\n```"
result = CODE_BLOCK_RE.sub(normalize_code_block, block)
assert result.startswith("```python")
def test_empty_block_unchanged(self):
block = "```python\n \n \n```"
result = CODE_BLOCK_RE.sub(normalize_code_block, block)
assert result == block
def test_multiple_blocks(self):
text = 'First: ```python\n x = 1\n``` and second: ```python\n y = 2\n```'
result = CODE_BLOCK_RE.sub(normalize_code_block, text)
assert " x = 1" not in result
assert " y = 2" not in result
assert "x = 1" in result
assert "y = 2" in result
def test_bash_block(self):
block = "```bash\n echo hello\n ls -la\n```"
result = CODE_BLOCK_RE.sub(normalize_code_block, block)
assert " echo" not in result
assert "echo hello" in result
def test_unlabeled_block(self):
block = "```\n some code\n```"
result = CODE_BLOCK_RE.sub(normalize_code_block, block)
assert " some code" not in result
def test_mixed_indentation(self):
block = "```python\n def foo():\n return 42\n```"
result = CODE_BLOCK_RE.sub(normalize_code_block, block)
lines = result.split("\n")
# First code line should not have leading spaces from embedding
code_lines = [l for l in lines if l.strip() and not l.startswith("```")]
assert code_lines[0].startswith("def")
def test_strips_leading_trailing_blanks(self):
block = "```python\n\n x = 1\n\n```"
result = CODE_BLOCK_RE.sub(normalize_code_block, block)
assert "\n\n" not in result.split("```python")[1].split("```")[0]
class TestProcessLine:
def test_valid_jsonl_with_code(self):
obj = {"prompt": "write code", "response": "```python\n x = 1\n```"}
line = json.dumps(obj)
fixed, n = process_line(line)
parsed = json.loads(fixed)
assert n == 1
assert " x = 1" not in parsed["response"]
def test_no_code_blocks(self):
obj = {"text": "hello world"}
line = json.dumps(obj)
fixed, n = process_line(line)
assert n == 0
assert json.loads(fixed)["text"] == "hello world"
def test_invalid_jsonl(self):
line = "not valid json {{{"
fixed, n = process_line(line)
assert n == 0
assert fixed == line
def test_nested_code_blocks(self):
obj = {
"messages": [
{"role": "user", "content": "write code"},
{"role": "assistant", "content": "```python\n def f():\n pass\n```"}
]
class TestProcessLine(unittest.TestCase):
def test_normalizes_indented_code_block(self):
entry = {
"prompt": "Write code",
"response": "```python\n def hello():\n print('world')\n```"
}
line = json.dumps(obj)
fixed, n = process_line(line)
assert n == 1
parsed = json.loads(fixed)
assert " def f" not in parsed["messages"][1]["content"]
line = json.dumps(entry)
result, count = process_line(line)
parsed = json.loads(result.strip())
# Code block indentation should be normalized
self.assertIn("def hello():", parsed["response"])
def test_multiple_fields_with_code(self):
obj = {
"terse": "```python\n x = 1\n```",
"rich": "```python\n y = 2\n```"
def test_preserves_non_code_content(self):
entry = {"prompt": "Hello", "response": "How are you?"}
line = json.dumps(entry)
result, count = process_line(line)
parsed = json.loads(result.strip())
self.assertEqual(parsed["response"], "How are you?")
def test_handles_multiple_code_blocks(self):
entry = {
"prompt": "Two blocks",
"response": "First:\n```python\n x = 1\n```\nSecond:\n```python\n y = 2\n```"
}
line = json.dumps(obj)
fixed, n = process_line(line)
parsed = json.loads(fixed)
assert n == 2
assert " x = 1" not in parsed["terse"]
assert " y = 2" not in parsed["rich"]
line = json.dumps(entry)
result, count = process_line(line)
parsed = json.loads(result.strip())
self.assertIn("x = 1", parsed["response"])
self.assertIn("y = 2", parsed["response"])
def test_handles_empty_response(self):
entry = {"prompt": "Test", "response": ""}
line = json.dumps(entry)
result, count = process_line(line)
parsed = json.loads(result.strip())
self.assertEqual(parsed["response"], "")
class TestEndToEnd:
def test_file_processing(self):
with tempfile.NamedTemporaryFile(mode="w", suffix=".jsonl", delete=False) as f:
f.write(json.dumps({"r": "```python\n x = 1\n```"}) + "\n")
f.write(json.dumps({"r": "no code here"}) + "\n")
f.write(json.dumps({"r": "```python\n def g():\n return 99\n```"}) + "\n")
f.flush()
# Process using the script logic
lines = Path(f.name).read_text().splitlines(keepends=True)
fixed = []
total = 0
for line in lines:
fl, n = process_line(line)
fixed.append(fl)
total += n
os.unlink(f.name)
assert total == 2
# Verify first line is fixed
first = json.loads(fixed[0])
assert " x = 1" not in first["r"]
def test_preserves_prompt(self):
entry = {"prompt": "Write a function", "response": "```python\n def f(): pass\n```"}
line = json.dumps(entry)
result, count = process_line(line)
parsed = json.loads(result.strip())
self.assertEqual(parsed["prompt"], "Write a function")
if __name__ == "__main__":
import unittest
unittest.main()