import { APIErrorCode, APIResponseError } from "@notionhq/client"; import { beforeEach, describe, expect, it, vi } from "vitest"; import type { NotionExportConfig } from "../types"; const pagesCreate = vi.fn(); const pagesUpdate = vi.fn(); // Only the transport is mocked: the real error classes are kept so the handler's // `isNotionClientError` / error-code narrowing is exercised for real. vi.mock("@notionhq/client", async (importOriginal) => { const actual = await importOriginal(); return { ...actual, Client: class { pages = { create: pagesCreate, update: pagesUpdate }; }, }; }); const { upsertNotionRecords } = await import("../handler"); const config: NotionExportConfig = { databaseId: "db", dataSourceId: "ds", properties: { Name: "title" }, fieldMapping: { name: "Name" }, createdByUpstart: false, }; function notionError(code: APIErrorCode, message: string) { return new APIResponseError({ code, status: code === APIErrorCode.ObjectNotFound ? 404 : 400, message, headers: new Headers(), rawBodyText: "", additional_data: undefined, request_id: undefined, }); } beforeEach(() => { pagesCreate.mockReset(); pagesUpdate.mockReset(); }); describe("upsertNotionRecords", () => { it("creates a page for an item that has no remote record yet", async () => { pagesCreate.mockResolvedValue({ id: "page-1" }); const results = await upsertNotionRecords({ accessToken: "tok", config, items: [{ itemId: "a", data: { name: "Ada" }, externalRecordId: null }], }); expect(pagesCreate).toHaveBeenCalledOnce(); expect(results[0]).toEqual({ itemId: "a", externalRecordId: "page-1" }); }); it("updates the page of an item already exported, restoring it if it was trashed", async () => { pagesUpdate.mockResolvedValue({ id: "page-1" }); const results = await upsertNotionRecords({ accessToken: "tok", config, items: [{ itemId: "a", data: { name: "Ada" }, externalRecordId: "page-1" }], }); // `in_trash: false` revives a page deleted on the Notion side — Notion refuses to // edit an archived page — and is a no-op on a live one. expect(pagesUpdate).toHaveBeenCalledWith(expect.objectContaining({ page_id: "page-1", in_trash: false })); expect(results[0]).toEqual({ itemId: "a", externalRecordId: "page-1" }); }); it("recreates the page immediately when it was permanently deleted", async () => { pagesUpdate.mockRejectedValue(notionError(APIErrorCode.ObjectNotFound, "Could not find page")); pagesCreate.mockResolvedValue({ id: "page-2" }); const results = await upsertNotionRecords({ accessToken: "tok", config, items: [{ itemId: "a", data: { name: "Ada" }, externalRecordId: "page-1" }], }); expect(pagesCreate).toHaveBeenCalledOnce(); expect(results[0]).toEqual({ itemId: "a", externalRecordId: "page-2" }); }); it("keeps the remote record id when the payload is rejected", async () => { // Typically a property renamed on the Notion side. Forgetting the id here would // create a duplicate page as soon as the schema is fixed. pagesUpdate.mockRejectedValue( notionError(APIErrorCode.ValidationError, "email is not a property that exists"), ); const results = await upsertNotionRecords({ accessToken: "tok", config, items: [{ itemId: "a", data: { name: "Ada" }, externalRecordId: "page-1" }], }); expect(results[0].error).toContain("is not a property that exists"); expect(results[0].resetExternalRecordId).toBe(false); }); it("forgets the remote record id only when the page is gone and recreating it fails too", async () => { pagesUpdate.mockRejectedValue(notionError(APIErrorCode.ObjectNotFound, "Could not find page")); pagesCreate.mockRejectedValue(notionError(APIErrorCode.ValidationError, "data source is full")); const results = await upsertNotionRecords({ accessToken: "tok", config, items: [{ itemId: "a", data: { name: "Ada" }, externalRecordId: "page-1" }], }); // Next run must start from a create rather than patching the dead id again. expect(results[0].resetExternalRecordId).toBe(true); }); it("reports a failed item without stopping the others", async () => { pagesCreate .mockRejectedValueOnce(notionError(APIErrorCode.ValidationError, "bad payload")) .mockResolvedValueOnce({ id: "page-2" }); const results = await upsertNotionRecords({ accessToken: "tok", config, items: [ { itemId: "a", data: { name: "Ada" }, externalRecordId: null }, { itemId: "b", data: { name: "Grace" }, externalRecordId: null }, ], }); expect(results[0].error).toContain("bad payload"); expect(results[1]).toEqual({ itemId: "b", externalRecordId: "page-2" }); }); });