implements PLN-0140

This commit is contained in:
2026-07-03 23:10:03 +01:00
parent 29e002c5f3
commit 1b29b2f985
10 changed files with 294 additions and 21 deletions
@@ -10,6 +10,8 @@ pub enum ForegroundOwner {
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ResidentGameState {
Foreground,
PauseRequested { remaining_ticks: u8 },
ResumeRequested,
PausedSuspended,
}
@@ -70,12 +72,81 @@ impl ForegroundStack {
Ok(())
}
pub fn request_home_from_game(&mut self, task_id: TaskId) -> Result<(), ForegroundStackError> {
pub fn request_home_from_game(
&mut self,
task_id: TaskId,
pause_budget_ticks: u8,
) -> Result<(), ForegroundStackError> {
match self.resident_game {
Some(game) if game.task_id == task_id => {
if !matches!(
game.state,
ResidentGameState::PauseRequested { .. } | ResidentGameState::PausedSuspended
) {
self.resident_game = Some(ResidentGame {
task_id,
state: ResidentGameState::PauseRequested {
remaining_ticks: pause_budget_ticks.max(1),
},
});
}
Ok(())
}
_ => Err(ForegroundStackError::TaskIsNotResidentGame(task_id)),
}
}
pub fn advance_pause_budget(&mut self, task_id: TaskId) -> Result<bool, ForegroundStackError> {
match self.resident_game {
Some(game)
if game.task_id == task_id
&& matches!(
game.state,
ResidentGameState::PauseRequested { remaining_ticks: 1 }
) =>
{
self.resident_game =
Some(ResidentGame { task_id, state: ResidentGameState::PausedSuspended });
self.owner = ForegroundOwner::Hub;
Ok(true)
}
Some(game)
if game.task_id == task_id
&& matches!(game.state, ResidentGameState::PauseRequested { .. }) =>
{
let ResidentGameState::PauseRequested { remaining_ticks } = game.state else {
unreachable!();
};
self.resident_game = Some(ResidentGame {
task_id,
state: ResidentGameState::PauseRequested {
remaining_ticks: remaining_ticks.saturating_sub(1).max(1),
},
});
Ok(false)
}
Some(game) if game.task_id == task_id => Ok(false),
_ => Err(ForegroundStackError::TaskIsNotResidentGame(task_id)),
}
}
pub fn request_resume_game(&mut self, task_id: TaskId) -> Result<(), ForegroundStackError> {
match self.resident_game {
Some(game) if game.task_id == task_id => {
self.resident_game =
Some(ResidentGame { task_id, state: ResidentGameState::ResumeRequested });
Ok(())
}
_ => Err(ForegroundStackError::TaskIsNotResidentGame(task_id)),
}
}
pub fn complete_resume_game(&mut self, task_id: TaskId) -> Result<(), ForegroundStackError> {
match self.resident_game {
Some(game) if game.task_id == task_id => {
self.resident_game =
Some(ResidentGame { task_id, state: ResidentGameState::Foreground });
self.owner = ForegroundOwner::Game(task_id);
Ok(())
}
_ => Err(ForegroundStackError::TaskIsNotResidentGame(task_id)),
@@ -159,13 +230,32 @@ mod tests {
}
#[test]
fn home_request_moves_resident_game_to_paused_suspended() {
fn home_request_moves_resident_game_to_pause_requested() {
let mut stack = ForegroundStack::new();
let game = TaskId(3);
stack.set_game_foreground(game).expect("game should be foreground");
stack.request_home_from_game(game).expect("home request should suspend game");
stack.request_home_from_game(game, 2).expect("home request should pause game");
assert_eq!(stack.owner(), ForegroundOwner::Game(game));
assert_eq!(
stack.resident_game(),
Some(ResidentGame {
task_id: game,
state: ResidentGameState::PauseRequested { remaining_ticks: 2 },
})
);
}
#[test]
fn pause_budget_expiry_suspends_game_and_returns_to_hub() {
let mut stack = ForegroundStack::new();
let game = TaskId(3);
stack.set_game_foreground(game).expect("game should be foreground");
stack.request_home_from_game(game, 1).expect("home request should pause game");
assert!(stack.advance_pause_budget(game).expect("budget should advance"));
assert_eq!(stack.owner(), ForegroundOwner::Hub);
assert_eq!(
stack.resident_game(),
@@ -173,6 +263,30 @@ mod tests {
);
}
#[test]
fn resume_request_precedes_game_foreground_restore() {
let mut stack = ForegroundStack::new();
let game = TaskId(3);
stack.set_game_foreground(game).expect("game should be foreground");
stack.request_home_from_game(game, 1).expect("home request should pause game");
stack.advance_pause_budget(game).expect("budget should expire");
stack.request_resume_game(game).expect("resume should be requested");
assert_eq!(stack.owner(), ForegroundOwner::Hub);
assert_eq!(
stack.resident_game(),
Some(ResidentGame { task_id: game, state: ResidentGameState::ResumeRequested })
);
stack.complete_resume_game(game).expect("resume should complete");
assert_eq!(stack.owner(), ForegroundOwner::Game(game));
assert_eq!(
stack.resident_game(),
Some(ResidentGame { task_id: game, state: ResidentGameState::Foreground })
);
}
#[test]
fn shell_foreground_keeps_resident_game() {
let mut stack = ForegroundStack::new();
@@ -180,7 +294,8 @@ mod tests {
let shell = TaskId(4);
stack.set_game_foreground(game).expect("game should be foreground");
stack.request_home_from_game(game).expect("home request should suspend game");
stack.request_home_from_game(game, 1).expect("home request should suspend game");
stack.advance_pause_budget(game).expect("budget should expire");
stack.set_shell_foreground(shell).expect("shell should become foreground");
assert_eq!(stack.owner(), ForegroundOwner::Shell(shell));