File size: 5,851 Bytes
5ca3c16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Fixed acceptance contract for the local file-backed purchase-order demo."""
from src.modules.execution.contracts import HttpCase,SandboxConfig

SKILL='erp-orders-development'
CONTRACT='''Implement demo.App as a Java17 standard library HttpServer on 127.0.0.1:8080.
On first start create /tmp/zkcoding-erp-orders.csv as UTF-8 without a header: exactly 1000 lines,
one row per order PO0001..PO1000. If it exists, load it without resetting it. Store each row as
id,name,supplier,product,quantity,received,returned,paidCents,totalCents. Deterministic initial row n:
id=PO plus 4 zero-padded digits; name=Order- plus those digits; supplier=Supplier- plus 2-digit
((n-1)%10+1); product=Product- plus 2-digit ((n-1)%20+1); quantity=10, received=0, returned=0,
paidCents=0, totalCents=100000. All integer cents; do not use floating point. Read the file for
every request. Synchronize mutations and replace the file atomically, retaining exactly 1000 rows.
GET /health -> {"status":"ok"}. All /api/orders endpoints require Authorization Bearer demo-sales;
missing/invalid -> 401 {"error":"UNAUTHORIZED"}. Unknown path -> 404 {"error":"NOT_FOUND"}.
GET /api/orders/meta -> {"total":1000,"storage":"file","fileLines":1000}, counting actual file lines.
GET /api/orders?page=1&query=PO0001&filter=all -> {"total":1,"page":1,"pageSize":20,"orders":[order]}.
Require exactly page, query and filter, page integer 1..100000, query (empty allowed), filter=all or pending. Pending means received<quantity.
Match query case-insensitively as substring of id, name, supplier or product; paginate ordered by id in pages of 20. The JSON order contains exactly
id,name,supplier,product,quantity,received,returned,paidCents,totalCents, with numeric fields integers.
GET /api/orders/detail?id=PO0001 -> that single order. Unknown ID -> 404 {"error":"NOT_FOUND"}.
POST /api/orders/receive?id=PO0999&quantity=3 increases received; POST /api/orders/return?id=PO0999&quantity=2
increases returned; POST /api/orders/pay?id=PO0999&cents=20000 increases paidCents. Success returns the updated
order object. Quantities and cents must be positive integers; received<=quantity, returned<=received,
paidCents<=totalCents. Exceeding a limit -> 409 {"error":"LIMIT_EXCEEDED"}. Bad/missing/extra/duplicate
parameters -> 400 {"error":"INVALID_INPUT"}. Wrong method -> 405 {"error":"METHOD_NOT_ALLOWED"}.
Do not accept request bodies. Demo payment changes local bookkeeping only; no external network/database.
Return JSON UTF-8. Only output src/main/java/demo/App.java, no build scripts, external libraries, tests or docs.'''


def order(n,*,received=0,returned=0,paid=0):
    return {'id':f'PO{n:04d}','name':f'Order-{n:04d}','supplier':f'Supplier-{(n-1)%10+1:02d}',
        'product':f'Product-{(n-1)%20+1:02d}','quantity':10,'received':received,'returned':returned,
        'paidCents':paid,'totalCents':100000}


def scenario(image):
    auth={'Authorization':'Bearer demo-sales'}
    def case(path,expected,status=200,method='GET',headers=auth):
        return HttpCase(path=path,method=method,headers=headers,status=status,expected_json=expected)
    health=case('/health',{'status':'ok'},headers={})
    cases=[health,case('/api/orders/meta',{'total':1000,'storage':'file','fileLines':1000}),
        case('/api/orders?page=1&query=PO0001&filter=all',{'total':1,'page':1,'pageSize':20,'orders':[order(1)]}),
        case('/api/orders?page=1&query=PO9999&filter=all',{'total':0,'page':1,'pageSize':20,'orders':[]}),
        case('/api/orders?page=1&query=&filter=all',{'total':1000,'page':1,'pageSize':20,'orders':[order(i) for i in range(1,21)]}),
        case('/api/orders?query=PO0001&filter=all',{'error':'INVALID_INPUT'},400),
        case('/api/orders?page=1&filter=all',{'error':'INVALID_INPUT'},400),
        case('/api/orders?page=1&query=PO0001',{'error':'INVALID_INPUT'},400),
        case('/api/orders?page=0&query=&filter=all',{'error':'INVALID_INPUT'},400),
        case('/api/orders?page=2147483647&query=&filter=all',{'error':'INVALID_INPUT'},400),
        case('/api/orders?page=1&query=&filter=invalid',{'error':'INVALID_INPUT'},400),
        case('/api/orders/detail?id=PO0999',order(999)),
        case('/api/orders/receive?id=PO0999&quantity=3',order(999,received=3),method='POST'),
        case('/api/orders/detail?id=PO0999',order(999,received=3)),
        case('/api/orders/receive?id=PO0999&quantity=8',{'error':'LIMIT_EXCEEDED'},409,'POST'),
        case('/api/orders/return?id=PO0999&quantity=2',order(999,received=3,returned=2),method='POST'),
        case('/api/orders/return?id=PO0999&quantity=2',{'error':'LIMIT_EXCEEDED'},409,'POST'),
        case('/api/orders/pay?id=PO0999&cents=20000',order(999,received=3,returned=2,paid=20000),method='POST'),
        case('/api/orders/pay?id=PO0999&cents=80001',{'error':'LIMIT_EXCEEDED'},409,'POST'),
        case('/api/orders/detail?id=PO0999',order(999,received=3,returned=2,paid=20000)),
        case('/api/orders/receive?id=PO0998&quantity=10',order(998,received=10),method='POST'),
        case('/api/orders?page=1&query=PO0998&filter=pending',{'total':0,'page':1,'pageSize':20,'orders':[]}),
        case('/api/orders/meta',{'total':1000,'storage':'file','fileLines':1000}),
        case('/api/orders/detail?id=PO1001',{'error':'NOT_FOUND'},404),
        case('/api/orders/receive?id=PO0001&quantity=0',{'error':'INVALID_INPUT'},400,'POST'),
        case('/api/orders/receive?id=PO0001&quantity=1&quantity=2',{'error':'INVALID_INPUT'},400,'POST'),
        case('/api/orders/detail?id=PO0001',{'error':'UNAUTHORIZED'},401,headers={}),
        case('/api/orders/pay?id=PO0001&cents=1',{'error':'METHOD_NOT_ALLOWED'},405),
        case('/unknown',{'error':'NOT_FOUND'},404,headers={})]
    return SandboxConfig(image=image,skill_ids=[SKILL],api_contract=CONTRACT,command_seconds=120,tests=cases,health=health)