From b39213063d01ebaf97492fad55215bb22c87a3ee Mon Sep 17 00:00:00 2001 From: Ben Brandt Date: Fri, 27 Oct 2023 17:32:28 +0200 Subject: [PATCH] Add testing around helper functions --- log-viewer/src/lib/log.test.ts | 56 ++++++++++++++++++++++++++++++++++ log-viewer/src/lib/log.ts | 6 ++-- 2 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 log-viewer/src/lib/log.test.ts diff --git a/log-viewer/src/lib/log.test.ts b/log-viewer/src/lib/log.test.ts new file mode 100644 index 000000000..e3282a2a0 --- /dev/null +++ b/log-viewer/src/lib/log.test.ts @@ -0,0 +1,56 @@ +import { describe, test, expect } from 'vitest'; +import { logRange, renderDuration } from './log'; +import { randomLogEntry, randomLogger, randomSpan } from './log.test_utils'; + +describe('logRange', () => { + test('Single Span', () => { + const span = randomSpan(); + const range = logRange({ name: 'test', logs: [span] }); + expect(range?.from).toEqual(new Date(span.start_timestamp)); + expect(range?.to).toEqual(new Date(span.end_timestamp)); + }); + + test('Empty Logs', () => { + const range = logRange({ name: 'test', logs: [] }); + expect(range).toBeNull(); + }); + + test('Single Entry', () => { + const entry = randomLogEntry(); + const range = logRange({ name: 'test', logs: [entry] }); + expect(range?.from).toEqual(range?.to); + expect(range?.from).toEqual(new Date(entry.timestamp)); + }); + + test('Larger Log', () => { + const log = randomLogger(); + const range = logRange(log); + if (range) { + expect(range.from <= range.to).toBe(true); + } + }); +}); + +describe('renderDuration', () => { + test('ms', () => { + expect(renderDuration(500)).toBe('500ms'); + }); + test('s', () => { + expect(renderDuration(1000)).toBe('1s'); + }); + test('partial s', () => { + expect(renderDuration(1001)).toBe('1.001s'); + }); + test('min', () => { + expect(renderDuration(60000)).toBe('1min'); + }); + test('partial min', () => { + expect(renderDuration(60050)).toBe('1.001min'); + }); + test('hr', () => { + expect(renderDuration(3600000)).toBe('1h'); + }); + test('partial hr', () => { + expect(renderDuration(3605000)).toBe('1.001h'); + }); +}); diff --git a/log-viewer/src/lib/log.ts b/log-viewer/src/lib/log.ts index b97d4c861..63e89510d 100644 --- a/log-viewer/src/lib/log.ts +++ b/log-viewer/src/lib/log.ts @@ -66,13 +66,13 @@ export function logRange(log: DebugLog): TimeRange | null { export function renderDuration(spanLength: number): string { let unit = 'ms'; let length = spanLength; - if (length > 1000) { + if (length >= 1000) { length /= 1000; unit = 's'; - if (length > 60) { + if (length >= 60) { length /= 60; unit = 'min'; - if (length > 60) { + if (length >= 60) { length /= 60; unit = 'h'; }