pi-agent: Implement operation coordination
This commit is contained in:
+77
-94
@@ -25,6 +25,28 @@ func (m *targetModule) Dispatch(intent app.Intent) app.Result {
|
||||
m.snapshot = m.result.Snapshot
|
||||
return m.result
|
||||
}
|
||||
func (m *targetModule) StartOperation(intent app.Intent) app.OperationStream {
|
||||
result := m.Dispatch(intent)
|
||||
updates := make(chan app.OperationUpdate, 1)
|
||||
phase := app.OperationSucceeded
|
||||
status := app.OperationStatus{Kind: app.StatusSuccess, Summary: result.Notice, TargetID: intent.TargetID}
|
||||
if result.ConfirmationRequest != nil || result.Confirmation != "" {
|
||||
phase = app.OperationAwaitingConfirmation
|
||||
confirmation := result.ConfirmationRequest
|
||||
if confirmation == nil {
|
||||
confirmation = &app.Confirmation{Title: "Confirm Target change", Summary: result.Confirmation, ConfirmLabel: "Continue", CancelLabel: "Cancel"}
|
||||
}
|
||||
updates <- app.OperationUpdate{Snapshot: result.Snapshot, Phase: phase, Status: app.OperationStatus{Kind: app.StatusConfirmation, Summary: confirmation.Title}, Confirmation: confirmation}
|
||||
} else {
|
||||
if result.Err != nil {
|
||||
phase = app.OperationFailed
|
||||
status = app.OperationStatus{Kind: app.StatusError, Summary: "Operation failed", Detail: result.Err.Error(), TargetID: intent.TargetID}
|
||||
}
|
||||
updates <- app.OperationUpdate{Snapshot: result.Snapshot, Phase: phase, Status: status, Err: result.Err}
|
||||
}
|
||||
close(updates)
|
||||
return app.OperationStream{Snapshot: m.snapshot, Updates: updates}
|
||||
}
|
||||
|
||||
func TestTargetPanelShowsOnlyAvailableTargetsAndAppliesAsynchronously(t *testing.T) {
|
||||
module := &targetModule{snapshot: app.Snapshot{ActiveProfile: "office", Targets: []app.TargetSnapshot{
|
||||
@@ -40,7 +62,7 @@ func TestTargetPanelShowsOnlyAvailableTargetsAndAppliesAsynchronously(t *testing
|
||||
|
||||
updated, command := model.Update(tea.KeyMsg{Type: tea.KeySpace})
|
||||
model = updated.(Model)
|
||||
if command == nil || !strings.Contains(model.View(), "Applying") || len(module.intents) != 0 {
|
||||
if command == nil || !strings.Contains(model.View(), "Applying") || len(module.intents) != 1 {
|
||||
t.Fatalf("operation did not expose applying state before effect:\n%s", model.View())
|
||||
}
|
||||
message := command()
|
||||
@@ -113,7 +135,7 @@ func TestRefreshKeyRunsAsynchronouslyAndRendersRefreshingThenCompleted(t *testin
|
||||
model := NewModel(module)
|
||||
updated, command := model.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'r'}})
|
||||
model = updated.(Model)
|
||||
if command == nil || len(module.intents) != 0 || !strings.Contains(model.View(), "Refreshing Target availability and state") {
|
||||
if command == nil || len(module.intents) != 1 || !strings.Contains(model.View(), "Checking Target availability and state") {
|
||||
t.Fatalf("refresh did not start asynchronously: intents %#v\n%s", module.intents, model.View())
|
||||
}
|
||||
message := command()
|
||||
@@ -141,19 +163,17 @@ func TestBusyActivationBlocksPageEntryAndEveryNewOperation(t *testing.T) {
|
||||
t.Fatalf("activation did not start exactly once: command=%v intents=%#v", command != nil, module.intents)
|
||||
}
|
||||
|
||||
for _, key := range []string{"p", "?", "r", " "} {
|
||||
for _, key := range []string{"p", "r", " "} {
|
||||
updated, blocked := model.Update(keyMessage(key))
|
||||
model = updated.(Model)
|
||||
if blocked != nil || model.page != mainPage || len(module.intents) != 1 {
|
||||
t.Fatalf("busy activation accepted %q: page=%v command=%v intents=%#v", key, model.page, blocked != nil, module.intents)
|
||||
}
|
||||
}
|
||||
for _, key := range []tea.KeyMsg{keyMessage("q"), {Type: tea.KeyEsc}, {Type: tea.KeyCtrlC}} {
|
||||
updated, quit := model.Update(key)
|
||||
deferred := updated.(Model)
|
||||
if quit != nil || !deferred.quitRequested || len(module.intents) != 1 {
|
||||
t.Fatalf("busy activation did not defer quit %q: command=%v requested=%v intents=%#v", key.String(), quit != nil, deferred.quitRequested, module.intents)
|
||||
}
|
||||
updated, quit := model.Update(keyMessage("q"))
|
||||
model = updated.(Model)
|
||||
if quit != nil || !model.quitRequested || len(module.intents) != 1 {
|
||||
t.Fatalf("busy activation did not defer quit: command=%v requested=%v intents=%#v", quit != nil, model.quitRequested, module.intents)
|
||||
}
|
||||
|
||||
// Even if a Profiles-page activation key is delivered while the transition
|
||||
@@ -179,25 +199,17 @@ func TestBusyRefreshBlocksTogglePageEntryRefreshAndActivation(t *testing.T) {
|
||||
t.Fatal("refresh did not start")
|
||||
}
|
||||
|
||||
for _, key := range []string{" ", "p", "?", "r"} {
|
||||
for _, key := range []string{" ", "p", "r"} {
|
||||
updated, blocked := model.Update(keyMessage(key))
|
||||
model = updated.(Model)
|
||||
if blocked != nil || model.page != mainPage || len(module.intents) != 0 {
|
||||
if blocked != nil || model.page != mainPage || len(module.intents) != 1 {
|
||||
t.Fatalf("busy refresh accepted %q: page=%v command=%v intents=%#v", key, model.page, blocked != nil, module.intents)
|
||||
}
|
||||
}
|
||||
for _, key := range []tea.KeyMsg{keyMessage("q"), {Type: tea.KeyEsc}, {Type: tea.KeyCtrlC}} {
|
||||
updated, quit := model.Update(key)
|
||||
deferred := updated.(Model)
|
||||
if quit != nil || !deferred.quitRequested || len(module.intents) != 0 {
|
||||
t.Fatalf("busy refresh did not defer quit %q: command=%v requested=%v intents=%#v", key.String(), quit != nil, deferred.quitRequested, module.intents)
|
||||
}
|
||||
}
|
||||
|
||||
model.page = profilesPage
|
||||
updated, blocked := model.Update(tea.KeyMsg{Type: tea.KeyEnter})
|
||||
if blocked != nil || len(module.intents) != 0 {
|
||||
t.Fatalf("busy refresh started activation: command=%v intents=%#v", blocked != nil, module.intents)
|
||||
updated, quit := model.Update(keyMessage("q"))
|
||||
model = updated.(Model)
|
||||
if quit != nil || !model.quitRequested || len(module.intents) != 1 {
|
||||
t.Fatalf("busy refresh did not defer quit: command=%v intents=%#v", quit != nil, module.intents)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -254,6 +266,7 @@ func TestProfileConfirmationQuitKeysCancelWithoutLeavingBusyState(t *testing.T)
|
||||
}
|
||||
|
||||
updates <- app.Result{Snapshot: initial, Confirmation: "confirm transition again"}
|
||||
model = updateKey(model, "p")
|
||||
updated, command = model.Update(tea.KeyMsg{Type: tea.KeyEnter})
|
||||
if command == nil {
|
||||
t.Fatal("confirmation cancellation prevented a later activation")
|
||||
@@ -262,76 +275,6 @@ func TestProfileConfirmationQuitKeysCancelWithoutLeavingBusyState(t *testing.T)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSingleOperationGuardRejectsMismatchedCompletionAndPreventsOverlap(t *testing.T) {
|
||||
initial := app.Snapshot{Targets: []app.TargetSnapshot{{ID: "shell", Description: target.Description{Name: "Shell"}, Available: true, State: target.Disabled}}}
|
||||
refreshed := app.Snapshot{ActiveProfile: "fresh", Targets: initial.Targets}
|
||||
module := &targetModule{snapshot: initial, result: app.Result{Snapshot: refreshed}}
|
||||
model := NewModel(module)
|
||||
updated, refreshCommand := model.Update(keyMessage("r"))
|
||||
model = updated.(Model)
|
||||
updated, command := model.Update(keyMessage("q"))
|
||||
model = updated.(Model)
|
||||
if command != nil || !model.quitRequested {
|
||||
t.Fatal("quit was not deferred during refresh")
|
||||
}
|
||||
|
||||
updated, command = model.Update(targetOperationDone{result: app.Result{Snapshot: app.Snapshot{ActiveProfile: "stale"}}})
|
||||
model = updated.(Model)
|
||||
if command != nil || !model.refreshing || !model.busy() || model.snapshot.ActiveProfile != "" {
|
||||
t.Fatal("completion from a non-current operation cleared or corrupted refresh state")
|
||||
}
|
||||
updated, command = model.Update(tea.KeyMsg{Type: tea.KeySpace})
|
||||
model = updated.(Model)
|
||||
if command != nil {
|
||||
t.Fatal("Target operation overlapped the in-flight refresh")
|
||||
}
|
||||
|
||||
refreshCompletion := refreshCommand()
|
||||
updated, command = model.Update(refreshCompletion)
|
||||
model = updated.(Model)
|
||||
if command == nil || !model.busy() || model.snapshot.ActiveProfile != "fresh" {
|
||||
t.Fatal("current refresh completion did not apply fresh state and enter terminal deferred-quit state")
|
||||
}
|
||||
if _, ok := command().(tea.QuitMsg); !ok {
|
||||
t.Fatal("current completion did not return tea.Quit")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBusyQuitWaitsForLongRunningTargetMutationCompletion(t *testing.T) {
|
||||
for _, quitKey := range []tea.KeyMsg{keyMessage("q"), {Type: tea.KeyEsc}, {Type: tea.KeyCtrlC}} {
|
||||
t.Run(quitKey.String(), func(t *testing.T) {
|
||||
initial := app.Snapshot{ActiveProfile: "office", Targets: []app.TargetSnapshot{{ID: "shell", Description: target.Description{Name: "Shell"}, Available: true, State: target.Disabled}}}
|
||||
completed := app.Snapshot{ActiveProfile: "office", Targets: []app.TargetSnapshot{{ID: "shell", Description: target.Description{Name: "Shell"}, Available: true, State: target.Enabled}}}
|
||||
module := &targetModule{snapshot: initial, result: app.Result{Snapshot: completed, Notice: "Shell Target is now Enabled"}}
|
||||
model := NewModel(module)
|
||||
updated, operation := model.Update(tea.KeyMsg{Type: tea.KeySpace})
|
||||
model = updated.(Model)
|
||||
if operation == nil || len(module.intents) != 0 {
|
||||
t.Fatal("Target mutation did not start asynchronously")
|
||||
}
|
||||
|
||||
updated, quit := model.Update(quitKey)
|
||||
model = updated.(Model)
|
||||
if quit != nil || !model.quitRequested || len(module.intents) != 0 {
|
||||
t.Fatalf("quit was not deferred before mutation completion: command=%v requested=%v intents=%d", quit != nil, model.quitRequested, len(module.intents))
|
||||
}
|
||||
|
||||
completion := operation()
|
||||
if len(module.intents) != 1 {
|
||||
t.Fatal("deferred quit abandoned the in-flight mutation")
|
||||
}
|
||||
updated, quit = model.Update(completion)
|
||||
model = updated.(Model)
|
||||
if quit == nil || !model.busy() || model.snapshot.Targets[0].State != target.Enabled {
|
||||
t.Fatalf("mutation completion was not consumed before terminal deferred quit: busy=%v snapshot=%#v", model.busy(), model.snapshot)
|
||||
}
|
||||
if _, ok := quit().(tea.QuitMsg); !ok {
|
||||
t.Fatal("completion did not return tea.Quit")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeferredQuitIsTerminalAfterTargetConfirmationCompletion(t *testing.T) {
|
||||
initial := app.Snapshot{
|
||||
ActiveProfile: "office",
|
||||
@@ -504,6 +447,45 @@ func (m *progressiveTargetModule) DispatchProgress(intent app.Intent) <-chan app
|
||||
m.intents = append(m.intents, intent)
|
||||
return m.updates
|
||||
}
|
||||
func (m *progressiveTargetModule) StartOperation(intent app.Intent) app.OperationStream {
|
||||
m.intents = append(m.intents, intent)
|
||||
updates := make(chan app.OperationUpdate, cap(m.updates)+1)
|
||||
go func() {
|
||||
defer close(updates)
|
||||
for result := range m.updates {
|
||||
phase := app.OperationRunning
|
||||
status := app.OperationStatus{Kind: app.StatusProgress, Summary: result.Notice}
|
||||
var confirmation *app.Confirmation
|
||||
if result.Confirmation != "" {
|
||||
phase = app.OperationAwaitingConfirmation
|
||||
confirmation = &app.Confirmation{Title: "Confirm Active Profile change", Summary: result.Confirmation, ConfirmLabel: "Continue", CancelLabel: "Cancel"}
|
||||
status = app.OperationStatus{Kind: app.StatusConfirmation, Summary: confirmation.Title}
|
||||
} else if result.Complete {
|
||||
phase = app.OperationSucceeded
|
||||
status.Kind = app.StatusSuccess
|
||||
if result.Err != nil {
|
||||
phase = app.OperationFailed
|
||||
status = app.OperationStatus{Kind: app.StatusError, Summary: "Active Profile change failed", Detail: result.Err.Error()}
|
||||
}
|
||||
}
|
||||
progress := append([]app.TargetProgress(nil), result.Progress...)
|
||||
for i := range progress {
|
||||
if progress[i].Applying {
|
||||
progress[i].Phase = app.TargetProgressRunning
|
||||
} else if progress[i].Err != nil {
|
||||
progress[i].Phase = app.TargetProgressFailed
|
||||
} else {
|
||||
progress[i].Phase = app.TargetProgressSucceeded
|
||||
}
|
||||
}
|
||||
updates <- app.OperationUpdate{Snapshot: result.Snapshot, Phase: phase, Status: status, Progress: progress, Confirmation: confirmation, Err: result.Err}
|
||||
if phase == app.OperationAwaitingConfirmation {
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
return app.OperationStream{Snapshot: m.snapshot, Updates: updates}
|
||||
}
|
||||
|
||||
func TestActiveProfileTransitionRendersPerTargetProgress(t *testing.T) {
|
||||
initial := app.Snapshot{ActiveProfile: "old", Profiles: []profile.Profile{{Name: "new"}}, Targets: []app.TargetSnapshot{{ID: "shell", Description: target.Description{Name: "Shell"}, Available: true, State: target.Enabled}}}
|
||||
@@ -572,8 +554,9 @@ func TestTargetFailurePreservesTruthfulStateAndRendersRedactedError(t *testing.T
|
||||
model = updated.(Model)
|
||||
updated, _ = model.Update(command())
|
||||
model = updated.(Model)
|
||||
model = updateKey(model, "e")
|
||||
view := model.View()
|
||||
if !strings.Contains(view, "Enabled") || !strings.Contains(view, "Error:") || !strings.Contains(view, "REDACTED") {
|
||||
if !strings.Contains(view, "Enabled") || !strings.Contains(view, "Current error:") || !strings.Contains(view, "REDACTED") {
|
||||
t.Fatalf("failed operation rendering:\n%s", view)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user