2026-11-27 16:41:09 +00:00
package preflight
import (
"context"
"errors"
"strings"
"testing"
"gitea.maximumdirect.net/eric/narratio/internal/adapters/audita"
2026-11-30 01:17:33 +00:00
"gitea.maximumdirect.net/eric/narratio/internal/adapters/notarius"
2026-11-27 16:41:09 +00:00
)
type auditaFunc func ( context . Context , audita . PolishRequest ) ( audita . PreflightResult , error )
func ( f auditaFunc ) Preflight ( ctx context . Context , r audita . PolishRequest ) ( audita . PreflightResult , error ) {
return f ( ctx , r )
}
2026-11-30 01:17:33 +00:00
func TestNotariusReadinessRequiresCompleteMatchingEvidence ( t * testing . T ) {
request := notarius . ReadinessRequest { RunRequest : notarius . RunRequest { PipelineID : "expected" } , RequiredLanes : [ ] string { "required" } }
for _ , tc := range [ ] struct {
name string
result notarius . ReadinessResult
failure error
success bool
} {
{ "ready with warning" , notarius . ReadinessResult { Pipeline : "expected" , Lanes : [ ] string { "required" } , InspectionComplete : true , AssessmentComplete : true , Ready : true , Checks : [ ] notarius . ReadinessCheck { { ID : "cache" , Result : "warning" , Message : "cache inaccessible" } } } , nil , true } ,
{ "missing lane" , notarius . ReadinessResult { Pipeline : "expected" , InspectionComplete : true , AssessmentComplete : true , Ready : true } , nil , false } ,
{ "incomplete" , notarius . ReadinessResult { Pipeline : "expected" , InspectionComplete : false , AssessmentComplete : true , Ready : true } , nil , false } ,
{ "not ready" , notarius . ReadinessResult { Pipeline : "expected" , Lanes : [ ] string { "required" } , InspectionComplete : true , AssessmentComplete : true } , nil , false } ,
{ "failed delivery" , notarius . ReadinessResult { Pipeline : "expected" , Lanes : [ ] string { "required" } , InspectionComplete : true , AssessmentComplete : true , Ready : true } , errors . New ( "delivery failed" ) , false } ,
} {
t . Run ( tc . name , func ( t * testing . T ) {
fake := & notarius . FakeRunner { PreflightResult : & tc . result , PreflightErr : tc . failure }
result , err := CheckNotarius ( context . Background ( ) , fake , request )
if ( err == nil ) != tc . success {
t . Fatalf ( "result = %#v, error = %v" , result , err )
}
if tc . name == "ready with warning" && ( len ( result . Findings ) != 1 || result . Findings [ 0 ] . Severity != SeverityWarning ) {
t . Fatalf ( "warning = %#v" , result . Findings )
}
} )
}
}
2026-11-27 16:41:09 +00:00
func TestDownstreamChecksPropagateFailureAndCancellation ( t * testing . T ) {
failure := errors . New ( "provider refused synthetic input" )
checker := auditaFunc ( func ( context . Context , audita . PolishRequest ) ( audita . PreflightResult , error ) {
return audita . PreflightResult { ExitCode : 2 } , failure
} )
2026-11-30 15:13:41 +00:00
result , err := CheckAudita ( context . Background ( ) , checker , audita . PolishRequest { } )
2026-11-27 16:41:09 +00:00
if ! errors . Is ( err , failure ) || len ( result . Findings ) != 1 || result . Findings [ 0 ] . Severity != SeverityError || ! strings . Contains ( result . Findings [ 0 ] . Message , "provider refused" ) {
t . Fatalf ( "failure: %+v %v" , result , err )
}
ctx , cancel := context . WithCancel ( context . Background ( ) )
cancel ( )
called := false
checker = auditaFunc ( func ( context . Context , audita . PolishRequest ) ( audita . PreflightResult , error ) {
called = true
return audita . PreflightResult { } , nil
} )
2026-11-30 15:13:41 +00:00
_ , err = CheckAudita ( ctx , checker , audita . PolishRequest { } )
2026-11-27 16:41:09 +00:00
if ! errors . Is ( err , context . Canceled ) || called {
t . Fatalf ( "cancellation: called=%v err=%v" , called , err )
}
}
2026-11-30 15:13:41 +00:00
func TestAuditaPreservesFailureAndCancellation ( t * testing . T ) {
ctx , cancel := context . WithCancel ( context . Background ( ) )
failure := errors . New ( "provider failure" )
checker := auditaFunc ( func ( context . Context , audita . PolishRequest ) ( audita . PreflightResult , error ) {
cancel ( )
return audita . PreflightResult { } , failure
} )
result , err := CheckAudita ( ctx , checker , audita . PolishRequest { } )
if ! errors . Is ( err , failure ) || ! errors . Is ( err , context . Canceled ) || len ( result . Findings ) != 1 || result . Findings [ 0 ] . Severity != SeverityError {
t . Fatalf ( "result = %+v, error = %v" , result , err )
}
}