Files
outline/plugins/granola/server/tasks/GranolaNoteTask.test.ts
T
Tom Moor ba2d9888cc feat: Granola integration (#14906)
* wip

* fix: Address review feedback on Granola integration

- Limit the size of Granola API responses
- Load the team when authorizing notes into personal collections
- Add the user's connection from granola.list to the integrations store

* feat: Connect Granola from a dialog

* fix: Accept Granola webhook test events

* Copy pass

* Copy pass
2026-12-05 14:59:12 -05:00

197 lines
5.9 KiB
TypeScript

import {
IntegrationService,
IntegrationType,
TeamPreference,
} from "@shared/types";
import {
Document,
Integration,
IntegrationAuthentication,
} from "@server/models";
import type { User } from "@server/models";
import {
buildCollection,
buildDocument,
buildPersonalCollection,
buildTeam,
buildUser,
} from "@server/test/factories";
import type { GranolaRule } from "../../shared/types";
import { Granola, GranolaApiError } from "../granola";
import GranolaNoteTask from "./GranolaNoteTask";
async function buildIntegration(user: User, rules: GranolaRule[]) {
const authentication = await IntegrationAuthentication.create({
service: IntegrationService.Granola,
userId: user.id,
teamId: user.teamId,
token: "grn_key",
clientSecret: "whsec_secret",
});
return Integration.create<Integration<IntegrationType.LinkedAccount>>({
service: IntegrationService.Granola,
type: IntegrationType.LinkedAccount,
userId: user.id,
teamId: user.teamId,
authenticationId: authentication.id,
settings: { granola: { webhookEndpointId: "whe_1", rules } },
});
}
const note = {
id: "not_1d3tmYTlCICgjy",
title: "Weekly sync",
web_url: "https://notes.granola.ai/d/1",
created_at: "2026-01-27T15:30:00Z",
attendees: [],
folder_membership: [{ id: "fol_1" }],
summary_markdown: "Summary text",
};
describe("GranolaNoteTask", () => {
afterEach(() => {
vi.restoreAllMocks();
});
it("creates a published document in the collection of the rule", async () => {
const user = await buildUser();
const collection = await buildCollection({
teamId: user.teamId,
userId: user.id,
});
const integration = await buildIntegration(user, [
{ folderId: null, collectionId: collection.id },
]);
const getNote = vi.spyOn(Granola, "getNote").mockResolvedValue(note);
const task = new GranolaNoteTask();
await task.perform({ integrationId: integration.id, noteId: note.id });
await task.perform({ integrationId: integration.id, noteId: note.id });
const documents = await Document.findAll({
where: { collectionId: collection.id },
});
expect(documents).toHaveLength(1);
expect(documents[0].title).toEqual("Weekly sync");
expect(documents[0].publishedAt).toBeTruthy();
expect(documents[0].createdById).toEqual(user.id);
expect(documents[0].createdAt.toISOString()).toEqual(
"2026-01-27T15:30:00.000Z"
);
expect(documents[0].sourceMetadata?.externalId).toEqual(note.id);
expect(documents[0].sourceMetadata?.externalUrl).toEqual(note.web_url);
expect(getNote).toHaveBeenCalledTimes(1);
});
it("creates a document in a personal collection", async () => {
const team = await buildTeam();
team.setPreference(TeamPreference.PersonalDocs, true);
await team.save();
const user = await buildUser({ teamId: team.id });
const collection = await buildPersonalCollection(user);
const integration = await buildIntegration(user, [
{ folderId: null, collectionId: collection.id },
]);
vi.spyOn(Granola, "getNote").mockResolvedValue(note);
await new GranolaNoteTask().perform({
integrationId: integration.id,
noteId: note.id,
});
expect(
await Document.count({ where: { collectionId: collection.id } })
).toEqual(1);
});
it("nests the document under the document of a folder rule", async () => {
const user = await buildUser();
const parent = await buildDocument({
teamId: user.teamId,
userId: user.id,
});
const other = await buildCollection({
teamId: user.teamId,
userId: user.id,
});
const integration = await buildIntegration(user, [
{ folderId: null, collectionId: other.id },
{ folderId: "fol_1", documentId: parent.id },
]);
vi.spyOn(Granola, "getNote").mockResolvedValue(note);
await new GranolaNoteTask().perform({
integrationId: integration.id,
noteId: note.id,
});
const document = await Document.findOne({
where: { parentDocumentId: parent.id },
});
expect(document?.collectionId).toEqual(parent.collectionId);
});
it("does not create a document when no rule matches", async () => {
const user = await buildUser();
const collection = await buildCollection({
teamId: user.teamId,
userId: user.id,
});
const integration = await buildIntegration(user, [
{ folderId: "fol_2", collectionId: collection.id },
]);
vi.spyOn(Granola, "getNote").mockResolvedValue(note);
await new GranolaNoteTask().perform({
integrationId: integration.id,
noteId: note.id,
});
expect(
await Document.count({ where: { collectionId: collection.id } })
).toEqual(0);
});
it("does not create a document when the user lost access", async () => {
const user = await buildUser();
const collection = await buildCollection({
teamId: user.teamId,
permission: null,
});
const integration = await buildIntegration(user, [
{ folderId: null, collectionId: collection.id },
]);
vi.spyOn(Granola, "getNote").mockResolvedValue(note);
await new GranolaNoteTask().perform({
integrationId: integration.id,
noteId: note.id,
});
expect(
await Document.count({ where: { collectionId: collection.id } })
).toEqual(0);
});
it("ignores a note that the key cannot read", async () => {
const user = await buildUser();
const collection = await buildCollection({
teamId: user.teamId,
userId: user.id,
});
const integration = await buildIntegration(user, [
{ folderId: null, collectionId: collection.id },
]);
vi.spyOn(Granola, "getNote").mockRejectedValue(
new GranolaApiError(404, "Not found")
);
await expect(
new GranolaNoteTask().perform({
integrationId: integration.id,
noteId: note.id,
})
).resolves.toBeUndefined();
});
});