Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions apps/worker/src/tasks/sync-post/processor.test.ts

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies, we actually need to support both time formats. Let's leave these tests with dates as-is and a new test for non-T dates

Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,90 @@ This is the post content.
]);
});

test("Syncs a post with a date-only published value", async () => {
const insertPostsValues = vi.fn().mockReturnValue({
onConflictDoNothing: vi.fn(),
});
const insertPostDataValues = vi.fn().mockReturnValue({
onConflictDoUpdate: vi.fn(),
});
const insertPostAuthorsValues = vi.fn();

vi.mocked(db.insert).mockImplementation((table) => {
if (table === posts) {
return { values: insertPostsValues } as never;
}
if (table === postData) {
return { values: insertPostDataValues } as never;
}
if (table === postAuthors) {
return { values: insertPostAuthorsValues } as never;
}
throw new Error(`Unexpected table: ${table}`);
});

const deleteWhere = vi.fn();
vi.mocked(db.delete).mockReturnValue({
where: deleteWhere,
} as never);

vi.mocked(github.getContents).mockImplementation(((params: {
path: string;
}) => {
if (params.path === "/content/example-author/posts/date-only-post/") {
return Promise.resolve({
data: {
entries: [
{
name: "index.md",
path: "content/example-author/posts/date-only-post/index.md",
},
],
},
status: 200,
});
}
return Promise.reject(new Error(`Unexpected path: ${params.path}`));
}) as never);

vi.mocked(github.getContentsRaw).mockImplementation((params) => {
if (
params.path === "/content/example-author/posts/date-only-post/index.md"
) {
return Promise.resolve({
data: `---
title: "Date Only Post"
description: "A test post with a date-only published value"
published: "2024-01-15"
---

# Hello World

This is the post content.
`,
status: 200,
});
}
return Promise.reject(new Error(`Unexpected path: ${params.path}`));
});

await processor({
data: {
author: "example-author",
post: "date-only-post",
ref: "main",
},
} as unknown as Job<TaskInputs["sync-post"]>);

expect(insertPostDataValues).toBeCalledWith(
expect.objectContaining({
slug: "date-only-post",
title: "Date Only Post",
publishedAt: new Date("2024-01-15"),
}),
);
});

test("Deletes a post record if it no longer exists", async () => {
const deleteWhere = vi.fn();
vi.mocked(db.delete).mockReturnValue({
Expand Down
5 changes: 4 additions & 1 deletion apps/worker/src/tasks/sync-post/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import { Type } from "typebox";
export const PostMetaSchema = Type.Object(
{
title: Type.String(),
published: Type.String({ format: "date-time" }),
published: Type.Union([
Type.String({ format: "date" }),
Type.String({ format: "date-time" }),
]),
description: Type.Optional(Type.String()),
version: Type.String({ default: "" }),
noindex: Type.Optional(Type.Boolean({ default: false })),
Expand Down