"""Only newly created tiny fixture files under this script's own sibling directory."""
from pathlib import Path
import hashlib,json,os,platform,uuid
def demo():
 base=Path(__file__).resolve().parent/'scope35-owned-fixtures';base.mkdir(exist_ok=True)
 folder=base/('authored-replace-'+uuid.uuid4().hex);folder.mkdir(mode=0o777)
 assert folder.parent.resolve()==base.resolve()
 dest=folder/'state.json';old=b'{"version":1,"items":["A"]}\n';new=b'{"version":2,"items":["A","B"]}\n';dest.write_bytes(old)
 attempts=[]
 def attempt(label,data,fail_before_replace=False):
  stage=folder/(label+'.stage');assert not stage.exists() and not dest.is_symlink()
  with stage.open('xb') as f:f.write(data);f.flush();os.fsync(f.fileno())
  try:
   obj=json.loads(stage.read_bytes())
   if obj!={'version':2,'items':['A','B']}:raise ValueError('fixture schema')
   if fail_before_replace:raise RuntimeError('authored injected failure')
  except (ValueError,RuntimeError) as e:
   assert dest.read_bytes()==old;attempts.append({'label':label,'result':type(e).__name__,'target_is_old':True});return
  os.replace(stage,dest);assert dest.read_bytes()==new and not stage.exists();attempts.append({'label':label,'result':'replaced','target_is_old':False})
 attempt('invalid',b'{broken');attempt('injected',new,True);attempt('valid',new)
 assert json.loads(dest.read_bytes())=={'version':2,'items':['A','B']}
 return {'runtime':platform.python_version(),'os_name':os.name,'attempts':attempts,'old_sha256':hashlib.sha256(old).hexdigest(),'new_sha256':hashlib.sha256(dest.read_bytes()).hexdigest(),'fresh_reopen_json':json.loads(dest.read_bytes()),'flush_fsync_performed':True,'power_failure_tested':False,'concurrent_reader_tested':False,'scope':'Three tiny own stage files and one own target, same directory; no existing user path modified, no deletion. POSIX atomic rename wording is not Windows crash-durability proof or multi-file transaction.'}
if __name__=='__main__':print(json.dumps(demo(),ensure_ascii=True,indent=2))
