import { describe, expect, it } from "vitest"; import { formatClock, parseClock } from "./time"; describe("formatClock", () => { it("drops the hour until it is the news", () => { expect(formatClock(0)).toBe("0:00"); expect(formatClock(9)).toBe("0:09"); expect(formatClock(65)).toBe("1:05"); expect(formatClock(599)).toBe("9:59"); }); it("pads minutes only once the hour exists", () => { // 1:02:05, not 1:2:5 — but 62 seconds is 1:02, not 01:02 expect(formatClock(3725)).toBe("1:02:05"); expect(formatClock(3600)).toBe("1:00:00"); expect(formatClock(86399)).toBe("23:59:59"); }); it("has an answer for the values that arrive before the data does", () => { // a song with no duration in the index, and a signal that has not ticked, // both show 0:00 rather than NaN:NaN or an empty cell expect(formatClock(undefined)).toBe("0:00"); expect(formatClock(Number.NaN)).toBe("0:00"); expect(formatClock(-5)).toBe("0:00"); expect(formatClock(205.9)).toBe("3:25"); // the clock is fractional; the readout is not }); }); describe("parseClock", () => { it("reads back what the formatter printed", () => { for (const s of [0, 9, 65, 599, 205, 3725, 3600]) { expect(parseClock(formatClock(s))).toBe(s); } }); it("takes the shapes a person types that the formatter never prints", () => { expect(parseClock("90")).toBe(90); // a bare count of seconds expect(parseClock("1:30")).toBe(90); expect(parseClock(" 2:00 ")).toBe(120); // the command line trims before it parses // a move, signed, which is what `-20` means to `seek` expect(parseClock("+20")).toBe(20); expect(parseClock("-20")).toBe(-20); expect(parseClock("-1:00")).toBe(-60); }); it("refuses the things that are not times, rather than guessing a place", () => { // A misread here is a seek to somewhere the reader did not ask for, so a missing // half of a time and the letter that looks like one are both refusals. for (const junk of ["", " ", "abc", "1:2o", ":30", "30s", "1:", "1:2:3:4", "--5"]) { expect(parseClock(junk)).toBeNull(); } }); it("does not invent a sign out of a digit", () => { // the difference between a place and a move is the sign, and `seek` cannot tell them // apart once both arrive as a number expect(parseClock("20")).toBe(20); }); });