import { describe, expect, expectTypeOf, it } from "vitest";
import { notion, type PageArgs, type PageIcon, type PropertyValue } from "./index.js";
import { createProvisioningArtifact } from "./provisioning.js";
import { finishMetadataRecording, startMetadataRecording } from "./recorder.js";
describe("Notion-as-Code page declarations", () => {
it("preserves vNext blocks and symbolic resource mentions in provisioning", () => {
const recorder = startMetadataRecording();
const content =
'Review Guide.\n\nIssues';
notion.page({ resourceId: "home", content });
const { intents } = finishMetadataRecording(recorder);
expect(createProvisioningArtifact(intents).intents).toEqual([
expect.objectContaining({ type: "page", resourceId: "home", content }),
]);
});
it("excludes file-backed icons from page arguments", () => {
expectTypeOf().toEqualTypeOf();
expectTypeOf>().toEqualTypeOf();
});
it.each(["π", "π΅οΈββοΈ", "ππ½", "πΊπΈ", "1οΈβ£", "β€"])("records the standard emoji %s", (emoji) => {
const recorder = startMetadataRecording();
notion.page({
resourceId: "emoji-page",
icon: { type: "emoji", emoji },
});
expect(finishMetadataRecording(recorder).intents[0]).toEqual(
expect.objectContaining({
icon: { type: "emoji", emoji },
}),
);
});
it("records a colored Notion icon on a child page", () => {
const recorder = startMetadataRecording();
const parent = notion.page({ resourceId: "parent" });
parent.addPage({
resourceId: "guide",
icon: { type: "notion_icon", description: "rocket", color: "blue" },
});
expect(finishMetadataRecording(recorder).intents[1]).toEqual(
expect.objectContaining({
parent: { type: "resourceId", resourceId: "parent" },
icon: { type: "notion_icon", description: "rocket", color: "blue" },
}),
);
});
it.each([":team:", "emoji:123", "https://example.com/icon.png", "ππ"])(
"rejects %s as a standard emoji before recording",
(emoji) => {
const recorder = startMetadataRecording();
expect(() =>
notion.page({
resourceId: "invalid-emoji",
icon: { type: "emoji", emoji },
}),
).toThrow("standard emoji");
expect(finishMetadataRecording(recorder).intents).toEqual([]);
},
);
it("records typed URL covers", () => {
const recorder = startMetadataRecording();
notion.page({
resourceId: "url-cover-page",
cover: {
type: "url",
url: "https://example.com/cover.png",
position: 0.25,
},
});
expect(finishMetadataRecording(recorder).intents).toEqual([
expect.objectContaining({
resourceId: "url-cover-page",
cover: {
type: "url",
url: "https://example.com/cover.png",
position: 0.25,
},
}),
]);
});
it("allows parenting a page to a reserved Apps resource ID", () => {
const recorder = startMetadataRecording();
notion.page({
resourceId: "guide",
parent: { type: "resourceId", resourceId: "__notion_apps_app_md__" },
properties: { title: notion.text("Guide") },
});
expect(finishMetadataRecording(recorder).intents[0]).toEqual(
expect.objectContaining({
resourceId: "guide",
parent: { type: "resourceId", resourceId: "__notion_apps_app_md__" },
}),
);
});
it("rejects creating a page whose resource ID uses the reserved Apps prefix", () => {
expect(() =>
notion.page({
resourceId: "__notion_apps_guide__",
properties: { title: notion.text("Guide") },
}),
).toThrow('Resource ID "__notion_apps_guide__" uses the reserved Apps prefix');
});
it("records a regular page title separately from Markdown content", () => {
const properties = {
title: notion.text("Workflow guide"),
} satisfies Record;
const recorder = startMetadataRecording();
notion.page({
resourceId: "workflow-guide",
properties,
content: "# Body heading",
});
expectTypeOf().toEqualTypeOf<
Record | undefined
>();
expect(finishMetadataRecording(recorder).intents).toEqual([
{
type: "page",
resourceId: "workflow-guide",
parent: {
type: "resourceId",
resourceId: "__notion_apps_workspace__",
},
properties: { title: [["Workflow guide"]] },
content: "# Body heading",
},
]);
});
});