@@ -10,6 +10,7 @@ type LogModule = {
1010 detectLogFormat : typeof import ( "../EventHub/index" ) . detectLogFormat ;
1111 LogFormat : typeof import ( "../EventHub/index" ) . LogFormat ;
1212 handleLogEntries : typeof import ( "../EventHub/index" ) . handleLogEntries ;
13+ unwrapEventHubMessage : typeof import ( "../EventHub/index" ) . unwrapEventHubMessage ;
1314} ;
1415
1516type EnvConfig = {
@@ -53,6 +54,142 @@ function loadLogModule(env: EnvConfig = {}): LogModule {
5354 return mod ! ;
5455}
5556
57+ describe ( "unwrapEventHubMessage - eventHub message parsing" , ( ) => {
58+ describe ( "JSON string parsing" , ( ) => {
59+ it ( "should parse JSON string into object" , ( ) => {
60+ const { unwrapEventHubMessage } = loadLogModule ( ) ;
61+ const jsonString = JSON . stringify ( { category : "Test" , message : "Hello" } ) ;
62+
63+ const result = unwrapEventHubMessage ( jsonString ) ;
64+
65+ expect ( result ) . toHaveLength ( 1 ) ;
66+ expect ( result [ 0 ] ) . toBe ( jsonString ) ;
67+ } ) ;
68+
69+ it ( "should keep invalid JSON as string" , ( ) => {
70+ const { unwrapEventHubMessage } = loadLogModule ( ) ;
71+ const invalidJson = "not valid json {" ;
72+
73+ const result = unwrapEventHubMessage ( invalidJson ) ;
74+
75+ expect ( result ) . toHaveLength ( 1 ) ;
76+ expect ( result [ 0 ] ) . toBe ( invalidJson ) ;
77+ } ) ;
78+
79+ it ( "should keep plain text as-is" , ( ) => {
80+ const { unwrapEventHubMessage } = loadLogModule ( ) ;
81+ const plainText = "2025-01-01 INFO Application started" ;
82+
83+ const result = unwrapEventHubMessage ( plainText ) ;
84+
85+ expect ( result ) . toHaveLength ( 1 ) ;
86+ expect ( result [ 0 ] ) . toBe ( plainText ) ;
87+ } ) ;
88+ } ) ;
89+
90+ describe ( "records array wrapper" , ( ) => {
91+ it ( "should unwrap records array from object" , ( ) => {
92+ const { unwrapEventHubMessage } = loadLogModule ( ) ;
93+ const input = {
94+ records : [
95+ { id : 1 , message : "first" } ,
96+ { id : 2 , message : "second" } ,
97+ { id : 3 , message : "third" } ,
98+ ] ,
99+ } ;
100+
101+ const result = unwrapEventHubMessage ( input ) ;
102+
103+ expect ( result ) . toHaveLength ( 3 ) ;
104+ expect ( result [ 0 ] . id ) . toBe ( 1 ) ;
105+ expect ( result [ 1 ] . id ) . toBe ( 2 ) ;
106+ expect ( result [ 2 ] . id ) . toBe ( 3 ) ;
107+ } ) ;
108+
109+ it ( "should unwrap records array from JSON string" , ( ) => {
110+ const { unwrapEventHubMessage } = loadLogModule ( ) ;
111+ const jsonString = JSON . stringify ( {
112+ records : [
113+ { id : 1 , message : "first" } ,
114+ { id : 2 , message : "second" } ,
115+ ] ,
116+ } ) ;
117+
118+ const result = unwrapEventHubMessage ( jsonString ) ;
119+
120+ expect ( result ) . toHaveLength ( 2 ) ;
121+ expect ( result [ 0 ] . id ) . toBe ( 1 ) ;
122+ expect ( result [ 1 ] . id ) . toBe ( 2 ) ;
123+ } ) ;
124+
125+ it ( "should handle empty records array" , ( ) => {
126+ const { unwrapEventHubMessage } = loadLogModule ( ) ;
127+ const input = { records : [ ] } ;
128+
129+ const result = unwrapEventHubMessage ( input ) ;
130+
131+ expect ( result ) . toHaveLength ( 0 ) ;
132+ } ) ;
133+
134+ it ( "should handle single record in array" , ( ) => {
135+ const { unwrapEventHubMessage } = loadLogModule ( ) ;
136+ const input = {
137+ records : [ { id : 1 , message : "only one" } ] ,
138+ } ;
139+
140+ const result = unwrapEventHubMessage ( input ) ;
141+
142+ expect ( result ) . toHaveLength ( 1 ) ;
143+ expect ( result [ 0 ] . id ) . toBe ( 1 ) ;
144+ } ) ;
145+ } ) ;
146+
147+ describe ( "body property wrapper" , ( ) => {
148+ it ( "should unwrap object body" , ( ) => {
149+ const { unwrapEventHubMessage } = loadLogModule ( ) ;
150+ const input = {
151+ body : { event : "user_login" , userId : 123 } ,
152+ } ;
153+
154+ const result = unwrapEventHubMessage ( input ) ;
155+
156+ expect ( result ) . toHaveLength ( 1 ) ;
157+ expect ( result [ 0 ] . event ) . toBe ( "user_login" ) ;
158+ } ) ;
159+
160+ it ( "should unwrap string body" , ( ) => {
161+ const { unwrapEventHubMessage } = loadLogModule ( ) ;
162+ const input = {
163+ body : "plain text log message" ,
164+ } ;
165+
166+ const result = unwrapEventHubMessage ( input ) ;
167+
168+ expect ( result ) . toHaveLength ( 1 ) ;
169+ expect ( result [ 0 ] ) . toBe ( "plain text log message" ) ;
170+ } ) ;
171+ } ) ;
172+
173+ describe ( "Edge cases" , ( ) => {
174+ it ( "should preserve nested objects in records" , ( ) => {
175+ const { unwrapEventHubMessage } = loadLogModule ( ) ;
176+ const jsonString = JSON . stringify ( {
177+ records : [
178+ {
179+ id : 1 ,
180+ nested : { deep : { value : 42 } } ,
181+ } ,
182+ ] ,
183+ } ) ;
184+
185+ const result = unwrapEventHubMessage ( jsonString ) ;
186+
187+ expect ( result ) . toHaveLength ( 1 ) ;
188+ expect ( result [ 0 ] . nested . deep . value ) . toBe ( 42 ) ;
189+ } ) ;
190+ } ) ;
191+ } ) ;
192+
56193describe ( "Application / Subsystem selector resolution" , ( ) => {
57194 it ( "uses default when selector is not set" , ( ) => {
58195 const mod = loadLogModule ( {
0 commit comments