1818 * supported length is refused by a sentence naming the limit.
1919 * - **Exact bytes.** The Sidekick's prefix bindings and the single-file-to-pair
2020 * upgrade measure and copy the file's own bytes through the same reader.
21+ * - **Bounded reading of a long value.** What the screen says a binding is
22+ * stays what it said, without writing out a value to show twenty columns of
23+ * it.
2124 *
2225 * Files are real, in a per-row temporary directory, because what is under test
2326 * is how bytes on disk are read; the chunk size comes from the reader itself so
@@ -36,14 +39,25 @@ import type { Operation } from "effection";
3639import { parseDurableEvent , serializeDurableEvent } from "@executablemd/durable-streams" ;
3740import type { DurableEvent } from "@executablemd/durable-streams" ;
3841
42+ import { INLINE_SOURCE_PATH } from "@executablemd/core" ;
43+ import type { Json } from "@executablemd/durable-streams" ;
44+
45+ import { describeApplication , initialState , NO_AGENT , viewFor } from "../src/repl/application.ts" ;
46+ import type { ReplLive } from "../src/repl/application.ts" ;
47+ import { NO_LIFECYCLE } from "../src/repl/lifecycle.ts" ;
48+ import { fields , readDescription } from "../src/repl/description.ts" ;
49+ import type { ReplRoute } from "../src/repl/route.ts" ;
50+ import type { ReplRenderer } from "../src/repl/renderer.ts" ;
51+ import type { ReplTerminalSize } from "../src/repl/terminal.ts" ;
52+ import { committedContext , useMeasuringRenderer } from "./fixtures/repl/presentation.ts" ;
3953import { ReplHost } from "../src/repl/host.ts" ;
4054import {
4155 HISTORY_LIMIT ,
4256 openExecutionFile ,
4357 readRecords ,
4458 RetainedReplStream ,
4559} from "../src/repl/journal.ts" ;
46- import { EntrySegmentStream } from "../src/repl/entries.ts" ;
60+ import { EntrySegmentStream , ROOT_IMPORT } from "../src/repl/entries.ts" ;
4761import { projectRepl } from "../src/repl/model.ts" ;
4862import { READ_CHUNK } from "../src/repl/record-file.ts" ;
4963import {
@@ -247,6 +261,64 @@ describe("#992 — retained events are kept once and shared", () => {
247261 expect ( held [ 0 ] ) . toBe ( before [ 0 ] ) ;
248262 } ) ;
249263
264+ it ( "H1: a frozen accessor operand is retained as the reading that was written" , function * ( ) {
265+ const directory = yield * useTempDirectory ( "xmd-retained-" ) ;
266+ yield * useHost ( "h1f" ) ;
267+ const path = join ( directory , "h1f.jsonl" ) ;
268+ const physical = new RetainedReplStream ( path ) ;
269+ const observed : DurableEvent [ ] = [ ] ;
270+ physical . onAppend = ( event ) => {
271+ observed . push ( event ) ;
272+ } ;
273+ const segment = new EntrySegmentStream ( physical ) ;
274+
275+ // The caller's own state, read through getters, inside frozen containers.
276+ // Frozen says nothing about what a getter will answer next, so a retained
277+ // event that kept these objects would be a live reading of this state
278+ // rather than a record of what was written.
279+ const caller = { answer : "acknowledged" , held : [ "kept" ] } ;
280+ const value : { readonly nested : { readonly answer : string } } = Object . freeze ( {
281+ nested : Object . freeze ( {
282+ get answer ( ) : string {
283+ return caller . answer ;
284+ } ,
285+ } ) ,
286+ } ) ;
287+ yield * segment . append ( {
288+ type : "yield" ,
289+ coroutineId : "root" ,
290+ description : Object . freeze ( {
291+ type : "test_effect" ,
292+ name : "accessor" ,
293+ get held ( ) : string [ ] {
294+ return caller . held ;
295+ } ,
296+ } ) ,
297+ result : { status : "ok" , value } ,
298+ } ) ;
299+ const written = yield * until ( readFile ( path , "utf8" ) ) ;
300+
301+ caller . answer = "changed after acknowledgement" ;
302+ caller . held . push ( "added later" ) ;
303+
304+ // Every reader, every time, still describes the record the file holds.
305+ for ( const read of [
306+ yield * physical . readAll ( ) ,
307+ yield * segment . readAll ( ) ,
308+ yield * physical . readAll ( ) ,
309+ yield * segment . readAll ( ) ,
310+ observed ,
311+ ] ) {
312+ expect ( read ) . toHaveLength ( 1 ) ;
313+ expect ( read . map ( serializeDurableEvent ) . join ( "" ) ) . toBe ( written ) ;
314+ }
315+ // And they are still one shared graph rather than a copy each.
316+ expect ( ( yield * physical . readAll ( ) ) [ 0 ] ) . toBe ( ( yield * segment . readAll ( ) ) [ 0 ] ) ;
317+ // The caller's own objects are untouched and still answer for the caller.
318+ expect ( value . nested . answer ) . toBe ( "changed after acknowledgement" ) ;
319+ expect ( caller . held ) . toEqual ( [ "kept" , "added later" ] ) ;
320+ } ) ;
321+
250322 it ( "H1: sealing keeps member order, absent and undefined members, and an own __proto__" , function * ( ) {
251323 const directory = yield * useTempDirectory ( "xmd-retained-" ) ;
252324 yield * useHost ( "h1d" ) ;
@@ -492,3 +564,132 @@ describe("#992 — every saved history reads as it did", () => {
492564 }
493565 } ) ;
494566} ) ;
567+
568+ describe ( "#992 — a long value is summarized without being written out" , ( ) => {
569+ const EXECUTION = "summary" ;
570+ const SCREEN : ReplTerminalSize = { columns : 160 , rows : 36 } ;
571+ const NOTHING_LIVE : ReplLive = Object . freeze ( {
572+ output : "" ,
573+ question : undefined ,
574+ expansion : "playing" ,
575+ pausable : false ,
576+ running : false ,
577+ agent : NO_AGENT ,
578+ lifecycle : NO_LIFECYCLE ,
579+ } ) ;
580+
581+ /** One settled entry that published `held`, as a run records one. */
582+ function publishing ( value : Json ) : DurableEvent [ ] {
583+ const content = "```js eval\nconst held = 1;\n```\n" ;
584+ return [
585+ {
586+ type : "yield" ,
587+ coroutineId : "root" ,
588+ description : { type : "import_component" , name : ROOT_IMPORT } ,
589+ result : {
590+ status : "ok" ,
591+ value : { kind : "repository" , path : INLINE_SOURCE_PATH , content } ,
592+ } ,
593+ } ,
594+ {
595+ type : "yield" ,
596+ coroutineId : "root" ,
597+ description : {
598+ type : "eval" ,
599+ name : "eval:eval:root:0" ,
600+ language : "js" ,
601+ "executablemd.source-position" : {
602+ path : INLINE_SOURCE_PATH ,
603+ offset : 0 ,
604+ line : 1 ,
605+ column : 1 ,
606+ } ,
607+ } ,
608+ result : { status : "ok" , value : { value : { held : value } } } ,
609+ } ,
610+ {
611+ type : "close" ,
612+ coroutineId : "root" ,
613+ result : {
614+ status : "ok" ,
615+ value : { status : "ok" , output : "" , value : "" , bindings : { held : value } } ,
616+ } ,
617+ } ,
618+ ] ;
619+ }
620+
621+ /** What the binding rows of this reading say, as a reader reads them. */
622+ function * bindingRows ( value : Json , engine : ReplRenderer ) : Operation < string [ ] > {
623+ const projected = projectRepl ( publishing ( value ) ) ;
624+ if ( ! projected . ok ) {
625+ throw projected . error ;
626+ }
627+ const opening = initialState ( EXECUTION ) ;
628+ // The bindings pane explicitly: the right-hand column defaults to the
629+ // Sidekick, so this asks for the pane the way a person does.
630+ const route : ReplRoute = { ...opening . route , scopes : [ "entry-1" ] , right : "bindings" } ;
631+ const view = viewFor (
632+ Object . freeze ( { ...opening , route } ) ,
633+ projected . value ,
634+ NOTHING_LIVE ,
635+ SCREEN ,
636+ ) ;
637+ if ( ! view . ok ) {
638+ throw view . error ;
639+ }
640+ const context = yield * committedContext ( engine , view . value ) ;
641+ const rows : string [ ] = [ ] ;
642+ for ( const description of describeApplication ( view . value , context ) ) {
643+ const read = readDescription ( description ) ;
644+ const label = fields ( read . input ) ?. [ "label" ] ;
645+ if ( read . key . startsWith ( "binding:" ) && typeof label === "string" ) {
646+ rows . push ( label . trim ( ) ) ;
647+ }
648+ }
649+ return rows ;
650+ }
651+
652+ /**
653+ * The summary writing the whole value out produced.
654+ *
655+ * Twenty columns, with the twentieth given to an ellipsis where there was
656+ * more — stated here as the contract rather than read from the screen's own
657+ * constant, so a changed constant fails this row instead of agreeing with it.
658+ */
659+ function whole ( value : Json ) : string {
660+ const written = JSON . stringify ( value ) ?? "null" ;
661+ return written . length <= 20 ? written : `${ written . slice ( 0 , 19 ) } \u2026` ;
662+ }
663+
664+ it ( "H6: a value too long to show reads exactly as writing it out whole did" , function * ( ) {
665+ const engine = yield * useMeasuringRenderer ( SCREEN ) ;
666+ const values : Json [ ] = [
667+ // Long enough that a summary is a cut, in the shapes a cut could move:
668+ // plain text, text whose every character escapes into two, quotes at the
669+ // front, characters outside the basic plane, and long text nested in a
670+ // container and in a list.
671+ "a" . repeat ( 5000 ) ,
672+ "\n" . repeat ( 5000 ) ,
673+ `${ '"' . repeat ( 10 ) } ${ "b" . repeat ( 5000 ) } ` ,
674+ "\u{1f600}" . repeat ( 3000 ) ,
675+ `\u2014${ "c" . repeat ( 5000 ) } ` ,
676+ { log : "d" . repeat ( 5000 ) } ,
677+ [ "e" . repeat ( 5000 ) ] ,
678+ { first : "f" . repeat ( 5000 ) , second : "g" . repeat ( 5000 ) } ,
679+ // And the lengths either side of the twenty columns a summary keeps, so
680+ // the row that is shown whole is shown whole.
681+ "h" . repeat ( 17 ) ,
682+ "i" . repeat ( 18 ) ,
683+ "j" . repeat ( 19 ) ,
684+ "alpha" ,
685+ 42 ,
686+ null ,
687+ true ,
688+ { a : 1 } ,
689+ [ ] ,
690+ ] ;
691+ for ( const value of values ) {
692+ expect ( yield * bindingRows ( value , engine ) ) . toEqual ( [ `held = ${ whole ( value ) } ` ] ) ;
693+ }
694+ } ) ;
695+ } ) ;
0 commit comments