fix(editor): Fix failing non-null assertion in useDataSchema:getNodeInputData (no-changelog) (#11807)

This commit is contained in:
Charlie Kolb 2024-11-20 15:30:12 +01:00 committed by GitHub
parent bcb9a20781
commit 187edf28aa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 131 additions and 5 deletions

View file

@ -1,6 +1,16 @@
import jp from 'jsonpath';
import { useDataSchema } from '@/composables/useDataSchema';
import type { Schema } from '@/Interface';
import type { IExecutionResponse, INodeUi, Schema } from '@/Interface';
import { setActivePinia } from 'pinia';
import { createTestingPinia } from '@pinia/testing';
import {
NodeConnectionType,
type INodeExecutionData,
type ITaskDataConnections,
} from 'n8n-workflow';
import { useWorkflowsStore } from '@/stores/workflows.store';
vi.mock('@/stores/workflows.store');
describe('useDataSchema', () => {
const getSchema = useDataSchema().getSchema;
@ -524,4 +534,118 @@ describe('useDataSchema', () => {
expect(filterSchema(flatSchema, '')).toEqual(flatSchema);
});
});
describe('getNodeInputData', () => {
const getNodeInputData = useDataSchema().getNodeInputData;
beforeEach(() => {
setActivePinia(createTestingPinia());
});
afterEach(() => {
vi.clearAllMocks();
});
const name = 'a';
const makeMockData = (data: ITaskDataConnections | undefined, runDataKey?: string) => ({
data: {
resultData: {
runData: {
[runDataKey ?? name]: [{ data, startTime: 0, executionTime: 0, source: [] }],
},
},
},
});
const mockExecutionDataMarker = Symbol() as unknown as INodeExecutionData[];
const Main = NodeConnectionType.Main;
test.each<
[
[Partial<INodeUi> | null, number, number, Partial<IExecutionResponse> | null],
ReturnType<typeof getNodeInputData>,
]
>([
//
// Null / Out of Bounds Cases
//
[[null, 0, 0, null], []],
[[{ name }, 0, 0, null], []],
[[{ name }, 0, 0, { data: undefined }], []],
[[{ name }, 0, 0, { data: { resultData: { runData: {} } } }], []],
[[{ name }, 0, 0, { data: { resultData: { runData: { [name]: [] } } } }], []],
[[{ name }, 0, 0, makeMockData(undefined)], []],
[[{ name }, 1, 0, makeMockData({})], []],
[[{ name }, -1, 0, makeMockData({})], []],
[[{ name }, 0, 0, makeMockData({}, 'DIFFERENT_NAME')], []],
// getMainInputData cases
[[{ name }, 0, 0, makeMockData({ [Main]: [] })], []],
[[{ name }, 0, 0, makeMockData({ [Main]: [null] })], []],
[[{ name }, 0, 1, makeMockData({ [Main]: [null] })], []],
[[{ name }, 0, -1, makeMockData({ [Main]: [null] })], []],
[
[{ name }, 0, 0, makeMockData({ [Main]: [mockExecutionDataMarker] })],
mockExecutionDataMarker,
],
[
[{ name }, 0, 0, makeMockData({ [Main]: [mockExecutionDataMarker, null] })],
mockExecutionDataMarker,
],
[
[{ name }, 0, 1, makeMockData({ [Main]: [null, mockExecutionDataMarker] })],
mockExecutionDataMarker,
],
[
[
{ name },
0,
1,
makeMockData({ DIFFERENT_NAME: [], [Main]: [null, mockExecutionDataMarker] }),
],
mockExecutionDataMarker,
],
[
[
{ name },
2,
1,
{
data: {
resultData: {
runData: {
[name]: [
{
startTime: 0,
executionTime: 0,
source: [],
},
{
startTime: 0,
executionTime: 0,
source: [],
},
{
data: { [Main]: [null, mockExecutionDataMarker] },
startTime: 0,
executionTime: 0,
source: [],
},
],
},
},
},
},
],
mockExecutionDataMarker,
],
])(
'should return correct output %s',
([node, runIndex, outputIndex, getWorkflowExecution], output) => {
vi.mocked(useWorkflowsStore).mockReturnValue({
...useWorkflowsStore(),
getWorkflowExecution: getWorkflowExecution as IExecutionResponse,
});
expect(getNodeInputData(node as INodeUi, runIndex, outputIndex)).toEqual(output);
},
);
});
});

View file

@ -72,12 +72,13 @@ export function useDataSchema() {
if (
!connectionsData?.hasOwnProperty(NodeConnectionType.Main) ||
connectionsData.main === undefined ||
connectionsData.main.length < outputIndex ||
outputIndex < 0 ||
outputIndex >= connectionsData.main.length ||
connectionsData.main[outputIndex] === null
) {
return [];
}
return connectionsData.main[outputIndex] as INodeExecutionData[];
return connectionsData.main[outputIndex];
}
function getNodeInputData(
@ -100,11 +101,12 @@ export function useDataSchema() {
}
const runData = executionData.resultData.runData;
if (!runData?.[node.name]?.[runIndex].data || runData[node.name][runIndex].data === undefined) {
const taskData = runData?.[node.name]?.[runIndex];
if (taskData?.data === undefined) {
return [];
}
return getMainInputData(runData[node.name][runIndex].data!, outputIndex);
return getMainInputData(taskData.data, outputIndex);
}
function getInputDataWithPinned(