113 lines
3.8 KiB
Python
113 lines
3.8 KiB
Python
#!/usr/bin/env python3
|
|
"""Reproduces the 'AI answer replaced with stale placeholder' bug:
|
|
HedgeDoc's /download route reads DB-backed content that lags the live
|
|
realtime document by up to ~1s. A client that reacts to a large remote
|
|
delete by fetching /download instead of just applying the operation can
|
|
apply a completely correct-looking BUT STALE snapshot, and never correct
|
|
itself since it already advanced past that revision.
|
|
|
|
No JOE/pty involved -- this bug lives entirely in hd_client.py.
|
|
"""
|
|
import importlib
|
|
import sys
|
|
import threading
|
|
import time
|
|
|
|
import socketio
|
|
|
|
sys.path.insert(0, str(__import__("pathlib").Path(__file__).resolve().parent.parent / "hd_common"))
|
|
from e2e_test import FakeServer, free_port # noqa: E402
|
|
|
|
|
|
def build_scenario():
|
|
prefix = "# Header\n\n"
|
|
block = (
|
|
"<!--Agent\n"
|
|
"User: Funkar det med AI?\n"
|
|
"Agent: Ja, det funkar! Jag kan lasa innehallet i ditt dokument och svara pa fragor baserat pa det.\n"
|
|
"User: Fasen vad bra\n"
|
|
"Agent: \U0001f9e0 Tanker...\n"
|
|
"-->"
|
|
)
|
|
new_block = (
|
|
"<!--Agent\n"
|
|
"User: Funkar det med AI?\n"
|
|
"Agent: Ja, det funkar! Jag kan lasa innehallet i ditt dokument och svara pa fragor baserat pa det.\n"
|
|
"User: Fasen vad bra\n"
|
|
"Agent: Visst ar det! Jag ser allt fran rubriken till din kommentar.\n"
|
|
"->\n"
|
|
"-->"
|
|
)
|
|
suffix = "\n(end)"
|
|
assert len(block) > 100, "test block must exceed the old >100-char large_delete threshold"
|
|
return prefix, block, new_block, suffix
|
|
|
|
|
|
def run(client_module_path):
|
|
# Force a clean import of whichever hd_client.py variant is on disk right now.
|
|
if "hd_client" in sys.modules:
|
|
del sys.modules["hd_client"]
|
|
hd_client = importlib.import_module("hd_client")
|
|
|
|
prefix, block, new_block, suffix = build_scenario()
|
|
initial_text = prefix + block + suffix
|
|
port = free_port()
|
|
server = FakeServer(initial_text, port, db_flush_interval=1.0)
|
|
server.start()
|
|
time.sleep(0.2)
|
|
|
|
events = []
|
|
client = hd_client.HedgeDocClient(
|
|
f"http://127.0.0.1:{port}", "test-note", verbose=False,
|
|
on_remote_operation=lambda doc: events.append(doc.text),
|
|
)
|
|
client.connect()
|
|
doc = client.wait_ready()
|
|
assert doc.text == initial_text
|
|
|
|
# Simulate the AI's own socket sending the block replacement, almost
|
|
# immediately -- well inside the server's 1s DB-flush window.
|
|
ai = socketio.Client()
|
|
ai.connect(f"http://127.0.0.1:{port}/?noteId=test-note", socketio_path="socket.io",
|
|
transports=["websocket", "polling"])
|
|
time.sleep(0.1)
|
|
op = [
|
|
hd_client.utf16_len(prefix),
|
|
-hd_client.utf16_len(block),
|
|
new_block,
|
|
hd_client.utf16_len(suffix),
|
|
]
|
|
ai.emit("operation", (0, op, {"ranges": []}))
|
|
time.sleep(0.4) # well before the 1.0s DB flush -- this is the race window
|
|
|
|
final_text = client.document.text
|
|
client.close()
|
|
ai.disconnect()
|
|
server.stop()
|
|
return final_text, new_block, initial_text
|
|
|
|
|
|
def main():
|
|
print("=" * 70)
|
|
print("Testing hd_client.py as currently on disk")
|
|
print("=" * 70)
|
|
final_text, new_block, initial_text = run("hd_client")
|
|
expected = initial_text.replace(
|
|
initial_text[initial_text.index("<!--Agent"):initial_text.index("-->") + 3],
|
|
new_block,
|
|
)
|
|
print("Got final client text ending:\n", repr(final_text[-160:]))
|
|
print()
|
|
print("Expected ending:\n", repr(expected[-160:]))
|
|
ok = final_text == expected
|
|
print()
|
|
print("PASS" if ok else "FAIL", "-- AI answer applied correctly, not stuck on placeholder")
|
|
if not ok:
|
|
if "Tanker" in final_text:
|
|
print("--> Stuck showing the 'Tanker...' placeholder: this IS the reported bug.")
|
|
sys.exit(0 if ok else 1)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|