One task, whole — detail, cycles refused, deletion that frees
Browse files at this commitb660c8ba2e88389ffc2dd49e5ebebf65cdcee4ecParent: 40165b4- feat
- one task, whole — detail, cycles refused, deletion that frees
5 changed files +1264 -9
- crates/dizey-core/src/detail.rs +357 -0
- crates/dizey-core/src/lib.rs +4 -0
- crates/dizey-core/src/store/mod.rs +47 -1
- crates/dizey-core/src/store/turso_store.rs +504 -8
- crates/dizey-core/tests/store.rs +352 -0
crates/dizey-core/src/detail.rsadded+357 -0
| @@ -0,0 +1,357 @@ | |||
| 1 | +//! The task detail vocabulary: one task with everything hung off it — status, | ||
| 2 | +//! assignees, deadline, description, both directions of its dependencies, its | ||
| 3 | +//! comments and its activity trail. | ||
| 4 | +//! | ||
| 5 | +//! Like [`board`](crate::board), this module compiles to wasm: the browser | ||
| 6 | +//! renders these exact types. | ||
| 7 | + | ||
| 8 | +use serde::{Deserialize, Serialize}; | ||
| 9 | +use time::{Date, OffsetDateTime}; | ||
| 10 | + | ||
| 11 | +use crate::board::{Column, Person, day_label}; | ||
| 12 | + | ||
| 13 | +/// The stored task, plus the two ids the reader needs to know it is allowed to | ||
| 14 | +/// see it. The description lives here rather than on | ||
| 15 | +/// [`TaskRow`](crate::board::TaskRow) so a board sweep does not carry every | ||
| 16 | +/// task's prose to the browser. | ||
| 17 | +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] | ||
| 18 | +pub struct TaskFacts { | ||
| 19 | + pub row: crate::board::TaskRow, | ||
| 20 | + pub description: String, | ||
| 21 | + pub board_id: String, | ||
| 22 | + pub workspace_id: String, | ||
| 23 | +} | ||
| 24 | + | ||
| 25 | +/// One end of a dependency, seen from the task being looked at. | ||
| 26 | +/// | ||
| 27 | +/// `cleared_at` is set when the blocking task finished; the row stays so the | ||
| 28 | +/// history — and the "you can start now" rule — has something to read. | ||
| 29 | +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] | ||
| 30 | +pub struct DependencyEdge { | ||
| 31 | + pub task_id: String, | ||
| 32 | + pub task_key: String, | ||
| 33 | + pub title: String, | ||
| 34 | + pub cleared_at: Option<OffsetDateTime>, | ||
| 35 | + pub done_at: Option<OffsetDateTime>, | ||
| 36 | +} | ||
| 37 | + | ||
| 38 | +impl DependencyEdge { | ||
| 39 | + pub fn is_cleared(&self) -> bool { | ||
| 40 | + self.cleared_at.is_some() || self.done_at.is_some() | ||
| 41 | + } | ||
| 42 | + | ||
| 43 | + /// The right-hand note on a `BLOCKED BY` row: `cleared Aug 16`, or what is | ||
| 44 | + /// still in the way. | ||
| 45 | + pub fn blocked_by_label(&self) -> String { | ||
| 46 | + match (self.cleared_at, self.done_at) { | ||
| 47 | + (Some(at), _) => format!("cleared {}", day_label(at.date())), | ||
| 48 | + (None, Some(at)) => format!("done {}", day_label(at.date())), | ||
| 49 | + (None, None) => "blocking this task".to_string(), | ||
| 50 | + } | ||
| 51 | + } | ||
| 52 | + | ||
| 53 | + /// The same note on a `BLOCKS` row. | ||
| 54 | + pub fn blocks_label(&self) -> String { | ||
| 55 | + if self.is_cleared() { | ||
| 56 | + "no longer waiting".to_string() | ||
| 57 | + } else { | ||
| 58 | + "waiting on this task".to_string() | ||
| 59 | + } | ||
| 60 | + } | ||
| 61 | +} | ||
| 62 | + | ||
| 63 | +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] | ||
| 64 | +pub struct Comment { | ||
| 65 | + pub id: String, | ||
| 66 | + pub author: Person, | ||
| 67 | + pub at: OffsetDateTime, | ||
| 68 | + pub body: String, | ||
| 69 | +} | ||
| 70 | + | ||
| 71 | +/// What happened to a task. The kind is the machine's word for it; the sentence | ||
| 72 | +/// the strip shows is built here rather than stored, so wording is not frozen | ||
| 73 | +/// into the database. | ||
| 74 | +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] | ||
| 75 | +#[serde(rename_all = "snake_case")] | ||
| 76 | +pub enum ActivityKind { | ||
| 77 | + Created, | ||
| 78 | + Retitled, | ||
| 79 | + Described, | ||
| 80 | + DeadlineSet, | ||
| 81 | + DeadlineCleared, | ||
| 82 | + Assigned, | ||
| 83 | + Unassigned, | ||
| 84 | + Linked, | ||
| 85 | + Unlinked, | ||
| 86 | + Moved, | ||
| 87 | + Unblocked, | ||
| 88 | + Deleted, | ||
| 89 | + /// A kind written by a newer version than the one reading it. The stored | ||
| 90 | + /// detail is shown as-is rather than dropping the row. | ||
| 91 | + Other(String), | ||
| 92 | +} | ||
| 93 | + | ||
| 94 | +impl ActivityKind { | ||
| 95 | + pub fn as_str(&self) -> &str { | ||
| 96 | + match self { | ||
| 97 | + ActivityKind::Created => "created", | ||
| 98 | + ActivityKind::Retitled => "retitled", | ||
| 99 | + ActivityKind::Described => "described", | ||
| 100 | + ActivityKind::DeadlineSet => "deadline_set", | ||
| 101 | + ActivityKind::DeadlineCleared => "deadline_cleared", | ||
| 102 | + ActivityKind::Assigned => "assigned", | ||
| 103 | + ActivityKind::Unassigned => "unassigned", | ||
| 104 | + ActivityKind::Linked => "linked", | ||
| 105 | + ActivityKind::Unlinked => "unlinked", | ||
| 106 | + ActivityKind::Moved => "moved", | ||
| 107 | + ActivityKind::Unblocked => "unblocked", | ||
| 108 | + ActivityKind::Deleted => "deleted", | ||
| 109 | + ActivityKind::Other(raw) => raw, | ||
| 110 | + } | ||
| 111 | + } | ||
| 112 | + | ||
| 113 | + pub fn parse(raw: &str) -> Self { | ||
| 114 | + match raw { | ||
| 115 | + "created" => ActivityKind::Created, | ||
| 116 | + "retitled" => ActivityKind::Retitled, | ||
| 117 | + "described" => ActivityKind::Described, | ||
| 118 | + "deadline_set" => ActivityKind::DeadlineSet, | ||
| 119 | + "deadline_cleared" => ActivityKind::DeadlineCleared, | ||
| 120 | + "assigned" => ActivityKind::Assigned, | ||
| 121 | + "unassigned" => ActivityKind::Unassigned, | ||
| 122 | + "linked" => ActivityKind::Linked, | ||
| 123 | + "unlinked" => ActivityKind::Unlinked, | ||
| 124 | + "moved" => ActivityKind::Moved, | ||
| 125 | + "unblocked" => ActivityKind::Unblocked, | ||
| 126 | + "deleted" => ActivityKind::Deleted, | ||
| 127 | + other => ActivityKind::Other(other.to_string()), | ||
| 128 | + } | ||
| 129 | + } | ||
| 130 | +} | ||
| 131 | + | ||
| 132 | +/// One line of the activity strip. | ||
| 133 | +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] | ||
| 134 | +pub struct ActivityEntry { | ||
| 135 | + pub id: String, | ||
| 136 | + /// Absent when the system did it rather than a person — the rules engine | ||
| 137 | + /// clearing a dependency, for one. | ||
| 138 | + pub actor: Option<Person>, | ||
| 139 | + pub kind: ActivityKind, | ||
| 140 | + /// The variable half: the deadline that was set, the key that was linked. | ||
| 141 | + pub detail: String, | ||
| 142 | + pub at: OffsetDateTime, | ||
| 143 | +} | ||
| 144 | + | ||
| 145 | +impl ActivityEntry { | ||
| 146 | + /// The sentence after the name: `created this task`, `set deadline Aug 21`. | ||
| 147 | + pub fn sentence(&self) -> String { | ||
| 148 | + let detail = self.detail.trim(); | ||
| 149 | + match &self.kind { | ||
| 150 | + ActivityKind::Created => "created this task".to_string(), | ||
| 151 | + ActivityKind::Retitled => "renamed this task".to_string(), | ||
| 152 | + ActivityKind::Described => "edited the description".to_string(), | ||
| 153 | + ActivityKind::DeadlineSet => format!("set deadline {detail}"), | ||
| 154 | + ActivityKind::DeadlineCleared => "removed the deadline".to_string(), | ||
| 155 | + ActivityKind::Assigned => format!("assigned {detail}"), | ||
| 156 | + ActivityKind::Unassigned => format!("unassigned {detail}"), | ||
| 157 | + ActivityKind::Linked => format!("linked {detail}"), | ||
| 158 | + ActivityKind::Unlinked => format!("unlinked {detail}"), | ||
| 159 | + ActivityKind::Moved => format!("moved {detail}"), | ||
| 160 | + ActivityKind::Unblocked => format!("unblocked this task — {detail}"), | ||
| 161 | + ActivityKind::Deleted => "deleted this task".to_string(), | ||
| 162 | + ActivityKind::Other(_) => detail.to_string(), | ||
| 163 | + } | ||
| 164 | + } | ||
| 165 | + | ||
| 166 | + /// `Aug 19 11:04`, the stamp the strip puts in front of the sentence. | ||
| 167 | + pub fn moment(&self) -> String { | ||
| 168 | + moment_label(self.at) | ||
| 169 | + } | ||
| 170 | +} | ||
| 171 | + | ||
| 172 | +/// `Aug 19 11:04`. Times are UTC, as everything stored is. | ||
| 173 | +pub fn moment_label(at: OffsetDateTime) -> String { | ||
| 174 | + format!( | ||
| 175 | + "{} {:02}:{:02}", | ||
| 176 | + day_label(at.date()), | ||
| 177 | + at.hour(), | ||
| 178 | + at.minute() | ||
| 179 | + ) | ||
| 180 | +} | ||
| 181 | + | ||
| 182 | +/// Everything one task detail needs, in one value. | ||
| 183 | +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] | ||
| 184 | +pub struct TaskDetail { | ||
| 185 | + pub id: String, | ||
| 186 | + pub task_key: String, | ||
| 187 | + pub title: String, | ||
| 188 | + pub description: String, | ||
| 189 | + /// The column the task is in — the artboard's STATUS field. | ||
| 190 | + pub column: Column, | ||
| 191 | + /// Every column on the board, so the status control has its options | ||
| 192 | + /// without a second call. | ||
| 193 | + pub columns: Vec<Column>, | ||
| 194 | + pub deadline: Option<Date>, | ||
| 195 | + pub done_at: Option<OffsetDateTime>, | ||
| 196 | + pub assignees: Vec<Person>, | ||
| 197 | + /// Who may be assigned: the workspace's writers. Viewers are never here — | ||
| 198 | + /// a viewer cannot be given work. | ||
| 199 | + pub assignable: Vec<Person>, | ||
| 200 | + /// Tasks that must finish before this one can start. | ||
| 201 | + pub blocked_by: Vec<DependencyEdge>, | ||
| 202 | + /// Tasks waiting on this one. | ||
| 203 | + pub blocks: Vec<DependencyEdge>, | ||
| 204 | + pub comments: Vec<Comment>, | ||
| 205 | + pub activity: Vec<ActivityEntry>, | ||
| 206 | +} | ||
| 207 | + | ||
| 208 | +impl TaskDetail { | ||
| 209 | + pub fn is_done(&self) -> bool { | ||
| 210 | + self.done_at.is_some() | ||
| 211 | + } | ||
| 212 | + | ||
| 213 | + /// Blocked means something unfinished is in front of it. | ||
| 214 | + pub fn is_blocked(&self) -> bool { | ||
| 215 | + !self.is_done() && self.blocked_by.iter().any(|edge| !edge.is_cleared()) | ||
| 216 | + } | ||
| 217 | + | ||
| 218 | + pub fn is_overdue(&self, today: Date) -> bool { | ||
| 219 | + match self.deadline { | ||
| 220 | + Some(day) => !self.is_done() && day < today, | ||
| 221 | + None => false, | ||
| 222 | + } | ||
| 223 | + } | ||
| 224 | + | ||
| 225 | + /// The DEADLINE field's text: `Aug 21 · overdue`, `Sep 12`, `no deadline`. | ||
| 226 | + pub fn deadline_label(&self, today: Date) -> String { | ||
| 227 | + match self.deadline { | ||
| 228 | + Some(day) if !self.is_done() && day < today => { | ||
| 229 | + format!("{} · overdue", day_label(day)) | ||
| 230 | + } | ||
| 231 | + Some(day) => day_label(day), | ||
| 232 | + None => "no deadline".to_string(), | ||
| 233 | + } | ||
| 234 | + } | ||
| 235 | + | ||
| 236 | + /// `YYYY-MM-DD` for the date input, or an empty string. | ||
| 237 | + pub fn deadline_input(&self) -> String { | ||
| 238 | + self.deadline | ||
| 239 | + .map(|day| { | ||
| 240 | + format!( | ||
| 241 | + "{:04}-{:02}-{:02}", | ||
| 242 | + day.year(), | ||
| 243 | + day.month() as u8, | ||
| 244 | + day.day() | ||
| 245 | + ) | ||
| 246 | + }) | ||
| 247 | + .unwrap_or_default() | ||
| 248 | + } | ||
| 249 | + | ||
| 250 | + /// Who is not on this task yet — the picker's contents. | ||
| 251 | + pub fn unassigned(&self) -> impl Iterator<Item = &Person> { | ||
| 252 | + self.assignable | ||
| 253 | + .iter() | ||
| 254 | + .filter(|person| !self.assignees.iter().any(|on| on.id == person.id)) | ||
| 255 | + } | ||
| 256 | +} | ||
| 257 | + | ||
| 258 | +#[cfg(feature = "server")] | ||
| 259 | +pub use reads::{DetailReads, load}; | ||
| 260 | + | ||
| 261 | +#[cfg(feature = "server")] | ||
| 262 | +mod reads { | ||
| 263 | + use async_trait::async_trait; | ||
| 264 | + | ||
| 265 | + use super::{ActivityEntry, Comment, DependencyEdge, TaskDetail, TaskFacts}; | ||
| 266 | + use crate::board::{Column, Person}; | ||
| 267 | + use crate::store::Result; | ||
| 268 | + | ||
| 269 | + /// One task's read half, split out the way [`BoardReads`](crate::BoardReads) | ||
| 270 | + /// is, so a test can wrap it and count the round trips a detail costs. | ||
| 271 | + /// | ||
| 272 | + /// Every method here is one sweep of one thing. There is deliberately no | ||
| 273 | + /// "the person who wrote this comment" call: authors arrive joined. | ||
| 274 | + #[async_trait] | ||
| 275 | + pub trait DetailReads: Send + Sync { | ||
| 276 | + /// The task and the board it belongs to, or `None` if it is gone. | ||
| 277 | + async fn task(&self, task_id: &str) -> Result<Option<TaskFacts>>; | ||
| 278 | + | ||
| 279 | + async fn columns_for_board(&self, board_id: &str) -> Result<Vec<Column>>; | ||
| 280 | + | ||
| 281 | + async fn assignees_for_task(&self, task_id: &str) -> Result<Vec<Person>>; | ||
| 282 | + | ||
| 283 | + /// Everyone in the workspace who may be given work. | ||
| 284 | + async fn assignable_people(&self, workspace_id: &str) -> Result<Vec<Person>>; | ||
| 285 | + | ||
| 286 | + /// Both directions at once: `(is_blocked_by, edge)`. `true` means the | ||
| 287 | + /// other task blocks this one. | ||
| 288 | + async fn dependencies_for_task(&self, task_id: &str) | ||
| 289 | + -> Result<Vec<(bool, DependencyEdge)>>; | ||
| 290 | + | ||
| 291 | + async fn comments_for_task(&self, task_id: &str) -> Result<Vec<Comment>>; | ||
| 292 | + | ||
| 293 | + async fn activity_for_task(&self, task_id: &str) -> Result<Vec<ActivityEntry>>; | ||
| 294 | + } | ||
| 295 | + | ||
| 296 | + /// Loads one task detail in seven queries — the task, the board's columns, | ||
| 297 | + /// its assignees, the workspace's writers, both directions of its | ||
| 298 | + /// dependencies, its comments and its activity. | ||
| 299 | + /// | ||
| 300 | + /// Seven, not seven-plus-one-per-comment: `a_task_detail_costs_seven_ | ||
| 301 | + /// queries_whatever_it_carries` in `tests/store.rs` holds that line. | ||
| 302 | + pub async fn load( | ||
| 303 | + reads: &dyn DetailReads, | ||
| 304 | + workspace_id: &str, | ||
| 305 | + task_id: &str, | ||
| 306 | + ) -> Result<Option<TaskDetail>> { | ||
| 307 | + let Some(facts) = reads.task(task_id).await? else { | ||
| 308 | + return Ok(None); | ||
| 309 | + }; | ||
| 310 | + // A task id from another workspace is not found, not forbidden: the | ||
| 311 | + // asker is told nothing about a board they cannot see. | ||
| 312 | + if facts.workspace_id != workspace_id { | ||
| 313 | + return Ok(None); | ||
| 314 | + } | ||
| 315 | + let task = facts.row; | ||
| 316 | + let columns = reads.columns_for_board(&facts.board_id).await?; | ||
| 317 | + let assignees = reads.assignees_for_task(task_id).await?; | ||
| 318 | + let assignable = reads.assignable_people(workspace_id).await?; | ||
| 319 | + let edges = reads.dependencies_for_task(task_id).await?; | ||
| 320 | + let comments = reads.comments_for_task(task_id).await?; | ||
| 321 | + let activity = reads.activity_for_task(task_id).await?; | ||
| 322 | + | ||
| 323 | + let Some(column) = columns | ||
| 324 | + .iter() | ||
| 325 | + .find(|column| column.id == task.column_id) | ||
| 326 | + .cloned() | ||
| 327 | + else { | ||
| 328 | + return Ok(None); | ||
| 329 | + }; | ||
| 330 | + let mut blocked_by = Vec::new(); | ||
| 331 | + let mut blocks = Vec::new(); | ||
| 332 | + for (is_blocked_by, edge) in edges { | ||
| 333 | + if is_blocked_by { | ||
| 334 | + blocked_by.push(edge); | ||
| 335 | + } else { | ||
| 336 | + blocks.push(edge); | ||
| 337 | + } | ||
| 338 | + } | ||
| 339 | + | ||
| 340 | + Ok(Some(TaskDetail { | ||
| 341 | + id: task.id, | ||
| 342 | + task_key: task.task_key, | ||
| 343 | + title: task.title, | ||
| 344 | + description: facts.description, | ||
| 345 | + column, | ||
| 346 | + columns, | ||
| 347 | + deadline: task.deadline, | ||
| 348 | + done_at: task.done_at, | ||
| 349 | + assignees, | ||
| 350 | + assignable, | ||
| 351 | + blocked_by, | ||
| 352 | + blocks, | ||
| 353 | + comments, | ||
| 354 | + activity, | ||
| 355 | + })) | ||
| 356 | + } | ||
| 357 | +} | ||
crates/dizey-core/src/lib.rsmodified+4 -0
| @@ -6,8 +6,10 @@ | |||
| 6 | //! token minting. | 6 | //! token minting. |
| 7 | 7 | ||
| 8 | pub mod board; | 8 | pub mod board; |
| 9 | +pub mod detail; | ||
| 9 | pub mod role; | 10 | pub mod role; |
| 10 | pub use board::{BoardView, Column, TaskCard}; | 11 | pub use board::{BoardView, Column, TaskCard}; |
| 12 | +pub use detail::TaskDetail; | ||
| 11 | pub use role::Role; | 13 | pub use role::Role; |
| 12 | 14 | ||
| 13 | #[cfg(feature = "server")] | 15 | #[cfg(feature = "server")] |
| @@ -22,4 +24,6 @@ pub use accounts::{AccountError, Accounts}; | |||
| 22 | #[cfg(feature = "server")] | 24 | #[cfg(feature = "server")] |
| 23 | pub use board::{BoardReads, load as load_board}; | 25 | pub use board::{BoardReads, load as load_board}; |
| 24 | #[cfg(feature = "server")] | 26 | #[cfg(feature = "server")] |
| 27 | +pub use detail::{DetailReads, load as load_detail}; | ||
| 28 | +#[cfg(feature = "server")] | ||
| 25 | pub use store::{Store, StoreError, TursoStore}; | 29 | pub use store::{Store, StoreError, TursoStore}; |
crates/dizey-core/src/store/mod.rsmodified+47 -1
| @@ -9,6 +9,7 @@ use serde::{Deserialize, Serialize}; | |||
| 9 | use time::OffsetDateTime; | 9 | use time::OffsetDateTime; |
| 10 | 10 | ||
| 11 | use crate::board::{BoardReads, TaskRow}; | 11 | use crate::board::{BoardReads, TaskRow}; |
| 12 | +use crate::detail::{ActivityKind, DetailReads}; | ||
| 12 | use crate::Role; | 13 | use crate::Role; |
| 13 | 14 | ||
| 14 | pub mod turso_store; | 15 | pub mod turso_store; |
| @@ -27,6 +28,9 @@ pub enum StoreError { | |||
| 27 | Conflict(&'static str), | 28 | Conflict(&'static str), |
| 28 | #[error("stored value is not valid: {0}")] | 29 | #[error("stored value is not valid: {0}")] |
| 29 | Corrupt(String), | 30 | Corrupt(String), |
| 31 | + /// The dependency asked for would put a task behind itself. | ||
| 32 | + #[error("that link would make a circle")] | ||
| 33 | + Cycle, | ||
| 30 | } | 34 | } |
| 31 | 35 | ||
| 32 | pub type Result<T> = std::result::Result<T, StoreError>; | 36 | pub type Result<T> = std::result::Result<T, StoreError>; |
| @@ -156,7 +160,7 @@ pub struct NewTask<'a> { | |||
| 156 | /// The board's reads live in [`BoardReads`], so the sweep-per-board shape is a | 160 | /// The board's reads live in [`BoardReads`], so the sweep-per-board shape is a |
| 157 | /// trait a test can wrap and count. | 161 | /// trait a test can wrap and count. |
| 158 | #[async_trait] | 162 | #[async_trait] |
| 159 | -pub trait Store: BoardReads + 'static { | 163 | +pub trait Store: BoardReads + DetailReads + 'static { |
| 160 | // -- workspace --------------------------------------------------------- | 164 | // -- workspace --------------------------------------------------------- |
| 161 | 165 | ||
| 162 | /// Claims an empty database: writes the workspace, its first account and | 166 | /// Claims an empty database: writes the workspace, its first account and |
| @@ -312,6 +316,11 @@ pub trait Store: BoardReads + 'static { | |||
| 312 | async fn unassign_task(&self, task_id: &str, user_id: &str) -> Result<()>; | 316 | async fn unassign_task(&self, task_id: &str, user_id: &str) -> Result<()>; |
| 313 | 317 | ||
| 314 | /// `blocked` cannot start until `blocking` is finished. | 318 | /// `blocked` cannot start until `blocking` is finished. |
| 319 | + /// | ||
| 320 | + /// Refuses with [`StoreError::Cycle`] if the edge would close a loop. The | ||
| 321 | + /// check runs inside the same transaction as the insert, so two writers | ||
| 322 | + /// racing to add the two halves of a circle cannot both pass a check that | ||
| 323 | + /// was true when they read it. | ||
| 315 | async fn add_dependency( | 324 | async fn add_dependency( |
| 316 | &self, | 325 | &self, |
| 317 | blocked_task_id: &str, | 326 | blocked_task_id: &str, |
| @@ -326,6 +335,8 @@ pub trait Store: BoardReads + 'static { | |||
| 326 | at: OffsetDateTime, | 335 | at: OffsetDateTime, |
| 327 | ) -> Result<()>; | 336 | ) -> Result<()>; |
| 328 | 337 | ||
| 338 | + /// The author is the session's user, decided by the handler. There is no | ||
| 339 | + /// author field on the form. | ||
| 329 | async fn add_comment( | 340 | async fn add_comment( |
| 330 | &self, | 341 | &self, |
| 331 | task_id: &str, | 342 | task_id: &str, |
| @@ -333,4 +344,39 @@ pub trait Store: BoardReads + 'static { | |||
| 333 | body: &str, | 344 | body: &str, |
| 334 | at: OffsetDateTime, | 345 | at: OffsetDateTime, |
| 335 | ) -> Result<String>; | 346 | ) -> Result<String>; |
| 347 | + | ||
| 348 | + /// Writes the title, description and deadline the detail screen saved, and | ||
| 349 | + /// records one activity line per field that actually changed. | ||
| 350 | + async fn save_task( | ||
| 351 | + &self, | ||
| 352 | + task_id: &str, | ||
| 353 | + title: &str, | ||
| 354 | + description: &str, | ||
| 355 | + deadline: Option<time::Date>, | ||
| 356 | + actor_id: &str, | ||
| 357 | + at: OffsetDateTime, | ||
| 358 | + ) -> Result<()>; | ||
| 359 | + | ||
| 360 | + /// Removes a task and every dependency edge it stood in. Tasks that were | ||
| 361 | + /// waiting only on this one become unblocked, and that is recorded in | ||
| 362 | + /// their activity — the rules engine will want to mail about it. | ||
| 363 | + /// | ||
| 364 | + /// Returns the ids of the tasks that were freed. | ||
| 365 | + async fn delete_task( | ||
| 366 | + &self, | ||
| 367 | + task_id: &str, | ||
| 368 | + actor_id: &str, | ||
| 369 | + at: OffsetDateTime, | ||
| 370 | + ) -> Result<Vec<String>>; | ||
| 371 | + | ||
| 372 | + /// Appends one line to a task's activity trail. `actor_id` is `None` when | ||
| 373 | + /// the system did it rather than a person. | ||
| 374 | + async fn record_activity( | ||
| 375 | + &self, | ||
| 376 | + task_id: &str, | ||
| 377 | + actor_id: Option<&str>, | ||
| 378 | + kind: &ActivityKind, | ||
| 379 | + detail: &str, | ||
| 380 | + at: OffsetDateTime, | ||
| 381 | + ) -> Result<()>; | ||
| 336 | } | 382 | } |
crates/dizey-core/src/store/turso_store.rsmodified+504 -8
| @@ -17,6 +17,7 @@ use super::{ | |||
| 17 | }; | 17 | }; |
| 18 | use crate::Role; | 18 | use crate::Role; |
| 19 | use crate::board::{BoardMeta, BoardReads, Column, Person, TaskRow}; | 19 | use crate::board::{BoardMeta, BoardReads, Column, Person, TaskRow}; |
| 20 | +use crate::detail::{ActivityEntry, ActivityKind, Comment, DependencyEdge, DetailReads, TaskFacts}; | ||
| 20 | use time::Date; | 21 | use time::Date; |
| 21 | use time::format_description::BorrowedFormatItem; | 22 | use time::format_description::BorrowedFormatItem; |
| 22 | use time::macros::format_description; | 23 | use time::macros::format_description; |
| @@ -856,6 +857,18 @@ impl Store for TursoStore { | |||
| 856 | ], | 857 | ], |
| 857 | ) | 858 | ) |
| 858 | .await?; | 859 | .await?; |
| 860 | + tx.execute( | ||
| 861 | + "INSERT INTO activity (id, task_id, actor_id, kind, detail, created_at) \ | ||
| 862 | + VALUES (?1, ?2, ?3, ?4, '', ?5)", | ||
| 863 | + params![ | ||
| 864 | + Uuid::new_v4().to_string(), | ||
| 865 | + id.clone(), | ||
| 866 | + new.created_by, | ||
| 867 | + ActivityKind::Created.as_str(), | ||
| 868 | + now.clone() | ||
| 869 | + ], | ||
| 870 | + ) | ||
| 871 | + .await?; | ||
| 859 | Ok::<_, turso::Error>(Some((task_key, position))) | 872 | Ok::<_, turso::Error>(Some((task_key, position))) |
| 860 | } | 873 | } |
| 861 | .await; | 874 | .await; |
| @@ -913,19 +926,87 @@ impl Store for TursoStore { | |||
| 913 | blocking_task_id: &str, | 926 | blocking_task_id: &str, |
| 914 | at: OffsetDateTime, | 927 | at: OffsetDateTime, |
| 915 | ) -> Result<()> { | 928 | ) -> Result<()> { |
| 916 | - // Re-adding a dependency that was cleared puts it back in force rather | 929 | + if blocked_task_id == blocking_task_id { |
| 917 | - // than leaving the cleared row to hide it. | 930 | + return Err(StoreError::Cycle); |
| 918 | - self.conn | 931 | + } |
| 919 | - .execute( | 932 | + let stamp = stamp(at)?; |
| 933 | + | ||
| 934 | + let mut conn = self.tx_conn().await?; | ||
| 935 | + // IMMEDIATE: the reachability read and the insert that invalidates it | ||
| 936 | + // must not be interleaved with another writer's pair, or two adds each | ||
| 937 | + // pass a check that was true when they read it and the pair closes a | ||
| 938 | + // circle neither of them could see. | ||
| 939 | + let tx = conn | ||
| 940 | + .transaction_with_behavior(TransactionBehavior::Immediate) | ||
| 941 | + .await | ||
| 942 | + .map_err(backend)?; | ||
| 943 | + | ||
| 944 | + let written = async { | ||
| 945 | + // Every edge, cleared or not: a cleared edge is a link that is | ||
| 946 | + // satisfied, not a link that is gone, and re-adding its other half | ||
| 947 | + // would still be a circle on the screen. | ||
| 948 | + let mut rows = tx | ||
| 949 | + .query( | ||
| 950 | + "SELECT d.blocked_task_id, d.blocking_task_id FROM task_dependency d \ | ||
| 951 | + JOIN task b ON b.id = d.blocked_task_id \ | ||
| 952 | + JOIN task k ON k.id = d.blocking_task_id \ | ||
| 953 | + WHERE b.deleted_at IS NULL AND k.deleted_at IS NULL", | ||
| 954 | + (), | ||
| 955 | + ) | ||
| 956 | + .await?; | ||
| 957 | + // blocked -> [what blocks it] | ||
| 958 | + let mut edges: std::collections::HashMap<String, Vec<String>> = | ||
| 959 | + std::collections::HashMap::new(); | ||
| 960 | + while let Some(row) = rows.next().await? { | ||
| 961 | + edges | ||
| 962 | + .entry(row.get::<String>(0)?) | ||
| 963 | + .or_default() | ||
| 964 | + .push(row.get::<String>(1)?); | ||
| 965 | + } | ||
| 966 | + drop(rows); | ||
| 967 | + | ||
| 968 | + // Walking "what blocks it" from the proposed blocker: if that walk | ||
| 969 | + // reaches the task being blocked, the new edge closes the loop. | ||
| 970 | + let mut seen = std::collections::HashSet::new(); | ||
| 971 | + let mut queue = std::collections::VecDeque::new(); | ||
| 972 | + queue.push_back(blocking_task_id.to_string()); | ||
| 973 | + while let Some(node) = queue.pop_front() { | ||
| 974 | + if node == blocked_task_id { | ||
| 975 | + return Ok(false); | ||
| 976 | + } | ||
| 977 | + if !seen.insert(node.clone()) { | ||
| 978 | + continue; | ||
| 979 | + } | ||
| 980 | + for next in edges.get(&node).into_iter().flatten() { | ||
| 981 | + queue.push_back(next.clone()); | ||
| 982 | + } | ||
| 983 | + } | ||
| 984 | + | ||
| 985 | + // Re-adding a dependency that was cleared puts it back in force | ||
| 986 | + // rather than leaving the cleared row to hide it. | ||
| 987 | + tx.execute( | ||
| 920 | "INSERT INTO task_dependency (blocked_task_id, blocking_task_id, created_at) \ | 988 | "INSERT INTO task_dependency (blocked_task_id, blocking_task_id, created_at) \ |
| 921 | VALUES (?1, ?2, ?3) \ | 989 | VALUES (?1, ?2, ?3) \ |
| 922 | ON CONFLICT (blocked_task_id, blocking_task_id) \ | 990 | ON CONFLICT (blocked_task_id, blocking_task_id) \ |
| 923 | DO UPDATE SET created_at = ?3, cleared_at = NULL", | 991 | DO UPDATE SET created_at = ?3, cleared_at = NULL", |
| 924 | - params![blocked_task_id, blocking_task_id, stamp(at)?], | 992 | + params![blocked_task_id, blocking_task_id, stamp.clone()], |
| 925 | ) | 993 | ) |
| 926 | - .await | 994 | + .await?; |
| 927 | - .map_err(backend)?; | 995 | + Ok::<_, turso::Error>(true) |
| 928 | - Ok(()) | 996 | + } |
| 997 | + .await; | ||
| 998 | + | ||
| 999 | + match written { | ||
| 1000 | + Ok(true) => tx.commit().await.map_err(backend), | ||
| 1001 | + Ok(false) => { | ||
| 1002 | + let _ = tx.rollback().await; | ||
| 1003 | + Err(StoreError::Cycle) | ||
| 1004 | + } | ||
| 1005 | + Err(e) => { | ||
| 1006 | + let _ = tx.rollback().await; | ||
| 1007 | + Err(backend(e)) | ||
| 1008 | + } | ||
| 1009 | + } | ||
| 929 | } | 1010 | } |
| 930 | 1011 | ||
| 931 | async fn clear_dependency( | 1012 | async fn clear_dependency( |
| @@ -963,6 +1044,421 @@ impl Store for TursoStore { | |||
| 963 | .map_err(backend)?; | 1044 | .map_err(backend)?; |
| 964 | Ok(id) | 1045 | Ok(id) |
| 965 | } | 1046 | } |
| 1047 | + | ||
| 1048 | + async fn save_task( | ||
| 1049 | + &self, | ||
| 1050 | + task_id: &str, | ||
| 1051 | + title: &str, | ||
| 1052 | + description: &str, | ||
| 1053 | + deadline: Option<Date>, | ||
| 1054 | + actor_id: &str, | ||
| 1055 | + at: OffsetDateTime, | ||
| 1056 | + ) -> Result<()> { | ||
| 1057 | + let deadline = deadline.map(day_text).transpose()?; | ||
| 1058 | + let stamp = stamp(at)?; | ||
| 1059 | + | ||
| 1060 | + let mut conn = self.tx_conn().await?; | ||
| 1061 | + // IMMEDIATE: the activity lines say what changed, so they are decided | ||
| 1062 | + // from the row this write is replacing and must see it unchanged. | ||
| 1063 | + let tx = conn | ||
| 1064 | + .transaction_with_behavior(TransactionBehavior::Immediate) | ||
| 1065 | + .await | ||
| 1066 | + .map_err(backend)?; | ||
| 1067 | + | ||
| 1068 | + let written = async { | ||
| 1069 | + let mut rows = tx | ||
| 1070 | + .query( | ||
| 1071 | + "SELECT title, description, deadline FROM task \ | ||
| 1072 | + WHERE id = ?1 AND deleted_at IS NULL", | ||
| 1073 | + params![task_id], | ||
| 1074 | + ) | ||
| 1075 | + .await?; | ||
| 1076 | + let Some(row) = rows.next().await? else { | ||
| 1077 | + return Ok(false); | ||
| 1078 | + }; | ||
| 1079 | + let was_title = row.get::<String>(0)?; | ||
| 1080 | + let was_description = row.get::<String>(1)?; | ||
| 1081 | + let was_deadline = row.get::<Option<String>>(2)?; | ||
| 1082 | + drop(rows); | ||
| 1083 | + | ||
| 1084 | + tx.execute( | ||
| 1085 | + "UPDATE task SET title = ?2, description = ?3, deadline = ?4, updated_at = ?5 \ | ||
| 1086 | + WHERE id = ?1", | ||
| 1087 | + params![task_id, title, description, deadline.clone(), stamp.clone()], | ||
| 1088 | + ) | ||
| 1089 | + .await?; | ||
| 1090 | + | ||
| 1091 | + let mut lines: Vec<(&'static str, String)> = Vec::new(); | ||
| 1092 | + if was_title != title { | ||
| 1093 | + lines.push((ActivityKind::Retitled.as_str(), title.to_string())); | ||
| 1094 | + } | ||
| 1095 | + if was_description != description { | ||
| 1096 | + lines.push((ActivityKind::Described.as_str(), String::new())); | ||
| 1097 | + } | ||
| 1098 | + if was_deadline != deadline { | ||
| 1099 | + match &deadline { | ||
| 1100 | + Some(day) => lines.push((ActivityKind::DeadlineSet.as_str(), day.clone())), | ||
| 1101 | + None => lines.push((ActivityKind::DeadlineCleared.as_str(), String::new())), | ||
| 1102 | + } | ||
| 1103 | + } | ||
| 1104 | + for (kind, detail) in lines { | ||
| 1105 | + tx.execute( | ||
| 1106 | + "INSERT INTO activity (id, task_id, actor_id, kind, detail, created_at) \ | ||
| 1107 | + VALUES (?1, ?2, ?3, ?4, ?5, ?6)", | ||
| 1108 | + params![ | ||
| 1109 | + Uuid::new_v4().to_string(), | ||
| 1110 | + task_id, | ||
| 1111 | + actor_id, | ||
| 1112 | + kind, | ||
| 1113 | + detail, | ||
| 1114 | + stamp.clone() | ||
| 1115 | + ], | ||
| 1116 | + ) | ||
| 1117 | + .await?; | ||
| 1118 | + } | ||
| 1119 | + Ok::<_, turso::Error>(true) | ||
| 1120 | + } | ||
| 1121 | + .await; | ||
| 1122 | + | ||
| 1123 | + match written { | ||
| 1124 | + Ok(true) => tx.commit().await.map_err(backend), | ||
| 1125 | + Ok(false) => { | ||
| 1126 | + let _ = tx.rollback().await; | ||
| 1127 | + Err(StoreError::NotFound) | ||
| 1128 | + } | ||
| 1129 | + Err(e) => { | ||
| 1130 | + let _ = tx.rollback().await; | ||
| 1131 | + Err(backend(e)) | ||
| 1132 | + } | ||
| 1133 | + } | ||
| 1134 | + } | ||
| 1135 | + | ||
| 1136 | + async fn delete_task( | ||
| 1137 | + &self, | ||
| 1138 | + task_id: &str, | ||
| 1139 | + actor_id: &str, | ||
| 1140 | + at: OffsetDateTime, | ||
| 1141 | + ) -> Result<Vec<String>> { | ||
| 1142 | + let stamp = stamp(at)?; | ||
| 1143 | + | ||
| 1144 | + let mut conn = self.tx_conn().await?; | ||
| 1145 | + // IMMEDIATE: whether a task is freed depends on the edges that are | ||
| 1146 | + // left after this one's are dropped. Reading them outside the write | ||
| 1147 | + // would let a concurrent link make the answer wrong. | ||
| 1148 | + let tx = conn | ||
| 1149 | + .transaction_with_behavior(TransactionBehavior::Immediate) | ||
| 1150 | + .await | ||
| 1151 | + .map_err(backend)?; | ||
| 1152 | + | ||
| 1153 | + let written = async { | ||
| 1154 | + let mut rows = tx | ||
| 1155 | + .query( | ||
| 1156 | + "SELECT task_key FROM task WHERE id = ?1 AND deleted_at IS NULL", | ||
| 1157 | + params![task_id], | ||
| 1158 | + ) | ||
| 1159 | + .await?; | ||
| 1160 | + let Some(row) = rows.next().await? else { | ||
| 1161 | + return Ok(None); | ||
| 1162 | + }; | ||
| 1163 | + let task_key = row.get::<String>(0)?; | ||
| 1164 | + drop(rows); | ||
| 1165 | + | ||
| 1166 | + // Everyone this task was standing in front of, before the edges go. | ||
| 1167 | + let mut rows = tx | ||
| 1168 | + .query( | ||
| 1169 | + "SELECT d.blocked_task_id FROM task_dependency d \ | ||
| 1170 | + JOIN task t ON t.id = d.blocked_task_id \ | ||
| 1171 | + WHERE d.blocking_task_id = ?1 AND d.cleared_at IS NULL \ | ||
| 1172 | + AND t.deleted_at IS NULL", | ||
| 1173 | + params![task_id], | ||
| 1174 | + ) | ||
| 1175 | + .await?; | ||
| 1176 | + let mut waiting = Vec::new(); | ||
| 1177 | + while let Some(row) = rows.next().await? { | ||
| 1178 | + waiting.push(row.get::<String>(0)?); | ||
| 1179 | + } | ||
| 1180 | + drop(rows); | ||
| 1181 | + | ||
| 1182 | + tx.execute( | ||
| 1183 | + "DELETE FROM task_dependency \ | ||
| 1184 | + WHERE blocked_task_id = ?1 OR blocking_task_id = ?1", | ||
| 1185 | + params![task_id], | ||
| 1186 | + ) | ||
| 1187 | + .await?; | ||
| 1188 | + tx.execute( | ||
| 1189 | + "UPDATE task SET deleted_at = ?2, updated_at = ?2 WHERE id = ?1", | ||
| 1190 | + params![task_id, stamp.clone()], | ||
| 1191 | + ) | ||
| 1192 | + .await?; | ||
| 1193 | + tx.execute( | ||
| 1194 | + "INSERT INTO activity (id, task_id, actor_id, kind, detail, created_at) \ | ||
| 1195 | + VALUES (?1, ?2, ?3, ?4, '', ?5)", | ||
| 1196 | + params![ | ||
| 1197 | + Uuid::new_v4().to_string(), | ||
| 1198 | + task_id, | ||
| 1199 | + actor_id, | ||
| 1200 | + ActivityKind::Deleted.as_str(), | ||
| 1201 | + stamp.clone() | ||
| 1202 | + ], | ||
| 1203 | + ) | ||
| 1204 | + .await?; | ||
| 1205 | + | ||
| 1206 | + // Freed only if nothing else is still in front of them. | ||
| 1207 | + let mut freed = Vec::new(); | ||
| 1208 | + for blocked in waiting { | ||
| 1209 | + let mut rows = tx | ||
| 1210 | + .query( | ||
| 1211 | + "SELECT COUNT(*) FROM task_dependency d \ | ||
| 1212 | + JOIN task t ON t.id = d.blocking_task_id \ | ||
| 1213 | + WHERE d.blocked_task_id = ?1 AND d.cleared_at IS NULL \ | ||
| 1214 | + AND t.deleted_at IS NULL", | ||
| 1215 | + params![blocked.clone()], | ||
| 1216 | + ) | ||
| 1217 | + .await?; | ||
| 1218 | + let left = match rows.next().await? { | ||
| 1219 | + Some(row) => row.get::<i64>(0)?, | ||
| 1220 | + None => 0, | ||
| 1221 | + }; | ||
| 1222 | + drop(rows); | ||
| 1223 | + if left > 0 { | ||
| 1224 | + continue; | ||
| 1225 | + } | ||
| 1226 | + tx.execute( | ||
| 1227 | + "INSERT INTO activity (id, task_id, actor_id, kind, detail, created_at) \ | ||
| 1228 | + VALUES (?1, ?2, NULL, ?3, ?4, ?5)", | ||
| 1229 | + params![ | ||
| 1230 | + Uuid::new_v4().to_string(), | ||
| 1231 | + blocked.clone(), | ||
| 1232 | + ActivityKind::Unblocked.as_str(), | ||
| 1233 | + format!("{task_key} was deleted"), | ||
| 1234 | + stamp.clone() | ||
| 1235 | + ], | ||
| 1236 | + ) | ||
| 1237 | + .await?; | ||
| 1238 | + freed.push(blocked); | ||
| 1239 | + } | ||
| 1240 | + Ok::<_, turso::Error>(Some(freed)) | ||
| 1241 | + } | ||
| 1242 | + .await; | ||
| 1243 | + | ||
| 1244 | + match written { | ||
| 1245 | + Ok(Some(freed)) => { | ||
| 1246 | + tx.commit().await.map_err(backend)?; | ||
| 1247 | + Ok(freed) | ||
| 1248 | + } | ||
| 1249 | + Ok(None) => { | ||
| 1250 | + let _ = tx.rollback().await; | ||
| 1251 | + Err(StoreError::NotFound) | ||
| 1252 | + } | ||
| 1253 | + Err(e) => { | ||
| 1254 | + let _ = tx.rollback().await; | ||
| 1255 | + Err(backend(e)) | ||
| 1256 | + } | ||
| 1257 | + } | ||
| 1258 | + } | ||
| 1259 | + | ||
| 1260 | + async fn record_activity( | ||
| 1261 | + &self, | ||
| 1262 | + task_id: &str, | ||
| 1263 | + actor_id: Option<&str>, | ||
| 1264 | + kind: &ActivityKind, | ||
| 1265 | + detail: &str, | ||
| 1266 | + at: OffsetDateTime, | ||
| 1267 | + ) -> Result<()> { | ||
| 1268 | + self.conn | ||
| 1269 | + .execute( | ||
| 1270 | + "INSERT INTO activity (id, task_id, actor_id, kind, detail, created_at) \ | ||
| 1271 | + VALUES (?1, ?2, ?3, ?4, ?5, ?6)", | ||
| 1272 | + params![ | ||
| 1273 | + Uuid::new_v4().to_string(), | ||
| 1274 | + task_id, | ||
| 1275 | + actor_id, | ||
| 1276 | + kind.as_str(), | ||
| 1277 | + detail, | ||
| 1278 | + stamp(at)? | ||
| 1279 | + ], | ||
| 1280 | + ) | ||
| 1281 | + .await | ||
| 1282 | + .map_err(backend)?; | ||
| 1283 | + Ok(()) | ||
| 1284 | + } | ||
| 1285 | +} | ||
| 1286 | + | ||
| 1287 | +#[async_trait] | ||
| 1288 | +impl DetailReads for TursoStore { | ||
| 1289 | + async fn task(&self, task_id: &str) -> Result<Option<TaskFacts>> { | ||
| 1290 | + let row = self | ||
| 1291 | + .one_row( | ||
| 1292 | + "SELECT t.id, t.task_key, t.title, t.column_id, t.deadline, t.position, \ | ||
| 1293 | + t.done_at, t.description, t.board_id, b.workspace_id \ | ||
| 1294 | + FROM task t JOIN board b ON b.id = t.board_id \ | ||
| 1295 | + WHERE t.id = ?1 AND t.deleted_at IS NULL", | ||
| 1296 | + params![task_id], | ||
| 1297 | + ) | ||
| 1298 | + .await?; | ||
| 1299 | + let Some(row) = row else { return Ok(None) }; | ||
| 1300 | + Ok(Some(TaskFacts { | ||
| 1301 | + row: TaskRow { | ||
| 1302 | + id: text(&row, 0)?, | ||
| 1303 | + task_key: text(&row, 1)?, | ||
| 1304 | + title: text(&row, 2)?, | ||
| 1305 | + column_id: text(&row, 3)?, | ||
| 1306 | + deadline: opt_day(&row, 4)?, | ||
| 1307 | + position: row.get::<f64>(5).map_err(backend)?, | ||
| 1308 | + done_at: opt_stamp(&row, 6)?, | ||
| 1309 | + }, | ||
| 1310 | + description: text(&row, 7)?, | ||
| 1311 | + board_id: text(&row, 8)?, | ||
| 1312 | + workspace_id: text(&row, 9)?, | ||
| 1313 | + })) | ||
| 1314 | + } | ||
| 1315 | + | ||
| 1316 | + async fn columns_for_board(&self, board_id: &str) -> Result<Vec<Column>> { | ||
| 1317 | + BoardReads::columns(self, board_id).await | ||
| 1318 | + } | ||
| 1319 | + | ||
| 1320 | + async fn assignees_for_task(&self, task_id: &str) -> Result<Vec<Person>> { | ||
| 1321 | + let mut rows = self | ||
| 1322 | + .conn | ||
| 1323 | + .query( | ||
| 1324 | + "SELECT u.id, u.display_name, u.photo_path FROM task_assignee a \ | ||
| 1325 | + JOIN user u ON u.id = a.user_id \ | ||
| 1326 | + WHERE a.task_id = ?1 ORDER BY u.display_name", | ||
| 1327 | + params![task_id], | ||
| 1328 | + ) | ||
| 1329 | + .await | ||
| 1330 | + .map_err(backend)?; | ||
| 1331 | + let mut out = Vec::new(); | ||
| 1332 | + while let Some(row) = rows.next().await.map_err(backend)? { | ||
| 1333 | + out.push(Person { | ||
| 1334 | + id: text(&row, 0)?, | ||
| 1335 | + display_name: text(&row, 1)?, | ||
| 1336 | + photo_path: opt_text(&row, 2)?, | ||
| 1337 | + }); | ||
| 1338 | + } | ||
| 1339 | + Ok(out) | ||
| 1340 | + } | ||
| 1341 | + | ||
| 1342 | + async fn assignable_people(&self, workspace_id: &str) -> Result<Vec<Person>> { | ||
| 1343 | + // Id, name and photo only: the picker shows no addresses and no roles, | ||
| 1344 | + // so neither leaves the server for this screen. | ||
| 1345 | + let mut rows = self | ||
| 1346 | + .conn | ||
| 1347 | + .query( | ||
| 1348 | + "SELECT id, display_name, photo_path FROM user \ | ||
| 1349 | + WHERE workspace_id = ?1 AND role <> ?2 ORDER BY display_name", | ||
| 1350 | + params![workspace_id, Role::Viewer.as_str()], | ||
| 1351 | + ) | ||
| 1352 | + .await | ||
| 1353 | + .map_err(backend)?; | ||
| 1354 | + let mut out = Vec::new(); | ||
| 1355 | + while let Some(row) = rows.next().await.map_err(backend)? { | ||
| 1356 | + out.push(Person { | ||
| 1357 | + id: text(&row, 0)?, | ||
| 1358 | + display_name: text(&row, 1)?, | ||
| 1359 | + photo_path: opt_text(&row, 2)?, | ||
| 1360 | + }); | ||
| 1361 | + } | ||
| 1362 | + Ok(out) | ||
| 1363 | + } | ||
| 1364 | + | ||
| 1365 | + async fn dependencies_for_task( | ||
| 1366 | + &self, | ||
| 1367 | + task_id: &str, | ||
| 1368 | + ) -> Result<Vec<(bool, DependencyEdge)>> { | ||
| 1369 | + // Both directions in one round trip: the leading column says which. | ||
| 1370 | + let mut rows = self | ||
| 1371 | + .conn | ||
| 1372 | + .query( | ||
| 1373 | + "SELECT 1, t.id, t.task_key, t.title, d.cleared_at, t.done_at \ | ||
| 1374 | + FROM task_dependency d JOIN task t ON t.id = d.blocking_task_id \ | ||
| 1375 | + WHERE d.blocked_task_id = ?1 AND t.deleted_at IS NULL \ | ||
| 1376 | + UNION ALL \ | ||
| 1377 | + SELECT 0, t.id, t.task_key, t.title, d.cleared_at, t.done_at \ | ||
| 1378 | + FROM task_dependency d JOIN task t ON t.id = d.blocked_task_id \ | ||
| 1379 | + WHERE d.blocking_task_id = ?2 AND t.deleted_at IS NULL", | ||
| 1380 | + params![task_id, task_id], | ||
| 1381 | + ) | ||
| 1382 | + .await | ||
| 1383 | + .map_err(backend)?; | ||
| 1384 | + let mut out = Vec::new(); | ||
| 1385 | + while let Some(row) = rows.next().await.map_err(backend)? { | ||
| 1386 | + let is_blocked_by = row.get::<i64>(0).map_err(backend)? != 0; | ||
| 1387 | + out.push(( | ||
| 1388 | + is_blocked_by, | ||
| 1389 | + DependencyEdge { | ||
| 1390 | + task_id: text(&row, 1)?, | ||
| 1391 | + task_key: text(&row, 2)?, | ||
| 1392 | + title: text(&row, 3)?, | ||
| 1393 | + cleared_at: opt_stamp(&row, 4)?, | ||
| 1394 | + done_at: opt_stamp(&row, 5)?, | ||
| 1395 | + }, | ||
| 1396 | + )); | ||
| 1397 | + } | ||
| 1398 | + Ok(out) | ||
| 1399 | + } | ||
| 1400 | + | ||
| 1401 | + async fn comments_for_task(&self, task_id: &str) -> Result<Vec<Comment>> { | ||
| 1402 | + let mut rows = self | ||
| 1403 | + .conn | ||
| 1404 | + .query( | ||
| 1405 | + "SELECT c.id, c.body, c.created_at, u.id, u.display_name, u.photo_path \ | ||
| 1406 | + FROM comment c JOIN user u ON u.id = c.author_id \ | ||
| 1407 | + WHERE c.task_id = ?1 ORDER BY c.created_at, c.rowid", | ||
| 1408 | + params![task_id], | ||
| 1409 | + ) | ||
| 1410 | + .await | ||
| 1411 | + .map_err(backend)?; | ||
| 1412 | + let mut out = Vec::new(); | ||
| 1413 | + while let Some(row) = rows.next().await.map_err(backend)? { | ||
| 1414 | + out.push(Comment { | ||
| 1415 | + id: text(&row, 0)?, | ||
| 1416 | + body: text(&row, 1)?, | ||
| 1417 | + at: parse_stamp(&text(&row, 2)?)?, | ||
| 1418 | + author: Person { | ||
| 1419 | + id: text(&row, 3)?, | ||
| 1420 | + display_name: text(&row, 4)?, | ||
| 1421 | + photo_path: opt_text(&row, 5)?, | ||
| 1422 | + }, | ||
| 1423 | + }); | ||
| 1424 | + } | ||
| 1425 | + Ok(out) | ||
| 1426 | + } | ||
| 1427 | + | ||
| 1428 | + async fn activity_for_task(&self, task_id: &str) -> Result<Vec<ActivityEntry>> { | ||
| 1429 | + // LEFT JOIN: a line the system wrote has no actor, and dropping it | ||
| 1430 | + // would hide exactly the events the rules engine causes. | ||
| 1431 | + let mut rows = self | ||
| 1432 | + .conn | ||
| 1433 | + .query( | ||
| 1434 | + "SELECT a.id, a.kind, a.detail, a.created_at, u.id, u.display_name, \ | ||
| 1435 | + u.photo_path \ | ||
| 1436 | + FROM activity a LEFT JOIN user u ON u.id = a.actor_id \ | ||
| 1437 | + WHERE a.task_id = ?1 ORDER BY a.created_at, a.rowid", | ||
| 1438 | + params![task_id], | ||
| 1439 | + ) | ||
| 1440 | + .await | ||
| 1441 | + .map_err(backend)?; | ||
| 1442 | + let mut out = Vec::new(); | ||
| 1443 | + while let Some(row) = rows.next().await.map_err(backend)? { | ||
| 1444 | + let actor = match opt_text(&row, 4)? { | ||
| 1445 | + Some(id) => Some(Person { | ||
| 1446 | + id, | ||
| 1447 | + display_name: text(&row, 5)?, | ||
| 1448 | + photo_path: opt_text(&row, 6)?, | ||
| 1449 | + }), | ||
| 1450 | + None => None, | ||
| 1451 | + }; | ||
| 1452 | + out.push(ActivityEntry { | ||
| 1453 | + id: text(&row, 0)?, | ||
| 1454 | + kind: ActivityKind::parse(&text(&row, 1)?), | ||
| 1455 | + detail: text(&row, 2)?, | ||
| 1456 | + at: parse_stamp(&text(&row, 3)?)?, | ||
| 1457 | + actor, | ||
| 1458 | + }); | ||
| 1459 | + } | ||
| 1460 | + Ok(out) | ||
| 1461 | + } | ||
| 966 | } | 1462 | } |
| 967 | 1463 | ||
| 968 | #[async_trait] | 1464 | #[async_trait] |
crates/dizey-core/tests/store.rsmodified+352 -0
| @@ -1542,3 +1542,355 @@ fn initials_fall_back_to_two_letters() { | |||
| 1542 | assert_eq!(person("Ada").initials(), "A"); | 1542 | assert_eq!(person("Ada").initials(), "A"); |
| 1543 | assert_eq!(person(" ").initials(), "?"); | 1543 | assert_eq!(person(" ").initials(), "?"); |
| 1544 | } | 1544 | } |
| 1545 | + | ||
| 1546 | +// -- task detail ------------------------------------------------------------ | ||
| 1547 | + | ||
| 1548 | +use dizey_core::detail::{ | ||
| 1549 | + ActivityEntry, ActivityKind, Comment, DependencyEdge, DetailReads, TaskFacts, | ||
| 1550 | + load as load_detail, | ||
| 1551 | +}; | ||
| 1552 | + | ||
| 1553 | +/// The same trick as [`CountingReads`], for the detail screen: the round trips | ||
| 1554 | +/// one task costs must not follow how much it carries. | ||
| 1555 | +struct CountingDetail<'a> { | ||
| 1556 | + inner: &'a TursoStore, | ||
| 1557 | + calls: AtomicUsize, | ||
| 1558 | +} | ||
| 1559 | + | ||
| 1560 | +impl<'a> CountingDetail<'a> { | ||
| 1561 | + fn new(inner: &'a TursoStore) -> Self { | ||
| 1562 | + Self { | ||
| 1563 | + inner, | ||
| 1564 | + calls: AtomicUsize::new(0), | ||
| 1565 | + } | ||
| 1566 | + } | ||
| 1567 | + | ||
| 1568 | + fn count(&self) -> usize { | ||
| 1569 | + self.calls.load(Ordering::Relaxed) | ||
| 1570 | + } | ||
| 1571 | + | ||
| 1572 | + fn tick(&self) { | ||
| 1573 | + self.calls.fetch_add(1, Ordering::Relaxed); | ||
| 1574 | + } | ||
| 1575 | +} | ||
| 1576 | + | ||
| 1577 | +#[async_trait::async_trait] | ||
| 1578 | +impl DetailReads for CountingDetail<'_> { | ||
| 1579 | + async fn task(&self, task_id: &str) -> Result<Option<TaskFacts>, StoreError> { | ||
| 1580 | + self.tick(); | ||
| 1581 | + self.inner.task(task_id).await | ||
| 1582 | + } | ||
| 1583 | + | ||
| 1584 | + async fn columns_for_board( | ||
| 1585 | + &self, | ||
| 1586 | + board_id: &str, | ||
| 1587 | + ) -> Result<Vec<dizey_core::Column>, StoreError> { | ||
| 1588 | + self.tick(); | ||
| 1589 | + self.inner.columns_for_board(board_id).await | ||
| 1590 | + } | ||
| 1591 | + | ||
| 1592 | + async fn assignees_for_task(&self, task_id: &str) -> Result<Vec<Person>, StoreError> { | ||
| 1593 | + self.tick(); | ||
| 1594 | + self.inner.assignees_for_task(task_id).await | ||
| 1595 | + } | ||
| 1596 | + | ||
| 1597 | + async fn assignable_people(&self, workspace_id: &str) -> Result<Vec<Person>, StoreError> { | ||
| 1598 | + self.tick(); | ||
| 1599 | + self.inner.assignable_people(workspace_id).await | ||
| 1600 | + } | ||
| 1601 | + | ||
| 1602 | + async fn dependencies_for_task( | ||
| 1603 | + &self, | ||
| 1604 | + task_id: &str, | ||
| 1605 | + ) -> Result<Vec<(bool, DependencyEdge)>, StoreError> { | ||
| 1606 | + self.tick(); | ||
| 1607 | + self.inner.dependencies_for_task(task_id).await | ||
| 1608 | + } | ||
| 1609 | + | ||
| 1610 | + async fn comments_for_task(&self, task_id: &str) -> Result<Vec<Comment>, StoreError> { | ||
| 1611 | + self.tick(); | ||
| 1612 | + self.inner.comments_for_task(task_id).await | ||
| 1613 | + } | ||
| 1614 | + | ||
| 1615 | + async fn activity_for_task(&self, task_id: &str) -> Result<Vec<ActivityEntry>, StoreError> { | ||
| 1616 | + self.tick(); | ||
| 1617 | + self.inner.activity_for_task(task_id).await | ||
| 1618 | + } | ||
| 1619 | +} | ||
| 1620 | + | ||
| 1621 | +#[tokio::test] | ||
| 1622 | +async fn a_task_detail_carries_both_directions_of_its_dependencies() { | ||
| 1623 | + let (scratch, workspace, admin) = workspace_with_admin().await; | ||
| 1624 | + let store = &scratch.store; | ||
| 1625 | + let now = OffsetDateTime::now_utc(); | ||
| 1626 | + | ||
| 1627 | + let middle = add_task(store, &workspace, "Backlog", "middle", None, &admin).await; | ||
| 1628 | + let before = add_task(store, &workspace, "Backlog", "before", None, &admin).await; | ||
| 1629 | + let after = add_task(store, &workspace, "Backlog", "after", None, &admin).await; | ||
| 1630 | + store.add_dependency(&middle, &before, now).await.unwrap(); | ||
| 1631 | + store.add_dependency(&after, &middle, now).await.unwrap(); | ||
| 1632 | + | ||
| 1633 | + let detail = load_detail(store, &workspace, &middle) | ||
| 1634 | + .await | ||
| 1635 | + .unwrap() | ||
| 1636 | + .expect("the task is in this workspace"); | ||
| 1637 | + assert_eq!(detail.title, "middle"); | ||
| 1638 | + assert_eq!(detail.column.name, "Backlog"); | ||
| 1639 | + assert_eq!(detail.columns.len(), 4, "every column, for the picker"); | ||
| 1640 | + let blocked_by: Vec<&str> = detail | ||
| 1641 | + .blocked_by | ||
| 1642 | + .iter() | ||
| 1643 | + .map(|edge| edge.title.as_str()) | ||
| 1644 | + .collect(); | ||
| 1645 | + let blocks: Vec<&str> = detail.blocks.iter().map(|e| e.title.as_str()).collect(); | ||
| 1646 | + assert_eq!(blocked_by, ["before"]); | ||
| 1647 | + assert_eq!(blocks, ["after"]); | ||
| 1648 | + assert!(detail.is_blocked()); | ||
| 1649 | + assert_eq!(detail.blocked_by[0].blocked_by_label(), "blocking this task"); | ||
| 1650 | + assert_eq!(detail.blocks[0].blocks_label(), "waiting on this task"); | ||
| 1651 | +} | ||
| 1652 | + | ||
| 1653 | +#[tokio::test] | ||
| 1654 | +async fn a_viewer_is_never_offered_as_an_assignee() { | ||
| 1655 | + let (scratch, workspace, admin) = workspace_with_admin().await; | ||
| 1656 | + let store = &scratch.store; | ||
| 1657 | + let quiet = store | ||
| 1658 | + .create_user(NewUser { | ||
| 1659 | + workspace_id: workspace.clone(), | ||
| 1660 | + email: "quiet@dizey.sh".into(), | ||
| 1661 | + display_name: "Quiet Reader".into(), | ||
| 1662 | + role: Role::Viewer, | ||
| 1663 | + }) | ||
| 1664 | + .await | ||
| 1665 | + .unwrap(); | ||
| 1666 | + let task = add_task(store, &workspace, "Backlog", "a task", None, &admin).await; | ||
| 1667 | + | ||
| 1668 | + let detail = load_detail(store, &workspace, &task).await.unwrap().unwrap(); | ||
| 1669 | + assert!( | ||
| 1670 | + !detail.assignable.iter().any(|p| p.id == quiet.id), | ||
| 1671 | + "a viewer cannot be given work, so the picker never lists one" | ||
| 1672 | + ); | ||
| 1673 | + assert!(detail.assignable.iter().any(|p| p.id == admin)); | ||
| 1674 | +} | ||
| 1675 | + | ||
| 1676 | +#[tokio::test] | ||
| 1677 | +async fn a_task_in_another_workspace_is_not_found_rather_than_forbidden() { | ||
| 1678 | + let (scratch, workspace, admin) = workspace_with_admin().await; | ||
| 1679 | + let store = &scratch.store; | ||
| 1680 | + let task = add_task(store, &workspace, "Backlog", "a task", None, &admin).await; | ||
| 1681 | + | ||
| 1682 | + // Asking as a workspace that does not hold it says nothing about whether | ||
| 1683 | + // the id is real. | ||
| 1684 | + let answer = load_detail(store, "some-other-workspace", &task) | ||
| 1685 | + .await | ||
| 1686 | + .unwrap(); | ||
| 1687 | + assert!(answer.is_none()); | ||
| 1688 | +} | ||
| 1689 | + | ||
| 1690 | +#[tokio::test] | ||
| 1691 | +async fn a_dependency_that_would_close_a_circle_is_refused() { | ||
| 1692 | + let (scratch, workspace, admin) = workspace_with_admin().await; | ||
| 1693 | + let store = &scratch.store; | ||
| 1694 | + let now = OffsetDateTime::now_utc(); | ||
| 1695 | + | ||
| 1696 | + let a = add_task(store, &workspace, "Backlog", "a", None, &admin).await; | ||
| 1697 | + let b = add_task(store, &workspace, "Backlog", "b", None, &admin).await; | ||
| 1698 | + let c = add_task(store, &workspace, "Backlog", "c", None, &admin).await; | ||
| 1699 | + | ||
| 1700 | + // a waits on b, b waits on c. Now ask for c to wait on a: three nodes, not | ||
| 1701 | + // a self-edge, and the loop only shows up on the second hop. | ||
| 1702 | + store.add_dependency(&a, &b, now).await.unwrap(); | ||
| 1703 | + store.add_dependency(&b, &c, now).await.unwrap(); | ||
| 1704 | + assert!(matches!( | ||
| 1705 | + store.add_dependency(&c, &a, now).await, | ||
| 1706 | + Err(StoreError::Cycle) | ||
| 1707 | + )); | ||
| 1708 | + assert!(matches!( | ||
| 1709 | + store.add_dependency(&a, &a, now).await, | ||
| 1710 | + Err(StoreError::Cycle) | ||
| 1711 | + )); | ||
| 1712 | + | ||
| 1713 | + // The refusal wrote nothing. | ||
| 1714 | + let detail = load_detail(store, &workspace, &c).await.unwrap().unwrap(); | ||
| 1715 | + assert!(detail.blocked_by.is_empty()); | ||
| 1716 | +} | ||
| 1717 | + | ||
| 1718 | +#[tokio::test] | ||
| 1719 | +async fn a_cleared_dependency_still_counts_as_a_circle() { | ||
| 1720 | + let (scratch, workspace, admin) = workspace_with_admin().await; | ||
| 1721 | + let store = &scratch.store; | ||
| 1722 | + let now = OffsetDateTime::now_utc(); | ||
| 1723 | + | ||
| 1724 | + let a = add_task(store, &workspace, "Backlog", "a", None, &admin).await; | ||
| 1725 | + let b = add_task(store, &workspace, "Backlog", "b", None, &admin).await; | ||
| 1726 | + store.add_dependency(&a, &b, now).await.unwrap(); | ||
| 1727 | + store.clear_dependency(&a, &b, now).await.unwrap(); | ||
| 1728 | + | ||
| 1729 | + // A cleared edge is a link that is satisfied, not a link that is gone: the | ||
| 1730 | + // pair would still draw a circle on the screen. | ||
| 1731 | + assert!(matches!( | ||
| 1732 | + store.add_dependency(&b, &a, now).await, | ||
| 1733 | + Err(StoreError::Cycle) | ||
| 1734 | + )); | ||
| 1735 | +} | ||
| 1736 | + | ||
| 1737 | +#[tokio::test] | ||
| 1738 | +async fn deleting_a_task_frees_what_was_waiting_on_it() { | ||
| 1739 | + let (scratch, workspace, admin) = workspace_with_admin().await; | ||
| 1740 | + let store = &scratch.store; | ||
| 1741 | + let now = OffsetDateTime::now_utc(); | ||
| 1742 | + | ||
| 1743 | + let blocking = add_task(store, &workspace, "Backlog", "blocking", None, &admin).await; | ||
| 1744 | + let other = add_task(store, &workspace, "Backlog", "other", None, &admin).await; | ||
| 1745 | + let freed = add_task(store, &workspace, "Backlog", "freed", None, &admin).await; | ||
| 1746 | + let still_stuck = add_task(store, &workspace, "Backlog", "stuck", None, &admin).await; | ||
| 1747 | + store.add_dependency(&freed, &blocking, now).await.unwrap(); | ||
| 1748 | + store | ||
| 1749 | + .add_dependency(&still_stuck, &blocking, now) | ||
| 1750 | + .await | ||
| 1751 | + .unwrap(); | ||
| 1752 | + store.add_dependency(&still_stuck, &other, now).await.unwrap(); | ||
| 1753 | + | ||
| 1754 | + let unblocked = store.delete_task(&blocking, &admin, now).await.unwrap(); | ||
| 1755 | + assert_eq!(unblocked, vec![freed.clone()], "only the one with nothing else in front of it"); | ||
| 1756 | + | ||
| 1757 | + // The deleted task is gone from the board and from the edges it stood in. | ||
| 1758 | + let board = board_of(store, &workspace).await; | ||
| 1759 | + assert!(!board | ||
| 1760 | + .columns | ||
| 1761 | + .iter() | ||
| 1762 | + .flat_map(|column| column.cards.iter()) | ||
| 1763 | + .any(|card| card.id == blocking)); | ||
| 1764 | + let freed_detail = load_detail(store, &workspace, &freed).await.unwrap().unwrap(); | ||
| 1765 | + assert!(freed_detail.blocked_by.is_empty()); | ||
| 1766 | + assert!(!freed_detail.is_blocked()); | ||
| 1767 | + | ||
| 1768 | + // And the freeing is a recorded event, because the rules engine will want | ||
| 1769 | + // to mail about it. | ||
| 1770 | + let unblocked_line = freed_detail | ||
| 1771 | + .activity | ||
| 1772 | + .iter() | ||
| 1773 | + .find(|entry| entry.kind == ActivityKind::Unblocked) | ||
| 1774 | + .expect("the freeing is on the record"); | ||
| 1775 | + assert!(unblocked_line.actor.is_none(), "the system did this, not a person"); | ||
| 1776 | + assert!(unblocked_line.sentence().starts_with("unblocked this task")); | ||
| 1777 | + | ||
| 1778 | + let stuck_detail = load_detail(store, &workspace, &still_stuck) | ||
| 1779 | + .await | ||
| 1780 | + .unwrap() | ||
| 1781 | + .unwrap(); | ||
| 1782 | + assert!(stuck_detail.is_blocked(), "something else is still in front of it"); | ||
| 1783 | + assert!(!stuck_detail | ||
| 1784 | + .activity | ||
| 1785 | + .iter() | ||
| 1786 | + .any(|entry| entry.kind == ActivityKind::Unblocked)); | ||
| 1787 | + | ||
| 1788 | + // A second delete finds nothing to delete. | ||
| 1789 | + assert!(matches!( | ||
| 1790 | + store.delete_task(&blocking, &admin, now).await, | ||
| 1791 | + Err(StoreError::NotFound) | ||
| 1792 | + )); | ||
| 1793 | +} | ||
| 1794 | + | ||
| 1795 | +#[tokio::test] | ||
| 1796 | +async fn saving_a_task_records_only_what_changed() { | ||
| 1797 | + let (scratch, workspace, admin) = workspace_with_admin().await; | ||
| 1798 | + let store = &scratch.store; | ||
| 1799 | + let task = add_task(store, &workspace, "Backlog", "first title", None, &admin).await; | ||
| 1800 | + | ||
| 1801 | + store | ||
| 1802 | + .save_task( | ||
| 1803 | + &task, | ||
| 1804 | + "first title", | ||
| 1805 | + "", | ||
| 1806 | + None, | ||
| 1807 | + &admin, | ||
| 1808 | + OffsetDateTime::now_utc(), | ||
| 1809 | + ) | ||
| 1810 | + .await | ||
| 1811 | + .unwrap(); | ||
| 1812 | + let detail = load_detail(store, &workspace, &task).await.unwrap().unwrap(); | ||
| 1813 | + let kinds: Vec<&ActivityKind> = detail.activity.iter().map(|e| &e.kind).collect(); | ||
| 1814 | + assert_eq!(kinds, [&ActivityKind::Created], "a save that changed nothing says nothing"); | ||
| 1815 | + | ||
| 1816 | + store | ||
| 1817 | + .save_task( | ||
| 1818 | + &task, | ||
| 1819 | + "second title", | ||
| 1820 | + "some prose", | ||
| 1821 | + Some(date!(2026 - 09 - 12)), | ||
| 1822 | + &admin, | ||
| 1823 | + OffsetDateTime::now_utc(), | ||
| 1824 | + ) | ||
| 1825 | + .await | ||
| 1826 | + .unwrap(); | ||
| 1827 | + let detail = load_detail(store, &workspace, &task).await.unwrap().unwrap(); | ||
| 1828 | + assert_eq!(detail.title, "second title"); | ||
| 1829 | + assert_eq!(detail.description, "some prose"); | ||
| 1830 | + assert_eq!(detail.deadline_input(), "2026-09-12"); | ||
| 1831 | + let kinds: Vec<&ActivityKind> = detail.activity.iter().map(|e| &e.kind).collect(); | ||
| 1832 | + assert_eq!( | ||
| 1833 | + kinds, | ||
| 1834 | + [ | ||
| 1835 | + &ActivityKind::Created, | ||
| 1836 | + &ActivityKind::Retitled, | ||
| 1837 | + &ActivityKind::Described, | ||
| 1838 | + &ActivityKind::DeadlineSet | ||
| 1839 | + ] | ||
| 1840 | + ); | ||
| 1841 | +} | ||
| 1842 | + | ||
| 1843 | +#[tokio::test] | ||
| 1844 | +async fn a_task_detail_costs_seven_queries_whatever_it_carries() { | ||
| 1845 | + let (scratch, workspace, admin) = workspace_with_admin().await; | ||
| 1846 | + let store = &scratch.store; | ||
| 1847 | + let now = OffsetDateTime::now_utc(); | ||
| 1848 | + | ||
| 1849 | + let bare = add_task(store, &workspace, "Backlog", "bare", None, &admin).await; | ||
| 1850 | + let counted = CountingDetail::new(store); | ||
| 1851 | + load_detail(&counted, &workspace, &bare).await.unwrap(); | ||
| 1852 | + assert_eq!(counted.count(), 7, "a task with nothing hung off it"); | ||
| 1853 | + | ||
| 1854 | + // Twenty comments, twenty activity lines, twenty people who could be | ||
| 1855 | + // assigned and twenty tasks on the other end of a dependency — the four | ||
| 1856 | + // things a naive detail query fans out on. | ||
| 1857 | + let heavy = add_task(store, &workspace, "Backlog", "heavy", None, &admin).await; | ||
| 1858 | + for n in 0..20 { | ||
| 1859 | + let person = store | ||
| 1860 | + .create_user(NewUser { | ||
| 1861 | + workspace_id: workspace.clone(), | ||
| 1862 | + email: format!("member{n}@dizey.sh"), | ||
| 1863 | + display_name: format!("Member {n}"), | ||
| 1864 | + role: Role::Member, | ||
| 1865 | + }) | ||
| 1866 | + .await | ||
| 1867 | + .unwrap(); | ||
| 1868 | + store.assign_task(&heavy, &person.id).await.unwrap(); | ||
| 1869 | + store | ||
| 1870 | + .add_comment(&heavy, &person.id, "a note", now) | ||
| 1871 | + .await | ||
| 1872 | + .unwrap(); | ||
| 1873 | + store | ||
| 1874 | + .record_activity(&heavy, Some(&person.id), &ActivityKind::Moved, "to Review", now) | ||
| 1875 | + .await | ||
| 1876 | + .unwrap(); | ||
| 1877 | + let neighbour = add_task(store, &workspace, "Backlog", &format!("n{n}"), None, &admin).await; | ||
| 1878 | + store.add_dependency(&neighbour, &heavy, now).await.unwrap(); | ||
| 1879 | + } | ||
| 1880 | + | ||
| 1881 | + let counted = CountingDetail::new(store); | ||
| 1882 | + let detail = load_detail(&counted, &workspace, &heavy) | ||
| 1883 | + .await | ||
| 1884 | + .unwrap() | ||
| 1885 | + .unwrap(); | ||
| 1886 | + assert_eq!(detail.comments.len(), 20); | ||
| 1887 | + assert_eq!(detail.assignees.len(), 20); | ||
| 1888 | + assert_eq!(detail.blocks.len(), 20); | ||
| 1889 | + // Twenty moves plus the line create_task wrote. | ||
| 1890 | + assert_eq!(detail.activity.len(), 21); | ||
| 1891 | + assert_eq!( | ||
| 1892 | + counted.count(), | ||
| 1893 | + 7, | ||
| 1894 | + "the round trips a detail costs must not follow what it carries" | ||
| 1895 | + ); | ||
| 1896 | +} | ||