From dcb3d3e0b02a75ffc14d873763e6751c05bc09c7 Mon Sep 17 00:00:00 2001 From: hrz <1710360675@qq.com> Date: Wed, 2 Sep 2026 20:15:40 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=20deviceTime=20=E5=B7=A5=E5=85=B7=20v?= =?UTF-8?q?itest=20=E6=B5=8B=E8=AF=95=EF=BC=8C22=20=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main/manager-web/tests/deviceTime.test.mjs | 75 ++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 main/manager-web/tests/deviceTime.test.mjs diff --git a/main/manager-web/tests/deviceTime.test.mjs b/main/manager-web/tests/deviceTime.test.mjs new file mode 100644 index 000000000..8bec49918 --- /dev/null +++ b/main/manager-web/tests/deviceTime.test.mjs @@ -0,0 +1,75 @@ +import { describe, it, expect } from 'vitest'; +import { + hasTimestampValue, + parseTimestamp, + parseLegacyDate, + formatTimestamp, + formatCreateDate, + compareTimestamps, +} from '../src/utils/deviceTime.mjs'; + +describe('hasTimestampValue', () => { + it('returns false for null', () => expect(hasTimestampValue(null)).toBe(false)); + it('returns false for undefined', () => expect(hasTimestampValue(undefined)).toBe(false)); + it('returns false for empty string', () => expect(hasTimestampValue('')).toBe(false)); + it('returns false for whitespace-only string', () => expect(hasTimestampValue(' ')).toBe(false)); + it('returns true for number', () => expect(hasTimestampValue(1234567890)).toBe(true)); + it('returns true for non-empty string', () => expect(hasTimestampValue('2026-09-02')).toBe(true)); +}); + +describe('parseTimestamp', () => { + it('returns null for null', () => expect(parseTimestamp(null)).toBe(null)); + it('returns null for empty string', () => expect(parseTimestamp('')).toBe(null)); + it('parses numeric string', () => { + const ts = parseTimestamp('1234567890000'); + expect(typeof ts).toBe('number'); + expect(ts).toBe(1234567890000); + }); + it('returns null for non-numeric string', () => expect(parseTimestamp('not-a-date')).toBe(null)); + it('parses number directly', () => expect(parseTimestamp(1234567890000)).toBe(1234567890000)); +}); + +describe('parseLegacyDate', () => { + it('returns null for empty', () => expect(parseLegacyDate('')).toBe(null)); + it('parses ISO date string', () => { + const ts = parseLegacyDate('2026-09-02T10:00:00Z'); + expect(typeof ts).toBe('number'); + expect(ts).toBeGreaterThan(0); + }); +}); + +describe('formatTimestamp', () => { + it('returns dash for null', () => expect(formatTimestamp(null)).toBe('-')); + it('uses default formatter', () => { + const out = formatTimestamp(0); + expect(typeof out).toBe('string'); + expect(out).not.toBe('-'); + }); + it('accepts custom formatter', () => { + expect(formatTimestamp(0, () => 'CUSTOM')).toBe('CUSTOM'); + }); +}); + +describe('formatCreateDate', () => { + it('falls back to legacy date when timestamp missing', () => { + expect(formatCreateDate(null, 'fallback')).toBe('fallback'); + }); + it('falls back to dash when both missing', () => { + expect(formatCreateDate(null, null)).toBe('-'); + }); +}); + +describe('compareTimestamps', () => { + it('returns negative when first < second', () => { + expect(compareTimestamps(100, 200)).toBeLessThan(0); + }); + it('returns positive when first > second', () => { + expect(compareTimestamps(200, 100)).toBeGreaterThan(0); + }); + it('returns 0 when both equal', () => { + expect(compareTimestamps(100, 100)).toBe(0); + }); + it('treats invalid as greater than nothing', () => { + expect(compareTimestamps(100, NaN)).toBeLessThan(0); + }); +}); \ No newline at end of file