7c8b21c2a5
suggestion.list|accept|reject (accept/reject write through the buffer path, viewers refused with forbidden, stale ranges answer stale_range, role from the bound server's member list cached 5 minutes, an unbound vault is the owner). Renaming a note moves its <note>.comments.json; a sync conflict on a sidecar merges the two by thread id instead of leaving two half-files (comments spec tasks 5 and 6). Built by MiniMax M3.1 Flash (OpenCode Zen free route); verified from a clean checkout of the staged files: notescraft-engine 816 tests pass, clippy -D warnings, xtask layers and tree clean. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
222 lines
9.5 KiB
Rust
222 lines
9.5 KiB
Rust
//! Integration tests of the `suggestion.*` commands through the registry: the
|
|
//! pending suggestions of a note, the write through the buffer path and the
|
|
//! refusals.
|
|
//!
|
|
//! The commands are driven the way the UI, the CLI and MCP drive them: by id,
|
|
//! with a params object.
|
|
|
|
mod common;
|
|
|
|
use common::{on_disk, seed, temp_vault};
|
|
use notescraft_engine::{Binding, Error, Registry, Session, register_core, save_binding};
|
|
use serde_json::{Value, json};
|
|
|
|
/// The note the tests review.
|
|
const NOTE: &str = "# Note\n\nThe sky is {++blue++} today.\n";
|
|
|
|
/// The mark of the note.
|
|
const MARK: &str = "{++blue++}";
|
|
|
|
/// The note the tests reject a mark of.
|
|
const GONE: &str = "# Gone\n\nThe sky is {--blue--} today.\n";
|
|
|
|
/// The mark of the second note.
|
|
const GONE_MARK: &str = "{--blue--}";
|
|
|
|
/// The text the note carries once its mark is accepted.
|
|
const ACCEPTED: &str = "# Note\n\nThe sky is blue today.\n";
|
|
|
|
/// A registry, a session and a vault with the notes of the tests.
|
|
fn fixture(name: &str) -> (Registry<Session>, Session, common::TempVault) {
|
|
let vault = temp_vault(name);
|
|
seed(vault.path(), &[("note.md", NOTE), ("gone.md", GONE)]);
|
|
let mut reg = Registry::new();
|
|
register_core(&mut reg).expect("register the core commands");
|
|
let session = Session::open(vault.path()).expect("open the vault");
|
|
(reg, session, vault)
|
|
}
|
|
|
|
/// The `{ code, message }` a command reported.
|
|
fn code(result: &Result<Value, Error>) -> String {
|
|
match result {
|
|
Err(Error::Command { source, .. }) => source.code.clone(),
|
|
other => panic!("expected a command error, got {other:?}"),
|
|
}
|
|
}
|
|
|
|
/// The byte offset of a mark in a note.
|
|
fn mark_start(text: &str, mark: &str) -> usize {
|
|
text.find(mark).expect("the mark is in the note")
|
|
}
|
|
|
|
/// Makes the session write as that user: the binding of the vault carries the
|
|
/// id of the account, so the commands take their author from it.
|
|
fn bind_as(session: &mut Session, user: &str) {
|
|
let binding =
|
|
Binding { server: "https://sync.example".to_owned(), vault_id: "v-1".to_owned(), device_id: "d-1".to_owned(), user_id: Some(user.to_owned()) };
|
|
save_binding(session.vault(), &binding).expect("bind the vault to an account");
|
|
}
|
|
|
|
/// Caches the members list of the vault, as the server sent it: the role of
|
|
/// the session comes from that list.
|
|
fn cache_members(session: &Session, members: &Value) {
|
|
let cache = json!({ "fetched_ms": now_ms(), "members": members });
|
|
let bytes = serde_json::to_vec(&cache).expect("the members cache");
|
|
session.vault().write_private(".notescraft/review/members.json", &bytes).expect("cache the members");
|
|
}
|
|
|
|
/// The clock, in milliseconds since the epoch.
|
|
fn now_ms() -> u64 {
|
|
let millis = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map_or(0, |since| since.as_millis());
|
|
u64::try_from(millis).unwrap_or(u64::MAX)
|
|
}
|
|
|
|
/// The list answers the pending suggestions of the note.
|
|
#[test]
|
|
fn the_list_answers_the_pending_suggestions() {
|
|
let (reg, mut session, _vault) = fixture("list");
|
|
let start = mark_start(NOTE, MARK);
|
|
|
|
let listed = reg.run("suggestion.list", &mut session, &json!({ "path": "note.md" })).expect("suggestion.list");
|
|
|
|
let suggestions = listed.get("suggestions").and_then(Value::as_array).expect("the suggestions of the note");
|
|
assert_eq!(suggestions.len(), 1);
|
|
let suggestion = suggestions.first().expect("the suggestion");
|
|
assert_eq!(suggestion.get("kind"), Some(&json!("added")));
|
|
assert_eq!(suggestion.get("start"), Some(&json!(start)));
|
|
assert_eq!(suggestion.get("end"), Some(&json!(start + MARK.len())));
|
|
assert_eq!(suggestion.get("text"), Some(&json!("blue")));
|
|
}
|
|
|
|
/// A note without marks has no suggestion.
|
|
#[test]
|
|
fn a_note_without_marks_has_no_suggestion() {
|
|
let (reg, mut session, vault) = fixture("plain");
|
|
seed(vault.path(), &[("plain.md", "# Plain\n\nNothing to review.\n")]);
|
|
|
|
let listed = reg.run("suggestion.list", &mut session, &json!({ "path": "plain.md" })).expect("suggestion.list");
|
|
|
|
assert_eq!(listed.get("suggestions"), Some(&json!([])));
|
|
}
|
|
|
|
/// Accepting writes the new text through the ordinary buffer path: the file
|
|
/// on disk and the buffer of the note hold the same text.
|
|
#[test]
|
|
fn accepting_writes_the_new_text_through_the_buffer_path() {
|
|
let (reg, mut session, vault) = fixture("accept");
|
|
let start = mark_start(NOTE, MARK);
|
|
|
|
let answered =
|
|
reg.run("suggestion.accept", &mut session, &json!({ "path": "note.md", "start": start, "end": start + MARK.len() })).expect("suggestion.accept");
|
|
|
|
assert_eq!(answered.get("text"), Some(&json!(ACCEPTED)));
|
|
assert_eq!(on_disk(vault.path(), "note.md"), ACCEPTED);
|
|
let opened = reg.run("buffer.open", &mut session, &json!({ "path": "note.md" })).expect("buffer.open");
|
|
assert_eq!(opened.get("text"), Some(&json!(ACCEPTED)), "the live room reads the same text");
|
|
assert_eq!(opened.get("dirty"), Some(&json!(false)), "the buffer was saved");
|
|
}
|
|
|
|
/// Rejecting a removed mark puts the old text back.
|
|
#[test]
|
|
fn rejecting_puts_the_old_text_back() {
|
|
let (reg, mut session, vault) = fixture("reject");
|
|
let start = mark_start(GONE, GONE_MARK);
|
|
|
|
let answered =
|
|
reg.run("suggestion.reject", &mut session, &json!({ "path": "gone.md", "start": start, "end": start + GONE_MARK.len() })).expect("suggestion.reject");
|
|
|
|
assert_eq!(answered.get("text"), Some(&json!("# Gone\n\nThe sky is blue today.\n")));
|
|
assert_eq!(on_disk(vault.path(), "gone.md"), "# Gone\n\nThe sky is blue today.\n");
|
|
}
|
|
|
|
/// A range that holds no mark is refused and the note stays the same.
|
|
#[test]
|
|
fn a_stale_range_is_refused_and_the_note_stays_the_same() {
|
|
let (reg, mut session, vault) = fixture("stale");
|
|
|
|
let result = reg.run("suggestion.accept", &mut session, &json!({ "path": "note.md", "start": 0, "end": 5 }));
|
|
|
|
assert_eq!(code(&result), "stale_range");
|
|
assert_eq!(on_disk(vault.path(), "note.md"), NOTE, "the refused accept changed nothing");
|
|
}
|
|
|
|
/// A note whose mark was accepted is byte for byte the same note written
|
|
/// without the mark.
|
|
#[test]
|
|
fn an_accepted_note_is_the_note_written_without_the_mark() {
|
|
let (reg, mut session, vault) = fixture("identical");
|
|
seed(vault.path(), &[("plain.md", ACCEPTED)]);
|
|
let start = mark_start(NOTE, MARK);
|
|
|
|
reg.run("suggestion.accept", &mut session, &json!({ "path": "note.md", "start": start, "end": start + MARK.len() })).expect("suggestion.accept");
|
|
|
|
assert_eq!(on_disk(vault.path(), "note.md"), on_disk(vault.path(), "plain.md"));
|
|
}
|
|
|
|
/// A viewer may not accept a suggestion; an editor may.
|
|
#[test]
|
|
fn a_viewer_is_refused_and_an_editor_is_not() {
|
|
let (reg, mut session, vault) = fixture("role");
|
|
bind_as(&mut session, "u_ana");
|
|
cache_members(&session, &json!([{ "user_id": "u_ana", "role": "viewer" }]));
|
|
let start = mark_start(NOTE, MARK);
|
|
|
|
let result = reg.run("suggestion.accept", &mut session, &json!({ "path": "note.md", "start": start, "end": start + MARK.len() }));
|
|
|
|
assert_eq!(code(&result), "forbidden");
|
|
assert_eq!(on_disk(vault.path(), "note.md"), NOTE, "the refused accept changed nothing");
|
|
|
|
// The same user as an editor may accept.
|
|
cache_members(&session, &json!([{ "user_id": "u_ana", "role": "editor" }]));
|
|
reg.run("suggestion.accept", &mut session, &json!({ "path": "note.md", "start": start, "end": start + MARK.len() })).expect("suggestion.accept");
|
|
assert_eq!(on_disk(vault.path(), "note.md"), ACCEPTED);
|
|
}
|
|
|
|
/// A vault with no server has no role: the local user owns it and may accept.
|
|
#[test]
|
|
fn a_local_vault_may_accept() {
|
|
let (reg, mut session, vault) = fixture("local");
|
|
let start = mark_start(NOTE, MARK);
|
|
|
|
reg.run("suggestion.accept", &mut session, &json!({ "path": "note.md", "start": start, "end": start + MARK.len() })).expect("suggestion.accept");
|
|
|
|
assert_eq!(on_disk(vault.path(), "note.md"), ACCEPTED);
|
|
}
|
|
|
|
/// A read-only session refuses every write command.
|
|
#[test]
|
|
fn a_read_only_session_refuses_the_write_commands() {
|
|
let vault = temp_vault("read-only-suggestions");
|
|
seed(vault.path(), &[("note.md", NOTE)]);
|
|
let mut reg = Registry::new();
|
|
register_core(&mut reg).expect("register the core commands");
|
|
let mut session = Session::open_read_only(vault.path()).expect("open read-only");
|
|
|
|
for id in ["suggestion.accept", "suggestion.reject"] {
|
|
assert!(!reg.is_enabled(id, &session), "{id} must be disabled");
|
|
assert!(matches!(reg.run(id, &mut session, &json!({ "path": "note.md" })), Err(Error::Disabled(_))), "{id} must not run");
|
|
}
|
|
assert!(reg.is_enabled("suggestion.list", &session));
|
|
assert!(reg.run("suggestion.list", &mut session, &json!({ "path": "note.md" })).is_ok());
|
|
assert_eq!(on_disk(vault.path(), "note.md"), NOTE, "a read-only session writes nothing");
|
|
}
|
|
|
|
/// Hostile params are refused, and nothing is written.
|
|
#[test]
|
|
fn hostile_params_are_refused() {
|
|
let (reg, mut session, vault) = fixture("hostile");
|
|
let cases = [
|
|
json!({ "path": "note.md\u{0}.md", "start": 0, "end": 1 }),
|
|
json!({ "path": "../escape.md", "start": 0, "end": 1 }),
|
|
json!({ "path": "note.md", "start": -1, "end": 2 }),
|
|
json!({ "path": "note.md", "start": 0, "end": 999_999 }),
|
|
json!({ "path": "note.md", "start": 5, "end": 2 }),
|
|
];
|
|
for params in cases {
|
|
let result = reg.run("suggestion.accept", &mut session, ¶ms);
|
|
assert!(result.is_err(), "suggestion.accept must refuse {params}");
|
|
assert!(!matches!(result, Err(Error::Panicked(_))), "suggestion.accept must not panic on {params}");
|
|
}
|
|
assert_eq!(on_disk(vault.path(), "note.md"), NOTE);
|
|
}
|