Files
tstest/test/test.migration.node.ts

112 lines
3.6 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');
await plugins.smartfile.fs.ensureEmptyDir(tempDir);
const legacyFile = plugins.path.join(tempDir, 'test.browser.ts');
await plugins.smartfile.memory.toFs('// Legacy test file\nexport default Promise.resolve();', legacyFile);
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
await plugins.smartfile.fs.removeSync(tempDir);
});
tap.test('Migration - detects both legacy pattern', async () => {
// Create temporary legacy files
const tempDir = plugins.path.join(process.cwd(), '.nogit', 'test_migration_both');
await plugins.smartfile.fs.ensureEmptyDir(tempDir);
const browserFile = plugins.path.join(tempDir, 'test.browser.ts');
const bothFile = plugins.path.join(tempDir, 'test.both.ts');
await plugins.smartfile.memory.toFs('// Browser test\nexport default Promise.resolve();', browserFile);
await plugins.smartfile.memory.toFs('// Both test\nexport default Promise.resolve();', bothFile);
const migration = new Migration({
baseDir: tempDir,
pattern: '**/*.ts',
dryRun: true,
});
const legacyFiles = await migration.findLegacyFiles();
expect(legacyFiles.length).toEqual(2);
// Clean up
await plugins.smartfile.fs.removeSync(tempDir);
});
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');
await plugins.smartfile.fs.ensureEmptyDir(tempDir);
const legacyFile = plugins.path.join(tempDir, 'test.browser.ts');
await plugins.smartfile.memory.toFs('// Legacy test file\nexport default Promise.resolve();', legacyFile);
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.smartfile.fs.fileExists(legacyFile);
expect(fileExists).toEqual(true);
// Clean up
await plugins.smartfile.fs.removeSync(tempDir);
});
export default tap.start();