|
| 1 | +//! Regression coverage for typed app-config environment overlays. |
| 2 | +
|
| 3 | +use std::fs; |
| 4 | +use std::process::{Command, Output}; |
| 5 | + |
| 6 | +use tempfile::TempDir; |
| 7 | +use toml_edit::{DocumentMut, value}; |
| 8 | + |
| 9 | +const LEGACY_CONFIG: &str = include_str!( |
| 10 | + "../../trusted-server-integration-tests/fixtures/configs/trusted-server.integration.toml" |
| 11 | +); |
| 12 | +const MANIFEST: &str = r#" |
| 13 | +[app] |
| 14 | +name = "trusted-server" |
| 15 | +
|
| 16 | +[adapters.axum.adapter] |
| 17 | +crate = "crates/trusted-server-adapter-axum" |
| 18 | +
|
| 19 | +[adapters.axum.commands] |
| 20 | +build = "echo" |
| 21 | +deploy = "echo" |
| 22 | +serve = "echo" |
| 23 | +
|
| 24 | +[stores.config] |
| 25 | +ids = ["trusted_server_config"] |
| 26 | +
|
| 27 | +[stores.secrets] |
| 28 | +ids = ["trusted_server_secrets"] |
| 29 | +"#; |
| 30 | +const REWRITE_ENV: &str = "TRUSTED_SERVER__AUCTION__REWRITE_CREATIVES"; |
| 31 | + |
| 32 | +struct MigratedProject { |
| 33 | + directory: TempDir, |
| 34 | + config_path: std::path::PathBuf, |
| 35 | + manifest_path: std::path::PathBuf, |
| 36 | +} |
| 37 | + |
| 38 | +fn migrated_legacy_project() -> MigratedProject { |
| 39 | + let directory = tempfile::tempdir().expect("should create temporary config directory"); |
| 40 | + let config_path = directory.path().join("trusted-server.toml"); |
| 41 | + let manifest_path = directory.path().join("edgezero.toml"); |
| 42 | + let mut document = LEGACY_CONFIG |
| 43 | + .parse::<DocumentMut>() |
| 44 | + .expect("should parse legacy integration config"); |
| 45 | + document["auction"]["rewrite_creatives"] = value(true); |
| 46 | + fs::write(&config_path, document.to_string()).expect("should write migrated config"); |
| 47 | + fs::write(&manifest_path, MANIFEST).expect("should write test manifest"); |
| 48 | + MigratedProject { |
| 49 | + directory, |
| 50 | + config_path, |
| 51 | + manifest_path, |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +fn validate_with_overlay(project: &MigratedProject, raw_value: &str) -> Output { |
| 56 | + Command::new(env!("CARGO_BIN_EXE_ts")) |
| 57 | + .args(["config", "validate", "--manifest"]) |
| 58 | + .arg(&project.manifest_path) |
| 59 | + .arg("--app-config") |
| 60 | + .arg(&project.config_path) |
| 61 | + .env(REWRITE_ENV, raw_value) |
| 62 | + .output() |
| 63 | + .expect("should run ts config validate") |
| 64 | +} |
| 65 | + |
| 66 | +#[test] |
| 67 | +fn migrated_legacy_config_applies_rewrite_creatives_environment_override() { |
| 68 | + let project = migrated_legacy_project(); |
| 69 | + let output = Command::new(env!("CARGO_BIN_EXE_ts")) |
| 70 | + .args(["config", "push", "--adapter", "axum", "--manifest"]) |
| 71 | + .arg(&project.manifest_path) |
| 72 | + .arg("--app-config") |
| 73 | + .arg(&project.config_path) |
| 74 | + .args(["--yes", "--no-diff"]) |
| 75 | + .env(REWRITE_ENV, "false") |
| 76 | + .output() |
| 77 | + .expect("should run ts config push"); |
| 78 | + |
| 79 | + assert!( |
| 80 | + output.status.success(), |
| 81 | + "valid boolean overlay should push successfully: {}", |
| 82 | + String::from_utf8_lossy(&output.stderr) |
| 83 | + ); |
| 84 | + |
| 85 | + let local_store_path = project |
| 86 | + .directory |
| 87 | + .path() |
| 88 | + .join(".edgezero/local-config-trusted_server_config.json"); |
| 89 | + let local_store: serde_json::Value = serde_json::from_str( |
| 90 | + &fs::read_to_string(local_store_path).expect("should read pushed local config"), |
| 91 | + ) |
| 92 | + .expect("should parse local config store"); |
| 93 | + let envelope_json = local_store |
| 94 | + .as_object() |
| 95 | + .and_then(|entries| entries.values().next()) |
| 96 | + .and_then(serde_json::Value::as_str) |
| 97 | + .expect("should contain a blob envelope"); |
| 98 | + let envelope: serde_json::Value = |
| 99 | + serde_json::from_str(envelope_json).expect("should parse blob envelope"); |
| 100 | + |
| 101 | + assert_eq!( |
| 102 | + envelope["data"]["auction"]["rewrite_creatives"], |
| 103 | + serde_json::Value::Bool(false), |
| 104 | + "pushed config should contain the environment override" |
| 105 | + ); |
| 106 | +} |
| 107 | + |
| 108 | +#[test] |
| 109 | +fn migrated_legacy_config_rejects_invalid_rewrite_creatives_environment_override() { |
| 110 | + let project = migrated_legacy_project(); |
| 111 | + let output = validate_with_overlay(&project, "not-a-boolean"); |
| 112 | + let stderr = String::from_utf8_lossy(&output.stderr); |
| 113 | + |
| 114 | + assert!( |
| 115 | + !output.status.success(), |
| 116 | + "invalid boolean overlay should fail validation" |
| 117 | + ); |
| 118 | + assert!( |
| 119 | + stderr.contains(REWRITE_ENV) && stderr.contains("boolean"), |
| 120 | + "error should identify the invalid boolean overlay: {stderr}" |
| 121 | + ); |
| 122 | +} |
0 commit comments