Files
AFFiNE-Mirror/packages/frontend/apps/electron/test/db/workspace-db-adapter.spec.ts
pengx17 f689c2f1fc feat(electron): move @blocksuite/affine to peer dependences for package speed on windows (#9756)
`@blocksuite/affine` is indirectly depended by electron package. It has around 120k files in total and will greatly slow down forge package build speed.
Move them to peer dependencies to mitigate the issue.
2025-01-18 10:16:22 +00:00

60 lines
1.5 KiB
TypeScript

import path from 'node:path';
import fs from 'fs-extra';
import { v4 } from 'uuid';
import { afterAll, afterEach, beforeAll, expect, test, vi } from 'vitest';
const tmpDir = path.join(__dirname, 'tmp');
const appDataPath = path.join(tmpDir, 'app-data');
beforeAll(() => {
vi.doMock('@affine/electron/helper/main-rpc', () => ({
mainRPC: {
getPath: async () => appDataPath,
},
}));
});
afterEach(async () => {
try {
await fs.remove(tmpDir);
} catch (e) {
console.error(e);
}
});
afterAll(() => {
vi.doUnmock('@affine/electron/helper/main-rpc');
});
test('can create new db file if not exists', async () => {
const { openWorkspaceDatabase } = await import(
'@affine/electron/helper/nbstore/v1/workspace-db-adapter'
);
const workspaceId = v4();
const db = await openWorkspaceDatabase('workspace', workspaceId);
const dbPath = path.join(
appDataPath,
`workspaces/${workspaceId}`,
`storage.db`
);
expect(await fs.exists(dbPath)).toBe(true);
await db.destroy();
});
test('on destroy, check if resources have been released', async () => {
const { openWorkspaceDatabase } = await import(
'@affine/electron/helper/nbstore/v1/workspace-db-adapter'
);
const workspaceId = v4();
const db = await openWorkspaceDatabase('workspace', workspaceId);
const updateSub = {
complete: vi.fn(),
next: vi.fn(),
};
db.update$ = updateSub as any;
await db.destroy();
expect(db.adapter.db).toBe(null);
expect(updateSub.complete).toHaveBeenCalled();
});