import json,urllib.request,pathlib,base64
root=pathlib.Path(__file__).parent
token=(root/'token').read_text().strip()
def api(path,data=None,method=None):
 req=urllib.request.Request('http://127.0.0.1:19351/api/v1/'+path,data=json.dumps(data).encode() if data is not None else None,headers={'Authorization':'token '+token,'Content-Type':'application/json'},method=method)
 with urllib.request.urlopen(req) as r:return json.load(r)
api('user/repos',{'name':'demo','auto_init':True,'default_branch':'main'})
for i in range(1,121):
 branch='feat-'+str(i)
 api('repos/tester/demo/branches',{'new_branch_name':branch,'old_branch_name':'main'})
 api('repos/tester/demo/contents/'+branch+'.txt',{'branch':branch,'content':base64.b64encode(str(i).encode()).decode(),'message':branch})
 api('repos/tester/demo/pulls',{'head':branch,'base':'main','title':'Synthetic change '+str(i)})
print('Seeded 120 real pull requests')

for i in range(1,4):
 api('repos/tester/demo/branches',{'new_branch_name':f'no-pr-branch-{i}','old_branch_name':'main'})
for page in range(1,5):
 items=api(f'repos/tester/demo/pulls?state=all&head=no-such-branch&limit=50&page={page}')
 print(f'Ignored-head probe page {page}: {len(items)} items')
