2026-11-27 16:41:09 +00:00
package preflight
import (
"context"
"errors"
2026-11-30 17:07:48 +00:00
"os"
2026-11-27 16:41:09 +00:00
"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
2026-11-30 17:07:48 +00:00
func TestNotariusPreservesMultipleFailuresAndAdapterCause ( t * testing . T ) {
cause := errors . New ( "provider unavailable" )
adapterErr := & os . PathError { Op : "probe" , Path : "synthetic" , Err : cause }
fake := & notarius . FakeRunner { PreflightErr : adapterErr , PreflightResult : & notarius . ReadinessResult {
Checks : [ ] notarius . ReadinessCheck {
{ ID : "provider" , Result : "failed" , Message : "provider check failed" } ,
{ ID : "output" , Result : "failed" , Message : "output check failed" } ,
{ ID : "cache" , Result : "warning" , Message : "cache unavailable" } ,
} ,
} }
result , err := CheckNotarius ( context . Background ( ) , fake , notarius . ReadinessRequest { } )
var typed * os . PathError
if ! errors . Is ( err , cause ) || ! errors . As ( err , & typed ) || typed != adapterErr {
t . Fatalf ( "adapter cause lost: %v" , err )
}
for _ , message := range [ ] string { "provider check failed" , "output check failed" } {
if ! strings . Contains ( err . Error ( ) , message ) {
t . Fatalf ( "missing failure %q: %v" , message , err )
}
}
for _ , check := range fake . PreflightResult . Checks {
found := false
for _ , finding := range result . Findings {
if finding . Name == check . ID && finding . Message == check . Message {
want := SeverityError
if check . Result == "warning" {
want = SeverityWarning
}
found = finding . Severity == want
}
}
if ! found {
t . Fatalf ( "missing or reclassified check %q: %+v" , check . ID , result . Findings )
}
}
}
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 )
}
}