fix(syncedinstance): Prevent same-instance syncs and sanitize post update payloads; update tests and docs

This commit is contained in:
2025-10-11 06:16:44 +00:00
parent 00dd0c69a5
commit 2bb86552e2
9 changed files with 582 additions and 49 deletions

View File

@@ -0,0 +1,95 @@
import { expect, tap } from '@push.rocks/tapbundle';
import * as qenv from '@push.rocks/qenv';
const testQenv = new qenv.Qenv('./', './.nogit/');
import * as ghost from '../ts/index.js';
let testGhostInstance: ghost.Ghost;
tap.test('initialize Ghost instance', async () => {
testGhostInstance = new ghost.Ghost({
baseUrl: 'http://localhost:2368',
adminApiKey: await testQenv.getEnvVarOnDemand('ADMIN_APIKEY'),
contentApiKey: await testQenv.getEnvVarOnDemand('CONTENT_APIKEY'),
});
expect(testGhostInstance).toBeInstanceOf(ghost.Ghost);
});
tap.test('should throw error when creating SyncedInstance with same instance', async () => {
let errorThrown = false;
let errorMessage = '';
try {
new ghost.SyncedInstance(testGhostInstance, [testGhostInstance]);
} catch (error) {
errorThrown = true;
errorMessage = error instanceof Error ? error.message : String(error);
}
expect(errorThrown).toEqual(true);
expect(errorMessage).toContain('Cannot sync to the same instance');
expect(errorMessage).toContain('localhost:2368');
console.log(`Correctly prevented same-instance sync: ${errorMessage}`);
});
tap.test('should throw error when target array includes same instance', async () => {
let errorThrown = false;
let errorMessage = '';
const anotherInstance = new ghost.Ghost({
baseUrl: 'http://localhost:2368',
adminApiKey: await testQenv.getEnvVarOnDemand('ADMIN_APIKEY'),
contentApiKey: await testQenv.getEnvVarOnDemand('CONTENT_APIKEY'),
});
try {
new ghost.SyncedInstance(testGhostInstance, [anotherInstance]);
} catch (error) {
errorThrown = true;
errorMessage = error instanceof Error ? error.message : String(error);
}
expect(errorThrown).toEqual(true);
expect(errorMessage).toContain('Cannot sync to the same instance');
console.log(`Correctly prevented sync with duplicate URL: ${errorMessage}`);
});
tap.test('should normalize URLs when comparing (trailing slash)', async () => {
let errorThrown = false;
const instanceWithTrailingSlash = new ghost.Ghost({
baseUrl: 'http://localhost:2368/',
adminApiKey: await testQenv.getEnvVarOnDemand('ADMIN_APIKEY'),
contentApiKey: await testQenv.getEnvVarOnDemand('CONTENT_APIKEY'),
});
try {
new ghost.SyncedInstance(testGhostInstance, [instanceWithTrailingSlash]);
} catch (error) {
errorThrown = true;
}
expect(errorThrown).toEqual(true);
console.log('Correctly detected same instance despite trailing slash difference');
});
tap.test('should normalize URLs when comparing (case insensitive)', async () => {
let errorThrown = false;
const instanceWithUpperCase = new ghost.Ghost({
baseUrl: 'http://LOCALHOST:2368',
adminApiKey: await testQenv.getEnvVarOnDemand('ADMIN_APIKEY'),
contentApiKey: await testQenv.getEnvVarOnDemand('CONTENT_APIKEY'),
});
try {
new ghost.SyncedInstance(testGhostInstance, [instanceWithUpperCase]);
} catch (error) {
errorThrown = true;
}
expect(errorThrown).toEqual(true);
console.log('Correctly detected same instance despite case difference');
});
export default tap.start();