2323
2424import { describe , it } from "@executablemd/test-support/bdd" ;
2525import { expect } from "@executablemd/test-support/expect" ;
26- import { all , createContext , Err , Ok , scoped , sleep , spawn , withResolvers } from "effection" ;
26+ import {
27+ all ,
28+ createContext ,
29+ ensure ,
30+ Err ,
31+ Ok ,
32+ race ,
33+ scoped ,
34+ sleep ,
35+ spawn ,
36+ withResolvers ,
37+ } from "effection" ;
2738import type { Operation , Result } from "effection" ;
2839import type { Json } from "../src/types.ts" ;
2940import { forEach } from "@effectionx/stream-helpers" ;
@@ -49,6 +60,7 @@ import { inlineSource } from "../src/root-source.ts";
4960import { registerComponents } from "../src/components/registration.ts" ;
5061import { createScannerWith , createSecretScanner } from "../src/secrets/scanner.ts" ;
5162import type { SecretScanner } from "../src/secrets/scanner.ts" ;
63+ import { SecretDetectedError } from "../src/secrets/findings.ts" ;
5264import type { SecretFinding } from "../src/secrets/findings.ts" ;
5365import {
5466 scanSecrets ,
@@ -870,8 +882,158 @@ describe("default-on secret detection", () => {
870882 expect ( replay . appendCount ) . toBe ( 0 ) ;
871883 } ) ;
872884 } ) ;
885+
886+ describe ( "output and settlement when a rendering is detected" , ( ) => {
887+ it ( "settles the clean control with its complete output" , function * ( ) {
888+ const run = yield * settledRendering ( "Token loopback-credential" ) ;
889+
890+ expect ( run . result . ok ) . toBe ( true ) ;
891+ expect ( run . chunks ) . toEqual ( [
892+ "# Probe\n\n" ,
893+ "\n\nThe header was Token loopback-credential.\n" ,
894+ ] ) ;
895+ expect ( run . close ) . toBe ( "# Probe\n\n\n\nThe header was Token loopback-credential.\n" ) ;
896+ expect ( run . events . map ( label ) ) . toEqual ( [
897+ "yield(import_component)" ,
898+ "yield(import_component)" ,
899+ "close(root)" ,
900+ ] ) ;
901+ const close = run . events . at ( - 1 ) ;
902+ expect ( close ?. type === "close" && close . result . status ) . toBe ( "ok" ) ;
903+ } ) ;
904+
905+ it ( "settles a detected rendering with Err and only safe partial output" , function * ( ) {
906+ const run = yield * settledRendering ( CANARY ) ;
907+
908+ expect ( run . result . ok ) . toBe ( false ) ;
909+ const error = run . result . ok ? new Error ( "unreachable" ) : run . result . error ;
910+ expect ( error instanceof SecretDetectedError ) . toBe ( true ) ;
911+
912+ // The unsafe chunk is withheld in full; what closed is exactly the
913+ // cleared prefix the consumer already received.
914+ expect ( run . chunks ) . toEqual ( [ "# Probe\n\n" ] ) ;
915+ expect ( run . close ) . toBe ( run . chunks . join ( "" ) ) ;
916+
917+ // The document's own source template legitimately persists with the root
918+ // import, so the sweep hunts the rendered sentence — the one carrying
919+ // the canary — rather than the template text.
920+ const tainted = `The header was ${ CANARY } .` ;
921+
922+ const journal = run . events . map ( serializeDurableEvent ) . join ( "" ) ;
923+ const readable = [
924+ ...run . chunks ,
925+ run . close ,
926+ String ( error ) ,
927+ error . name ,
928+ error . message ,
929+ error . stack ?? "" ,
930+ String ( error . cause ?? "" ) ,
931+ JSON . stringify ( findingsOf ( error ) ) ,
932+ journal ,
933+ ] ;
934+ for ( const channel of readable ) {
935+ expect ( channel ) . not . toContain ( CANARY ) ;
936+ expect ( channel ) . not . toContain ( tainted ) ;
937+ }
938+
939+ expect ( run . events . map ( label ) ) . toEqual ( [
940+ "yield(import_component)" ,
941+ "yield(import_component)" ,
942+ "close(root)" ,
943+ ] ) ;
944+ const close = run . events . at ( - 1 ) ;
945+ expect ( close ?. type === "close" && close . result . status ) . toBe ( "err" ) ;
946+ expect ( journal ) . toContain ( "SecretDetectedError" ) ;
947+
948+ expect ( run . order ) . toEqual ( [ "output-closed" , "teardown" , "completion-observed" ] ) ;
949+ } ) ;
950+ } ) ;
873951} ) ;
874952
953+ /** A clean root that renders a registered component's value into its body. */
954+ const RENDERING = `# Probe
955+
956+ <Header as="header" />
957+
958+ The header was {header}.
959+ ` ;
960+
961+ /** What one drain-then-await run of the rendering probe left behind. */
962+ interface SettledRendering {
963+ chunks : string [ ] ;
964+ close : string ;
965+ result : Result < Json > ;
966+ events : DurableEvent [ ] ;
967+ order : string [ ] ;
968+ }
969+
970+ /**
971+ * Run the rendering probe end to end — output drained first, completion
972+ * awaited second — bounded by a failing five-second timer.
973+ *
974+ * The registered return value is the only variable, so what differs between
975+ * the clean control and the detected case is what detection did. A real
976+ * interval holds the event loop open for the whole sequence: an abandoned
977+ * completion under an idle loop is a test-runner diagnostic, while a held-open
978+ * loop turns it into an indefinite hang, and the losing timer turns either
979+ * into a plain failure.
980+ */
981+ function * settledRendering ( value : string ) : Operation < SettledRendering > {
982+ const backend = new InMemoryStream ( ) ;
983+ const chunks : string [ ] = [ ] ;
984+ const order : string [ ] = [ ] ;
985+
986+ return yield * scoped ( function * ( ) {
987+ yield * registerComponents ( [
988+ {
989+ name : "Header" ,
990+ origin : "test:settlement" ,
991+ props : { type : "object" , properties : { } , additionalProperties : false } ,
992+ returns : { type : "string" } ,
993+ // deno-lint-ignore require-yield
994+ * fn ( ) : Operation < Json > {
995+ return value ;
996+ } ,
997+ } ,
998+ ] ) ;
999+ yield * Execution . around ( {
1000+ * document ( [ props ] , next ) {
1001+ yield * ensure ( ( ) => {
1002+ order . push ( "teardown" ) ;
1003+ } ) ;
1004+ return yield * next ( props ) ;
1005+ } ,
1006+ } ) ;
1007+
1008+ const interval = setInterval ( ( ) => { } , 50 ) ;
1009+ yield * ensure ( ( ) => {
1010+ clearInterval ( interval ) ;
1011+ } ) ;
1012+
1013+ return yield * race ( [ drainThenAwait ( ) , timedOut ( ) ] ) ;
1014+ } ) ;
1015+
1016+ function * drainThenAwait ( ) : Operation < SettledRendering > {
1017+ const execution = yield * execute ( { ...inlineSource ( RENDERING ) , stream : backend } ) ;
1018+ const subscription = yield * execution . output ;
1019+ let next = yield * subscription . next ( ) ;
1020+ while ( ! next . done ) {
1021+ chunks . push ( next . value ) ;
1022+ next = yield * subscription . next ( ) ;
1023+ }
1024+ order . push ( "output-closed" ) ;
1025+ const close = next . value ;
1026+ const result = yield * execution ;
1027+ order . push ( "completion-observed" ) ;
1028+ return { chunks, close, result, events : backend . snapshot ( ) , order } ;
1029+ }
1030+
1031+ function * timedOut ( ) : Operation < never > {
1032+ yield * sleep ( 5000 ) ;
1033+ throw new Error ( "the drain-then-await sequence did not settle within five seconds" ) ;
1034+ }
1035+ }
1036+
8751037/**
8761038 * Run a document and return what `observe` saw from inside the execution.
8771039 *
0 commit comments