Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(editor): Enable pinning main output with error and always allow unpinning #11452

Merged
6 changes: 2 additions & 4 deletions packages/editor-ui/src/components/RunData.vue
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ export default defineComponent({
return false;
}

const canPinNode = usePinnedData(this.node).canPinNode(false);
const canPinNode = usePinnedData(this.node).canPinNode(false, this.currentOutputIndex);

return (
canPinNode &&
Expand Down Expand Up @@ -1213,9 +1213,7 @@ export default defineComponent({
<template>
<div :class="['run-data', $style.container]" @mouseover="activatePane">
<n8n-callout
v-if="
canPinData && pinnedData.hasData.value && !editMode.enabled && !isProductionExecutionPreview
"
v-if="pinnedData.hasData.value && !editMode.enabled && !isProductionExecutionPreview"
theme="secondary"
icon="thumbtack"
:class="$style.pinnedDataCallout"
Expand Down
117 changes: 116 additions & 1 deletion packages/editor-ui/src/composables/usePinnedData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import { setActivePinia, createPinia } from 'pinia';
import { ref } from 'vue';
import { usePinnedData } from '@/composables/usePinnedData';
import type { INodeUi } from '@/Interface';
import { MAX_PINNED_DATA_SIZE } from '@/constants';
import { HTTP_REQUEST_NODE_TYPE, IF_NODE_TYPE, MAX_PINNED_DATA_SIZE } from '@/constants';
import { useWorkflowsStore } from '@/stores/workflows.store';
import { useTelemetry } from '@/composables/useTelemetry';
import { NodeConnectionType, STICKY_NODE_TYPE, type INodeTypeDescription } from 'n8n-workflow';

vi.mock('@/composables/useToast', () => ({ useToast: vi.fn(() => ({ showError: vi.fn() })) }));
vi.mock('@/composables/useI18n', () => ({
Expand All @@ -17,6 +18,13 @@ vi.mock('@/composables/useExternalHooks', () => ({
})),
}));

const getNodeType = vi.fn();
vi.mock('@/stores/nodeTypes.store', () => ({
useNodeTypesStore: vi.fn(() => ({
getNodeType,
})),
}));

describe('usePinnedData', () => {
beforeEach(() => {
setActivePinia(createPinia());
Expand Down Expand Up @@ -133,4 +141,111 @@ describe('usePinnedData', () => {
expect(spy).toHaveBeenCalled();
});
});

describe('canPinData()', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for adding Unit tests. Would it also be useful to add some e2e tests as well for this bug?

Copy link
Contributor Author

@CharlieKolb CharlieKolb Nov 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Especially if we now drop the condition in the vue file or move it to the composable this unit test should now have us fully covered.

I'm a bit unclear still on when to add e2e tests, I understand we're looking to move away from them "by default" and that there is a real cost associated?

Copy link
Contributor

@mutdmour mutdmour Nov 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's no rule. My general rule is if the bug depends on the interaction of multiple modules, then better to have an e2e test for it.

afterEach(() => {
vi.clearAllMocks();
});

it('allows pin on single output', async () => {
const node = ref({
name: 'single output node',
typeVersion: 1,
type: HTTP_REQUEST_NODE_TYPE,

parameters: {},
onError: 'stopWorkflow',
} as INodeUi);
getNodeType.mockReturnValue(makeNodeType([NodeConnectionType.Main], HTTP_REQUEST_NODE_TYPE));

const { canPinNode } = usePinnedData(node);

expect(canPinNode()).toBe(true);
expect(canPinNode(false, 0)).toBe(true);
// validate out of range index
expect(() => canPinNode(false, 1)).toThrow();
expect(() => canPinNode(false, -1)).toThrow();
});

it('allows pin on one main and one error output', async () => {
const node = ref({
name: 'single output node',
typeVersion: 1,
type: HTTP_REQUEST_NODE_TYPE,
parameters: {},
onError: 'continueErrorOutput',
} as INodeUi);
getNodeType.mockReturnValue(makeNodeType([NodeConnectionType.Main], HTTP_REQUEST_NODE_TYPE));

const { canPinNode } = usePinnedData(node);

expect(canPinNode()).toBe(true);
expect(canPinNode(false, 0)).toBe(true);
expect(canPinNode(false, 1)).toBe(false);
// validate out of range index
expect(() => canPinNode(false, 2)).toThrow();
expect(() => canPinNode(false, -1)).toThrow();
});

it('does not allow pin on two main outputs', async () => {
const node = ref({
name: 'single output node',
typeVersion: 1,
type: IF_NODE_TYPE,
parameters: {},
onError: 'stopWorkflow',
} as INodeUi);
getNodeType.mockReturnValue(
makeNodeType([NodeConnectionType.Main, NodeConnectionType.Main], IF_NODE_TYPE),
);

const { canPinNode } = usePinnedData(node);

expect(canPinNode()).toBe(false);
expect(canPinNode(false, 0)).toBe(false);
expect(canPinNode(false, 1)).toBe(false);
// validate out of range index
expect(() => canPinNode(false, 2)).toThrow();
expect(() => canPinNode(false, -1)).toThrow();
});

it('does not allow pin on denylisted node', async () => {
const node = ref({
name: 'single output node',
typeVersion: 1,
type: STICKY_NODE_TYPE,
} as INodeUi);
const { canPinNode } = usePinnedData(node);

expect(canPinNode()).toBe(false);
expect(canPinNode(false, 0)).toBe(false);
});

it('does not allow pin with checkDataEmpty and no pin', async () => {
const node = ref({
name: 'single output node',
typeVersion: 1,
type: HTTP_REQUEST_NODE_TYPE,
} as INodeUi);
getNodeType.mockReturnValue(makeNodeType([NodeConnectionType.Main], HTTP_REQUEST_NODE_TYPE));

const { canPinNode } = usePinnedData(node);

expect(canPinNode(true)).toBe(false);
expect(canPinNode(true, 0)).toBe(false);
});
});
});

const makeNodeType = (outputs: NodeConnectionType[], name: string) =>
({
displayName: name,
name,
version: [1],
inputs: [],
outputs,
properties: [],
defaults: { color: '', name: '' },
group: [],
description: '',
}) as INodeTypeDescription;
28 changes: 20 additions & 8 deletions packages/editor-ui/src/composables/usePinnedData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,24 +75,36 @@ export function usePinnedData(
);
});

function canPinNode(checkDataEmpty = false) {
function canPinNode(checkDataEmpty = false, outputIndex?: number) {
const targetNode = unref(node);
if (targetNode === null) return false;
if (targetNode === null || PIN_DATA_NODE_TYPES_DENYLIST.includes(targetNode.type)) return false;

const nodeType = useNodeTypesStore().getNodeType(targetNode.type, targetNode.typeVersion);
const dataToPin = getInputDataWithPinned(targetNode);

if (!nodeType || (checkDataEmpty && dataToPin.length === 0)) return false;

const workflow = workflowsStore.getCurrentWorkflow();
const outputs = NodeHelpers.getNodeOutputs(workflow, targetNode, nodeType);
const mainOutputs = outputs.filter((output) =>
typeof output === 'string'
? output === NodeConnectionType.Main
: output.type === NodeConnectionType.Main,
const outputs = NodeHelpers.getNodeOutputs(workflow, targetNode, nodeType).map((output) =>
typeof output === 'string' ? { type: output } : output,
);

return mainOutputs.length === 1 && !PIN_DATA_NODE_TYPES_DENYLIST.includes(targetNode.type);
const mainOutputs = outputs.filter(
(output) => output.type === NodeConnectionType.Main && output.category !== 'error',
);

let indexAcceptable = true;

if (outputIndex !== undefined) {
const output = outputs[outputIndex];

if (outputs[outputIndex] === undefined)
throw new Error(`Out of bounds index ${outputIndex} provided to canPinNode`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could brick the UI. So might be better to have a sensible default rather than break UI.

For example adding a node with no outputs.
Screenshot 2024-11-05 at 16 25 16

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh nice catch, I had assumed we always have an output.


indexAcceptable = output.type === NodeConnectionType.Main && output.category !== 'error';
}

return mainOutputs.length === 1 && indexAcceptable;
}

function isValidJSON(data: string): boolean {
Expand Down