@@ -8,6 +8,15 @@ import {
88} from "@jest/globals" ;
99import { RunHistoryService } from "../../services/runHistoryService" ;
1010
11+ // Matches structure from @altimateai /dbt-integration RunResultsData
12+ const createRunResults = ( overrides : Record < string , unknown > = { } ) => ( {
13+ metadata : { invocation_id : "test-invocation" } ,
14+ args : { which : "run" , select : [ "model1" ] } ,
15+ results : [ { unique_id : "model.project.model1" , status : "success" } ] ,
16+ elapsed_time : 1.0 ,
17+ ...overrides ,
18+ } ) ;
19+
1120describe ( "RunHistoryService" , ( ) => {
1221 let service : RunHistoryService ;
1322
@@ -21,86 +30,80 @@ describe("RunHistoryService", () => {
2130 } ) ;
2231
2332 describe ( "addCompletedRun" , ( ) => {
24- it ( "should add a completed run to history" , ( ) => {
25- const runResults = {
26- metadata : {
27- invocation_id : "test-invocation-123" ,
28- } ,
29- args : {
30- which : "run" ,
31- select : [ "model1" , "model2" ] ,
32- } ,
33+ it ( "should parse RunResultsData structure correctly" , ( ) => {
34+ const runResults = createRunResults ( {
35+ metadata : { invocation_id : "test-123" } ,
36+ args : { which : "build" , select : [ "+my_model+" ] } ,
3337 results : [
3438 {
35- unique_id : "model.project.model1 " ,
39+ unique_id : "model.jaffle.stg_customers " ,
3640 status : "success" ,
37- execution_time : 1.5 ,
41+ execution_time : 1.23 ,
42+ message : "CREATE TABLE" ,
3843 } ,
3944 ] ,
4045 elapsed_time : 2.5 ,
41- } ;
46+ } ) ;
4247
4348 const entry = service . addCompletedRun ( runResults , "test-project" ) ;
4449
45- expect ( entry . id ) . toBe ( "test-invocation-123" ) ;
46- expect ( entry . command ) . toBe ( "run" ) ;
47- expect ( entry . args ) . toEqual ( [ "model1" , "model2" ] ) ;
48- expect ( entry . projectName ) . toBe ( "test-project" ) ;
50+ expect ( entry . id ) . toBe ( "test-123" ) ;
51+ expect ( entry . command ) . toBe ( "build" ) ;
52+ expect ( entry . args ) . toEqual ( [ "+my_model+" ] ) ;
4953 expect ( entry . elapsedTime ) . toBe ( 2.5 ) ;
50- expect ( entry . models ) . toHaveLength ( 1 ) ;
51- } ) ;
52-
53- it ( "should generate id when invocation_id is missing" , ( ) => {
54- const runResults = {
55- metadata : { } ,
56- results : [ ] ,
57- elapsed_time : 1.0 ,
58- } ;
59-
60- const entry = service . addCompletedRun ( runResults , "test-project" ) ;
54+ expect ( entry . projectName ) . toBe ( "test-project" ) ;
55+ expect ( entry . models [ 0 ] ) . toMatchObject ( {
56+ name : "stg_customers" ,
57+ uniqueId : "model.jaffle.stg_customers" ,
58+ status : "success" ,
59+ executionTime : 1.23 ,
60+ message : "CREATE TABLE" ,
61+ resourceType : "model" ,
62+ } ) ;
63+ } ) ;
64+
65+ it ( "should handle minimal/missing fields with defaults" , ( ) => {
66+ const entry = service . addCompletedRun (
67+ { results : [ { unique_id : "model.p.m" } ] , elapsed_time : 0.5 } ,
68+ "project" ,
69+ ) ;
6170
6271 expect ( entry . id ) . toMatch ( / ^ r u n - \d + $ / ) ;
63- } ) ;
64-
65- it ( "should use 'unknown' command when args.which is missing" , ( ) => {
66- const runResults = {
67- metadata : { } ,
68- results : [ ] ,
69- elapsed_time : 1.0 ,
70- } ;
71-
72- const entry = service . addCompletedRun ( runResults , "test-project" ) ;
73-
7472 expect ( entry . command ) . toBe ( "unknown" ) ;
75- } ) ;
76-
77- it ( "should use empty array when args.select is missing" , ( ) => {
78- const runResults = {
79- metadata : { } ,
80- args : { } ,
81- results : [ ] ,
82- elapsed_time : 1.0 ,
83- } ;
84-
85- const entry = service . addCompletedRun ( runResults , "test-project" ) ;
86-
8773 expect ( entry . args ) . toEqual ( [ ] ) ;
88- } ) ;
74+ expect ( entry . models [ 0 ] . status ) . toBe ( "unknown" ) ;
75+ expect ( entry . models [ 0 ] . executionTime ) . toBeNull ( ) ;
76+ } ) ;
77+
78+ it . each ( [
79+ [ "model.project.my_model" , "model" , "my_model" ] ,
80+ [ "test.project.not_null_id" , "test" , "not_null_id" ] ,
81+ [ "seed.project.raw_data" , "seed" , "raw_data" ] ,
82+ [ "snapshot.project.orders_snap" , "snapshot" , "orders_snap" ] ,
83+ [ "unknown.project.something" , "model" , "something" ] ,
84+ [ "simplemodel" , "model" , "simplemodel" ] ,
85+ ] ) (
86+ "should parse unique_id %s as resourceType=%s, name=%s" ,
87+ ( uniqueId , expectedType , expectedName ) => {
88+ const entry = service . addCompletedRun (
89+ { results : [ { unique_id : uniqueId } ] , elapsed_time : 0.5 } ,
90+ "project" ,
91+ ) ;
92+
93+ expect ( entry . models [ 0 ] . resourceType ) . toBe ( expectedType ) ;
94+ expect ( entry . models [ 0 ] . name ) . toBe ( expectedName ) ;
95+ } ,
96+ ) ;
8997
9098 it ( "should add new runs at the beginning of history" , ( ) => {
91- const runResults1 = {
92- metadata : { invocation_id : "first" } ,
93- results : [ ] ,
94- elapsed_time : 1.0 ,
95- } ;
96- const runResults2 = {
97- metadata : { invocation_id : "second" } ,
98- results : [ ] ,
99- elapsed_time : 1.0 ,
100- } ;
101-
102- service . addCompletedRun ( runResults1 , "project" ) ;
103- service . addCompletedRun ( runResults2 , "project" ) ;
99+ service . addCompletedRun (
100+ createRunResults ( { metadata : { invocation_id : "first" } } ) ,
101+ "project" ,
102+ ) ;
103+ service . addCompletedRun (
104+ createRunResults ( { metadata : { invocation_id : "second" } } ) ,
105+ "project" ,
106+ ) ;
104107
105108 const history = service . getHistory ( ) ;
106109 expect ( history [ 0 ] . id ) . toBe ( "second" ) ;
@@ -111,17 +114,11 @@ describe("RunHistoryService", () => {
111114 const listener = jest . fn ( ) ;
112115 service . onHistoryChanged ( listener ) ;
113116
114- const runResults = {
115- metadata : { invocation_id : "test" } ,
116- results : [ ] ,
117- elapsed_time : 1.0 ,
118- } ;
119-
120- service . addCompletedRun ( runResults , "project" ) ;
117+ service . addCompletedRun ( createRunResults ( ) , "project" ) ;
121118
122119 expect ( listener ) . toHaveBeenCalledTimes ( 1 ) ;
123120 expect ( listener ) . toHaveBeenCalledWith (
124- expect . objectContaining ( { id : "test" } ) ,
121+ expect . objectContaining ( { id : "test-invocation " } ) ,
125122 ) ;
126123 } ) ;
127124 } ) ;
@@ -132,13 +129,7 @@ describe("RunHistoryService", () => {
132129 } ) ;
133130
134131 it ( "should return a copy of history array" , ( ) => {
135- const runResults = {
136- metadata : { invocation_id : "test" } ,
137- results : [ ] ,
138- elapsed_time : 1.0 ,
139- } ;
140-
141- service . addCompletedRun ( runResults , "project" ) ;
132+ service . addCompletedRun ( createRunResults ( ) , "project" ) ;
142133
143134 const history1 = service . getHistory ( ) ;
144135 const history2 = service . getHistory ( ) ;
@@ -148,127 +139,8 @@ describe("RunHistoryService", () => {
148139 } ) ;
149140 } ) ;
150141
151- describe ( "parseResults" , ( ) => {
152- it ( "should parse model results correctly" , ( ) => {
153- const runResults = {
154- metadata : { invocation_id : "test" } ,
155- results : [
156- {
157- unique_id : "model.jaffle_shop.stg_customers" ,
158- status : "success" ,
159- execution_time : 1.23 ,
160- message : "OK" ,
161- } ,
162- ] ,
163- elapsed_time : 2.0 ,
164- } ;
165-
166- const entry = service . addCompletedRun ( runResults , "project" ) ;
167- const model = entry . models [ 0 ] ;
168-
169- expect ( model . name ) . toBe ( "stg_customers" ) ;
170- expect ( model . uniqueId ) . toBe ( "model.jaffle_shop.stg_customers" ) ;
171- expect ( model . status ) . toBe ( "success" ) ;
172- expect ( model . executionTime ) . toBe ( 1.23 ) ;
173- expect ( model . message ) . toBe ( "OK" ) ;
174- expect ( model . resourceType ) . toBe ( "model" ) ;
175- } ) ;
176-
177- it ( "should extract resource type from unique_id" , ( ) => {
178- const runResults = {
179- metadata : { invocation_id : "test" } ,
180- results : [
181- { unique_id : "model.project.my_model" , status : "success" } ,
182- { unique_id : "test.project.my_test" , status : "pass" } ,
183- { unique_id : "seed.project.my_seed" , status : "success" } ,
184- { unique_id : "snapshot.project.my_snapshot" , status : "success" } ,
185- ] ,
186- elapsed_time : 1.0 ,
187- } ;
188-
189- const entry = service . addCompletedRun ( runResults , "project" ) ;
190-
191- expect ( entry . models [ 0 ] . resourceType ) . toBe ( "model" ) ;
192- expect ( entry . models [ 1 ] . resourceType ) . toBe ( "test" ) ;
193- expect ( entry . models [ 2 ] . resourceType ) . toBe ( "seed" ) ;
194- expect ( entry . models [ 3 ] . resourceType ) . toBe ( "snapshot" ) ;
195- } ) ;
196-
197- it ( "should default to 'model' for unknown resource types" , ( ) => {
198- const runResults = {
199- metadata : { invocation_id : "test" } ,
200- results : [
201- { unique_id : "unknown.project.something" , status : "success" } ,
202- ] ,
203- elapsed_time : 1.0 ,
204- } ;
205-
206- const entry = service . addCompletedRun ( runResults , "project" ) ;
207-
208- expect ( entry . models [ 0 ] . resourceType ) . toBe ( "model" ) ;
209- } ) ;
210-
211- it ( "should extract model name from unique_id" , ( ) => {
212- const runResults = {
213- metadata : { invocation_id : "test" } ,
214- results : [
215- { unique_id : "model.jaffle_shop.stg_customers" , status : "success" } ,
216- {
217- unique_id :
218- "test.jaffle_shop.schema_test.accepted_values_customers_status" ,
219- status : "pass" ,
220- } ,
221- ] ,
222- elapsed_time : 1.0 ,
223- } ;
224-
225- const entry = service . addCompletedRun ( runResults , "project" ) ;
226-
227- expect ( entry . models [ 0 ] . name ) . toBe ( "stg_customers" ) ;
228- expect ( entry . models [ 1 ] . name ) . toBe ( "accepted_values_customers_status" ) ;
229- } ) ;
230-
231- it ( "should handle missing status with default 'unknown'" , ( ) => {
232- const runResults = {
233- metadata : { invocation_id : "test" } ,
234- results : [ { unique_id : "model.project.model1" } ] ,
235- elapsed_time : 1.0 ,
236- } ;
237-
238- const entry = service . addCompletedRun ( runResults , "project" ) ;
239-
240- expect ( entry . models [ 0 ] . status ) . toBe ( "unknown" ) ;
241- } ) ;
242-
243- it ( "should handle missing execution_time with default 0" , ( ) => {
244- const runResults = {
245- metadata : { invocation_id : "test" } ,
246- results : [ { unique_id : "model.project.model1" , status : "success" } ] ,
247- elapsed_time : 1.0 ,
248- } ;
249-
250- const entry = service . addCompletedRun ( runResults , "project" ) ;
251-
252- expect ( entry . models [ 0 ] . executionTime ) . toBe ( 0 ) ;
253- } ) ;
254-
255- it ( "should handle unique_id without dots" , ( ) => {
256- const runResults = {
257- metadata : { invocation_id : "test" } ,
258- results : [ { unique_id : "simplemodel" , status : "success" } ] ,
259- elapsed_time : 1.0 ,
260- } ;
261-
262- const entry = service . addCompletedRun ( runResults , "project" ) ;
263-
264- expect ( entry . models [ 0 ] . name ) . toBe ( "simplemodel" ) ;
265- expect ( entry . models [ 0 ] . resourceType ) . toBe ( "model" ) ; // defaults to model
266- } ) ;
267- } ) ;
268-
269142 describe ( "dispose" , ( ) => {
270- it ( "should dispose event emitter" , ( ) => {
271- // Just verify dispose doesn't throw
143+ it ( "should dispose without throwing" , ( ) => {
272144 expect ( ( ) => service . dispose ( ) ) . not . toThrow ( ) ;
273145 } ) ;
274146 } ) ;
0 commit comments