Files
Netcatty/application/state/pluginImporterTransaction.test.ts
T

57 lines
2.3 KiB
TypeScript

import assert from 'node:assert/strict';
import test from 'node:test';
import { STORAGE_KEY_PLUGIN_IMPORT_TRANSACTION } from '../../infrastructure/config/storageKeys';
import {
commitPluginImporterTransaction,
recoverPluginImporterTransaction,
} from './pluginImporterTransaction';
const storage = (initial: Record<string, string> = {}) => {
const values = new Map(Object.entries(initial));
return {
values,
read<T>(key: string): T | null {
const value = values.get(key);
return value === undefined ? null : JSON.parse(value) as T;
},
readString: (key: string) => values.get(key) ?? null,
write<T>(key: string, value: T) { values.set(key, JSON.stringify(value)); return true; },
writeString(key: string, value: string) { values.set(key, value); return true; },
remove(key: string) { values.delete(key); },
};
};
test('plugin importer transaction commits all keys and removes its journal', () => {
const target = storage({ hosts: JSON.stringify(['old']) });
commitPluginImporterTransaction(target, [
['hosts', ['new']],
['keys', ['key']],
]);
assert.deepEqual(target.read('hosts'), ['new']);
assert.deepEqual(target.read('keys'), ['key']);
assert.equal(target.readString(STORAGE_KEY_PLUGIN_IMPORT_TRANSACTION), null);
});
test('plugin importer recovery rolls back a crash during the prepared phase', () => {
const target = storage({ hosts: JSON.stringify(['partial']), keys: JSON.stringify(['old-key']) });
target.write(STORAGE_KEY_PLUGIN_IMPORT_TRANSACTION, {
version: 1,
phase: 'prepared',
previous: [
{ key: 'hosts', value: JSON.stringify(['old-host']) },
{ key: 'keys', value: JSON.stringify(['old-key']) },
],
});
assert.equal(recoverPluginImporterTransaction(target, new Set(['hosts', 'keys'])), 'rolled-back');
assert.deepEqual(target.read('hosts'), ['old-host']);
assert.deepEqual(target.read('keys'), ['old-key']);
assert.equal(target.readString(STORAGE_KEY_PLUGIN_IMPORT_TRANSACTION), null);
});
test('plugin importer recovery keeps fully committed values', () => {
const target = storage({ hosts: JSON.stringify(['new']) });
target.write(STORAGE_KEY_PLUGIN_IMPORT_TRANSACTION, { version: 1, phase: 'committed' });
assert.equal(recoverPluginImporterTransaction(target, new Set(['hosts'])), 'committed');
assert.deepEqual(target.read('hosts'), ['new']);
});