boxmux 0.240.3373

YAML-driven terminal UI framework for rich, interactive CLI applications and dashboards with PTY support
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
#[cfg(test)]
mod comprehensive_variable_tests {
    use crate::model::app::load_app_from_yaml;
    use std::env;
    use std::fs;
    use tempfile::NamedTempFile;

    /// Test that would have caught the "variables ignored" issue
    #[test]
    fn test_app_variables_actually_substituted_in_content() {
        let yaml_content = r#"
app:
  variables:
    TEST_APP_VAR: "app_variable_value"
  layouts:
    - id: 'main'
      root: true
      title: 'Test'
      children:
        - id: 'muxbox1'
          title: 'MuxBox'
          position: {x1: 10%, y1: 10%, x2: 90%, y2: 90%}
          content: 'App var should resolve to: ${TEST_APP_VAR}'
"#;

        let temp_file = NamedTempFile::new().expect("Failed to create temp file");
        fs::write(&temp_file, yaml_content).expect("Failed to write temp file");

        let result = load_app_from_yaml(temp_file.path().to_str().unwrap());
        assert!(result.is_ok(), "YAML loading failed: {:?}", result.err());

        let app = result.unwrap();
        let muxbox = &app.layouts[0].children.as_ref().unwrap()[0];

        // This test SHOULD FAIL with current implementation
        // MuxBox content should have substituted variable, not literal text
        let content = muxbox.content.as_ref().unwrap();
        assert!(
            content.contains("app_variable_value"),
            "FAILED: App variable not substituted in muxbox content. Got: '{}'",
            content
        );
        assert!(
            !content.contains("${TEST_APP_VAR}"),
            "FAILED: Variable placeholder still present. Got: '{}'",
            content
        );
    }

    /// Test that would have caught the "muxbox variables ignored" issue  
    #[test]
    fn test_muxbox_variables_actually_substituted_in_scripts() {
        let yaml_content = r#"
app:
  layouts:
    - id: 'main'
      root: true
      title: 'Test'
      children:
        - id: 'muxbox1'
          title: 'MuxBox'
          position: {x1: 10%, y1: 10%, x2: 90%, y2: 90%}
          variables:
            MUXBOX_LOCAL_VAR: "muxbox_specific_value"
          script:
            - "echo 'MuxBox var: ${MUXBOX_LOCAL_VAR}'"
"#;

        let temp_file = NamedTempFile::new().expect("Failed to create temp file");
        fs::write(&temp_file, yaml_content).expect("Failed to write temp file");

        let result = load_app_from_yaml(temp_file.path().to_str().unwrap());
        assert!(result.is_ok(), "YAML loading failed: {:?}", result.err());

        let app = result.unwrap();
        let muxbox = &app.layouts[0].children.as_ref().unwrap()[0];

        // This test SHOULD FAIL with current implementation
        let script_command = &muxbox.script.as_ref().unwrap()[0];
        assert!(
            script_command.contains("muxbox_specific_value"),
            "FAILED: MuxBox variable not substituted in script. Got: '{}'",
            script_command
        );
        assert!(
            !script_command.contains("${MUXBOX_LOCAL_VAR}"),
            "FAILED: Variable placeholder still present. Got: '{}'",
            script_command
        );
    }

    /// Test that would have caught the "variable precedence" issue
    #[test]
    fn test_variable_precedence_order() {
        // Set environment variable
        env::set_var("PRECEDENCE_TEST_VAR", "env_value");

        let yaml_content = r#"
app:
  variables:
    PRECEDENCE_TEST_VAR: "app_value"
  layouts:
    - id: 'main'
      root: true
      title: 'Test'
      children:
        - id: 'muxbox1'
          title: 'MuxBox'
          position: {x1: 10%, y1: 10%, x2: 90%, y2: 90%}
          variables:
            PRECEDENCE_TEST_VAR: "muxbox_value"
          content: 'Value: ${PRECEDENCE_TEST_VAR}'
"#;

        let temp_file = NamedTempFile::new().expect("Failed to create temp file");
        fs::write(&temp_file, yaml_content).expect("Failed to write temp file");

        let result = load_app_from_yaml(temp_file.path().to_str().unwrap());
        assert!(result.is_ok(), "YAML loading failed: {:?}", result.err());

        let app = result.unwrap();
        let muxbox = &app.layouts[0].children.as_ref().unwrap()[0];

        // MuxBox-local should win over app and environment
        let content = muxbox.content.as_ref().unwrap();
        assert!(
            content.contains("muxbox_value"),
            "FAILED: MuxBox-local variable should have highest precedence. Got: '{}'",
            content
        );
        assert!(
            !content.contains("app_value") && !content.contains("env_value"),
            "FAILED: Wrong precedence - should use muxbox value. Got: '{}'",
            content
        );

        env::remove_var("PRECEDENCE_TEST_VAR");
    }

    /// Test that would have caught "nested variables" issue
    #[test]
    fn test_nested_variables_not_supported() {
        let yaml_content = r#"
app:
  variables:
    DEFAULT_USER: "fallback_user"
  layouts:
    - id: 'main'
      root: true
      title: 'Test'
      children:
        - id: 'muxbox1'
          title: 'MuxBox'
          position: {x1: 10%, y1: 10%, x2: 90%, y2: 90%}
          content: 'User: ${USER:${DEFAULT_USER}}'
"#;

        let temp_file = NamedTempFile::new().expect("Failed to create temp file");
        fs::write(&temp_file, yaml_content).expect("Failed to write temp file");

        let result = load_app_from_yaml(temp_file.path().to_str().unwrap());

        // This should either:
        // 1. Fail gracefully with a clear error message, OR
        // 2. Handle nested variables correctly
        // Currently it produces malformed output

        if result.is_ok() {
            let app = result.unwrap();
            let muxbox = &app.layouts[0].children.as_ref().unwrap()[0];
            let content = muxbox.content.as_ref().unwrap();

            // Should not contain malformed substitution
            assert!(
                !content.contains("${DEFAULT_USER}}"),
                "FAILED: Malformed nested variable substitution. Got: '{}'",
                content
            );

            // Should either substitute correctly or show clear error
            assert!(
                content.contains("fallback_user")
                    || content.contains("error")
                    || content.contains("invalid"),
                "FAILED: Nested variables should be handled gracefully. Got: '{}'",
                content
            );
        } else {
            // If it fails, error should be descriptive
            let error = result.err().unwrap().to_string();
            assert!(
                error.contains("nested") || error.contains("variable") || error.contains("syntax"),
                "FAILED: Error message should be descriptive for nested variables. Got: '{}'",
                error
            );
        }
    }

    /// Test that would have caught "variables only work via environment" issue
    #[test]
    fn test_yaml_variables_work_without_environment() {
        // Explicitly clear environment to ensure we're not relying on it
        let vars_to_clear = ["TEST_YAML_ONLY", "APP_NAME", "MUXBOX_VAR"];
        for var in &vars_to_clear {
            env::remove_var(var);
        }

        let yaml_content = r#"
app:
  variables:
    TEST_YAML_ONLY: "yaml_only_value"
    APP_NAME: "Pure YAML App"
  layouts:
    - id: 'main'
      root: true
      title: '${APP_NAME}'
      children:
        - id: 'muxbox1'
          title: 'MuxBox'
          position: {x1: 10%, y1: 10%, x2: 90%, y2: 90%}
          variables:
            MUXBOX_VAR: "muxbox_only_value"
          content: 'App: ${TEST_YAML_ONLY}, MuxBox: ${MUXBOX_VAR}'
"#;

        let temp_file = NamedTempFile::new().expect("Failed to create temp file");
        fs::write(&temp_file, yaml_content).expect("Failed to write temp file");

        let result = load_app_from_yaml(temp_file.path().to_str().unwrap());
        assert!(result.is_ok(), "YAML loading failed: {:?}", result.err());

        let app = result.unwrap();

        // Test layout title substitution
        assert_eq!(
            app.layouts[0].title.as_ref().unwrap(),
            "Pure YAML App",
            "FAILED: App variable not substituted in layout title"
        );

        // Test muxbox content substitution
        let muxbox = &app.layouts[0].children.as_ref().unwrap()[0];
        let content = muxbox.content.as_ref().unwrap();

        assert!(
            content.contains("yaml_only_value") && content.contains("muxbox_only_value"),
            "FAILED: YAML variables not substituted without environment. Got: '{}'",
            content
        );
    }

    /// Test that would have caught "defaults not working" issue
    #[test]
    fn test_default_values_actually_used() {
        // Clear environment to ensure defaults are used
        env::remove_var("MISSING_VAR");
        env::remove_var("ANOTHER_MISSING_VAR");

        let yaml_content = r#"
app:
  layouts:
    - id: 'main'
      root: true
      title: 'Test'
      children:
        - id: 'muxbox1'
          title: 'MuxBox'
          position: {x1: 10%, y1: 10%, x2: 90%, y2: 90%}
          script:
            - "echo 'Value1: ${MISSING_VAR:default_one}'"
            - "echo 'Value2: ${ANOTHER_MISSING_VAR:default_two}'"
"#;

        let temp_file = NamedTempFile::new().expect("Failed to create temp file");
        fs::write(&temp_file, yaml_content).expect("Failed to write temp file");

        let result = load_app_from_yaml(temp_file.path().to_str().unwrap());
        assert!(result.is_ok(), "YAML loading failed: {:?}", result.err());

        let app = result.unwrap();
        let muxbox = &app.layouts[0].children.as_ref().unwrap()[0];
        let script = muxbox.script.as_ref().unwrap();

        assert!(
            script[0].contains("default_one"),
            "FAILED: Default value not used for first missing var. Got: '{}'",
            script[0]
        );
        assert!(
            script[1].contains("default_two"),
            "FAILED: Default value not used for second missing var. Got: '{}'",
            script[1]
        );

        // Should not contain variable placeholders
        assert!(
            !script[0].contains("${MISSING_VAR") && !script[1].contains("${ANOTHER_MISSING_VAR"),
            "FAILED: Variable placeholders still present in scripts"
        );
    }

    /// Test that would have caught "script vs content vs title" substitution gaps
    #[test]
    fn test_variables_substituted_in_all_fields() {
        let yaml_content = r#"
app:
  variables:
    GLOBAL_VAR: "global_value"
  layouts:
    - id: 'main'
      root: true
      title: 'Layout: ${GLOBAL_VAR}'
      children:
        - id: 'muxbox1'
          title: 'MuxBox: ${GLOBAL_VAR}'
          position: {x1: 10%, y1: 10%, x2: 90%, y2: 90%}
          content: 'Content: ${GLOBAL_VAR}'
          script:
            - "echo 'Script: ${GLOBAL_VAR}'"
          redirect_output: 'muxbox2'
        - id: 'muxbox2'
          title: 'Output MuxBox'
          position: {x1: 10%, y1: 50%, x2: 90%, y2: 90%}
          content: 'Redirect target'
"#;

        let temp_file = NamedTempFile::new().expect("Failed to create temp file");
        fs::write(&temp_file, yaml_content).expect("Failed to write temp file");

        let result = load_app_from_yaml(temp_file.path().to_str().unwrap());
        assert!(result.is_ok(), "YAML loading failed: {:?}", result.err());

        let app = result.unwrap();
        let layout = &app.layouts[0];
        let muxbox1 = &layout.children.as_ref().unwrap()[0];

        // Test substitution in all field types
        assert_eq!(
            layout.title.as_ref().unwrap(),
            "Layout: global_value",
            "FAILED: Variable not substituted in layout title"
        );

        assert_eq!(
            muxbox1.title.as_ref().unwrap(),
            "MuxBox: global_value",
            "FAILED: Variable not substituted in muxbox title"
        );

        assert_eq!(
            muxbox1.content.as_ref().unwrap(),
            "Content: global_value",
            "FAILED: Variable not substituted in muxbox content"
        );

        let script = muxbox1.script.as_ref().unwrap();
        assert!(
            script[0].contains("global_value"),
            "FAILED: Variable not substituted in script. Got: '{}'",
            script[0]
        );
    }

    /// Test that would have caught "empty defaults" edge case
    #[test]
    fn test_empty_default_values() {
        env::remove_var("EMPTY_DEFAULT_VAR");

        let yaml_content = r#"
app:
  layouts:
    - id: 'main'
      root: true
      title: 'Test'
      children:
        - id: 'muxbox1'
          title: 'MuxBox'
          position: {x1: 10%, y1: 10%, x2: 90%, y2: 90%}
          content: 'Before${EMPTY_DEFAULT_VAR:}After'
"#;

        let temp_file = NamedTempFile::new().expect("Failed to create temp file");
        fs::write(&temp_file, yaml_content).expect("Failed to write temp file");

        let result = load_app_from_yaml(temp_file.path().to_str().unwrap());
        assert!(result.is_ok(), "YAML loading failed: {:?}", result.err());

        let app = result.unwrap();
        let muxbox = &app.layouts[0].children.as_ref().unwrap()[0];
        let content = muxbox.content.as_ref().unwrap();

        assert_eq!(
            content, "BeforeAfter",
            "FAILED: Empty default value not handled correctly. Got: '{}'",
            content
        );
    }

    /// Test that would have caught "malformed regex" or "special characters" issues
    #[test]
    fn test_special_characters_in_variable_values() {
        let yaml_content = r#"
app:
  variables:
    SPECIAL_CHARS: "value with spaces, $pecial chars & symbols!"
    PATH_LIKE: "/usr/bin:/usr/local/bin"
    QUOTED_VALUE: '"quoted string"'
  layouts:
    - id: 'main'
      root: true
      title: 'Test'
      children:
        - id: 'muxbox1'
          title: 'MuxBox'
          position: {x1: 10%, y1: 10%, x2: 90%, y2: 90%}
          content: 'Special: ${SPECIAL_CHARS}, Path: ${PATH_LIKE}, Quoted: ${QUOTED_VALUE}'
"#;

        let temp_file = NamedTempFile::new().expect("Failed to create temp file");
        fs::write(&temp_file, yaml_content).expect("Failed to write temp file");

        let result = load_app_from_yaml(temp_file.path().to_str().unwrap());
        assert!(result.is_ok(), "YAML loading failed: {:?}", result.err());

        let app = result.unwrap();
        let muxbox = &app.layouts[0].children.as_ref().unwrap()[0];
        let content = muxbox.content.as_ref().unwrap();

        assert!(
            content.contains("value with spaces, $pecial chars & symbols!"),
            "FAILED: Special characters not preserved. Got: '{}'",
            content
        );
        assert!(
            content.contains("/usr/bin:/usr/local/bin"),
            "FAILED: Path-like value not preserved. Got: '{}'",
            content
        );
        assert!(
            content.contains("\"quoted string\""),
            "FAILED: Quoted value not preserved. Got: '{}'",
            content
        );
    }
}