fix(cli): validate --request-timeout flag and de-duplicate its parser#17
Open
Davidson3556 wants to merge 1 commit into
Open
fix(cli): validate --request-timeout flag and de-duplicate its parser#17Davidson3556 wants to merge 1 commit into
Davidson3556 wants to merge 1 commit into
Conversation
`parseRequestTimeoutFlag` was copy-pasted byte-for-byte into five command files (auth, project, usage, init, test). Every copy silently returned `undefined` for an invalid value, so an explicit `--request-timeout 30s` (a natural "30 seconds" typo) resolved to undefined and the command ran with the default 120s deadline — the operator believed they had set a timeout but had not, with no signal. Hoist a single definition into client-factory.ts (next to resolveRequestTimeoutMs and the REQUEST_TIMEOUT_* constants) and make the flag strict: a non-numeric, zero, or negative value now throws a typed VALIDATION_ERROR (exit 5), consistent with every other validated flag (--page-size, --output, --type). Positive out-of-range values are still accepted and clamped by resolveRequestTimeoutMs, and the TESTSPRITE_REQUEST_TIMEOUT_MS env-var path stays lenient by design (a stray global env var should not hard-fail every command). Adds unit coverage for parseRequestTimeoutFlag and a subprocess regression that `--request-timeout 30s` exits 5 instead of falling back to 120s.
Author
|
@zeshi-du please kindly review. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #16
Describe the changes you have made in this PR -
Two related problems with the global
--request-timeout <seconds>flag:Invalid values were silently dropped.
parseRequestTimeoutFlagreturnedundefinedfor any non-numeric / zero / negative input, andundefinedis indistinguishable from "flag omitted" — so the command fell back toTESTSPRITE_REQUEST_TIMEOUT_MSor the 120s default. A natural typo like--request-timeout 30s(meaning "30 seconds") ran with the default 120s deadline while the operator believed they had set 30s, with no error. Every other validated flag (--page-size,--output,--type) exits 5 on bad input; this one was the exception.The parser was duplicated five times. The identical
parseRequestTimeoutFlagbody lived inauth.ts,project.ts,usage.ts,init.ts, andtest.ts. Drift between copies would silently change timeout behaviour depending on the command.Changes:
parseRequestTimeoutFlagintosrc/lib/client-factory.ts, co-located withresolveRequestTimeoutMsand theREQUEST_TIMEOUT_*constants; the five command files import it.VALIDATION_ERROR(exit 5).resolveRequestTimeoutMs, which already clamps to[1s, 600s]; theTESTSPRITE_REQUEST_TIMEOUT_MSenv-var path stays lenient (its existing tests assert non-numeric/zero/negative env vars fall back to the default) — a stray global env var should not hard-fail every command.parseRequestTimeoutFlagand a subprocess regression that--request-timeout 30sexits 5 instead of silently using 120s.Demo/Screenshot for feature changes and bug fixes -
Before (main) — the invalid flag is ignored and execution proceeds past validation (here reaching the auth step), so it would have silently run with the default 120s:
After (this branch) — the flag is validated up front; valid values still proceed:
Tests:
Code Understanding and AI Usage
Did you use AI assistance (ChatGPT, Claude, Copilot, etc.) to write any part of this code?
If you used AI assistance:
Explain your implementation approach:
Problem: an explicit
--request-timeoutvalue that didn't parse was silently discarded, so the operator's chosen deadline was ignored without any signal — and the parser was copy-pasted into five files.Alternatives considered:
resolveRequestTimeoutMstests — a globally-setTESTSPRITE_REQUEST_TIMEOUT_MSshould not hard-fail every single command. Only the explicit per-invocation flag should fail loudly.Key function:
parseRequestTimeoutFlag(raw)—undefined→undefined(flag omitted, factory falls back to env/default); a positive number → seconds×1000 ms (including out-of-range values, whichresolveRequestTimeoutMsclamps to[1s, 600s]); non-numeric / zero / negative → throws the typedVALIDATION_ERROR(exit 5). Placed inclient-factory.tsnext to the related timeout logic so the flag, env, and default resolution all live together.Edge cases handled/tested: omitted flag; positive seconds (incl. fractional like
2.5); positive out-of-range (700parses to 700000, clamped later, not rejected);abc,30s,0,-5,NaN,Infinity, and empty string each throw exit 5; subprocess test confirms the CLI exit code is 5, not a silent fall-back.Checklist before requesting a review
Note: Please check Allow edits from maintainers if you would like us to assist in the PR.