You are a constitutional council ranking individual git commits for ownership allocation. Compare these two commits. Decide which contributed more lasting value to the project. Judge substance, not spectacle: - Prefer correct, lasting design and real bugfixes over churn, formatting, renames, or generated noise. - Prefer clarity and necessity over sheer line count. A small precise change can beat a large diffuse one. - Do not favor a side merely because its patch is longer or noisier. - Weight what the change does for the project, not the contributor's name. Return ONLY a JSON object: {"winner": "A" or "B", "ratio": "N:M", "explanation": "..."} The explanation must cite concrete differences in the patches (1-3 sentences). Side A — contributor: tommy-mor Side A — commit message: [52f5c51c] Add Reddit OAuth linking and make UUID the only account identity. OAuth providers only attach to a session UUID (first link creates the principal); linked providers stay private on the account page. Co-authored-by: Cursor Side A — unified diff (full patch): diff --git a/AGENTS.md b/AGENTS.md index 6e0fd8ebb65d665c9c1438e3275971d62b98fd95..e9cc3173dbeb21ad0fc090ca7b407b027c7820a9 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -35,8 +35,11 @@ Environment variables (defaults in `server/src/state.rs`): - `SORTER2_DATA_DIR` — default `./data` (created on startup) - `SORTER2_EVENT_LOG` — default `{data_dir}/events.jsonl` - `SORTER2_BASE_URL` — public origin (also drives Secure cookies when `https://`) -- `GITHUB_CLIENT_ID` / `GITHUB_CLIENT_SECRET` — GitHub OAuth (optional; login disabled if unset) -- `SORTER2_ALLOW_MOCK_OAUTH=1` — allow `mock_user` on `/auth/github` (tests only) +- `GITHUB_CLIENT_ID` / `GITHUB_CLIENT_SECRET` — GitHub OAuth linking (optional) +- `REDDIT_CLIENT_ID` / `REDDIT_CLIENT_SECRET` (or `REDDIT_APP_*`) — Reddit API import + OAuth linking (optional) +- `SORTER2_ALLOW_MOCK_OAUTH=1` — allow `mock_user` on `/auth/github` and `/auth/reddit` (tests only) + +Identity: UUID is canonical. OAuth providers only *link* to a UUID (first link creates the principal). Linked providers are private to the account owner. Health check: `GET /healthz` → `ok`. diff --git a/server/src/auth/mod.rs b/server/src/auth/mod.rs index 5906f93b13853421e96a3c37bc9d8202a47842bf..c706ae8045a811e5941f5f6c72da88f42a403a82 100644 --- a/server/src/auth/mod.rs +++ b/server/src/auth/mod.rs @@ -1,4 +1,8 @@ -//! GitHub OAuth login, session cookies, and vote actor resolution. +//! OAuth linking, session cookies, and vote actor resolution. +//! +//! Canonical identity is a UUID. OAuth providers only *link* to that UUID +//! (first link creates the principal; later links attach while logged in). +//! Which providers are linked is private to the account owner. pub mod config; pub mod identity; @@ -22,7 +26,9 @@ use crate::{ form_template::template_json_compact, html::layout, state::AppState, - storage_schema::{oauth_link_owner, pseudonym_owner, Store, StoreFields}, + storage_schema::{ + linked_providers_for_uuid, oauth_link_owner, pseudonym_owner, Store, StoreFields, + }, ui_action::UI_RPC_FIELD, }; @@ -53,10 +59,12 @@ fn new_actor_uuid() -> String { pub struct LoginQuery { #[serde(default)] pub return_to: Option, + #[serde(default)] + pub error: Option, } #[derive(Debug, Deserialize)] -pub struct GitHubStartQuery { +pub struct OAuthStartQuery { #[serde(default)] pub return_to: Option, #[serde(default)] @@ -72,15 +80,22 @@ fn return_from_query_or_jar(jar: &CookieJar, query: Option<&str>) -> String { .unwrap_or_else(|| "/".to_string()) } -fn oauth_providers(base_url: &str, return_to: &str) -> Vec<(&'static str, String)> { +/// Available OAuth link targets: `(provider_key, label, start_href)`. +fn oauth_providers(base_url: &str, return_to: &str) -> Vec<(&'static str, &'static str, String)> { let mut out = Vec::new(); + let enc = urlencoding::encode(return_to); if oauth::GitHubConfig::from_env(base_url).is_some() { out.push(( - "GitHub", - format!( - "/auth/github?return_to={}", - urlencoding::encode(return_to) - ), + "github", + oauth::provider_label("github"), + format!("/auth/github?return_to={enc}"), + )); + } + if oauth::RedditConfig::from_env(base_url).is_some() { + out.push(( + "reddit", + oauth::provider_label("reddit"), + format!("/auth/reddit?return_to={enc}"), )); } out @@ -125,23 +140,41 @@ fn alias_claim_forms(return_to: &str, submit_label: &str) -> Result Markup { +fn login_error_message(code: Option<&str>) -> Option<&'static str> { + match code { + Some("oauth_taken") => { + Some("that OAuth account is already linked to a different sorter2 account") + } + Some("oauth_failed") => Some("OAuth failed — try again"), + _ => None, + } +} + +fn signed_out_body( + providers: &[(&str, &str, String)], + error: Option<&str>, +) -> Markup { html! { main class="panel login-page" { section class="login-section" { h1 { "sign in" } - p class="muted" { "link an account to vote under a lasting alias" } + p class="muted" { + "link an OAuth account to create your identity, then claim an alias to vote" + } + @if let Some(msg) = login_error_message(error) { + p class="alias-bad" data-testid="login-error" { (msg) } + } @if providers.is_empty() { p class="muted" { - "OAuth is not configured. Set GITHUB_CLIENT_ID and GITHUB_CLIENT_SECRET." + "OAuth is not configured. Set GitHub and/or Reddit client credentials." } } @else { ul class="oauth-provider-list" { - @for (name, href) in providers { + @for (key, label, href) in providers { li { a href=(href) class="btn-primary oauth-provider" - data-testid=(format!("oauth-{}", name.to_lowercase())) { - (format!("Continue with {name}")) + data-testid=(format!("oauth-{key}")) { + (format!("Link {label}")) } } } @@ -156,7 +189,10 @@ fn signed_out_body(providers: &[(&str, String)]) -> Markup { fn account_body( actor: &session::SessionActor, aliases: &[String], - providers: &[(&str, String)], + // Provider keys already linked to this UUID (private). + linked: &[String], + // Providers available to link: not yet attached. + unlinkable: &[(&str, &str, String)], claim_forms: Markup, ) -> Markup { let current = actor.pseudonym.trim(); @@ -212,16 +248,29 @@ fn account_body( (claim_forms) } - @if !providers.is_empty() { - section class="login-section" { - h2 { "linked sign-in" } - p class="muted small" { "sign in again with the same provider to return to this account" } + section class="login-section" { + h2 { "linked sign-in" } + p class="muted small" { + "private to you — linking more providers raises trust weight without publishing which accounts you use" + } + @if linked.is_empty() { + p class="muted" data-testid="linked-providers-empty" { "none yet" } + } @else { + ul class="linked-provider-list" data-testid="linked-providers" { + @for key in linked { + li data-testid=(format!("linked-{key}")) { + (oauth::provider_label(key)) + } + } + } + } + @if !unlinkable.is_empty() { ul class="oauth-provider-list" { - @for (name, href) in providers { + @for (key, label, href) in unlinkable { li { a href=(href) class="btn-secondary oauth-provider" - data-testid=(format!("oauth-relink-{}", name.to_lowercase())) { - (format!("Re-link {name}")) + data-testid=(format!("oauth-link-{key}")) { + (format!("Link {label}")) } } } @@ -243,12 +292,21 @@ fn account_body( fn login_body( session: Option<&session::SessionActor>, aliases: &[String], - providers: &[(&str, String)], + linked: &[String], + providers: &[(&str, &str, String)], claim_forms: Option, + error: Option<&str>, ) -> Markup { match (session, claim_forms) { - (Some(actor), Some(forms)) => account_body(actor, aliases, providers, forms), - _ => signed_out_body(providers), + (Some(actor), Some(forms)) => { + let unlinkable: Vec<_> = providers + .iter() + .filter(|(key, _, _)| !linked.iter().any(|p| p == key)) + .cloned() + .collect(); + account_body(actor, aliases, linked, &unlinkable, forms) + } + _ => signed_out_body(providers, error), } } @@ -268,6 +326,10 @@ pub async fn login_page( .as_ref() .map(|s| alias_list(db, &s.uuid)) .unwrap_or_default(); + let linked = session + .as_ref() + .map(|s| linked_providers_for_uuid(db, &s.uuid).unwrap_or_default()) + .unwrap_or_default(); let providers = oauth_providers(&base_url_from_env(state.cfg.port), &return_to); let claim_forms = if session.is_some() { @@ -282,7 +344,14 @@ pub async fn login_page( } else { "login · sorter2" }, - login_body(session.as_ref(), &aliases, &providers, claim_forms), + login_body( + session.as_ref(), + &aliases, + &linked, + &providers, + claim_forms, + query.error.as_deref(), + ), state.views.get_views("/login"), session .as_ref() @@ -302,7 +371,6 @@ pub async fn alias_page( let db = state.projection_store.db(); let session = session::load_valid_session(db, &session_id).ok_or(StatusCode::UNAUTHORIZED)?; if session::session_has_pseudonym(&session) { - // Already onboarded — manage aliases on the account page. return Ok(Redirect::to("/login").into_response()); } @@ -331,7 +399,7 @@ pub async fn alias_page( pub async fn github_start( State(state): State, jar: CookieJar, - Query(query): Query, + Query(query): Query, ) -> Result { let cfg = oauth::GitHubConfig::from_env(&base_url_from_env(state.cfg.port)) .ok_or(StatusCode::SERVICE_UNAVAILABLE)?; @@ -342,7 +410,28 @@ pub async fn github_start( } else { None }; - let url = oauth::authorize_url(&cfg, &state_token, mock_user); + let url = oauth::github_authorize_url(&cfg, &state_token, mock_user); + let jar = jar + .add(session::oauth_state_cookie_value(&state_token)) + .add(session::auth_return_cookie_value(&return_to)); + Ok((jar, Redirect::temporary(&url)).into_response()) +} + +pub async fn reddit_start( + State(state): State, + jar: CookieJar, + Query(query): Query, +) -> Result { + let cfg = oauth::RedditConfig::from_env(&base_url_from_env(state.cfg.port)) + .ok_or(StatusCode::SERVICE_UNAVAILABLE)?; + let return_to = return_from_query_or_jar(&jar, query.return_to.as_deref()); + let state_token = session::new_oauth_state(); + let mock_user = if config::mock_oauth_allowed() { + query.mock_user.as_deref() + } else { + None + }; + let url = oauth::reddit_authorize_url(&cfg, &state_token, mock_user); let jar = jar .add(session::oauth_state_cookie_value(&state_token)) .add(session::auth_return_cookie_value(&return_to)); @@ -355,6 +444,13 @@ pub struct OAuthCallbackQuery { pub state: String, } +/// Link `provider:provider_id` to a UUID. +/// +/// - Logged in + new provider → attach to session UUID +/// - Logged in + already ours → no-op +/// - Logged in + owned by someone else → conflict +/// - Logged out + known link → resume that UUID +/// - Logged out + unknown → create principal + first link async fn finish_oauth_login( state: &AppState, jar: CookieJar, @@ -364,11 +460,41 @@ async fn finish_oauth_login( let db = state.projection_store.db(); let return_to = return_from_query_or_jar(&jar, None); - let uuid = match oauth_link_owner(db, provider, &provider_id) - .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)? - { - Some(existing) => existing, - None => { + let existing_owner = oauth_link_owner(db, provider, &provider_id) + .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; + + let session_uuid = session::session_id_from_jar(&jar) + .as_deref() + .and_then(|id| session::load_valid_session(db, id)) + .map(|s| s.uuid); + let linking_while_logged_in = session_uuid.is_some(); + + let uuid = match (session_uuid, existing_owner) { + (Some(session_uuid), Some(owner)) if owner == session_uuid => session_uuid, + (Some(_), Some(_)) => { + return Ok(( + jar.add(session::clear_oauth_state_cookie()), + "/login?error=oauth_taken".into(), + )); + } + (Some(session_uuid), None) => { + let ts = now_ms(); + state + .append_identity_events(vec![Event::OauthLinked { + uuid: session_uuid.clone(), + provider: provider.to_string(), + provider_id, + ts, + }]) + .await + .map_err(|e| { + tracing::warn!(err = %e, "oauth link append failed"); + StatusCode::INTERNAL_SERVER_ERROR + })?; + session_uuid + } + (None, Some(owner)) => owner, + (None, None) => { let uuid = new_actor_uuid(); let ts = now_ms(); state @@ -409,6 +535,9 @@ async fn finish_oauth_login( "/login/alias?return_to={}", urlencoding::encode(&return_to) ) + } else if linking_while_logged_in { + // Additional link while already in an account → stay on account page. + "/login".to_string() } else { return_to }; @@ -434,22 +563,57 @@ pub async fn github_callback( .build() .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; - let token = oauth::exchange_code(&client, &cfg, &query.code) + let token = oauth::github_exchange_code(&client, &cfg, &query.code) .await .map_err(|e| { tracing::warn!(err = %e, "github oauth token exchange failed"); StatusCode::BAD_GATEWAY })?; - let user = oauth::fetch_user(&client, &cfg.api_base, &token) + let user = oauth::github_fetch_user(&client, &cfg.api_base, &token) .await .map_err(|e| { tracing::warn!(err = %e, "github user fetch failed"); StatusCode::BAD_GATEWAY })?; - let provider = "github"; - let provider_id = oauth::provider_id(&user); - let (jar, dest) = finish_oauth_login(&state, jar, provider, provider_id).await?; + let (jar, dest) = + finish_oauth_login(&state, jar, "github", oauth::github_provider_id(&user)).await?; + Ok((jar, Redirect::to(&dest)).into_response()) +} + +pub async fn reddit_callback( + State(state): State, + jar: CookieJar, + Query(query): Query, +) -> Result { + let cfg = oauth::RedditConfig::from_env(&base_url_from_env(state.cfg.port)) + .ok_or(StatusCode::SERVICE_UNAVAILABLE)?; + + let expected_state = session::oauth_state_from_jar(&jar).ok_or(StatusCode::BAD_REQUEST)?; + if expected_state != query.state { + return Err(StatusCode::BAD_REQUEST); + } + + let client = Client::builder() + .timeout(std::time::Duration::from_secs(15)) + .build() + .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; + + let token = oauth::reddit_exchange_code(&client, &cfg, &query.code) + .await + .map_err(|e| { + tracing::warn!(err = %e, "reddit oauth token exchange failed"); + StatusCode::BAD_GATEWAY + })?; + let user = oauth::reddit_fetch_user(&client, &cfg, &token) + .await + .map_err(|e| { + tracing::warn!(err = %e, "reddit user fetch failed"); + StatusCode::BAD_GATEWAY + })?; + + let (jar, dest) = + finish_oauth_login(&state, jar, "reddit", oauth::reddit_provider_id(&user)).await?; Ok((jar, Redirect::to(&dest)).into_response()) } diff --git a/server/src/auth/oauth.rs b/server/src/auth/oauth.rs index b80078fee0c5acd905b9125d29454e46c4e0d066..369b1527d9032cf312a07819279e0f988ef4a36e 100644 --- a/server/src/auth/oauth.rs +++ b/server/src/auth/oauth.rs @@ -1,8 +1,15 @@ -//! GitHub OAuth (raw reqwest, same style as reddit.rs). +//! OAuth providers (GitHub + Reddit). Provider accounts only *link* to a UUID; +//! the UUID is the canonical identity. Which providers are linked is private. use reqwest::Client; use serde::Deserialize; +use crate::reddit::{ + default_user_agent, reddit_oauth_api_base, reddit_oauth_token_base, +}; + +// ── GitHub ────────────────────────────────────────────────────────────────── + #[derive(Debug, Clone)] pub struct GitHubConfig { pub client_id: String, @@ -40,7 +47,7 @@ impl GitHubConfig { } #[derive(Debug, Deserialize)] -struct TokenResponse { +struct GitHubTokenResponse { access_token: String, } @@ -50,7 +57,7 @@ pub struct GitHubUser { pub login: String, } -pub fn authorize_url(cfg: &GitHubConfig, state: &str, mock_user: Option<&str>) -> String { +pub fn github_authorize_url(cfg: &GitHubConfig, state: &str, mock_user: Option<&str>) -> String { let mut url = format!( "{}/login/oauth/authorize?client_id={}&redirect_uri={}&scope=read:user&state={}", cfg.oauth_base.trim_end_matches('/'), @@ -65,7 +72,7 @@ pub fn authorize_url(cfg: &GitHubConfig, state: &str, mock_user: Option<&str>) - url } -pub async fn exchange_code( +pub async fn github_exchange_code( client: &Client, cfg: &GitHubConfig, code: &str, @@ -90,14 +97,14 @@ pub async fn exchange_code( return Err(format!("github token HTTP {}", resp.status())); } - let body: TokenResponse = resp + let body: GitHubTokenResponse = resp .json() .await .map_err(|e| format!("github token parse failed: {e}"))?; Ok(body.access_token) } -pub async fn fetch_user( +pub async fn github_fetch_user( client: &Client, api_base: &str, access_token: &str, @@ -120,10 +127,149 @@ pub async fn fetch_user( .map_err(|e| format!("github user parse failed: {e}")) } -pub fn provider_id(user: &GitHubUser) -> String { +pub fn github_provider_id(user: &GitHubUser) -> String { user.id.to_string() } +// ── Reddit ────────────────────────────────────────────────────────────────── + +#[derive(Debug, Clone)] +pub struct RedditConfig { + pub client_id: String, + pub client_secret: String, + pub redirect_uri: String, + /// Host for `/api/v1/authorize` (www.reddit.com in production). + pub authorize_base: String, + /// Host for `POST /api/v1/access_token`. + pub token_base: String, + /// Host for bearer `GET /api/v1/me` (oauth.reddit.com). + pub api_base: String, + pub user_agent: String, +} + +/// Authorize page base; defaults to the same host as token POSTs. +pub fn reddit_authorize_base() -> String { + std::env::var("REDDIT_OAUTH_AUTHORIZE_BASE") + .or_else(|_| std::env::var("REDDIT_OAUTH_BASE")) + .unwrap_or_else(|_| "https://www.reddit.com".into()) +} + +impl RedditConfig { + pub fn from_env(base_url: &str) -> Option { + let client_id = std::env::var("REDDIT_CLIENT_ID") + .or_else(|_| std::env::var("REDDIT_APP_ID")) + .ok()?; + let client_secret = std::env::var("REDDIT_CLIENT_SECRET") + .or_else(|_| std::env::var("REDDIT_APP_SECRET")) + .ok()?; + if client_id.is_empty() || client_secret.is_empty() { + return None; + } + let base = base_url.trim_end_matches('/'); + Some(Self { + client_id, + client_secret, + redirect_uri: format!("{base}/auth/reddit/callback"), + authorize_base: reddit_authorize_base(), + token_base: reddit_oauth_token_base(), + api_base: reddit_oauth_api_base(), + user_agent: default_user_agent(), + }) + } +} + +#[derive(Debug, Deserialize)] +struct RedditTokenResponse { + access_token: String, +} + +#[derive(Debug, Deserialize)] +pub struct RedditUser { + /// Stable id (`t2_…`); never use `name` as identity. + pub id: String, + pub name: String, +} + +pub fn reddit_authorize_url(cfg: &RedditConfig, state: &str, mock_user: Option<&str>) -> String { + let mut url = format!( + "{}/api/v1/authorize?client_id={}&response_type=code&state={}&redirect_uri={}&duration=temporary&scope=identity", + cfg.authorize_base.trim_end_matches('/'), + urlencoding::encode(&cfg.client_id), + urlencoding::encode(state), + urlencoding::encode(&cfg.redirect_uri), + ); + if let Some(user) = mock_user { + url.push_str("&mock_user="); + url.push_str(&urlencoding::encode(user)); + } + url +} + +pub async fn reddit_exchange_code( + client: &Client, + cfg: &RedditConfig, + code: &str, +) -> Result { + let resp = client + .post(format!( + "{}/api/v1/access_token", + cfg.token_base.trim_end_matches('/') + )) + .header("User-Agent", &cfg.user_agent) + .basic_auth(&cfg.client_id, Some(&cfg.client_secret)) + .form(&[ + ("grant_type", "authorization_code"), + ("code", code), + ("redirect_uri", cfg.redirect_uri.as_str()), + ]) + .send() + .await + .map_err(|e| format!("reddit token request failed: {e}"))?; + + if !resp.status().is_success() { + let status = resp.status(); + let body = resp.text().await.unwrap_or_default(); + return Err(format!("reddit token HTTP {status}: {body}")); + } + + let body: RedditTokenResponse = resp + .json() + .await + .map_err(|e| format!("reddit token parse failed: {e}"))?; + Ok(body.access_token) +} + +pub async fn reddit_fetch_user( + client: &Client, + cfg: &RedditConfig, + access_token: &str, +) -> Result { + let resp = client + .get(format!( + "{}/api/v1/me", + cfg.api_base.trim_end_matches('/') + )) + .header("User-Agent", &cfg.user_agent) + .bearer_auth(access_token) + .send() + .await + .map_err(|e| format!("reddit user request failed: {e}"))?; + + if !resp.status().is_success() { + return Err(format!("reddit user HTTP {}", resp.status())); + } + + resp.json() + .await + .map_err(|e| format!("reddit user parse failed: {e}")) +} + +pub fn reddit_provider_id(user: &RedditUser) -> String { + user.id.clone() +} + +// ── Shared helpers ────────────────────────────────────────────────────────── + pub fn validate_pseudonym(raw: &str) -> Result { let trimmed = raw.trim(); if trimmed.is_empty() { @@ -144,3 +290,12 @@ pub fn validate_pseudonym(raw: &str) -> Result { pub fn sanitize_pseudonym(login: &str) -> String { validate_pseudonym(login).unwrap_or_else(|_| "user".to_string()) } + +/// Display name for a provider key (`github` → `GitHub`). Never show provider ids. +pub fn provider_label(provider: &str) -> &'static str { + match provider { + "github" => "GitHub", + "reddit" => "Reddit", + _ => "OAuth", + } +} diff --git a/server/src/lib.rs b/server/src/lib.rs index 84f2565b105fb302241b949af64bd5e49916eab2..0d948af1457504fdbd34d0b14261f65ac58da0ec 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -47,6 +47,8 @@ pub fn create_app(state: AppState) -> Router { .route("/login/alias", get(crate::auth::alias_page)) .route("/auth/github", get(crate::auth::github_start)) .route("/auth/github/callback", get(crate::auth::github_callback)) + .route("/auth/reddit", get(crate::auth::reddit_start)) + .route("/auth/reddit/callback", get(crate::auth::reddit_callback)) .route("/auth/logout", post(crate::auth::logout)) .route("/auth/switch", post(crate::auth::switch_pseudonym)) .route("/ui", post(crate::api::ui_html::post_ui_html)) diff --git a/server/src/projection_apply.rs b/server/src/projection_apply.rs index 4fb4b48ee0a53b7f673fae0c9731eed5acc33332..f50458c2ff3c447a3cfd28adb298e6883a2da4e9 100644 --- a/server/src/projection_apply.rs +++ b/server/src/projection_apply.rs @@ -4,6 +4,8 @@ //! child links, recent-vote appends) plus a cursor advance, all committed in //! one atomic `DisableWal` batch. +use std::collections::HashMap; + use crate::{ event_log::EventLogError, events::{Event, EventRecord}, @@ -44,6 +46,8 @@ pub fn apply_records( let db = projection_store.db(); let mut batch = db.batch(); let mut last_seq = 0u64; + // Weight reads must see earlier writes in this same batch. + let mut pending_weights: HashMap = HashMap::new(); for record in records { match &record.event { @@ -85,6 +89,7 @@ pub fn apply_records( ensure_path_writes(&mut batch, &parsed); } Event::PrincipalCreated { uuid, .. } => { + pending_weights.insert(uuid.clone(), BASE_TRUST_WEIGHT); batch.write( Store::root() .user_weights() @@ -112,17 +117,25 @@ pub fn apply_records( } } else { batch.write(Store::root().oauth_links().key(&link_key).set(uuid)); - let current = Store::root() - .user_weights() - .key(&uuid.clone()) - .get(db) - .map_err(|e| EventLogError::Apply(e.to_string()))? + let current = pending_weights + .get(uuid) + .copied() + .or_else(|| { + Store::root() + .user_weights() + .key(&uuid.clone()) + .get(db) + .ok() + .flatten() + }) .unwrap_or(BASE_TRUST_WEIGHT); + let next = trust_weight_after_link(current); + pending_weights.insert(uuid.clone(), next); batch.write( Store::root() .user_weights() .key(&uuid.clone()) - .set(&trust_weight_after_link(current)), + .set(&next), ); } } @@ -205,6 +218,15 @@ mod tests { ), record( 3, + Event::OauthLinked { + uuid: uuid.into(), + provider: "reddit".into(), + provider_id: "t2_abc".into(), + ts, + }, + ), + record( + 4, Event::PseudonymClaimed { uuid: uuid.into(), pseudonym: "octocat".into(), @@ -219,8 +241,12 @@ mod tests { oauth_link_owner(store.db(), "github", "42").unwrap(), Some(uuid.to_string()) ); + assert_eq!( + crate::storage_schema::linked_providers_for_uuid(store.db(), uuid).unwrap(), + vec!["github".to_string(), "reddit".to_string()] + ); assert_eq!(resolve_actor_uuid(store.db(), "octocat").unwrap(), uuid); - assert_eq!(user_trust_weight(store.db(), uuid).unwrap(), 1.5); + assert_eq!(user_trust_weight(store.db(), uuid).unwrap(), 2.0); let aliases = Store::root() .user_pseudonyms() .key(&uuid.to_string()) diff --git a/server/src/storage_schema.rs b/server/src/storage_schema.rs index b942810afd96d3bf01b7765718303a39be4ef8d2..dac6f8e6080e3f5683c24dbb8861f5a981d456c4 100644 --- a/server/src/storage_schema.rs +++ b/server/src/storage_schema.rs @@ -103,6 +103,25 @@ pub fn oauth_link_owner(db: &Db, provider: &str, provider_id: &str) -> durable:: .get(db) } +/// Provider names linked to a UUID (`github`, `reddit`, …). Private — for the +/// account owner's page only; never expose which providers are linked publicly. +pub fn linked_providers_for_uuid(db: &Db, uuid: &str) -> durable::Result> { + let mut providers = Vec::new(); + for (key, owner) in Store::root().oauth_links().iter(db)? { + if owner != uuid { + continue; + } + let Some((provider, _)) = key.split_once(':') else { + continue; + }; + if !providers.iter().any(|p| p == provider) { + providers.push(provider.to_string()); + } + } + providers.sort(); + Ok(providers) +} + pub const RECENT_VOTES_CAP: u64 = 200; fn id_key(id: &ItemId) -> String { diff --git a/test/support/harness.clj b/test/support/harness.clj index 4505ece5aa193e826ca61c52f1467d252080d66b..741024aab1d14269e225791189633287980ae2e4 100644 --- a/test/support/harness.clj +++ b/test/support/harness.clj @@ -48,12 +48,15 @@ "GITHUB_CLIENT_SECRET" "test-secret" "GITHUB_OAUTH_BASE" (str "http://127.0.0.1:" oauth-port) "GITHUB_API_BASE" (str "http://127.0.0.1:" oauth-port) + ;; Reddit import fixtures + Reddit OAuth on reddit-port. "REDDIT_API_BASE" (str "http://127.0.0.1:" reddit-port) + "REDDIT_CLIENT_ID" "test-reddit" + "REDDIT_CLIENT_SECRET" "test-reddit-secret" "REDDIT_OAUTH_BASE" (str "http://127.0.0.1:" reddit-port) - "REDDIT_CLIENT_ID" "" - "REDDIT_CLIENT_SECRET" "" - "REDDIT_APP_ID" "" - "REDDIT_APP_SECRET" ""})) + "REDDIT_OAUTH_AUTHORIZE_BASE" (str "http://127.0.0.1:" reddit-port) + "REDDIT_OAUTH_TOKEN_BASE" (str "http://127.0.0.1:" reddit-port) + "REDDIT_OAUTH_API_BASE" (str "http://127.0.0.1:" reddit-port) + "REDDIT_USER_AGENT" "web:sorter2-test:v0 (by /u/test)"})) (defn with-auth-servers "Start mock Reddit + mock OAuth + release sorter2-server. diff --git a/test/support/mock_oauth.clj b/test/support/mock_oauth.clj index 909d7a7be8b159ea54a122d69c46af3db3318118..5ba7e9be3cf6d2226648e9609a09ed45f306930f 100644 --- a/test/support/mock_oauth.clj +++ b/test/support/mock_oauth.clj @@ -1,5 +1,5 @@ (ns test.support.mock-oauth - "In-process HTTP stub for GitHub OAuth (authorize, token, /user)." + "In-process HTTP stub for GitHub + Reddit OAuth (authorize, token, user)." (:require [clojure.string :as str]) (:import [com.sun.net.httpserver HttpServer HttpHandler HttpExchange] [java.net InetSocketAddress URLDecoder])) @@ -12,11 +12,14 @@ (URLDecoder/decode (or v "") "UTF-8")))) (str/split query #"&")))) -(defn- parse-mock-user [raw] +(defn- parse-mock-user + "GitHub-style `id:login` (numeric id). Reddit-style `t2_xxx:name`." + [raw] (let [s (or raw "1002:newbie") [id login] (str/split s #":" 2)] - {:id (Long/parseLong id) - :login (or login "newbie")})) + {:id id + :login (or login "newbie") + :numeric? (re-matches #"\d+" id)})) (defn- send-json [^HttpExchange ex status body] (let [bytes (.getBytes body "UTF-8")] @@ -31,9 +34,10 @@ (.sendResponseHeaders ex 302 -1) (.close (.getResponseBody ex))) -(defn- read-form-code [^HttpExchange ex] +(defn- read-form [^HttpExchange ex] (let [body (slurp (.getInputStream ex))] - (query-param body "code"))) + {:code (query-param body "code") + :grant (query-param body "grant_type")})) (defn- bearer-token [^HttpExchange ex] (some-> (.getRequestHeaders ex) @@ -44,8 +48,18 @@ (when (str/starts-with? token "mock:") (parse-mock-user (subs token 5)))) +(defn- authorize-redirect [exchange query] + (let [redirect-uri (query-param query "redirect_uri") + state (query-param query "state") + mock-user (query-param query "mock_user") + user (parse-mock-user mock-user) + code (str "mock:" (:id user) ":" (:login user)) + loc (str redirect-uri "?code=" (java.net.URLEncoder/encode code "UTF-8") + "&state=" (java.net.URLEncoder/encode state "UTF-8"))] + (send-redirect exchange loc))) + (defn start-mock-oauth - "Start mock GitHub OAuth on `port`. Returns a zero-arg `stop` function." + "Start mock GitHub + Reddit OAuth on `port`. Returns a zero-arg `stop` function." [port] (let [server (HttpServer/create (InetSocketAddress. "127.0.0.1" port) 0) handler @@ -53,28 +67,45 @@ (handle [^HttpExchange exchange] (let [uri (.getRequestURI exchange) path (.getPath uri) - query (.getQuery uri)] + query (.getQuery uri) + method (.getRequestMethod exchange)] (cond + ;; GitHub authorize (str/ends-with? path "/login/oauth/authorize") - (let [redirect-uri (query-param query "redirect_uri") - state (query-param query "state") - mock-user (query-param query "mock_user") - user (parse-mock-user mock-user) - code (str "mock:" (:id user) ":" (:login user)) - loc (str redirect-uri "?code=" (java.net.URLEncoder/encode code "UTF-8") - "&state=" (java.net.URLEncoder/encode state "UTF-8"))] - (send-redirect exchange loc)) + (authorize-redirect exchange query) + + ;; Reddit authorize + (str/ends-with? path "/api/v1/authorize") + (authorize-redirect exchange query) - (str/ends-with? path "/login/oauth/access_token") - (let [code (or (read-form-code exchange) "mock:1002:newbie")] + ;; GitHub token + (and (= method "POST") (str/ends-with? path "/login/oauth/access_token")) + (let [code (or (:code (read-form exchange)) "mock:1002:newbie")] (send-json exchange 200 (str "{\"access_token\":\"" code "\",\"token_type\":\"bearer\"}"))) + ;; Reddit token (client_credentials for import + authorization_code for login) + (and (= method "POST") (str/ends-with? path "/api/v1/access_token")) + (let [form (read-form exchange) + grant (or (:grant form) "") + code (or (:code form) "mock:t2_test:redditor")] + (if (= grant "client_credentials") + (send-json exchange 200 "{\"access_token\":\"app-token\",\"token_type\":\"bearer\",\"expires_in\":3600}") + (send-json exchange 200 (str "{\"access_token\":\"" code "\",\"token_type\":\"bearer\",\"expires_in\":3600}")))) + + ;; GitHub user (= path "/user") (let [token (bearer-token exchange) - user (or (parse-token-user token) {:id 1002 :login "newbie"})] + user (or (parse-token-user token) {:id "1002" :login "newbie" :numeric? true})] (send-json exchange 200 (str "{\"id\":" (:id user) ",\"login\":\"" (:login user) "\"}"))) + ;; Reddit /api/v1/me + (str/ends-with? path "/api/v1/me") + (let [token (bearer-token exchange) + user (or (parse-token-user token) {:id "t2_test" :login "redditor"})] + (send-json exchange 200 + (str "{\"id\":\"" (:id user) "\",\"name\":\"" (:login user) "\"}"))) + :else (send-json exchange 404 "{\"error\":\"not found\"}")))))] (.createContext server "/" handler) diff --git a/test/support/mock_reddit.clj b/test/support/mock_reddit.clj index 5efa92db3e1f79b8f423a2f1adcbda959123c1ad..a630cf0938722193e9af88382d60e777ff371be4 100644 --- a/test/support/mock_reddit.clj +++ b/test/support/mock_reddit.clj @@ -1,14 +1,56 @@ (ns test.support.mock-reddit - "In-process HTTP stub for Reddit API fixtures (`test/fixtures/reddit/`)." + "In-process HTTP stub for Reddit API fixtures + OAuth login endpoints." (:require [clojure.java.io :as io] [clojure.string :as str]) (:import [com.sun.net.httpserver HttpServer HttpHandler HttpExchange] - [java.net InetSocketAddress])) + [java.net InetSocketAddress URLDecoder])) (defn fixtures-dir ([] (fixtures-dir (System/getProperty "user.dir"))) ([root] (str root "/test/fixtures/reddit"))) +(defn- query-param [query key] + (when query + (some (fn [pair] + (let [[k v] (str/split pair "=" 2)] + (when (= k key) + (URLDecoder/decode (or v "") "UTF-8")))) + (str/split query #"&")))) + +(defn- parse-mock-user [raw] + (let [s (or raw "t2_test:redditor") + [id login] (str/split s #":" 2)] + {:id id :login (or login "redditor")})) + +(defn- send-bytes [^HttpExchange ex status ^bytes body content-type] + (.set (.getResponseHeaders ex) "Content-Type" content-type) + (.sendResponseHeaders ex status (alength body)) + (doto (.getResponseBody ex) + (.write body) + (.close))) + +(defn- send-json [^HttpExchange ex status body] + (send-bytes ex status (.getBytes body "UTF-8") "application/json")) + +(defn- send-redirect [^HttpExchange ex location] + (.set (.getResponseHeaders ex) "Location" location) + (.sendResponseHeaders ex 302 -1) + (.close (.getResponseBody ex))) + +(defn- read-form [^HttpExchange ex] + (let [body (slurp (.getInputStream ex))] + {:code (query-param body "code") + :grant (query-param body "grant_type")})) + +(defn- bearer-token [^HttpExchange ex] + (some-> (.getRequestHeaders ex) + (.getFirst "Authorization") + (str/replace #"^[Bb]earer " ""))) + +(defn- parse-token-user [token] + (when (str/starts-with? token "mock:") + (parse-mock-user (subs token 5)))) + (defn start-mock-reddit "Start a mock Reddit API on `port`. Returns a zero-arg `stop` function." ([port] (start-mock-reddit port (fixtures-dir))) @@ -19,15 +61,38 @@ handler (proxy [HttpHandler] [] (handle [^HttpExchange exchange] - ;; `/r//about.json` → subreddit entity; `/r/.json` → listing. - (let [path (.getPath (.getRequestURI exchange)) - body (if (str/includes? path "/about") - about - listing)] - (.sendResponseHeaders exchange 200 (alength body)) - (let [out (.getResponseBody exchange)] - (.write out body) - (.close out)))))] + (let [uri (.getRequestURI exchange) + path (.getPath uri) + query (.getQuery uri) + method (.getRequestMethod exchange)] + (cond + (str/ends-with? path "/api/v1/authorize") + (let [redirect-uri (query-param query "redirect_uri") + state (query-param query "state") + user (parse-mock-user (query-param query "mock_user")) + code (str "mock:" (:id user) ":" (:login user)) + loc (str redirect-uri "?code=" (java.net.URLEncoder/encode code "UTF-8") + "&state=" (java.net.URLEncoder/encode state "UTF-8"))] + (send-redirect exchange loc)) + + (and (= method "POST") (str/ends-with? path "/api/v1/access_token")) + (let [form (read-form exchange) + grant (or (:grant form) "") + code (or (:code form) "mock:t2_test:redditor")] + (if (= grant "client_credentials") + (send-json exchange 200 "{\"access_token\":\"app-token\",\"token_type\":\"bearer\",\"expires_in\":3600}") + (send-json exchange 200 (str "{\"access_token\":\"" code "\",\"token_type\":\"bearer\",\"expires_in\":3600}")))) + + (str/ends-with? path "/api/v1/me") + (let [user (or (parse-token-user (bearer-token exchange)) + {:id "t2_test" :login "redditor"})] + (send-json exchange 200 + (str "{\"id\":\"" (:id user) "\",\"name\":\"" (:login user) "\"}"))) + + ;; `/r//about.json` → subreddit entity; `/r/.json` → listing. + :else + (let [body (if (str/includes? path "/about") about listing)] + (send-bytes exchange 200 body "application/json"))))))] (.createContext server "/" handler) (.setExecutor server nil) (.start server) Side B — contributor: tommy-mor Side B — commit message: [2bc302c3] refactor Side B — unified diff (full patch): diff --git a/server/src/api/ui_html.rs b/server/src/api/ui_html.rs index 06001212820101e0cc953d3687dea64f85e60787..d2f9769108def7ca2c5857aec8b4319426188a66 100644 --- a/server/src/api/ui_html.rs +++ b/server/src/api/ui_html.rs @@ -47,7 +47,7 @@ pub async fn post_ui_html( ratio_left, ratio_right, scope, - next, + vote_compare, } => { let parent = parent_from_scope(&scope); if let Err(e) = state @@ -57,14 +57,12 @@ pub async fn post_ui_html( return ui_js_warn(&e).into_response(); } let tree = state.tree.read().await; - if !next.trim().is_empty() { + if vote_compare { + let left = parse_item_param(&a); + let right = parse_item_param(&b); + let morph = crate::html::vote::vote_recorded_morph(&tree, &parent, &left, &right); drop(tree); - return JsBuilder::new() - .raw(&format!( - "window.location.href={};", - js_string_literal(next.trim()) - )) - .into_response(); + return morph.into_response(); } let empty = crate::reducer::NodeState::default(); let node = tree.get(&parent).unwrap_or(&empty); @@ -137,7 +135,7 @@ mod tests { ratio_left: 3, ratio_right: 1, scope: String::new(), - next: String::new(), + vote_compare: false, } ); } diff --git a/server/src/html/mod.rs b/server/src/html/mod.rs index 61cdbc094819ddedb755572c59456ec0d6617619..cd578a5fea46a9a1d49e238d08a80c2caf18708d 100644 --- a/server/src/html/mod.rs +++ b/server/src/html/mod.rs @@ -65,6 +65,15 @@ impl JsBuilder { self } + pub(crate) fn morph_inner_selector(mut self, selector: &str, markup: Markup) -> Self { + let html = js_string_literal(&markup.into_string()); + self.snippets.push(format!( + "var __el = document.querySelector({sel}); if (__el) {{ Idiomorph.morph(__el, {html}, {{ morphStyle: 'innerHTML' }}); }}", + sel = js_string_literal(selector), + )); + self + } + pub(crate) fn raw(mut self, js: &str) -> Self { if !js.is_empty() { self.snippets.push(js.to_string()); diff --git a/server/src/html/vote.rs b/server/src/html/vote.rs new file mode 100644 index 0000000000000000000000000000000000000000..89ed621ad861214e147add2062224fc4137ff8ad --- /dev/null +++ b/server/src/html/vote.rs @@ -0,0 +1,306 @@ +//! Pairwise vote UI — `/vote?parent=` with optional `left` / `right`. + +use axum::{ + extract::{Query, State}, + response::{Html, IntoResponse}, +}; +use maud::{html, Markup}; +use serde::Deserialize; + +use crate::{ + form_template::template_json_compact, + html::JsBuilder, + pair::{children_of, resolve_pair, suggest_next_pair_in_pool}, + path_types::ItemId, + reducer::{GlobalTree, GroupState, NodeState, VoteData}, + state::{parse_item_param, AppState}, + ui_action::UI_RPC_FIELD, +}; + +use super::{breadcrumb_path, item_href, layout}; + +#[derive(Debug, Deserialize)] +pub struct VoteQuery { + pub parent: String, + #[serde(default)] + pub left: Option, + #[serde(default)] + pub right: Option, +} + +pub fn vote_href(parent: &ItemId) -> String { + format!( + "/vote?parent={}", + urlencoding::encode(parent.as_str()) + ) +} + +fn vote_compare_href(parent: &ItemId, left: &ItemId, right: &ItemId) -> String { + format!( + "/vote?parent={}&left={}&right={}", + urlencoding::encode(parent.as_str()), + urlencoding::encode(left.as_str()), + urlencoding::encode(right.as_str()), + ) +} + +fn display_label(id: &ItemId) -> String { + id.segments() + .last() + .map_or("item".into(), |v| v.to_string()) +} + +fn child_title(tree: &GlobalTree, id: &ItemId) -> String { + tree.get(id) + .and_then(|n| n.data.as_ref()) + .map(|d| d.title.clone()) + .unwrap_or_else(|| display_label(id)) +} + +fn ratio_pct(ratio_left: i32, ratio_right: i32) -> f64 { + let l = ratio_left.max(0) as f64; + let r = ratio_right.max(0) as f64; + let sum = l + r; + if sum <= 0.0 { + 50.0 + } else { + (l / sum) * 100.0 + } +} + +fn ratios_for_page(v: &VoteData, page_left: &ItemId, page_right: &ItemId) -> (i32, i32) { + match (v.a.as_str(), v.b.as_str()) { + (a, b) if a == page_left.as_str() && b == page_right.as_str() => { + (v.ratio_left, v.ratio_right) + } + (a, b) if a == page_right.as_str() && b == page_left.as_str() => { + (v.ratio_right, v.ratio_left) + } + _ => (v.ratio_left, v.ratio_right), + } +} + +fn edge_votes(group: &GroupState, left: &ItemId, right: &ItemId) -> Vec { + group + .recent_votes + .iter() + .filter(|v| { + (v.a.as_str() == left.as_str() && v.b.as_str() == right.as_str()) + || (v.a.as_str() == right.as_str() && v.b.as_str() == left.as_str()) + }) + .cloned() + .collect() +} + +fn vote_edge_history(tree: &GlobalTree, group: &GroupState, left: &ItemId, right: &ItemId) -> Markup { + let mut votes = edge_votes(group, left, right); + votes.sort_by(|a, b| b.ts.cmp(&a.ts)); + let legend_left = child_title(tree, left); + let legend_right = child_title(tree, right); + html! { + @if votes.is_empty() { + p class="muted vote-edge-empty" { "no votes on this pair yet" } + } @else { + h3 class="vote-edge-history-title" { + "votes on this pair" + span class="vote-edge-history-axis muted" { " · " (legend_left) " : " (legend_right) } + } + ul class="vote-edge-history" { + @for v in &votes { + @let (r_left, r_right) = ratios_for_page(v, left, right); + @let pct = ratio_pct(r_left, r_right); + li class="vote-edge-history-row" { + div class="vote-edge-meta" { + span class="vote-edge-ratio" { (format!("{}:{}", r_left, r_right)) } + } + div class="ratio-bar vote-edge-bar" aria-hidden="true" { + div class="ratio-left" style={(format!("width: {:.3}%;", pct))} {} + div class="ratio-right" style={(format!("width: {:.3}%;", 100.0 - pct))} {} + } + } + } + } + } + } +} + +fn vote_back_nav(parent: &ItemId) -> Markup { + html! { + div class="vote-compare-nav" { + a class="vote-compare-back muted" href=(item_href(parent)) { "← back to " (display_label(parent)) } + } + } +} + +fn vote_compare_actions(parent: &ItemId, next: Option<&(ItemId, ItemId)>) -> Markup { + let next_href = next.map(|(l, r)| vote_compare_href(parent, l, r)); + html! { + div id="vote-compare-actions" class="vote-compare-actions" { + button type="submit" class="btn-primary" data-testid="vote-post" { "post vote" } + @if let Some(href) = &next_href { + a class="btn-secondary vote-compare-next" data-testid="vote-next-pair" href=(href) { "next pair" } + } @else { + span class="btn-secondary vote-compare-next is-disabled" { "no next pair" } + } + } + } +} + +/// After recording a vote on the compare page: refresh edge history and next-pair link. +pub(crate) fn vote_recorded_morph( + tree: &GlobalTree, + parent: &ItemId, + left: &ItemId, + right: &ItemId, +) -> JsBuilder { + let pool = children_of(tree, parent); + let empty = NodeState::default(); + let group = tree + .get(parent) + .unwrap_or(&empty) + .local_ranking + .clone(); + let edge_history = vote_edge_history(tree, &group, left, right); + let next_pair = suggest_next(&group, left, right, &pool); + let actions = vote_compare_actions(parent, next_pair.as_ref()); + JsBuilder::new() + .morph_inner_selector("#vote-edge-history-region", edge_history) + .morph_selector("#vote-compare-actions", actions) +} + +fn vote_compare_item_card(tree: &GlobalTree, item: &ItemId, side_class: &str) -> Markup { + let href = item_href(item); + let title = child_title(tree, item); + html! { + div class=(format!("vote-compare-side {side_class}")) { + a class=(format!("vote-compare-item {side_class}")) href=(href) { + @if let Some(row) = crate::render::reddit::child_row_markup(tree, item, &href) { + (row) + } @else { + strong { (title) } + } + } + @if let Some(node) = tree.get(item) { + @if crate::render::reddit::is_reddit_post(item) { + @if let Some(data) = &node.data { + @if let Some(src) = data.image_url.as_ref().or(data.thumb_url.as_ref()) { + figure class="vote-compare-figure" { + img class="vote-compare-image" src=(src) alt="" loading="lazy"; + } + } + @if let Some(author) = &data.author { + p class="muted small" { "by " (author) } + } + } + } @else if let Some(data) = &node.data { + @if let Some(body) = &data.body_html { + div class="vote-compare-item-body" { + (maud::PreEscaped(body)) + } + } + } + } + } + } +} + + +fn suggest_next(group: &GroupState, left: &ItemId, right: &ItemId, pool: &[ItemId]) -> Option<(ItemId, ItemId)> { + suggest_next_pair_in_pool(group, pool, Some((left, right))) +} + +pub async fn vote_page( + State(state): State, + Query(q): Query, +) -> impl IntoResponse { + let parent = parse_item_param(&q.parent); + let left_param = q.left.as_deref().map(parse_item_param); + let right_param = q.right.as_deref().map(parse_item_param); + + let tree = state.tree.read().await; + let empty = NodeState::default(); + let parent_node = tree.get(&parent).unwrap_or(&empty); + + let (left, right) = match resolve_pair( + &tree, + &parent, + left_param.as_ref(), + right_param.as_ref(), + ) { + Ok(p) => p, + Err(e) => { + let (msg, status) = e.status_message(); + return (status, msg).into_response(); + } + }; + + let pool = children_of(&tree, &parent); + let group = &parent_node.local_ranking; + let next_pair = suggest_next(group, &left, &right, &pool); + let edge_history = vote_edge_history(&tree, group, &left, &right); + + let rpc_json = template_json_compact(&serde_json::json!({ + "action": "record_vote", + "a": left.as_str(), + "b": right.as_str(), + "ratio_left": {"$form:i32": "ratio_left"}, + "ratio_right": {"$form:i32": "ratio_right"}, + "scope": parent.as_str(), + "vote_compare": true, + })) + .expect("vote rpc json"); + + let title = format!( + "vote — {} vs {}", + child_title(&tree, &left), + child_title(&tree, &right) + ); + + let body = html! { + section class="vote-compare-shell" { + h1 { "compare" } + (breadcrumb_path(&parent)) + p class="muted vote-compare-scope" { + "ranking children of " + a href=(item_href(&parent)) { (child_title(&tree, &parent)) } + } + div class="vote-compare-pair" { + (vote_compare_item_card(&tree, &left, "vote-compare-left")) + span class="vote-compare-vs" { "vs" } + (vote_compare_item_card(&tree, &right, "vote-compare-right")) + } + (vote_back_nav(&parent)) + div id="vote-edge-history-region" { + (edge_history) + } + form id="vote-compare-form" method="POST" action="/ui" { + input type="hidden" name=(UI_RPC_FIELD) value=(rpc_json); + input type="hidden" name="ratio_left" id="vote-ratio-left" value="50"; + input type="hidden" name="ratio_right" id="vote-ratio-right" value="50"; + label class="vote-compare-slider-label" { + span id="vote-slider-left-label" { (child_title(&tree, &left)) } + input type="range" id="vote-preference-slider" min="0" max="100" value="50" + aria-valuemin="0" aria-valuemax="100"; + span id="vote-slider-right-label" { (child_title(&tree, &right)) } + } + (vote_compare_actions(&parent, next_pair.as_ref())) + } + } + }; + + drop(tree); + + let path = format!("/vote?parent={}", urlencoding::encode(parent.as_str())); + state.views.increment(path.clone()); + let views = state.views.get_views(&path); + + Html( + layout( + &title, + body, + views, + ) + .into_string(), + ) + .into_response() +} diff --git a/server/src/pair.rs b/server/src/pair.rs new file mode 100644 index 0000000000000000000000000000000000000000..606ffa51038a57ffacf335aa48bdb1185f8483fb --- /dev/null +++ b/server/src/pair.rs @@ -0,0 +1,340 @@ +//! Pick two children of a parent scope for pairwise voting. +//! +//! Pair selection prefers **bridge** votes — comparisons between items in +//! different connected components of the voted-pairs graph — so the pool +//! merges into one ranking group before refining within it. + +use rand::seq::SliceRandom; +use std::collections::{HashMap, HashSet}; + +use crate::{ + path_types::ItemId, + ranking::connected_components_from_voted_pairs, + reducer::{GlobalTree, GroupState}, +}; + +fn pairs_match(a: &ItemId, b: &ItemId, x: &ItemId, y: &ItemId) -> bool { + (a == x && b == y) || (a == y && b == x) +} + +fn pair_is_voted(group: &GroupState, a: &ItemId, b: &ItemId) -> bool { + let Some(&ai) = group.item_to_idx.get(a) else { + return false; + }; + let Some(&bi) = group.item_to_idx.get(b) else { + return false; + }; + let (i, j) = if ai < bi { (ai, bi) } else { (bi, ai) }; + group.voted_pairs.contains(&(i, j)) +} + +/// Component id per pool item: voted-pairs graph components plus one id per +/// never-voted child. +fn component_ids(group: &GroupState, pool: &[ItemId]) -> HashMap { + let n = group.idx_to_item.len(); + let (comps, isolates) = + connected_components_from_voted_pairs(n, group.voted_pairs.iter().copied()); + + let mut out: HashMap = HashMap::new(); + for (comp_idx, comp) in comps.iter().enumerate() { + for &idx in comp { + if idx < n { + out.insert(group.idx_to_item[idx].clone(), comp_idx); + } + } + } + let mut next = comps.len(); + for &idx in &isolates { + if idx < n { + out.insert(group.idx_to_item[idx].clone(), next); + next += 1; + } + } + for item in pool { + out.entry(item.clone()).or_insert_with(|| { + let id = next; + next += 1; + id + }); + } + out +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] +enum PairPriority { + /// Unvoted edge between two components — grows the ranking group. + BridgeUnvoted = 0, + /// Unvoted edge inside one component — refines order. + WithinUnvoted = 1, + /// Re-vote across components (rare once merged). + BridgeVoted = 2, + /// Re-vote within a component. + WithinVoted = 3, +} + +fn pair_priority( + group: &GroupState, + components: &HashMap, + a: &ItemId, + b: &ItemId, +) -> PairPriority { + let voted = pair_is_voted(group, a, b); + let bridge = components.get(a) != components.get(b); + match (bridge, voted) { + (true, false) => PairPriority::BridgeUnvoted, + (false, false) => PairPriority::WithinUnvoted, + (true, true) => PairPriority::BridgeVoted, + (false, true) => PairPriority::WithinVoted, + } +} + +/// All unordered pairs from `pool`, optionally skipping `exclude`. +fn candidate_pairs( + pool: &[ItemId], + exclude: Option<(&ItemId, &ItemId)>, +) -> Vec<(ItemId, ItemId)> { + let mut out = Vec::new(); + for i in 0..pool.len() { + for j in (i + 1)..pool.len() { + let a = &pool[i]; + let b = &pool[j]; + if a == b { + continue; + } + if exclude.is_some_and(|(x, y)| pairs_match(a, b, x, y)) { + continue; + } + out.push((a.clone(), b.clone())); + } + } + out +} + +/// Pick the next pair to vote on within `pool`. +/// +/// 1. Prefer unvoted **bridge** pairs (connect separate ranking components). +/// 2. Then unvoted within-component pairs (refinement). +/// 3. Then already-voted pairs (re-compare). +pub fn suggest_next_pair_in_pool( + group: &GroupState, + pool: &[ItemId], + exclude: Option<(&ItemId, &ItemId)>, +) -> Option<(ItemId, ItemId)> { + let candidates = candidate_pairs(pool, exclude); + if candidates.is_empty() { + return None; + } + let components = component_ids(group, pool); + let best = candidates + .iter() + .map(|(a, b)| (pair_priority(group, &components, a, b), (a, b))) + .min_by_key(|(p, _)| *p)? + .0; + let best_pairs: Vec<(ItemId, ItemId)> = candidates + .into_iter() + .filter(|(a, b)| pair_priority(group, &components, a, b) == best) + .collect(); + best_pairs.choose(&mut rand::thread_rng()).cloned() +} + +/// Random distinct pair from `children` (legacy pair.rs behavior). +pub fn random_pair(children: &[ItemId]) -> Option<(ItemId, ItemId)> { + if children.len() < 2 { + return None; + } + let left = children.choose(&mut rand::thread_rng())?; + let mut right = children.choose(&mut rand::thread_rng())?; + let mut guard = 0; + while left == right && guard < 32 { + right = children.choose(&mut rand::thread_rng())?; + guard += 1; + } + if left == right { + return None; + } + Some((left.clone(), right.clone())) +} + +/// Sorted children of `parent` from the global tree. +pub fn children_of(tree: &GlobalTree, parent: &ItemId) -> Vec { + let Some(node) = tree.get(parent) else { + return Vec::new(); + }; + let mut children: Vec = node.children.iter().cloned().collect(); + children.sort_by(|a, b| a.as_str().cmp(b.as_str())); + children +} + +/// Resolve a pair to compare under `parent`. +pub fn resolve_pair( + tree: &GlobalTree, + parent: &ItemId, + left: Option<&ItemId>, + right: Option<&ItemId>, +) -> Result<(ItemId, ItemId), PairError> { + let children = children_of(tree, parent); + if children.len() < 2 { + return Err(PairError::TooFewChildren); + } + let child_set: HashSet<_> = children.iter().collect(); + + match (left, right) { + (Some(l), Some(r)) => { + if l == r { + return Err(PairError::SameItem); + } + if !child_set.contains(l) || !child_set.contains(r) { + return Err(PairError::NotChild); + } + Ok((l.clone(), r.clone())) + } + (None, None) => { + let group = tree + .get(parent) + .map(|n| &n.local_ranking) + .cloned() + .unwrap_or_default(); + suggest_next_pair_in_pool(&group, &children, None).ok_or(PairError::NoPair) + } + _ => Err(PairError::IncompletePair), + } +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum PairError { + TooFewChildren, + SameItem, + NotChild, + IncompletePair, + NoPair, +} + +impl PairError { + pub fn status_message(&self) -> (&'static str, axum::http::StatusCode) { + match self { + Self::TooFewChildren => ( + "parent needs at least 2 children to vote", + axum::http::StatusCode::BAD_REQUEST, + ), + Self::SameItem => ( + "left and right must differ", + axum::http::StatusCode::BAD_REQUEST, + ), + Self::NotChild => ( + "left and right must be children of parent", + axum::http::StatusCode::BAD_REQUEST, + ), + Self::IncompletePair => ( + "provide both left and right, or neither", + axum::http::StatusCode::BAD_REQUEST, + ), + Self::NoPair => ( + "no pair available", + axum::http::StatusCode::BAD_REQUEST, + ), + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::reducer::{GlobalTree, VoteData}; + + fn seed_children(parent: &ItemId, ids: &[&str]) -> GlobalTree { + let mut tree = GlobalTree::new(); + tree.ensure_path(parent); + for id in ids { + let child = ItemId::parse(id).unwrap(); + tree.ensure_path(&child); + if let Some(p) = tree.nodes.get_mut(parent) { + p.children.insert(child); + } + } + tree + } + + fn pair_set(pair: &(ItemId, ItemId)) -> HashSet<&str> { + [pair.0.as_str(), pair.1.as_str()].into_iter().collect() + } + + #[test] + fn suggest_prefers_unvoted_pair() { + let parent = ItemId::parse("reddit.com/r/rust").unwrap(); + let mut tree = seed_children( + &parent, + &[ + "reddit.com/r/rust/a", + "reddit.com/r/rust/b", + "reddit.com/r/rust/c", + ], + ); + let vote = + VoteData::from_recorded(1, "reddit.com/r/rust/a", "reddit.com/r/rust/b", 2, 1).unwrap(); + tree.apply_vote(&parent, vote); + let group = tree.get(&parent).unwrap().local_ranking.clone(); + let pool = children_of(&tree, &parent); + let (l, r) = suggest_next_pair_in_pool(&group, &pool, None).unwrap(); + let voted_ab = (l.as_str() == "reddit.com/r/rust/a" && r.as_str() == "reddit.com/r/rust/b") + || (l.as_str() == "reddit.com/r/rust/b" && r.as_str() == "reddit.com/r/rust/a"); + assert!(!voted_ab); + } + + #[test] + fn suggest_bridges_separate_components() { + let parent = ItemId::parse("reddit.com/r/rust").unwrap(); + let mut tree = seed_children( + &parent, + &[ + "reddit.com/r/rust/a", + "reddit.com/r/rust/b", + "reddit.com/r/rust/c", + "reddit.com/r/rust/d", + ], + ); + let ab = VoteData::from_recorded(1, "reddit.com/r/rust/a", "reddit.com/r/rust/b", 2, 1).unwrap(); + let cd = VoteData::from_recorded(2, "reddit.com/r/rust/c", "reddit.com/r/rust/d", 2, 1).unwrap(); + tree.apply_vote(&parent, ab); + tree.apply_vote(&parent, cd); + let group = tree.get(&parent).unwrap().local_ranking.clone(); + let pool = children_of(&tree, &parent); + let pair = suggest_next_pair_in_pool(&group, &pool, None).unwrap(); + let chosen = pair_set(&pair); + let from_ab = chosen.contains("reddit.com/r/rust/a") || chosen.contains("reddit.com/r/rust/b"); + let from_cd = chosen.contains("reddit.com/r/rust/c") || chosen.contains("reddit.com/r/rust/d"); + assert!(from_ab && from_cd, "expected bridge pair, got {:?}", chosen); + } + + #[test] + fn suggest_connects_isolate_to_existing_component() { + let parent = ItemId::parse("reddit.com/r/rust").unwrap(); + let mut tree = seed_children( + &parent, + &[ + "reddit.com/r/rust/a", + "reddit.com/r/rust/b", + "reddit.com/r/rust/c", + ], + ); + let ab = VoteData::from_recorded(1, "reddit.com/r/rust/a", "reddit.com/r/rust/b", 2, 1).unwrap(); + tree.apply_vote(&parent, ab); + let group = tree.get(&parent).unwrap().local_ranking.clone(); + let pool = children_of(&tree, &parent); + let pair = suggest_next_pair_in_pool(&group, &pool, None).unwrap(); + let chosen = pair_set(&pair); + assert!(chosen.contains("reddit.com/r/rust/c")); + assert!(chosen.contains("reddit.com/r/rust/a") || chosen.contains("reddit.com/r/rust/b")); + } + + #[test] + fn resolve_pair_picks_from_pool() { + let parent = ItemId::parse("reddit.com/r/rust").unwrap(); + let tree = seed_children(&parent, &["reddit.com/r/rust/a", "reddit.com/r/rust/b"]); + let pair = resolve_pair(&tree, &parent, None, None).unwrap(); + let pool: HashSet<_> = ["reddit.com/r/rust/a", "reddit.com/r/rust/b"] + .into_iter() + .collect(); + assert!(pool.contains(pair.0.as_str())); + assert!(pool.contains(pair.1.as_str())); + } +} diff --git a/server/src/path_types.rs b/server/src/path_types.rs index 12dce9888f5cd4e1a0974d12d6468368d0f775b9..a0d1a028b8bd3c5d1c714dea3ec597d1ac39e1bc 100644 --- a/server/src/path_types.rs +++ b/server/src/path_types.rs @@ -40,6 +40,22 @@ impl ItemId { Self::canonicalize(raw_url).map(Self) } + /// Normalize strings from forms, events, and Reddit imports into the same + /// stored id shape (e.g. drop post title slug after comment id). + pub fn from_storage(s: &str) -> Option { + let t = s.trim(); + if t.is_empty() { + return None; + } + if t.contains("://") || t.starts_with("r/") { + return Self::from_url(t).or_else(|| Self::parse(t)); + } + if t.starts_with("reddit.com/") && t.contains("/comments/") { + return Self::from_url(t).or_else(|| Self::parse(t)); + } + Self::parse(t).or_else(|| Self::from_url(t)) + } + /// Map legacy scope keys (`""`, `"rust"`) to fractal parent nodes. pub fn from_legacy_scope(raw: &str) -> Self { let s = raw.trim(); @@ -324,6 +340,12 @@ mod tests { assert_eq!(id.as_str(), "reddit.com/r/amitheasshole"); } + #[test] + fn from_storage_strips_post_title_slug() { + let id = ItemId::from_storage("reddit.com/r/rust/comments/aaa/announcing_rust_199").unwrap(); + assert_eq!(id.as_str(), "reddit.com/r/rust/comments/aaa"); + } + #[test] fn from_browse_uri_strips_prefix() { let id = ItemId::from_browse_uri("/~/https://reddit.com/r/rust").unwrap(); diff --git a/server/src/reddit.rs b/server/src/reddit.rs index 1168ec2afc77c092514eec91b513bac301cbe225..626454e5a2f638734193b5190a2beea286af85b6 100644 --- a/server/src/reddit.rs +++ b/server/src/reddit.rs @@ -567,7 +567,7 @@ fn parse_children(_parent: &ItemId, payload: &Value) -> Vec<(ItemId, Value)> { _ => continue, }; let path = format!("reddit.com{}", permalink.trim_end_matches('/')); - if let Some(id) = ItemId::parse(&path) { + if let Some(id) = ItemId::from_storage(&path) { out.push((id, child.clone())); } } diff --git a/server/src/reducer.rs b/server/src/reducer.rs index 336e78b77d3ab59361b89af5c9868e13da4ac962..fc23f41137d7df33997afe19c533505c250cc305 100644 --- a/server/src/reducer.rs +++ b/server/src/reducer.rs @@ -28,8 +28,8 @@ impl VoteData { ratio_left: i32, ratio_right: i32, ) -> Option { - let a = ItemId::parse(a)?; - let b = ItemId::parse(b)?; + let a = ItemId::from_storage(a)?; + let b = ItemId::from_storage(b)?; if a == b { return None; } @@ -85,8 +85,8 @@ impl GroupState { } pub fn apply_vote(&mut self, mut vote: VoteData) { - vote.a = ItemId::parse(vote.a.as_str()).unwrap_or_else(|| vote.a.clone()); - vote.b = ItemId::parse(vote.b.as_str()).unwrap_or_else(|| vote.b.clone()); + vote.a = ItemId::from_storage(vote.a.as_str()).unwrap_or(vote.a.clone()); + vote.b = ItemId::from_storage(vote.b.as_str()).unwrap_or(vote.b.clone()); if vote.ratio_left < 0 { vote.ratio_left = 0; } diff --git a/server/src/state.rs b/server/src/state.rs index d238701208a1c708b94a778a6a2e1891a678ecbc..4c3008e73cc74d8483a2064a0a280e73d82a01ec 100644 --- a/server/src/state.rs +++ b/server/src/state.rs @@ -18,7 +18,7 @@ pub fn parse_item_param(raw: &str) -> ItemId { if s.is_empty() { return ItemId::root(); } - ItemId::from_url(s).or_else(|| ItemId::parse(s)).unwrap_or_else(|| ItemId::opaque(s)) + ItemId::from_storage(s).unwrap_or_else(|| ItemId::opaque(s)) } /// Legacy: normalize raw ranking subject into a scope key for old event replay. diff --git a/server/src/ui_action.rs b/server/src/ui_action.rs index 53581e1362bfcc5dfb4ae3069c41ea6f7be41437..237411a689fb10ad1b7022ea66aee1969c7507ec 100644 --- a/server/src/ui_action.rs +++ b/server/src/ui_action.rs @@ -31,9 +31,9 @@ pub enum HtmlUiAction { /// Parent node [`ItemId`] string; empty = tree root. #[serde(default)] scope: String, - /// After vote, navigate here (vote compare page). + /// Posted from `/vote` compare UI — morph edge history in place. #[serde(default)] - next: String, + vote_compare: bool, }, /// Parse pasted Reddit URL/path; redirect to subreddit ranking on success. ParseQuery { @@ -98,7 +98,7 @@ mod tests { ratio_left: 60, ratio_right: 40, scope: "parent".into(), - next: String::new(), + vote_compare: false, } ); } @@ -129,7 +129,7 @@ mod tests { ratio_left: 2, ratio_right: 1, scope: "amitheasshole".into(), - next: String::new(), + vote_compare: false, } ); } @@ -156,7 +156,7 @@ mod tests { ratio_left: 2, ratio_right: 1, scope: String::new(), - next: String::new(), + vote_compare: false, } ); } diff --git a/server/static/sorter.css b/server/static/sorter.css index 3c2cec5cb5094013387147c621d75bd341bc06cc..b95718ce463b200a5667d3014dac2119836a0bef 100644 --- a/server/static/sorter.css +++ b/server/static/sorter.css @@ -289,11 +289,27 @@ h1 { } .vote-compare-nav { + margin: 1rem 0 0.5rem; +} + +.vote-compare-actions { display: flex; - justify-content: space-between; + flex-wrap: wrap; align-items: center; - gap: 1rem; - margin: 1rem 0; + gap: 0.75rem; + margin-top: 0.5rem; +} + +.vote-compare-actions .btn-secondary { + margin-top: 0; + display: inline-block; + text-decoration: none; + line-height: 1.4; +} + +.vote-compare-actions .vote-compare-next.is-disabled { + opacity: 0.6; + cursor: default; } .vote-compare-next { diff --git a/server/static/sorter_ui.js b/server/static/sorter_ui.js index c8d7c3ef2413399ced7898a7edb3e7a2c8e16163..ab15d27f508cc6f5fbca9d8926fd0ca26d64857b 100644 --- a/server/static/sorter_ui.js +++ b/server/static/sorter_ui.js @@ -104,11 +104,10 @@ if (f.getAttribute('data-navigate') === 'full') return; e.preventDefault(); await postUiForm(f); - if (f.id === 'vote-form' || f.id === 'vote-compare-form') { + if (f.id === 'vote-form') { f.reset(); - var slider = f.querySelector('#vote-preference-slider'); - if (slider) slider.value = '50'; - initVoteSlider(); + var firstField = f.querySelector('input[type="text"]'); + if (firstField) firstField.focus(); } }); } diff --git a/server/tests/integration_ui.rs b/server/tests/integration_ui.rs index df7d9ab357531c5146d0b326c149fdaa6b531a4a..56d2db313b313ffce07eff38a8ccdaa2fbb9498f 100644 --- a/server/tests/integration_ui.rs +++ b/server/tests/integration_ui.rs @@ -25,6 +25,53 @@ async fn start_test_server() -> (SocketAddr, TempDir) { (addr, tmp) } +#[tokio::test] +async fn post_ui_vote_compare_morphs_edge_history() { + let (addr, _tmp) = start_test_server().await; + let parent = "reddit.com/r/rust"; + let a = "reddit.com/r/rust/comments/aaa/announcing_rust_199"; + let b = "reddit.com/r/rust/comments/bbb/what_are_you_working_on"; + + let rpc = serde_json::json!({ + "action": "record_vote", + "a": a, + "b": b, + "ratio_left": {"$form:i32": "ratio_left"}, + "ratio_right": {"$form:i32": "ratio_right"}, + "scope": parent, + "vote_compare": true, + }) + .to_string(); + let mut form = HashMap::new(); + form.insert(UI_RPC_FIELD.to_string(), rpc); + form.insert("ratio_left".into(), "70".into()); + form.insert("ratio_right".into(), "30".into()); + + let client = reqwest::Client::new(); + let body = client + .post(format!("http://{addr}/ui")) + .form(&form) + .send() + .await + .unwrap() + .text() + .await + .unwrap(); + + assert!( + body.contains("vote-edge-history"), + "expected edge history morph, got: {body}" + ); + assert!( + body.contains("70:30"), + "expected recorded ratio in morph, got: {body}" + ); + assert!( + !body.contains("no votes on this pair yet"), + "should not show empty edge history after vote, got: {body}" + ); +} + #[tokio::test] async fn post_ui_record_vote_morphs_ranking_and_persists() { let (addr, tmp) = start_test_server().await; diff --git a/test/reddit_import.clj b/test/reddit_import.clj index 45a2a19f20799d77e84d8aa64735ab5e7e45f97c..b476488526252c13fd73bdda76e5201678e4a714 100644 --- a/test/reddit_import.clj +++ b/test/reddit_import.clj @@ -2,9 +2,8 @@ (:require [babashka.process :as process] [clojure.java.io :as io] [clojure.string :as str] - [clojure.test :refer [deftest is testing]]) - (:import [com.sun.net.httpserver HttpServer HttpHandler HttpExchange] - [java.net InetSocketAddress])) + [clojure.test :refer [deftest is testing]] + [test.support.mock-reddit :as mock-reddit])) (defn- repo-root [] (.getCanonicalPath (io/file (System/getProperty "user.dir")))) @@ -13,29 +12,6 @@ (with-open [s (java.net.ServerSocket. 0)] (.getLocalPort s))) -(defn- start-mock-reddit [port fixtures-dir] - (let [about (.getBytes (slurp (io/file fixtures-dir "r_rust_about.json")) "UTF-8") - listing (.getBytes (slurp (io/file fixtures-dir "r_rust_listing.json")) "UTF-8") - server (HttpServer/create (InetSocketAddress. "127.0.0.1" port) 0) - handler - (proxy [HttpHandler] [] - (handle [^HttpExchange exchange] - ;; Route by path: `/r//about.json` is the subreddit entity, - ;; `/r/.json` is the children listing. - (let [path (.getPath (.getRequestURI exchange)) - body (if (str/includes? path "/about") - about - listing)] - (.sendResponseHeaders exchange 200 (alength body)) - (let [out (.getResponseBody exchange)] - (.write out body) - (.close out)))))] - (.createContext server "/" handler) - (.setExecutor server nil) - (.start server) - (fn stop [] - (.stop server 0)))) - (defn- wait-health [base-url ms] (let [deadline (+ (System/currentTimeMillis) ms) url (str base-url "/healthz")] @@ -100,7 +76,7 @@ (deftest reddit-fetch-via-mock-api (testing "Fetch more queues import; event log stores full payload; page shows title" (let [root (repo-root) - fixtures (str root "/test/fixtures/reddit") + fixtures (mock-reddit/fixtures-dir root) data-dir (.getAbsolutePath (doto (io/file (System/getProperty "java.io.tmpdir") (str "sorter2-reddit-" (System/currentTimeMillis))) @@ -110,7 +86,7 @@ reddit-base (str "http://127.0.0.1:" reddit-port) app-base (str "http://127.0.0.1:" app-port) bin (str root "/target/release/sorter2-server") - stop-mock (start-mock-reddit reddit-port fixtures)] + stop-mock (mock-reddit/start-mock-reddit reddit-port fixtures)] (try (is (zero? (:exit (process/shell {:dir root} "cargo" "build" "--release" "--package" "sorter2-server"))) diff --git a/test/support/mock_reddit.clj b/test/support/mock_reddit.clj new file mode 100644 index 0000000000000000000000000000000000000000..5efa92db3e1f79b8f423a2f1adcbda959123c1ad --- /dev/null +++ b/test/support/mock_reddit.clj @@ -0,0 +1,35 @@ +(ns test.support.mock-reddit + "In-process HTTP stub for Reddit API fixtures (`test/fixtures/reddit/`)." + (:require [clojure.java.io :as io] + [clojure.string :as str]) + (:import [com.sun.net.httpserver HttpServer HttpHandler HttpExchange] + [java.net InetSocketAddress])) + +(defn fixtures-dir + ([] (fixtures-dir (System/getProperty "user.dir"))) + ([root] (str root "/test/fixtures/reddit"))) + +(defn start-mock-reddit + "Start a mock Reddit API on `port`. Returns a zero-arg `stop` function." + ([port] (start-mock-reddit port (fixtures-dir))) + ([port dir] + (let [about (.getBytes (slurp (io/file dir "r_rust_about.json")) "UTF-8") + listing (.getBytes (slurp (io/file dir "r_rust_listing.json")) "UTF-8") + server (HttpServer/create (InetSocketAddress. "127.0.0.1" port) 0) + handler + (proxy [HttpHandler] [] + (handle [^HttpExchange exchange] + ;; `/r//about.json` → subreddit entity; `/r/.json` → listing. + (let [path (.getPath (.getRequestURI exchange)) + body (if (str/includes? path "/about") + about + listing)] + (.sendResponseHeaders exchange 200 (alength body)) + (let [out (.getResponseBody exchange)] + (.write out body) + (.close out)))))] + (.createContext server "/" handler) + (.setExecutor server nil) + (.start server) + (fn stop [] + (.stop server 0))))) diff --git a/test/vote_compare.clj b/test/vote_compare.clj new file mode 100644 index 0000000000000000000000000000000000000000..fbc00281e466a1ba0833193c7b96fa30aeb45a5c --- /dev/null +++ b/test/vote_compare.clj @@ -0,0 +1,101 @@ +(ns test.vote-compare + (:require [babashka.process :as process] + [clojure.java.io :as io] + [clojure.string :as str] + [clojure.test :refer [deftest is testing]] + [com.blockether.spel.core :as core] + [com.blockether.spel.locator :as loc] + [com.blockether.spel.page :as page] + [test.support.mock-reddit :as mock-reddit]) + (:import [java.net URLEncoder])) + +(defn- repo-root [] + (.getCanonicalPath (io/file (System/getProperty "user.dir")))) + +(defn- pick-port [] + (with-open [s (java.net.ServerSocket. 0)] + (.getLocalPort s))) + +(defn- wait-health [base-url ms] + (let [deadline (+ (System/currentTimeMillis) ms) + url (str base-url "/healthz")] + (loop [] + (let [resp (try + (process/shell {:out :string :err :string} + "curl" "-sf" url) + (catch Exception _ nil))] + (if (and resp (zero? (:exit resp)) (= "ok" (str/trim (:out resp "")))) + true + (if (< (System/currentTimeMillis) deadline) + (do (Thread/sleep 200) (recur)) + false)))))) + +(defn- curl-fetch-children [base item] + (process/shell {:out :string :err :string} + "curl" "-sfN" "--max-time" "20" + "-X" "POST" (str base "/ui") + "--data-urlencode" + (str "__rpc__={\"action\":\"fetch_entity\",\"item\":\"" item + "\",\"kind\":\"children\"}"))) + +(defn- vote-page-url [base parent] + (str base "/vote?parent=" + (URLEncoder/encode parent "UTF-8"))) + +(deftest vote-compare-shows-recorded-vote-after-post + (testing "post vote on /vote morphs edge history (mock Reddit children seeded)" + (let [root (repo-root) + fixtures (mock-reddit/fixtures-dir root) + data-dir (.getAbsolutePath + (doto (io/file (System/getProperty "java.io.tmpdir") + (str "sorter2-vote-" (System/currentTimeMillis))) + (.mkdirs))) + reddit-port (pick-port) + app-port (pick-port) + reddit-base (str "http://127.0.0.1:" reddit-port) + app-base (str "http://127.0.0.1:" app-port) + bin (str root "/target/release/sorter2-server") + stop-mock (mock-reddit/start-mock-reddit reddit-port fixtures)] + (try + (is (zero? (:exit (process/shell {:dir root} + "cargo" "build" "--release" "--package" "sorter2-server"))) + "release build succeeds") + (let [proc (process/process {:dir root + :env (into (into {} (System/getenv)) + {"SORTER2_SKIP_DOTENV" "1" + "SORTER2_DATA_DIR" data-dir + "SORTER2_EVENT_LOG" (str data-dir "/events.jsonl") + "PORT" (str app-port) + "REDDIT_API_BASE" reddit-base + "REDDIT_OAUTH_BASE" reddit-base + "REDDIT_CLIENT_ID" "" + "REDDIT_CLIENT_SECRET" "" + "REDDIT_APP_ID" "" + "REDDIT_APP_SECRET" ""}) + :out :string + :err :string} + bin)] + (try + (is (wait-health app-base 20000) "app healthz") + (let [fetch (curl-fetch-children app-base "reddit.com/r/rust")] + (is (zero? (:exit fetch)) "fetch posts via mock Reddit") + (is (str/includes? (:out fetch) "Idiomorph.morph"))) + (core/with-testing-page [pg] + (page/navigate pg (vote-page-url app-base "reddit.com/r/rust")) + (page/wait-for-selector pg "#vote-compare-form") + (let [before (loc/text-content (page/locator pg "#vote-edge-history-region"))] + (is (str/includes? before "no votes on this pair yet") + "empty edge history before first vote")) + (loc/click (page/get-by-test-id pg "vote-post")) + (page/wait-for-selector pg ".vote-edge-history-title") + (let [after (loc/text-content (page/locator pg "#vote-edge-history-region"))] + (is (str/includes? after "votes on this pair") + "shows edge history title after vote") + (is (str/includes? after "50:50") + "shows submitted ratio after vote") + (is (not (str/includes? after "no votes on this pair yet")) + "does not revert to empty edge history"))) + (finally + (process/destroy proc)))) + (finally + (stop-mock))))))