braze-sync 0.9.0

GitOps CLI for managing Braze configuration as code
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
//! Content Block file I/O.
//!
//! Each `.liquid` file is YAML frontmatter followed by the Liquid body.
//! Single-file-with-frontmatter (rather than a directory) is deliberate
//! so that the body starts on line 1 — operators editing templates
//! shouldn't have to scroll past `tags:` to see what they're changing.

use crate::error::{Error, Result};
use crate::fs::{frontmatter, try_read_resource_dir, validate_resource_name, write_atomic};
use crate::resource::{ContentBlock, ContentBlockState};
use serde::{Deserialize, Serialize};
use std::path::Path;

const FILE_EXT: &str = "liquid";

/// On-disk wire shape. Kept private so the file layout can change
/// without affecting consumers of the domain [`ContentBlock`].
#[derive(Debug, Serialize, Deserialize)]
struct Frontmatter {
    name: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    description: Option<String>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    tags: Vec<String>,
    // Local-only documentation field. Excluded from
    // `diff::content_block::syncable_eq` and from the update wire body
    // (see `src/diff/content_block.rs` module docs for the
    // "infinite drift" rationale). Optional on disk so a fresh export
    // — which cannot observe real remote state because `/info` does
    // not return it — writes no `state:` line instead of lying with
    // the default `active`. Operator-authored `state: draft` is still
    // round-tripped because `save_content_block` only omits the field
    // when its value equals the type default.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    state: Option<ContentBlockState>,
}

/// Load every `.liquid` file directly under `root`, sorted by name.
/// Missing root is not an error. Each file's stem must match its
/// frontmatter `name:` — divergence is treated as a hard parse error.
pub fn load_all_content_blocks(root: &Path) -> Result<Vec<ContentBlock>> {
    let Some(read_dir) = try_read_resource_dir(root, "content_blocks")? else {
        return Ok(Vec::new());
    };

    let mut blocks = Vec::new();
    for entry in read_dir {
        let entry = entry?;
        let path = entry.path();
        if !entry.file_type()?.is_file() {
            tracing::debug!(path = %path.display(), "skipping non-file entry");
            continue;
        }
        if path.extension().and_then(|e| e.to_str()) != Some(FILE_EXT) {
            continue;
        }
        let stem = path
            .file_stem()
            .and_then(|s| s.to_str())
            .unwrap_or_default()
            .to_string();
        let cb = read_content_block_file(&path)?;
        if cb.name != stem {
            return Err(Error::InvalidFormat {
                path: path.clone(),
                message: format!(
                    "content block name '{}' does not match its file stem '{}'",
                    cb.name, stem
                ),
            });
        }
        blocks.push(cb);
    }

    blocks.sort_by(|a, b| a.name.cmp(&b.name));
    Ok(blocks)
}

/// Read a single `.liquid` file. Does not validate that the file stem
/// matches `name`; callers do that.
pub fn read_content_block_file(path: &Path) -> Result<ContentBlock> {
    let text = std::fs::read_to_string(path)?;
    let (fm, body): (Frontmatter, &str) = frontmatter::parse(path, &text)?;
    Ok(ContentBlock {
        name: fm.name,
        description: fm.description,
        content: body.to_string(),
        tags: fm.tags,
        // Missing `state:` on disk → default (Active). Keeps the
        // domain type non-Optional so diff/apply callers don't need
        // an unwrap.
        state: fm.state.unwrap_or_default(),
    })
}

/// Write `cb` to `<root>/<cb.name>.liquid`. Names containing path
/// separators or `..` are rejected as defence in depth.
pub fn save_content_block(root: &Path, cb: &ContentBlock) -> Result<()> {
    validate_resource_name("content block", &cb.name)?;
    let path = root.join(format!("{}.{FILE_EXT}", cb.name));

    // `state` is only emitted when it differs from the type default.
    // Rationale: `braze::content_block::get_content_block` hard-codes
    // `state: Active` because Braze's `/info` endpoint does not return
    // state, so a fresh `export` cannot know whether a block is really
    // Active or Draft. Writing the default would turn an unknown into
    // a confident lie in the file. Operator-authored `state: draft`
    // still round-trips because Draft is a non-default value and gets
    // serialized explicitly.
    let fm = Frontmatter {
        name: cb.name.clone(),
        description: cb.description.clone(),
        tags: cb.tags.clone(),
        state: if cb.state == ContentBlockState::default() {
            None
        } else {
            Some(cb.state)
        },
    };
    // Body is written byte-exact. A previous version unconditionally
    // appended `\n` here, which caused any Braze block whose stored
    // content lacked a trailing newline to round-trip as `body\n`,
    // diff as Modified, and (if Braze normalizes trailing whitespace
    // on store) loop forever. `frontmatter::render` already terminates
    // the closing fence with `\n`, so an empty body still produces a
    // valid file; only the no-trailing-newline body case is affected.
    let text = frontmatter::render(&path, &fm, &cb.content)?;
    write_atomic(&path, text.as_bytes())?;
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    fn cb(name: &str, content: &str) -> ContentBlock {
        ContentBlock {
            name: name.into(),
            description: Some(format!("desc for {name}")),
            content: content.into(),
            tags: vec!["t1".into()],
            state: ContentBlockState::Active,
        }
    }

    #[test]
    fn round_trip_single_block() {
        let dir = tempfile::tempdir().unwrap();
        let original = cb("promo", "Hello {{ user.${first_name} }}\n");
        save_content_block(dir.path(), &original).unwrap();
        let loaded = load_all_content_blocks(dir.path()).unwrap();
        assert_eq!(loaded.len(), 1);
        assert_eq!(loaded[0], original);
    }

    #[test]
    fn save_creates_root_directory() {
        let dir = tempfile::tempdir().unwrap();
        let nested = dir.path().join("braze").join("content_blocks");
        save_content_block(&nested, &cb("nested", "x")).unwrap();
        assert!(nested.join("nested.liquid").is_file());
    }

    #[test]
    fn load_sorts_alphabetically() {
        let dir = tempfile::tempdir().unwrap();
        save_content_block(dir.path(), &cb("zebra", "z")).unwrap();
        save_content_block(dir.path(), &cb("apple", "a")).unwrap();
        save_content_block(dir.path(), &cb("mango", "m")).unwrap();
        let loaded = load_all_content_blocks(dir.path()).unwrap();
        assert_eq!(
            loaded.iter().map(|c| c.name.as_str()).collect::<Vec<_>>(),
            vec!["apple", "mango", "zebra"]
        );
    }

    #[test]
    fn missing_root_returns_empty() {
        let dir = tempfile::tempdir().unwrap();
        let nonexistent = dir.path().join("not_here");
        let loaded = load_all_content_blocks(&nonexistent).unwrap();
        assert!(loaded.is_empty());
    }

    #[test]
    fn empty_root_returns_empty() {
        let dir = tempfile::tempdir().unwrap();
        let loaded = load_all_content_blocks(dir.path()).unwrap();
        assert!(loaded.is_empty());
    }

    #[test]
    fn root_pointing_at_a_file_is_rejected() {
        let dir = tempfile::tempdir().unwrap();
        let file_path = dir.path().join("not_a_dir");
        std::fs::write(&file_path, "x").unwrap();
        let err = load_all_content_blocks(&file_path).unwrap_err();
        assert!(matches!(err, Error::InvalidFormat { .. }), "got {err:?}");
    }

    #[test]
    fn non_liquid_files_are_ignored() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join("README.md"), "# notes\n").unwrap();
        std::fs::write(dir.path().join("notes.txt"), "irrelevant\n").unwrap();
        save_content_block(dir.path(), &cb("real", "body")).unwrap();
        let loaded = load_all_content_blocks(dir.path()).unwrap();
        assert_eq!(loaded.len(), 1);
        assert_eq!(loaded[0].name, "real");
    }

    #[test]
    fn name_mismatch_with_file_stem_is_an_error() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(
            dir.path().join("on_disk_name.liquid"),
            "---\nname: in_yaml_name\nstate: active\n---\nbody\n",
        )
        .unwrap();
        let err = load_all_content_blocks(dir.path()).unwrap_err();
        match err {
            Error::InvalidFormat { message, .. } => {
                assert!(message.contains("on_disk_name"));
                assert!(message.contains("in_yaml_name"));
            }
            other => panic!("expected InvalidFormat, got {other:?}"),
        }
    }

    #[test]
    fn missing_state_field_defaults_to_active() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(
            dir.path().join("legacy.liquid"),
            "---\nname: legacy\n---\nold body\n",
        )
        .unwrap();
        let loaded = load_all_content_blocks(dir.path()).unwrap();
        assert_eq!(loaded.len(), 1);
        assert_eq!(loaded[0].state, ContentBlockState::Active);
        assert_eq!(loaded[0].content, "old body\n");
    }

    #[test]
    fn unknown_frontmatter_field_is_ignored_for_forward_compat() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(
            dir.path().join("future.liquid"),
            "---\nname: future\nfuture_v2_field: surprise\nstate: active\n---\nbody\n",
        )
        .unwrap();
        let loaded = load_all_content_blocks(dir.path()).unwrap();
        assert_eq!(loaded.len(), 1);
        assert_eq!(loaded[0].name, "future");
    }

    #[test]
    fn save_rejects_path_traversal_in_name() {
        let dir = tempfile::tempdir().unwrap();
        for bad in ["../evil", "..", ".", "", "a/b", "a\\b"] {
            let bad_cb = ContentBlock {
                name: bad.into(),
                description: None,
                content: String::new(),
                tags: vec![],
                state: ContentBlockState::Active,
            };
            let err = save_content_block(dir.path(), &bad_cb).unwrap_err();
            assert!(
                matches!(err, Error::InvalidFormat { .. }),
                "name {bad:?} should be rejected; got {err:?}"
            );
        }
    }

    #[test]
    fn save_overwrites_existing_file() {
        let dir = tempfile::tempdir().unwrap();
        save_content_block(dir.path(), &cb("ovr", "v1\n")).unwrap();
        save_content_block(dir.path(), &cb("ovr", "v2\n")).unwrap();
        let loaded = load_all_content_blocks(dir.path()).unwrap();
        assert_eq!(loaded.len(), 1);
        assert_eq!(loaded[0].content, "v2\n");
    }

    #[test]
    fn body_without_trailing_newline_round_trips_byte_exact() {
        // Regression: a previous save_content_block appended `\n`
        // unconditionally, so a Braze block whose stored content was
        // `Hello` (no terminator) would reload as `Hello\n` and diff
        // as Modified forever. The fix preserves body bytes verbatim.
        let dir = tempfile::tempdir().unwrap();
        let original = ContentBlock {
            name: "no_eol".into(),
            description: None,
            content: "Hello".into(),
            tags: vec![],
            state: ContentBlockState::Active,
        };
        save_content_block(dir.path(), &original).unwrap();
        let loaded = load_all_content_blocks(dir.path()).unwrap();
        assert_eq!(loaded.len(), 1);
        assert_eq!(loaded[0].content, "Hello");
        assert_eq!(loaded[0], original);
    }

    #[test]
    fn multiline_body_without_trailing_newline_round_trips() {
        // Same fidelity guarantee for the harder case where the body
        // contains internal newlines but no terminator.
        let dir = tempfile::tempdir().unwrap();
        let original = ContentBlock {
            name: "multi".into(),
            description: None,
            content: "line one\nline two".into(),
            tags: vec![],
            state: ContentBlockState::Active,
        };
        save_content_block(dir.path(), &original).unwrap();
        let loaded = load_all_content_blocks(dir.path()).unwrap();
        assert_eq!(loaded[0].content, "line one\nline two");
    }

    #[test]
    fn empty_body_round_trips() {
        let dir = tempfile::tempdir().unwrap();
        let empty_body = ContentBlock {
            name: "blank".into(),
            description: None,
            content: String::new(),
            tags: vec![],
            state: ContentBlockState::Active,
        };
        save_content_block(dir.path(), &empty_body).unwrap();
        let loaded = load_all_content_blocks(dir.path()).unwrap();
        assert_eq!(loaded[0], empty_body);
    }

    #[test]
    fn save_with_default_state_does_not_emit_state_line() {
        // Honesty guard: `get_content_block` hard-codes state=Active
        // because Braze's /info doesn't return state, so a fresh
        // export cannot know the real value. Writing `state: active`
        // would be a confident lie about a genuinely unknown field.
        // The fix is to skip the line for the type default and let
        // `read_content_block_file` re-default on load.
        let dir = tempfile::tempdir().unwrap();
        let active = ContentBlock {
            name: "exported".into(),
            description: None,
            content: "Hello\n".into(),
            tags: vec![],
            state: ContentBlockState::Active,
        };
        save_content_block(dir.path(), &active).unwrap();
        let text = std::fs::read_to_string(dir.path().join("exported.liquid")).unwrap();
        assert!(
            !text.contains("state:"),
            "default-state file should not carry a state line; got:\n{text}"
        );
        // And it still round-trips (load defaults missing state to Active).
        let loaded = load_all_content_blocks(dir.path()).unwrap();
        assert_eq!(loaded[0], active);
    }

    #[test]
    fn save_with_draft_state_emits_state_line_and_round_trips() {
        // Counterpart to the honesty guard: operator-authored
        // `state: draft` must still persist, otherwise the local
        // annotation feature documented in the README is a fiction.
        // Draft is the non-default value, so serialization keeps it.
        let dir = tempfile::tempdir().unwrap();
        let draft = ContentBlock {
            name: "wip".into(),
            description: None,
            content: "work in progress\n".into(),
            tags: vec![],
            state: ContentBlockState::Draft,
        };
        save_content_block(dir.path(), &draft).unwrap();
        let text = std::fs::read_to_string(dir.path().join("wip.liquid")).unwrap();
        assert!(
            text.contains("state: draft"),
            "draft state should be serialized; got:\n{text}"
        );
        let loaded = load_all_content_blocks(dir.path()).unwrap();
        assert_eq!(loaded[0], draft);
    }

    #[test]
    fn load_file_without_state_line_defaults_to_active() {
        // Symmetric guard for `save_with_default_state_does_not_emit_state_line`:
        // after that change, the canonical on-disk shape for an Active
        // block has no `state:` line at all. Loading must default, not
        // fail. Overlaps with `missing_state_field_defaults_to_active`
        // but pins the exact "no line present" shape that the new save
        // path produces, so a future regression that re-introduces
        // state serialization can't sneak past the round-trip tests.
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(
            dir.path().join("stateless.liquid"),
            "---\nname: stateless\n---\nbody\n",
        )
        .unwrap();
        let loaded = load_all_content_blocks(dir.path()).unwrap();
        assert_eq!(loaded.len(), 1);
        assert_eq!(loaded[0].state, ContentBlockState::Active);
    }
}