workos-office / docs /10_API_Gateway /validate_contract.py
dellweb3's picture
Work OS validated demo local-v0.8
99caad5 verified
Raw History Blame Contribute Delete
6.49 kB
"""Static design validation only; does not claim a running Gateway exists.
Usage: python validate_contract.py [--dependency-dir PATH]
"""
from pathlib import Path
import argparse,copy,hashlib,json,re,sys
from datetime import datetime,timezone
from importlib.metadata import version
parser=argparse.ArgumentParser()
parser.add_argument('--dependency-dir',help='Optional isolated directory containing validation dependencies')
args=parser.parse_args()
if args.dependency_dir:sys.path.insert(0,args.dependency_dir)
from jsonschema import Draft202012Validator,FormatChecker
from openapi_spec_validator import validate
root=Path(__file__).resolve().parent
read=lambda n:json.loads((root/n).read_text(encoding='utf-8-sig'))
spec=read('openapi.json');catalog=read('api-catalog.json');examples=read('examples.json')
validate(spec)
checks=['OpenAPI 3.1 document validation']
schemas=spec['components']['schemas']
def expand(schema):
if isinstance(schema,dict):
if '$ref' in schema:
assert schema['$ref'].startswith('#/components/schemas/'),schema['$ref']
return expand(schemas[schema['$ref'].rsplit('/',1)[-1]])
return {k:expand(v) for k,v in schema.items()}
if isinstance(schema,list):return [expand(v) for v in schema]
return schema
for n,s in schemas.items():Draft202012Validator.check_schema(expand(s))
for case in examples['cases']:
Draft202012Validator(expand(schemas[case['schema']]),format_checker=FormatChecker()).validate(case['value'])
checks.append(f"{len(examples['cases'])} typed examples and {len(schemas)} schemas validated")
ops={}
for path,item in spec['paths'].items():
for method,op in item.items():
if method not in ['get','post','put','patch','delete','head','options','trace']:continue
assert op['operationId'] not in ops
ops[op['operationId']]=(method.upper(),path,op)
expected=set(re.findall(r'\{([^}]+)\}',path))
actual={p['name'] for p in op.get('parameters',[]) if p['in']=='path' and p.get('required')}
assert expected==actual,(path,expected,actual)
for op in catalog['operations']:
if op['phase']!='PC':
assert op['operation_id'] not in ops
continue
method,path,contract=ops[op['operation_id']]
assert (method,path)==(op['method'],op['path'])
assert str(op['status']) in contract['responses']
idem=[p for p in contract.get('parameters',[]) if p['in']=='header' and p['name']=='Idempotency-Key']
assert bool(idem and idem[0].get('required'))==op['idempotency_required']
if op['scope']=='public':assert contract['security']==[]
else:assert contract.get('security',spec.get('security'))
assert len(ops)==sum(x['phase']=='PC' for x in catalog['operations'])==37
checks.append('37 PC operations: unique IDs, paths, success codes, required path parameters, authentication and idempotency headers match catalog; 7 future operations excluded')
config=read('modules.example.json');configschema=read('modules.schema.json')
Draft202012Validator.check_schema(configschema)
Draft202012Validator(configschema).validate(config)
ids=[x['module_id'] for x in config['modules']];assert len(ids)==len(set(ids))
pending={x['module_id']:set(x['depends_on']) for x in config['modules']};done=set()
while pending:
ready={i for i,d in pending.items() if d<=done};assert ready,('cycle or missing dependency',pending)
for i in ready:pending.pop(i)
done|=ready
checks.append('Module configuration schema, unique module IDs, dependency completeness and acyclic startup graph')
negative=[('StartRun',{'employee_id':'e','input_message_id':'m','owner_user_id':'another-user'}),('StartRun',{'employee_id':'e'}),('ApprovalInput',{'artifact_id':'a','expected_version':0,'content_hash':'bad'}),('SearchInput',{'workspace_id':'w','query':'q','top_k':11})]
for name,value in negative:assert not Draft202012Validator(expand(schemas[name])).is_valid(value)
checks.append('4 negative payloads rejected structurally (not runtime authorization tests)')
case_map={x['schema']:x['value'] for x in examples['cases']}
a=case_map['Artifact'];approval=case_map['ApprovalInput']
assert a['content_hash']==hashlib.sha256(a['content'].encode('utf-8')).hexdigest()==approval['content_hash']
assert a['version']==approval['expected_version'] and a['artifact_id']==approval['artifact_id']
fixtures={x['skill_id']:x['content'] for x in examples['skill_source_fixtures']}
for s in case_map['Run']['effective_skills']:assert s['content_hash']==hashlib.sha256(fixtures[s['skill_id']].encode('utf-8')).hexdigest()
checks.append('Example artifact approval and 4 Skill snapshot content hashes are internally consistent')
md=(root/'README.md').read_text(encoding='utf-8-sig')
assert sum(line.startswith('```') for line in md.splitlines())%2==0
assert len(re.findall(r'^## \d+\.',md,re.M))==18
for target in re.findall(r'\]\(([^)]+)\)',md):
if not target.startswith(('http:','https:','#')):
path=target.split('#')[0]
# validation-report is the output created at the end of this successful run.
assert path=='validation-report.json' or (root/path).exists(),path
checks.append('18 document sections, balanced code fences and existing local attachment links; Mermaid rendering not tested')
# Hash reviewed inputs, excluding the report itself.
files={p.name:hashlib.sha256(p.read_bytes()).hexdigest() for p in sorted(root.iterdir()) if p.is_file() and p.name!='validation-report.json'}
report={'status':'passed','scope':'static_design_contract_only','checked_at_utc':datetime.now(timezone.utc).isoformat(),'tools':{'python':sys.version.split()[0],'openapi-spec-validator':version('openapi-spec-validator'),'jsonschema':version('jsonschema')},'counts':{'pc_operations':len(ops),'future_operations':len(catalog['operations'])-len(ops),'schemas':len(schemas),'typed_examples':len(examples['cases']),'negative_examples':len(negative),'requirements':len(re.findall(r'^\| GW-\d{2} \|',md,re.M))},'checks':checks,'not_executed':['Backend integration and runtime authorization','Real Qwen model and WeCom delivery','Nginx configuration execution and browser SSE','Load, resilience and Kylin/Hygon hardware qualification','Mermaid rendering'],'sha256':files}
(root/'validation-report.json').write_text(json.dumps(report,ensure_ascii=False,indent=2)+'\n',encoding='utf-8')
print(json.dumps({k:report[k] for k in ['status','scope','counts']},ensure_ascii=False))