-
Notifications
You must be signed in to change notification settings - Fork 8.6k
Expand file tree
/
Copy pathplugin.test.ts
More file actions
187 lines (161 loc) · 6.66 KB
/
Copy pathplugin.test.ts
File metadata and controls
187 lines (161 loc) · 6.66 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
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { coreMock } from '@kbn/core/server/mocks';
import { DEFAULT_SPACE_ID } from '@kbn/core-spaces-common';
import { loggerMock } from '@kbn/logging-mocks';
import type { AlertZeroConfig } from './config';
import { ALERTZERO_API_PRIVILEGE_READ, ALERTZERO_API_PRIVILEGE_WRITE } from '../common/constants';
import { AlertZeroPlugin } from './plugin';
import { initializeManagedWorkflows } from './managed_workflows/initialize_managed_workflows';
import { registerOwner } from './managed_workflows/register_owner';
import { registerRoutes } from './routes/register_routes';
import { ensureAgentSafe, registerAgentType } from './agent';
jest.mock('./managed_workflows/register_owner', () => ({
registerOwner: jest.fn(),
}));
jest.mock('./managed_workflows/initialize_managed_workflows', () => ({
initializeManagedWorkflows: jest.fn().mockResolvedValue(undefined),
}));
jest.mock('./agent', () => ({
agentType: { id: 'mock-alertzero-type', baseConfiguration: {} },
ensureAgentSafe: jest.fn().mockResolvedValue(undefined),
registerAgentType: jest.fn(),
}));
jest.mock('./routes/register_routes', () => ({
registerRoutes: jest.fn(),
}));
const createConfig = (overrides: Partial<AlertZeroConfig> = {}): AlertZeroConfig => ({
enabled: false,
ui: { useMockData: true },
...overrides,
});
const createContext = (config: AlertZeroConfig) => {
const context = {
logger: { get: () => loggerMock.create() },
config: { get: () => config },
} as unknown as ConstructorParameters<typeof AlertZeroPlugin>[0];
return context;
};
describe('AlertZeroPlugin feature-flag gating', () => {
beforeEach(() => {
jest.clearAllMocks();
});
describe('when xpack.alertzero.enabled is false', () => {
it('does not register managed-workflow ownership, features, or HTTP routes', () => {
const plugin = new AlertZeroPlugin(createContext(createConfig({ enabled: false })));
const coreSetup = coreMock.createSetup();
const features = { registerKibanaFeature: jest.fn() };
const workflowsExtensions = { registerManagedWorkflowOwner: jest.fn() };
plugin.setup(
coreSetup as never,
{
features,
workflowsExtensions,
workflowsManagement: undefined,
} as never
);
expect(registerOwner).not.toHaveBeenCalled();
expect(features.registerKibanaFeature).not.toHaveBeenCalled();
expect(registerRoutes).not.toHaveBeenCalled();
expect(coreSetup.http.createRouter).not.toHaveBeenCalled();
expect(registerAgentType).not.toHaveBeenCalled();
});
it('does not install managed worker workflows on start', () => {
const plugin = new AlertZeroPlugin(createContext(createConfig({ enabled: false })));
const coreStart = coreMock.createStart();
plugin.start(coreStart, {
spaces: undefined,
workflowsExtensions: { initManagedWorkflowsClient: jest.fn() },
} as never);
expect(initializeManagedWorkflows).not.toHaveBeenCalled();
expect(ensureAgentSafe).not.toHaveBeenCalled();
});
});
describe('when xpack.alertzero.enabled is true', () => {
it('registers ownership, feature privileges, and routes during setup', () => {
const plugin = new AlertZeroPlugin(createContext(createConfig({ enabled: true })));
const coreSetup = coreMock.createSetup();
const features = { registerKibanaFeature: jest.fn() };
const workflowsExtensions = { registerManagedWorkflowOwner: jest.fn() };
plugin.setup(
coreSetup as never,
{
features,
workflowsExtensions,
workflowsManagement: { management: {} },
agentBuilder: { tools: { register: jest.fn() } },
} as never
);
expect(registerOwner).toHaveBeenCalledWith({ workflowsExtensions });
expect(features.registerKibanaFeature).toHaveBeenCalledWith(
expect.objectContaining({
privileges: expect.objectContaining({
all: expect.objectContaining({
api: expect.arrayContaining([
ALERTZERO_API_PRIVILEGE_READ,
ALERTZERO_API_PRIVILEGE_WRITE,
]),
ui: expect.arrayContaining(['write']),
}),
read: expect.objectContaining({ api: [ALERTZERO_API_PRIVILEGE_READ] }),
}),
})
);
expect(registerRoutes).toHaveBeenCalled();
expect(registerAgentType).toHaveBeenCalled();
});
it('registers the AlertZero thin agent type when Agent Builder is available at setup', () => {
const plugin = new AlertZeroPlugin(createContext(createConfig({ enabled: true })));
const coreSetup = coreMock.createSetup();
const features = { registerKibanaFeature: jest.fn() };
const workflowsExtensions = { registerManagedWorkflowOwner: jest.fn() };
const agentBuilder = { agents: { registerType: jest.fn() }, tools: { register: jest.fn() } };
plugin.setup(
coreSetup as never,
{
features,
workflowsExtensions,
workflowsManagement: { management: {} },
agentBuilder,
} as never
);
expect(registerAgentType).toHaveBeenCalledWith(agentBuilder);
});
it('installs managed worker workflows during start', () => {
const plugin = new AlertZeroPlugin(createContext(createConfig({ enabled: true })));
const coreStart = coreMock.createStart();
const workflowsExtensions = { initManagedWorkflowsClient: jest.fn() };
plugin.start(coreStart, {
spaces: undefined,
workflowsExtensions,
agenticInvestigations: { getProposalsService: jest.fn().mockReturnValue({}) },
} as never);
expect(initializeManagedWorkflows).toHaveBeenCalledWith(
expect.objectContaining({
workflowsExtensions,
})
);
});
it('ensures the thin agent in the default space', () => {
const plugin = new AlertZeroPlugin(createContext(createConfig({ enabled: true })));
const coreStart = coreMock.createStart();
const agentBuilder = { agents: { ensure: jest.fn() } };
plugin.start(coreStart, {
spaces: undefined,
workflowsExtensions: { initManagedWorkflowsClient: jest.fn() },
agentBuilder,
agenticInvestigations: { getProposalsService: jest.fn().mockReturnValue({}) },
} as never);
expect(ensureAgentSafe).toHaveBeenCalledWith(
expect.objectContaining({
agentBuilder,
spaceId: DEFAULT_SPACE_ID,
})
);
});
});
});