115 lines
4.1 KiB
TypeScript
115 lines
4.1 KiB
TypeScript
import { expect, tap } from '../ts_tapbundle/index.js';
|
|
import { Migration } from '../ts/tstest.classes.migration.js';
|
|
import * as plugins from '../ts/tstest.plugins.js';
|
|
import * as paths from '../ts/tstest.paths.js';
|
|
|
|
tap.test('Migration - can initialize', async () => {
|
|
const migration = new Migration({
|
|
baseDir: process.cwd(),
|
|
dryRun: true,
|
|
});
|
|
|
|
expect(migration).toBeInstanceOf(Migration);
|
|
});
|
|
|
|
tap.test('Migration - findLegacyFiles returns empty for no legacy files', async () => {
|
|
const migration = new Migration({
|
|
baseDir: process.cwd(),
|
|
pattern: 'test/test.migration.node.ts', // This file itself, not legacy
|
|
dryRun: true,
|
|
});
|
|
|
|
const legacyFiles = await migration.findLegacyFiles();
|
|
expect(legacyFiles).toEqual([]);
|
|
});
|
|
|
|
tap.test('Migration - generateReport works', async () => {
|
|
const migration = new Migration({
|
|
baseDir: process.cwd(),
|
|
dryRun: true,
|
|
});
|
|
|
|
const report = await migration.generateReport();
|
|
expect(report).toBeTypeOf('string');
|
|
expect(report).toContain('Test File Migration Report');
|
|
});
|
|
|
|
tap.test('Migration - detects legacy files when they exist', async () => {
|
|
// Create a temporary legacy test file
|
|
const tempDir = plugins.path.join(process.cwd(), '.nogit', 'test_migration');
|
|
try { await plugins.smartfsInstance.directory(tempDir).recursive().delete(); } catch (e) { /* may not exist */ }
|
|
await plugins.smartfsInstance.directory(tempDir).recursive().create();
|
|
|
|
const legacyFile = plugins.path.join(tempDir, 'test.browser.ts');
|
|
await plugins.smartfsInstance.file(legacyFile).write('// Legacy test file\nexport default Promise.resolve();');
|
|
|
|
const migration = new Migration({
|
|
baseDir: tempDir,
|
|
pattern: '**/*.ts',
|
|
dryRun: true,
|
|
});
|
|
|
|
const legacyFiles = await migration.findLegacyFiles();
|
|
expect(legacyFiles.length).toEqual(1);
|
|
expect(legacyFiles[0]).toContain('test.browser.ts');
|
|
|
|
// Clean up
|
|
plugins.fs.rmSync(tempDir, { recursive: true, force: true });
|
|
});
|
|
|
|
tap.test('Migration - detects both legacy pattern', async () => {
|
|
// Create temporary legacy files
|
|
const tempDir = plugins.path.join(process.cwd(), '.nogit', 'test_migration_both');
|
|
try { await plugins.smartfsInstance.directory(tempDir).recursive().delete(); } catch (e) { /* may not exist */ }
|
|
await plugins.smartfsInstance.directory(tempDir).recursive().create();
|
|
|
|
const browserFile = plugins.path.join(tempDir, 'test.browser.ts');
|
|
const bothFile = plugins.path.join(tempDir, 'test.both.ts');
|
|
await plugins.smartfsInstance.file(browserFile).write('// Browser test\nexport default Promise.resolve();');
|
|
await plugins.smartfsInstance.file(bothFile).write('// Both test\nexport default Promise.resolve();');
|
|
|
|
const migration = new Migration({
|
|
baseDir: tempDir,
|
|
pattern: '**/*.ts',
|
|
dryRun: true,
|
|
});
|
|
|
|
const legacyFiles = await migration.findLegacyFiles();
|
|
expect(legacyFiles.length).toEqual(2);
|
|
|
|
// Clean up
|
|
plugins.fs.rmSync(tempDir, { recursive: true, force: true });
|
|
});
|
|
|
|
tap.test('Migration - dry run does not modify files', async () => {
|
|
// Create a temporary legacy test file
|
|
const tempDir = plugins.path.join(process.cwd(), '.nogit', 'test_migration_dryrun');
|
|
try { await plugins.smartfsInstance.directory(tempDir).recursive().delete(); } catch (e) { /* may not exist */ }
|
|
await plugins.smartfsInstance.directory(tempDir).recursive().create();
|
|
|
|
const legacyFile = plugins.path.join(tempDir, 'test.browser.ts');
|
|
await plugins.smartfsInstance.file(legacyFile).write('// Legacy test file\nexport default Promise.resolve();');
|
|
|
|
const migration = new Migration({
|
|
baseDir: tempDir,
|
|
pattern: '**/*.ts',
|
|
dryRun: true,
|
|
verbose: false,
|
|
});
|
|
|
|
const summary = await migration.run();
|
|
|
|
expect(summary.dryRun).toEqual(true);
|
|
expect(summary.totalLegacyFiles).toEqual(1);
|
|
expect(summary.migratedCount).toEqual(1); // Dry run still counts as "would migrate"
|
|
|
|
// Verify original file still exists
|
|
const fileExists = await plugins.smartfsInstance.file(legacyFile).exists();
|
|
expect(fileExists).toEqual(true);
|
|
|
|
// Clean up
|
|
plugins.fs.rmSync(tempDir, { recursive: true, force: true });
|
|
});
|
|
|
|
export default tap.start();
|