Spaces:
Sleeping
Sleeping
Download src/tests/runtime/test_calculator_contract.py from dellweb3/VibeCoding: direct link, hf CLI and curl.
- Browser
- Download file 4.34 kB
-
https://huggingface.co/spaces/dellweb3/VibeCoding/resolve/main/src/tests/runtime/test_calculator_contract.py
- Command line
-
hf download hf://spaces/dellweb3/VibeCoding/src/tests/runtime/test_calculator_contract.py
-
curl -L -o test_calculator_contract.py https://huggingface.co/spaces/dellweb3/VibeCoding/resolve/main/src/tests/runtime/test_calculator_contract.py
4.34 kB
| """Check independent calculator probe oracles and containment; does not run Java on the host.""" | |
| import ast | |
| import base64 | |
| from decimal import Decimal, localcontext, ROUND_HALF_UP | |
| import unittest | |
| from src.modules.execution.scenarios.calculator import ( | |
| ACCEPTANCE_MAP, CONTRACT, DEMO_CASES, REQUIREMENT_ITEMS, acceptance_cases, calculation_path, scenario, | |
| ) | |
| def arithmetic_oracle(expression): | |
| """Independent Python AST oracle limited to decimal constants and four operators.""" | |
| text = expression.strip() | |
| def evaluate(node): | |
| if isinstance(node, ast.Constant) and type(node.value) in (int, float): | |
| return Decimal(ast.get_source_segment(text, node)) | |
| if isinstance(node, ast.UnaryOp) and isinstance(node.op, (ast.UAdd, ast.USub)): | |
| value = evaluate(node.operand) | |
| return -value if isinstance(node.op, ast.USub) else value | |
| if isinstance(node, ast.BinOp): | |
| left, right = evaluate(node.left), evaluate(node.right) | |
| if isinstance(node.op, ast.Add): return left + right | |
| if isinstance(node.op, ast.Sub): return left - right | |
| if isinstance(node.op, ast.Mult): return left * right | |
| if isinstance(node.op, ast.Div): return left / right | |
| raise ValueError('Oracle expression is outside the four-operation grammar') | |
| with localcontext() as context: | |
| context.prec = 34 | |
| value = evaluate(ast.parse(text, mode='eval').body) | |
| with localcontext() as context: | |
| context.prec = 64 | |
| value = value.quantize(Decimal('0.000000000001'), rounding=ROUND_HALF_UP) | |
| result = format(value, 'f').rstrip('0').rstrip('.') | |
| return '0' if value == 0 else result | |
| class CalculatorContractTests(unittest.TestCase): | |
| def test_success_oracles_match_independent_decimal_arithmetic(self): | |
| successful = [case['http'] for case in acceptance_cases() if case['http'].status == 200 and case['id'] != 'health'] | |
| self.assertEqual(len(successful), 15) | |
| for case in successful: | |
| expression = case.expected_json['expression'] | |
| with self.subTest(expression=expression): | |
| self.assertEqual(case.expected_json['result'], arithmetic_oracle(expression)) | |
| for example in DEMO_CASES: | |
| self.assertEqual(example['result'], arithmetic_oracle(example['expression'])) | |
| def test_expression_transport_preserves_operators_and_stays_in_probe_policy(self): | |
| config = scenario('test-java@sha256:' + 'a' * 64) | |
| self.assertLessEqual(len(CONTRACT), 4000) | |
| self.assertEqual(len(config.tests), 39) | |
| self.assertTrue(all('%' not in case.path and '..' not in case.path and len(case.path) <= 241 for case in config.tests)) | |
| for expression in ['-(2+3)*-2', ' 1 / (2 + 3) ', '1+' * 64 + '1']: | |
| path = calculation_path(expression) | |
| token = path.split('=', 1)[1] | |
| decoded = base64.urlsafe_b64decode(token + '=' * (-len(token) % 4)).decode('utf-8') | |
| self.assertEqual(decoded, expression) | |
| self.assertNotIn('=', token) | |
| def test_rejection_gates_and_requirements_cannot_disappear(self): | |
| cases = {case['id']: case['http'] for case in acceptance_cases()} | |
| self.assertEqual(cases['zero-divisor'].expected_json, {'error': 'DIVISION_BY_ZERO'}) | |
| for name in ['length-limit', 'depth-limit', 'operator-limit', 'digit-limit', 'value-limit']: | |
| self.assertEqual((cases[name].status, cases[name].expected_json), (422, {'error': 'LIMIT_EXCEEDED'})) | |
| for name in ['missing-argument', 'duplicate-argument', 'extra-argument', 'invalid-base64', 'padded-base64', 'invalid-utf8']: | |
| self.assertEqual((cases[name].status, cases[name].expected_json), (400, {'error': 'INVALID_INPUT'})) | |
| self.assertEqual(cases['unauthorized'].headers, {}) | |
| self.assertEqual(cases['unauthorized'].status, 401) | |
| self.assertEqual(cases['wrong-method'].headers, {}) | |
| self.assertEqual((cases['wrong-method'].method, cases['wrong-method'].status), ('POST', 405)) | |
| self.assertEqual(cases['unknown-path'].status, 404) | |
| self.assertEqual(set(ACCEPTANCE_MAP), set(REQUIREMENT_ITEMS)) | |
| self.assertTrue(all(ACCEPTANCE_MAP[requirement] for requirement in REQUIREMENT_ITEMS)) | |
| if __name__ == '__main__': | |
| unittest.main() | |