Unify duration parsing behind a core/duration tokenizer
The two --expire flags each carried a byte-identical copy of the same regex and unit-conversion switch, and a third parser (parseDurationMs) lived in capabilities/helpers.ts with yet another regex. Because the copies were independent, --expire 300 could be read differently on each path without anyone noticing. core/duration.ts now owns the grammar (tokenizeDuration) and the unitless heuristic (expireSeconds: a bare number <= 168 reads as hours, larger as seconds). Each flag keeps only what genuinely differs: Seal delegation links floor to whole hours and cap at 168h, Hose links round to seconds, simulation windows are milliseconds and fall back instead of failing the batch. Adds a cross-flag regression test asserting both --expire parsers read the same literal the same way.
This commit is contained in:
1 parent
df44791779
commit
6e3d4634f1
6 files changed
+270
-125
No files matched your search
@@ -0,0 +1,75 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import {
|
||||
durationSeconds,
|
||||
expireSeconds,
|
||||
parseDurationMs,
|
||||
tokenizeDuration
|
||||
} from "./duration.js";
|
||||
|
||||
describe("tokenizeDuration", () => {
|
||||
test("splits magnitude from unit and accepts every alias", () => {
|
||||
expect(tokenizeDuration("7d")).toEqual({ value: 7, unit: "d" });
|
||||
expect(tokenizeDuration("24h")).toEqual({ value: 24, unit: "h" });
|
||||
expect(tokenizeDuration("90m")).toEqual({ value: 90, unit: "m" });
|
||||
expect(tokenizeDuration("7200s")).toEqual({ value: 7200, unit: "s" });
|
||||
expect(tokenizeDuration("1.5h")).toEqual({ value: 1.5, unit: "h" });
|
||||
expect(tokenizeDuration("2 hours")).toEqual({ value: 2, unit: "h" });
|
||||
expect(tokenizeDuration("500ms")).toEqual({ value: 500, unit: "ms" });
|
||||
expect(tokenizeDuration(" 300 ")).toEqual({ value: 300, unit: undefined });
|
||||
expect(tokenizeDuration(168)).toEqual({ value: 168, unit: undefined });
|
||||
});
|
||||
|
||||
test("rejects unparseable input instead of guessing", () => {
|
||||
expect(tokenizeDuration("invalid")).toBeUndefined();
|
||||
expect(tokenizeDuration("")).toBeUndefined();
|
||||
expect(tokenizeDuration("-5m")).toBeUndefined();
|
||||
expect(tokenizeDuration("5parsecs")).toBeUndefined();
|
||||
expect(tokenizeDuration(Number.NaN)).toBeUndefined();
|
||||
expect(tokenizeDuration(true as unknown as string)).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe("expireSeconds", () => {
|
||||
test("reads a unitless number at most 168 as hours and a larger one as seconds", () => {
|
||||
// Both --expire paths used to reimplement this rule separately, and they
|
||||
// disagreed at the boundary; 300 now means 5 minutes everywhere.
|
||||
expect(expireSeconds({ value: 168, unit: undefined })).toBe(168 * 3600);
|
||||
expect(expireSeconds({ value: 169, unit: undefined })).toBe(169);
|
||||
expect(expireSeconds({ value: 300, unit: undefined })).toBe(300);
|
||||
expect(expireSeconds({ value: 86400, unit: undefined })).toBe(86_400);
|
||||
});
|
||||
|
||||
test("converts explicit units regardless of magnitude", () => {
|
||||
expect(expireSeconds({ value: 7, unit: "d" })).toBe(604_800);
|
||||
expect(expireSeconds({ value: 7200, unit: "s" })).toBe(7200);
|
||||
expect(expireSeconds({ value: 30, unit: "m" })).toBe(1800);
|
||||
});
|
||||
});
|
||||
|
||||
describe("durationSeconds", () => {
|
||||
test("uses the caller's unitless policy when none is written", () => {
|
||||
expect(durationSeconds({ value: 5, unit: undefined }, "s")).toBe(5);
|
||||
expect(durationSeconds({ value: 5, unit: undefined }, "h")).toBe(18_000);
|
||||
expect(durationSeconds({ value: 5, unit: "ms" }, "h")).toBe(0.005);
|
||||
});
|
||||
});
|
||||
|
||||
describe("parseDurationMs", () => {
|
||||
test("converts simulation windows to milliseconds", () => {
|
||||
expect(parseDurationMs("2s")).toBe(2000);
|
||||
expect(parseDurationMs("10m")).toBe(600_000);
|
||||
expect(parseDurationMs("5m")).toBe(300_000);
|
||||
expect(parseDurationMs("500ms")).toBe(500);
|
||||
expect(parseDurationMs("1h")).toBe(3_600_000);
|
||||
expect(parseDurationMs("1d")).toBe(86_400_000);
|
||||
});
|
||||
|
||||
test("reads unitless input as seconds, matching the flag's defaults", () => {
|
||||
expect(parseDurationMs("30")).toBe(30_000);
|
||||
});
|
||||
|
||||
test("falls back rather than failing a batch over a bad poll interval", () => {
|
||||
expect(parseDurationMs("soon")).toBe(600_000);
|
||||
expect(parseDurationMs("soon", 1000)).toBe(1000);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user