Skip to content
Open
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
68 changes: 68 additions & 0 deletions dev/test/cli/cli_create_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,16 @@ describe('createAgent', () => {

beforeEach(() => {
vi.clearAllMocks();
// createAgent() reads these before shelling out to gcloud, so a developer's
// exported values would shadow the execSync mock.
vi.stubEnv('GOOGLE_CLOUD_PROJECT', undefined);
vi.stubEnv('GOOGLE_CLOUD_LOCATION', undefined);
(isCancel as unknown as Mock).mockReturnValue(false);
(listFiles as Mock).mockResolvedValue(['file1', 'file2']);
});

afterEach(() => {
vi.unstubAllEnvs();
vi.restoreAllMocks();
});

Expand Down Expand Up @@ -213,15 +218,50 @@ describe('createAgent', () => {

expect(text).toHaveBeenCalledWith(
expect.objectContaining({
message: 'Enter the Google Cloud Project ID',
initialValue: 'gcloud-project',
}),
);
expect(text).toHaveBeenCalledWith(
expect.objectContaining({
message: 'Enter the Google Cloud Region',
initialValue: 'gcloud-region',
}),
);
expect(saveToFile).toHaveBeenCalledWith(
expect.stringContaining('.env'),
expect.stringContaining('GOOGLE_CLOUD_PROJECT=gcloud-project'),
);
});

it('should prefer the GCP environment variables over the gcloud defaults', async () => {
vi.stubEnv('GOOGLE_CLOUD_PROJECT', 'env-project');
vi.stubEnv('GOOGLE_CLOUD_LOCATION', 'env-region');

(select as Mock).mockResolvedValueOnce('gemini-2.5-flash');
(select as Mock).mockResolvedValueOnce('ts');
(select as Mock).mockResolvedValueOnce('vertex'); // Backend

(text as Mock).mockResolvedValueOnce('env-project');
(text as Mock).mockResolvedValueOnce('env-region');

await createAgent(getFreshOptions());

expect(text).toHaveBeenCalledWith(
expect.objectContaining({
message: 'Enter the Google Cloud Project ID',
initialValue: 'env-project',
}),
);
expect(text).toHaveBeenCalledWith(
expect.objectContaining({
message: 'Enter the Google Cloud Region',
initialValue: 'env-region',
}),
);
expect(execSync).not.toHaveBeenCalled();
});

it('should exit without writing files if project prompt is cancelled', async () => {
// Mirror clack's contract: only the raw cancel symbol counts as a cancel.
(isCancel as unknown as Mock).mockImplementation(
Expand All @@ -238,6 +278,34 @@ describe('createAgent', () => {

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

it('should fall back to an empty initial value when gcloud is unavailable', async () => {
(select as Mock).mockResolvedValueOnce('gemini-2.5-flash');
(select as Mock).mockResolvedValueOnce('ts');
(select as Mock).mockResolvedValueOnce('vertex'); // Backend

(execSync as Mock).mockImplementation(() => {
throw new Error('gcloud not found');
});

(text as Mock).mockResolvedValueOnce('');
(text as Mock).mockResolvedValueOnce('');

await createAgent(getFreshOptions());

expect(text).toHaveBeenCalledWith(
expect.objectContaining({
message: 'Enter the Google Cloud Project ID',
initialValue: '',
}),
);
expect(text).toHaveBeenCalledWith(
expect.objectContaining({
message: 'Enter the Google Cloud Region',
initialValue: '',
}),
);
});
});

describe('Folder Handling', () => {
Expand Down
Loading