1- import { StepFunctionContextService } from "./step-function-service" ;
1+ import { PARENT_ID , StepFunctionContextService } from "./step-function-service" ;
22
33describe ( "StepFunctionContextService" , ( ) => {
44 const stepFunctionEvent = {
@@ -194,8 +194,8 @@ describe("StepFunctionContextService", () => {
194194
195195 expect ( spanContext ) . not . toBeNull ( ) ;
196196
197- expect ( spanContext ?. toTraceId ( ) ) . toBe ( "947965466153612645 " ) ;
198- expect ( spanContext ?. toSpanId ( ) ) . toBe ( "4602916161841036335 " ) ;
197+ expect ( spanContext ?. toTraceId ( ) ) . toBe ( "1139193989631387307 " ) ;
198+ expect ( spanContext ?. toSpanId ( ) ) . toBe ( "5892738536804826142 " ) ;
199199 expect ( spanContext ?. sampleMode ( ) ) . toBe ( "1" ) ;
200200 expect ( spanContext ?. source ) . toBe ( "event" ) ;
201201 } ) ;
@@ -211,54 +211,55 @@ describe("StepFunctionContextService", () => {
211211 } ) ;
212212 } ) ;
213213
214- describe ( "deterministicMd5HashToBigIntString " , ( ) => {
214+ describe ( "deterministicSha256HashToBigIntString " , ( ) => {
215215 it ( "returns the same hash number generated in `logs backend` for a random string" , ( ) => {
216216 const instance = StepFunctionContextService . instance ( ) ;
217- const hash = instance [ "deterministicMd5HashToBigIntString " ] ( "some_testing_random_string" ) ;
218- expect ( hash ) . toEqual ( "2251275791555400689 " ) ;
217+ const hash = instance [ "deterministicSha256HashToBigIntString " ] ( "some_testing_random_string" , PARENT_ID ) ;
218+ expect ( hash ) . toEqual ( "4364271812988819936 " ) ;
219219 } ) ;
220220
221221 it ( "returns the same hash number generated in `logs backend` for execution id # state name # entered time" , ( ) => {
222222 const instance = StepFunctionContextService . instance ( ) ;
223- const hash = instance [ "deterministicMd5HashToBigIntString " ] (
223+ const hash = instance [ "deterministicSha256HashToBigIntString " ] (
224224 "arn:aws:states:sa-east-1:601427271234:express:DatadogStateMachine:acaf1a67-336a-e854-1599-2a627eb2dd8a:c8baf081-31f1-464d-971f-70cb17d01111#step-one#2022-12-08T21:08:19.224Z" ,
225+ PARENT_ID ,
225226 ) ;
226- expect ( hash ) . toEqual ( "8034507082463708833 " ) ;
227+ expect ( hash ) . toEqual ( "4340734536022949921 " ) ;
227228 } ) ;
228229 } ) ;
229230
230- describe ( "deterministicMd5HashInBinary " , ( ) => {
231+ describe ( "deterministicSha256Hash " , ( ) => {
231232 it . each ( [
232233 [
233234 "a random string" ,
234235 "some_testing_random_string" ,
235- "0001111100111110001000110110011110010111000110001001001111110001 " ,
236+ "0011110010010001000000100001011101001100011100101101100111100000 " ,
236237 ] ,
237238 [
238239 "an execution id" ,
239240 "arn:aws:states:sa-east-1:601427271234:express:DatadogStateMachine:acaf1a67-336a-e854-1599-2a627eb2dd8a:c8baf081-31f1-464d-971f-70cb17d041f4" ,
240- "0010010000101100100000101011111101111100110110001110111100111101 " ,
241+ "0100010100110010010010100001011001110100111011010100110010000100 " ,
241242 ] ,
242243 [
243244 "another execution id" ,
244245 "arn:aws:states:sa-east-1:601427271234:express:DatadogStateMachine:acaf1a67-336a-e854-1599-2a627eb2dd8a:c8baf081-31f1-464d-971f-70cb17d01111" ,
245- "0010001100110000011011011111010000100111100000110000100100101010 " ,
246+ "0010111110001100100010000101001100110000000000010111011100101011 " ,
246247 ] ,
247248 [
248249 "execution id # state name # entered time" ,
249250 "arn:aws:states:sa-east-1:601427271234:express:DatadogStateMachine:acaf1a67-336a-e854-1599-2a627eb2dd8a:c8baf081-31f1-464d-971f-70cb17d01111#step-one#2022-12-08T21:08:19.224Z" ,
250- "0110111110000000010011011001111101110011100111000000011010100001 " ,
251+ "0011110000111101011000110000111111110011111010110000000000100001 " ,
251252 ] ,
252253 ] ) ( "returns the same hash number generated in `logs backend` for %s" , ( _ , str , expected ) => {
253254 const instance = StepFunctionContextService . instance ( ) ;
254- const hash = instance [ "deterministicMd5HashInBinary " ] ( str ) ;
255+ const hash = instance [ "deterministicSha256Hash " ] ( str , PARENT_ID ) ;
255256 expect ( hash ) . toEqual ( expected ) ;
256257 } ) ;
257258
258259 it ( "returns a hash always leading with 0" , ( ) => {
259260 const instance = StepFunctionContextService . instance ( ) ;
260261 for ( let i = 0 ; i < 20 ; i ++ ) {
261- const hash = instance [ "deterministicMd5HashInBinary " ] ( i . toString ( ) ) ;
262+ const hash = instance [ "deterministicSha256Hash " ] ( i . toString ( ) , PARENT_ID ) ;
262263 expect ( hash . substring ( 0 , 1 ) ) . toMatch ( "0" ) ;
263264 }
264265 } ) ;
@@ -268,36 +269,44 @@ describe("StepFunctionContextService", () => {
268269 const times = 20 ;
269270 for ( let i = 0 ; i < times ; i ++ ) {
270271 for ( let j = i + 1 ; j < times ; j ++ ) {
271- const hash1 = instance [ "deterministicMd5HashInBinary " ] ( i . toString ( ) ) ;
272- const hash2 = instance [ "deterministicMd5HashInBinary " ] ( j . toString ( ) ) ;
272+ const hash1 = instance [ "deterministicSha256Hash " ] ( i . toString ( ) , PARENT_ID ) ;
273+ const hash2 = instance [ "deterministicSha256Hash " ] ( j . toString ( ) , PARENT_ID ) ;
273274 expect ( hash1 ) . not . toMatch ( hash2 ) ;
274275 }
275276 }
276277 } ) ;
277278 } ) ;
278279
279- describe ( "hexToBinary " , ( ) => {
280+ describe ( "numberToBinaryString " , ( ) => {
280281 const instance = StepFunctionContextService . instance ( ) ;
281282 it . each ( [
282- [ "0" , "0000" ] ,
283- [ "1" , "0001" ] ,
284- [ "2" , "0010" ] ,
285- [ "3" , "0011" ] ,
286- [ "4" , "0100" ] ,
287- [ "5" , "0101" ] ,
288- [ "6" , "0110" ] ,
289- [ "7" , "0111" ] ,
290- [ "8" , "1000" ] ,
291- [ "9" , "1001" ] ,
292- [ "a" , "1010" ] ,
293- [ "b" , "1011" ] ,
294- [ "c" , "1100" ] ,
295- [ "d" , "1101" ] ,
296- [ "e" , "1110" ] ,
297- [ "f" , "1111" ] ,
283+ [ 0 , "00000000" ] ,
284+ [ 1 , "00000001" ] ,
285+ [ 2 , "00000010" ] ,
286+ [ 3 , "00000011" ] ,
287+ [ 4 , "00000100" ] ,
298288 ] ) ( "returns the right binary number for %s => %s" , ( hex , expected ) => {
299- const binary = instance [ "hexToBinary " ] ( hex ) ;
289+ const binary = instance [ "numberToBinaryString " ] ( hex ) ;
300290 expect ( binary ) . toBe ( expected ) ;
301291 } ) ;
302292 } ) ;
293+
294+ describe ( "test 64 bits deterministicSha256HashToBigIntString for span id" , ( ) => {
295+ const instance = StepFunctionContextService . instance ( ) ;
296+ it ( "first test of #1" , ( ) => {
297+ const actual = instance [ "deterministicSha256HashToBigIntString" ] (
298+ "arn:aws:states:sa-east-1:425362996713:stateMachine:MyStateMachine-b276uka1j#lambda#1" ,
299+ PARENT_ID ,
300+ ) ;
301+ expect ( actual ) . toEqual ( "3711631873188331089" ) ;
302+ } ) ;
303+
304+ it ( "test same hashing number is generated as logs-backend for execution id # state name # entered time" , ( ) => {
305+ const actual = instance [ "deterministicSha256HashToBigIntString" ] (
306+ "arn:aws:states:sa-east-1:425362996713:stateMachine:MyStateMachine-b276uka1j#lambda#2" ,
307+ PARENT_ID ,
308+ ) ;
309+ expect ( actual ) . toEqual ( "5759173372325510050" ) ;
310+ } ) ;
311+ } ) ;
303312} ) ;
0 commit comments