File size: 8,634 Bytes
caf8059
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
253a5d4
caf8059
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Independent calculator acceptance contract; no model output controls these cases."""
import base64

from src.modules.execution.contracts import HttpCase, SandboxConfig

BACKGROUND = '为内网业务人员提供无需安装的网页计算器,支持金额试算和带括号的四则表达式;网页通过隔离的 Java HTTP 服务计算,浏览器不自行宣称计算通过。'
REQUIREMENT_ITEMS = {
    'CALC-01': 'Java 17 标准库实现 HTTP 服务,GET /health;GET /api/calculate 接收无填充base64url表达式,网页显示实际返回的结果。',
    'CALC-02': '支持加减乘除、先乘除后加减、括号、一元正负、空格、小数和连续运算;不允许 eval、脚本引擎或外部程序。',
    'CALC-03': '用 BigDecimal 和 DECIMAL128 运算;最终保留最多12位小数、HALF_UP、去除无意义末尾零,负零显示0。',
    'CALC-04': '除零、缺失操作数、未配对括号、相邻数字、非法字符及无填充base64url、UTF-8、参数集合错误必须明确报错。',
    'CALC-05': '表达式最多128字符、括号最多16层、运算符最多64个、数字字面量最多24位数字;每次运算绝对值不超过10的24次方;校验演示身份、HTTP方法和未知路由。',
    'CALC-06': '独立 HTTP 系统测试覆盖算术、精度、结合顺序、负号、边界和错误;网页验证键盘、历史回填、连续运算、除零及窄屏。源码、背景/需求、运行网页及隔离沙箱测试证据一起交付。',
}
REQUIREMENTS = '\n'.join(f'{key}:{value}' for key, value in REQUIREMENT_ITEMS.items())
DEMO_CASES = [
    {'expression': '2+3*4', 'result': '14', 'purpose': '运算优先级'},
    {'expression': '(2+3)*4', 'result': '20', 'purpose': '括号'},
    {'expression': '0.1+0.2', 'result': '0.3', 'purpose': '精确小数'},
    {'expression': '-(2+3)*-2', 'result': '10', 'purpose': '一元负号'},
    {'expression': '1/6', 'result': '0.166666666667', 'purpose': '小数舍入'},
]

CONTRACT = '''Implement demo.App, Java17 standard library HttpServer on port8080. Source src/main/java/demo/App.java <=160 lines; optional docs/README.md. No dependencies, eval, script engines, reflection, subprocesses or outbound connections. Use a recursive-descent arithmetic parser, BigDecimal with MathContext.DECIMAL128 for all binary operations, bounded/default single-thread request handling, close exchanges.
GET /health=>200 {"status":"ok"}. GET /api/calculate?expression64=... accepts exactly one nonempty expression64 key, no duplicate/extra keys. Value is canonical UNPADDED base64url (alphabet A-Z a-z 0-9 _ -) of UTF-8 expression. No URL percent encoding needed. Invalid encoding/UTF-8/query=>400 {"error":"INVALID_INPUT"}.
Known endpoints wrong method=>405 {"error":"METHOD_NOT_ALLOWED"} before auth. /api/calculate requires Authorization: Bearer demo-calculator (PUBLIC DEMO credential only); otherwise401 {"error":"UNAUTHORIZED"}. Unknown paths=>404 {"error":"NOT_FOUND"}.
Expression grammar: sum=product ((+|-) product)*; product=unary ((*|/) unary)*; unary=(+|-)* atom; atom=number | (sum). Number is digits optionally followed by dot and one-or-more digits, or dot followed by digits. Allow ASCII spaces between tokens, not within a number. Only ASCII digits, space, + - * / ( ) dot. No exponent, implicit multiplication, NaN or Infinity. Binary operations left associative; multiplication/division precede addition/subtraction. Unary signs each count as an operator.
Reject blank or malformed grammar, trailing characters, unmatched parentheses=>400 {"error":"INVALID_EXPRESSION"}; zero divisor=>400 {"error":"DIVISION_BY_ZERO"}.
Limits: decoded expression<=128 chars; parentheses nesting<=16; total unary+binary operators<=64; each numeric literal<=24 digits excluding dot; absolute value of every literal and intermediate result<=1e24. Limit violation=>422 {"error":"LIMIT_EXCEEDED"}. Check decoded length before grammar; check depth before recursion. Encoded input length<=256; longer transport input=>400 INVALID_INPUT.
These are different layers: a valid base64url value of <=256 characters can decode to an expression longer than128; that is422 LIMIT_EXCEEDED, never400 INVALID_INPUT. Preserve this distinction during repairs.
Successful200 JSON exactly {"expression":"<original decoded expression>","result":"<decimal string>"}. Round FINAL value only to scale12 HALF_UP, stripTrailingZeros and use toPlainString; any zero is "0". For 1/6 result "0.166666666667", 0.1+0.2 result "0.3". JSON UTF-8. Empty expression64 is INVALID_INPUT; encoded spaces is INVALID_EXPRESSION. Health needs no auth. Do not modify protected tests, runner, API contract or sandbox.'''


def encode_expression(expression):
    return base64.urlsafe_b64encode(expression.encode('utf-8')).decode('ascii').rstrip('=')


def calculation_path(expression):
    return '/api/calculate?expression64=' + encode_expression(expression)


def acceptance_cases():
    cases = []
    def add(case_id, requirement, path, expected, status=200, method='GET', auth=True):
        cases.append({'id': case_id, 'requirement': requirement, 'http': HttpCase(
            path=path, method=method, status=status, expected_json=expected,
            headers={'Authorization': 'Bearer demo-calculator'} if auth else {})})
    add('health', 'CALC-01', '/health', {'status': 'ok'}, auth=False)
    successes = [
        ('addition', '2+3', '5'), ('subtraction', '10-13', '-3'), ('multiplication', '6*7', '42'),
        ('division', '8/2', '4'), ('precedence', '2+3*4', '14'), ('parentheses', '(2+3)*4', '20'),
        ('decimal', '0.1+0.2', '0.3'), ('unary', '-(2+3)*-2', '10'), ('chained', '10/2*3-1', '14'),
        ('rounding', '1/6', '0.166666666667'), ('negative-zero', '-0', '0'), ('leading-dot', '.5+.25', '0.75'),
        ('spaces', ' 12 + ( 3 * 2 ) ', '18'), ('depth-boundary', '(' * 16 + '1' + ')' * 16, '1'),
        ('value-boundary', '1000000000000*1000000000000', '1000000000000000000000000'),
    ]
    for case_id, expression, result in successes:
        requirement = 'CALC-03' if case_id in ('decimal', 'rounding', 'negative-zero') else 'CALC-05' if case_id.endswith('boundary') else 'CALC-02'
        add(case_id, requirement, calculation_path(expression), {'expression': expression, 'result': result})
    for case_id, expression in [('trailing-operator', '1+'), ('unmatched', '(1+2'), ('adjacent-numbers', '1 2'),
                                ('implicit-multiply', '2(3)'), ('invalid-character', '1+a'), ('exponent', '1e3'),
                                ('blank', ' '), ('trailing-dot', '1.')]:
        add(case_id, 'CALC-04', calculation_path(expression), {'error': 'INVALID_EXPRESSION'}, 400)
    add('zero-divisor', 'CALC-04', calculation_path('1/(2-2)'), {'error': 'DIVISION_BY_ZERO'}, 400)
    for case_id, expression in [('length-limit', '1+' * 64 + '1'), ('depth-limit', '(' * 17 + '1' + ')' * 17),
                                ('operator-limit', '-' * 65 + '1'), ('digit-limit', '1' * 25),
                                ('value-limit', '999999999999999999999999*2')]:
        add(case_id, 'CALC-05', calculation_path(expression), {'error': 'LIMIT_EXCEEDED'}, 422)
    for case_id, suffix in [('missing-argument', ''), ('duplicate-argument', '?expression64=MQ&expression64=Mg'),
                            ('extra-argument', '?expression64=MQ&other=Mg'), ('invalid-base64', '?expression64=A'),
                            ('padded-base64', '?expression64=MQ=='), ('invalid-utf8', '?expression64=_w')]:
        add(case_id, 'CALC-04', '/api/calculate' + suffix, {'error': 'INVALID_INPUT'}, 400)
    add('unauthorized', 'CALC-05', calculation_path('1+1'), {'error': 'UNAUTHORIZED'}, 401, auth=False)
    add('wrong-method', 'CALC-05', calculation_path('1+1'), {'error': 'METHOD_NOT_ALLOWED'}, 405, 'POST', auth=False)
    add('unknown-path', 'CALC-05', '/unknown', {'error': 'NOT_FOUND'}, 404, auth=False)
    return cases


ACCEPTANCE_MAP = {requirement: [case['id'] for case in acceptance_cases() if case['requirement'] == requirement]
                  for requirement in REQUIREMENT_ITEMS if requirement != 'CALC-06'}
ACCEPTANCE_MAP['CALC-06'] = ['independent-vm-test', 'website-keyboard-history', 'website-chained-calculation',
                              'website-zero-divisor', 'website-mobile', 'artifact-source-digest', 'exported-requirements']


def scenario(image):
    cases = [case['http'] for case in acceptance_cases()]
    return SandboxConfig(image=image, skill_ids=['calculator-java-development'], api_contract=CONTRACT,
                         command_seconds=120, tests=cases, health=cases[0])