Files
ghost/test/test.syncedinstance.node.ts

186 lines
5.7 KiB
TypeScript
Raw Normal View History

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 sourceGhost: ghost.Ghost;
let targetGhost: ghost.Ghost;
let syncedInstance: ghost.SyncedInstance;
let testTag: ghost.Tag;
let testPost: ghost.Post;
let testPage: ghost.Page;
tap.test('initialize source and target Ghost instances', async () => {
sourceGhost = new ghost.Ghost({
baseUrl: 'http://localhost:2368',
adminApiKey: await testQenv.getEnvVarOnDemand('ADMIN_APIKEY'),
contentApiKey: await testQenv.getEnvVarOnDemand('CONTENT_APIKEY'),
});
targetGhost = new ghost.Ghost({
baseUrl: 'http://localhost:2368',
adminApiKey: await testQenv.getEnvVarOnDemand('ADMIN_APIKEY'),
contentApiKey: await testQenv.getEnvVarOnDemand('CONTENT_APIKEY'),
});
expect(sourceGhost).toBeInstanceOf(ghost.Ghost);
expect(targetGhost).toBeInstanceOf(ghost.Ghost);
});
tap.test('create SyncedInstance', async () => {
syncedInstance = new ghost.SyncedInstance(sourceGhost, [targetGhost]);
expect(syncedInstance).toBeInstanceOf(ghost.SyncedInstance);
expect(syncedInstance.sourceGhost).toEqual(sourceGhost);
expect(syncedInstance.targetGhosts).toBeArray();
expect(syncedInstance.targetGhosts.length).toEqual(1);
});
tap.test('create test tag on source', async () => {
const timestamp = Date.now();
testTag = await sourceGhost.createTag({
name: `Sync Test Tag ${timestamp}`,
slug: `sync-test-tag-${timestamp}`,
description: 'This is a test tag for syncing'
});
expect(testTag).toBeInstanceOf(ghost.Tag);
});
tap.test('sync tags from source to target', async () => {
const report = await syncedInstance.syncTags();
expect(report).toBeTruthy();
expect(report.contentType).toEqual('tags');
expect(report.totalItems).toBeGreaterThan(0);
expect(report.targetReports).toBeArray();
expect(report.targetReports.length).toEqual(1);
const targetReport = report.targetReports[0];
expect(targetReport.results).toBeArray();
});
tap.test('verify sync status was tracked', async () => {
const status = syncedInstance.getSyncStatus();
expect(status.totalMappings).toBeGreaterThan(0);
expect(status.recentSyncs).toBeArray();
expect(status.recentSyncs.length).toBeGreaterThan(0);
});
tap.test('create test post on source', async () => {
const timestamp = Date.now();
testPost = await sourceGhost.createPost({
title: `Sync Test Post ${timestamp}`,
slug: `sync-test-post-${timestamp}`,
html: '<p>This is a test post for syncing</p>',
status: 'published',
tags: [{ id: testTag.tagData.id }]
});
expect(testPost).toBeInstanceOf(ghost.Post);
});
tap.test('sync posts from source to target', async () => {
const report = await syncedInstance.syncPosts();
expect(report).toBeTruthy();
expect(report.contentType).toEqual('posts');
expect(report.totalItems).toBeGreaterThan(0);
expect(report.targetReports).toBeArray();
expect(report.targetReports.length).toEqual(1);
});
tap.test('verify post sync in status', async () => {
const status = syncedInstance.getSyncStatus();
expect(status.recentSyncs).toBeArray();
const postSync = status.recentSyncs.find(s => s.contentType === 'posts');
expect(postSync).toBeTruthy();
});
tap.test('create test page on source', async () => {
const timestamp = Date.now();
testPage = await sourceGhost.createPage({
title: `Sync Test Page ${timestamp}`,
slug: `sync-test-page-${timestamp}`,
html: '<p>This is a test page for syncing</p>',
status: 'published'
});
expect(testPage).toBeInstanceOf(ghost.Page);
});
tap.test('sync pages from source to target', async () => {
const report = await syncedInstance.syncPages();
expect(report).toBeTruthy();
expect(report.contentType).toEqual('pages');
expect(report.totalItems).toBeGreaterThan(0);
expect(report.targetReports).toBeArray();
expect(report.targetReports.length).toEqual(1);
});
tap.test('verify page sync in status', async () => {
const status = syncedInstance.getSyncStatus();
const pageSync = status.recentSyncs.find(s => s.contentType === 'pages');
expect(pageSync).toBeTruthy();
});
tap.test('test syncAll method', async () => {
const reports = await syncedInstance.syncAll({
types: ['tags', 'posts', 'pages'],
syncOptions: { dryRun: true }
});
expect(reports).toBeArray();
expect(reports.length).toEqual(3);
expect(reports[0].contentType).toEqual('tags');
expect(reports[1].contentType).toEqual('posts');
expect(reports[2].contentType).toEqual('pages');
});
tap.test('test clear methods', async () => {
syncedInstance.clearMappings();
syncedInstance.clearSyncHistory();
const status = syncedInstance.getSyncStatus();
expect(status.totalMappings).toEqual(0);
expect(status.recentSyncs.length).toEqual(0);
});
tap.test('update tag and re-sync', async () => {
await testTag.update({
description: 'Updated description for sync test'
});
const report = await syncedInstance.syncTags();
expect(report).toBeTruthy();
expect(report.totalItems).toBeGreaterThan(0);
});
tap.test('cleanup - delete synced content', async () => {
if (testPost) {
await testPost.delete();
const targetPosts = await targetGhost.getPosts({ filter: `slug:${testPost.postData.slug}` });
if (targetPosts.length > 0) {
await targetPosts[0].delete();
}
}
if (testPage) {
await testPage.delete();
try {
const targetPage = await targetGhost.getPageBySlug(testPage.pageData.slug);
await targetPage.delete();
} catch (error) {
}
}
if (testTag) {
await testTag.delete();
try {
const targetTag = await targetGhost.getTagBySlug(testTag.tagData.slug);
await targetTag.delete();
} catch (error) {
}
}
});
export default tap.start();