@@ -4,6 +4,57 @@ import { tmpdir } from "node:os"
44import path from "node:path"
55
66const entry = path . join ( import . meta. dir , "../../../src/index.ts" )
7+ const cliEnv = { ...process . env , ALTIMATE_TELEMETRY_DISABLED : "1" , ALTIMATE_TOOL_DAEMON : "0" }
8+
9+ /** A manifest whose hub has 450 readers, so its `dbt_lineage` graph is far over a pipe's 64 KiB. */
10+ async function bigAnswerProject ( ) {
11+ const dir = await mkdtemp ( path . join ( tmpdir ( ) , "tool-cli-" ) )
12+ const model = ( name : string , deps : string [ ] ) => ( {
13+ resource_type : "model" ,
14+ unique_id : `model.shop.${ name } ` ,
15+ name,
16+ alias : name ,
17+ database : "shop" ,
18+ schema : "main" ,
19+ original_file_path : `models/${ name } .sql` ,
20+ depends_on : { nodes : deps . map ( ( dep ) => `model.shop.${ dep } ` ) } ,
21+ config : { materialized : "table" } ,
22+ compiled_code : "select 1 as id" ,
23+ columns : { } ,
24+ } )
25+ const readers = Array . from ( { length : 450 } , ( _ , i ) => model ( `reader_with_a_longish_name_${ i } ` , [ "hub" ] ) )
26+ const nodes = [ model ( "hub" , [ ] ) , ...readers ]
27+ const manifest = path . join ( dir , "manifest.json" )
28+ await writeFile (
29+ manifest ,
30+ JSON . stringify ( {
31+ metadata : { adapter_type : "duckdb" } ,
32+ nodes : Object . fromEntries ( nodes . map ( ( node ) => [ node . unique_id , node ] ) ) ,
33+ sources : { } ,
34+ exposures : { } ,
35+ } ) ,
36+ )
37+ await writeFile ( path . join ( dir , "args.json" ) , JSON . stringify ( { manifest_path : manifest , model : "hub" , include_graph : true } ) )
38+ return dir
39+ }
40+
41+ /**
42+ * Runs the CLI with a reader that waits for it to exit, up to 20 s, as a busy event loop can: the pipe is full
43+ * until then. A CLI that drops what the pipe can't take exits at once and leaves 64 KiB; one that writes it all waits.
44+ */
45+ async function lateReader ( dir : string , command : string ) {
46+ const proc = Bun . spawn (
47+ [
48+ "sh" ,
49+ "-c" ,
50+ `{ "$0" --conditions=browser "$1" ${ command } ; touch done; } | { i=0; until [ -e done ] || [ $i -ge 200 ]; do sleep 0.1; i=$((i+1)); done; cat > out.json; }` ,
51+ process . execPath ,
52+ entry ,
53+ ] ,
54+ { cwd : dir , stderr : "ignore" , env : cliEnv } ,
55+ )
56+ return proc . exited
57+ }
758
859async function cli ( args : string [ ] , stdin ?: string ) {
960 const dir = await mkdtemp ( path . join ( tmpdir ( ) , "tool-cli-" ) )
@@ -30,49 +81,36 @@ describe("tool CLI", () => {
3081
3182 // Past the 64 KiB a pipe holds, a process that exits with the rest still buffered loses it.
3283 test ( "a big --json answer reaches a reader that starts late whole" , async ( ) => {
84+ const dir = await bigAnswerProject ( )
85+ expect ( await lateReader ( dir , "tool run dbt_lineage --args - --json < args.json" ) ) . toBe ( 0 )
86+ const stdout = await Bun . file ( path . join ( dir , "out.json" ) ) . text ( )
87+ expect ( stdout . length ) . toBeGreaterThan ( 100_000 )
88+ expect ( Object . keys ( JSON . parse ( stdout ) . metadata . graph . nodes ) . length ) . toBe ( 451 )
89+ } , 60000 )
90+
91+ test ( "tool list --json reaches a reader that starts late whole" , async ( ) => {
3392 const dir = await mkdtemp ( path . join ( tmpdir ( ) , "tool-cli-" ) )
34- const model = ( name : string , deps : string [ ] ) => ( {
35- resource_type : "model" ,
36- unique_id : `model.shop.${ name } ` ,
37- name,
38- alias : name ,
39- database : "shop" ,
40- schema : "main" ,
41- original_file_path : `models/${ name } .sql` ,
42- depends_on : { nodes : deps . map ( ( dep ) => `model.shop.${ dep } ` ) } ,
43- config : { materialized : "table" } ,
44- compiled_code : "select 1 as id" ,
45- columns : { } ,
46- } )
47- const readers = Array . from ( { length : 450 } , ( _ , i ) => model ( `reader_with_a_longish_name_${ i } ` , [ "hub" ] ) )
48- const nodes = [ model ( "hub" , [ ] ) , ...readers ]
49- const manifest = path . join ( dir , "manifest.json" )
50- await writeFile (
51- manifest ,
52- JSON . stringify ( {
53- metadata : { adapter_type : "duckdb" } ,
54- nodes : Object . fromEntries ( nodes . map ( ( node ) => [ node . unique_id , node ] ) ) ,
55- sources : { } ,
56- exposures : { } ,
57- } ) ,
58- )
59- await writeFile ( path . join ( dir , "args.json" ) , JSON . stringify ( { manifest_path : manifest , model : "hub" , include_graph : true } ) )
60- // The reader waits for the CLI to exit, up to 20 s, as a busy event loop can: the pipe is full until then.
61- // A CLI that drops what the pipe can't take exits at once and leaves 64 KiB; one that writes it all waits.
93+ expect ( await lateReader ( dir , "tool list --json" ) ) . toBe ( 0 )
94+ const stdout = await Bun . file ( path . join ( dir , "out.json" ) ) . text ( )
95+ expect ( stdout . length ) . toBeGreaterThan ( 65_536 )
96+ expect ( ( JSON . parse ( stdout ) as unknown [ ] ) . length ) . toBeGreaterThan ( 0 )
97+ } , 60000 )
98+
99+ test ( "a reader that leaves early is not an error" , async ( ) => {
100+ const dir = await bigAnswerProject ( )
62101 const proc = Bun . spawn (
63102 [
64103 "sh" ,
65104 "-c" ,
66- `{ "$0" --conditions=browser "$1" tool run dbt_lineage --args - --json < args.json; touch done; } | { i=0; until [ -e done ] || [ $i -ge 200 ]; do sleep 0.1; i=$((i+1)); done; cat > out.json; } ` ,
105+ `{ "$0" --conditions=browser "$1" tool run dbt_lineage --args - --json < args.json 2> err.txt; echo $? > code.txt; } | head -c 10 > /dev/null ` ,
67106 process . execPath ,
68107 entry ,
69108 ] ,
70- { cwd : dir , stderr : "ignore" , env : { ... process . env , ALTIMATE_TELEMETRY_DISABLED : "1" , ALTIMATE_TOOL_DAEMON : "0" } } ,
109+ { cwd : dir , stderr : "ignore" , env : cliEnv } ,
71110 )
72- expect ( await proc . exited ) . toBe ( 0 )
73- const stdout = await Bun . file ( path . join ( dir , "out.json" ) ) . text ( )
74- expect ( stdout . length ) . toBeGreaterThan ( 100_000 )
75- expect ( Object . keys ( JSON . parse ( stdout ) . metadata . graph . nodes ) . length ) . toBe ( 451 )
111+ await proc . exited
112+ expect ( ( await Bun . file ( path . join ( dir , "code.txt" ) ) . text ( ) ) . trim ( ) ) . toBe ( "0" )
113+ expect ( await Bun . file ( path . join ( dir , "err.txt" ) ) . text ( ) ) . toBe ( "" )
76114 } , 60000 )
77115
78116 test ( "rejects --args - with empty stdin" , async ( ) => {
0 commit comments