2026-07-26 17:12:57 +00:00
package cli
import (
"bytes"
2026-11-22 04:05:44 +00:00
"context"
2026-07-26 17:12:57 +00:00
"errors"
"io"
"path/filepath"
2026-11-22 04:05:44 +00:00
"strings"
2026-07-26 17:12:57 +00:00
"testing"
2026-11-22 04:05:44 +00:00
"text/template"
2026-07-26 17:12:57 +00:00
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
2026-11-22 04:05:44 +00:00
"go.maximumdirect.net/commandkit"
2026-07-26 17:12:57 +00:00
)
2026-11-22 04:05:44 +00:00
func TestRunResultUsesOneNativeValueAcrossFormats ( t * testing . T ) {
value := runResult { Pipeline : "demo" , ArtifactCount : 3 , OutputDirectory : "/artifacts/run-one" , DebugDirectory : "/debug/run-one" , FallbackCount : 2 }
for _ , format := range [ ] commandkit . Format { commandkit . FormatText , commandkit . FormatJSON , commandkit . FormatYAML } {
var output bytes . Buffer
if err := commandkit . RenderReport ( & output , commandkit . Report { Value : value , TextTemplate : runResultTemplate } , format ) ; err != nil {
t . Fatal ( err )
}
if format == commandkit . FormatText {
if ! strings . Contains ( output . String ( ) , "Success with fallback: demo produced 3 artifacts in /artifacts/run-one" ) || ! strings . Contains ( output . String ( ) , "2 operations used fallback." ) || ! strings . Contains ( output . String ( ) , "Debug capture: /debug/run-one" ) {
t . Fatalf ( "fallback text: %s" , output . String ( ) )
2026-07-26 17:12:57 +00:00
}
2026-11-22 04:05:44 +00:00
continue
}
report := decodeCompletionReport ( t , format , output . Bytes ( ) )
if report [ "pipeline" ] != "demo" || report [ "output_directory" ] != value . OutputDirectory || report [ "debug_directory" ] != value . DebugDirectory || report [ "fallback_count" ] != float64 ( 2 ) || report [ "artifact_count" ] != float64 ( 3 ) || len ( report ) != 5 {
t . Fatalf ( "completion value: %#v" , report )
}
2026-07-26 17:12:57 +00:00
}
}
2026-11-22 04:05:44 +00:00
func TestNativeCompletionDiagnosticsRetainDomainClassification ( t * testing . T ) {
collection := contracts . DiagnosticCollection { }
for _ , disposition := range [ ] contracts . DiagnosticDisposition { contracts . DiagnosticDispositionWarning , contracts . DiagnosticDispositionAdvisory , contracts . DiagnosticDispositionObservation } {
category := contracts . DiagnosticCategoryDataQuality
if disposition == contracts . DiagnosticDispositionWarning {
category = contracts . DiagnosticCategoryFallback
} else if disposition == contracts . DiagnosticDispositionObservation {
category = contracts . DiagnosticCategoryNormalization
}
collection . Groups = append ( collection . Groups , contracts . DiagnosticGroup { Disposition : disposition , Category : category , ReasonCode : string ( disposition ) , Origin : contracts . DiagnosticOrigin { Stage : contracts . DiagnosticOriginStageExtract , StepID : "build" , LaneID : "notes" , ModuleKey : "producer" } , OccurrenceCount : 1 , Samples : [ ] contracts . DiagnosticSample { { Scope : "current source" , Message : string ( disposition ) + " finding" } } } )
}
if err := collection . Validate ( ) ; err != nil {
2026-08-27 01:27:25 +00:00
t . Fatal ( err )
}
2026-11-22 04:05:44 +00:00
var output , diagnostics bytes . Buffer
app , err := commandkit . New ( commandkit . Identity { Name : "fixture" } , commandkit . NewCommand ( commandkit . CommandSpec [ struct { } ] { Name : "report" , Run : func ( invocation commandkit . Invocation , _ struct { } ) commandkit . Outcome {
reportRunDiagnostics ( invocation . Reporter ( ) , collection )
return reportRunResult ( invocation , runResult { Pipeline : "demo" , ArtifactCount : 1 , OutputDirectory : "/artifacts/run-one" } )
} } ) )
2026-08-27 01:27:25 +00:00
if err != nil {
t . Fatal ( err )
}
2026-11-22 04:05:44 +00:00
outcome := app . Execute ( context . Background ( ) , [ ] string { "report" , "--verbose" } , commandkit . Streams { Out : & output , Err : & diagnostics } )
if outcome . Status != 0 || strings . Contains ( output . String ( ) , "finding" ) || strings . Count ( diagnostics . String ( ) , "WARN:" ) != 1 {
t . Fatalf ( "diagnostic classification: %#v stdout=%s stderr=%s" , outcome , output . String ( ) , diagnostics . String ( ) )
2026-07-26 17:12:57 +00:00
}
2026-11-22 04:05:44 +00:00
for _ , group := range collection . Groups {
if ! strings . Contains ( diagnostics . String ( ) , string ( group . Disposition ) + " finding" ) {
t . Fatalf ( "missing diagnostic: %s" , diagnostics . String ( ) )
}
2026-07-26 17:12:57 +00:00
}
}
2026-11-22 04:05:44 +00:00
func TestCompletedRunRenderingFailureRetainsPublicationEvenWhenQuiet ( t * testing . T ) {
previous := runResultTemplate
t . Cleanup ( func ( ) { runResultTemplate = previous } )
// Simulate a broken application-owned text template, not a new renderer seam.
runResultTemplate = template . Must ( commandkit . ParseTextTemplate ( "invalid" , "{{.MissingField}}" , nil ) )
for _ , quiet := range [ ] bool { false , true } {
probe := & runProbe { }
app , file , input , _ := runFixture ( t , probe )
args := [ ] string { "run" , "demo" , "--config" , file , "--input-file" , input , "--cache" , "bypass" , "--debug" }
if quiet {
args = append ( args , "--quiet" )
}
var output , diagnostics bytes . Buffer
outcome := app . Execute ( context . Background ( ) , args , commandkit . Streams { Out : & output , Err : & diagnostics } )
if outcome . Status != 1 || outcome . Cause != nil || outcome . Presentation . Output . RenderError == nil || outcome . Presentation . Output . State != commandkit . DeliveryIncomplete || probe . produced != 1 {
t . Fatalf ( "render failure: %#v %s" , outcome , diagnostics . String ( ) )
}
assertCompletedRunFiles ( t , file , true )
artifacts := filepath . Join ( filepath . Dir ( file ) , "output-must-not-exist" , "run-1" )
if ! strings . Contains ( diagnostics . String ( ) , artifacts ) || ( quiet && output . Len ( ) != 0 ) {
t . Fatalf ( "render failure locations/quiet: %s" , diagnostics . String ( ) )
}
2026-07-26 17:12:57 +00:00
}
}
2026-11-22 04:05:44 +00:00
type failingResultWriter struct {
cause error
2026-07-26 17:12:57 +00:00
}
2026-11-22 04:05:44 +00:00
func ( writer failingResultWriter ) Write ( [ ] byte ) ( int , error ) { return 0 , writer . cause }
2026-07-26 17:12:57 +00:00
2026-11-22 04:05:44 +00:00
type partialResultWriter struct {
content bytes . Buffer
cause error
2026-07-26 17:12:57 +00:00
}
2026-11-22 04:05:44 +00:00
func ( writer * partialResultWriter ) Write ( content [ ] byte ) ( int , error ) {
if len ( content ) > 8 {
content = content [ : 8 ]
2026-07-26 17:12:57 +00:00
}
2026-11-22 04:05:44 +00:00
n , _ := writer . content . Write ( content )
return n , writer . cause
2026-07-26 17:12:57 +00:00
}
2026-11-22 04:05:44 +00:00
var _ io . Writer = ( * partialResultWriter ) ( nil )
func TestPipelinePreflightClassifiesOnlyIdentifiableInvalidSelections ( t * testing . T ) {
// Real native missing-resource errors retain their category separately from
// private lookup causes. A joined operational branch must still win.
probe := & runProbe { }
app , file , input , _ := runFixture ( t , probe )
var diagnostics bytes . Buffer
missing := app . Execute ( context . Background ( ) , [ ] string { "run" , "demo" , "--config" , file , "--input-file" , input , "--llm-profile" , "absent" } , commandkit . Streams { Out : io . Discard , Err : & diagnostics } ) . Cause
for _ , test := range [ ] struct {
cause error
status int
} {
{ missing , 2 } ,
{ errors . Join ( missing , context . Canceled ) , 1 } ,
{ errors . Join ( missing , errors . New ( "unavailable inspection" ) ) , 1 } ,
{ errors . New ( "unavailable inspection" ) , 1 } ,
} {
outcome := pipelineResourceFailure ( test . cause )
if outcome . Status != test . status || ! errors . Is ( outcome . Cause , test . cause ) {
t . Fatalf ( "preflight classification: %#v" , outcome )
}
2026-07-26 17:12:57 +00:00
}
}