Skip to content

Commit

Permalink
Add testing around helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
benbrandt committed Oct 27, 2023
1 parent 341a314 commit b392130
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 3 deletions.
56 changes: 56 additions & 0 deletions log-viewer/src/lib/log.test.ts
Original file line number Diff line number Diff line change
@@ -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');
});
});
6 changes: 3 additions & 3 deletions log-viewer/src/lib/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
Expand Down

0 comments on commit b392130

Please sign in to comment.