first commit

This commit is contained in:
Joakim
2026-10-15 17:27:46 +02:00
parent 0232909b8a
commit 0e0febffda
38 changed files with 8068 additions and 0 deletions

88
hd-joe/fake_hedgedoc.py Normal file
View File

@@ -0,0 +1,88 @@
#!/usr/bin/env python3
"""Minimal fake HedgeDoc 1.x realtime server: just enough Socket.IO wire
protocol (doc / operation / ack) to drive hd_client.py end-to-end without a
real HedgeDoc instance. Single-client, no peer broadcast -- sufficient to
reproduce and verify the BOF/EOF local-edit bugs, which don't need
concurrency to manifest.
"""
import argparse
import json
import sys
import threading
import socketio
from werkzeug.serving import make_server
import sys as _sys
from pathlib import Path as _Path
_sys.path.insert(0, str(_Path(__file__).resolve().parent.parent / "hd_common"))
from hd_client import apply_operation, utf16_len
def apply_operation_strict(text, op):
"""Mirror ot.js's TextOperation.fromJSON: a literal 0 anywhere in the op
is invalid and must raise -- matches isRetain/isInsert/isDelete in the
real server's text-operation.js."""
for c in op:
if isinstance(c, bool):
raise ValueError(f"unknown operation: {c!r}")
if isinstance(c, int) and c == 0:
raise ValueError(f"unknown operation: {c!r}")
return apply_operation(text, op)
sio = socketio.Server(async_mode="threading", cors_allowed_origins="*")
app = socketio.WSGIApp(sio)
state = {"text": "", "revision": 0}
state_lock = threading.Lock()
operations_received = [] # for test assertions
@sio.event
def connect(sid, environ, auth):
with state_lock:
sio.emit("doc", {"str": state["text"], "revision": state["revision"], "clients": []}, to=sid)
@sio.on("operation")
def on_operation(sid, base_revision, operation, selection):
with state_lock:
operations_received.append((base_revision, operation, selection))
if base_revision != state["revision"]:
# In this single-client test harness this should never happen.
sys.stderr.write(
f"[fake_hedgedoc] REJECTED operation: base_revision={base_revision} "
f"!= server revision={state['revision']} op={operation!r}\n"
)
return
try:
state["text"] = apply_operation_strict(state["text"], operation)
except Exception as exc:
sys.stderr.write(f"[fake_hedgedoc] operation raised: {exc!r} op={operation!r}\n")
return
state["revision"] += 1
sio.emit("ack", state["revision"], to=sid)
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--port", type=int, default=5999)
ap.add_argument("--text", default="")
ap.add_argument("--state-file", help="write final {text, revision} as JSON here on SIGTERM/exit")
args = ap.parse_args()
state["text"] = args.text
srv = make_server("127.0.0.1", args.port, app)
try:
srv.serve_forever()
finally:
if args.state_file:
with state_lock:
with open(args.state_file, "w", encoding="utf-8") as f:
json.dump({"text": state["text"], "revision": state["revision"],
"operations": operations_received}, f)
if __name__ == "__main__":
main()