File size: 10,751 Bytes
e5034c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
use std::sync::Arc;

use forge_domain::{TerminalCommand, TerminalContext};

use crate::EnvironmentInfra;

/// Environment variable exported by the zsh plugin containing
/// `\x1F`-separated (ASCII Unit Separator) command strings.
pub const ENV_TERM_COMMANDS: &str = "_FORGE_TERM_COMMANDS";

/// Environment variable exported by the zsh plugin containing
/// `\x1F`-separated exit codes corresponding to [`ENV_TERM_COMMANDS`].
pub const ENV_TERM_EXIT_CODES: &str = "_FORGE_TERM_EXIT_CODES";

/// Environment variable exported by the zsh plugin containing
/// `\x1F`-separated Unix timestamps corresponding to [`ENV_TERM_COMMANDS`].
pub const ENV_TERM_TIMESTAMPS: &str = "_FORGE_TERM_TIMESTAMPS";

/// The separator used to join and split environment variable lists.
///
/// ASCII Unit Separator (`\x1F`) is chosen because it cannot appear in
/// shell command strings, paths, URLs, or exit codes — unlike `:` which
/// is common in all of those.
pub const ENV_LIST_SEPARATOR: char = '\x1F';

/// Service that reads terminal context from environment variables exported by
/// the zsh plugin and constructs a structured [`TerminalContext`].
///
/// The zsh plugin exports three `\x1F`-separated environment variables before
/// invoking forge:
/// - [`ENV_TERM_COMMANDS`]   — the command strings
/// - [`ENV_TERM_EXIT_CODES`] — the corresponding exit codes
/// - [`ENV_TERM_TIMESTAMPS`] — the corresponding Unix timestamps
#[derive(Clone)]
pub struct TerminalContextService<S>(Arc<S>);

impl<S> TerminalContextService<S> {
    /// Creates a new `TerminalContextService` backed by the provided
    /// infrastructure.
    pub fn new(infra: Arc<S>) -> Self {
        Self(infra)
    }
}

impl<S: EnvironmentInfra<Config = forge_config::ForgeConfig>> TerminalContextService<S> {
    /// Reads the terminal context from environment variables.
    ///
    /// Commands are sorted by timestamp (oldest first, most recent last).
    ///
    /// Returns `None` if none of the required variables are set or if no
    /// commands were recorded.
    pub fn get_terminal_context(&self) -> Option<TerminalContext> {
        let commands_raw = self.0.get_env_var(ENV_TERM_COMMANDS)?;

        let commands: Vec<String> = split_env_list(&commands_raw);
        if commands.is_empty() {
            return None;
        }

        let exit_codes_raw = self.0.get_env_var(ENV_TERM_EXIT_CODES).unwrap_or_default();
        let timestamps_raw = self.0.get_env_var(ENV_TERM_TIMESTAMPS).unwrap_or_default();

        let exit_codes: Vec<i32> = split_env_list(&exit_codes_raw)
            .iter()
            .map(|s| s.parse::<i32>().unwrap_or(0))
            .collect();

        let timestamps: Vec<u64> = split_env_list(&timestamps_raw)
            .iter()
            .map(|s| s.parse::<u64>().unwrap_or(0))
            .collect();
        // Zip the three lists together; pad missing exit codes/timestamps with 0.
        // The outer zip() truncates to the length of `commands`, so the
        // repeat() padding never produces extra entries.
        let mut entries: Vec<TerminalCommand> = commands
            .into_iter()
            .zip(exit_codes.into_iter().chain(std::iter::repeat(0)))
            .zip(timestamps.into_iter().chain(std::iter::repeat(0)))
            .map(|((command, exit_code), timestamp)| TerminalCommand {
                command,
                exit_code,
                timestamp,
            })
            .collect();

        // Sort by timestamp so the most recent command appears last.
        entries.sort_by_key(|e| e.timestamp);

        if entries.is_empty() {
            None
        } else {
            Some(TerminalContext { commands: entries })
        }
    }
}

/// Splits an `\x1F`-separated (ASCII Unit Separator) environment variable
/// value into a list of strings, filtering out any empty segments.
pub fn split_env_list(raw: &str) -> Vec<String> {
    raw.split(ENV_LIST_SEPARATOR)
        .filter(|s| !s.is_empty())
        .map(String::from)
        .collect()
}

#[cfg(test)]
mod tests {
    use std::collections::BTreeMap;
    use std::sync::Arc;

    use forge_domain::{Environment, TerminalCommand, TerminalContext};
    use pretty_assertions::assert_eq;

    use super::*;

    struct MockInfra {
        env_vars: BTreeMap<String, String>,
    }

    impl MockInfra {
        fn new(vars: &[(&str, &str)]) -> Arc<Self> {
            Arc::new(Self {
                env_vars: vars
                    .iter()
                    .map(|(k, v)| (k.to_string(), v.to_string()))
                    .collect(),
            })
        }
    }

    impl crate::EnvironmentInfra for MockInfra {
        type Config = forge_config::ForgeConfig;

        fn get_environment(&self) -> Environment {
            use fake::{Fake, Faker};
            Faker.fake()
        }

        fn get_config(&self) -> anyhow::Result<forge_config::ForgeConfig> {
            Ok(forge_config::ForgeConfig::default())
        }

        async fn update_environment(
            &self,
            _ops: Vec<forge_domain::ConfigOperation>,
        ) -> anyhow::Result<()> {
            Ok(())
        }

        fn get_env_var(&self, key: &str) -> Option<String> {
            self.env_vars.get(key).cloned()
        }

        fn get_env_vars(&self) -> BTreeMap<String, String> {
            self.env_vars.clone()
        }
    }

    #[test]
    fn test_no_env_vars_returns_none() {
        let fixture = TerminalContextService::new(MockInfra::new(&[]));
        let actual = fixture.get_terminal_context();
        assert_eq!(actual, None);
    }

    #[test]
    fn test_empty_commands_returns_none() {
        let fixture = TerminalContextService::new(MockInfra::new(&[(ENV_TERM_COMMANDS, "")]));
        let actual = fixture.get_terminal_context();
        assert_eq!(actual, None);
    }

    #[test]
    fn test_single_command_no_extras() {
        let fixture =
            TerminalContextService::new(MockInfra::new(&[(ENV_TERM_COMMANDS, "cargo build")]));
        let actual = fixture.get_terminal_context();
        let expected = Some(TerminalContext {
            commands: vec![TerminalCommand {
                command: "cargo build".to_string(),
                exit_code: 0,
                timestamp: 0,
            }],
        });
        assert_eq!(actual, expected);
    }

    #[test]
    fn test_multiple_commands_with_exit_codes_and_timestamps() {
        let sep = ENV_LIST_SEPARATOR;
        let fixture = TerminalContextService::new(MockInfra::new(&[
            (
                ENV_TERM_COMMANDS,
                &format!("ls{sep}cargo test{sep}git status"),
            ),
            (ENV_TERM_EXIT_CODES, &format!("0{sep}1{sep}0")),
            (
                ENV_TERM_TIMESTAMPS,
                &format!("1700000001{sep}1700000002{sep}1700000003"),
            ),
        ]));
        let actual = fixture.get_terminal_context();
        let expected = Some(TerminalContext {
            commands: vec![
                TerminalCommand {
                    command: "ls".to_string(),
                    exit_code: 0,
                    timestamp: 1700000001,
                },
                TerminalCommand {
                    command: "cargo test".to_string(),
                    exit_code: 1,
                    timestamp: 1700000002,
                },
                TerminalCommand {
                    command: "git status".to_string(),
                    exit_code: 0,
                    timestamp: 1700000003,
                },
            ],
        });
        assert_eq!(actual, expected);
    }

    #[test]
    fn test_split_env_list_empty() {
        let actual = split_env_list("");
        let expected: Vec<String> = vec![];
        assert_eq!(actual, expected);
    }

    #[test]
    fn test_split_env_list_single() {
        let actual = split_env_list("hello");
        let expected = vec!["hello".to_string()];
        assert_eq!(actual, expected);
    }

    #[test]
    fn test_split_env_list_multiple() {
        let sep = ENV_LIST_SEPARATOR;
        let actual = split_env_list(&format!("a{sep}b{sep}c"));
        let expected = vec!["a".to_string(), "b".to_string(), "c".to_string()];
        assert_eq!(actual, expected);
    }

    #[test]
    fn test_split_env_list_command_with_colon() {
        // Commands containing `:` (e.g. URLs, port mappings) must not be split.
        let sep = ENV_LIST_SEPARATOR;
        let actual = split_env_list(&format!(
            "curl https://example.com{sep}docker run -p 8080:80 nginx"
        ));
        let expected = vec![
            "curl https://example.com".to_string(),
            "docker run -p 8080:80 nginx".to_string(),
        ];
        assert_eq!(actual, expected);
    }

    #[test]
    fn test_commands_sorted_by_timestamp_oldest_first() {
        // Supply commands in reverse-timestamp order to confirm sorting is applied.
        let sep = ENV_LIST_SEPARATOR;
        let fixture = TerminalContextService::new(MockInfra::new(&[
            (
                ENV_TERM_COMMANDS,
                &format!("git status{sep}cargo test{sep}ls"),
            ),
            (ENV_TERM_EXIT_CODES, &format!("0{sep}1{sep}0")),
            (
                ENV_TERM_TIMESTAMPS,
                &format!("1700000003{sep}1700000002{sep}1700000001"),
            ),
        ]));
        let actual = fixture.get_terminal_context();
        let expected = Some(TerminalContext {
            commands: vec![
                TerminalCommand {
                    command: "ls".to_string(),
                    exit_code: 0,
                    timestamp: 1700000001,
                },
                TerminalCommand {
                    command: "cargo test".to_string(),
                    exit_code: 1,
                    timestamp: 1700000002,
                },
                TerminalCommand {
                    command: "git status".to_string(),
                    exit_code: 0,
                    timestamp: 1700000003,
                },
            ],
        });
        assert_eq!(actual, expected);
    }

    #[test]
    fn test_all_commands_included() {
        // All captured commands are included (no limit).
        let sep = ENV_LIST_SEPARATOR;
        let fixture = TerminalContextService::new(MockInfra::new(&[
            (
                ENV_TERM_COMMANDS,
                &format!("ls{sep}cargo test{sep}git status"),
            ),
            (ENV_TERM_EXIT_CODES, &format!("0{sep}1{sep}0")),
            (
                ENV_TERM_TIMESTAMPS,
                &format!("1700000001{sep}1700000002{sep}1700000003"),
            ),
        ]));
        let actual = fixture.get_terminal_context();
        assert_eq!(actual.unwrap().commands.len(), 3);
    }
}