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
26 changes: 26 additions & 0 deletions src/commands/test.artifact.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import type { CliFailureContext, CliLatestResult, CliTestStep } from './test.js'
import {
assertOutDirParentExists,
createTestArtifactCommand,
resolveDefaultArtifactDir,
runArtifactGet,
runFailureGet,
} from './test.js';
Expand Down Expand Up @@ -319,6 +320,31 @@ describe('runArtifactGet', () => {
}
});

it('rejects path-like runId before auth or fetch when default --out is used', async () => {
const fetchImpl = vi.fn<typeof globalThis.fetch>();

await expect(
runArtifactGet(
{
profile: 'default',
output: 'json',
debug: false,
runId: '../../outside',
failedOnly: false,
},
{ fetchImpl, stdout: () => {} },
),
).rejects.toMatchObject({ code: 'VALIDATION_ERROR', exitCode: 5 });

expect(fetchImpl).not.toHaveBeenCalled();
});

it('keeps the documented default directory for path-safe runIds', () => {
expect(resolveDefaultArtifactDir(SAMPLE_RUN_ID, '/repo')).toBe(
join('/repo', '.testsprite', 'runs', SAMPLE_RUN_ID),
);
});

// ---- --failed-only passed through to writeBundle ----

it('passes --failed-only through to writeBundle (steps filtered to failed ± 1)', async () => {
Expand Down
19 changes: 17 additions & 2 deletions src/commands/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6703,6 +6703,20 @@ export interface ArtifactGetResult {
bundle?: WriteBundleResult;
}

export function resolveDefaultArtifactDir(runId: string, cwd: string = process.cwd()): string {
requireNonEmpty('run-id', runId);
if (runId === '.' || runId === '..' || runId.includes('/') || runId.includes('\\')) {
throw localValidationError(
'run-id',
'must be a single path-safe segment for the default output directory; pass --out <dir> to choose a custom path',
);
}
if (runId.includes('\0')) {
throw localValidationError('run-id', 'must not contain NUL bytes');
}
return join(cwd, '.testsprite', 'runs', runId);
}

/**
* Validate that the parent directory of `resolvedDir` exists and is a
* directory. Surfaces `VALIDATION_ERROR` (exit 5) — matches the convention
Expand Down Expand Up @@ -6752,14 +6766,13 @@ export async function runArtifactGet(
deps: TestDeps = {},
): Promise<ArtifactGetResult> {
const out = makeOutput(opts.output, deps);
const client = makeClient(opts, deps);
const { runId } = opts;

// Resolve output dir: explicit --out or the default .testsprite/runs/<runId>/
const resolvedDir =
opts.out !== undefined
? resolveBundleDir(opts.out)
: join(process.cwd(), '.testsprite', 'runs', runId);
: resolveDefaultArtifactDir(runId);

// --dry-run: no network, no disk write.
// The client (makeClient) is already wired with createDryRunFetch() when
Expand Down Expand Up @@ -6805,6 +6818,8 @@ export async function runArtifactGet(
await assertOutDirParentExists(resolvedDir);
}

const client = makeClient(opts, deps);

// Fetch the run-scoped failure bundle.
const { body: context, requestId: fetchRequestId } = await client.getWithMeta<CliFailureContext>(
`/runs/${encodeURIComponent(runId)}/failure`,
Expand Down