import { describe, expect, it } from "vitest"; import Time from "./Time"; describe("Time", () => { describe("toShortTime", () => { it("test toShortTime", () => { const date = new Date("2027-01-03T04:05:06.789Z"); expect(Time.toShortTime(date.getTime())).toBe("05:06"); }); }); describe("between", () => { it("should return time segments between begin and end", () => { const begin = new Date("2026-01-01T00:00:00.000Z"); const end = new Date("2027-01-03T04:05:06.789Z"); const result = Time.between(begin, end); expect(result.y).toBe(1); expect(result.d).toBe(2); expect(result.h).toBe(4); expect(result.m).toBe(5); expect(result.s).toBe(6); expect(result.ms).toBeGreaterThanOrEqual(788); expect(result.ms).toBeLessThanOrEqual(789); }); }); describe("duration", () => { it("should return empty string when totalMs is falsy", () => { expect(Time.duration()).toBe(""); expect(Time.duration(0)).toBe(""); }); it("should format duration without milliseconds by default", () => { const value = Time.D * 366 + Time.H * 2 + Time.M * 3 + Time.S * 4 + 567; expect(Time.duration(value)).toBe("1 年 1 日 2 小时 3 分钟 4 秒"); }); it("should include milliseconds when ms flag is true", () => { const value = Time.M + 250; expect(Time.duration(value, true)).toBe("1 分钟 250 毫秒"); }); }); describe("toMediaTime", () => { it("should format seconds to media time", () => { expect(Time.toMediaTime(59.9)).toBe("00:59"); expect(Time.toMediaTime(61)).toBe("01:01"); expect(Time.toMediaTime(3661)).toBe("1:01:01"); }); }); describe("parseToMS", () => { it("should parse value without unit as milliseconds", () => { expect(Time.parseToMS("10")).toBe(10); expect(Time.parseToMS("10ms")).toBe(10); }); it("should parse supported units", () => { expect(Time.parseToMS("10s")).toBe(10 * Time.S); expect(Time.parseToMS("10m")).toBe(10 * Time.M); expect(Time.parseToMS("10h")).toBe(10 * Time.H); expect(Time.parseToMS("10d")).toBe(10 * Time.D); }); it("should parse decimal with spaces and uppercase unit", () => { expect(Time.parseToMS(" 10.5 D ")).toBe(Math.round(10.5 * Time.D)); }); it("should throw for empty or invalid format", () => { expect(() => Time.parseToMS("")).toThrowError("not found timeStr"); expect(() => Time.parseToMS("abc")).toThrowError("invalid format: abc"); }); }); });