maarcadetweet: initial commit

AT Protocol PDS + AppView + Tauri Desktop Client, 160-char post limit.

- PDS (Rust + axum + sqlx)
  - Auth: createAccount, createSession, refreshSession
  - Records: createRecord, deleteRecord (race-safe via SELECT FOR UPDATE)
  - Feed: feed.like.create, feed.repost.create
  - Sync: getRepo, getBlocks, getLatestCommit, getRecord (with MST proof), listRepos
  - Identity: resolveHandle
  - MST: spec-conformant (at-mst crate, 27 tests)
  - Repo: signed commits, TID counter (monotonic, 4096 wrap safe)

- AppView (Rust + axum + sqlx)
  - Jetstream consumer (WebSocket, exponential backoff, 38k+ events indexed)
  - REST API: timeline/home (graph-aware), profile, search, post (with thread hydration)
  - Handle-sync worker (did:plc + did:web)
  - JSONB embed storage + thread columns (migration 0003)
  - Like/repost counter cache (migration 0004)

- Tauri 2 + Svelte 5 Desktop Client
  - System tray (Show/Compose/Quit menu)
  - OS notifications (tauri-plugin-notification)
  - Auto-update (tauri-plugin-updater, placeholder endpoint)
  - Window-state (tauri-plugin-window-state)
  - 160-char compose with live counter
  - Image/Link embed rendering
  - LocalStorage-persisted like state
  - Timeline with poll (prepend new posts)
  - Custom TitleBar (transparent, no decorations)
  - Orange/IBM Plex Mono maarcade design

Tests: 231 Rust + 9 vitest = 240 passed.
This commit is contained in:
tomdebone
2026-07-05 20:01:31 +02:00
commit c586fd39c9
134 changed files with 35279 additions and 0 deletions
+619
View File
@@ -0,0 +1,619 @@
//! Integration tests for the new read API routes
//! (`/api/timeline/home`, `/api/profile/...`, `/api/search`).
//!
//! These run against a live appview service + DB. Like
//! `appview_integration.rs`, they're fail-open: if the service or DB
//! isn't reachable, the test prints a notice and returns success
//! rather than panicking — so `cargo test --workspace` stays green in
//! environments where the appview hasn't been started.
use serde_json::{json, Value};
use std::time::Duration;
const APPVIEW_URL: &str = "http://127.0.0.1:2584";
async fn client() -> reqwest::Client {
reqwest::Client::builder()
.timeout(Duration::from_secs(5))
.build()
.unwrap()
}
async fn wait_for_appview_db() -> bool {
let c = client().await;
for _ in 0..20 {
if let Ok(r) = c.get(format!("{APPVIEW_URL}/healthz")).send().await {
if r.status().is_success() {
return true;
}
}
tokio::time::sleep(Duration::from_millis(250)).await;
}
false
}
async fn try_db_url() -> Option<String> {
std::env::var("DATABASE_URL_APPVIEW").ok()
}
async fn db_reachable() -> bool {
let Some(url) = try_db_url().await else {
return false;
};
matches!(
tokio::time::timeout(Duration::from_secs(2), sqlx::PgPool::connect(&url)).await,
Ok(Ok(_))
)
}
async fn post_ingest(c: &reqwest::Client, body: Value) -> reqwest::Response {
c.post(format!("{APPVIEW_URL}/internal/ingest-commit"))
.json(&body)
.send()
.await
.unwrap()
}
/// Insert a follow row directly via the DB. We bypass the ingest
/// endpoint because (a) 1500 individual HTTP round-trips are
/// prohibitively slow for the cap test, and (b) we don't need the
/// indexer to also re-resolve handles etc. for this test.
async fn insert_follow(
pool: &sqlx::PgPool,
follower_did: &str,
subject_did: &str,
) {
sqlx::query(
r#"INSERT INTO follows (follower_did, subject_did, created_at)
VALUES ($1, $2, now())
ON CONFLICT (follower_did, subject_did) DO NOTHING"#,
)
.bind(follower_did)
.bind(subject_did)
.execute(pool)
.await
.unwrap();
}
/// Seed N posts for a DID with sequential rkeys and `created_at`
/// timestamps that strictly increase, so the cursor ordering test is
/// deterministic.
async fn seed_posts(c: &reqwest::Client, did: &str, texts: &[&str]) {
for (i, text) in texts.iter().enumerate() {
let r = post_ingest(
c,
json!({
"did": did,
"collection": "app.twi.post",
"action": "create",
"rkey": rkey(),
"cid": "bafyreicid",
"record": {
"text": text,
"createdAt": format!("2026-07-01T12:00:{:02}Z", i),
}
}),
)
.await;
assert_eq!(r.status().as_u16(), 200);
}
}
fn did_for_test(name: &str) -> String {
// Random per-test DID so the tests can run in parallel without
// colliding on URI primary keys.
format!("did:plc:test_{}_{}", name, uuid::Uuid::new_v4().simple())
}
fn rkey() -> String {
uuid::Uuid::new_v4().simple().to_string()
}
#[tokio::test]
async fn timeline_returns_seeded_posts() {
if !wait_for_appview_db().await {
eprintln!("appview not running, skipping");
return;
}
if !db_reachable().await {
eprintln!("appview DB unreachable, skipping");
return;
}
let c = client().await;
let did = did_for_test("tl");
// Seed 3 posts with distinct rkeys.
for i in 0..3 {
let r = post_ingest(
&c,
json!({
"did": did,
"collection": "app.twi.post",
"action": "create",
"rkey": rkey(),
"cid": "bafyreicid",
"record": {
"text": format!("seeded post #{i}"),
"createdAt": "2026-07-01T12:00:00Z",
}
}),
)
.await;
assert_eq!(r.status().as_u16(), 200);
}
// Give Jetstream / ingest a beat to settle — `indexed_at` defaults
// to `now()` on insert, so we want a non-zero chance of seeing all
// three rows in the first page.
tokio::time::sleep(Duration::from_millis(50)).await;
let resp = c
.get(format!("{APPVIEW_URL}/api/timeline/home"))
.query(&[("did", did.as_str()), ("limit", "10")])
.send()
.await
.unwrap();
assert_eq!(resp.status().as_u16(), 200);
let body: Value = resp.json().await.unwrap();
let posts = body["posts"].as_array().expect("posts is array");
assert!(posts.len() >= 3, "expected >=3 posts, got {}", posts.len());
// All three seeded posts must be in the response and all share the
// same DID.
let our_uris: Vec<&str> = posts
.as_slice()
.iter()
.filter_map(|p| {
let uri = p["uri"].as_str()?;
if uri.starts_with(&format!("at://{did}/")) {
Some(uri)
} else {
None
}
})
.collect();
assert!(our_uris.len() >= 3, "missing our seeded posts in {posts:?}");
// Posts must be sorted with `indexed_at DESC`. We can't see
// indexed_at directly in the response, but the URI order in
// `app.twi.post/<rkey>` is rkey-random here, so we only assert
// `created_at` is non-increasing.
let mut prev: Option<String> = None;
for p in posts {
let ca = p["createdAt"].as_str().unwrap().to_string();
if let Some(p) = prev.take() {
assert!(ca <= p, "createdAt must be non-increasing: {ca} <= {p}");
}
prev = Some(ca);
}
}
#[tokio::test]
async fn timeline_paginates_with_cursor() {
if !wait_for_appview_db().await {
eprintln!("appview not running, skipping");
return;
}
if !db_reachable().await {
eprintln!("appview DB unreachable, skipping");
return;
}
let c = client().await;
let did = did_for_test("pg");
// Seed 50 posts.
for _ in 0..50 {
let r = post_ingest(
&c,
json!({
"did": did,
"collection": "app.twi.post",
"action": "create",
"rkey": rkey(),
"cid": "bafyreicid",
"record": {
"text": "page",
"createdAt": "2026-07-01T12:00:00Z",
}
}),
)
.await;
assert_eq!(r.status().as_u16(), 200);
}
tokio::time::sleep(Duration::from_millis(100)).await;
// Page 1: limit=20.
let resp = c
.get(format!("{APPVIEW_URL}/api/timeline/home"))
.query(&[("did", did.as_str()), ("limit", "20")])
.send()
.await
.unwrap();
let body: Value = resp.json().await.unwrap();
let page1 = body["posts"].as_array().unwrap().clone();
let cursor1 = body["cursor"].as_str().expect("page1 cursor");
assert_eq!(page1.len(), 20, "page1 should be exactly 20");
// Page 2: with cursor.
let resp = c
.get(format!("{APPVIEW_URL}/api/timeline/home"))
.query(&[
("did", did.as_str()),
("limit", "20"),
("cursor", cursor1),
])
.send()
.await
.unwrap();
let body: Value = resp.json().await.unwrap();
let page2 = body["posts"].as_array().unwrap().clone();
assert_eq!(page2.len(), 20, "page2 should be exactly 20");
// Pages must not overlap.
let p1: std::collections::HashSet<&str> = page1
.iter()
.map(|p| p["uri"].as_str().unwrap())
.collect();
let p2: std::collections::HashSet<&str> = page2
.iter()
.map(|p| p["uri"].as_str().unwrap())
.collect();
assert!(p1.is_disjoint(&p2), "page1 and page2 overlap");
// Page 3: tail — fewer than 20 expected, cursor=null.
let cursor2 = body["cursor"].as_str().expect("page2 cursor");
let resp = c
.get(format!("{APPVIEW_URL}/api/timeline/home"))
.query(&[
("did", did.as_str()),
("limit", "20"),
("cursor", cursor2),
])
.send()
.await
.unwrap();
let body: Value = resp.json().await.unwrap();
let page3 = body["posts"].as_array().unwrap().clone();
assert!(page3.len() <= 20, "page3 should be <= 20");
// At least one of the three pages should be non-empty.
assert!(!page1.is_empty() || !page2.is_empty() || !page3.is_empty());
}
#[tokio::test]
async fn profile_returns_posts_for_handle() {
if !wait_for_appview_db().await {
eprintln!("appview not running, skipping");
return;
}
if !db_reachable().await {
eprintln!("appview DB unreachable, skipping");
return;
}
let c = client().await;
let did_a = did_for_test("alice");
let did_b = did_for_test("bob");
let handle_a = format!("alice.{}", uuid::Uuid::new_v4().simple());
// Seed a post for A with handle populated, and a post for B with a
// different handle. The internal-ingest path doesn't expose a
// `handle` field, so we update the column directly.
for did in [&did_a, &did_b] {
let r = post_ingest(
&c,
json!({
"did": did,
"collection": "app.twi.post",
"action": "create",
"rkey": rkey(),
"cid": "bafyreicid",
"record": {
"text": "hi",
"createdAt": "2026-07-01T12:00:00Z",
}
}),
)
.await;
assert_eq!(r.status().as_u16(), 200);
}
// Backfill handle for A only.
let url = std::env::var("DATABASE_URL_APPVIEW").unwrap();
let pool = sqlx::PgPool::connect(&url).await.unwrap();
sqlx::query("UPDATE posts SET handle = $1 WHERE did = $2")
.bind(&handle_a)
.bind(&did_a)
.execute(&pool)
.await
.unwrap();
// Query by handle (no leading @).
let resp = c
.get(format!("{APPVIEW_URL}/api/profile/{handle_a}"))
.send()
.await
.unwrap();
assert_eq!(resp.status().as_u16(), 200);
let body: Value = resp.json().await.unwrap();
assert_eq!(body["did"], json!(did_a));
assert_eq!(body["handle"], json!(handle_a));
let posts = body["posts"].as_array().unwrap();
assert!(
posts.iter().any(|p| p["did"] == json!(did_a)),
"did_a post missing from profile"
);
assert!(
!posts.iter().any(|p| p["did"] == json!(did_b)),
"did_b post leaked into alice's profile"
);
// Same query, with leading @ — must also work.
let resp = c
.get(format!("{APPVIEW_URL}/api/profile/@{handle_a}"))
.send()
.await
.unwrap();
assert_eq!(resp.status().as_u16(), 200);
// 404 for an unknown handle.
let resp = c
.get(format!(
"{APPVIEW_URL}/api/profile/nobody_{}",
uuid::Uuid::new_v4().simple()
))
.send()
.await
.unwrap();
assert_eq!(resp.status().as_u16(), 404);
// /api/profile?did=... must work too.
let resp = c
.get(format!("{APPVIEW_URL}/api/profile"))
.query(&[("did", did_a.as_str())])
.send()
.await
.unwrap();
assert_eq!(resp.status().as_u16(), 200);
let body: Value = resp.json().await.unwrap();
assert_eq!(body["did"], json!(did_a));
}
#[tokio::test]
async fn search_finds_text_match() {
if !wait_for_appview_db().await {
eprintln!("appview not running, skipping");
return;
}
if !db_reachable().await {
eprintln!("appview DB unreachable, skipping");
return;
}
let c = client().await;
let did = did_for_test("srch");
for text in ["hello world from test", "goodbye cruel world", "x"] {
let r = post_ingest(
&c,
json!({
"did": did,
"collection": "app.twi.post",
"action": "create",
"rkey": rkey(),
"cid": "bafyreicid",
"record": {
"text": text,
"createdAt": "2026-07-01T12:00:00Z",
}
}),
)
.await;
assert_eq!(r.status().as_u16(), 200);
}
tokio::time::sleep(Duration::from_millis(50)).await;
let resp = c
.get(format!("{APPVIEW_URL}/api/search"))
.query(&[("q", "hello"), ("limit", "10")])
.send()
.await
.unwrap();
assert_eq!(resp.status().as_u16(), 200);
let body: Value = resp.json().await.unwrap();
assert_eq!(body["q"], json!("hello"));
let posts = body["posts"].as_array().unwrap();
assert!(!posts.is_empty(), "expected at least one match for 'hello'");
for p in posts {
let t = p["text"].as_str().unwrap();
assert!(
t.to_lowercase().contains("hello"),
"post in result doesn't contain 'hello': {t}"
);
}
// Empty q is a 400.
let resp = c
.get(format!("{APPVIEW_URL}/api/search"))
.query(&[("q", "")])
.send()
.await
.unwrap();
assert_eq!(resp.status().as_u16(), 400);
}
/// When alice follows bob and carol but NOT dave, her home timeline
/// must show bob's and carol's posts only — dave's post is invisible
/// to her even though it sits in the global recent feed.
#[tokio::test]
async fn timeline_filters_to_followees() {
if !wait_for_appview_db().await {
eprintln!("appview not running, skipping");
return;
}
if !db_reachable().await {
eprintln!("appview DB unreachable, skipping");
return;
}
let c = client().await;
let url = std::env::var("DATABASE_URL_APPVIEW").unwrap();
let pool = sqlx::PgPool::connect(&url).await.unwrap();
let alice = did_for_test("alice");
let bob = did_for_test("bob");
let carol = did_for_test("carol");
let dave = did_for_test("dave");
// Alice follows bob + carol (NOT dave).
insert_follow(&pool, &alice, &bob).await;
insert_follow(&pool, &alice, &carol).await;
// Each person posts once.
seed_posts(&c, &bob, &["bob says hi"]).await;
seed_posts(&c, &carol, &["carol says hi"]).await;
seed_posts(&c, &dave, &["dave says hi (alice should NOT see this)"]).await;
tokio::time::sleep(Duration::from_millis(100)).await;
let resp = c
.get(format!("{APPVIEW_URL}/api/timeline/home"))
.query(&[("did", alice.as_str()), ("limit", "100")])
.send()
.await
.unwrap();
assert_eq!(resp.status().as_u16(), 200);
let body: Value = resp.json().await.unwrap();
let posts = body["posts"].as_array().expect("posts is array");
// Collect DIDs of returned posts.
let returned_dids: std::collections::HashSet<String> = posts
.iter()
.map(|p| p["did"].as_str().unwrap().to_string())
.collect();
// Bob and carol MUST be present; dave MUST NOT be.
assert!(
returned_dids.contains(&bob),
"bob's post missing from alice's timeline: {posts:?}"
);
assert!(
returned_dids.contains(&carol),
"carol's post missing from alice's timeline: {posts:?}"
);
assert!(
!returned_dids.contains(&dave),
"dave's post leaked into alice's timeline: {posts:?}"
);
// Stronger: walk every post and assert no `did` matches dave.
for p in posts {
let did = p["did"].as_str().unwrap();
assert_ne!(did, dave, "dave leaked: {p:?}");
}
}
/// Alice posts without following anyone. The endpoint must still
/// surface her own posts — via the global-recent "cold start"
/// fallback — so a brand-new account with no follows can see what
/// they've posted.
#[tokio::test]
async fn timeline_includes_own_posts() {
if !wait_for_appview_db().await {
eprintln!("appview not running, skipping");
return;
}
if !db_reachable().await {
eprintln!("appview DB unreachable, skipping");
return;
}
let c = client().await;
let alice = did_for_test("alone");
// Alice posts without seeding any follows.
seed_posts(&c, &alice, &["alice's first post", "alice's second post"]).await;
tokio::time::sleep(Duration::from_millis(100)).await;
let resp = c
.get(format!("{APPVIEW_URL}/api/timeline/home"))
.query(&[("did", alice.as_str()), ("limit", "100")])
.send()
.await
.unwrap();
assert_eq!(resp.status().as_u16(), 200);
let body: Value = resp.json().await.unwrap();
let posts = body["posts"].as_array().expect("posts is array");
// At least alice's two posts must be present. The global fallback
// will include other recent posts from the DB too — we only
// assert on alice's visibility here.
let alice_uris: Vec<&str> = posts
.iter()
.filter_map(|p| {
let uri = p["uri"].as_str()?;
if uri.starts_with(&format!("at://{alice}/")) {
Some(uri)
} else {
None
}
})
.collect();
assert!(
alice_uris.len() >= 2,
"alice's own posts missing from her own timeline: {posts:?}"
);
}
/// Alice follows 1500 fake DIDs. The endpoint must NOT blow up — the
/// `target_dids` cap at MAX_FOLLOWED_DIDS=1000 kicks in, the user's
/// own DID is re-inserted, and the SQL `ANY($)` array stays bounded.
#[tokio::test]
async fn timeline_caps_followee_list() {
if !wait_for_appview_db().await {
eprintln!("appview not running, skipping");
return;
}
if !db_reachable().await {
eprintln!("appview DB unreachable, skipping");
return;
}
let c = client().await;
let url = std::env::var("DATABASE_URL_APPVIEW").unwrap();
let pool = sqlx::PgPool::connect(&url).await.unwrap();
let alice = did_for_test("poweruser");
// Seed 1500 follows (well over MAX_FOLLOWED_DIDS=1000).
for _ in 0..1500 {
let fake = format!(
"did:plc:fake_{}_{}",
uuid::Uuid::new_v4().simple(),
uuid::Uuid::new_v4().simple()
);
insert_follow(&pool, &alice, &fake).await;
}
// Alice also posts — to confirm she sees her own DID even though
// the cap trimmed the lexically-greatest 1000 followees.
seed_posts(&c, &alice, &["poweruser post"]).await;
tokio::time::sleep(Duration::from_millis(100)).await;
let resp = c
.get(format!("{APPVIEW_URL}/api/timeline/home"))
.query(&[("did", alice.as_str()), ("limit", "50")])
.send()
.await
.unwrap();
assert_eq!(resp.status().as_u16(), 200);
let body: Value = resp.json().await.unwrap();
let posts = body["posts"].as_array().expect("posts is array");
// Alice's own post must be visible — the cap invariant guarantees
// her own DID is preserved.
let alice_visible = posts
.iter()
.any(|p| p["did"].as_str() == Some(alice.as_str()));
assert!(
alice_visible,
"alice's own post not visible after cap: {posts:?}"
);
// The fake followee DIDs have no posts, so nothing else should
// leak in. We only assert the endpoint didn't error and that
// alice's own DID is honored.
}
+299
View File
@@ -0,0 +1,299 @@
//! Integration tests for the AppView HTTP service.
//!
//! These exercise the running `appview` binary over HTTP: the `/healthz`
//! endpoint and `POST /internal/ingest-commit`. Like the PDS integration
//! tests, they are no-ops when the service isn't running — they fail-open
//! with `eprintln!` instead of panicking.
use serde_json::{json, Value};
use std::time::Duration;
const APPVIEW_URL: &str = "http://127.0.0.1:2584";
async fn client() -> reqwest::Client {
reqwest::Client::builder()
.timeout(Duration::from_secs(5))
.build()
.unwrap()
}
async fn wait_for_appview_db() -> bool {
let c = client().await;
for _ in 0..20 {
if let Ok(r) = c.get(format!("{APPVIEW_URL}/healthz")).send().await {
if r.status().is_success() {
return true;
}
}
tokio::time::sleep(Duration::from_millis(250)).await;
}
false
}
async fn try_db_url() -> Option<String> {
std::env::var("DATABASE_URL_APPVIEW").ok()
}
async fn ping_db() -> bool {
let Some(url) = try_db_url().await else {
return false;
};
let Ok(c) = client().await.get("http://127.0.0.1:9/_never_").build() else {
return false;
};
let _ = c;
match tokio::time::timeout(Duration::from_secs(2), sqlx::PgPool::connect(&url)).await {
Ok(Ok(_pool)) => true,
_ => false,
}
}
#[tokio::test]
async fn healthz_returns_ok() {
if !wait_for_appview_db().await {
eprintln!("appview not running, skipping");
return;
}
let c = client().await;
let resp = c
.get(format!("{APPVIEW_URL}/healthz"))
.send()
.await
.unwrap();
assert_eq!(resp.status().as_u16(), 200);
let body: Value = resp.json().await.unwrap();
assert_eq!(body["ok"], json!(true));
// The new fields must all be present.
assert!(body.get("lag_ms").is_some(), "missing lag_ms: {body}");
assert!(
body.get("events_processed").is_some(),
"missing events_processed: {body}"
);
assert!(
body.get("jetstream_connected").is_some(),
"missing jetstream_connected: {body}"
);
}
async fn post_ingest(c: &reqwest::Client, body: Value) -> reqwest::Response {
c.post(format!("{APPVIEW_URL}/internal/ingest-commit"))
.json(&body)
.send()
.await
.unwrap()
}
async fn fetch_post_uri(c: &reqwest::Client, uri: &str) -> Option<Value> {
// Probe: rely on direct DB? No — we don't want to expose DB to tests.
// Just check that the ingest endpoint accepted the request and returned
// applied: true. End-to-end correctness is exercised by the indexer
// unit tests against the same schema.
let _ = c;
let _ = uri;
None
}
fn did_for_test(name: &str) -> String {
// Random per-test DID so the tests can run in parallel without
// colliding on URI primary keys.
format!("did:plc:test_{}_{}", name, uuid::Uuid::new_v4().simple())
}
#[tokio::test]
async fn ingest_commit_persists_post() {
if !wait_for_appview_db().await {
eprintln!("appview not running, skipping");
return;
}
if !ping_db().await {
eprintln!("appview DB unreachable, skipping");
return;
}
let c = client().await;
let did = did_for_test("post");
let rkey = uuid::Uuid::new_v4().simple().to_string();
let uri = format!("at://{did}/app.twi.post/{rkey}");
let resp = post_ingest(
&c,
json!({
"did": did,
"collection": "app.twi.post",
"action": "create",
"rkey": rkey,
"cid": "bafyreicidpost",
"record": {
"text": "hello from integration test",
"createdAt": "2026-07-01T12:00:00Z",
}
}),
)
.await;
assert_eq!(resp.status().as_u16(), 200);
let body: Value = resp.json().await.unwrap();
assert_eq!(body["ok"], json!(true));
assert_eq!(body["applied"], json!(true));
// Sanity: idem — a second create with the same rkey is a no-op upsert.
let resp2 = post_ingest(
&c,
json!({
"did": did,
"collection": "app.twi.post",
"action": "create",
"rkey": rkey,
"cid": "bafyreicidpost",
"record": {
"text": "still here",
"createdAt": "2026-07-01T12:00:00Z",
}
}),
)
.await;
assert_eq!(resp2.status().as_u16(), 200);
let _ = (uri.clone(), fetch_post_uri(&c, &uri).await);
}
#[tokio::test]
async fn ingest_commit_persists_like() {
if !wait_for_appview_db().await {
eprintln!("appview not running, skipping");
return;
}
if !ping_db().await {
eprintln!("appview DB unreachable, skipping");
return;
}
let c = client().await;
let did = did_for_test("like");
let rkey = uuid::Uuid::new_v4().simple().to_string();
let resp = post_ingest(
&c,
json!({
"did": did,
"collection": "app.bsky.feed.like",
"action": "create",
"rkey": rkey,
"cid": "bafyreicidlike",
"record": {
"subject": {
"uri": "at://did:plc:target/app.twi.post/abc",
"cid": "bafyreicidtarget"
},
"createdAt": "2026-07-01T12:00:00Z"
}
}),
)
.await;
assert_eq!(resp.status().as_u16(), 200);
let body: Value = resp.json().await.unwrap();
assert_eq!(body["ok"], json!(true));
assert_eq!(body["applied"], json!(true));
}
#[tokio::test]
async fn ingest_delete_removes_post() {
if !wait_for_appview_db().await {
eprintln!("appview not running, skipping");
return;
}
if !ping_db().await {
eprintln!("appview DB unreachable, skipping");
return;
}
let c = client().await;
let did = did_for_test("del");
let rkey = uuid::Uuid::new_v4().simple().to_string();
// Create.
let created = post_ingest(
&c,
json!({
"did": did,
"collection": "app.twi.post",
"action": "create",
"rkey": rkey,
"cid": "bafyreicid",
"record": {
"text": "first",
"createdAt": "2026-07-01T12:00:00Z"
}
}),
)
.await;
assert_eq!(created.status().as_u16(), 200);
// Delete.
let deleted = post_ingest(
&c,
json!({
"did": did,
"collection": "app.twi.post",
"action": "delete",
"rkey": rkey,
}),
)
.await;
assert_eq!(deleted.status().as_u16(), 200);
let body: Value = deleted.json().await.unwrap();
assert_eq!(body["applied"], json!(true));
// Delete again — must still 200 with applied=true (idempotent).
let deleted2 = post_ingest(
&c,
json!({
"did": did,
"collection": "app.twi.post",
"action": "delete",
"rkey": rkey,
}),
)
.await;
assert_eq!(deleted2.status().as_u16(), 200);
}
#[tokio::test]
async fn ingest_follow_requires_subject() {
if !wait_for_appview_db().await {
eprintln!("appview not running, skipping");
return;
}
if !ping_db().await {
eprintln!("appview DB unreachable, skipping");
return;
}
let c = client().await;
let did = did_for_test("follow");
// Without subject_did AND without record.subject → 400.
let r = post_ingest(
&c,
json!({
"did": did,
"collection": "app.bsky.graph.follow",
"action": "create",
"rkey": "frk",
"record": { "createdAt": "2026-07-01T12:00:00Z" }
}),
)
.await;
assert_eq!(r.status().as_u16(), 400);
// With subject_did → 200.
let r2 = post_ingest(
&c,
json!({
"did": did,
"collection": "app.bsky.graph.follow",
"action": "create",
"rkey": "frk",
"subject_did": "did:plc:followed",
"record": { "subject": "did:plc:followed",
"createdAt": "2026-07-01T12:00:00Z" }
}),
)
.await;
assert_eq!(r2.status().as_u16(), 200);
}
+443
View File
@@ -0,0 +1,443 @@
//! Integration tests for embed capture + thread hydration.
//!
//! These exercise the AppView's `embed` storage and the new
//! `/api/post/{uri}` thread-hydration endpoint end-to-end:
//!
//! - `timeline_includes_embed` — seed a post with an image embed, query
//! the home timeline, verify the embed came back as raw JSON.
//! - `timeline_includes_external_embed` — same but with a link card.
//! - `post_endpoint_returns_thread` — seed 3 posts (root + reply + reply
//! to reply), fetch the middle one's URI, verify the thread
//! hydration returns the right parent + root rows.
//!
//! Like the sibling API tests these are fail-open: if the AppView
//! service isn't running on the expected port the test prints a notice
//! and returns rather than panicking. The point of the tests is to
//! catch regressions in CI where the service IS up.
use serde_json::{json, Value};
use std::time::Duration;
const APPVIEW_URL: &str = "http://127.0.0.1:2584";
async fn client() -> reqwest::Client {
reqwest::Client::builder()
.timeout(Duration::from_secs(5))
.build()
.unwrap()
}
async fn wait_for_appview_db() -> bool {
let c = client().await;
for _ in 0..20 {
if let Ok(r) = c.get(format!("{APPVIEW_URL}/healthz")).send().await {
if r.status().is_success() {
return true;
}
}
tokio::time::sleep(Duration::from_millis(250)).await;
}
false
}
async fn db_reachable() -> bool {
let Some(url) = std::env::var("DATABASE_URL_APPVIEW").ok() else {
return false;
};
matches!(
tokio::time::timeout(Duration::from_secs(2), sqlx::PgPool::connect(&url)).await,
Ok(Ok(_))
)
}
async fn post_ingest(c: &reqwest::Client, body: Value) -> reqwest::Response {
c.post(format!("{APPVIEW_URL}/internal/ingest-commit"))
.json(&body)
.send()
.await
.unwrap()
}
fn did_for_test(prefix: &str) -> String {
format!(
"did:plc:emb_{}_{}",
prefix,
uuid::Uuid::new_v4().simple()
)
}
fn rkey() -> String {
uuid::Uuid::new_v4().simple().to_string()
}
/// Seed a single post with the given record payload and return its URI.
async fn seed_post(c: &reqwest::Client, did: &str, record: Value) -> String {
let rk = rkey();
let uri = format!("at://{did}/app.twi.post/{rk}");
let resp = post_ingest(
c,
json!({
"did": did,
"collection": "app.twi.post",
"action": "create",
"rkey": rk,
"cid": "bafyreicid",
"record": record,
}),
)
.await;
assert_eq!(resp.status().as_u16(), 200, "ingest failed: {record}");
uri
}
/// Seed a post whose parent/root URIs are given explicitly. Used by
/// the thread test to build a 3-deep chain (root → reply → reply).
async fn seed_reply(
c: &reqwest::Client,
did: &str,
text: &str,
parent_uri: &str,
root_uri: &str,
) -> String {
seed_post(
c,
did,
json!({
"text": text,
"createdAt": "2026-07-01T12:00:00Z",
"reply": {
"parent": {"uri": parent_uri, "cid": "cp"},
"root": {"uri": root_uri, "cid": "cr"}
}
}),
)
.await
}
#[tokio::test]
async fn timeline_includes_embed() {
if !wait_for_appview_db().await {
eprintln!("appview not running, skipping");
return;
}
if !db_reachable().await {
eprintln!("appview DB unreachable, skipping");
return;
}
let c = client().await;
let did = did_for_test("img");
let uri = seed_post(
&c,
&did,
json!({
"text": "look at this image",
"createdAt": "2026-07-01T12:00:00Z",
"embed": {
"$type": "app.bsky.embed.images",
"images": [
{
"alt": "a sunset over mountains",
"image": {
"$type": "blob",
"ref": {"$link": "bafyreimgres1"},
"mimeType": "image/jpeg",
"size": 12345
},
"aspectRatio": {"width": 1200, "height": 800}
},
{
"alt": "second image",
"image": {
"$type": "blob",
"ref": {"$link": "bafyreimgres2"},
"mimeType": "image/jpeg",
"size": 6789
}
}
]
}
}),
)
.await;
let resp = c
.get(format!("{APPVIEW_URL}/api/timeline/home"))
.query(&[("did", did.as_str()), ("limit", "10")])
.send()
.await
.unwrap();
assert_eq!(resp.status().as_u16(), 200);
let body: Value = resp.json().await.unwrap();
let posts = body["posts"].as_array().unwrap();
let our = posts
.iter()
.find(|p| p["uri"] == json!(uri))
.expect("seeded post missing from timeline");
let embed = our
.get("embed")
.expect("embed field missing from PostRow");
assert!(!embed.is_null(), "embed must not be null for image post");
assert_eq!(embed["$type"], "app.bsky.embed.images");
let imgs = embed["images"].as_array().expect("images array");
assert_eq!(imgs.len(), 2);
assert_eq!(imgs[0]["alt"], "a sunset over mountains");
assert_eq!(imgs[0]["image"]["ref"]["$link"], "bafyreimgres1");
assert_eq!(imgs[0]["aspectRatio"]["width"], 1200);
assert_eq!(imgs[1]["alt"], "second image");
}
#[tokio::test]
async fn timeline_includes_external_embed() {
if !wait_for_appview_db().await {
eprintln!("appview not running, skipping");
return;
}
if !db_reachable().await {
eprintln!("appview DB unreachable, skipping");
return;
}
let c = client().await;
let did = did_for_test("ext");
let uri = seed_post(
&c,
&did,
json!({
"text": "see link",
"createdAt": "2026-07-01T12:00:00Z",
"embed": {
"$type": "app.bsky.embed.external",
"external": {
"uri": "https://example.com/article",
"title": "An interesting article",
"description": "A short description of the linked page.",
"thumb": {
"$type": "blob",
"ref": {"$link": "bafyreithumb"}
}
}
}
}),
)
.await;
let resp = c
.get(format!("{APPVIEW_URL}/api/timeline/home"))
.query(&[("did", did.as_str()), ("limit", "10")])
.send()
.await
.unwrap();
assert_eq!(resp.status().as_u16(), 200);
let body: Value = resp.json().await.unwrap();
let posts = body["posts"].as_array().unwrap();
// The ingest endpoint commits asynchronously; the timeline may
// not yet contain the row on the first poll. Retry briefly with
// a 50ms back-off so we don't flake on busy CI.
let mut our = posts.iter().find(|p| p["uri"] == json!(uri)).cloned();
for _ in 0..10 {
if our.is_some() {
break;
}
tokio::time::sleep(Duration::from_millis(50)).await;
let resp = c
.get(format!("{APPVIEW_URL}/api/timeline/home"))
.query(&[("did", did.as_str()), ("limit", "10")])
.send()
.await
.unwrap();
let body: Value = resp.json().await.unwrap();
our = body["posts"]
.as_array()
.unwrap()
.iter()
.find(|p| p["uri"] == json!(uri))
.cloned();
}
let our = our.expect("seeded post missing from timeline");
let embed = our["embed"].as_object().expect("embed object");
assert_eq!(embed["$type"], "app.bsky.embed.external");
assert_eq!(embed["external"]["uri"], "https://example.com/article");
assert_eq!(embed["external"]["title"], "An interesting article");
}
#[tokio::test]
async fn post_endpoint_returns_thread() {
if !wait_for_appview_db().await {
eprintln!("appview not running, skipping");
return;
}
if !db_reachable().await {
eprintln!("appview DB unreachable, skipping");
return;
}
let c = client().await;
let alice = did_for_test("thread_alice");
let bob = did_for_test("thread_bob");
let carol = did_for_test("thread_carol");
// Build the chain: root (alice) → reply (bob) → reply to reply (carol).
let root_uri = seed_post(
&c,
&alice,
json!({
"text": "alice's root post",
"createdAt": "2026-07-01T12:00:00Z"
}),
)
.await;
let reply1_uri = seed_reply(
&c,
&bob,
"bob's reply to alice",
&root_uri,
&root_uri,
)
.await;
let reply2_uri = seed_reply(
&c,
&carol,
"carol's reply to bob",
&reply1_uri,
&root_uri,
)
.await;
// Fetch carol's post and verify the thread hydration returns both
// bob's reply (parent) and alice's root (root).
let resp = c
.get(format!("{APPVIEW_URL}/api/post/{reply2_uri}"))
.send()
.await
.unwrap();
assert_eq!(resp.status().as_u16(), 200);
let body: Value = resp.json().await.unwrap();
assert_eq!(body["post"]["uri"], json!(reply2_uri));
assert_eq!(
body["post"]["text"],
json!("carol's reply to bob")
);
let parent = &body["thread"]["parent"];
let root = &body["thread"]["root"];
assert_eq!(parent["uri"], json!(reply1_uri));
assert_eq!(parent["text"], json!("bob's reply to alice"));
assert_eq!(root["uri"], json!(root_uri));
assert_eq!(root["text"], json!("alice's root post"));
// Reply → reply case: carol's `parent_uri` is bob's, `root_uri` is
// alice's, and they must differ — so the root field must NOT be
// collapsed into the parent field.
assert_ne!(
parent["uri"], root["uri"],
"root and parent must be distinct rows for a 2-deep reply chain"
);
}
#[tokio::test]
async fn post_endpoint_single_post_thread_self_referential() {
if !wait_for_appview_db().await {
eprintln!("appview not running, skipping");
return;
}
if !db_reachable().await {
eprintln!("appview DB unreachable, skipping");
return;
}
let c = client().await;
let did = did_for_test("self");
let uri = seed_post(
&c,
&did,
json!({
"text": "standalone post, no parent",
"createdAt": "2026-07-01T12:00:00Z"
}),
)
.await;
let resp = c
.get(format!("{APPVIEW_URL}/api/post/{uri}"))
.send()
.await
.unwrap();
assert_eq!(resp.status().as_u16(), 200);
let body: Value = resp.json().await.unwrap();
assert_eq!(body["post"]["uri"], json!(uri));
assert!(
body["thread"]["parent"].is_null(),
"post with no parent must have null parent"
);
assert!(
body["thread"]["root"].is_null(),
"post with no parent must have null root"
);
}
#[tokio::test]
async fn post_endpoint_unknown_uri_returns_null_post() {
if !wait_for_appview_db().await {
eprintln!("appview not running, skipping");
return;
}
let c = client().await;
let bogus = format!(
"at://did:plc:nope-{}/app.twi.post/nope-{}",
uuid::Uuid::new_v4().simple(),
uuid::Uuid::new_v4().simple()
);
let resp = c
.get(format!("{APPVIEW_URL}/api/post/{bogus}"))
.send()
.await
.unwrap();
assert_eq!(resp.status().as_u16(), 200);
let body: Value = resp.json().await.unwrap();
assert!(body["post"].is_null());
assert!(body["thread"]["parent"].is_null());
assert!(body["thread"]["root"].is_null());
}
#[tokio::test]
async fn timeline_post_without_embed_has_null_embed() {
if !wait_for_appview_db().await {
eprintln!("appview not running, skipping");
return;
}
if !db_reachable().await {
eprintln!("appview DB unreachable, skipping");
return;
}
let c = client().await;
let did = did_for_test("plain");
let uri = seed_post(
&c,
&did,
json!({
"text": "plain text only",
"createdAt": "2026-07-01T12:00:00Z"
}),
)
.await;
let resp = c
.get(format!("{APPVIEW_URL}/api/timeline/home"))
.query(&[("did", did.as_str()), ("limit", "10")])
.send()
.await
.unwrap();
assert_eq!(resp.status().as_u16(), 200);
let body: Value = resp.json().await.unwrap();
let our = body["posts"]
.as_array()
.unwrap()
.iter()
.find(|p| p["uri"] == json!(uri))
.expect("plain post missing");
assert!(
our["embed"].is_null(),
"plain text post must have null embed, got: {}",
our["embed"]
);
}
@@ -0,0 +1,461 @@
//! Integration tests for `HandleSyncWorker::run_once()`.
//!
//! These exercise the worker's SQL against a live appview DB. The
//! resolver is substituted for a stub so the tests do not depend on
//! `plc.directory` being reachable (and so we can deterministically
//! prove the "don't overwrite" race protection works).
//!
//! Like the sibling `api_integration.rs`, every test is fail-open: if
//! `DATABASE_URL_APPVIEW` is unset or the DB isn't reachable, the test
//! prints a notice and returns. This keeps `cargo test --workspace`
//! green in environments without the appview stack running.
use anyhow::Result;
use async_trait::async_trait;
use at_identity::DidHandleResolver;
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use std::time::Duration;
use tokio::time::timeout;
use uuid::Uuid;
use appview::handle_sync::{HandleSyncWorker, SyncReport};
/// In-process test double for the PLC client. We never want these
/// tests to talk to the real PLC.
#[derive(Default)]
struct StubResolver {
/// DID → resolved handle (or `None` for an unresolvable DID).
/// `Some("")` is treated as "no result" by the worker.
mapping: Mutex<HashMap<String, Option<String>>>,
/// How many times each DID was queried — used by the limit test.
queries: Mutex<Vec<String>>,
}
impl StubResolver {
fn new(map: HashMap<String, Option<String>>) -> Self {
Self {
mapping: Mutex::new(map),
queries: Mutex::new(Vec::new()),
}
}
fn into_arc(self) -> Arc<StubResolver> {
Arc::new(self)
}
fn query_count(&self, did: &str) -> usize {
self.queries
.lock()
.unwrap()
.iter()
.filter(|d| d.as_str() == did)
.count()
}
}
#[async_trait]
impl DidHandleResolver for StubResolver {
async fn resolve_handle(&self, did: &str) -> Result<Option<String>> {
self.queries.lock().unwrap().push(did.to_string());
// Snapshot the mapping out so the worker sees a consistent view
// even if another writer fiddles mid-call.
let m = self.mapping.lock().unwrap();
// None → unknown; Some("") → unknown; Some("h") → resolved.
match m.get(did) {
Some(Some(h)) if !h.is_empty() => Ok(Some(h.clone())),
_ => Ok(None),
}
}
}
/// Build a worker whose PLC and web resolvers are both the same stub.
/// The integration tests in this file don't care which method the
/// DID uses — the stub answers for any prefix.
fn worker_with(db: sqlx::PgPool, stub: Arc<StubResolver>) -> HandleSyncWorker {
let r: Arc<dyn DidHandleResolver> = stub;
HandleSyncWorker {
db,
plc_resolver: Arc::clone(&r),
web_resolver: Arc::clone(&r),
interval_secs: 999,
}
}
async fn try_test_db() -> Option<sqlx::PgPool> {
let url = std::env::var("DATABASE_URL_APPVIEW").ok()?;
match timeout(Duration::from_secs(2), sqlx::PgPool::connect(&url)).await {
Ok(Ok(pool)) => match sqlx::migrate!("../../migrations/appview")
.run(&pool)
.await
{
Ok(()) => Some(pool),
Err(_) => None,
},
_ => None,
}
}
fn unique_did(prefix: &str) -> String {
format!("did:plc:hsync_{}_{}", prefix, Uuid::new_v4().simple())
}
async fn seed_post(
db: &sqlx::PgPool,
did: &str,
rkey: &str,
handle: &str,
text: &str,
) -> Result<()> {
let uri = format!("at://{did}/app.twi.post/{rkey}");
sqlx::query(
r#"INSERT INTO posts
(uri, did, handle, rkey, collection, text, cid,
parent_uri, root_uri, langs, created_at)
VALUES ($1,$2,$3,$4,'app.twi.post',$5,'bafy',NULL,NULL,NULL, now())
ON CONFLICT (uri) DO NOTHING"#,
)
.bind(&uri)
.bind(did)
.bind(handle)
.bind(rkey)
.bind(text)
.execute(db)
.await?;
Ok(())
}
async fn fetch_handle(
db: &sqlx::PgPool,
did: &str,
) -> Result<Option<String>> {
let row: Option<(String,)> = sqlx::query_as(
"SELECT handle FROM posts WHERE did = $1 \
ORDER BY indexed_at DESC LIMIT 1",
)
.bind(did)
.fetch_optional(db)
.await?;
Ok(row.and_then(|(s,)| if s.is_empty() { None } else { Some(s) }))
}
async fn count_empty_handle_for(db: &sqlx::PgPool, did: &str) -> Result<i64> {
let (n,): (i64,) = sqlx::query_as(
"SELECT COUNT(*) FROM posts WHERE did = $1 AND handle = ''",
)
.bind(did)
.fetch_one(db)
.await?;
Ok(n)
}
/// Seed two posts for one DID with empty handles, point the stub
/// resolver at a known handle, and assert the worker fills both rows.
#[tokio::test]
async fn sync_resolves_known_did() {
let Some(db) = try_test_db().await else {
eprintln!("appview DB unavailable; skipping");
return;
};
let did = unique_did("known");
let _ = sqlx::query("DELETE FROM posts WHERE did = $1")
.bind(&did)
.execute(&db)
.await
.unwrap();
let expected = format!("known.{}", Uuid::new_v4().simple());
let stub = StubResolver::new(HashMap::from([(
did.clone(),
Some(expected.clone()),
)]))
.into_arc();
// Two posts → two rows must be updated.
seed_post(&db, &did, "rka", "", "first").await.unwrap();
seed_post(&db, &did, "rkb", "", "second").await.unwrap();
assert_eq!(count_empty_handle_for(&db, &did).await.unwrap(), 2);
let worker = worker_with(db.clone(), stub.clone());
let report: SyncReport = worker.run_once().await.unwrap();
assert_eq!(report.resolved, 2, "{report:?}");
assert_eq!(report.failed, 0);
assert_eq!(report.skipped, 0);
// No empty-handle rows remain for this DID and the handle matches.
assert_eq!(count_empty_handle_for(&db, &did).await.unwrap(), 0);
let got = fetch_handle(&db, &did).await.unwrap();
assert_eq!(got.as_deref(), Some(expected.as_str()));
// Resolver was consulted exactly once for this DID.
assert_eq!(stub.query_count(&did), 1);
// Cleanup.
let _ = sqlx::query("DELETE FROM posts WHERE did = $1")
.bind(&did)
.execute(&db)
.await;
}
/// A DID whose posts already carry a handle must NOT be re-queried
/// or overwritten — the worker's `SELECT … WHERE handle = ''` filters
/// it out entirely.
#[tokio::test]
async fn sync_skips_already_resolved() {
let Some(db) = try_test_db().await else {
eprintln!("appview DB unavailable; skipping");
return;
};
let did = unique_did("already");
let _ = sqlx::query("DELETE FROM posts WHERE did = $1")
.bind(&did)
.execute(&db)
.await
.unwrap();
let pre = "preset.handle".to_string();
seed_post(&db, &did, "rkA", &pre, "alpha").await.unwrap();
seed_post(&db, &did, "rkB", &pre, "beta").await.unwrap();
// The stub would overwrite with a different handle if asked.
let stub = StubResolver::new(HashMap::from([(
did.clone(),
Some("wrong.handle".into()),
)]))
.into_arc();
let worker = worker_with(db.clone(), stub.clone());
let report = worker.run_once().await.unwrap();
assert_eq!(report.resolved, 0, "{report:?}");
assert_eq!(report.failed, 0);
// Both rows must still carry the pre-existing handle.
let (cnt,): (i64,) = sqlx::query_as(
"SELECT COUNT(*) FROM posts WHERE did = $1 AND handle = $2",
)
.bind(&did)
.bind(&pre)
.fetch_one(&db)
.await
.unwrap();
assert_eq!(cnt, 2);
// Resolver was NOT consulted for this DID.
assert_eq!(stub.query_count(&did), 0);
let _ = sqlx::query("DELETE FROM posts WHERE did = $1")
.bind(&did)
.execute(&db)
.await;
}
/// Seed more than `BATCH_SIZE` distinct empty-handle DIDs and verify
/// only the first batch is processed this pass. The leftover DIDs
/// remain empty (will be picked up next pass).
#[tokio::test]
async fn sync_respects_limit() {
use appview::handle_sync::BATCH_SIZE;
let Some(db) = try_test_db().await else {
eprintln!("appview DB unavailable; skipping");
return;
};
let prefix = unique_did("limit");
// Seed BATCH_SIZE + 5 distinct DIDs, each with one empty-handle post.
let total = BATCH_SIZE as usize + 5;
let mut all_dids = Vec::with_capacity(total);
for i in 0..total {
let did = format!("{prefix}_{i}");
seed_post(&db, &did, "rk", "", "x").await.unwrap();
all_dids.push(did);
}
let mut mapping = HashMap::new();
for did in &all_dids {
mapping.insert(
did.clone(),
Some(format!("resolved.{}", &did[did.len() - 6..])),
);
}
let stub = StubResolver::new(mapping).into_arc();
let worker = worker_with(db.clone(), stub.clone());
let report = worker.run_once().await.unwrap();
assert_eq!(
report.resolved as i64,
BATCH_SIZE,
"expected exactly BATCH_SIZE rows resolved, got {report:?}"
);
assert_eq!(report.failed, 0);
assert_eq!(report.skipped, 0);
// Exactly 5 empty-handle posts remain (the capped overflow).
let remaining: i64 = sqlx::query_scalar(
"SELECT COUNT(*) FROM posts WHERE did LIKE $1 AND handle = ''",
)
.bind(format!("{prefix}_%"))
.fetch_one(&db)
.await
.unwrap();
assert_eq!(remaining, 5, "expected 5 unresolved rows left");
// The stub resolver was consulted for exactly BATCH_SIZE DIDs.
// (Note: the worker can't know which 5 were left out — the query
// count is process-wide; we count the total below.)
let total_qs = {
let guard = stub.queries.lock().unwrap();
guard.len()
};
assert_eq!(
total_qs as i64,
BATCH_SIZE,
"resolver must be called at most BATCH_SIZE times, got {total_qs}"
);
// Cleanup so repeated runs stay hygienic.
let _ = sqlx::query("DELETE FROM posts WHERE did LIKE $1")
.bind(format!("{prefix}_%"))
.execute(&db)
.await;
}
/// Unresolvable DIDs (stub returns `Ok(None)`) count as `skipped`,
/// not `failed`, so a temporary PLC outage doesn't poison
/// observability dashboards.
#[tokio::test]
async fn sync_skips_unresolvable_dids() {
let Some(db) = try_test_db().await else {
eprintln!("appview DB unavailable; skipping");
return;
};
let did = unique_did("unres");
let _ = sqlx::query("DELETE FROM posts WHERE did = $1")
.bind(&did)
.execute(&db)
.await
.unwrap();
seed_post(&db, &did, "rk", "", "").await.unwrap();
// DID deliberately absent from the stub's mapping → Ok(None).
let stub = StubResolver::new(HashMap::new()).into_arc();
let worker = worker_with(db.clone(), stub.clone());
let report = worker.run_once().await.unwrap();
assert_eq!(report.resolved, 0);
assert_eq!(report.failed, 0);
assert_eq!(report.skipped, 1, "{report:?}");
assert_eq!(
count_empty_handle_for(&db, &did).await.unwrap(),
1,
"post must remain empty until resolver succeeds"
);
let _ = sqlx::query("DELETE FROM posts WHERE did = $1")
.bind(&did)
.execute(&db)
.await;
}
/// End-to-end test for the `did:web:` dispatch path: a `did:web:`
/// DID with an empty post handle must be routed to the **web**
/// resolver (not the PLC one) and the post handle must be updated
/// from the web resolver's answer.
#[tokio::test]
async fn sync_resolves_did_web_via_web_resolver() {
let Some(db) = try_test_db().await else {
eprintln!("appview DB unavailable; skipping");
return;
};
let did = format!("did:web:example.com:user:{}", Uuid::new_v4().simple());
let _ = sqlx::query("DELETE FROM posts WHERE did = $1")
.bind(&did)
.execute(&db)
.await
.unwrap();
seed_post(&db, &did, "rk", "", "web post").await.unwrap();
// Two stubs that disagree. The dispatcher MUST pick the web one
// for a did:web DID — choosing the PLC one would write the wrong
// handle.
let expected = format!("web-handle.{}", Uuid::new_v4().simple());
let plc = StubResolver::new(HashMap::from([(
did.clone(),
Some("WRONG-PLC-HANDLE".into()),
)]));
let web = StubResolver::new(HashMap::from([(
did.clone(),
Some(expected.clone()),
)]));
let plc_arc: Arc<dyn DidHandleResolver> = plc.into_arc();
let web_arc: Arc<dyn DidHandleResolver> = web.into_arc();
let worker = HandleSyncWorker {
db: db.clone(),
plc_resolver: plc_arc,
web_resolver: web_arc,
interval_secs: 999,
};
let report = worker.run_once().await.unwrap();
assert_eq!(
report.resolved, 1,
"did:web must resolve through the web resolver, got {report:?}"
);
assert_eq!(report.failed, 0);
let h = fetch_handle(&db, &did).await.unwrap();
assert_eq!(h.as_deref(), Some(expected.as_str()));
let _ = sqlx::query("DELETE FROM posts WHERE did = $1")
.bind(&did)
.execute(&db)
.await;
}
/// `did:plc:` DIDs must still flow through the PLC resolver — the
/// web resolver must NOT be consulted (which would otherwise issue
/// a `https://plc.directory/.../did.json` request and fail).
#[tokio::test]
async fn sync_resolves_did_plc_via_plc_resolver() {
let Some(db) = try_test_db().await else {
eprintln!("appview DB unavailable; skipping");
return;
};
let did = unique_did("plcpath");
let _ = sqlx::query("DELETE FROM posts WHERE did = $1")
.bind(&did)
.execute(&db)
.await
.unwrap();
seed_post(&db, &did, "rk", "", "plc post").await.unwrap();
let expected = format!("plc-handle.{}", Uuid::new_v4().simple());
let plc = StubResolver::new(HashMap::from([(
did.clone(),
Some(expected.clone()),
)]));
// The web stub deliberately holds the wrong handle. If dispatch
// wrongly routed a did:plc DID to the web resolver, the row would
// end up with "WRONG-WEB-HANDLE".
let web = StubResolver::new(HashMap::from([(
did.clone(),
Some("WRONG-WEB-HANDLE".into()),
)]));
let plc_arc: Arc<dyn DidHandleResolver> = plc.into_arc();
let web_arc: Arc<dyn DidHandleResolver> = web.into_arc();
let worker = HandleSyncWorker {
db: db.clone(),
plc_resolver: plc_arc,
web_resolver: web_arc,
interval_secs: 999,
};
let report = worker.run_once().await.unwrap();
assert_eq!(
report.resolved, 1,
"did:plc must resolve through the PLC resolver, got {report:?}"
);
let h = fetch_handle(&db, &did).await.unwrap();
assert_eq!(h.as_deref(), Some(expected.as_str()));
let _ = sqlx::query("DELETE FROM posts WHERE did = $1")
.bind(&did)
.execute(&db)
.await;
}
+370
View File
@@ -0,0 +1,370 @@
//! Integration tests for the `/api/post/{uri}` engagement counts.
//!
//! Like the sibling `embeds_integration.rs` these are fail-open
//! against a live AppView: the tests `eprintln!` and skip if the
//! service isn't running on the expected port or the DB is
//! unreachable.
//!
//! Tests:
//!
//! - `post_endpoint_returns_like_counts` — seed a like via
//! `/internal/ingest-commit`, fetch the post endpoint, verify
//! the `like_count` is 1. Seed a repost, verify the
//! `repost_count` is 1.
use serde_json::{json, Value};
use std::time::Duration;
const APPVIEW_URL: &str = "http://127.0.0.1:2584";
async fn client() -> reqwest::Client {
reqwest::Client::builder()
.timeout(Duration::from_secs(5))
.build()
.unwrap()
}
async fn wait_for_appview_db() -> bool {
let c = client().await;
for _ in 0..20 {
if let Ok(r) = c.get(format!("{APPVIEW_URL}/healthz")).send().await {
if r.status().is_success() {
return true;
}
}
tokio::time::sleep(Duration::from_millis(250)).await;
}
false
}
async fn db_reachable() -> bool {
let Some(url) = std::env::var("DATABASE_URL_APPVIEW").ok() else {
return false;
};
matches!(
tokio::time::timeout(Duration::from_secs(2), sqlx::PgPool::connect(&url)).await,
Ok(Ok(_))
)
}
async fn post_ingest(c: &reqwest::Client, body: Value) -> reqwest::Response {
c.post(format!("{APPVIEW_URL}/internal/ingest-commit"))
.json(&body)
.send()
.await
.unwrap()
}
fn did_for_test(prefix: &str) -> String {
format!(
"did:plc:likes_{}_{}",
prefix,
uuid::Uuid::new_v4().simple()
)
}
fn rkey() -> String {
uuid::Uuid::new_v4().simple().to_string()
}
async fn seed_post(c: &reqwest::Client, did: &str, text: &str) -> String {
let rk = rkey();
let uri = format!("at://{did}/app.twi.post/{rk}");
let resp = post_ingest(
c,
json!({
"did": did,
"collection": "app.twi.post",
"action": "create",
"rkey": rk,
"cid": "bafyreicid",
"record": {
"text": text,
"createdAt": "2026-07-01T12:00:00Z",
},
}),
)
.await;
assert_eq!(resp.status().as_u16(), 200, "post ingest failed");
uri
}
async fn seed_like(
c: &reqwest::Client,
liker_did: &str,
subject_uri: &str,
subject_cid: &str,
) -> String {
let rk = rkey();
let like_uri = format!("at://{liker_did}/app.bsky.feed.like/{rk}");
let resp = post_ingest(
c,
json!({
"did": liker_did,
"collection": "app.bsky.feed.like",
"action": "create",
"rkey": rk,
"cid": "bafyreilike",
"record": {
"subject": {
"uri": subject_uri,
"cid": subject_cid,
},
"createdAt": "2026-07-01T12:00:00Z",
},
}),
)
.await;
assert_eq!(resp.status().as_u16(), 200, "like ingest failed");
like_uri
}
async fn seed_repost(
c: &reqwest::Client,
reposter_did: &str,
subject_uri: &str,
subject_cid: &str,
) -> String {
let rk = rkey();
let repost_uri = format!("at://{reposter_did}/app.bsky.feed.repost/{rk}");
let resp = post_ingest(
c,
json!({
"did": reposter_did,
"collection": "app.bsky.feed.repost",
"action": "create",
"rkey": rk,
"cid": "bafyreirepost",
"record": {
"subject": {
"uri": subject_uri,
"cid": subject_cid,
},
"createdAt": "2026-07-01T12:00:00Z",
},
}),
)
.await;
assert_eq!(resp.status().as_u16(), 200, "repost ingest failed");
repost_uri
}
/// Poll the post endpoint a few times so we don't flake on
/// ingestion latency. The ingest-commit handler is async, so
/// counts may not be visible on the first request.
async fn post_endpoint_with_counts(
c: &reqwest::Client,
uri: &str,
) -> Option<Value> {
for _ in 0..10 {
let resp = c
.get(format!("{APPVIEW_URL}/api/post/{uri}"))
.send()
.await
.ok()?;
if !resp.status().is_success() {
tokio::time::sleep(Duration::from_millis(50)).await;
continue;
}
let body: Value = resp.json().await.ok()?;
if body.get("like_count").is_some() || body.get("repost_count").is_some() {
return Some(body);
}
tokio::time::sleep(Duration::from_millis(50)).await;
}
None
}
#[tokio::test]
async fn post_endpoint_returns_like_counts() {
if !wait_for_appview_db().await {
eprintln!("appview not running, skipping");
return;
}
if !db_reachable().await {
eprintln!("appview DB unreachable, skipping");
return;
}
let c = client().await;
let author = did_for_test("author");
let liker = did_for_test("liker");
let reposter = did_for_test("reposter");
// The post endpoint needs the post itself in the `posts` table
// to return a non-null `post` and the engagement counts. The
// embed for the post is fine to be null.
let post_uri = seed_post(&c, &author, "a post that will get engagement").await;
let post_cid = "bafyreicid";
// No likes / reposts yet — counts must be 0.
let initial = post_endpoint_with_counts(&c, &post_uri)
.await
.expect("post endpoint never resolved");
assert_eq!(
initial["post"]["uri"],
json!(post_uri),
"endpoint should return our seeded post"
);
assert_eq!(initial["like_count"], json!(0), "initial like_count");
assert_eq!(initial["repost_count"], json!(0), "initial repost_count");
// Seed one like and one repost from different DIDs.
let _ = seed_like(&c, &liker, &post_uri, post_cid).await;
let _ = seed_repost(&c, &reposter, &post_uri, post_cid).await;
let body = post_endpoint_with_counts(&c, &post_uri)
.await
.expect("post endpoint never resolved after engagement");
assert_eq!(
body["like_count"],
json!(1),
"like_count should be 1 after one like ingest: {body:?}"
);
assert_eq!(
body["repost_count"],
json!(1),
"repost_count should be 1 after one repost ingest: {body:?}"
);
}
#[tokio::test]
async fn post_endpoint_missing_post_returns_null_counts() {
if !wait_for_appview_db().await {
eprintln!("appview not running, skipping");
return;
}
let c = client().await;
let bogus = format!(
"at://did:plc:nope_lc_{}/app.twi.post/nope_{}",
uuid::Uuid::new_v4().simple(),
uuid::Uuid::new_v4().simple()
);
let resp = c
.get(format!("{APPVIEW_URL}/api/post/{bogus}"))
.send()
.await
.unwrap();
assert_eq!(resp.status().as_u16(), 200);
let body: Value = resp.json().await.unwrap();
assert!(body["post"].is_null(), "missing post should be null");
// Counts are skipped when the post isn't found — we shouldn't
// pay the `COUNT(*)` cost on the "not in index" path.
assert!(
body.get("like_count").is_none() || body["like_count"].is_null(),
"like_count should be absent for missing post, got: {body:?}"
);
assert!(
body.get("repost_count").is_none() || body["repost_count"].is_null(),
"repost_count should be absent for missing post, got: {body:?}"
);
}
/// Phase 5b review H4 — `viewer_liked` / `viewer_reposted` must reach
/// the UI so it can highlight the engagement buttons. Without this
/// the Tauri client can show the counts but never knows whether the
/// user has already liked/reposted the post, so the "liked" state
/// doesn't persist visually across reloads.
#[tokio::test]
async fn post_endpoint_with_viewer_did_returns_liked_state() {
if !wait_for_appview_db().await {
eprintln!("appview not running, skipping");
return;
}
if !db_reachable().await {
eprintln!("appview DB unreachable, skipping");
return;
}
let c = client().await;
let author = did_for_test("vl_author");
let liker = did_for_test("vl_liker");
let reposter = did_for_test("vl_reposter");
let outsider = did_for_test("vl_outsider");
let post_uri = seed_post(&c, &author, "post that some viewers like").await;
let post_cid = "bafyreicid";
// Seed a like from `liker` and a repost from `reposter`.
let _ = seed_like(&c, &liker, &post_uri, post_cid).await;
let _ = seed_repost(&c, &reposter, &post_uri, post_cid).await;
// Poll the endpoint with `viewer_did=liker` and verify
// `viewer_liked = true` and `viewer_reposted = false` (liker did
// not repost).
let mut body_liker: Option<Value> = None;
for _ in 0..20 {
let resp = c
.get(format!(
"{APPVIEW_URL}/api/post/{post_uri}"
))
.query(&[("viewer_did", liker.as_str())])
.send()
.await
.unwrap();
assert_eq!(resp.status().as_u16(), 200);
let body: Value = resp.json().await.unwrap();
if body.get("viewer_liked").is_some() {
body_liker = Some(body);
break;
}
tokio::time::sleep(Duration::from_millis(50)).await;
}
let body_liker = body_liker.expect("viewer_liked never appeared");
assert_eq!(
body_liker["viewer_liked"],
json!(true),
"viewer_liker should see viewer_liked=true: {body_liker:?}"
);
assert_eq!(
body_liker["viewer_reposted"],
json!(false),
"viewer_liker did not repost: {body_liker:?}"
);
assert_eq!(body_liker["like_count"], json!(1));
assert_eq!(body_liker["repost_count"], json!(1));
// Now query with `viewer_did=reposter`: opposite state.
let body_reposter: Value = c
.get(format!("{APPVIEW_URL}/api/post/{post_uri}"))
.query(&[("viewer_did", reposter.as_str())])
.send()
.await
.unwrap()
.json()
.await
.unwrap();
assert_eq!(body_reposter["viewer_liked"], json!(false));
assert_eq!(body_reposter["viewer_reposted"], json!(true));
// And `viewer_did=outsider` (no engagement) → both false.
let body_outsider: Value = c
.get(format!("{APPVIEW_URL}/api/post/{post_uri}"))
.query(&[("viewer_did", outsider.as_str())])
.send()
.await
.unwrap()
.json()
.await
.unwrap();
assert_eq!(body_outsider["viewer_liked"], json!(false));
assert_eq!(body_outsider["viewer_reposted"], json!(false));
// Without `viewer_did`, the booleans should be absent (the client
// renders "unknown" state). The counts still come back so the UI
// can show "1 like".
let body_anonymous: Value = c
.get(format!("{APPVIEW_URL}/api/post/{post_uri}"))
.send()
.await
.unwrap()
.json()
.await
.unwrap();
assert!(
body_anonymous.get("viewer_liked").is_none(),
"viewer_liked should be absent without viewer_did: {body_anonymous:?}"
);
assert!(
body_anonymous.get("viewer_reposted").is_none(),
"viewer_reposted should be absent without viewer_did: {body_anonymous:?}"
);
}