/ clis / chaoxing / utils.test.js
utils.test.js
 1  import { describe, expect, it } from 'vitest';
 2  import { formatTimestamp, workStatusLabel } from './utils.js';
 3  function localDatePrefixFromMillis(ts) {
 4      const d = new Date(ts);
 5      const yyyy = d.getFullYear();
 6      const mm = String(d.getMonth() + 1).padStart(2, '0');
 7      const dd = String(d.getDate()).padStart(2, '0');
 8      return `${yyyy}-${mm}-${dd}`;
 9  }
10  describe('formatTimestamp', () => {
11      it('formats millisecond timestamp', () => {
12          const ts = new Date('2026-01-15T00:30:00Z').getTime();
13          const result = formatTimestamp(ts);
14          expect(result).toMatch(new RegExp(`^${localDatePrefixFromMillis(ts)}\\s`));
15      });
16      it('formats second timestamp', () => {
17          const millis = new Date('2026-06-01T12:00:00Z').getTime();
18          const ts = Math.floor(millis / 1000);
19          const result = formatTimestamp(ts);
20          expect(result).toMatch(new RegExp(`^${localDatePrefixFromMillis(millis)}\\s`));
21      });
22      it('returns empty for null/undefined/0', () => {
23          expect(formatTimestamp(null)).toBe('');
24          expect(formatTimestamp(undefined)).toBe('');
25          expect(formatTimestamp(0)).toBe('');
26          expect(formatTimestamp('')).toBe('');
27      });
28      it('passes through readable date strings', () => {
29          expect(formatTimestamp('2026-03-20 23:59')).toBe('2026-03-20 23:59');
30      });
31  });
32  describe('workStatusLabel', () => {
33      it('maps numeric status codes', () => {
34          expect(workStatusLabel(0)).toBe('未交');
35          expect(workStatusLabel(1)).toBe('已交');
36          expect(workStatusLabel(2)).toBe('已批阅');
37      });
38      it('passes through string status', () => {
39          expect(workStatusLabel('已交')).toBe('已交');
40      });
41      it('returns 未知 for empty/null', () => {
42          expect(workStatusLabel(null)).toBe('未知');
43          expect(workStatusLabel('')).toBe('未知');
44      });
45  });