2026-11-20 05:47:41 +00:00
package app
import (
"context"
"errors"
"testing"
"gitea.maximumdirect.net/eric/narratio/internal/adapters/whisperx"
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
"gitea.maximumdirect.net/eric/narratio/internal/config"
"gitea.maximumdirect.net/eric/narratio/internal/manifest"
"gitea.maximumdirect.net/eric/narratio/internal/stage"
)
type terminalCountingStore struct {
manifest . Store
terminalSaves int
terminalErr error
}
func ( s * terminalCountingStore ) Save ( ctx context . Context , path string , m * manifest . Manifest ) error {
if m . LastError != nil {
s . terminalSaves ++
if s . terminalErr != nil {
return s . terminalErr
}
}
return s . Store . Save ( ctx , path , m )
}
func TestPartialRunRetainsAllFailureCausesAndDurableSuccess ( t * testing . T ) {
for _ , compositionFailure := range [ ] bool { false , true } {
for _ , saveFails := range [ ] bool { false , true } {
t . Run ( map [ bool ] string { false : "stage" , true : "composition" } [ compositionFailure ] + map [ bool ] string { false : " saved" , true : " save failed" } [ saveFails ] , func ( t * testing . T ) {
cfg := testConfig ( t )
cause := errors . New ( "operation unavailable" )
saveErr := errors . New ( "terminal persistence unavailable" )
releaseErr := errors . New ( "lock release failed" )
store := & terminalCountingStore { Store : & manifest . LocalStore { } }
if saveFails {
store . terminalErr = saveErr
}
local := & releaseErrorArtifactStore { Store : artifacts . NewLocalStore ( cfg . Pipeline . Workspace . Root ) , err : releaseErr }
firstRuns := 0
stages := [ ] stage . Stage { countingStage { name : "prepare" , runs : & firstRuns } , failingStage { name : "transcribe" , err : cause } , countingStage { name : "merge" , runs : new ( int ) } }
deps := invocationDependencies { newWhisperX : func ( * config . Config ) ( whisperx . Client , error ) {
if compositionFailure {
return nil , cause
}
return & whisperx . NoopClient { } , nil
} }
summary , err := executePlanWithDependencies ( context . Background ( ) , cfg , BoundedPlan { stages : stages } , RunOptions { Env : & Env { ManifestStore : store , ArtifactStore : local } } , deps )
if ! errors . Is ( err , cause ) || ! errors . Is ( err , releaseErr ) || ( saveFails && ! errors . Is ( err , saveErr ) ) {
t . Fatalf ( "lost causes: %v" , err )
}
if store . terminalSaves != 1 {
t . Fatalf ( "terminal saves=%d" , store . terminalSaves )
}
if summary == nil || summary . Complete || len ( summary . StageResults ) != 3 {
t . Fatalf ( "summary=%#v" , summary )
}
if compositionFailure {
2026-11-27 17:00:16 +00:00
if firstRuns != 1 || summary . StageResults [ 0 ] . Outcome != "completed" || summary . StageResults [ 1 ] . Attempted || summary . StageResults [ 1 ] . Outcome != "failed" || summary . StageResults [ 2 ] . Outcome != "not_attempted" {
t . Fatalf ( "composition results: %#v" , summary )
2026-11-20 05:47:41 +00:00
}
return
}
if firstRuns != 1 || summary . StageResults [ 0 ] . Outcome != "completed" || summary . StageResults [ 1 ] . Outcome != "failed" || ! summary . StageResults [ 1 ] . Attempted || summary . StageResults [ 2 ] . Outcome != "not_attempted" {
t . Fatalf ( "partial results=%#v" , summary )
}
durable , err := store . Load ( context . Background ( ) , summary . ManifestPath )
if err != nil {
t . Fatal ( err )
}
if durable . Stages [ "prepare" ] . Status != manifest . StatusSucceeded {
t . Fatal ( "earlier success lost" )
}
if saveFails {
if durable . Stages [ "transcribe" ] . Status != manifest . StatusRunning {
t . Fatal ( "failed terminal save advertised durable failure" )
}
} else if durable . Stages [ "transcribe" ] . Status != manifest . StatusFailed {
t . Fatal ( "failed attempt not persisted" )
}
retry , err := executeStages ( context . Background ( ) , cfg , [ ] stage . Stage { countingStage { name : "prepare" , runs : & firstRuns } , countingStage { name : "transcribe" , runs : new ( int ) } } , RunOptions { } )
if err != nil || firstRuns != 1 || retry . StageResults [ 0 ] . Outcome != "reused" || ! retry . Complete {
t . Fatalf ( "retry=%#v runs=%d err=%v" , retry , firstRuns , err )
}
} )
}
}
}