Increment the generated project id instead of falling back to untitled

This commit is contained in:
Alex Gleason committed 2025-12-01 23:47:06 -06:00
1 parent 89244d88e5
commit 56d9bca5c7
2 files changed
+76 -24

No files matched your search

+50 -11
View File
@@ -39,6 +39,8 @@ describe('useGenerateProjectInfo', () => {
}
];
let mockGetProject: ReturnType<typeof vi.fn>;
beforeEach(() => {
vi.clearAllMocks();
@@ -81,8 +83,9 @@ describe('useGenerateProjectInfo', () => {
});
// Mock useProjectsManager
mockGetProject = vi.fn().mockResolvedValue(null);
const mockProjectsManager = {
getProject: vi.fn().mockResolvedValue(null),
getProject: mockGetProject,
} as unknown as ProjectsManager;
mockUseProjectsManager.mockReturnValue(mockProjectsManager);
@@ -333,7 +336,7 @@ describe('useGenerateProjectInfo', () => {
expect(projectInfo.projectId).toBe('untitled');
});
it('should fallback to untitled when project already exists', async () => {
it('should increment AI-generated ID when project already exists', async () => {
mockUseAISettings.mockReturnValue({
settings: {
providers: [
@@ -352,23 +355,59 @@ describe('useGenerateProjectInfo', () => {
removeMCPServer: vi.fn(),
});
// Mock getProject - first call returns project (exists), second returns null (untitled is available)
const mockGetProject = vi.fn()
// Reset and override the mock to return project on first call, null on second
mockGetProject.mockReset();
mockGetProject
.mockResolvedValueOnce({ id: 'test-project-name', name: 'Test Project', path: '/projects/test-project-name', lastModified: new Date() })
.mockResolvedValueOnce(null);
const mockProjectsManager = {
getProject: mockGetProject,
} as unknown as ProjectsManager;
mockUseProjectsManager.mockReturnValue(mockProjectsManager);
.mockResolvedValue(null);
const { result } = renderHook(() => useGenerateProjectInfo());
const projectInfo = await result.current.generateProjectInfo('openai/gpt-4', 'test prompt');
expect(projectInfo.projectId).toBe('untitled');
expect(projectInfo.projectId).toBe('test-project-name-1');
expect(projectInfo.template.url).toBe('https://gitlab.com/soapbox-pub/mkstack.git');
expect(mockGetProject).toHaveBeenCalledTimes(2);
expect(mockGetProject).toHaveBeenNthCalledWith(1, 'test-project-name');
expect(mockGetProject).toHaveBeenNthCalledWith(2, 'test-project-name-1');
});
it('should increment multiple times when necessary', async () => {
mockUseAISettings.mockReturnValue({
settings: {
providers: [
{ id: 'openai', name: 'OpenAI', apiKey: 'test-key', baseURL: 'https://api.openai.com/v1' }
],
recentlyUsedModels: [],
mcpServers: {}
},
isConfigured: true,
updateSettings: vi.fn(),
setProvider: vi.fn(),
removeProvider: vi.fn(),
setProviders: vi.fn(),
addRecentlyUsedModel: vi.fn(),
setMCPServer: vi.fn(),
removeMCPServer: vi.fn(),
});
// Reset and override the mock - base and -1 exist, -2 is available
mockGetProject.mockReset();
mockGetProject
.mockResolvedValueOnce({ id: 'test-project-name', name: 'Test Project', path: '/projects/test-project-name', lastModified: new Date() })
.mockResolvedValueOnce({ id: 'test-project-name-1', name: 'Test Project 1', path: '/projects/test-project-name-1', lastModified: new Date() })
.mockResolvedValue(null);
const { result } = renderHook(() => useGenerateProjectInfo());
const projectInfo = await result.current.generateProjectInfo('openai/gpt-4', 'test prompt');
expect(projectInfo.projectId).toBe('test-project-name-2');
expect(projectInfo.template.url).toBe('https://gitlab.com/soapbox-pub/mkstack.git');
expect(mockGetProject).toHaveBeenCalledTimes(3);
expect(mockGetProject).toHaveBeenNthCalledWith(1, 'test-project-name');
expect(mockGetProject).toHaveBeenNthCalledWith(2, 'test-project-name-1');
expect(mockGetProject).toHaveBeenNthCalledWith(3, 'test-project-name-2');
});
it('should handle loading state correctly', async () => {
+26 -13
View File
@@ -27,19 +27,30 @@ export function useGenerateProjectInfo({ onError }: UseGenerateProjectInfoOption
const { config } = useAppContext();
const projectsManager = useProjectsManager();
// Helper function to generate untitled project name
const fallbackWithUntitled = useCallback(async (template: { url: string; name: string; description: string }): Promise<ProjectInfo> => {
let fallbackId = 'untitled';
let counter = 1;
// Helper function to generate incremented project name
// Note: This assumes the baseId already exists (caller has verified this)
const generateIncrementedId = useCallback(async (baseId: string, template: { url: string; name: string; description: string }, baseExists: boolean = true): Promise<ProjectInfo> => {
// If base doesn't exist, just use it
if (!baseExists) {
const existingProject = await projectsManager.getProject(baseId);
if (existingProject === null) {
return {
projectId: baseId,
template,
};
}
}
// Check if base "untitled" exists
// Start incrementing from -1
let counter = 1;
let fallbackId = `${baseId}-${counter}`;
let existingProject = await projectsManager.getProject(fallbackId);
// Keep incrementing until we find an available name
while (existingProject !== null) {
fallbackId = `untitled-${counter}`;
existingProject = await projectsManager.getProject(fallbackId);
counter++;
fallbackId = `${baseId}-${counter}`;
existingProject = await projectsManager.getProject(fallbackId);
}
return {
@@ -130,7 +141,7 @@ You MUST call the create_project tool with your choices.`;
if (!selectedTemplate) {
// Fallback to first template if invalid URL selected
console.warn(`Invalid template URL selected: ${templateUrl}, falling back to first template`);
return await fallbackWithUntitled(config.templates[0]);
return await generateIncrementedId('untitled', config.templates[0], false);
}
// Validate the generated project ID
@@ -145,11 +156,13 @@ You MUST call the create_project tool with your choices.`;
template: selectedTemplate,
};
}
// Project already exists - increment the AI-generated ID (baseExists = true)
return await generateIncrementedId(projectId, selectedTemplate, true);
}
// If we reach here, either the regex failed or the project already exists
// Fall back to "untitled" with number suffixes
return await fallbackWithUntitled(selectedTemplate);
// If we reach here, the regex failed - fall back to "untitled" with number suffixes
return await generateIncrementedId('untitled', selectedTemplate, false);
} catch (error) {
// On any error, fall back to first template with untitled name
console.error('Error generating project info:', error);
@@ -160,11 +173,11 @@ You MUST call the create_project tool with your choices.`;
throw new Error(errorMessage);
}
return await fallbackWithUntitled(config.templates[0]);
return await generateIncrementedId('untitled', config.templates[0], false);
} finally {
setIsLoading(false);
}
}, [isConfigured, settings, onError, user, projectsManager, config.corsProxy, config.templates, fallbackWithUntitled]);
}, [isConfigured, settings, onError, user, projectsManager, config.corsProxy, config.templates, generateIncrementedId]);
return {
generateProjectInfo,