Skip to content

Commit ff8d76c

Browse files
committed
test(animation): cover special branches and manual checkpoint in AnimationEngine
1 parent 95b3510 commit ff8d76c

1 file changed

Lines changed: 317 additions & 0 deletions

File tree

‎src/animation.rs‎

Lines changed: 317 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,6 +1402,7 @@ impl AnimationEngine {
14021402
#[cfg(test)]
14031403
mod tests {
14041404
use super::*;
1405+
use chrono::{DateTime, Utc};
14051406

14061407
fn speed_rule(rule: &str) -> SpeedRule {
14071408
SpeedRule::parse(rule).expect("valid speed rule")
@@ -1416,6 +1417,98 @@ mod tests {
14161417
}
14171418
}
14181419

1420+
fn line_change(change_type: LineChangeType, content: &str) -> crate::git::LineChange {
1421+
crate::git::LineChange {
1422+
change_type,
1423+
content: content.to_string(),
1424+
old_line_no: None,
1425+
new_line_no: None,
1426+
}
1427+
}
1428+
1429+
fn hunk(old_start: usize, lines: Vec<crate::git::LineChange>) -> DiffHunk {
1430+
DiffHunk {
1431+
old_start,
1432+
old_lines: lines.len(),
1433+
new_start: old_start,
1434+
new_lines: lines.len(),
1435+
lines,
1436+
}
1437+
}
1438+
1439+
#[allow(clippy::too_many_arguments)]
1440+
fn file_change(
1441+
path: &str,
1442+
old_path: Option<&str>,
1443+
status: FileStatus,
1444+
is_excluded: bool,
1445+
exclusion_reason: Option<&str>,
1446+
old_content: Option<&str>,
1447+
new_content: Option<&str>,
1448+
hunks: Vec<DiffHunk>,
1449+
) -> FileChange {
1450+
FileChange {
1451+
path: path.to_string(),
1452+
old_path: old_path.map(str::to_string),
1453+
status,
1454+
is_binary: false,
1455+
is_excluded,
1456+
exclusion_reason: exclusion_reason.map(str::to_string),
1457+
old_content: old_content.map(str::to_string),
1458+
new_content: new_content.map(str::to_string),
1459+
hunks,
1460+
diff: String::new(),
1461+
}
1462+
}
1463+
1464+
fn metadata(hash: &str, message: &str, changes: Vec<FileChange>) -> CommitMetadata {
1465+
CommitMetadata {
1466+
hash: hash.to_string(),
1467+
author: "Author".to_string(),
1468+
date: DateTime::parse_from_rfc3339("2024-01-02T03:04:05Z")
1469+
.unwrap()
1470+
.with_timezone(&Utc),
1471+
message: message.to_string(),
1472+
changes,
1473+
}
1474+
}
1475+
1476+
fn terminal_commands(steps: &[AnimationStep]) -> Vec<String> {
1477+
let (mut commands, current) = steps.iter().fold(
1478+
(Vec::new(), None::<String>),
1479+
|(mut commands, current), step| match step {
1480+
AnimationStep::TerminalPrompt => {
1481+
if let Some(command) = current {
1482+
commands.push(command);
1483+
}
1484+
(commands, Some(String::new()))
1485+
}
1486+
AnimationStep::TerminalTypeChar { ch } => {
1487+
let mut command = current.unwrap_or_default();
1488+
command.push(*ch);
1489+
(commands, Some(command))
1490+
}
1491+
_ => (commands, current),
1492+
},
1493+
);
1494+
1495+
if let Some(command) = current {
1496+
commands.push(command);
1497+
}
1498+
1499+
commands
1500+
}
1501+
1502+
fn terminal_outputs(steps: &[AnimationStep]) -> Vec<String> {
1503+
steps
1504+
.iter()
1505+
.filter_map(|step| match step {
1506+
AnimationStep::TerminalOutput { text } => Some(text.clone()),
1507+
_ => None,
1508+
})
1509+
.collect()
1510+
}
1511+
14191512
#[test]
14201513
fn speed_rule_uses_last_colon_as_separator() {
14211514
let rule = speed_rule("docs/v1:guide/*.md:45");
@@ -1481,4 +1574,228 @@ mod tests {
14811574
assert_eq!(engine.dialog_title, None);
14821575
assert!(engine.dialog_typing_text.is_empty());
14831576
}
1577+
1578+
#[test]
1579+
fn load_commit_generates_special_file_sequences_for_regular_commits() {
1580+
let mut engine = AnimationEngine::new(30);
1581+
let commit = metadata(
1582+
"abcdef1234567890",
1583+
"Refine animation\n\nbody",
1584+
vec![
1585+
file_change(
1586+
"Cargo.lock",
1587+
None,
1588+
FileStatus::Modified,
1589+
true,
1590+
Some("lock file"),
1591+
Some("old"),
1592+
Some("new"),
1593+
vec![],
1594+
),
1595+
file_change(
1596+
"src/deleted.rs",
1597+
None,
1598+
FileStatus::Deleted,
1599+
false,
1600+
None,
1601+
Some("obsolete();\n"),
1602+
None,
1603+
vec![],
1604+
),
1605+
file_change(
1606+
"src/renamed.rs",
1607+
Some("src/original.rs"),
1608+
FileStatus::Renamed,
1609+
false,
1610+
None,
1611+
Some("before\n"),
1612+
Some("after\n"),
1613+
vec![],
1614+
),
1615+
file_change(
1616+
"src/lib.rs",
1617+
None,
1618+
FileStatus::Modified,
1619+
false,
1620+
None,
1621+
Some("fn main() {\n}\n"),
1622+
Some("fn main() {\n println!(\"hi\");\n}\n"),
1623+
vec![hunk(
1624+
1,
1625+
vec![
1626+
line_change(LineChangeType::Context, "fn main() {"),
1627+
line_change(LineChangeType::Addition, " println!(\"hi\");"),
1628+
line_change(LineChangeType::Context, "}"),
1629+
],
1630+
)],
1631+
),
1632+
],
1633+
);
1634+
1635+
engine.load_commit(&commit);
1636+
1637+
let commands = terminal_commands(&engine.steps);
1638+
let outputs = terminal_outputs(&engine.steps);
1639+
let switched_paths = engine
1640+
.steps
1641+
.iter()
1642+
.filter_map(|step| match step {
1643+
AnimationStep::SwitchFile { path, .. } => Some(path.clone()),
1644+
_ => None,
1645+
})
1646+
.collect::<Vec<_>>();
1647+
1648+
assert_eq!(
1649+
commands,
1650+
vec![
1651+
"time-travel 2024-01-02 03:04:05".to_string(),
1652+
"rm src/deleted.rs".to_string(),
1653+
"git add src/deleted.rs".to_string(),
1654+
"git add src/lib.rs".to_string(),
1655+
"mv src/original.rs src/renamed.rs".to_string(),
1656+
"git add src/renamed.rs".to_string(),
1657+
"git commit -m \"Refine animation\"".to_string(),
1658+
"git push origin main".to_string(),
1659+
]
1660+
);
1661+
assert_eq!(
1662+
switched_paths,
1663+
vec![
1664+
"Cargo.lock".to_string(),
1665+
"src/deleted.rs".to_string(),
1666+
"src/lib.rs".to_string(),
1667+
"src/renamed.rs".to_string(),
1668+
]
1669+
);
1670+
assert_eq!(
1671+
engine
1672+
.steps
1673+
.iter()
1674+
.filter(|step| matches!(step, AnimationStep::OpenFileDialogStart))
1675+
.count(),
1676+
1
1677+
);
1678+
assert!(engine.steps.iter().any(|step| {
1679+
matches!(
1680+
step,
1681+
AnimationStep::InsertLine { line, content }
1682+
if *line == 1 && content == " "
1683+
)
1684+
}));
1685+
assert!(outputs
1686+
.iter()
1687+
.any(|text| text.contains("Cargo.lock (skipped - lock file)")));
1688+
assert!(outputs
1689+
.iter()
1690+
.any(|text| text.contains("Location: commit abcdef1 by Author")));
1691+
assert!(outputs.iter().any(|text| text.contains("Refine animation")));
1692+
assert!(outputs.iter().any(|text| text.contains("✨ SUCCESS")));
1693+
assert!(
1694+
matches!(engine.pending_metadata.as_ref(), Some(pending) if pending.hash == commit.hash)
1695+
);
1696+
assert_eq!(engine.buffer.lines, vec![String::new()]);
1697+
}
1698+
1699+
#[test]
1700+
fn load_commit_uses_working_tree_intro_without_commit_or_push_commands() {
1701+
let mut engine = AnimationEngine::new(30);
1702+
let diff = metadata(
1703+
"working-tree",
1704+
"Staged changes",
1705+
vec![file_change(
1706+
"src/draft.rs",
1707+
None,
1708+
FileStatus::Modified,
1709+
false,
1710+
None,
1711+
Some("fn main() {}\n"),
1712+
Some("fn main() {\n println!(\"draft\");\n}\n"),
1713+
vec![hunk(
1714+
1,
1715+
vec![
1716+
line_change(LineChangeType::Context, "fn main() {"),
1717+
line_change(LineChangeType::Addition, " println!(\"draft\");"),
1718+
line_change(LineChangeType::Context, "}"),
1719+
],
1720+
)],
1721+
)],
1722+
);
1723+
1724+
engine.load_commit(&diff);
1725+
1726+
let commands = terminal_commands(&engine.steps);
1727+
let outputs = terminal_outputs(&engine.steps);
1728+
1729+
assert_eq!(
1730+
commands.first().map(String::as_str),
1731+
Some("git diff --stat")
1732+
);
1733+
assert!(commands
1734+
.iter()
1735+
.any(|command| command == "git add src/draft.rs"));
1736+
assert!(!commands
1737+
.iter()
1738+
.any(|command| command.starts_with("git commit -m")));
1739+
assert!(!commands
1740+
.iter()
1741+
.any(|command| command == "git push origin main"));
1742+
assert!(outputs.iter().any(|text| text == "📝 Staged changes"));
1743+
assert!(outputs.iter().any(|text| text == "📁 1 file changed"));
1744+
}
1745+
1746+
#[test]
1747+
fn manual_step_records_and_restores_line_and_change_checkpoints() {
1748+
let mut engine = AnimationEngine::new(30);
1749+
engine.state = AnimationState::Playing;
1750+
engine.steps = vec![
1751+
AnimationStep::ResetState,
1752+
switch_file("src/lib.rs", "seed", "seed!\nbranch"),
1753+
AnimationStep::InsertChar {
1754+
line: 0,
1755+
col: 4,
1756+
ch: '!',
1757+
},
1758+
AnimationStep::Pause {
1759+
multiplier: INSERT_LINE_PAUSE,
1760+
},
1761+
AnimationStep::InsertLine {
1762+
line: 1,
1763+
content: "branch".to_string(),
1764+
},
1765+
AnimationStep::Pause {
1766+
multiplier: HUNK_PAUSE,
1767+
},
1768+
];
1769+
1770+
assert!(engine.manual_step(StepMode::Line));
1771+
assert!(engine.manual_step(StepMode::Change));
1772+
assert_eq!(engine.buffer.lines, vec!["seed".to_string()]);
1773+
1774+
assert!(engine.manual_step(StepMode::Line));
1775+
assert_eq!(engine.buffer.lines, vec!["seed!".to_string()]);
1776+
assert_eq!(engine.line_checkpoints.len(), 2);
1777+
assert_eq!(engine.change_checkpoints.len(), 1);
1778+
1779+
assert!(engine.manual_step(StepMode::Change));
1780+
assert_eq!(
1781+
engine.buffer.lines,
1782+
vec!["seed!".to_string(), "branch".to_string()]
1783+
);
1784+
assert_eq!(engine.line_checkpoints.len(), 3);
1785+
assert_eq!(engine.change_checkpoints.len(), 2);
1786+
1787+
assert!(engine.restore_line_checkpoint());
1788+
assert_eq!(engine.buffer.lines, vec!["seed!".to_string()]);
1789+
assert_eq!(engine.current_step, 4);
1790+
assert_eq!(engine.state, AnimationState::Playing);
1791+
assert!(engine.paused);
1792+
1793+
assert!(engine.restore_change_checkpoint());
1794+
assert_eq!(engine.buffer.lines, vec!["seed".to_string()]);
1795+
assert_eq!(engine.current_step, 2);
1796+
assert!(matches!(
1797+
engine.current_file_path.as_deref(),
1798+
Some("src/lib.rs")
1799+
));
1800+
}
14841801
}

0 commit comments

Comments
 (0)