Files
MyNote/tests/unit/migration-service.test.ts
T

48 lines
2.2 KiB
TypeScript

import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import nodePath from 'node:path';
import os from 'node:os';
import { promises as fs } from 'node:fs';
import { MigrationService } from '../../apps/desktop/src/main/services/migration-service';
const tmp = nodePath.join(os.tmpdir(), 'dsh-mig-test');
beforeEach(async () => { await fs.rm(tmp, { recursive: true, force: true }); await fs.mkdir(tmp, { recursive: true }); });
afterEach(async () => { await fs.rm(tmp, { recursive: true, force: true }); });
describe('M10 MigrationService 幂等保护 (FR-D2/D3)', () => {
it('未迁移时 isMigrated=false', async () => {
const m = new MigrationService(tmp);
expect(await m.isMigrated()).toBe(false);
});
it('shouldMigrate:旧数据存在 + 未迁移 + 新库空 → true', async () => {
const oldDir = nodePath.join(tmp, 'old'); const vault = nodePath.join(tmp, 'vault');
await fs.mkdir(oldDir, { recursive: true }); await fs.mkdir(vault, { recursive: true });
await fs.writeFile(nodePath.join(oldDir, 'data.json'), '{}');
const m = new MigrationService(tmp);
expect(await m.shouldMigrate(oldDir, vault)).toBe(true);
});
it('shouldMigrate:新库非空 → false', async () => {
const oldDir = nodePath.join(tmp, 'old'); const vault = nodePath.join(tmp, 'vault');
await fs.mkdir(oldDir, { recursive: true }); await fs.mkdir(vault, { recursive: true });
await fs.writeFile(nodePath.join(vault, 'existing.md'), 'x');
const m = new MigrationService(tmp);
expect(await m.shouldMigrate(oldDir, vault)).toBe(false);
});
it('迁移后标记,再次 shouldMigrate=false(幂等)', async () => {
const oldDir = nodePath.join(tmp, 'old'); const vault = nodePath.join(tmp, 'vault');
await fs.mkdir(oldDir, { recursive: true }); await fs.mkdir(vault, { recursive: true });
await fs.writeFile(nodePath.join(oldDir, 'd.json'), '{}');
const m = new MigrationService(tmp);
await m.migrate(oldDir, vault);
expect(await m.isMigrated()).toBe(true);
expect(await m.shouldMigrate(oldDir, vault)).toBe(false);
});
it('旧数据不存在 → false', async () => {
const m = new MigrationService(tmp);
expect(await m.shouldMigrate(nodePath.join(tmp, 'nope'), tmp)).toBe(false);
});
});