mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-06 00:27:24 +00:00
refactors
This commit is contained in:
1 parent
6f4aa264eb
commit
f4ccc2d924
6 files changed
+349
-45
No files matched your search
@@ -1,6 +1,27 @@
|
||||
import { describe, it, expect, beforeEach } from 'vitest';
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
import { ProjectsManager } from './ProjectsManager';
|
||||
|
||||
// Mock JSZip
|
||||
vi.mock('jszip', () => ({
|
||||
default: class MockJSZip {
|
||||
files: Record<string, { dir: boolean; async: () => Uint8Array }> = {};
|
||||
|
||||
static async loadAsync(buffer: ArrayBuffer) {
|
||||
const instance = new MockJSZip();
|
||||
// Add a mock file entry
|
||||
instance.files['package.json'] = {
|
||||
dir: false,
|
||||
async: () => new Uint8Array(buffer)
|
||||
};
|
||||
return instance;
|
||||
}
|
||||
|
||||
async file(name: string) {
|
||||
return this.files[name];
|
||||
}
|
||||
}
|
||||
}));
|
||||
|
||||
// Mock filesystem for testing
|
||||
class MockFS {
|
||||
private files: Map<string, string | Uint8Array> = new Map();
|
||||
@@ -167,4 +188,77 @@ describe('ProjectsManager', () => {
|
||||
expect(project).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('importProjectFromZip', () => {
|
||||
it('should preserve original project name when overwriting', async () => {
|
||||
await projectsManager.init();
|
||||
|
||||
// Create an existing project with a specific name
|
||||
await fs.mkdir('/projects/my-project');
|
||||
await fs.mkdir('/projects/my-project/.git');
|
||||
await fs.writeFile('/projects/my-project/package.json', '{"name": "original-name"}');
|
||||
|
||||
// Mock the git methods to simulate existing project
|
||||
const mockGit = {
|
||||
init: vi.fn(),
|
||||
clone: vi.fn(),
|
||||
add: vi.fn(),
|
||||
commit: vi.fn(),
|
||||
getConfig: vi.fn(),
|
||||
getAllFiles: vi.fn().mockResolvedValue(['package.json']),
|
||||
} as Partial<import('./git').Git>;
|
||||
|
||||
// Replace git instance
|
||||
Object.assign(projectsManager, { git: mockGit });
|
||||
|
||||
// Create a mock ZIP file with different name
|
||||
const zipFile = new File(['test content'], 'different-name.zip', { type: 'application/zip' });
|
||||
|
||||
// Mock arrayBuffer method
|
||||
Object.defineProperty(zipFile, 'arrayBuffer', {
|
||||
value: () => Promise.resolve(new TextEncoder().encode('test content').buffer),
|
||||
writable: false
|
||||
});
|
||||
|
||||
// Import with overwrite
|
||||
const project = await projectsManager.importProjectFromZip(zipFile, 'my-project', true);
|
||||
|
||||
// Should preserve original project name
|
||||
expect(project.name).toBe('my-project');
|
||||
expect(project.id).toBe('my-project');
|
||||
});
|
||||
|
||||
it('should use new project name when not overwriting', async () => {
|
||||
await projectsManager.init();
|
||||
|
||||
// Mock the git methods
|
||||
const mockGit = {
|
||||
init: vi.fn(),
|
||||
clone: vi.fn(),
|
||||
add: vi.fn(),
|
||||
commit: vi.fn(),
|
||||
getConfig: vi.fn(),
|
||||
getAllFiles: vi.fn().mockResolvedValue(['package.json']),
|
||||
} as Partial<import('./git').Git>;
|
||||
|
||||
// Replace git instance
|
||||
Object.assign(projectsManager, { git: mockGit });
|
||||
|
||||
// Create a mock ZIP file
|
||||
const zipFile = new File(['test content'], 'new-project.zip', { type: 'application/zip' });
|
||||
|
||||
// Mock arrayBuffer method
|
||||
Object.defineProperty(zipFile, 'arrayBuffer', {
|
||||
value: () => Promise.resolve(new TextEncoder().encode('test content').buffer),
|
||||
writable: false
|
||||
});
|
||||
|
||||
// Import without overwrite
|
||||
const project = await projectsManager.importProjectFromZip(zipFile, undefined, false);
|
||||
|
||||
// Should use formatted name from ID
|
||||
expect(project.name).toBe('new-project');
|
||||
expect(project.id).toBe('new-project');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -95,7 +95,20 @@ export class ProjectsManager {
|
||||
async importProjectFromZip(zipFile: File, customId?: string, overwrite = false): Promise<Project> {
|
||||
// Generate a project ID based on the zip file name or use custom ID
|
||||
const baseName = zipFile.name.replace(/\.zip$/i, '');
|
||||
const id = customId || await this.generateUniqueProjectId(baseName);
|
||||
const generatedId = baseName
|
||||
.toLowerCase()
|
||||
.replace(/[^a-z0-9]+/g, '-')
|
||||
.replace(/^-+|-+$/g, '');
|
||||
|
||||
// Determine the project ID to use
|
||||
let id: string;
|
||||
if (customId) {
|
||||
// Use the provided custom ID (for overwrite scenarios)
|
||||
id = customId;
|
||||
} else {
|
||||
// If no custom ID, generate a unique ID (for new projects)
|
||||
id = await this.generateUniqueProjectId(generatedId);
|
||||
}
|
||||
|
||||
const projectPath = `${this.dir}/${id}`;
|
||||
const projectExists = await this.projectExists(id);
|
||||
@@ -105,6 +118,15 @@ export class ProjectsManager {
|
||||
throw new Error(`Project with ID "${id}" already exists`);
|
||||
}
|
||||
|
||||
// Get original project name if overwriting, otherwise use formatted name
|
||||
let projectName: string;
|
||||
if (overwrite && projectExists) {
|
||||
const existingProject = await this.getProject(id);
|
||||
projectName = existingProject?.name || this.formatProjectName(id);
|
||||
} else {
|
||||
projectName = this.formatProjectName(id);
|
||||
}
|
||||
|
||||
try {
|
||||
// Create project directory (or ensure it exists for overwrite)
|
||||
await this.fs.mkdir(projectPath, { recursive: true });
|
||||
@@ -230,7 +252,7 @@ export class ProjectsManager {
|
||||
|
||||
return {
|
||||
id,
|
||||
name: this.formatProjectName(id),
|
||||
name: projectName,
|
||||
path: projectPath,
|
||||
lastModified: timestamp,
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user