#!/usr/bin/env python3
"""Create the two synthetic MCP servers for the real Cursor/UI reproduction.
Usage: python3 fixture.py [output directory]. Requires git and node.
"""
from pathlib import Path
import json, subprocess, sys
root = Path(sys.argv[1] if len(sys.argv)>1 else '/tmp/cursor-whitespace-proof').resolve()
server = "import { appendFileSync } from 'node:fs';\nconst label = LABEL;\nappendFileSync(LOG, JSON.stringify({label,script:process.argv[1],cwd:process.cwd()})+'\\n');\nlet data=''; process.stdin.on('data',chunk=>{data+=chunk;let i;while((i=data.indexOf('\\n'))>=0){const line=data.slice(0,i);data=data.slice(i+1);try{const q=JSON.parse(line);if(q.id===undefined)continue;let result={};if(q.method==='initialize')result={protocolVersion:'2024-11-05',capabilities:{tools:{}},serverInfo:{name:label,version:'1.0.0'}};if(q.method==='tools/list')result={tools:[{name:label+'_whoami',description:'Report this MCP server identity',inputSchema:{type:'object',properties:{}}}]};if(q.method==='tools/call')result={content:[{type:'text',text:'launched from '+label+': '+process.argv[1]+', cwd '+process.cwd()}]};process.stdout.write(JSON.stringify({jsonrpc:'2.0',id:q.id,result})+'\\n');}catch{}}});\n"
for dirname,label in [('workspace','sibling'), ('workspace ','realrepo')]:
    folder=root/'fixture'/'space'/dirname
    (folder/'.cursor').mkdir(parents=True,exist_ok=True)
    (folder/'server.mjs').write_text(server.replace('LABEL',json.dumps(label)).replace('LOG',json.dumps(str(root/'launches.jsonl'))))
    (folder/'.cursor'/'mcp.json').write_text(json.dumps({'mcpServers':{'demo':{'command':'node','args':['${workspaceFolder}/server.mjs']}}}))
    if label=='realrepo':
        (folder/'app').mkdir(exist_ok=True)
        subprocess.run(['git','init','-q',str(folder)],check=True)
print(root/'fixture'/'space'/'workspace '/'app')
