-
Notifications
You must be signed in to change notification settings - Fork 56.7k
Expand file tree
/
Copy pathcreate-data-table.tool.test.ts
More file actions
220 lines (186 loc) · 6.43 KB
/
create-data-table.tool.test.ts
File metadata and controls
220 lines (186 loc) · 6.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import { DEFAULT_INSTANCE_AI_PERMISSIONS } from '@n8n/api-types';
import type { InstanceAiContext } from '../../../types';
import { createCreateDataTableTool } from '../create-data-table.tool';
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
function createMockContext(overrides?: Partial<InstanceAiContext>): InstanceAiContext {
return {
userId: 'test-user',
workflowService: {
list: jest.fn(),
get: jest.fn(),
getAsWorkflowJSON: jest.fn(),
createFromWorkflowJSON: jest.fn(),
updateFromWorkflowJSON: jest.fn(),
archive: jest.fn(),
delete: jest.fn(),
publish: jest.fn(),
unpublish: jest.fn(),
},
executionService: {
list: jest.fn(),
run: jest.fn(),
getStatus: jest.fn(),
getResult: jest.fn(),
stop: jest.fn(),
getDebugInfo: jest.fn(),
getNodeOutput: jest.fn(),
},
credentialService: {
list: jest.fn(),
get: jest.fn(),
delete: jest.fn(),
test: jest.fn(),
},
nodeService: {
listAvailable: jest.fn(),
getDescription: jest.fn(),
listSearchable: jest.fn(),
},
dataTableService: {
list: jest.fn(),
create: jest.fn(),
delete: jest.fn(),
getSchema: jest.fn(),
addColumn: jest.fn(),
deleteColumn: jest.fn(),
renameColumn: jest.fn(),
queryRows: jest.fn(),
insertRows: jest.fn(),
updateRows: jest.fn(),
deleteRows: jest.fn(),
},
...overrides,
};
}
const validInput = {
name: 'Shopping List',
columns: [{ name: 'item', type: 'string' as const }],
};
const mockTable = {
id: 'dt-123',
name: 'Shopping List',
columns: [{ id: 'col-1', name: 'item', type: 'string' }],
createdAt: '2026-01-01',
updatedAt: '2026-01-01',
};
// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------
describe('createCreateDataTableTool', () => {
let context: InstanceAiContext;
beforeEach(() => {
context = createMockContext();
});
it('has the expected tool id', () => {
const tool = createCreateDataTableTool(context);
expect(tool.id).toBe('create-data-table');
});
describe('when permission requires approval (default)', () => {
it('suspends for user confirmation on first call', async () => {
const tool = createCreateDataTableTool(context);
const suspend = jest.fn();
await tool.execute!(validInput, {
agent: { suspend, resumeData: undefined },
} as never);
expect(suspend).toHaveBeenCalledTimes(1);
const payload = (suspend.mock.calls as unknown[][])[0][0] as Record<string, unknown>;
expect(payload).toEqual(
expect.objectContaining({
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
message: expect.stringContaining('Shopping List'),
severity: 'info',
}),
);
});
it('creates when resumed with approved: true', async () => {
(context.dataTableService.create as jest.Mock).mockResolvedValue(mockTable);
const tool = createCreateDataTableTool(context);
const result = (await tool.execute!(validInput, {
agent: { suspend: jest.fn(), resumeData: { approved: true } },
} as never)) as Record<string, unknown>;
expect(context.dataTableService.create).toHaveBeenCalled();
expect(result.table).toEqual(mockTable);
});
it('returns denied when resumed with approved: false', async () => {
const tool = createCreateDataTableTool(context);
const result = (await tool.execute!(validInput, {
agent: { suspend: jest.fn(), resumeData: { approved: false } },
} as never)) as Record<string, unknown>;
expect(context.dataTableService.create).not.toHaveBeenCalled();
expect(result).toEqual({ denied: true, reason: 'User denied the action' });
});
});
describe('when permission is always_allow', () => {
beforeEach(() => {
context = createMockContext({
permissions: {
...DEFAULT_INSTANCE_AI_PERMISSIONS,
createDataTable: 'always_allow',
},
});
});
it('creates the table without suspending', async () => {
(context.dataTableService.create as jest.Mock).mockResolvedValue(mockTable);
const tool = createCreateDataTableTool(context);
const result = (await tool.execute!(validInput, {
agent: { suspend: jest.fn(), resumeData: undefined },
} as never)) as Record<string, unknown>;
expect(context.dataTableService.create).toHaveBeenCalledWith(
'Shopping List',
[{ name: 'item', type: 'string' }],
{ projectId: undefined },
);
expect(result.table).toEqual(mockTable);
});
it('returns denied when table already exists', async () => {
const conflictError = new Error(
"Data table with name 'Shopping List' already exists in this project",
);
Object.defineProperty(conflictError, 'constructor', {
value: { name: 'DataTableNameConflictError' },
});
// Simulate the cause chain: MastraError wraps the original
const wrappedError = new Error('wrapped');
(wrappedError as Error & { cause: Error }).cause = conflictError;
(context.dataTableService.create as jest.Mock).mockRejectedValue(wrappedError);
const tool = createCreateDataTableTool(context);
const result = (await tool.execute!(validInput, {
agent: { suspend: jest.fn(), resumeData: undefined },
} as never)) as Record<string, unknown>;
expect(result.denied).toBe(true);
expect(result.reason).toContain('already exists');
expect(result.table).toBeUndefined();
});
it('throws non-conflict errors normally', async () => {
(context.dataTableService.create as jest.Mock).mockRejectedValue(
new Error('Database connection failed'),
);
const tool = createCreateDataTableTool(context);
await expect(
tool.execute!(validInput, {
agent: { suspend: jest.fn(), resumeData: undefined },
} as never),
).rejects.toThrow('Database connection failed');
});
});
describe('when permission is blocked', () => {
beforeEach(() => {
context = createMockContext({
permissions: {
...DEFAULT_INSTANCE_AI_PERMISSIONS,
createDataTable: 'blocked',
},
});
});
it('returns denied without calling the service', async () => {
const tool = createCreateDataTableTool(context);
const result = (await tool.execute!(validInput, {
agent: { suspend: jest.fn(), resumeData: undefined },
} as never)) as Record<string, unknown>;
expect(context.dataTableService.create).not.toHaveBeenCalled();
expect(result).toEqual({ denied: true, reason: 'Action blocked by admin' });
});
});
});