2023-05-31 06:09:18 +03:00
|
|
|
import path from 'node:path';
|
2023-06-07 09:52:19 +03:00
|
|
|
import { setTimeout } from 'node:timers/promises';
|
2023-05-31 06:09:18 +03:00
|
|
|
|
2023-09-17 23:26:06 +03:00
|
|
|
import { removeWithRetry } from '@affine-test/kit/utils/utils';
|
2023-05-31 06:09:18 +03:00
|
|
|
import { v4 } from 'uuid';
|
2024-01-05 10:23:22 +03:00
|
|
|
import { afterAll, afterEach, beforeEach, expect, test, vi } from 'vitest';
|
2023-05-31 06:09:18 +03:00
|
|
|
|
|
|
|
const tmpDir = path.join(__dirname, 'tmp');
|
2023-06-13 05:01:43 +03:00
|
|
|
const appDataPath = path.join(tmpDir, 'app-data');
|
2023-05-31 06:09:18 +03:00
|
|
|
|
2023-10-19 06:14:30 +03:00
|
|
|
vi.doMock('@affine/electron/helper/main-rpc', () => ({
|
2023-06-13 05:01:43 +03:00
|
|
|
mainRPC: {
|
|
|
|
getPath: async () => appDataPath,
|
2023-05-31 06:09:18 +03:00
|
|
|
},
|
2023-06-13 05:01:43 +03:00
|
|
|
}));
|
2023-05-31 06:09:18 +03:00
|
|
|
|
|
|
|
const constructorStub = vi.fn();
|
|
|
|
const destroyStub = vi.fn();
|
2023-06-07 09:52:19 +03:00
|
|
|
destroyStub.mockReturnValue(Promise.resolve());
|
2023-05-31 06:09:18 +03:00
|
|
|
|
2023-06-13 05:01:43 +03:00
|
|
|
function existProcess() {
|
|
|
|
process.emit('beforeExit', 0);
|
|
|
|
}
|
|
|
|
|
2023-10-19 06:14:30 +03:00
|
|
|
vi.doMock('@affine/electron/helper/db/secondary-db', () => {
|
2023-05-31 06:09:18 +03:00
|
|
|
return {
|
|
|
|
SecondaryWorkspaceSQLiteDB: class {
|
|
|
|
constructor(...args: any[]) {
|
|
|
|
constructorStub(...args);
|
|
|
|
}
|
|
|
|
|
2023-06-07 09:52:19 +03:00
|
|
|
connectIfNeeded = () => Promise.resolve();
|
|
|
|
|
|
|
|
pull = () => Promise.resolve();
|
|
|
|
|
2023-05-31 06:09:18 +03:00
|
|
|
destroy = destroyStub;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
vi.useFakeTimers({ shouldAdvanceTime: true });
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(async () => {
|
2023-06-13 05:01:43 +03:00
|
|
|
existProcess();
|
2023-07-03 13:46:47 +03:00
|
|
|
await removeWithRetry(tmpDir);
|
2023-05-31 06:09:18 +03:00
|
|
|
vi.useRealTimers();
|
|
|
|
});
|
|
|
|
|
2024-01-05 10:23:22 +03:00
|
|
|
afterAll(() => {
|
|
|
|
vi.doUnmock('@affine/electron/helper/main-rpc');
|
|
|
|
});
|
|
|
|
|
2023-05-31 06:09:18 +03:00
|
|
|
test('can get a valid WorkspaceSQLiteDB', async () => {
|
2023-10-19 06:14:30 +03:00
|
|
|
const { ensureSQLiteDB } = await import(
|
|
|
|
'@affine/electron/helper/db/ensure-db'
|
|
|
|
);
|
2023-05-31 06:09:18 +03:00
|
|
|
const workspaceId = v4();
|
|
|
|
const db0 = await ensureSQLiteDB(workspaceId);
|
|
|
|
expect(db0).toBeDefined();
|
|
|
|
expect(db0.workspaceId).toBe(workspaceId);
|
|
|
|
|
|
|
|
const db1 = await ensureSQLiteDB(v4());
|
|
|
|
expect(db1).not.toBe(db0);
|
|
|
|
expect(db1.workspaceId).not.toBe(db0.workspaceId);
|
|
|
|
|
|
|
|
// ensure that the db is cached
|
|
|
|
expect(await ensureSQLiteDB(workspaceId)).toBe(db0);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('db should be destroyed when app quits', async () => {
|
2023-10-19 06:14:30 +03:00
|
|
|
const { ensureSQLiteDB } = await import(
|
|
|
|
'@affine/electron/helper/db/ensure-db'
|
|
|
|
);
|
2023-05-31 06:09:18 +03:00
|
|
|
const workspaceId = v4();
|
|
|
|
const db0 = await ensureSQLiteDB(workspaceId);
|
|
|
|
const db1 = await ensureSQLiteDB(v4());
|
|
|
|
|
2024-05-16 09:30:53 +03:00
|
|
|
expect(db0.adapter).not.toBeNull();
|
|
|
|
expect(db1.adapter).not.toBeNull();
|
2023-05-31 06:09:18 +03:00
|
|
|
|
2023-06-13 05:01:43 +03:00
|
|
|
existProcess();
|
2023-06-07 09:52:19 +03:00
|
|
|
|
|
|
|
// wait the async `db.destroy()` to be called
|
|
|
|
await setTimeout(100);
|
2023-05-31 06:09:18 +03:00
|
|
|
|
2024-05-16 09:30:53 +03:00
|
|
|
expect(db0.adapter.db).toBeNull();
|
|
|
|
expect(db1.adapter.db).toBeNull();
|
2023-05-31 06:09:18 +03:00
|
|
|
});
|
|
|
|
|
2023-06-07 09:52:19 +03:00
|
|
|
test('db should be removed in db$Map after destroyed', async () => {
|
2023-10-19 06:14:30 +03:00
|
|
|
const { ensureSQLiteDB, db$Map } = await import(
|
|
|
|
'@affine/electron/helper/db/ensure-db'
|
|
|
|
);
|
2023-06-07 09:52:19 +03:00
|
|
|
const workspaceId = v4();
|
|
|
|
const db = await ensureSQLiteDB(workspaceId);
|
|
|
|
await db.destroy();
|
|
|
|
await setTimeout(100);
|
|
|
|
expect(db$Map.has(workspaceId)).toBe(false);
|
|
|
|
});
|