Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
177 changes: 175 additions & 2 deletions src/commands/auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ describe('runConfigure', () => {
const signal = init?.signal;
const timeout = setTimeout(() => {
reject(new Error('requestTimeoutMs was not applied to the validation ping'));
}, 50);
}, 1800);
signal?.addEventListener(
'abort',
() => {
Expand All @@ -211,7 +211,7 @@ describe('runConfigure', () => {
output: 'text',
debug: false,
fromEnv: true,
requestTimeoutMs: 1,
requestTimeoutMs: 1000,
},
{
...deps,
Expand All @@ -226,6 +226,179 @@ describe('runConfigure', () => {
expect(readProfile('default', { path: credentialsPath })).toBeUndefined();
});

it('applies TESTSPRITE_REQUEST_TIMEOUT_MS to the pre-write validation ping when no flag is set', async () => {
const { deps } = makeCapture();
let sawAbort = false;
const fetchImpl = vi.fn(
async (_input: string | URL | Request, init?: RequestInit) =>
new Promise<Response>((_resolve, reject) => {
const signal = init?.signal;
const timeout = setTimeout(() => {
reject(new Error('TESTSPRITE_REQUEST_TIMEOUT_MS was not applied to the validation ping'));
}, 2500);
signal?.addEventListener(
'abort',
() => {
sawAbort = true;
clearTimeout(timeout);
reject(new DOMException('The operation timed out.', 'TimeoutError'));
},
{ once: true },
);
}),
) as unknown as typeof fetch;

await expect(
runConfigure(
{
profile: 'default',
output: 'text',
debug: false,
fromEnv: true,
},
{
...deps,
env: {
TESTSPRITE_API_KEY: 'sk-user-min',
TESTSPRITE_REQUEST_TIMEOUT_MS: '1000',
},
credentialsPath,
fetchImpl,
},
),
).rejects.toBeInstanceOf(CLIError);

expect(sawAbort).toBe(true);
expect(readProfile('default', { path: credentialsPath })).toBeUndefined();
});

it('uses the default timeout for the pre-write validation ping when nothing is configured', async () => {
vi.useFakeTimers();
try {
const { deps } = makeCapture();
let sawAbort = false;
const fetchImpl = vi.fn(
async (_input: string | URL | Request, init?: RequestInit) =>
new Promise<Response>((_resolve, reject) => {
const signal = init?.signal;
signal?.addEventListener(
'abort',
() => {
sawAbort = true;
reject(signal.reason);
},
{ once: true },
);
}),
) as unknown as typeof fetch;

const pending = runConfigure(
{ profile: 'default', output: 'text', debug: false, fromEnv: true },
{
...deps,
env: { TESTSPRITE_API_KEY: 'sk-user-min' },
credentialsPath,
fetchImpl,
},
);
const assertion = expect(pending).rejects.toBeInstanceOf(CLIError);
await vi.advanceTimersByTimeAsync(120_000 + 1);
await assertion;
expect(sawAbort).toBe(true);
expect(readProfile('default', { path: credentialsPath })).toBeUndefined();
} finally {
vi.useRealTimers();
}
});

it('clamps a sub-minimum TESTSPRITE_REQUEST_TIMEOUT_MS up to 1s for the validation ping', async () => {
const { deps } = makeCapture();
let sawAbort = false;
const fetchImpl = vi.fn(
async (_input: string | URL | Request, init?: RequestInit) =>
new Promise<Response>((_resolve, reject) => {
const signal = init?.signal;
const timeout = setTimeout(() => {
reject(new Error('clamped TESTSPRITE_REQUEST_TIMEOUT_MS was not applied'));
}, 2500);
signal?.addEventListener(
'abort',
() => {
sawAbort = true;
clearTimeout(timeout);
reject(new DOMException('The operation timed out.', 'TimeoutError'));
},
{ once: true },
);
}),
) as unknown as typeof fetch;

await expect(
runConfigure(
{ profile: 'default', output: 'text', debug: false, fromEnv: true },
{
...deps,
env: {
TESTSPRITE_API_KEY: 'sk-user-min',
TESTSPRITE_REQUEST_TIMEOUT_MS: '50',
},
credentialsPath,
fetchImpl,
},
),
).rejects.toBeInstanceOf(CLIError);

expect(sawAbort).toBe(true);
});

it('the request-timeout flag wins over TESTSPRITE_REQUEST_TIMEOUT_MS for the validation ping', async () => {
const { deps } = makeCapture();
let sawAbort = false;
const fetchImpl = vi.fn(
async (_input: string | URL | Request, init?: RequestInit) =>
new Promise<Response>((_resolve, reject) => {
const signal = init?.signal;
// If the env var (600000ms) won over the flag (1000ms), the abort
// would land far past this watchdog and the test fails fast.
const timeout = setTimeout(() => {
reject(new Error('flag did not win over TESTSPRITE_REQUEST_TIMEOUT_MS'));
}, 2500);
signal?.addEventListener(
'abort',
() => {
sawAbort = true;
clearTimeout(timeout);
reject(new DOMException('The operation timed out.', 'TimeoutError'));
},
{ once: true },
);
}),
) as unknown as typeof fetch;

await expect(
runConfigure(
{
profile: 'default',
output: 'text',
debug: false,
fromEnv: true,
requestTimeoutMs: 1000,
},
{
...deps,
env: {
TESTSPRITE_API_KEY: 'sk-user-min',
TESTSPRITE_REQUEST_TIMEOUT_MS: '600000',
},
credentialsPath,
fetchImpl,
},
),
).rejects.toBeInstanceOf(CLIError);

expect(sawAbort).toBe(true);
});

it('throws VALIDATION_ERROR when --from-env is set but key is missing', async () => {
const { deps } = makeCapture();
await expect(
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Expand Down
6 changes: 5 additions & 1 deletion src/commands/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
emitDryRunBanner,
makeHttpClient,
parseRequestTimeoutFlag,
resolveRequestTimeoutMs,
type CommonOptions as FactoryCommonOptions,
} from '../lib/client-factory.js';
import { ApiError, CLIError, InterruptError } from '../lib/errors.js';
Expand Down Expand Up @@ -283,7 +284,10 @@ export async function runConfigure(
baseUrl: facadeBaseUrl(apiUrl),
apiKey,
fetchImpl: deps.fetchImpl,
requestTimeoutMs: opts.requestTimeoutMs,
// Same precedence as every other command's client (flag > env > default,
// same clamping): `setup --from-env` previously read the flag only, so a
// TESTSPRITE_REQUEST_TIMEOUT_MS deadline was silently ignored here.
requestTimeoutMs: resolveRequestTimeoutMs(opts, env),
shutdownSignal: deps.shutdownSignal ?? globalShutdown.signal,
shutdown: deps.shutdown ?? globalShutdown,
});
Expand Down
Loading