70 lines
2.0 KiB
TypeScript
70 lines
2.0 KiB
TypeScript
import { expect, tap } from '@git.zone/tstest/tapbundle';
|
|
|
|
import { createMigrationRunner } from '../ts_migrations/index.js';
|
|
|
|
function setPath(target: Record<string, any>, path: string, value: unknown): void {
|
|
const parts = path.split('.');
|
|
let cursor = target;
|
|
for (const part of parts.slice(0, -1)) {
|
|
cursor[part] = cursor[part] || {};
|
|
cursor = cursor[part];
|
|
}
|
|
cursor[parts[parts.length - 1]] = value;
|
|
}
|
|
|
|
function applySet(document: Record<string, any>, set: Record<string, unknown>): void {
|
|
for (const [key, value] of Object.entries(set)) {
|
|
setPath(document, key, value);
|
|
}
|
|
}
|
|
|
|
function createFakeDb(currentVersion: string) {
|
|
const ledgerDocument = {
|
|
nameId: 'smartmigration:smartmigration',
|
|
data: {
|
|
currentVersion,
|
|
steps: {},
|
|
lock: { holder: null, acquiredAt: null, expiresAt: null },
|
|
checkpoints: {},
|
|
},
|
|
};
|
|
|
|
const emptyCollection = {
|
|
find: () => ({
|
|
async *[Symbol.asyncIterator]() {},
|
|
}),
|
|
updateMany: async () => ({ modifiedCount: 0 }),
|
|
};
|
|
|
|
const ledgerCollection = {
|
|
createIndex: async () => undefined,
|
|
findOne: async () => structuredClone(ledgerDocument),
|
|
findOneAndUpdate: async (_query: unknown, update: any) => {
|
|
applySet(ledgerDocument, update.$set || {});
|
|
return structuredClone(ledgerDocument);
|
|
},
|
|
updateOne: async (_query: unknown, update: any) => {
|
|
applySet(ledgerDocument, update.$set || {});
|
|
return { matchedCount: 1, modifiedCount: 1, upsertedCount: 0 };
|
|
},
|
|
};
|
|
|
|
return {
|
|
mongoDb: {
|
|
collection: (name: string) =>
|
|
name === 'SmartdataEasyStore' ? ledgerCollection : emptyCollection,
|
|
},
|
|
};
|
|
}
|
|
|
|
tap.test('migration runner bridges old package-version targets without real schema steps', async () => {
|
|
const runner = await createMigrationRunner(createFakeDb('13.16.0'), '13.31.0');
|
|
const result = await runner.run();
|
|
|
|
expect(result.currentVersionBefore).toEqual('13.16.0');
|
|
expect(result.currentVersionAfter).toEqual('13.31.0');
|
|
expect(result.stepsApplied).toHaveLength(3);
|
|
});
|
|
|
|
export default tap.start();
|