mirror of
https://github.com/Dasemu/Synapse.git
synced 2026-11-26 23:36:20 +00:00
feat(mcp): add synapse-mcp server with stdio binary
Read tools (search, get_page, get_journal, list_pages, backlinks, related, list_tags, list_types, hubs, timeline, journal_links) and page-curation write tools behind --allow-write (create_page, append_blocks, update_block, delete_block, set_page_type, set_page_tags, set_page_hub). Journals are read-only. Reads run in a rolled-back transaction; writes in BEGIN IMMEDIATE through the Graph API so they are logged as ops and sync. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
c8f7cdaf92
commit
c0f0716d3b
9 files changed
+3308
No files matched your search
@@ -0,0 +1,163 @@
|
||||
//! Smoke test of the `synapse-mcp` binary over stdio: initialize, tools/list, tools/call.
|
||||
|
||||
use serde_json::{Value, json};
|
||||
use std::io::{BufRead, BufReader, Write};
|
||||
use std::process::{Child, ChildStdin, Command, Stdio};
|
||||
use std::sync::mpsc;
|
||||
use std::time::Duration;
|
||||
use synapse_core::{Graph, NewPage, Placement};
|
||||
|
||||
struct Server {
|
||||
child: Child,
|
||||
stdin: ChildStdin,
|
||||
lines: mpsc::Receiver<String>,
|
||||
}
|
||||
|
||||
impl Server {
|
||||
fn start(graph: &std::path::Path, extra: &[&str]) -> Self {
|
||||
let mut child = Command::new(env!("CARGO_BIN_EXE_synapse-mcp"))
|
||||
.arg("--graph")
|
||||
.arg(graph)
|
||||
.args(extra)
|
||||
.stdin(Stdio::piped())
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(Stdio::null())
|
||||
.spawn()
|
||||
.unwrap();
|
||||
let stdin = child.stdin.take().unwrap();
|
||||
let stdout = child.stdout.take().unwrap();
|
||||
let (tx, lines) = mpsc::channel();
|
||||
std::thread::spawn(move || {
|
||||
for l in BufReader::new(stdout).lines() {
|
||||
let Ok(l) = l else { break };
|
||||
if tx.send(l).is_err() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
Self {
|
||||
child,
|
||||
stdin,
|
||||
lines,
|
||||
}
|
||||
}
|
||||
|
||||
fn send(&mut self, msg: Value) {
|
||||
writeln!(self.stdin, "{msg}").unwrap();
|
||||
self.stdin.flush().unwrap();
|
||||
}
|
||||
|
||||
/// The response to request `id` (skipping notifications).
|
||||
fn call(&mut self, id: u64, method: &str, params: Value) -> Value {
|
||||
self.send(json!({"jsonrpc": "2.0", "id": id, "method": method, "params": params}));
|
||||
loop {
|
||||
let line = self
|
||||
.lines
|
||||
.recv_timeout(Duration::from_secs(20))
|
||||
.expect("server reply");
|
||||
let v: Value = serde_json::from_str(&line).unwrap();
|
||||
if v["id"] == json!(id) {
|
||||
return v;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn init(&mut self) -> Value {
|
||||
let r = self.call(
|
||||
1,
|
||||
"initialize",
|
||||
json!({"protocolVersion": "2025-06-18", "capabilities": {},
|
||||
"clientInfo": {"name": "test", "version": "1"}}),
|
||||
);
|
||||
self.send(json!({"jsonrpc": "2.0", "method": "notifications/initialized"}));
|
||||
r
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Server {
|
||||
fn drop(&mut self) {
|
||||
let _ = self.child.kill();
|
||||
let _ = self.child.wait();
|
||||
}
|
||||
}
|
||||
|
||||
fn tool_names(list: &Value) -> Vec<String> {
|
||||
list["result"]["tools"]
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.map(|t| t["name"].as_str().unwrap().to_string())
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stdio_initialize_list_and_call() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
{
|
||||
let g = Graph::open(dir.path()).unwrap();
|
||||
let p = g.create_page(NewPage::titled("Rust")).unwrap();
|
||||
g.insert_block(
|
||||
p.id,
|
||||
Placement::LastChildOf(None),
|
||||
"ownership and borrowing",
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
let mut s = Server::start(dir.path(), &[]);
|
||||
let init = s.init();
|
||||
assert_eq!(
|
||||
init["result"]["serverInfo"]["name"], "synapse-mcp",
|
||||
"{init}"
|
||||
);
|
||||
let names = tool_names(&s.call(2, "tools/list", json!({})));
|
||||
assert!(names.contains(&"search".to_string()), "{names:?}");
|
||||
assert!(
|
||||
!names.contains(&"create_page".to_string()),
|
||||
"read-only: {names:?}"
|
||||
);
|
||||
let r = s.call(
|
||||
3,
|
||||
"tools/call",
|
||||
json!({"name": "search", "arguments": {"query": "borrowing"}}),
|
||||
);
|
||||
let text = r["result"]["content"][0]["text"].as_str().unwrap();
|
||||
assert!(text.contains("[[Rust]]"), "{r}");
|
||||
// Write tools are not callable read-only.
|
||||
let r = s.call(
|
||||
4,
|
||||
"tools/call",
|
||||
json!({"name": "create_page", "arguments": {"title": "X"}}),
|
||||
);
|
||||
assert!(
|
||||
r.get("error").is_some() || r["result"]["isError"] == json!(true),
|
||||
"{r}"
|
||||
);
|
||||
drop(s);
|
||||
|
||||
let mut s = Server::start(dir.path(), &["--allow-write"]);
|
||||
s.init();
|
||||
let names = tool_names(&s.call(2, "tools/list", json!({})));
|
||||
assert!(names.contains(&"create_page".to_string()), "{names:?}");
|
||||
let r = s.call(
|
||||
3,
|
||||
"tools/call",
|
||||
json!({"name": "create_page", "arguments": {"title": "Borrow checker", "type": "Concept"}}),
|
||||
);
|
||||
assert_eq!(r["result"]["isError"], json!(false), "{r}");
|
||||
drop(s);
|
||||
let g = Graph::open(dir.path()).unwrap();
|
||||
let p = g.page_by_title("borrow checker").unwrap().unwrap();
|
||||
assert_eq!(p.page_type(), Some("Concept"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn refuses_a_missing_graph() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let out = Command::new(env!("CARGO_BIN_EXE_synapse-mcp"))
|
||||
.arg("--graph")
|
||||
.arg(dir.path().join("nope"))
|
||||
.output()
|
||||
.unwrap();
|
||||
assert!(!out.status.success());
|
||||
assert!(!dir.path().join("nope").exists(), "must not create a graph");
|
||||
}
|
||||
@@ -0,0 +1,622 @@
|
||||
//! Every tool on a temp graph, through the same read/write wrappers the server uses.
|
||||
|
||||
use chrono::NaiveDate;
|
||||
use synapse_core::{Graph, NewPage, Placement};
|
||||
use synapse_mcp::params::*;
|
||||
use synapse_mcp::tools::{self, ToolResult};
|
||||
use synapse_mcp::{read_job, write_job};
|
||||
|
||||
fn day(s: &str) -> NaiveDate {
|
||||
NaiveDate::parse_from_str(s, "%Y-%m-%d").unwrap()
|
||||
}
|
||||
|
||||
/// 2026-03-10 is "today". Journals on 03-08 and 03-10; a typed+tagged book in hub "Reading".
|
||||
fn fixture() -> (tempfile::TempDir, Graph) {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let g = Graph::open(dir.path()).unwrap();
|
||||
let noon = |d: &str| {
|
||||
day(d)
|
||||
.and_hms_opt(12, 0, 0)
|
||||
.unwrap()
|
||||
.and_local_timezone(chrono::Local)
|
||||
.unwrap()
|
||||
.timestamp_millis()
|
||||
};
|
||||
g.set_now(Some(noon("2026-03-08")));
|
||||
let j1 = g.journal(day("2026-03-08")).unwrap();
|
||||
let b = g
|
||||
.insert_block(
|
||||
j1.id,
|
||||
Placement::LastChildOf(None),
|
||||
"Started reading [[Dune]] by Frank Herbert",
|
||||
)
|
||||
.unwrap();
|
||||
g.insert_block(
|
||||
j1.id,
|
||||
Placement::LastChildOf(Some(b.id)),
|
||||
"the canción of spice",
|
||||
)
|
||||
.unwrap();
|
||||
let hub = g.create_hub("Reading", "Books and articles").unwrap();
|
||||
let dune = g.page_by_title("Dune").unwrap().unwrap();
|
||||
g.set_page_type(dune.id, Some("Book")).unwrap();
|
||||
g.set_page_tags(dune.id, vec!["scifi".into()]).unwrap();
|
||||
g.add_to_hub(dune.id, hub.id).unwrap();
|
||||
g.insert_block(
|
||||
dune.id,
|
||||
Placement::LastChildOf(None),
|
||||
"Arrakis is a desert planet",
|
||||
)
|
||||
.unwrap();
|
||||
g.set_now(Some(noon("2026-03-10")));
|
||||
let j2 = g.journal(day("2026-03-10")).unwrap();
|
||||
g.insert_block(
|
||||
j2.id,
|
||||
Placement::LastChildOf(None),
|
||||
"Talked about Dune and [[Gardening]] with Ana",
|
||||
)
|
||||
.unwrap();
|
||||
(dir, g)
|
||||
}
|
||||
|
||||
fn read(g: &Graph, f: impl Fn(&Graph) -> ToolResult + Send + Sync + 'static) -> String {
|
||||
read_job(f)(g).unwrap_or_else(|e| panic!("tool failed: {e}"))
|
||||
}
|
||||
|
||||
fn read_err(g: &Graph, f: impl Fn(&Graph) -> ToolResult + Send + Sync + 'static) -> String {
|
||||
read_job(f)(g).expect_err("expected an error").message
|
||||
}
|
||||
|
||||
fn write(g: &Graph, f: impl Fn(&Graph) -> ToolResult + Send + Sync + 'static) -> ToolResult {
|
||||
write_job(f)(g)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn search_finds_blocks_and_titles_with_filters() {
|
||||
let (_d, g) = fixture();
|
||||
let out = read(&g, |g| {
|
||||
tools::search(
|
||||
g,
|
||||
&SearchParams {
|
||||
query: "dune".into(),
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
});
|
||||
assert!(out.starts_with("3 results"), "{out}");
|
||||
assert!(
|
||||
out.lines().nth(1).unwrap().contains("[[Dune]] (page"),
|
||||
"{out}"
|
||||
);
|
||||
assert!(out.contains("[[2026-03-08]] (("), "{out}");
|
||||
// Accent-insensitive, journals only, date filter.
|
||||
let out = read(&g, |g| {
|
||||
tools::search(
|
||||
g,
|
||||
&SearchParams {
|
||||
query: "cancion".into(),
|
||||
journals: Some("only".into()),
|
||||
from: Some("2026-03-08".into()),
|
||||
to: Some("2026-03-08".into()),
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
});
|
||||
assert!(out.contains("canción of spice"), "{out}");
|
||||
// Tag filter keeps only the tagged page's content.
|
||||
let out = read(&g, |g| {
|
||||
tools::search(
|
||||
g,
|
||||
&SearchParams {
|
||||
query: "arrakis".into(),
|
||||
tag: Some("scifi".into()),
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
});
|
||||
assert!(out.starts_with("1 results"), "{out}");
|
||||
let out = read(&g, |g| {
|
||||
tools::search(
|
||||
g,
|
||||
&SearchParams {
|
||||
query: "dune".into(),
|
||||
limit: Some(1),
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
});
|
||||
assert!(out.contains("next cursor: 1"), "{out}");
|
||||
assert!(read_err(&g, |g| tools::search(g, &SearchParams::default())).contains("empty"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_page_renders_header_and_outline() {
|
||||
let (_d, g) = fixture();
|
||||
let out = read(&g, |g| {
|
||||
tools::get_page(
|
||||
g,
|
||||
&GetPageParams {
|
||||
page: "dune".into(),
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
});
|
||||
assert!(out.starts_with("# Dune\n"), "{out}");
|
||||
assert!(out.contains("- type: Book"), "{out}");
|
||||
assert!(out.contains("- tags: scifi"), "{out}");
|
||||
assert!(out.contains("- hub: Reading"), "{out}");
|
||||
assert!(out.contains("born in [[2026-03-08]]"), "{out}");
|
||||
assert!(out.contains("- Arrakis is a desert planet (("), "{out}");
|
||||
// Journal by date, subtree and depth, no ids.
|
||||
let out = read(&g, |g| {
|
||||
tools::get_page(
|
||||
g,
|
||||
&GetPageParams {
|
||||
page: "2026-03-08".into(),
|
||||
max_depth: Some(0),
|
||||
ids: Some(false),
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
});
|
||||
assert!(
|
||||
out.contains("- Started reading [[Dune]] by Frank Herbert\n"),
|
||||
"{out}"
|
||||
);
|
||||
assert!(!out.contains("spice"), "{out}");
|
||||
// Hub lists members.
|
||||
let out = read(&g, |g| {
|
||||
tools::get_page(
|
||||
g,
|
||||
&GetPageParams {
|
||||
page: "Reading".into(),
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
});
|
||||
assert!(out.contains("## Members (1)\n- [[Dune]]"), "{out}");
|
||||
let e = read_err(&g, |g| {
|
||||
tools::get_page(
|
||||
g,
|
||||
&GetPageParams {
|
||||
page: "Dun".into(),
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
});
|
||||
assert!(e.contains("did you mean: [[Dune]]"), "{e}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_journal_day_and_range_never_create() {
|
||||
let (_d, g) = fixture();
|
||||
let seq = g.last_seq().unwrap();
|
||||
let out = read(&g, |g| {
|
||||
tools::get_journal(
|
||||
g,
|
||||
&GetJournalParams {
|
||||
date: Some("today".into()),
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
});
|
||||
assert!(out.starts_with("# 2026-03-10\n- Talked about"), "{out}");
|
||||
let out = read(&g, |g| {
|
||||
tools::get_journal(
|
||||
g,
|
||||
&GetJournalParams {
|
||||
date: Some("2026-03-09".into()),
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
});
|
||||
assert!(out.contains("(no journal)"), "{out}");
|
||||
let out = read(&g, |g| {
|
||||
tools::get_journal(
|
||||
g,
|
||||
&GetJournalParams {
|
||||
from: Some("2026-03-01".into()),
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
});
|
||||
let a = out.find("# 2026-03-08").unwrap();
|
||||
let b = out.find("# 2026-03-10").unwrap();
|
||||
assert!(a < b, "oldest first: {out}");
|
||||
assert_eq!(g.last_seq().unwrap(), seq, "reads must not write");
|
||||
assert!(
|
||||
g.get_page(synapse_core::store::journal_id(day("2026-03-09")))
|
||||
.unwrap()
|
||||
.is_none()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn list_pages_filters_and_sorts() {
|
||||
let (_d, g) = fixture();
|
||||
let out = read(&g, |g| tools::list_pages(g, &ListPagesParams::default()));
|
||||
assert!(
|
||||
out.contains("- [[Dune]] · type: Book · tags: scifi · hub: Reading"),
|
||||
"{out}"
|
||||
);
|
||||
assert!(!out.contains("[[2026-03-08]]"), "journals excluded: {out}");
|
||||
let out = read(&g, |g| {
|
||||
tools::list_pages(
|
||||
g,
|
||||
&ListPagesParams {
|
||||
untyped: Some(true),
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
});
|
||||
assert!(
|
||||
out.contains("[[Gardening]]") && !out.contains("[[Dune]]"),
|
||||
"{out}"
|
||||
);
|
||||
assert!(!out.contains("[[Reading]]"), "hubs excluded: {out}");
|
||||
let out = read(&g, |g| {
|
||||
tools::list_pages(
|
||||
g,
|
||||
&ListPagesParams {
|
||||
page_type: Some("book".into()),
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
});
|
||||
assert!(out.starts_with("1 pages"), "{out}");
|
||||
let out = read(&g, |g| {
|
||||
tools::list_pages(
|
||||
g,
|
||||
&ListPagesParams {
|
||||
journals: Some("only".into()),
|
||||
sort: Some("title".into()),
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
});
|
||||
assert!(
|
||||
out.find("2026-03-08").unwrap() < out.find("2026-03-10").unwrap(),
|
||||
"{out}"
|
||||
);
|
||||
let out = read(&g, |g| {
|
||||
tools::list_pages(
|
||||
g,
|
||||
&ListPagesParams {
|
||||
created_since: Some("2026-03-10".into()),
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
});
|
||||
assert!(
|
||||
out.contains("[[Gardening]]") && !out.contains("[[Dune]]"),
|
||||
"{out}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn backlinks_and_related() {
|
||||
let (_d, g) = fixture();
|
||||
let out = read(&g, |g| {
|
||||
tools::backlinks(
|
||||
g,
|
||||
&PageRefParams {
|
||||
page: "Dune".into(),
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
});
|
||||
assert!(
|
||||
out.starts_with("1 linked references to [[Dune]] from 1 pages"),
|
||||
"{out}"
|
||||
);
|
||||
assert!(
|
||||
out.contains("## [[2026-03-08]]\n- Started reading"),
|
||||
"{out}"
|
||||
);
|
||||
let out = read(&g, |g| {
|
||||
tools::related(
|
||||
g,
|
||||
&PageRefParams {
|
||||
page: "Dune".into(),
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
});
|
||||
assert!(out.contains("[[2026-03-08]]"), "{out}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tags_types_hubs_timeline() {
|
||||
let (_d, g) = fixture();
|
||||
let out = read(&g, |g| tools::list_tags(g, &ListParams::default()));
|
||||
assert!(out.contains("- scifi — 1 (1/0/0)"), "{out}");
|
||||
assert!(out.contains("- Book — 1 (0/1/0)"), "{out}");
|
||||
assert!(out.contains("- Reading — 1 (0/0/1)"), "{out}");
|
||||
let out = read(&g, |g| tools::list_types(g, &ListParams::default()));
|
||||
assert!(
|
||||
out.starts_with("1 types in use:\n- Book — 1 pages"),
|
||||
"{out}"
|
||||
);
|
||||
let out = read(&g, |g| {
|
||||
tools::hubs(
|
||||
g,
|
||||
&HubsParams {
|
||||
members: Some(true),
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
});
|
||||
assert!(
|
||||
out.contains("- [[Reading]] (1 notes) — Books and articles\n - Dune"),
|
||||
"{out}"
|
||||
);
|
||||
let out = read(&g, |g| {
|
||||
tools::hubs(
|
||||
g,
|
||||
&HubsParams {
|
||||
hub: Some("Reading".into()),
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
});
|
||||
assert!(
|
||||
out.contains("- [[Dune]] · type: Book · tags: scifi"),
|
||||
"{out}"
|
||||
);
|
||||
let out = read(&g, |g| tools::timeline(g, &RangeParams::default()));
|
||||
assert!(out.contains("## 2026-03-08 (journal)\n- born: "), "{out}");
|
||||
assert!(out.contains("[[Dune]]"), "{out}");
|
||||
let e = read_err(&g, |g| {
|
||||
tools::timeline(
|
||||
g,
|
||||
&RangeParams {
|
||||
from: Some("2025-01-01".into()),
|
||||
to: None,
|
||||
},
|
||||
)
|
||||
});
|
||||
assert!(e.contains("range too long"), "{e}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn journal_links_reports_status_and_unlinked_mentions() {
|
||||
let (_d, g) = fixture();
|
||||
let out = read(&g, |g| tools::journal_links(g, &RangeParams::default()));
|
||||
assert!(
|
||||
out.contains("## 2026-03-08\n- linked [[Dune]] · type: Book"),
|
||||
"{out}"
|
||||
);
|
||||
assert!(
|
||||
out.contains("- linked [[Gardening]] · untyped · untagged · empty"),
|
||||
"{out}"
|
||||
);
|
||||
assert!(out.contains("- mentioned (not linked) [[Dune]]"), "{out}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn write_tools_curate_pages() {
|
||||
let (_d, g) = fixture();
|
||||
let out = write(&g, |g| {
|
||||
tools::create_page(
|
||||
g,
|
||||
&CreatePageParams {
|
||||
title: "Frank Herbert".into(),
|
||||
page_type: Some("Person".into()),
|
||||
tags: Some(vec!["author".into(), "scifi".into()]),
|
||||
hub: Some("Reading".into()),
|
||||
content: Some("- Wrote [[Dune]]\n - 1965\n- born:: 1920".into()),
|
||||
source_journal: Some("2026-03-08".into()),
|
||||
},
|
||||
)
|
||||
})
|
||||
.unwrap();
|
||||
assert!(out.starts_with("Created [[Frank Herbert]]"), "{out}");
|
||||
assert!(out.contains("with 3 blocks"), "{out}");
|
||||
let p = g.page_by_title("frank herbert").unwrap().unwrap();
|
||||
assert_eq!(p.page_type(), Some("Person"));
|
||||
assert_eq!(p.tags, vec!["author", "scifi"]);
|
||||
assert_eq!(p.origin_journal, Some(day("2026-03-08")));
|
||||
let tree = g.page_tree(p.id).unwrap();
|
||||
assert_eq!(tree[1].0, 1, "nested child");
|
||||
// Duplicate title.
|
||||
let e = write(&g, |g| {
|
||||
tools::create_page(
|
||||
g,
|
||||
&CreatePageParams {
|
||||
title: "Dune".into(),
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
})
|
||||
.unwrap_err();
|
||||
assert!(e.message.contains("already exists"), "{e}");
|
||||
|
||||
let out = write(&g, |g| {
|
||||
tools::append_blocks(
|
||||
g,
|
||||
&AppendBlocksParams {
|
||||
page: "Frank Herbert".into(),
|
||||
content: "- Also wrote [[Children of Dune]]".into(),
|
||||
parent: None,
|
||||
},
|
||||
)
|
||||
})
|
||||
.unwrap();
|
||||
assert!(out.starts_with("Appended 1 blocks"), "{out}");
|
||||
let first = g.page_tree(p.id).unwrap()[0].1.id;
|
||||
let out = write(&g, move |g| {
|
||||
tools::update_block(
|
||||
g,
|
||||
&UpdateBlockParams {
|
||||
block: format!("(({first}))"),
|
||||
content: "Wrote [[Dune]] (1965)".into(),
|
||||
},
|
||||
)
|
||||
})
|
||||
.unwrap();
|
||||
assert!(out.contains("Wrote [[Dune]] (1965)"), "{out}");
|
||||
let out = write(&g, move |g| {
|
||||
tools::delete_block(
|
||||
g,
|
||||
&DeleteBlockParams {
|
||||
block: first.to_string(),
|
||||
},
|
||||
)
|
||||
})
|
||||
.unwrap();
|
||||
assert!(out.contains("and 1 children"), "{out}");
|
||||
|
||||
write(&g, |g| {
|
||||
tools::set_page_type(
|
||||
g,
|
||||
&SetPageTypeParams {
|
||||
page: "Gardening".into(),
|
||||
page_type: Some("Topic".into()),
|
||||
},
|
||||
)
|
||||
})
|
||||
.unwrap();
|
||||
let out = write(&g, |g| {
|
||||
tools::set_page_tags(
|
||||
g,
|
||||
&SetPageTagsParams {
|
||||
page: "Gardening".into(),
|
||||
add: Some(vec!["hobby".into(), "outdoors".into()]),
|
||||
remove: Some(vec!["Outdoors".into()]),
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
})
|
||||
.unwrap();
|
||||
assert_eq!(out, "[[Gardening]] tags: hobby\n");
|
||||
let out = write(&g, |g| {
|
||||
tools::set_page_hub(
|
||||
g,
|
||||
&SetPageHubParams {
|
||||
page: "Gardening".into(),
|
||||
hub: Some("Reading".into()),
|
||||
},
|
||||
)
|
||||
})
|
||||
.unwrap();
|
||||
assert!(out.contains("now in hub [[Reading]]"), "{out}");
|
||||
write(&g, |g| {
|
||||
tools::set_page_hub(
|
||||
g,
|
||||
&SetPageHubParams {
|
||||
page: "Gardening".into(),
|
||||
hub: None,
|
||||
},
|
||||
)
|
||||
})
|
||||
.unwrap();
|
||||
let gp = g.page_by_title("Gardening").unwrap().unwrap();
|
||||
assert_eq!(gp.page_type(), Some("Topic"));
|
||||
assert!(gp.hubs.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn write_tools_refuse_journals_and_roll_back_on_error() {
|
||||
let (_d, g) = fixture();
|
||||
let jblock = g
|
||||
.page_tree(synapse_core::store::journal_id(day("2026-03-10")))
|
||||
.unwrap()[0]
|
||||
.1
|
||||
.id;
|
||||
let seq = g.last_seq().unwrap();
|
||||
for e in [
|
||||
write(&g, |g| {
|
||||
tools::append_blocks(
|
||||
g,
|
||||
&AppendBlocksParams {
|
||||
page: "today".into(),
|
||||
content: "- x".into(),
|
||||
parent: None,
|
||||
},
|
||||
)
|
||||
}),
|
||||
write(&g, move |g| {
|
||||
tools::update_block(
|
||||
g,
|
||||
&UpdateBlockParams {
|
||||
block: jblock.to_string(),
|
||||
content: "x".into(),
|
||||
},
|
||||
)
|
||||
}),
|
||||
write(&g, move |g| {
|
||||
tools::delete_block(
|
||||
g,
|
||||
&DeleteBlockParams {
|
||||
block: jblock.to_string(),
|
||||
},
|
||||
)
|
||||
}),
|
||||
write(&g, |g| {
|
||||
tools::set_page_tags(
|
||||
g,
|
||||
&SetPageTagsParams {
|
||||
page: "2026-03-08".into(),
|
||||
tags: Some(vec!["x".into()]),
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
}),
|
||||
write(&g, |g| {
|
||||
tools::create_page(
|
||||
g,
|
||||
&CreatePageParams {
|
||||
title: "2026-04-01".into(),
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
}),
|
||||
// Fails after creating the page (unknown hub is checked first; bad tag is not):
|
||||
// the whole call must roll back.
|
||||
write(&g, |g| {
|
||||
tools::create_page(
|
||||
g,
|
||||
&CreatePageParams {
|
||||
title: "Half done".into(),
|
||||
tags: Some(vec!["x".repeat(500)]),
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
}),
|
||||
write(&g, |g| {
|
||||
tools::create_page(
|
||||
g,
|
||||
&CreatePageParams {
|
||||
title: "From missing day".into(),
|
||||
source_journal: Some("2026-03-09".into()),
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
}),
|
||||
] {
|
||||
assert!(e.is_err(), "{e:?}");
|
||||
}
|
||||
assert_eq!(g.last_seq().unwrap(), seq, "nothing written");
|
||||
assert!(g.page_by_title("Half done").unwrap().is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reads_are_rolled_back() {
|
||||
let (_d, g) = fixture();
|
||||
let seq = g.last_seq().unwrap();
|
||||
// A read job that tries to write is undone.
|
||||
let out = read(&g, |g| {
|
||||
g.create_page(NewPage::titled("Sneaky"))?;
|
||||
Ok("ok".into())
|
||||
});
|
||||
assert_eq!(out, "ok");
|
||||
assert_eq!(g.last_seq().unwrap(), seq);
|
||||
assert!(g.page_by_title("Sneaky").unwrap().is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn resources_render_pages() {
|
||||
let (_d, g) = fixture();
|
||||
let out = read(&g, |g| tools::page_resource(g, "Dune"));
|
||||
assert!(out.contains("- Arrakis is a desert planet\n"), "{out}");
|
||||
}
|
||||
Reference in new issue
Block a user