From 19525d07b185217088f0ad163fcd4d263f259507 Mon Sep 17 00:00:00 2001 From: Carter Mak <49076171+cartermak@users.noreply.github.com> Date: Wed, 4 Dec 2024 13:43:13 -0800 Subject: [PATCH] Fix missing radix when serializing relative time milliseconds (#1567) * Fix missing radix when serializing relative time milliseconds --- .../sequence-editor/to-seq-json.test.ts | 94 ++++++++++++++++++- src/utilities/sequence-editor/to-seq-json.ts | 4 +- src/utilities/time.test.ts | 69 ++++++++++++++ 3 files changed, 164 insertions(+), 3 deletions(-) diff --git a/src/utilities/sequence-editor/to-seq-json.test.ts b/src/utilities/sequence-editor/to-seq-json.test.ts index dd86bcec23..6fa987bb36 100644 --- a/src/utilities/sequence-editor/to-seq-json.test.ts +++ b/src/utilities/sequence-editor/to-seq-json.test.ts @@ -365,7 +365,7 @@ C ECHO L01STR it('local and parameter block', async () => { const id = 'test.sequence'; const seq = `@ID "test.inline" -@LOCALS_BEGIN +@LOCALS_BEGIN L00STR L01 INT L02ENUM ENUM TEMPERATURE "" "hot, cold" @@ -1073,6 +1073,98 @@ G03:00:00 "GroundEpochName" @REQUEST_BEGIN("request2.name") expect(actual).toEqual(expected); }); + it('should handle all time tag types', async () => { + const seq = `A2029-365T23:20:50 BAKE_BREAD +A2029-365T23:21:51.123 BAKE_BREAD +R00:00:30 BAKE_BREAD +R10 BAKE_BREAD +R00:00:30.500 BAKE_BREAD +E00:06:40.333 BAKE_BREAD +E00:00:10 BAKE_BREAD +E-00:06:40.333 BAKE_BREAD`; + const id = 'test'; + const expectedJson = { + id: 'test', + metadata: {}, + steps: [ + { + args: [], + stem: 'BAKE_BREAD', + time: { + tag: '2029-365T23:20:50', + type: 'ABSOLUTE', + }, + type: 'command', + }, + { + args: [], + stem: 'BAKE_BREAD', + time: { + tag: '2029-365T23:21:51.123', + type: 'ABSOLUTE', + }, + type: 'command', + }, + { + args: [], + stem: 'BAKE_BREAD', + time: { + tag: '00:00:30', + type: 'COMMAND_RELATIVE', + }, + type: 'command', + }, + { + args: [], + stem: 'BAKE_BREAD', + time: { + tag: '00:00:10', + type: 'COMMAND_RELATIVE', + }, + type: 'command', + }, + { + args: [], + stem: 'BAKE_BREAD', + time: { + tag: '00:00:30.500', + type: 'COMMAND_RELATIVE', + }, + type: 'command', + }, + { + args: [], + stem: 'BAKE_BREAD', + time: { + tag: '00:06:40.333', + type: 'EPOCH_RELATIVE', + }, + type: 'command', + }, + { + args: [], + stem: 'BAKE_BREAD', + time: { + tag: '00:00:10', + type: 'EPOCH_RELATIVE', + }, + type: 'command', + }, + { + args: [], + stem: 'BAKE_BREAD', + time: { + tag: '-00:06:40.333', + type: 'EPOCH_RELATIVE', + }, + type: 'command', + }, + ], + }; + const actual = JSON.parse(await sequenceToSeqJson(SeqLanguage.parser.parse(seq), seq, commandDictionary, id)); + expect(actual).toEqual(expectedJson); + }); + describe('round trip', () => { it('should round trip commands', async () => { const input = ` diff --git a/src/utilities/sequence-editor/to-seq-json.ts b/src/utilities/sequence-editor/to-seq-json.ts index a885e9d98a..0da3ef102a 100644 --- a/src/utilities/sequence-editor/to-seq-json.ts +++ b/src/utilities/sequence-editor/to-seq-json.ts @@ -404,7 +404,7 @@ function parseTime(commandNode: SyntaxNode, text: string): Time { const { isNegative, days, hours, minutes, seconds, milliseconds } = getDurationTimeComponents( parseDurationString(timeTagEpochText, 'seconds'), ); - tag = `${isNegative}${days}${days ? 'T' : ''}${hours}:${minutes}:${seconds}${milliseconds}`; + tag = `${isNegative}${days}${days ? 'T' : ''}${hours}:${minutes}:${seconds}${milliseconds ? '.' : ''}${milliseconds}`; return { tag, type: 'EPOCH_RELATIVE' }; } @@ -424,7 +424,7 @@ function parseTime(commandNode: SyntaxNode, text: string): Time { const { isNegative, days, hours, minutes, seconds, milliseconds } = getDurationTimeComponents( parseDurationString(timeTagRelativeText, 'seconds'), ); - tag = `${isNegative}${days}${days ? 'T' : ''}${hours}:${minutes}:${seconds}${milliseconds}`; + tag = `${isNegative}${days}${days ? 'T' : ''}${hours}:${minutes}:${seconds}${milliseconds ? '.' : ''}${milliseconds}`; return { tag, type: 'COMMAND_RELATIVE' }; } diff --git a/src/utilities/time.test.ts b/src/utilities/time.test.ts index d4423b0d1a..86394ebff2 100644 --- a/src/utilities/time.test.ts +++ b/src/utilities/time.test.ts @@ -13,6 +13,7 @@ import { getDoy, getDoyTime, getDoyTimeComponents, + getDurationTimeComponents, getShortISOForDate, getTimeAgo, getUnixEpochTime, @@ -468,6 +469,74 @@ test('parseDurationString', () => { }); }); +test('getDurationTimeComponents', () => { + expect( + getDurationTimeComponents({ + days: 3, + hours: 10, + isNegative: false, + microseconds: 0, + milliseconds: 0, + minutes: 30, + seconds: 45, + years: 2, + }), + ).toEqual({ + days: '003', + hours: '10', + isNegative: '', + microseconds: '', + milliseconds: '', + minutes: '30', + seconds: '45', + years: '0002', + }); + + expect( + getDurationTimeComponents({ + days: 300, + hours: 2, + isNegative: true, + microseconds: 1, + milliseconds: 123, + minutes: 1, + seconds: 2, + years: 0, + }), + ).toEqual({ + days: '300', + hours: '02', + isNegative: '-', + microseconds: '001', + milliseconds: '123', + minutes: '01', + seconds: '02', + years: '0000', + }); + + expect( + getDurationTimeComponents({ + days: 0, + hours: 0, + isNegative: false, + microseconds: 123, + milliseconds: 0, + minutes: 0, + seconds: 2, + years: 0, + }), + ).toEqual({ + days: '', + hours: '00', + isNegative: '', + microseconds: '123', + milliseconds: '', + minutes: '00', + seconds: '02', + years: '0000', + }); +}); + test('isTimeBalanced', () => { expect(isTimeBalanced('2024-001T00:00:00', TimeTypes.ABSOLUTE)).toBe(true); expect(isTimeBalanced('2024-001T12:90:00', TimeTypes.ABSOLUTE)).toBe(false);