import { describe, expect, it } from "vitest"; import type { ExportSchema } from "../../types"; import { buildNotionProperties, mapItemToNotionProperties, toNotionValue } from "../handler"; import type { NotionExportConfig } from "../types"; const schema: ExportSchema = { labelBy: "name", properties: { $id: { type: "string" }, name: { type: "string" }, email: { type: "string", format: "email" }, website: { type: "string", format: "uri" }, signedUpAt: { type: "string", format: "date-time" }, seats: { type: "number" }, active: { type: "boolean" }, plan: { type: "string", enum: ["free", "pro"] }, tags: { type: "string", enum: ["a", "b"], metadata: { "ui:widget": "checkbox" } }, }, }; const typeOf = (properties: Record, name: string) => Object.keys(properties[name])[0]; describe("buildNotionProperties", () => { it("maps JSON Schema properties to Notion property types", () => { const properties = buildNotionProperties(schema); expect(typeOf(properties, "email")).toBe("email"); expect(typeOf(properties, "website")).toBe("url"); expect(typeOf(properties, "signedUpAt")).toBe("date"); expect(typeOf(properties, "seats")).toBe("number"); expect(typeOf(properties, "active")).toBe("checkbox"); expect(typeOf(properties, "plan")).toBe("select"); expect(typeOf(properties, "tags")).toBe("multi_select"); expect(typeOf(properties, "$id")).toBe("rich_text"); }); it("uses labelBy as the title property, which Notion requires exactly one of", () => { const properties = buildNotionProperties(schema); const titles = Object.keys(properties).filter((name) => typeOf(properties, name) === "title"); expect(titles).toEqual(["name"]); }); it("falls back to the first non-system string property when labelBy is missing", () => { const properties = buildNotionProperties({ ...schema, labelBy: undefined }); expect(typeOf(properties, "name")).toBe("title"); }); it("falls back to $id when there is no other string property", () => { const properties = buildNotionProperties({ properties: { $id: { type: "string" }, seats: { type: "number" } }, }); expect(typeOf(properties, "$id")).toBe("title"); }); it("always produces a title property, even for an empty schema", () => { const properties = buildNotionProperties({ properties: {} }); expect(Object.values(properties).some((config) => "title" in config)).toBe(true); }); }); describe("toNotionValue", () => { it("wraps text values in Notion's rich text shape", () => { expect(toNotionValue("hello", "title")).toEqual({ title: [{ text: { content: "hello" } }] }); expect(toNotionValue("hello", "rich_text")).toEqual({ rich_text: [{ text: { content: "hello" } }] }); }); it("coerces scalars", () => { expect(toNotionValue("42", "number")).toEqual({ number: 42 }); expect(toNotionValue("true", "checkbox")).toEqual({ checkbox: true }); expect(toNotionValue(false, "checkbox")).toEqual({ checkbox: false }); expect(toNotionValue("not-a-number", "number")).toEqual({ number: null }); }); it("splits comma-separated values for multi_select", () => { expect(toNotionValue("a, b", "multi_select")).toEqual({ multi_select: [{ name: "a" }, { name: "b" }], }); expect(toNotionValue(["a"], "multi_select")).toEqual({ multi_select: [{ name: "a" }] }); }); it("clears the property when the value is empty", () => { expect(toNotionValue(null, "rich_text")).toEqual({ rich_text: [] }); expect(toNotionValue("", "number")).toEqual({ number: null }); expect(toNotionValue(undefined, "date")).toEqual({ date: null }); }); it("never writes to computed property types", () => { expect(toNotionValue("x", "formula")).toBeNull(); expect(toNotionValue("x", "rollup")).toBeNull(); }); }); describe("mapItemToNotionProperties", () => { const config: Pick = { fieldMapping: { name: "Name" }, properties: { Name: "title", seats: "number", active: "checkbox" }, }; it("applies the field mapping and the remote property types", () => { expect(mapItemToNotionProperties({ name: "Ada", seats: 3, active: true }, config)).toEqual({ Name: { title: [{ text: { content: "Ada" } }] }, seats: { number: 3 }, active: { checkbox: true }, }); }); it("skips properties the remote data source does not have", () => { expect(mapItemToNotionProperties({ unknown: "x", seats: 1 }, config)).toEqual({ seats: { number: 1 } }); }); });