Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
233 changes: 233 additions & 0 deletions backend/tests/user_interests_revision_conflicts_bdd.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
//! Behavioural coverage for revision-safe interests updates against real DB wiring.

use rstest::fixture;
use rstest_bdd_macros::{given, scenario, then, when};
use uuid::Uuid;

mod support;

use support::handle_cluster_setup_failure;

#[path = "../src/server/config.rs"]
#[expect(
dead_code,
reason = "tests import ServerConfig from server_config for DB-backed HTTP flows"
)]
mod server_config;
pub(crate) use server_config::ServerConfig;

#[path = "../src/server/state_builders.rs"]
mod state_builders;

#[path = "user_interests_revision_conflicts_bdd/flow_support.rs"]
mod flow_support;

use flow_support::{
FIRST_THEME_ID, PreferencesData, SAFETY_TOGGLE_ID, SECOND_THEME_ID, THIRD_THEME_ID, World,
assert_conflict_snapshot, assert_interests_snapshot, assert_preferences_snapshot, is_skipped,
run_first_write, run_matching_revision_write, run_missing_revision_conflict,
run_preserve_non_interest_flow, run_stale_revision_conflict, seed_preferences, seed_user,
setup_db_context,
};
use support::profile_interests::FIXTURE_AUTH_ID;

#[fixture]
fn world() -> World {
World::default()
}

#[given("db-present startup mode backed by embedded postgres")]
fn db_present_startup_mode_backed_by_embedded_postgres(world: &mut World) {
match setup_db_context() {
Ok(db) => {
seed_user(
db.database_url.as_str(),
Uuid::parse_str(FIXTURE_AUTH_ID).expect("valid fixture UUID"),
"Revision Ada",
)
.expect("seed db user");
world.db = Some(db);
world.skip_reason = None;
}
Err(error) => {
let _ = handle_cluster_setup_failure::<()>(error.as_str());
world.skip_reason = Some(error);
}
}
}

#[given("existing preferences revision 1 with preserved safety and unit settings")]
fn existing_preferences_revision_1_with_preserved_safety_and_unit_settings(world: &mut World) {
if is_skipped(world) {
return;
}

let db = world.db.as_ref().expect("db context");
seed_preferences(
db.database_url.as_str(),
Uuid::parse_str(FIXTURE_AUTH_ID).expect("valid fixture UUID"),
PreferencesData::new(&[FIRST_THEME_ID], &[SAFETY_TOGGLE_ID], "imperial", 1),
)
.expect("seed user preferences");
}
Comment on lines +60 to +72
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❌ New issue: Code Duplication
The module contains 6 functions with similar structure: existing_preferences_revision_1_with_preserved_safety_and_unit_settings,existing_preferences_revision_2,the_first_interests_response_includes_revision_1,the_response_is_a_conflict_with_expected_revision_1_and_actual_revision_2 and 2 more functions

Suppress


#[given("existing preferences revision 2")]
fn existing_preferences_revision_2(world: &mut World) {
if is_skipped(world) {
return;
}

let db = world.db.as_ref().expect("db context");
seed_preferences(
db.database_url.as_str(),
Uuid::parse_str(FIXTURE_AUTH_ID).expect("valid fixture UUID"),
PreferencesData::new(&[FIRST_THEME_ID], &[SAFETY_TOGGLE_ID], "metric", 2),
)
.expect("seed user preferences");
}

#[when("the client writes interests for the first time")]
fn the_client_writes_interests_for_the_first_time(world: &mut World) {
run_first_write(world);
}

#[when("the client writes interests twice using the returned revision")]
fn the_client_writes_interests_twice_using_the_returned_revision(world: &mut World) {
run_matching_revision_write(world);
}

#[when("the client writes interests with stale expected revision 1")]
fn the_client_writes_interests_with_stale_expected_revision_1(world: &mut World) {
run_stale_revision_conflict(world);
}

#[when("the client writes interests without expected revision after preferences exist")]
fn the_client_writes_interests_without_expected_revision_after_preferences_exist(
world: &mut World,
) {
run_missing_revision_conflict(world);
}

#[when("the client updates interests and then fetches preferences")]
fn the_client_updates_interests_and_then_fetches_preferences(world: &mut World) {
run_preserve_non_interest_flow(world);
}

#[then("the first interests response includes revision 1")]
fn the_first_interests_response_includes_revision_1(world: &mut World) {
if is_skipped(world) {
return;
}

assert_interests_snapshot(
world.first_update.as_ref().expect("first update response"),
&[FIRST_THEME_ID],
1,
);
}

#[then("the second interests response includes revision 2")]
fn the_second_interests_response_includes_revision_2(world: &mut World) {
if is_skipped(world) {
return;
}

assert_interests_snapshot(
world
.second_update
.as_ref()
.expect("second update response"),
&[SECOND_THEME_ID],
2,
);
}

#[then("the response is a conflict with expected revision 1 and actual revision 2")]
fn the_response_is_a_conflict_with_expected_revision_1_and_actual_revision_2(world: &mut World) {
if is_skipped(world) {
return;
}

assert_conflict_snapshot(
world.first_update.as_ref().expect("conflict response"),
Some(1),
2,
);
}

#[then("the response is a conflict with missing expected revision and actual revision 1")]
fn the_response_is_a_conflict_with_missing_expected_revision_and_actual_revision_1(
world: &mut World,
) {
if is_skipped(world) {
return;
}

assert_conflict_snapshot(
world.first_update.as_ref().expect("conflict response"),
None,
1,
);
}

#[then("the fetched preferences preserve safety and unit settings while advancing revision 2")]
fn the_fetched_preferences_preserve_safety_and_unit_settings_while_advancing_revision_2(
world: &mut World,
) {
if is_skipped(world) {
return;
}

assert_interests_snapshot(
world.first_update.as_ref().expect("update response"),
&[THIRD_THEME_ID],
2,
);
assert_preferences_snapshot(
world.preferences.as_ref().expect("preferences response"),
&[THIRD_THEME_ID],
&[SAFETY_TOGGLE_ID],
"imperial",
2,
);
}

#[scenario(

Check failure on line 195 in backend/tests/user_interests_revision_conflicts_bdd.rs

View workflow job for this annotation

GitHub Actions / build

feature file not found: /home/runner/work/wildside/wildside/backend/tests/features/user_interests_revision_conflicts.feature
path = "tests/features/user_interests_revision_conflicts.feature",
name = "First interests write creates revision 1"
)]
fn first_interests_write_creates_revision_1(world: World) {
drop(world);
}

#[scenario(

Check failure on line 203 in backend/tests/user_interests_revision_conflicts_bdd.rs

View workflow job for this annotation

GitHub Actions / build

feature file not found: /home/runner/work/wildside/wildside/backend/tests/features/user_interests_revision_conflicts.feature
path = "tests/features/user_interests_revision_conflicts.feature",
name = "Matching expected revision advances interests revision"
)]
fn matching_expected_revision_advances_interests_revision(world: World) {
drop(world);
}

#[scenario(

Check failure on line 211 in backend/tests/user_interests_revision_conflicts_bdd.rs

View workflow job for this annotation

GitHub Actions / build

feature file not found: /home/runner/work/wildside/wildside/backend/tests/features/user_interests_revision_conflicts.feature
path = "tests/features/user_interests_revision_conflicts.feature",
name = "Stale expected revision returns a conflict"
)]
fn stale_expected_revision_returns_a_conflict(world: World) {
drop(world);
}

#[scenario(

Check failure on line 219 in backend/tests/user_interests_revision_conflicts_bdd.rs

View workflow job for this annotation

GitHub Actions / build

feature file not found: /home/runner/work/wildside/wildside/backend/tests/features/user_interests_revision_conflicts.feature
path = "tests/features/user_interests_revision_conflicts.feature",
name = "Missing expected revision after preferences exist returns a conflict"
)]
fn missing_expected_revision_after_preferences_exist_returns_a_conflict(world: World) {
drop(world);
}

#[scenario(

Check failure on line 227 in backend/tests/user_interests_revision_conflicts_bdd.rs

View workflow job for this annotation

GitHub Actions / build

feature file not found: /home/runner/work/wildside/wildside/backend/tests/features/user_interests_revision_conflicts.feature
path = "tests/features/user_interests_revision_conflicts.feature",
name = "Interests updates preserve non-interest preferences fields"
)]
fn interests_updates_preserve_non_interest_preferences_fields(world: World) {
drop(world);
}
124 changes: 124 additions & 0 deletions backend/tests/user_interests_revision_conflicts_bdd/db_support.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
//! DB bootstrap helpers for revision-safe interests BDD coverage.

use pg_embedded_setup_unpriv::TemporaryDatabase;
use postgres::{Client, NoTls};
use uuid::Uuid;

use backend::outbound::persistence::{DbPool, PoolConfig};

use super::super::support::atexit_cleanup::shared_cluster_handle;
use super::super::support::{format_postgres_error, provision_template_database};

pub(crate) struct DbContext {
pub(crate) database_url: String,
pub(crate) pool: DbPool,
_database: TemporaryDatabase,
}

#[derive(Default)]
pub(crate) struct World {
pub(crate) db: Option<DbContext>,
pub(crate) first_update: Option<super::Snapshot>,
pub(crate) second_update: Option<super::Snapshot>,
pub(crate) preferences: Option<super::Snapshot>,
pub(crate) skip_reason: Option<String>,
}

pub(crate) fn is_skipped(world: &World) -> bool {
if let Some(reason) = world.skip_reason.as_deref() {
eprintln!("SKIP-TEST-CLUSTER: scenario skipped ({reason})");
true
} else {
false
}
}

pub(crate) fn setup_db_context() -> Result<DbContext, String> {
let cluster = shared_cluster_handle().map_err(|error| error.to_string())?;
let database = provision_template_database(cluster).map_err(|error| error.to_string())?;
let database_url = database.url().to_owned();
let pool = super::run_async(DbPool::new(
PoolConfig::new(database_url.as_str())
.with_max_size(2)
.with_min_idle(Some(1)),
))
.map_err(|error| error.to_string())?;
Ok(DbContext {
database_url,
pool,
_database: database,
})
}

pub(crate) fn seed_user(url: &str, user_id: Uuid, display_name: &str) -> Result<(), String> {
let mut client = Client::connect(url, NoTls).map_err(|error| format_postgres_error(&error))?;
client
.execute(
"INSERT INTO users (id, display_name) VALUES ($1, $2)",
&[&user_id, &display_name],
)
.map_err(|error| format_postgres_error(&error))
.map(|_| ())
}

/// Encapsulates user preferences data for test seeding.
pub(crate) struct PreferencesData<'a> {
pub(crate) interest_ids: &'a [&'a str],
pub(crate) safety_ids: &'a [&'a str],
pub(crate) unit_system: &'a str,
pub(crate) revision: i32,
}

impl<'a> PreferencesData<'a> {
/// Create new preferences data for seeding.
pub(crate) fn new(
interest_ids: &'a [&'a str],
safety_ids: &'a [&'a str],
unit_system: &'a str,
revision: i32,
) -> Self {
Self {
interest_ids,
safety_ids,
unit_system,
revision,
}
}
}

pub(crate) fn seed_preferences(
url: &str,
user_id: Uuid,
preferences: PreferencesData<'_>,
) -> Result<(), String> {
let mut client = Client::connect(url, NoTls).map_err(|error| format_postgres_error(&error))?;
let interest_ids = preferences
.interest_ids
.iter()
.map(|value| Uuid::parse_str(value).expect("valid interest UUID"))
.collect::<Vec<_>>();
let safety_ids = preferences
.safety_ids
.iter()
.map(|value| Uuid::parse_str(value).expect("valid safety UUID"))
.collect::<Vec<_>>();
client
.execute(
"INSERT INTO user_preferences (
user_id,
interest_theme_ids,
safety_toggle_ids,
unit_system,
revision
) VALUES ($1, $2, $3, $4, $5)",
&[
&user_id,
&interest_ids,
&safety_ids,
&preferences.unit_system,
&preferences.revision,
],
)
.map_err(|error| format_postgres_error(&error))
.map(|_| ())
}
Loading
Loading