145 lines
4.9 KiB
TypeScript
145 lines
4.9 KiB
TypeScript
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;
|
|
let createdPost: ghost.Post;
|
|
|
|
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 get all posts', async () => {
|
|
const posts = await testGhostInstance.getPosts();
|
|
expect(posts).toBeArray();
|
|
if (posts.length > 0) {
|
|
expect(posts[0]).toBeInstanceOf(ghost.Post);
|
|
console.log(`Found ${posts.length} posts`);
|
|
}
|
|
});
|
|
|
|
tap.test('should get posts with limit', async () => {
|
|
const posts = await testGhostInstance.getPosts({ limit: 5 });
|
|
expect(posts).toBeArray();
|
|
expect(posts.length).toBeLessThanOrEqual(5);
|
|
});
|
|
|
|
tap.test('should filter posts by tag', async () => {
|
|
const tags = await testGhostInstance.getTags({ limit: 1 });
|
|
if (tags.length > 0) {
|
|
const posts = await testGhostInstance.getPosts({ tag: tags[0].slug, limit: 5 });
|
|
expect(posts).toBeArray();
|
|
console.log(`Found ${posts.length} posts with tag '${tags[0].name}'`);
|
|
}
|
|
});
|
|
|
|
tap.test('should filter posts by author', async () => {
|
|
const authors = await testGhostInstance.getAuthors({ limit: 1 });
|
|
if (authors.length > 0) {
|
|
const posts = await testGhostInstance.getPosts({ author: authors[0].getSlug(), limit: 5 });
|
|
expect(posts).toBeArray();
|
|
console.log(`Found ${posts.length} posts by author '${authors[0].getName()}'`);
|
|
}
|
|
});
|
|
|
|
tap.test('should filter posts by featured status', async () => {
|
|
const featuredPosts = await testGhostInstance.getPosts({ featured: true, limit: 5 });
|
|
expect(featuredPosts).toBeArray();
|
|
console.log(`Found ${featuredPosts.length} featured posts`);
|
|
if (featuredPosts.length > 0) {
|
|
expect(featuredPosts[0].postData.featured).toEqual(true);
|
|
}
|
|
});
|
|
|
|
tap.test('should search posts by title', async () => {
|
|
const searchResults = await testGhostInstance.searchPosts('the', { limit: 5 });
|
|
expect(searchResults).toBeArray();
|
|
console.log(`Found ${searchResults.length} posts matching 'the'`);
|
|
});
|
|
|
|
tap.test('should get post by ID', async () => {
|
|
const posts = await testGhostInstance.getPosts({ limit: 1 });
|
|
if (posts.length > 0) {
|
|
const post = await testGhostInstance.getPostById(posts[0].getId());
|
|
expect(post).toBeInstanceOf(ghost.Post);
|
|
expect(post.getId()).toEqual(posts[0].getId());
|
|
}
|
|
});
|
|
|
|
tap.test('should get related posts', async () => {
|
|
const posts = await testGhostInstance.getPosts({ limit: 1 });
|
|
if (posts.length > 0) {
|
|
const relatedPosts = await testGhostInstance.getRelatedPosts(posts[0].getId(), 3);
|
|
expect(relatedPosts).toBeArray();
|
|
console.log(`Found ${relatedPosts.length} related posts for '${posts[0].getTitle()}'`);
|
|
}
|
|
});
|
|
|
|
tap.test('should create post from HTML', async () => {
|
|
const timestamp = Date.now();
|
|
createdPost = await testGhostInstance.createPostFromHtml({
|
|
title: `Test Post ${timestamp}`,
|
|
html: '<p>This is a test post created by automated tests.</p>',
|
|
status: 'published'
|
|
} as any);
|
|
expect(createdPost).toBeInstanceOf(ghost.Post);
|
|
expect(createdPost.getTitle()).toEqual(`Test Post ${timestamp}`);
|
|
console.log(`Created post: ${createdPost.getId()}`);
|
|
});
|
|
|
|
tap.test('should access post methods', async () => {
|
|
if (createdPost) {
|
|
expect(createdPost.getId()).toBeTruthy();
|
|
expect(createdPost.getTitle()).toBeTruthy();
|
|
const json = createdPost.toJson();
|
|
expect(json).toBeTruthy();
|
|
expect(json.id).toEqual(createdPost.getId());
|
|
}
|
|
});
|
|
|
|
tap.test('should update post', async () => {
|
|
if (createdPost) {
|
|
const updatedPost = await createdPost.update({
|
|
...createdPost.postData,
|
|
html: '<p>This post has been updated.</p>'
|
|
});
|
|
expect(updatedPost).toBeInstanceOf(ghost.Post);
|
|
console.log(`Updated post: ${updatedPost.getId()}`);
|
|
}
|
|
});
|
|
|
|
tap.test('should delete post', async () => {
|
|
if (createdPost) {
|
|
await createdPost.delete();
|
|
console.log(`Deleted post: ${createdPost.getId()}`);
|
|
}
|
|
});
|
|
|
|
tap.test('should bulk update posts', async () => {
|
|
const posts = await testGhostInstance.getPosts({ limit: 2 });
|
|
if (posts.length >= 2) {
|
|
const postIds = posts.map(p => p.getId());
|
|
const originalFeatured = posts[0].postData.featured;
|
|
|
|
const updatedPosts = await testGhostInstance.bulkUpdatePosts(postIds, {
|
|
featured: !originalFeatured
|
|
});
|
|
expect(updatedPosts).toBeArray();
|
|
expect(updatedPosts.length).toEqual(postIds.length);
|
|
|
|
await testGhostInstance.bulkUpdatePosts(postIds, {
|
|
featured: originalFeatured
|
|
});
|
|
console.log(`Bulk updated ${updatedPosts.length} posts`);
|
|
}
|
|
});
|
|
|
|
export default tap.start();
|