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:
gitea committed 2026-10-11 19:33:25 +08:00
1 parent df44791779
commit 6e3d4634f1
6 files changed
+270 -125

No files matched your search

+20
View File
@@ -157,6 +157,26 @@ describe("option helpers", () => {
expect(() => parseHoseExpireSeconds("0s")).toThrow("必须为大于 0 的有效时长");
expect(() => parseHoseExpireSeconds("invalid")).toThrow("必须为有效时长");
});
test("both --expire flags read a unitless literal the same way", () => {
// Regression: the two parsers used to each carry their own copy of the
// unitless heuristic, so `--expire 300` meant 5 minutes on one path and
// clamped to 1 hour on the other purely by implementation accident.
for (const literal of [300, "300", 86400, "86400", 7200, "7200"]) {
const hours = parseDelegationExpireHours(literal);
const seconds = parseHoseExpireSeconds(literal);
const unitlessSeconds = typeof literal === "number" ? literal : Number(literal);
expect(seconds).toBe(unitlessSeconds);
// Delegation floors to whole hours and never drops below 1h.
expect(hours).toBe(Math.max(Math.floor(seconds / 3600), 1));
}
// A small unitless value is hours on both paths.
expect(parseDelegationExpireHours("24")).toBe(24);
expect(parseHoseExpireSeconds("24")).toBe(86_400);
expect(parseDelegationExpireHours(2)).toBe(2);
expect(parseHoseExpireSeconds(2)).toBe(7200);
});
});
describe("flag registry cross-check", () => {