Something went wrong. Try again.
A CLI for publishing standard.site documents to ATProto
Something went wrong. Try again.
TypeScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300import { describe, expect, it } from "bun:test";import type { Agent } from "@atproto/api";import { createBlueskyPost, createDocument, updateDocument,} from "../src/lib/atproto";import type { BlogPost, PublisherConfig } from "../src/lib/types";
function createPost(overrides: Partial<BlogPost> = {}): BlogPost { return { filePath: "/tmp/hello-world.md", slug: "hello-world", frontmatter: { title: "Hello World", description: "A short description", publishDate: "2025-01-02T03:04:05.000Z", }, content: "Body content", rawContent: "---\ntitle: Hello World\n---\nBody content", rawFrontmatter: {}, ...overrides, };}
function createConfig( overrides: Partial<PublisherConfig> = {},): PublisherConfig { return { siteUrl: "https://example.com", contentDir: "./content", publicationUri: "at://did:plc:publisher/site.standard.publication/main", publishContent: false, ...overrides, };}
describe("ATProto document publishing", () => { it("does not persist canonicalUrl on newly created document records", async () => { let capturedRecord: Record<string, unknown> | undefined;
const agent = { did: "did:plc:publisher", com: { atproto: { repo: { createRecord: async (input: { repo: string; collection: string; record: Record<string, unknown>; }) => { capturedRecord = input.record; return { data: { cid: "new-cid", uri: "at://did:plc:publisher/site.standard.document/new", }, }; }, }, }, }, } as Agent;
await createDocument(agent, createPost(), createConfig());
expect(capturedRecord).toBeDefined(); expect(capturedRecord?.canonicalUrl).toBeUndefined(); expect(capturedRecord?.path).toBe("/posts/hello-world"); });
it("removes canonicalUrl from updated document records while preserving other fields", async () => { let capturedRecord: Record<string, unknown> | undefined;
const agent = { did: "did:plc:publisher", com: { atproto: { repo: { getRecord: async () => ({ data: { value: { $type: "site.standard.document", title: "Old title", site: "at://did:plc:publisher/site.standard.publication/main", path: "/posts/old", publishedAt: "2024-01-01T00:00:00.000Z", canonicalUrl: "https://example.com/posts/old", bskyPostRef: { uri: "at://did:plc:publisher/app.bsky.feed.post/abc", cid: "bsky-cid", }, location: "keep-me", }, }, }), putRecord: async (input: { repo: string; collection: string; rkey: string; record: Record<string, unknown>; }) => { capturedRecord = input.record; return { data: { cid: "updated-cid", uri: "at://did:plc:publisher/site.standard.document/existing", }, }; }, }, }, }, } as unknown as Agent;
await updateDocument( agent, createPost(), "at://did:plc:publisher/site.standard.document/existing", createConfig(), );
expect(capturedRecord).toBeDefined(); expect(capturedRecord?.canonicalUrl).toBeUndefined(); expect(capturedRecord?.path).toBe("/posts/hello-world"); expect(capturedRecord?.location).toBe("keep-me"); expect(capturedRecord?.bskyPostRef).toEqual({ uri: "at://did:plc:publisher/app.bsky.feed.post/abc", cid: "bsky-cid", }); });
it("omits textContent entirely when publishContent is disabled", async () => { let capturedRecord: Record<string, unknown> | undefined;
const agent = { did: "did:plc:publisher", com: { atproto: { repo: { createRecord: async (input: { repo: string; collection: string; record: Record<string, unknown>; }) => { capturedRecord = input.record; return { data: { cid: "new-cid", uri: "at://did:plc:publisher/site.standard.document/new", }, }; }, }, }, }, } as Agent;
await createDocument(agent, createPost(), createConfig());
expect(capturedRecord).toBeDefined(); expect("textContent" in capturedRecord!).toBe(false); });
it("includes textContent when publishContent is enabled", async () => { let capturedRecord: Record<string, unknown> | undefined;
const agent = { did: "did:plc:publisher", com: { atproto: { repo: { createRecord: async (input: { repo: string; collection: string; record: Record<string, unknown>; }) => { capturedRecord = input.record; return { data: { cid: "new-cid", uri: "at://did:plc:publisher/site.standard.document/new", }, }; }, }, }, }, } as Agent;
await createDocument( agent, createPost(), createConfig({ publishContent: true }), );
expect(capturedRecord?.textContent).toBe("Body content"); });
it("strips existing textContent on update when publishContent is disabled", async () => { let capturedRecord: Record<string, unknown> | undefined;
const agent = { did: "did:plc:publisher", com: { atproto: { repo: { getRecord: async () => ({ data: { value: { $type: "site.standard.document", title: "Old title", site: "at://did:plc:publisher/site.standard.publication/main", path: "/posts/old", publishedAt: "2024-01-01T00:00:00.000Z", textContent: "Previously published body", }, }, }), putRecord: async (input: { repo: string; collection: string; rkey: string; record: Record<string, unknown>; }) => { capturedRecord = input.record; return { data: { cid: "updated-cid", uri: "at://did:plc:publisher/site.standard.document/existing", }, }; }, }, }, }, } as unknown as Agent;
await updateDocument( agent, createPost(), "at://did:plc:publisher/site.standard.document/existing", createConfig(), );
expect(capturedRecord).toBeDefined(); expect("textContent" in capturedRecord!).toBe(false); });
it("still uses canonicalUrl for the Bluesky external embed URL", async () => { let capturedRecord: Record<string, unknown> | undefined;
const agent = { did: "did:plc:publisher", com: { atproto: { repo: { createRecord: async (input: { repo: string; collection: string; record: Record<string, unknown>; }) => { capturedRecord = input.record; return { data: { cid: "bsky-cid", uri: "at://did:plc:publisher/app.bsky.feed.post/new", }, }; }, }, }, }, } as Agent;
await createBlueskyPost(agent, { title: "Hello World", description: "A short description", canonicalUrl: "https://example.com/posts/hello-world", documentRef: { uri: "at://did:plc:publisher/site.standard.document/hello-world", cid: "doc-cid", }, publicationRef: { uri: "at://did:plc:publisher/site.standard.publication/main", cid: "pub-cid", }, publishedAt: "2025-01-02T03:04:05.000Z", });
expect(capturedRecord).toBeDefined(); expect(capturedRecord?.embed).toMatchObject({ $type: "app.bsky.embed.external", external: { uri: "https://example.com/posts/hello-world", }, }); });});