Some checks failed
Build & Test / Analyze (javascript, affine) (push) Failing after 4s
Build & Test / Analyze (typescript, affine) (push) Failing after 3s
Build & Test / Rust test filter (push) Failing after 4s
Build & Test / Check yarn binary (push) Failing after 3s
Build & Test / E2E BlockSuite Test (3) (push) Failing after 3s
Build & Test / Analyze (javascript, blocksuite) (push) Failing after 3s
Build & Test / Analyze (typescript, blocksuite) (push) Failing after 3s
Build & Test / Mobile native build filter (push) Failing after 3s
Build & Test / E2E BlockSuite Test (1) (push) Failing after 4s
Build & Test / E2E BlockSuite Test (2) (push) Failing after 4s
Build & Test / E2E Mobile Test (1) (push) Failing after 3s
Build & Test / Unit Test (1) (push) Has been skipped
Build & Test / Unit Test (2) (push) Has been skipped
Build & Test / Unit Test (3) (push) Has been skipped
Build & Test / Unit Test (4) (push) Has been skipped
Build & Test / Unit Test (5) (push) Has been skipped
Build & Test / Native Unit Test (push) Has been skipped
Build & Test / E2E BlockSuite Test (4) (push) Failing after 3s
Build & Test / E2E BlockSuite Test (5) (push) Failing after 3s
Build & Test / E2E BlockSuite Cross Browser Test (push) Failing after 3s
Build & Test / E2E Mobile Test (2) (push) Failing after 3s
Build & Test / Build AFFiNE native (x86_64-unknown-linux-gnu) (push) Failing after 3s
Build & Test / Build Server native (push) Failing after 3s
Build & Test / Check Git Status (push) Has been skipped
Build & Test / Server Test (0, 4) (push) Has been skipped
Build & Test / Server Test (1, 4) (push) Has been skipped
Build & Test / Server Test (2, 4) (push) Has been skipped
Build & Test / Server Test (3, 4) (push) Has been skipped
Build & Test / Server Test with Elasticsearch (push) Has been skipped
Build & Test / Build @affine/electron renderer (push) Failing after 3s
Build & Test / Copilot test filter (push) Failing after 4s
Build & Test / Lint Rust (push) Has been skipped
Build & Test / Server Test with Embedded Search (push) Has been skipped
Build & Test / Server Test with Manticore Search (push) Has been skipped
Build & Test / Server E2E Test (push) Has been skipped
Build & Test / Cloud Desktop E2E Test (push) Has been skipped
Build & Test / Cloud E2E Test 1/2 (push) Has been skipped
Build & Test / Cloud E2E Test 2/2 (push) Has been skipped
Build & Test / Build Android app (push) Has been skipped
Build & Test / Test Android app (push) Has been skipped
Build & Test / Run native tests (push) Has been skipped
Build & Test / Server Copilot Api Test (push) Has been skipped
Build & Test / Frontend Copilot E2E Test (1, 5) (push) Has been skipped
Build & Test / Frontend Copilot E2E Test (2, 5) (push) Has been skipped
Build & Test / Frontend Copilot E2E Test (3, 5) (push) Has been skipped
Build & Test / Frontend Copilot E2E Test (4, 5) (push) Has been skipped
Build & Test / Frontend Copilot E2E Test (5, 5) (push) Has been skipped
Release / Canary Gate (push) Has been skipped
Build & Test / Build AFFiNE native (aarch64-apple-darwin) (push) Has been cancelled
Build & Test / Lint (push) Has been cancelled
Build & Test / Typecheck (push) Has been cancelled
Build & Test / Build AFFiNE native (x86_64-apple-darwin) (push) Has been cancelled
Build & Test / Desktop Test (ubuntu-latest, linux, x64, x86_64-unknown-linux-gnu, true) (push) Has been cancelled
Build & Test / Desktop Test (windows-latest, windows, x64, x86_64-pc-windows-msvc, true) (push) Has been cancelled
Build & Test / E2E Test (1) (push) Has been cancelled
Build & Test / E2E Test (2) (push) Has been cancelled
Build & Test / E2E Test (3) (push) Has been cancelled
Build & Test / E2E Test (4) (push) Has been cancelled
Build & Test / E2E Test (5) (push) Has been cancelled
Build & Test / Build AFFiNE native (aarch64-pc-windows-msvc) (push) Has been cancelled
Build & Test / Build AFFiNE native (x86_64-pc-windows-msvc) (push) Has been cancelled
Build & Test / Desktop Test (macos-latest, macos, arm64, aarch64-apple-darwin, true) (push) Has been cancelled
Build & Test / Desktop Test (macos-latest, macos, x64, x86_64-apple-darwin, false) (push) Has been cancelled
Build & Test / 3, 2, 1 Launch (push) Has been cancelled
Build & Test / Build iOS app (push) Has been cancelled
Copilot Test Automatically / Setup Test (push) Failing after 1s
Release / Prepare (push) Failing after 2s
Release / Release Cloud (push) Has been skipped
Release / Release Desktop (push) Has been skipped
Release / Release Docker Image (push) Has been skipped
Release / Release Mobile (push) Has been skipped
178 lines
5.0 KiB
TypeScript
178 lines
5.0 KiB
TypeScript
import path from 'node:path';
|
|
|
|
import { universalId } from '@affine/nbstore';
|
|
import fs from 'fs-extra';
|
|
import { v4 } from 'uuid';
|
|
import { afterAll, afterEach, describe, expect, test, vi } from 'vitest';
|
|
|
|
const tmpDir = path.join(__dirname, 'tmp');
|
|
const appDataPath = path.join(tmpDir, 'app-data');
|
|
|
|
vi.doMock('@affine/electron/helper/db/ensure-db', () => ({
|
|
ensureSQLiteDB: async () => ({
|
|
destroy: () => {},
|
|
}),
|
|
}));
|
|
|
|
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');
|
|
});
|
|
|
|
describe('workspace db management', () => {
|
|
test('list local workspace ids', async () => {
|
|
const { listLocalWorkspaceIds } =
|
|
await import('@affine/electron/helper/workspace/handlers');
|
|
const validWorkspaceId = v4();
|
|
const noDbWorkspaceId = v4();
|
|
const deletedWorkspaceId = v4();
|
|
const fileEntry = 'README.txt';
|
|
|
|
const validWorkspacePath = path.join(
|
|
appDataPath,
|
|
'workspaces',
|
|
'local',
|
|
validWorkspaceId
|
|
);
|
|
const noDbWorkspacePath = path.join(
|
|
appDataPath,
|
|
'workspaces',
|
|
'local',
|
|
noDbWorkspaceId
|
|
);
|
|
const deletedWorkspacePath = path.join(
|
|
appDataPath,
|
|
'workspaces',
|
|
'local',
|
|
deletedWorkspaceId
|
|
);
|
|
const deletedWorkspaceTrashPath = path.join(
|
|
appDataPath,
|
|
'deleted-workspaces',
|
|
deletedWorkspaceId
|
|
);
|
|
const nonDirectoryPath = path.join(
|
|
appDataPath,
|
|
'workspaces',
|
|
'local',
|
|
fileEntry
|
|
);
|
|
|
|
await fs.ensureDir(validWorkspacePath);
|
|
await fs.ensureFile(path.join(validWorkspacePath, 'storage.db'));
|
|
await fs.ensureDir(noDbWorkspacePath);
|
|
await fs.ensureDir(deletedWorkspacePath);
|
|
await fs.ensureFile(path.join(deletedWorkspacePath, 'storage.db'));
|
|
await fs.ensureDir(deletedWorkspaceTrashPath);
|
|
await fs.outputFile(nonDirectoryPath, 'not-a-workspace');
|
|
|
|
const ids = await listLocalWorkspaceIds();
|
|
expect(ids).toContain(validWorkspaceId);
|
|
expect(ids).not.toContain(noDbWorkspaceId);
|
|
expect(ids).not.toContain(deletedWorkspaceId);
|
|
expect(ids).not.toContain(fileEntry);
|
|
});
|
|
|
|
test('trash workspace', async () => {
|
|
const { trashWorkspace } =
|
|
await import('@affine/electron/helper/workspace/handlers');
|
|
const workspaceId = v4();
|
|
const workspacePath = path.join(
|
|
appDataPath,
|
|
'workspaces',
|
|
'local',
|
|
workspaceId
|
|
);
|
|
await fs.ensureDir(workspacePath);
|
|
await trashWorkspace(
|
|
universalId({ peer: 'local', type: 'workspace', id: workspaceId })
|
|
);
|
|
expect(await fs.pathExists(workspacePath)).toBe(false);
|
|
// removed workspace will be moved to deleted-workspaces
|
|
expect(
|
|
await fs.pathExists(
|
|
path.join(appDataPath, 'deleted-workspaces', workspaceId)
|
|
)
|
|
).toBe(true);
|
|
});
|
|
|
|
test('delete workspace', async () => {
|
|
const { deleteWorkspace } =
|
|
await import('@affine/electron/helper/workspace/handlers');
|
|
const workspaceId = v4();
|
|
const workspacePath = path.join(
|
|
appDataPath,
|
|
'workspaces',
|
|
'local',
|
|
workspaceId
|
|
);
|
|
await fs.ensureDir(workspacePath);
|
|
await deleteWorkspace(
|
|
universalId({ peer: 'local', type: 'workspace', id: workspaceId })
|
|
);
|
|
expect(await fs.pathExists(workspacePath)).toBe(false);
|
|
// deleted workspace will remove it permanently
|
|
expect(
|
|
await fs.pathExists(
|
|
path.join(appDataPath, 'deleted-workspaces', workspaceId)
|
|
)
|
|
).toBe(false);
|
|
});
|
|
|
|
test.each([
|
|
{
|
|
name: 'deleting a workspace',
|
|
outsideDirName: 'outside-delete-target',
|
|
call: async () => {
|
|
const { deleteWorkspace } =
|
|
await import('@affine/electron/helper/workspace/handlers');
|
|
return deleteWorkspace(
|
|
universalId({
|
|
peer: 'local',
|
|
type: 'workspace',
|
|
id: '../../outside-delete-target',
|
|
})
|
|
);
|
|
},
|
|
},
|
|
{
|
|
name: 'deleting backup workspaces',
|
|
outsideDirName: 'outside-backup-target',
|
|
call: async () => {
|
|
const { deleteBackupWorkspace } =
|
|
await import('@affine/electron/helper/workspace/handlers');
|
|
return deleteBackupWorkspace('../../outside-backup-target');
|
|
},
|
|
},
|
|
{
|
|
name: 'recovering backup workspaces',
|
|
outsideDirName: 'outside-recover-target',
|
|
call: async () => {
|
|
const { recoverBackupWorkspace } =
|
|
await import('@affine/electron/helper/workspace/handlers');
|
|
return recoverBackupWorkspace('../../outside-recover-target');
|
|
},
|
|
},
|
|
])('rejects unsafe ids when $name', async ({ outsideDirName, call }) => {
|
|
const outsideDir = path.join(tmpDir, outsideDirName);
|
|
|
|
await fs.ensureDir(outsideDir);
|
|
|
|
await expect(call()).rejects.toThrow('Invalid workspace id');
|
|
expect(await fs.pathExists(outsideDir)).toBe(true);
|
|
});
|
|
});
|