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);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,118 @@
|
||||
/**
|
||||
* Duration tokenizing shared by every flag that accepts `7d` / `1.5h` / `300`.
|
||||
*
|
||||
* The tokenizer owns exactly one job: turning the written form into a magnitude
|
||||
* plus a canonical unit. Interpretation stays with each caller, because the
|
||||
* three consumers genuinely disagree — Seal delegation links take whole hours
|
||||
* in 1..168, Hose assistance links take seconds, simulation windows take
|
||||
* milliseconds. Sharing the grammar (and the unitless heuristic) is what stops
|
||||
* `--expire 300` from parsing two different ways in two files.
|
||||
*/
|
||||
|
||||
export type DurationUnit = "ms" | "s" | "m" | "h" | "d";
|
||||
|
||||
export interface DurationToken {
|
||||
/** The magnitude exactly as written, before any unit conversion. */
|
||||
value: number;
|
||||
/** `undefined` when the input carried no unit, e.g. `300`. */
|
||||
unit: DurationUnit | undefined;
|
||||
}
|
||||
|
||||
const UNIT_ALIASES: Record<string, DurationUnit> = {
|
||||
ms: "ms",
|
||||
s: "s",
|
||||
sec: "s",
|
||||
secs: "s",
|
||||
second: "s",
|
||||
seconds: "s",
|
||||
m: "m",
|
||||
min: "m",
|
||||
mins: "m",
|
||||
minute: "m",
|
||||
minutes: "m",
|
||||
h: "h",
|
||||
hr: "h",
|
||||
hrs: "h",
|
||||
hour: "h",
|
||||
hours: "h",
|
||||
d: "d",
|
||||
day: "d",
|
||||
days: "d"
|
||||
};
|
||||
|
||||
const DURATION_PATTERN = /^(\d+(?:\.\d+)?)\s*([a-z]+)?$/i;
|
||||
|
||||
/**
|
||||
* Parse a duration literal. Returns `undefined` when the input cannot be read
|
||||
* at all — callers decide whether that is a usage error or a default.
|
||||
*
|
||||
* A unitless number keeps whatever magnitude it had; pair it with
|
||||
* {@link expireSeconds} or an explicit `unitless` argument to resolve it.
|
||||
*/
|
||||
export function tokenizeDuration(value: string | number): DurationToken | undefined {
|
||||
if (typeof value === "number") {
|
||||
return Number.isFinite(value) ? { value, unit: undefined } : undefined;
|
||||
}
|
||||
if (typeof value !== "string") {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const match = value.trim().match(DURATION_PATTERN);
|
||||
if (!match || !match[1]) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const rawUnit = match[2]?.toLowerCase();
|
||||
if (!rawUnit) {
|
||||
return { value: parseFloat(match[1]), unit: undefined };
|
||||
}
|
||||
|
||||
const unit = UNIT_ALIASES[rawUnit];
|
||||
if (!unit) {
|
||||
return undefined;
|
||||
}
|
||||
return { value: parseFloat(match[1]), unit };
|
||||
}
|
||||
|
||||
/** Convert a token to seconds, resolving a unitless value as `unitless`. */
|
||||
export function durationSeconds(token: DurationToken, unitless: DurationUnit): number {
|
||||
switch (token.unit ?? unitless) {
|
||||
case "ms":
|
||||
return token.value / 1000;
|
||||
case "m":
|
||||
return token.value * 60;
|
||||
case "h":
|
||||
return token.value * 3600;
|
||||
case "d":
|
||||
return token.value * 86400;
|
||||
case "s":
|
||||
default:
|
||||
return token.value;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Unitless `--expire` heuristic shared by the Seal and Hose link flags:
|
||||
* a bare number at most 168 reads as hours (7 days of them), a larger one
|
||||
* reads as seconds. Without this, `--expire 300` would mean 300 hours on one
|
||||
* path and 5 minutes on another.
|
||||
*/
|
||||
export function expireSeconds(token: DurationToken): number {
|
||||
return durationSeconds(token, token.value <= 168 ? "h" : "s");
|
||||
}
|
||||
|
||||
/** Default used by {@link parseDurationMs} when the literal is unreadable. */
|
||||
export const DEFAULT_DURATION_MS = 600_000;
|
||||
|
||||
/**
|
||||
* Simulation windows and poll intervals: milliseconds, unitless input read as
|
||||
* seconds, and an unreadable literal falls back to the default instead of
|
||||
* failing the call — a poll interval is not worth aborting a batch over.
|
||||
*/
|
||||
export function parseDurationMs(duration: string, fallbackMs = DEFAULT_DURATION_MS): number {
|
||||
const token = tokenizeDuration(duration);
|
||||
if (!token || token.value < 0) {
|
||||
return fallbackMs;
|
||||
}
|
||||
return Math.round(durationSeconds(token, "s") * 1000);
|
||||
}
|
||||
Reference in new issue
Block a user