-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_ot.py
More file actions
191 lines (137 loc) · 5.91 KB
/
Copy pathtest_ot.py
File metadata and controls
191 lines (137 loc) · 5.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
"""
Exhaustive tests for compute_ot_ops and apply_sharejs_ops.
The invariant: applying compute_ot_ops(old, new) to old one-op-at-a-time
(simulating the Overleaf server receiving each applyOtUpdate in sequence)
must yield exactly new.
"""
from __future__ import annotations
import pytest
from overleaf_sync import compute_ot_ops, apply_sharejs_ops
def server_apply(content: str, ops: list[dict]) -> str:
"""Simulate Overleaf server applying ops one by one in order."""
for op in ops:
p = op["p"]
if "i" in op:
content = content[:p] + op["i"] + content[p:]
elif "d" in op:
content = content[:p] + content[p + len(op["d"]):]
return content
def roundtrip(old: str, new: str):
"""Assert that ops computed from old→new reproduce new when applied by server."""
ops = compute_ot_ops(old, new)
result = server_apply(old, ops)
assert result == new, (
f"FAIL: expected {new!r}, got {result!r}\n"
f" old={old!r}\n"
f" ops={ops}"
)
# ── pure inserts ────────────────────────────────────────────────────────────
def test_insert_at_beginning():
roundtrip("world", "hello world")
def test_insert_at_end():
roundtrip("hello", "hello world")
def test_insert_in_middle():
roundtrip("helloworld", "hello beautiful world")
def test_insert_empty_old():
roundtrip("", "hello")
def test_insert_newline():
roundtrip("line1\nline3", "line1\nline2\nline3")
# ── pure deletes ─────────────────────────────────────────────────────────────
def test_delete_at_beginning():
roundtrip("hello world", "world")
def test_delete_at_end():
roundtrip("hello world", "hello")
def test_delete_in_middle():
roundtrip("hello beautiful world", "hello world")
def test_delete_all():
roundtrip("hello", "")
def test_delete_newline():
roundtrip("line1\nline2\nline3", "line1\nline3")
# ── replace (delete + insert at same position) ───────────────────────────────
# This is the class of operations that triggers the ordering bug.
def test_replace_at_beginning():
roundtrip("Hello World", "Hi World")
def test_replace_at_end():
roundtrip("Hello World", "Hello Earth")
def test_replace_in_middle():
roundtrip("Hello World", "Hello Beautiful World")
def test_replace_longer_with_shorter():
roundtrip("Hello World", "Hi W")
def test_replace_shorter_with_longer():
roundtrip("Hi W", "Hello World")
def test_replace_whole_word():
roundtrip("foo bar baz", "foo qux baz")
def test_replace_latex_command():
old = r"\section{Introduction}"
new = r"\section{Related Work}"
roundtrip(old, new)
def test_replace_multiline():
old = "line1\nold content here\nline3"
new = "line1\nnew content here\nline3"
roundtrip(old, new)
def test_replace_grows():
roundtrip("Hello World", "Hi Earth and Beyond")
def test_replace_shrinks():
roundtrip("Hi Earth and Beyond", "Hello World")
# ── realistic LaTeX edits ────────────────────────────────────────────────────
SAMPLE = r"""\documentclass{article}
\begin{document}
\section{Introduction}
This is an introduction.
\end{document}
"""
def test_latex_change_section_title():
new = SAMPLE.replace("Introduction", "Background")
roundtrip(SAMPLE, new)
def test_latex_add_paragraph():
new = SAMPLE.replace(
"This is an introduction.",
"This is an introduction.\n\nMore text here."
)
roundtrip(SAMPLE, new)
def test_latex_delete_sentence():
new = SAMPLE.replace("\nThis is an introduction.", "")
roundtrip(SAMPLE, new)
def test_latex_replace_sentence():
new = SAMPLE.replace("This is an introduction.", "This section motivates the work.")
roundtrip(SAMPLE, new)
def test_latex_add_usepackage():
new = SAMPLE.replace(r"\begin{document}", r"\usepackage{amsmath}" + "\n" + r"\begin{document}")
roundtrip(SAMPLE, new)
# ── edge cases ───────────────────────────────────────────────────────────────
def test_identical():
assert compute_ot_ops("abc", "abc") == []
def test_single_char_replace():
roundtrip("a", "b")
def test_single_char_insert():
roundtrip("", "x")
def test_single_char_delete():
roundtrip("x", "")
def test_trailing_newline_add():
roundtrip("hello", "hello\n")
def test_trailing_newline_remove():
roundtrip("hello\n", "hello")
def test_only_newlines():
roundtrip("\n\n", "\n\n\n")
def test_unicode():
roundtrip("café", "café latte")
def test_unicode_replace():
roundtrip("naïve", "naive")
# ── git backup comparison ────────────────────────────────────────────────────
def test_git_backup_roundtrip():
"""Verify ops roundtrip on the actual project file from git backup."""
import subprocess, pathlib
backup = pathlib.Path.home() / "workspace/overleaf_backup/69d7ba24e392a5121b52a73f/main.tex"
if not backup.exists():
pytest.skip("git backup not present")
content = backup.read_text(encoding="utf-8")
# Simulate a realistic edit: change a word in the content
if "Introduction" in content:
edited = content.replace("Introduction", "Background", 1)
roundtrip(content, edited)
# Simulate appending a line
roundtrip(content, content + "\n% added line\n")
# Simulate deleting first line
lines = content.split("\n")
if len(lines) > 1:
roundtrip(content, "\n".join(lines[1:]))