A cleared edge is not a circle, a finished one still is
Browse files at this commit2cfc56134284f4d9f05a5b3443eced604504ec1aParent: 293a3a0- fix
- a cleared edge is not a circle, a finished one still is
4 changed files +112 -7
- Cargo.lock +1 -0
- crates/dizey-core/Cargo.toml +1 -0
- crates/dizey-core/src/store/turso_store.rs +7 -4
- crates/dizey-core/tests/store.rs +103 -3
Cargo.lockmodified+1 -0
| @@ -963,6 +963,7 @@ dependencies = [ | ||
| 963 | 963 | "leptos_meta", |
| 964 | 964 | "leptos_router", |
| 965 | 965 | "serde", |
| 966 | + "serde_json", | |
| 966 | 967 | "thiserror", |
| 967 | 968 | "time", |
| 968 | 969 | "tokio", |
crates/dizey-core/Cargo.tomlmodified+1 -0
| @@ -38,3 +38,4 @@ uuid = { workspace = true, optional = true } | ||
| 38 | 38 | async-trait = "0.1.92" |
| 39 | 39 | serde_json.workspace = true |
| 40 | 40 | tokio = { workspace = true } |
| 41 | +turso = { workspace = true } |
crates/dizey-core/src/store/turso_store.rsmodified+7 -4
| @@ -977,15 +977,18 @@ impl Store for TursoStore { | ||
| 977 | 977 | .map_err(backend)?; |
| 978 | 978 | |
| 979 | 979 | let written = async { |
| 980 | - // Every edge, cleared or not: a cleared edge is a link that is | |
| 981 | - // satisfied, not a link that is gone, and re-adding its other half | |
| 982 | - // would still be a circle on the screen. | |
| 980 | + // Live edges only. `cleared_at` is set by an unlink and nothing | |
| 981 | + // else, so a cleared edge is a link that was removed — walking it | |
| 982 | + // refuses a link the screen no longer shows as a circle. A link | |
| 983 | + // whose blocker is merely finished still walks: that is `done_at`, | |
| 984 | + // and the edge is still in force. | |
| 983 | 985 | let mut rows = tx |
| 984 | 986 | .query( |
| 985 | 987 | "SELECT d.blocked_task_id, d.blocking_task_id FROM task_dependency d \ |
| 986 | 988 | JOIN task b ON b.id = d.blocked_task_id \ |
| 987 | 989 | JOIN task k ON k.id = d.blocking_task_id \ |
| 988 | - WHERE b.deleted_at IS NULL AND k.deleted_at IS NULL", | |
| 990 | + WHERE d.cleared_at IS NULL \ | |
| 991 | + AND b.deleted_at IS NULL AND k.deleted_at IS NULL", | |
| 989 | 992 | (), |
| 990 | 993 | ) |
| 991 | 994 | .await?; |
crates/dizey-core/tests/store.rsmodified+103 -3
| @@ -1895,7 +1895,7 @@ async fn a_dependency_that_would_close_a_circle_is_refused() { | ||
| 1895 | 1895 | } |
| 1896 | 1896 | |
| 1897 | 1897 | #[tokio::test] |
| 1898 | -async fn a_cleared_dependency_still_counts_as_a_circle() { | |
| 1898 | +async fn a_cleared_dependency_is_not_a_circle() { | |
| 1899 | 1899 | let (scratch, workspace, admin) = workspace_with_admin().await; |
| 1900 | 1900 | let store = &scratch.store; |
| 1901 | 1901 | let now = OffsetDateTime::now_utc(); |
| @@ -1905,8 +1905,38 @@ async fn a_cleared_dependency_still_counts_as_a_circle() { | ||
| 1905 | 1905 | store.add_dependency(&a, &b, now).await.unwrap(); |
| 1906 | 1906 | store.clear_dependency(&a, &b, now).await.unwrap(); |
| 1907 | 1907 | |
| 1908 | - // A cleared edge is a link that is satisfied, not a link that is gone: the | |
| 1909 | - // pair would still draw a circle on the screen. | |
| 1908 | + // `cleared_at` is set by an unlink and nothing else, and a cleared row no | |
| 1909 | + // longer appears on the screen. Refusing the other direction would refuse a | |
| 1910 | + // circle nobody can see — and leave the pair unlinkable for good, because | |
| 1911 | + // the cleared row never goes away. A link whose blocker is merely finished | |
| 1912 | + // is a different thing: that is `done_at`, the edge is live, and it still | |
| 1913 | + // walks. | |
| 1914 | + store.add_dependency(&b, &a, now).await.unwrap(); | |
| 1915 | + | |
| 1916 | + let detail = load_detail(store, &workspace, &b).await.unwrap().unwrap(); | |
| 1917 | + assert_eq!(detail.blocked_by.len(), 1, "the new link stands"); | |
| 1918 | + let detail = load_detail(store, &workspace, &a).await.unwrap().unwrap(); | |
| 1919 | + assert!(detail.blocked_by.is_empty(), "the cleared one is gone"); | |
| 1920 | +} | |
| 1921 | + | |
| 1922 | +#[tokio::test] | |
| 1923 | +async fn a_finished_blocker_still_counts_as_a_circle() { | |
| 1924 | + let (scratch, workspace, admin) = workspace_with_admin().await; | |
| 1925 | + let store = &scratch.store; | |
| 1926 | + let now = OffsetDateTime::now_utc(); | |
| 1927 | + | |
| 1928 | + let a = add_task(store, &workspace, "Backlog", "a", None, &admin).await; | |
| 1929 | + let b = add_task(store, &workspace, "Backlog", "b", None, &admin).await; | |
| 1930 | + store.add_dependency(&a, &b, now).await.unwrap(); | |
| 1931 | + let done = column_named(store, &workspace, "Done").await; | |
| 1932 | + let backlog = column_named(store, &workspace, "Backlog").await; | |
| 1933 | + store | |
| 1934 | + .move_task(&b, &backlog, &done, &admin, now) | |
| 1935 | + .await | |
| 1936 | + .unwrap(); | |
| 1937 | + | |
| 1938 | + // b is finished, so the row reads as cleared on the screen — but the link | |
| 1939 | + // is still in force, and the other direction is still a circle. | |
| 1910 | 1940 | assert!(matches!( |
| 1911 | 1941 | store.add_dependency(&b, &a, now).await, |
| 1912 | 1942 | Err(StoreError::Cycle) |
| @@ -2365,3 +2395,73 @@ async fn a_deleted_card_does_not_move() { | ||
| 2365 | 2395 | |
| 2366 | 2396 | assert!(matches!(refused, Err(StoreError::NotFound))); |
| 2367 | 2397 | } |
| 2398 | + | |
| 2399 | +/// The schema's REFERENCES clauses are only worth anything if the engine acts | |
| 2400 | +/// on them. A drive database once held members pointing at a workspace id with | |
| 2401 | +/// no row behind it — those rows went in through the `sqlite3` CLI, which has | |
| 2402 | +/// `foreign_keys` OFF by default, but the only way to know that is to prove the | |
| 2403 | +/// store's own connections have it ON and refuse the same insert. | |
| 2404 | +#[tokio::test] | |
| 2405 | +async fn an_orphan_row_is_refused() { | |
| 2406 | + let (scratch, workspace, _admin) = workspace_with_admin().await; | |
| 2407 | + | |
| 2408 | + // Through the store's own API first: a user needs a workspace that exists. | |
| 2409 | + let refused = scratch | |
| 2410 | + .store | |
| 2411 | + .create_user(NewUser { | |
| 2412 | + workspace_id: "no-such-workspace".into(), | |
| 2413 | + email: "orphan@dizey.sh".into(), | |
| 2414 | + display_name: "Orphan".into(), | |
| 2415 | + role: Role::Member, | |
| 2416 | + }) | |
| 2417 | + .await; | |
| 2418 | + assert!( | |
| 2419 | + refused.is_err(), | |
| 2420 | + "a user in a workspace that does not exist was accepted" | |
| 2421 | + ); | |
| 2422 | + | |
| 2423 | + // And at the engine, where the pragma either fired or did not. Turso is | |
| 2424 | + // single-writer, so this gets a file of its own rather than a second handle | |
| 2425 | + // on the live one, which would fail with "database is locked" and tell us | |
| 2426 | + // nothing about foreign keys. | |
| 2427 | + let existing = scratch | |
| 2428 | + .store | |
| 2429 | + .users(&workspace) | |
| 2430 | + .await | |
| 2431 | + .expect("the workspace reads back") | |
| 2432 | + .len(); | |
| 2433 | + assert_eq!(existing, 1, "only the admin"); | |
| 2434 | + | |
| 2435 | + let dir = std::env::temp_dir().join(format!("dizey-fk-{}", Uuid::new_v4())); | |
| 2436 | + std::fs::create_dir_all(&dir).unwrap(); | |
| 2437 | + let path = dir.join("dizey.db").to_str().unwrap().to_owned(); | |
| 2438 | + { | |
| 2439 | + TursoStore::open(&path).await.unwrap(); | |
| 2440 | + } | |
| 2441 | + | |
| 2442 | + let db = turso::Builder::new_local(&path).build().await.unwrap(); | |
| 2443 | + let conn = db.connect().unwrap(); | |
| 2444 | + conn.execute("PRAGMA foreign_keys = ON", ()).await.unwrap(); | |
| 2445 | + let mut rows = conn.query("PRAGMA foreign_keys", ()).await.unwrap(); | |
| 2446 | + let on = rows.next().await.unwrap().expect("the pragma reads back"); | |
| 2447 | + assert_eq!( | |
| 2448 | + on.get::<i64>(0).unwrap(), | |
| 2449 | + 1, | |
| 2450 | + "foreign_keys did not stay ON for this connection" | |
| 2451 | + ); | |
| 2452 | + drop(rows); | |
| 2453 | + | |
| 2454 | + let refused = conn | |
| 2455 | + .execute( | |
| 2456 | + "INSERT INTO user (id, workspace_id, email, display_name, role, created_at) \ | |
| 2457 | + VALUES ('u-orphan', 'no-such-workspace', 'orphan@dizey.sh', 'Orphan', \ | |
| 2458 | + 'member', '2026-08-26T00:00:00Z')", | |
| 2459 | + (), | |
| 2460 | + ) | |
| 2461 | + .await; | |
| 2462 | + assert!( | |
| 2463 | + refused.is_err(), | |
| 2464 | + "the engine accepted a user pointing at no workspace" | |
| 2465 | + ); | |
| 2466 | + let _ = std::fs::remove_dir_all(&dir); | |
| 2467 | +} |