import { describe, expect, it } from "vitest"; import { access } from "../workflow-access.js"; import { notion } from "./index.js"; import { createProvisioningArtifact } from "./provisioning.js"; import { finishMetadataRecording, startMetadataRecording } from "./recorder.js"; describe("Notion-as-Code custom agent declarations", () => { it("preserves vNext instruction mentions in provisioning", () => { const recorder = startMetadataRecording(); const instructions = 'Review Issues and ask Analyst.'; notion.customAgent({ resourceId: "triage-agent", name: "Triage", instructions }); const { intents } = finishMetadataRecording(recorder); expect(createProvisioningArtifact(intents).intents).toEqual([ expect.objectContaining({ type: "custom_agent", resourceId: "triage-agent", instructions, }), ]); }); it("records access, web, URL trust, and triggers", () => { const tasks = notion.database("tasks-db", { dataSourceResourceId: "tasks", name: "Tasks", schema: { Title: { resourceId: "title", type: "title" } }, }); const recorder = startMetadataRecording(); notion.customAgent({ resourceId: "triage-agent", name: "Triage agent", webAccess: true, browserUse: true, trustedUrls: ["docs.example.com/*", "support.example.com"], access: [access.edit(tasks)], triggers: [ { type: "recurrence", resourceId: "weekday-trigger", frequency: "week", interval: 1, start: "2026-09-21T09:00:00", timeZone: "America/Los_Angeles", weekdays: { MO: true, FR: true }, }, { type: "page_added", resourceId: "task-added-trigger", dataSourceResourceId: "tasks", }, { type: "property_updated", resourceId: "task-update-trigger", dataSourceResourceId: "tasks", propertyConditions: { status: { type: "property_edited" }, }, triggerWhenPageContentEdited: true, }, ], }); expect(finishMetadataRecording(recorder).intents).toEqual([ { type: "custom_agent", resourceId: "triage-agent", name: "Triage agent", webAccess: true, browserUse: true, trustedUrls: { type: "list", urls: ["docs.example.com/*", "support.example.com"] }, access: [{ resourceId: "tasks-db", level: "edit" }], triggers: [ { type: "recurrence", resourceId: "weekday-trigger", frequency: "week", interval: 1, start: "2026-09-21T09:00:00", timeZone: "America/Los_Angeles", weekdays: { MO: true, FR: true }, }, { type: "page_added", resourceId: "task-added-trigger", dataSourceResourceId: "tasks", waitForEditsToFinish: false, }, { type: "property_updated", resourceId: "task-update-trigger", waitForEditsToFinish: false, dataSourceResourceId: "tasks", propertyConditions: { status: { type: "property_edited" }, }, triggerWhenPageContentEdited: true, }, ], }, ]); }); it("records browser disablement and no trusted URLs", () => { const recorder = startMetadataRecording(); notion.customAgent({ resourceId: "triage-agent", name: "Triage agent", browserUse: false, trustedUrls: "none", }); expect(finishMetadataRecording(recorder).intents).toEqual([ { type: "custom_agent", resourceId: "triage-agent", name: "Triage agent", browserUse: false, trustedUrls: { type: "list", urls: [] }, }, ]); }); it("records unrestricted trust and an explicit wait for edits", () => { const recorder = startMetadataRecording(); notion.customAgent({ resourceId: "triage-agent", name: "Triage agent", trustedUrls: "all", triggers: [ { type: "page_added", resourceId: "task-added-trigger", dataSourceResourceId: "tasks", waitForEditsToFinish: true, }, { type: "property_updated", resourceId: "task-update-trigger", dataSourceResourceId: "tasks", triggerWhenPageContentEdited: true, waitForEditsToFinish: true, }, ], }); expect(finishMetadataRecording(recorder).intents).toEqual([ { type: "custom_agent", resourceId: "triage-agent", name: "Triage agent", trustedUrls: { type: "all" }, triggers: [ { type: "page_added", resourceId: "task-added-trigger", dataSourceResourceId: "tasks", waitForEditsToFinish: true, }, { type: "property_updated", resourceId: "task-update-trigger", dataSourceResourceId: "tasks", triggerWhenPageContentEdited: true, waitForEditsToFinish: true, }, ], }, ]); }); it("rejects reserved resource IDs inside triggers", () => { expect(() => notion.customAgent({ resourceId: "triage-agent", name: "Triage agent", triggers: [ { type: "page_added", resourceId: "__notion_apps_trigger", dataSourceResourceId: "tasks", }, ], }), ).toThrow("reserved"); }); it("rejects reserved resource IDs inside access grants", () => { expect(() => notion.customAgent({ resourceId: "triage-agent", name: "Triage agent", access: [ access.view({ ...notion.page({ resourceId: "tasks" }), resourceId: "__notion_apps_tasks", }), ], }), ).toThrow("reserved"); }); }); it.each(["view", "comment", "edit", "fullAccess"] as const)( "serializes page %s access using the workflow level", (level) => { const home = notion.page({ resourceId: "home" }); const recorder = startMetadataRecording(); notion.customAgent({ resourceId: "agent", name: "Agent", access: [access[level](home)] }); expect(finishMetadataRecording(recorder).intents).toEqual([ { type: "custom_agent", resourceId: "agent", name: "Agent", access: [{ resourceId: "home", level }], }, ]); }, ); it("rejects data source grants from untyped callers", () => { const table = notion.database("table", { dataSourceResourceId: "source", name: "Table", schema: { Title: { resourceId: "title", type: "title" } }, }); expect(() => Reflect.apply(notion.customAgent, undefined, [ { resourceId: "agent", name: "Agent", access: [access.view(table.dataSource)], }, ]), ).toThrow("page or database handle"); }); it("rejects agent call grants from untyped callers", () => { const agent = notion.customAgent({ resourceId: "target", name: "Target" }); expect(() => Reflect.apply(notion.customAgent, undefined, [ { resourceId: "agent", name: "Agent", access: [access.call(agent)], }, ]), ).toThrow("page or database handle"); });