AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents
TypeScript ai-agentai-agent-toolsai-agentsai-agents-frameworkmcpmcp-servermcp-toolsmcpsn8n-alternativeno-code-automationworkflowworkflow-automationworkflows
// @vitest-environment jsdom
import {
EmptyTrigger,
FlowOperationRequest,
FlowOperationStatus,
FlowOperationType,
FlowStatus,
FlowTriggerType,
FlowVersionState,
Note,
NoteColorVariant,
PopulatedFlow,
} from '@activepieces/shared';
import { QueryClient } from '@tanstack/react-query';
import { act } from 'react';
import { createRoot, type Root } from 'react-dom/client';
import { io } from 'socket.io-client';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import {
BuilderStateContext,
BuilderStore,
createBuilderStore,
} from '@/app/builder/builder-hooks';
import { flowCanvasHooks } from '@/app/builder/flow-canvas/hooks';
import {
ApNoteCanvasNode,
NoteContent,
} from '@/app/builder/flow-canvas/nodes/note-node';
import { ApNodeType } from '@/app/builder/flow-canvas/utils/types';
declare global {
// eslint-disable-next-line no-var
var IS_REACT_ACT_ENVIRONMENT: boolean;
}
globalThis.IS_REACT_ACT_ENVIRONMENT = true;
const flowsApiMock = vi.hoisted(() => ({
update:
vi.fn<
(
flowId: string,
operation: FlowOperationRequest,
) => Promise<PopulatedFlow>
>(),
get: vi.fn<(flowId: string) => Promise<PopulatedFlow>>(),
}));
const noteEditor = vi.hoisted((): { type: (value: string) => void } => ({
type: () => undefined,
}));
vi.mock('i18next', () => ({ t: (key: string) => key }));
vi.mock('@/features/flows/api/flows-api', () => ({
flowsApi: flowsApiMock,
}));
vi.mock('@/components/custom/markdown-input', () => ({
MarkdownInput: ({
onChange,
initialValue,
}: {
onChange: (value: string) => void;
initialValue: string;
}) => {
noteEditor.type = onChange;
return <div data-testid="note-editor">{initialValue}</div>;
},
}));
vi.mock('@xyflow/react', async (importOriginal) => ({
...(await importOriginal<typeof import('@xyflow/react')>()),
NodeResizeControl: () => null,
}));
describe('NoteContent', () => {
let container: HTMLDivElement;
let root: Root;
beforeEach(() => {
vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout', 'Date'] });
flowsApiMock.update.mockReset();
flowsApiMock.update.mockResolvedValue(flowWithNote(note()));
container = document.createElement('div');
document.body.appendChild(container);
root = createRoot(container);
});
afterEach(() => {
act(() => root.unmount());
container.remove();
vi.useRealTimers();
});
it('saves a note edit typed just before the builder unmounts', async () => {
const store = createStore(flowWithNote(note()));
act(() => {
root.render(
<BuilderStateContext.Provider value={store}>
<BuilderWithNote />
</BuilderStateContext.Provider>,
);
});
act(() => noteEditor.type('Call the customer'));
act(() => root.unmount());
await vi.advanceTimersByTimeAsync(0);
expect(sentOperations()).toEqual([
{
type: FlowOperationType.UPDATE_NOTE,
request: expect.objectContaining({
id: 'note-1',
content: 'Call the customer',
}),
},
]);
});
});
describe('ApNoteCanvasNode', () => {
let container: HTMLDivElement;
let root: Root;
beforeEach(() => {
vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout', 'Date'] });
flowsApiMock.update.mockReset();
flowsApiMock.get.mockReset();
container = document.createElement('div');
document.body.appendChild(container);
root = createRoot(container);
});
afterEach(() => {
act(() => root.unmount());
container.remove();
vi.useRealTimers();
});
it('shows the saved text again when a note edit fails to save', async () => {
flowsApiMock.update.mockRejectedValue(
new Error('Request failed with status code 400'),
);
flowsApiMock.get.mockResolvedValue(flowWithNote(note()));
const store = createStore(flowWithNote(note()));
renderNoteNode(store);
act(() => noteEditor.type('Text the server rejects'));
expect(shownNoteText()).toBe('Text the server rejects');
await act(() => vi.advanceTimersByTimeAsync(1000));
expect(store.getState().flowVersion.notes[0].content).toBe(
'Check the order',
);
expect(shownNoteText()).toBe('Check the order');
});
it('keeps the note editor when a Draft Refresh does not change the note', () => {
const store = createStore(flowWithNote(note()));
renderNoteNode(store);
const editorBefore = noteEditorElement();
act(() => {
const refreshedFlow = flowWithNote(note());
store.getState().applyDraftRefresh({
...refreshedFlow,
version: { ...refreshedFlow.version, displayName: 'Renamed flow' },
});
});
expect(noteEditorElement()).toBe(editorBefore);
});
function renderNoteNode(store: BuilderStore) {
act(() => {
root.render(
<BuilderStateContext.Provider value={store}>
<ApNoteCanvasNode
id="note-1"
type={ApNodeType.NOTE}
data={{
content: note().content,
ownerId: null,
color: NoteColorVariant.YELLOW,
size: note().size,
}}
dragging={false}
zIndex={0}
isConnectable={false}
positionAbsoluteX={0}
positionAbsoluteY={0}
/>
</BuilderStateContext.Provider>,
);
});
}
function noteEditorElement(): Element | null {
return container.querySelector('[data-testid="note-editor"]');
}
function shownNoteText(): string | null | undefined {
return noteEditorElement()?.textContent;
}
});
function BuilderWithNote() {
flowCanvasHooks.useFlushPendingEditsOnLeave();
return <NoteContent note={note()} isDragging={false} />;
}
function sentOperations(): FlowOperationRequest[] {
return flowsApiMock.update.mock.calls.map(([, operation]) => operation);
}
function createStore(flow: PopulatedFlow): BuilderStore {
return createBuilderStore({
flow,
flowVersion: flow.version,
readonly: false,
hideTestWidget: false,
run: null,
outputSampleData: {},
inputSampleData: {},
socket: io({ autoConnect: false }),
queryClient: new QueryClient(),
});
}
function note(): Note {
return {
id: 'note-1',
content: 'Check the order',
ownerId: null,
color: NoteColorVariant.YELLOW,
position: { x: 0, y: 0 },
size: { width: 200, height: 200 },
createdAt: '2026-01-01T00:00:00.000Z',
updatedAt: '2026-01-01T00:00:00.000Z',
};
}
function flowWithNote(flowNote: Note): PopulatedFlow {
const trigger: EmptyTrigger = {
name: 'trigger',
valid: false,
displayName: 'Select Trigger',
type: FlowTriggerType.EMPTY,
settings: {},
lastUpdatedDate: '2026-01-01T00:00:00.000Z',
};
return {
id: 'flow-1',
created: '2026-01-01T00:00:00.000Z',
updated: '2026-01-01T00:00:00.000Z',
projectId: 'project-1',
externalId: 'flow-1',
ownerId: null,
folderId: null,
status: FlowStatus.DISABLED,
publishedVersionId: null,
metadata: null,
operationStatus: FlowOperationStatus.NONE,
timeSavedPerRun: null,
templateId: null,
createdBy: null,
version: {
id: 'draft-1',
created: '2026-01-01T00:00:00.000Z',
updated: '2026-01-01T00:00:00.000Z',
flowId: 'flow-1',
displayName: 'Order lookup',
trigger,
updatedBy: null,
valid: false,
schemaVersion: null,
agentIds: [],
state: FlowVersionState.DRAFT,
connectionIds: [],
backupFiles: null,
notes: [flowNote],
},
};
}