From b0083d08cc4536bcbce826395e56e14061403b2d Mon Sep 17 00:00:00 2001 From: Matt Isenhower Date: Sun, 15 Feb 2026 15:40:52 -0800 Subject: [PATCH] Add unit tests for getDurationParts Co-Authored-By: Claude Opus 4.6 --- src/common/time.js | 2 +- src/common/time.test.js | 63 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 src/common/time.test.js diff --git a/src/common/time.js b/src/common/time.js index b79219b..d1fb83c 100644 --- a/src/common/time.js +++ b/src/common/time.js @@ -1,7 +1,7 @@ import { useI18n } from 'vue-i18n'; import { useTimeStore } from '@/stores/time'; -function getDurationParts(value) { +export function getDurationParts(value) { let negative = (value < 0) ? '-' : ''; value = Math.abs(value); diff --git a/src/common/time.test.js b/src/common/time.test.js new file mode 100644 index 0000000..db9e99c --- /dev/null +++ b/src/common/time.test.js @@ -0,0 +1,63 @@ +import { describe, it, expect } from 'vitest'; +import { getDurationParts } from './time.js'; + +describe('getDurationParts', () => { + it('returns all zeros for zero', () => { + expect(getDurationParts(0)).toEqual({ + negative: '', + days: 0, + hours: 0, + minutes: 0, + seconds: 0, + }); + }); + + it('computes 1d 1h 1m 1s for 90061', () => { + expect(getDurationParts(90061)).toEqual({ + negative: '', + days: 1, + hours: 1, + minutes: 1, + seconds: 1, + }); + }); + + it('sets negative flag for negative values', () => { + const result = getDurationParts(-90061); + expect(result.negative).toBe('-'); + expect(result.days).toBe(1); + expect(result.hours).toBe(1); + expect(result.minutes).toBe(1); + expect(result.seconds).toBe(1); + }); + + it('computes exactly 1 day for 86400', () => { + expect(getDurationParts(86400)).toEqual({ + negative: '', + days: 1, + hours: 0, + minutes: 0, + seconds: 0, + }); + }); + + it('computes only seconds for 59', () => { + expect(getDurationParts(59)).toEqual({ + negative: '', + days: 0, + hours: 0, + minutes: 0, + seconds: 59, + }); + }); + + it('computes hours and minutes without days', () => { + expect(getDurationParts(3661)).toEqual({ + negative: '', + days: 0, + hours: 1, + minutes: 1, + seconds: 1, + }); + }); +});