0514568df0
The testing pass: 2140 tests to 2283, and four bugs that no amount of reading had turned up. Three came from driving the JavaScript under a Node DOM stub, which is the practice CLAUDE.md sets out and this is the reason it does. The terminal dropped every keystroke after a reconnect. `onclose` closed over the module-level socket rather than its own, and close() queues its event -- so the old socket's close arrived after a new one was assigned and nulled the live one. Output kept coming, because onmessage is bound to the object, while every send gates on the variable. It also announced "Disconnected" about a shell that had just reconnected. Two scripts were loaded twice on /messages, once by base.html and again by the page. Each is an IIFE with its own state, so four keyboard shortcuts toggled their panel twice and therefore did nothing, /help opened two dialogs, and an @ mention attached its file twice. A sweep refuses any template re-loading what base.html has. The microphone had no guard while the permission prompt was up, so each click opened another stream and only the last was ever stopped. And a skill shared with you took its name out of your own library: create checked uniqueness against what is *visible* rather than what is owned, against a (owner_id, name) constraint, and told you to edit a row you cannot edit. --ink-faint failed the contrast minimum in both themes -- 3.85 and 3.19 against 4.5 -- so the smallest text on every screen was the hardest to read. Measured in a headless browser rather than judged by eye. And the suite runs on 3.11 and 3.12 now as well as 3.14. It had only ever run on 3.14 while the image ships 3.12 and the packaging claimed 3.11: the interpreter most people would run was the one nothing had tested. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
180 lines
6.6 KiB
Python
180 lines
6.6 KiB
Python
"""Reading and writing a file for somebody who is about to edit it.
|
|
|
|
The model-facing `read_file`/`write_file` pair is deliberately untouched: what
|
|
they return is a contract a model has been shown, and it is the right contract
|
|
for a model. It is the wrong one here, and these are the cases that say why.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import pytest
|
|
|
|
from lembas.services.agent import ssh as ssh_service
|
|
from lembas.services.agent.base import Conflict, ExecError
|
|
|
|
# Stands up something real -- see the `slow` marker in pyproject.toml.
|
|
pytestmark = pytest.mark.slow
|
|
|
|
asyncssh = pytest.importorskip("asyncssh")
|
|
|
|
|
|
class _Server(asyncssh.SSHServer):
|
|
def begin_auth(self, username: str) -> bool:
|
|
return False
|
|
|
|
|
|
@pytest.fixture
|
|
async def machine(tmp_path):
|
|
project = tmp_path / "project"
|
|
project.mkdir()
|
|
server = await asyncssh.create_server(
|
|
_Server,
|
|
"127.0.0.1",
|
|
0,
|
|
server_host_keys=[asyncssh.generate_private_key("ssh-ed25519")],
|
|
sftp_factory=True,
|
|
)
|
|
port = next(iter(server.sockets)).getsockname()[1]
|
|
line, fingerprint = await ssh_service.capture_host_key("127.0.0.1", port)
|
|
try:
|
|
yield {"port": port, "host_key": line, "dir": str(project), "path": project}
|
|
finally:
|
|
server.close()
|
|
await server.wait_closed()
|
|
|
|
|
|
def _executor(machine) -> ssh_service.SshExecutor:
|
|
return ssh_service.SshExecutor(
|
|
{
|
|
"host": "127.0.0.1",
|
|
"port": machine["port"],
|
|
"username": "tester",
|
|
"auth": "password",
|
|
"credential": "",
|
|
"host_key": machine["host_key"],
|
|
},
|
|
machine["dir"],
|
|
)
|
|
|
|
|
|
# --- Fidelity ------------------------------------------------------------------
|
|
async def test_an_escape_sequence_survives_a_round_trip(machine):
|
|
"""The whole reason this is not `read_file`. That one ends in
|
|
`clean_output`, which strips ANSI escapes -- right for the output of a
|
|
command, and here it means opening a file and pressing Save rewrites it
|
|
with the escapes gone."""
|
|
original = "red \x1b[31mtext\x1b[0m here\n"
|
|
(machine["path"] / "colours.txt").write_text(original)
|
|
executor = _executor(machine)
|
|
|
|
opened = await executor.read_text("colours.txt")
|
|
assert opened.text == original
|
|
|
|
await executor.write_text("colours.txt", opened.text, if_unchanged=opened.revision)
|
|
assert (machine["path"] / "colours.txt").read_text() == original
|
|
|
|
|
|
async def test_the_model_facing_read_still_strips_them(machine):
|
|
"""Pinned as a pair: the contract a model was shown has not moved."""
|
|
(machine["path"] / "colours.txt").write_text("red \x1b[31mtext\x1b[0m here\n")
|
|
text = await _executor(machine).read_file("colours.txt")
|
|
assert "\x1b[31m" not in text
|
|
|
|
|
|
async def test_undecodable_bytes_are_reported_rather_than_replaced(machine):
|
|
"""errors="replace" would hand back U+FFFD for every one of them, and
|
|
saving that back is how a file is quietly destroyed."""
|
|
(machine["path"] / "blob.bin").write_bytes(b"\xff\xfe\x00\x01binary")
|
|
opened = await _executor(machine).read_text("blob.bin")
|
|
assert opened.binary is True
|
|
assert opened.text == ""
|
|
|
|
|
|
async def test_a_nul_byte_early_on_reads_as_binary(machine):
|
|
(machine["path"] / "blob.bin").write_bytes(b"text\x00more text")
|
|
assert (await _executor(machine).read_text("blob.bin")).binary is True
|
|
|
|
|
|
async def test_utf8_beyond_ascii_is_not_binary(machine):
|
|
(machine["path"] / "note.txt").write_text("a mallorn tree — Lothlórien\n")
|
|
opened = await _executor(machine).read_text("note.txt")
|
|
assert opened.binary is False
|
|
assert "Lothlórien" in opened.text
|
|
|
|
|
|
# --- Size ------------------------------------------------------------------------
|
|
async def test_a_large_file_opens_truncated(machine):
|
|
(machine["path"] / "big.log").write_text("x" * (ssh_service.MAX_READ_BYTES + 500))
|
|
opened = await _executor(machine).read_text("big.log")
|
|
assert opened.truncated is True
|
|
assert len(opened.text) == ssh_service.MAX_READ_BYTES
|
|
|
|
|
|
async def test_an_oversize_write_is_refused_not_truncated(machine):
|
|
"""`write_file` truncates because a model is told how many bytes it wrote.
|
|
Somebody pressing Save would lose the tail with nothing said."""
|
|
executor = _executor(machine)
|
|
(machine["path"] / "big.txt").write_text("small")
|
|
|
|
with pytest.raises(ExecError, match="Nothing was written"):
|
|
await executor.write_text("big.txt", "y" * (ssh_service.MAX_WRITE_BYTES + 1))
|
|
|
|
assert (machine["path"] / "big.txt").read_text() == "small"
|
|
|
|
|
|
# --- Conflict ---------------------------------------------------------------------
|
|
async def test_a_file_that_moved_underneath_refuses_the_save(machine):
|
|
import os
|
|
|
|
target = machine["path"] / "note.txt"
|
|
target.write_text("alpha\n")
|
|
executor = _executor(machine)
|
|
opened = await executor.read_text("note.txt")
|
|
|
|
# Somebody else's editor, a build, a checkout. The size differs, so this
|
|
# does not depend on the filesystem's mtime resolution.
|
|
target.write_text("something else entirely\n")
|
|
os.utime(target, (0, 0))
|
|
|
|
with pytest.raises(Conflict):
|
|
await executor.write_text("note.txt", "beta\n", if_unchanged=opened.revision)
|
|
|
|
assert target.read_text() == "something else entirely\n"
|
|
|
|
|
|
async def test_a_save_with_no_token_overwrites(machine):
|
|
"""Which is what Overwrite on the conflict card does."""
|
|
target = machine["path"] / "note.txt"
|
|
target.write_text("alpha\n")
|
|
await _executor(machine).write_text("note.txt", "beta\n")
|
|
assert target.read_text() == "beta\n"
|
|
|
|
|
|
async def test_a_new_file_can_be_created(machine):
|
|
"""Open a path that is not there, type, Save. The stat finds nothing and
|
|
there is nothing for the token to disagree with."""
|
|
executor = _executor(machine)
|
|
await executor.write_text("fresh.txt", "hello\n", if_unchanged="0:0")
|
|
assert (machine["path"] / "fresh.txt").read_text() == "hello\n"
|
|
|
|
|
|
async def test_the_revision_moves_after_a_write(machine):
|
|
"""Or the second save from the same tab would always conflict."""
|
|
target = machine["path"] / "note.txt"
|
|
target.write_text("alpha\n")
|
|
executor = _executor(machine)
|
|
|
|
opened = await executor.read_text("note.txt")
|
|
written = await executor.write_text(
|
|
"note.txt", "much longer contents\n", if_unchanged=opened.revision
|
|
)
|
|
assert written.revision != opened.revision
|
|
|
|
await executor.write_text("note.txt", "again\n", if_unchanged=written.revision)
|
|
assert target.read_text() == "again\n"
|
|
|
|
|
|
async def test_reading_something_that_is_not_there_says_so(machine):
|
|
with pytest.raises(ExecError, match="no file"):
|
|
await _executor(machine).read_text("nowhere.txt")
|