fix: project got corrupted sometimes

This commit is contained in:
2026-02-22 10:27:28 +01:00
parent 03657e7a84
commit f43e150668
8 changed files with 176 additions and 17 deletions

View File

@@ -48,6 +48,27 @@ vi.mock('fs/promises', () => ({
throw err;
}
}),
rename: vi.fn(async (oldPath: string, newPath: string) => {
const normalizedOldPath = oldPath.replace(/\\/g, '/');
const normalizedNewPath = newPath.replace(/\\/g, '/');
const content = mockFiles.get(normalizedOldPath);
if (content === undefined) {
const err = new Error(`ENOENT: no such file or directory, rename '${oldPath}' -> '${newPath}'`) as NodeJS.ErrnoException;
err.code = 'ENOENT';
throw err;
}
mockFiles.set(normalizedNewPath, content);
mockFiles.delete(normalizedOldPath);
}),
unlink: vi.fn(async (filePath: string) => {
const normalizedPath = filePath.replace(/\\/g, '/');
if (!mockFiles.has(normalizedPath)) {
const err = new Error(`ENOENT: no such file or directory, unlink '${filePath}'`) as NodeJS.ErrnoException;
err.code = 'ENOENT';
throw err;
}
mockFiles.delete(normalizedPath);
}),
}));
// Mock electron app
@@ -986,6 +1007,27 @@ describe('MetaEngine', () => {
expect(metaEngine.isInitialized()).toBe(false);
});
it('should keep initialized flag when project context is unchanged', async () => {
await metaEngine.syncOnStartup();
expect(metaEngine.isInitialized()).toBe(true);
metaEngine.setProjectContext('test-project');
expect(metaEngine.isInitialized()).toBe(true);
});
it('should de-duplicate concurrent syncOnStartup calls', async () => {
const collectTagsSpy = vi.spyOn(metaEngine as unknown as {
collectTagsFromPosts: () => Promise<string[]>;
}, 'collectTagsFromPosts');
await Promise.all([
metaEngine.syncOnStartup(),
metaEngine.syncOnStartup(),
]);
expect(collectTagsSpy).toHaveBeenCalledTimes(1);
});
it('should use custom dataDir when provided in setProjectContext', () => {
const customDataDir = path.join('custom', 'data', 'path');
metaEngine.setProjectContext('project-with-custom-dir', customDataDir);