Skip to content
Merged
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
17 changes: 15 additions & 2 deletions packages/cli/src/cmd/project/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import * as tui from '../../tui.ts';
import { projectDelete, projectList, projectGet } from '@agentuity/server';
import enquirer from 'enquirer';
import { getCommand } from '../../command-prefix.ts';
import { isDryRunMode, outputDryRun } from '../../explain.ts';
import { isJSONMode } from '../../output.ts';

interface ProjectDisplayInfo {
id: string;
Expand Down Expand Up @@ -35,7 +37,7 @@ export const deleteSubcommand = createSubcommand({
},
{
command: getCommand('--dry-run project delete proj_abc123def456'),
description: 'Delete item',
description: 'Preview project deletion without making changes',
},
],
schema: {
Expand All @@ -53,7 +55,7 @@ export const deleteSubcommand = createSubcommand({
},

async handler(ctx) {
const { args, opts, apiClient } = ctx;
const { args, opts, apiClient, options } = ctx;

let projectsToDelete: ProjectDisplayInfo[] = [];

Expand Down Expand Up @@ -136,6 +138,17 @@ export const deleteSubcommand = createSubcommand({
return { success: false, projectIds: [], count: 0 };
}

if (isDryRunMode(options)) {
const projectLabel = projectsToDelete.length === 1 ? 'project' : 'projects';
const projectDisplay = projectsToDelete.map(formatProjectDisplay).join(', ');
outputDryRun(`Would delete ${projectLabel}: ${projectDisplay}`, options);
if (!isJSONMode(options)) {
tui.newline();
tui.info('[DRY RUN] Project deletion skipped');
}
return { success: false, projectIds: [], count: 0 };
}

const skipConfirm = opts?.confirm === true;

if (!process.stdout.isTTY && !skipConfirm) {
Expand Down
126 changes: 126 additions & 0 deletions packages/cli/test/cmd/project/delete.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import { afterEach, beforeAll, beforeEach, describe, expect, test } from 'bun:test';
import { existsSync } from 'node:fs';
import { mkdtemp, rm } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join, resolve } from 'node:path';

const CLI_ROOT = resolve(import.meta.dir, '..', '..', '..');
const SRC_ENTRY = join(CLI_ROOT, 'src', 'main.ts');
const SERVER_ENTRY = join(CLI_ROOT, '..', 'server', 'dist', 'index.js');
const PROJECT_ID = 'proj_dry_run_test';

interface RecordedRequest {
readonly method: string;
readonly pathname: string;
}

interface CliRunResult {
readonly exitCode: number;
readonly stdout: string;
readonly stderr: string;
}

let configDir: string;
let server: ReturnType<typeof Bun.serve> | undefined;
let requests: RecordedRequest[];

beforeAll(async () => {
if (existsSync(SERVER_ENTRY)) return;

const build = Bun.spawn(['bun', 'run', 'build'], {
cwd: CLI_ROOT,
stdout: 'inherit',
stderr: 'inherit',
});
const exitCode = await build.exited;
if (exitCode !== 0) {
throw new Error(`bun run build failed with exit code ${exitCode}`);
}
});

beforeEach(async () => {
configDir = await mkdtemp(join(tmpdir(), 'agentuity-project-delete-test-'));
requests = [];
server = Bun.serve({
port: 0,
fetch(request) {
const url = new URL(request.url);
requests.push({ method: request.method, pathname: url.pathname });

if (request.method === 'GET' && url.pathname === `/cli/project/${PROJECT_ID}`) {
return Response.json({
success: true,
data: {
id: PROJECT_ID,
name: 'Dry Run Test',
orgId: 'org_test',
},
});
}

if (request.method === 'DELETE' && url.pathname === '/cli/project') {
return Response.json({ success: true, data: [PROJECT_ID] });
}

return Response.json({ success: false, message: 'Not found' }, { status: 404 });
},
});
});

afterEach(async () => {
server?.stop(true);
server = undefined;
await rm(configDir, { recursive: true, force: true });
});

async function runCLI(args: readonly string[]): Promise<CliRunResult> {
if (!server) {
throw new Error('Test server is not running');
}

const env: Record<string, string> = {
AGENTUITY_AGENT_MODE: 'none',
AGENTUITY_API_KEY: 'ag_test',
AGENTUITY_API_URL: `http://127.0.0.1:${server.port}`,
AGENTUITY_CONFIG_DIR: configDir,
AGENTUITY_SKIP_VERSION_CHECK: '1',
};
if (process.env.HOME) env.HOME = process.env.HOME;
if (process.env.PATH) env.PATH = process.env.PATH;
if (process.env.TMPDIR) env.TMPDIR = process.env.TMPDIR;

const proc = Bun.spawn(['bun', SRC_ENTRY, ...args], {
cwd: configDir,
env,
stdout: 'pipe',
stderr: 'pipe',
});
const [exitCode, stdout, stderr] = await Promise.all([
proc.exited,
new Response(proc.stdout).text(),
new Response(proc.stderr).text(),
]);
return { exitCode, stdout, stderr };
}

describe('project delete command', () => {
test('dry run resolves the project without deleting it', async () => {
const result = await runCLI(['--dry-run', 'project', 'delete', PROJECT_ID, '--confirm']);

expect(result).toEqual(expect.objectContaining({ exitCode: 0 }));
expect(requests).toEqual([{ method: 'GET', pathname: `/cli/project/${PROJECT_ID}` }]);
expect(result.stdout).toContain(
`[DRY RUN] Would delete project: Dry Run Test (${PROJECT_ID})`
);
expect(result.stderr).toContain('[DRY RUN] Project deletion skipped');
});

test('dry run does not require deletion confirmation', async () => {
const result = await runCLI(['--dry-run', 'project', 'delete', PROJECT_ID]);

expect(result).toEqual(expect.objectContaining({ exitCode: 0 }));
expect(result.stdout).toContain('[DRY RUN]');
expect(result.stderr).not.toContain('no TTY and --confirm is false');
expect(requests).toEqual([{ method: 'GET', pathname: `/cli/project/${PROJECT_ID}` }]);
});
});
Loading