feat(kanban): authorize canonical sync reads
req: sync/007 req: auth/005 req: security/004
This commit is contained in:
@@ -42,11 +42,11 @@ encryption, retention, backup, and deployment policy remain host concerns.
|
||||
## Slice 4 — authoritative reconnect and convergence
|
||||
|
||||
- [ ] **User value:** offline and concurrent work reconnects without duplicate mutation, silent loss, stale authorization, or ambiguous conflict.
|
||||
- **State:** In progress — one app-owned `move_card` server command validates a durable client command id, applies the authoritative canonical column once, returns the same acknowledgement for an identical retry, rejects id reuse with a different payload, assigns one server sequence, and redelivers that canonical acknowledgement after a real EventSource disconnect/reconnect. Canonical acknowledgements and the next sequence are durably stored in a strict versioned JSON envelope using fsync plus atomic replacement; startup refuses malformed/unknown state, rebuilds the canonical board, and preserves idempotency and event replay across a real process restart. A dedicated opt-in sync route reads one pending IndexedDB command, retries transient failures with capped exponential backoff and randomized jitter, exposes online/offline state plus an accessible manual retry after exhaustion, waits for canonical acknowledgement on the reconnecting transport, and only then removes the durable command; later retry converges without a new local mutation. Canonical payload conflicts are not retried and remain durable with a visible reason. If requested history predates retained events, the stream emits a typed snapshot-required event and the client loads a versioned canonical snapshot. One deterministic rebase rule treats `reorder_card` as converged only when the canonical snapshot already places that card in `done`; it then atomically stores the snapshot/cursor and removes the satisfied command. If a later canonical command instead places the same card in `doing`, the rebase is explicitly `conflicted`, retains the local command and last committed snapshot/cursor unchanged, and exposes the divergent canonical column/reason. Two same-origin tabs coordinate an app-owned expiring IndexedDB lease so only one uploads; the standby exposes its role without issuing a request, and after the leader closes it takes over, receives one canonical acknowledgement/sequence, and removes the queue once. Each activation serializes uploads with one in flight, processes at most two acknowledged commands, exposes the retained durable count when backpressured, and resumes the next bounded run only through the visible retry action. A mixed queue commits and removes its accepted prefix exactly once, then stops on the first permanent rejection with the typed server cause visible, the rejected command plus untouched suffix durable, and blind retry disabled. The IndexedDB v1-to-v2 command migration transactionally adds the explicit target column, records a typed migration receipt, preserves causal order and interaction intent through an interrupted upload, and later drains in the original order. Every command POST now derives its principal, permission, and tenant from the current same-origin session before idempotency lookup or mutation; authenticated mode accepts only app-configured opaque session tokens and fails closed when signed out. Direct cross-tenant and stale-permission command attempts receive typed authorization denial before mutation. IndexedDB v3 additionally indexes every command and lease by the current server-derived tenant/principal partition: switched users enumerate zero foreign commands, issue no foreign replay, cannot export foreign payloads, and the owning account can export then resume its intact queue. Signed-out context lookup fails closed before opening the queue. Broader conflict decisions and authorization of snapshot/event reads remain.
|
||||
- **State:** In progress — one app-owned `move_card` server command validates a durable client command id, applies the authoritative canonical column once, returns the same acknowledgement for an identical retry, rejects id reuse with a different payload, assigns one server sequence, and redelivers that canonical acknowledgement after a real EventSource disconnect/reconnect. Canonical acknowledgements and the next sequence are durably stored in a strict versioned JSON envelope using fsync plus atomic replacement; startup refuses malformed/unknown state, rebuilds the canonical board, and preserves idempotency and event replay across a real process restart. A dedicated opt-in sync route reads one pending IndexedDB command, retries transient failures with capped exponential backoff and randomized jitter, exposes online/offline state plus an accessible manual retry after exhaustion, waits for canonical acknowledgement on the reconnecting transport, and only then removes the durable command; later retry converges without a new local mutation. Canonical payload conflicts are not retried and remain durable with a visible reason. If requested history predates retained events, the stream emits a typed snapshot-required event and the client loads a versioned canonical snapshot. One deterministic rebase rule treats `reorder_card` as converged only when the canonical snapshot already places that card in `done`; it then atomically stores the snapshot/cursor and removes the satisfied command. If a later canonical command instead places the same card in `doing`, the rebase is explicitly `conflicted`, retains the local command and last committed snapshot/cursor unchanged, and exposes the divergent canonical column/reason. Two same-origin tabs coordinate an app-owned expiring IndexedDB lease so only one uploads; the standby exposes its role without issuing a request, and after the leader closes it takes over, receives one canonical acknowledgement/sequence, and removes the queue once. Each activation serializes uploads with one in flight, processes at most two acknowledged commands, exposes the retained durable count when backpressured, and resumes the next bounded run only through the visible retry action. A mixed queue commits and removes its accepted prefix exactly once, then stops on the first permanent rejection with the typed server cause visible, the rejected command plus untouched suffix durable, and blind retry disabled. The IndexedDB v1-to-v2 command migration transactionally adds the explicit target column, records a typed migration receipt, preserves causal order and interaction intent through an interrupted upload, and later drains in the original order. Every command POST now derives its principal, permission, and tenant from the current same-origin session before idempotency lookup or mutation; authenticated mode accepts only app-configured opaque session tokens and fails closed when signed out. Direct cross-tenant and stale-permission command attempts receive typed authorization denial before mutation. IndexedDB v3 additionally indexes every command and lease by the current server-derived tenant/principal partition: switched users enumerate zero foreign commands, issue no foreign replay, cannot export foreign payloads, and the owning account can export then resume its intact queue. Signed-out context lookup fails closed before opening the queue. Canonical snapshots and replayable acknowledgement history now derive the same current session, filter card state and events by tenant before serialization, use tenant-local sequence cursors, and reject signed-out reads. Broader conflict decisions remain.
|
||||
- **Build:** materialize `hemx-sync` over an integration transport with idempotent server command processing, snapshot/change cursor, durable acknowledgements, bounded ordered replay, current auth checks, rejection/conflict results, canonical replacement, reconnect jitter/backoff, multi-tab coordination, and redacted diagnostics.
|
||||
- **Refusals:** no default CRDT, transport in core, cached enqueue-time permission, unbounded queue, or silent last-write-wins policy.
|
||||
- **Requirements:** `sync/001-023`, `operations/001-005`, `security/002-005`, `performance/004-005`.
|
||||
- **Proof:** `cargo test -p hemx-kanban-example --test browser_e2e idempotent_server_command_is_acknowledged_after_reconnect -- --exact` proves duplicate POST delivery yields one identical canonical acknowledgement/sequence, conflicting id reuse is rejected, EventSource reconnects after a server-closed first stream, the acknowledgement is delivered once with its sequence as event id, and a page reload shows the authoritative card in the canonical column. `cargo test -p hemx-kanban-example --test browser_e2e pending_local_command_uploads_with_bounded_retry_and_is_removed_on_ack -- --exact` proves automatic platform-store upload, one explicit transient failure, bounded exponential backoff plus jitter, reconnect acknowledgement, pending-before-ack ordering, acknowledged removal, canonical board convergence, and non-retried 409 rejection remaining durable with a visible reason. `cargo test -p hemx-kanban-example --test browser_e2e canonical_acknowledgement_survives_server_restart -- --exact` proves the versioned store is materialized before success, a real process restart reloads the same idempotent acknowledgement/sequence, EventSource replays it by id, and canonical board state is rebuilt. `cargo test -p hemx-kanban-example --test browser_e2e exhausted_offline_retries_keep_command_until_later_reconnect -- --exact` proves three bounded retries exhaust into visible offline/manual-recovery state while the command remains durable, then a later retry acknowledges/removes it and converges canonically. `cargo test -p hemx-kanban-example --test browser_e2e missing_history_rebase_converges_without_losing_local_intent -- --exact` proves retained-history gap detection, typed/versioned snapshot fallback, deterministic already-canonical convergence, atomic snapshot/cursor commit with acknowledged removal, then a divergent canonical update producing explicit conflict while the local command and prior committed snapshot/cursor remain intact. `cargo test -p hemx-kanban-example --test browser_e2e two_tabs_coordinate_single_uploader_and_takeover_without_duplicate_application -- --exact` proves one retry-exhausted leader/one explicit standby, zero follower upload before takeover, lease takeover after the leader closes, one canonical sequence/event, one queue removal, and one board application. `cargo test -p hemx-kanban-example --test browser_e2e upload_backpressure_keeps_pending_work_visible_and_recoverable -- --exact` proves one in-flight upload, a two-acknowledgement activation limit, one retained durable command with visible recovery state, and explicit retry draining the final command without loss. `cargo test -p hemx-kanban-example --test browser_e2e mixed_queue_removes_accepted_prefix_and_retains_rejected_tail -- --exact` proves an accepted prefix is canonically applied and removed once before a permanent rejection stops processing, exposes its typed HTTP/server cause, disables blind retry, and leaves both the rejected command and untouched suffix durable. `cargo test -p hemx-kanban-example --test browser_e2e schema_upgrade_preserves_queued_order_and_local_intent -- --exact` proves a three-command v1 queue migrates atomically to the explicit-target v2 schema, remains byte-for-intent ordered after interrupted upload, then receives canonical sequences 1–3 in original order and drains without loss. `cargo test -p hemx-kanban-example --test browser_e2e account_partition_hides_replay_and_export_until_owner_returns -- --exact` proves a beta editor and alpha viewer enumerate zero commands and issue no replay for an alpha owner queue, signed-out startup cannot open a partition, no foreign id/export surface leaks, and only the returning alpha owner can export the intact command then resume it exactly once. The completed slice proof must additionally cover broader conflict decisions and authorization of snapshot/event reads.
|
||||
- **Proof:** `cargo test -p hemx-kanban-example --test browser_e2e idempotent_server_command_is_acknowledged_after_reconnect -- --exact` proves duplicate POST delivery yields one identical canonical acknowledgement/sequence, conflicting id reuse is rejected, EventSource reconnects after a server-closed first stream, the acknowledgement is delivered once with its sequence as event id, and a page reload shows the authoritative card in the canonical column. `cargo test -p hemx-kanban-example --test browser_e2e pending_local_command_uploads_with_bounded_retry_and_is_removed_on_ack -- --exact` proves automatic platform-store upload, one explicit transient failure, bounded exponential backoff plus jitter, reconnect acknowledgement, pending-before-ack ordering, acknowledged removal, canonical board convergence, and non-retried 409 rejection remaining durable with a visible reason. `cargo test -p hemx-kanban-example --test browser_e2e canonical_acknowledgement_survives_server_restart -- --exact` proves the versioned store is materialized before success, a real process restart reloads the same idempotent acknowledgement/sequence, EventSource replays it by id, and canonical board state is rebuilt. `cargo test -p hemx-kanban-example --test browser_e2e exhausted_offline_retries_keep_command_until_later_reconnect -- --exact` proves three bounded retries exhaust into visible offline/manual-recovery state while the command remains durable, then a later retry acknowledges/removes it and converges canonically. `cargo test -p hemx-kanban-example --test browser_e2e missing_history_rebase_converges_without_losing_local_intent -- --exact` proves retained-history gap detection, typed/versioned snapshot fallback, deterministic already-canonical convergence, atomic snapshot/cursor commit with acknowledged removal, then a divergent canonical update producing explicit conflict while the local command and prior committed snapshot/cursor remain intact. `cargo test -p hemx-kanban-example --test browser_e2e two_tabs_coordinate_single_uploader_and_takeover_without_duplicate_application -- --exact` proves one retry-exhausted leader/one explicit standby, zero follower upload before takeover, lease takeover after the leader closes, one canonical sequence/event, one queue removal, and one board application. `cargo test -p hemx-kanban-example --test browser_e2e upload_backpressure_keeps_pending_work_visible_and_recoverable -- --exact` proves one in-flight upload, a two-acknowledgement activation limit, one retained durable command with visible recovery state, and explicit retry draining the final command without loss. `cargo test -p hemx-kanban-example --test browser_e2e mixed_queue_removes_accepted_prefix_and_retains_rejected_tail -- --exact` proves an accepted prefix is canonically applied and removed once before a permanent rejection stops processing, exposes its typed HTTP/server cause, disables blind retry, and leaves both the rejected command and untouched suffix durable. `cargo test -p hemx-kanban-example --test browser_e2e schema_upgrade_preserves_queued_order_and_local_intent -- --exact` proves a three-command v1 queue migrates atomically to the explicit-target v2 schema, remains byte-for-intent ordered after interrupted upload, then receives canonical sequences 1–3 in original order and drains without loss. `cargo test -p hemx-kanban-example --test browser_e2e account_partition_hides_replay_and_export_until_owner_returns -- --exact` proves a beta editor and alpha viewer enumerate zero commands and issue no replay for an alpha owner queue, signed-out startup cannot open a partition, no foreign id/export surface leaks, and only the returning alpha owner can export the intact command then resume it exactly once. `cargo test -p hemx-kanban-example --test browser_e2e canonical_snapshot_and_history_are_tenant_scoped -- --exact` proves alpha and beta sessions receive only their own canonical cards and acknowledgement events with independent sequence 1 cursors, while signed-out snapshot/history reads receive 401 and expose no stream. The completed slice proof must additionally cover broader conflict decisions.
|
||||
|
||||
## Slice 5 — local-first multiplayer Kanban milestone
|
||||
|
||||
|
||||
+84
-17
@@ -145,6 +145,8 @@ struct SyncAcknowledgement {
|
||||
card_id: u64,
|
||||
canonical_column: CanonicalColumn,
|
||||
status: &'static str,
|
||||
#[serde(skip_serializing)]
|
||||
tenant: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
@@ -213,6 +215,8 @@ struct PersistedAcknowledgement {
|
||||
server_sequence: u64,
|
||||
card_id: u64,
|
||||
canonical_column: CanonicalColumn,
|
||||
#[serde(default)]
|
||||
tenant: Option<String>,
|
||||
}
|
||||
|
||||
impl SyncStore {
|
||||
@@ -227,7 +231,7 @@ impl SyncStore {
|
||||
fs::read(&self.0).map_err(|error| format!("read {}: {error}", self.0.display()))?;
|
||||
let persisted: PersistedSync = serde_json::from_slice(&bytes)
|
||||
.map_err(|error| format!("decode {}: {error}", self.0.display()))?;
|
||||
if persisted.schema_version != 1 || persisted.next_sequence == 0 {
|
||||
if !matches!(persisted.schema_version, 1 | 2) || persisted.next_sequence == 0 {
|
||||
return Err(format!("unsupported sync store {}", self.0.display()));
|
||||
}
|
||||
let mut acknowledgements = BTreeMap::new();
|
||||
@@ -237,12 +241,23 @@ impl SyncStore {
|
||||
if stored.server_sequence == 0 || stored.card_id == 0 {
|
||||
return Err(format!("invalid acknowledgement in {}", self.0.display()));
|
||||
}
|
||||
let tenant = if persisted.schema_version == 1 {
|
||||
"demo".to_owned()
|
||||
} else {
|
||||
stored
|
||||
.tenant
|
||||
.filter(|tenant| matches!(tenant.as_str(), "demo" | "alpha" | "beta"))
|
||||
.ok_or_else(|| {
|
||||
format!("invalid acknowledgement tenant in {}", self.0.display())
|
||||
})?
|
||||
};
|
||||
let acknowledgement = SyncAcknowledgement {
|
||||
command_id: stored.command_id,
|
||||
server_sequence: stored.server_sequence,
|
||||
card_id: stored.card_id,
|
||||
canonical_column: stored.canonical_column,
|
||||
status: "accepted",
|
||||
tenant,
|
||||
};
|
||||
if acknowledgements
|
||||
.insert(command_id, acknowledgement)
|
||||
@@ -271,8 +286,8 @@ impl SyncStore {
|
||||
acknowledgements.push(PersistedAcknowledgement::from(acknowledgement));
|
||||
acknowledgements.sort_by_key(|item| item.server_sequence);
|
||||
let persisted = PersistedSync {
|
||||
schema_version: 1,
|
||||
next_sequence: acknowledgement.server_sequence + 1,
|
||||
schema_version: 2,
|
||||
next_sequence: sync.next_sequence + 1,
|
||||
acknowledgements,
|
||||
};
|
||||
let bytes = serde_json::to_vec_pretty(&persisted)
|
||||
@@ -309,6 +324,7 @@ impl From<&SyncAcknowledgement> for PersistedAcknowledgement {
|
||||
server_sequence: value.server_sequence,
|
||||
card_id: value.card_id,
|
||||
canonical_column: value.canonical_column,
|
||||
tenant: Some(value.tenant.clone()),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -603,6 +619,19 @@ async fn sync_context(
|
||||
}))
|
||||
}
|
||||
|
||||
fn visible_card(principal: CurrentSyncPrincipal, card_id: u64) -> bool {
|
||||
principal.tenant == "demo"
|
||||
|| (principal.tenant == "beta" && card_id == 2)
|
||||
|| (principal.tenant == "alpha" && card_id != 2)
|
||||
}
|
||||
|
||||
fn visible_acknowledgement(
|
||||
principal: CurrentSyncPrincipal,
|
||||
acknowledgement: &SyncAcknowledgement,
|
||||
) -> bool {
|
||||
principal.tenant == "demo" || acknowledgement.tenant == principal.tenant
|
||||
}
|
||||
|
||||
fn authorize_sync_replay(
|
||||
principal: CurrentSyncPrincipal,
|
||||
card_id: u64,
|
||||
@@ -642,6 +671,11 @@ async fn sync_command(
|
||||
|
||||
let mut sync = state.sync.lock().unwrap();
|
||||
if let Some(existing) = sync.acknowledgements.get(&command_id) {
|
||||
if !visible_acknowledgement(principal, existing) {
|
||||
return Err(SyncRejection::Forbidden(
|
||||
"current tenant cannot access command acknowledgement",
|
||||
));
|
||||
}
|
||||
if existing.card_id != card_id || existing.canonical_column != canonical_column {
|
||||
return Err(SyncRejection::Conflict(
|
||||
"command_id was already used for a different payload",
|
||||
@@ -667,12 +701,21 @@ async fn sync_command(
|
||||
.iter()
|
||||
.position(|card| card.id == card_id)
|
||||
.ok_or(SyncRejection::BadRequest("unknown card_id"))?;
|
||||
let server_sequence = sync
|
||||
.acknowledgements
|
||||
.values()
|
||||
.filter(|acknowledgement| visible_acknowledgement(principal, acknowledgement))
|
||||
.map(|acknowledgement| acknowledgement.server_sequence)
|
||||
.max()
|
||||
.unwrap_or_default()
|
||||
+ 1;
|
||||
let acknowledgement = SyncAcknowledgement {
|
||||
command_id: command_id.0.clone(),
|
||||
server_sequence: sync.next_sequence,
|
||||
server_sequence,
|
||||
card_id,
|
||||
canonical_column,
|
||||
status: "accepted",
|
||||
tenant: principal.tenant.to_owned(),
|
||||
};
|
||||
if let Some(store) = &state.sync_store {
|
||||
store.persist(&sync, &acknowledgement).map_err(|error| {
|
||||
@@ -687,23 +730,35 @@ async fn sync_command(
|
||||
Ok(Json(acknowledgement))
|
||||
}
|
||||
|
||||
// req: sync/007 req: sync/020
|
||||
async fn sync_snapshot(State(state): State<Arc<AppState>>) -> Json<SyncSnapshot> {
|
||||
// req: sync/007 req: sync/020 req: auth/005 req: security/004
|
||||
async fn sync_snapshot(
|
||||
State(state): State<Arc<AppState>>,
|
||||
headers: HeaderMap,
|
||||
) -> Result<Json<SyncSnapshot>, SyncRejection> {
|
||||
let principal = current_sync_principal(&headers, &state.sync_sessions)?;
|
||||
let board = state.board.lock().unwrap();
|
||||
let sync = state.sync.lock().unwrap();
|
||||
let cards = board
|
||||
.cards
|
||||
.iter()
|
||||
.filter(|card| visible_card(principal, card.id))
|
||||
.map(|card| SnapshotCard {
|
||||
id: card.id,
|
||||
column: canonical_column(card.column),
|
||||
})
|
||||
.collect();
|
||||
Json(SyncSnapshot {
|
||||
let server_sequence = sync
|
||||
.acknowledgements
|
||||
.values()
|
||||
.filter(|acknowledgement| visible_acknowledgement(principal, acknowledgement))
|
||||
.map(|acknowledgement| acknowledgement.server_sequence)
|
||||
.max()
|
||||
.unwrap_or_default();
|
||||
Ok(Json(SyncSnapshot {
|
||||
schema_version: 1,
|
||||
server_sequence: sync.next_sequence.saturating_sub(1),
|
||||
server_sequence,
|
||||
cards,
|
||||
})
|
||||
}))
|
||||
}
|
||||
|
||||
fn canonical_column(column: usize) -> CanonicalColumn {
|
||||
@@ -714,11 +769,13 @@ fn canonical_column(column: usize) -> CanonicalColumn {
|
||||
}
|
||||
}
|
||||
|
||||
// req: sync/005 req: sync/006 req: sync/007 req: sync/013
|
||||
// req: sync/005 req: sync/006 req: sync/007 req: sync/013 req: auth/005 req: security/004
|
||||
async fn sync_acknowledgements(
|
||||
State(state): State<Arc<AppState>>,
|
||||
headers: HeaderMap,
|
||||
Query(params): Query<BTreeMap<String, String>>,
|
||||
) -> Sse<impl futures_util::Stream<Item = Result<Event, Infallible>>> {
|
||||
) -> Result<Sse<impl futures_util::Stream<Item = Result<Event, Infallible>>>, SyncRejection> {
|
||||
let principal = current_sync_principal(&headers, &state.sync_sessions)?;
|
||||
let after = params
|
||||
.get("after")
|
||||
.and_then(|value| value.parse::<u64>().ok())
|
||||
@@ -733,22 +790,31 @@ async fn sync_acknowledgements(
|
||||
let attempts = sync.reconnects.entry(key).or_default();
|
||||
*attempts += 1;
|
||||
if *attempts == 1 {
|
||||
return Sse::new(
|
||||
return Ok(Sse::new(
|
||||
stream::iter([Ok(Event::default()
|
||||
.comment("reconnect")
|
||||
.retry(Duration::from_millis(25)))])
|
||||
.boxed(),
|
||||
)
|
||||
.keep_alive(KeepAlive::default());
|
||||
.keep_alive(KeepAlive::default()));
|
||||
}
|
||||
}
|
||||
let first_available = sync
|
||||
.acknowledgements
|
||||
.values()
|
||||
.filter(|acknowledgement| acknowledgement.server_sequence > sync.retained_after)
|
||||
.filter(|acknowledgement| {
|
||||
visible_acknowledgement(principal, acknowledgement)
|
||||
&& acknowledgement.server_sequence > sync.retained_after
|
||||
})
|
||||
.map(|acknowledgement| acknowledgement.server_sequence)
|
||||
.min();
|
||||
let latest = sync.next_sequence.saturating_sub(1);
|
||||
let latest = sync
|
||||
.acknowledgements
|
||||
.values()
|
||||
.filter(|acknowledgement| visible_acknowledgement(principal, acknowledgement))
|
||||
.map(|acknowledgement| acknowledgement.server_sequence)
|
||||
.max()
|
||||
.unwrap_or_default();
|
||||
let history_missing = after < latest
|
||||
&& first_available.is_none_or(|first_sequence| after.saturating_add(1) < first_sequence);
|
||||
let events = if history_missing {
|
||||
@@ -766,7 +832,8 @@ async fn sync_acknowledgements(
|
||||
sync.acknowledgements
|
||||
.values()
|
||||
.filter(|acknowledgement| {
|
||||
acknowledgement.server_sequence > after
|
||||
visible_acknowledgement(principal, acknowledgement)
|
||||
&& acknowledgement.server_sequence > after
|
||||
&& acknowledgement.server_sequence > sync.retained_after
|
||||
})
|
||||
.map(|acknowledgement| {
|
||||
@@ -778,7 +845,7 @@ async fn sync_acknowledgements(
|
||||
})
|
||||
.collect::<Vec<Result<Event, Infallible>>>()
|
||||
};
|
||||
Sse::new(stream::iter(events).boxed()).keep_alive(KeepAlive::default())
|
||||
Ok(Sse::new(stream::iter(events).boxed()).keep_alive(KeepAlive::default()))
|
||||
}
|
||||
|
||||
fn registry(state: Arc<AppState>) -> impl DispatchRegistry {
|
||||
|
||||
@@ -598,6 +598,129 @@ async fn account_partition_hides_replay_and_export_until_owner_returns() -> WebD
|
||||
result.and(quit)
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn canonical_snapshot_and_history_are_tenant_scoped() -> WebDriverResult<()> {
|
||||
// test req: sync/007 req: security/004 req: auth/005
|
||||
let app_port = available_port();
|
||||
let app_addr = format!("127.0.0.1:{app_port}");
|
||||
let mut app_command = Command::new(env!("CARGO_BIN_EXE_hemx-kanban-example"));
|
||||
app_command
|
||||
.env("HEMX_KANBAN_ADDR", &app_addr)
|
||||
.env(
|
||||
"HEMX_KANBAN_SESSION_ALICE_ALPHA_EDITOR",
|
||||
"test-token-alice-alpha-editor",
|
||||
)
|
||||
.env(
|
||||
"HEMX_KANBAN_SESSION_BOB_ALPHA_VIEWER",
|
||||
"test-token-bob-alpha-viewer",
|
||||
)
|
||||
.env(
|
||||
"HEMX_KANBAN_SESSION_CAROL_BETA_EDITOR",
|
||||
"test-token-carol-beta-editor",
|
||||
);
|
||||
let _app = TestProcess::start(app_command, "hemx-kanban", &app_addr, STARTUP_TIMEOUT)
|
||||
.expect("start ready hemx-kanban");
|
||||
|
||||
let webdriver_port = available_port();
|
||||
let webdriver_addr = format!("127.0.0.1:{webdriver_port}");
|
||||
let mut webdriver = Command::new("geckodriver");
|
||||
webdriver.arg("--port").arg(webdriver_port.to_string());
|
||||
let _webdriver = TestProcess::start(webdriver, "geckodriver", &webdriver_addr, STARTUP_TIMEOUT)
|
||||
.expect("start ready geckodriver");
|
||||
let mut caps = DesiredCapabilities::firefox();
|
||||
caps.set_headless()?;
|
||||
let driver = WebDriver::new(&format!("http://{webdriver_addr}"), caps).await?;
|
||||
|
||||
let result = async {
|
||||
driver.goto(&format!("http://{app_addr}/")).await?;
|
||||
let proof = driver
|
||||
.execute_async(
|
||||
r#"
|
||||
const done = arguments[arguments.length - 1];
|
||||
const session = (token) => { document.cookie = `hemx_kanban_session=${token}; Path=/; SameSite=Strict`; };
|
||||
const command = (id, card) => fetch(`/sync/commands?command_id=${encodeURIComponent(id)}&card_id=${card}&column=done`, { method: 'POST' });
|
||||
(async () => {
|
||||
session('test-token-alice-alpha-editor');
|
||||
const aliceWrite = await command('alice-history:1', 1);
|
||||
session('test-token-carol-beta-editor');
|
||||
const carolWrite = await command('carol-history:1', 2);
|
||||
const betaSnapshotResponse = await fetch('/sync/snapshot', { cache: 'no-store' });
|
||||
const betaSnapshot = await betaSnapshotResponse.json();
|
||||
const betaHistoryResponse = await fetch('/sync/acknowledgements?after=0', { headers: { Accept: 'text/event-stream' }, cache: 'no-store' });
|
||||
const betaHistory = await betaHistoryResponse.text();
|
||||
session('test-token-bob-alpha-viewer');
|
||||
const alphaSnapshotResponse = await fetch('/sync/snapshot', { cache: 'no-store' });
|
||||
const alphaSnapshot = await alphaSnapshotResponse.json();
|
||||
const alphaHistoryResponse = await fetch('/sync/acknowledgements?after=0', { headers: { Accept: 'text/event-stream' }, cache: 'no-store' });
|
||||
const alphaHistory = await alphaHistoryResponse.text();
|
||||
document.cookie = 'hemx_kanban_session=; Path=/; Max-Age=0; SameSite=Strict';
|
||||
const signedOutSnapshot = await fetch('/sync/snapshot', { cache: 'no-store' });
|
||||
const signedOutHistory = await fetch('/sync/acknowledgements?after=0', { headers: { Accept: 'text/event-stream' }, cache: 'no-store' });
|
||||
done({
|
||||
aliceWrite: aliceWrite.status,
|
||||
carolWrite: carolWrite.status,
|
||||
betaSnapshotStatus: betaSnapshotResponse.status,
|
||||
betaSnapshot,
|
||||
betaHistoryStatus: betaHistoryResponse.status,
|
||||
betaHistory,
|
||||
alphaSnapshotStatus: alphaSnapshotResponse.status,
|
||||
alphaSnapshot,
|
||||
alphaHistoryStatus: alphaHistoryResponse.status,
|
||||
alphaHistory,
|
||||
signedOutSnapshotStatus: signedOutSnapshot.status,
|
||||
signedOutHistoryStatus: signedOutHistory.status,
|
||||
});
|
||||
})().catch((error) => done({ error: String(error), stack: error.stack }));
|
||||
"#,
|
||||
Vec::new(),
|
||||
)
|
||||
.await?
|
||||
.json()
|
||||
.clone();
|
||||
assert!(proof.get("error").is_none(), "browser proof failed: {proof}");
|
||||
assert_eq!(proof["aliceWrite"], 200);
|
||||
assert_eq!(proof["carolWrite"], 200);
|
||||
assert_eq!(proof["betaSnapshotStatus"], 200);
|
||||
assert_eq!(
|
||||
proof["betaSnapshot"],
|
||||
serde_json::json!({
|
||||
"schemaVersion": 1,
|
||||
"serverSequence": 1,
|
||||
"cards": [{ "id": 2, "column": "done" }]
|
||||
})
|
||||
);
|
||||
assert_eq!(proof["betaHistoryStatus"], 200);
|
||||
let beta_history = proof["betaHistory"].as_str().unwrap();
|
||||
assert!(beta_history.contains("carol-history:1"));
|
||||
assert!(!beta_history.contains("alice-history:1"));
|
||||
assert!(!beta_history.contains("\"cardId\":1"));
|
||||
|
||||
assert_eq!(proof["alphaSnapshotStatus"], 200);
|
||||
assert_eq!(
|
||||
proof["alphaSnapshot"],
|
||||
serde_json::json!({
|
||||
"schemaVersion": 1,
|
||||
"serverSequence": 1,
|
||||
"cards": [
|
||||
{ "id": 1, "column": "done" },
|
||||
{ "id": 3, "column": "done" }
|
||||
]
|
||||
})
|
||||
);
|
||||
assert_eq!(proof["alphaHistoryStatus"], 200);
|
||||
let alpha_history = proof["alphaHistory"].as_str().unwrap();
|
||||
assert!(alpha_history.contains("alice-history:1"));
|
||||
assert!(!alpha_history.contains("carol-history:1"));
|
||||
assert!(!alpha_history.contains("\"cardId\":2"));
|
||||
assert_eq!(proof["signedOutSnapshotStatus"], 401);
|
||||
assert_eq!(proof["signedOutHistoryStatus"], 401);
|
||||
Ok(())
|
||||
}
|
||||
.await;
|
||||
let quit = driver.quit().await;
|
||||
result.and(quit)
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn schema_upgrade_preserves_queued_order_and_local_intent() -> WebDriverResult<()> {
|
||||
// test req: sync/004 req: sync/010 req: sync/014
|
||||
@@ -1592,7 +1715,8 @@ async fn canonical_acknowledgement_survives_server_restart() -> WebDriverResult<
|
||||
assert!(store.is_file(), "server did not materialize sync store");
|
||||
let persisted = fs::read_to_string(&store).expect("read sync store");
|
||||
assert!(persisted.contains("restart-proof:1"));
|
||||
assert!(persisted.contains("\"schemaVersion\": 1"));
|
||||
assert!(persisted.contains("\"schemaVersion\": 2"));
|
||||
assert!(persisted.contains("\"tenant\": \"demo\""));
|
||||
|
||||
drop(first_app);
|
||||
let mut second_app_command = Command::new(env!("CARGO_BIN_EXE_hemx-kanban-example"));
|
||||
|
||||
Reference in New Issue
Block a user