The other side reviews a draft (phase 13, step 1)
CI / secret scan (push) Successful in 5s
CI / fmt, clippy, tests (push) Successful in 49s
CI / release (push) Skipped

A lawyer sends a contract version to one of its parties with a link. On
it the other side marks the text up in an editor that keeps every change
visible, as tracked changes do: deleting our text crosses it out in red,
typing is marked as added in green, and the highlighter marks a passage
in yellow with a comment. Restore and Undo take marks back. They send it
back once a round; each later version comes on the same link.

- Migration 0014: review_links (token kept as a SHA-256, one open link a
  party) and review_rounds (the reply written once: their pieces, text,
  SHA-256, name, message; a trigger refuses any other change and every
  delete). Journal entries "sent for review", "review returned" (with the
  fingerprint of their text) and "review link closed"; audit entity
  "review".
- The reply is a list of pieces (same, ins, del, mark). The server checks
  that the pieces without the added ones are exactly the version sent, so
  nothing changes unseen (reviews::check).
- Our review page: their message, the changes numbered with the words
  around them and a box each, the highlights with comments, a git-style
  diff (line numbers, - and + lines, the changed words marked, three lines
  of context per hunk) and their markup. "Take the ticked changes" opens
  the contract page with the version form filled with our text plus those
  changes.
- static/review-editor.js: edits work on the pieces and redraw, so our
  text cannot vanish; keyboard compositions and edits the browser will not
  cancel are compared with the text and taken as edits.
- Property tests (any markup is accepted and taken back exactly; markup
  that changes our text unseen is refused), the fuzz text target runs
  reviews::check, integration tests, and the demo has the label's review
  of the recording agreement.
- PLAN: phase 13.
This commit is contained in:
grabowski committed 2026-11-23 15:31:43 +07:00
1 parent ff716eb9d3
commit a4155548b8
24 files changed
+3004 -6

No files matched your search

+1
View File
@@ -22,6 +22,7 @@ mod pages;
mod papers;
mod people;
mod records;
mod reviews;
#[test]
fn every_test_file_is_a_module() {
+17
View File
@@ -101,7 +101,24 @@ async fn every_page_renders_with_demo_data(pool: PgPool) {
.await
.unwrap();
// the label's review of the recording agreement (phase 13)
let (review_contract, review_round): (i64, i64) = sqlx::query_as(
"SELECT l.contract_id, r.id FROM review_rounds r JOIN review_links l ON l.id = r.link_id \
WHERE r.replied_at IS NOT NULL LIMIT 1",
)
.fetch_one(&pool)
.await
.unwrap();
let mut pages = vec![
(
format!("/contracts/{review_contract}/reviews/{review_round}"),
"As a diff".to_string(),
),
(
format!("/contracts/{review_contract}"),
"Review by the other side".to_string(),
),
(
"/originals".to_string(),
"Chiang Mai Immigration".to_string(),
+287
View File
@@ -0,0 +1,287 @@
//! The other side reviews a draft (phase 13): a version sent on a link,
//! marked up there and sent back, read as a diff and taken change by
//! change into the next version; every step in the journal.
use axum::http::StatusCode;
use sqlx::PgPool;
use crate::common::{App, local};
const SAME: &[(&str, &str)] = &[("sec-fetch-site", "same-origin")];
/// The value of the input `name` in a page.
fn input(page: &str, name: &str) -> String {
let at = page
.find(&format!("name=\"{name}\""))
.unwrap_or_else(|| panic!("no {name} in {page}"));
let rest = &page[at..];
let v = rest.find("value=\"").unwrap() + 7;
rest[v..v + rest[v..].find('"').unwrap()].to_string()
}
async fn performance(pool: &PgPool) -> (i64, i64, String) {
sqlx::query_as(
"SELECT c.id, p.id, v.body FROM contracts c \
JOIN contract_parties p ON p.contract_id = c.id \
JOIN contract_versions v ON v.contract_id = c.id \
WHERE c.title LIKE 'Performance agreement%' AND v.body <> '' \
ORDER BY v.version DESC LIMIT 1",
)
.fetch_one(pool)
.await
.unwrap()
}
async fn send(app: &App, contract: i64, party: i64) -> String {
let r = app
.post(
&format!("/contracts/{contract}/reviews"),
&[
("review_party", &party.to_string()),
("review_version", "1"),
("review_message", "Please look at the fee."),
],
)
.await;
assert_eq!(r.status, StatusCode::OK, "{}", r.text());
r.text()
}
#[sqlx::test(migrator = "visa::db::MIGRATOR")]
async fn the_other_side_marks_a_version_up_and_we_take_their_changes(pool: PgPool) {
let app = local(pool.clone());
let today = visa::dates::today();
visa::seed::demo(&pool, today).await.unwrap();
visa::seed::legal::demo(&pool, None, today).await.unwrap();
let (contract, party, body) = performance(&pool).await;
// the contract page offers the review; sending makes a link shown once
let t = app.get(&format!("/contracts/{contract}")).await.text();
assert!(
t.contains("id=\"reviews\"") && t.contains("id=\"send-form\""),
"{t}"
);
let made = send(&app, contract, party).await;
let at = made.find("/r/").expect("the link is shown");
let token: String = made[at + 3..]
.chars()
.take_while(|c| c.is_ascii_alphanumeric() || *c == '-' || *c == '_')
.collect();
assert_eq!(token.len(), 43);
let path = format!("/r/{token}");
// their page: the text in the editor, nothing of the staff's app
let r = app.get(&path).await;
assert_eq!(r.status, StatusCode::OK, "{}", r.text());
let page = r.text();
assert!(page.contains("id=\"rv-editor\"") && page.contains("Please look at the fee."));
assert!(page.contains("review-editor.js"));
assert!(!page.contains("/desk"), "no link into the back office");
let csrf = input(&page, "csrf");
let round = input(&page, "round");
// their marks: the fee changed, the travel highlighted, a line added
let (head, rest) = body.split_once("45,000").unwrap();
let (mid, tail) = rest.split_once("flight from Berlin").unwrap();
let pieces = serde_json::json!([
["same", head, ""],
["del", "45,000", ""],
["ins", "60,000", ""],
["same", mid, ""],
["mark", "flight from Berlin", "Business class, please."],
["same", tail, ""],
[
"ins",
"\n\n# 4. Sound\nThe Festival provides two CDJ-3000 and a DJM-A9.",
""
]
])
.to_string();
let post = |pieces: String, name: &'static str, csrf: String| {
let path = path.clone();
let round = round.clone();
let app = &app;
async move {
app.post_raw(
&path,
&[
("csrf", &csrf),
("round", &round),
("pieces", &pieces),
("name", name),
("message", "Our counter offer."),
],
SAME,
)
.await
}
};
// a change to our text without a mark is refused
let sneaky = serde_json::json!([["same", body.replace("45,000", "90,000"), ""]]).to_string();
let r = post(sneaky, "Ann", csrf.clone()).await;
assert_eq!(r.status, StatusCode::UNPROCESSABLE_ENTITY);
assert!(r.text().contains("does not match the version we sent"));
// without a name the marks come back in the editor
let r = post(pieces.clone(), "", csrf.clone()).await;
assert_eq!(r.status, StatusCode::UNPROCESSABLE_ENTITY);
let t = r.text();
assert!(
t.contains("Say who sends this back") && t.contains("60,000"),
"{t}"
);
// the staff's token is no token here
let r = post(pieces.clone(), "Ann", "x".repeat(43)).await;
assert_eq!(r.status, StatusCode::FORBIDDEN);
let r = post(pieces.clone(), "Ann Festival", csrf.clone()).await;
assert_eq!(r.status, StatusCode::SEE_OTHER, "{}", r.text());
// once a round
let r = post(pieces.clone(), "Ann Festival", csrf.clone()).await;
assert_eq!(r.status, StatusCode::CONFLICT);
let t = app.get(&path).await.text();
assert!(
t.contains("Thank you") && t.contains("2 changes and 1 highlight"),
"{t}"
);
// the journal has both, with the fingerprint of their text
let events: Vec<(String, String)> = sqlx::query_as(
"SELECT event, detail FROM journal WHERE contract_id = $1 AND event IN \
('sent for review', 'review returned') ORDER BY seq",
)
.bind(contract)
.fetch_all(&pool)
.await
.unwrap();
assert_eq!(events.len(), 2, "{events:?}");
assert!(
events[1]
.1
.contains("(Ann Festival) sent version 1 back with 2 changes and 1 highlight")
);
let sha: String = sqlx::query_scalar("SELECT reply_sha256 FROM review_rounds WHERE id = $1")
.bind(round.parse::<i64>().unwrap())
.fetch_one(&pool)
.await
.unwrap();
assert!(events[1].1.contains(&sha));
assert!(visa::journal::verify(&pool).await.unwrap().whole());
// our side: the changes, the highlight and the git view
let review = format!("/contracts/{contract}/reviews/{round}");
let t = app.get(&review).await.text();
assert!(t.contains("id=\"take-form\"") && t.contains("Business class, please."));
assert!(t.contains("class=\"gitdiff\"") && t.contains("Our counter offer."));
// take only the fee: the version form starts with it, without the sound
let r = app.post(&format!("{review}/take"), &[("take", "1")]).await;
assert_eq!(r.status, StatusCode::OK);
let t = r.text();
assert!(t.contains("60,000 THB") && !t.contains("CDJ-3000"), "{t}");
assert!(t.contains("1 of 2 changes by"));
// every ticked box counts
let t = app
.post(&format!("{review}/take"), &[("take", "1"), ("take", "2")])
.await
.text();
assert!(t.contains("60,000 THB") && t.contains("CDJ-3000"));
assert!(
t.contains("Ann Festival") || t.contains("changes of"),
"{t}"
);
// the next version goes on the same link, as a new round
let made = send(&app, contract, party).await;
assert!(
made.contains("on the link") && !made.contains("/r/"),
"{made}"
);
let t = app.get(&path).await.text();
assert!(t.contains("id=\"rv-editor\"") && t.contains("id=\"earlier\""));
// a reply to the old round is refused
let r = post(pieces, "Ann Festival", csrf).await;
assert_eq!(r.status, StatusCode::CONFLICT);
// the database keeps what they sent
assert!(
sqlx::query("UPDATE review_rounds SET reply_body = 'other' WHERE id = $1")
.bind(round.parse::<i64>().unwrap())
.execute(&pool)
.await
.is_err()
);
assert!(
sqlx::query("DELETE FROM review_rounds")
.execute(&pool)
.await
.is_err()
);
// closing the link ends it
let link: i64 = sqlx::query_scalar("SELECT id FROM review_links WHERE party_id = $1")
.bind(party)
.fetch_one(&pool)
.await
.unwrap();
let r = app
.post(
&format!("/contracts/{contract}/review-links/{link}/close"),
&[],
)
.await;
assert_eq!(r.status, StatusCode::SEE_OTHER);
assert_eq!(app.get(&path).await.status, StatusCode::GONE);
assert_eq!(
app.get(&format!("/r/{}", "a".repeat(43))).await.status,
StatusCode::NOT_FOUND
);
}
#[sqlx::test(migrator = "visa::db::MIGRATOR")]
async fn only_a_live_party_and_a_version_with_a_text_can_be_sent(pool: PgPool) {
let app = local(pool.clone());
let today = visa::dates::today();
visa::seed::demo(&pool, today).await.unwrap();
visa::seed::legal::demo(&pool, None, today).await.unwrap();
let (contract, party, _) = performance(&pool).await;
let other: i64 =
sqlx::query_scalar("SELECT id FROM contract_parties WHERE contract_id <> $1 LIMIT 1")
.bind(contract)
.fetch_one(&pool)
.await
.unwrap();
let to = format!("/contracts/{contract}/reviews");
for form in [
vec![
("review_party", other.to_string()),
("review_version", "1".to_string()),
],
vec![
("review_party", party.to_string()),
("review_version", "9".to_string()),
],
] {
let pairs: Vec<(&str, &str)> = form.iter().map(|(a, b)| (*a, b.as_str())).collect();
assert_eq!(
app.post(&to, &pairs).await.status,
StatusCode::UNPROCESSABLE_ENTITY
);
}
// the demo's label review reads as a diff with its four changes
let (c, round): (i64, i64) = sqlx::query_as(
"SELECT l.contract_id, r.id FROM review_rounds r JOIN review_links l ON l.id = r.link_id \
WHERE r.replied_at IS NOT NULL LIMIT 1",
)
.fetch_one(&pool)
.await
.unwrap();
let t = app
.get(&format!("/contracts/{c}/reviews/{round}"))
.await
.text();
assert!(
t.contains("id=\"change-4\"") && !t.contains("id=\"change-5\""),
"{t}"
);
assert!(t.contains("We need fifteen for the catalogue."));
let t = app.get(&format!("/contracts/{c}")).await.text();
assert!(t.contains("sent back") && t.contains("4 changes"));
}