Skip to content

Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors

Rust

test(cli): line up the api-key list past ASCII

Vinícius DS Andrade3b1b2d0a6898
diff --git a/crates/ai-memory-cli/src/commands/api_key.rs b/crates/ai-memory-cli/src/commands/api_key.rs
index 4ec7a161..ef1b4996 100644
--- a/crates/ai-memory-cli/src/commands/api_key.rs
+++ b/crates/ai-memory-cli/src/commands/api_key.rs
@@ -201,3 +201,68 @@ fn confirm(prompt: &str) -> Result<()> {
}
Ok(())
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ fn credential(label: &str) -> CredentialRow {
+ CredentialRow {
+ id: "k1".to_owned(),
+ user_id: "u1".to_owned(),
+ label: label.to_owned(),
+ preview: Some("aim_ab".to_owned()),
+ created_at: 0,
+ last_used_at: None,
+ expires_at: None,
+ revoked_at: None,
+ }
+ }
+
+ /// The display-cell offset at which `line` carries `cell`, or `None`.
+ fn cell_column(line: &str, cell: &str) -> Option<usize> {
+ line.find(cell)
+ .map(|byte| unicode_width::UnicodeWidthStr::width(&line[..byte]))
+ }
+
+ fn render(credentials: &[CredentialRow]) -> String {
+ let mut buf = Vec::new();
+ write_credentials(credentials, &mut buf).expect("rendering to a buffer cannot fail");
+ String::from_utf8(buf).expect("the table is utf-8")
+ }
+
+ #[test]
+ fn credentials_table_columns_line_up_past_ascii() {
+ // Under a byte-measured width the double-width label is padded to its
+ // byte count in characters, which pushes its STATUS and PREVIEW cells
+ // right of the ASCII row's.
+ let table = render(&[credential("記憶"), credential("ci")]);
+ let header = table.lines().next().expect("a header row");
+ let status_col = cell_column(header, "STATUS").expect("a STATUS column");
+ for line in table.lines().skip(1) {
+ assert_eq!(
+ cell_column(line, "active"),
+ Some(status_col),
+ "STATUS column moved on {line:?}:\n{table}"
+ );
+ }
+
+ // An accented label must not widen LABEL past what any row needs:
+ // STATUS follows LABEL by the widest label plus the separator.
+ let accented = render(&[credential("produção"), credential("ci")]);
+ let header = accented.lines().next().expect("a header row");
+ let label_col = cell_column(header, "LABEL").expect("a LABEL column");
+ let status_col = cell_column(header, "STATUS").expect("a STATUS column");
+ let widest_label = "produção".chars().count();
+ assert_eq!(status_col, label_col + widest_label + 2, "{accented}");
+
+ // Control: ASCII rows line up under either measure, so the rows above
+ // are what carry the assertions.
+ let ascii = render(&[credential("prod"), credential("ci")]);
+ let ascii_header = ascii.lines().next().expect("a header row");
+ let ascii_status = cell_column(ascii_header, "STATUS").expect("a STATUS column");
+ for line in ascii.lines().skip(1) {
+ assert_eq!(cell_column(line, "active"), Some(ascii_status), "{ascii}");
+ }
+ }
+}