Skip to content

Commit 01adcdf

Browse files
test: ai user action tests
CMK-33107 Change-Id: I40194ec114320a468cc5bf9babf72d43d249ce5e
1 parent c1143e8 commit 01adcdf

1 file changed

Lines changed: 98 additions & 0 deletions

File tree

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/**
2+
* Copyright (C) 2026 Checkmk GmbH - License: GNU General Public License v2
3+
* This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
4+
* conditions defined in the file COPYING, which is part of this source code package.
5+
*/
6+
import { afterEach, describe, expect, test, vi } from 'vitest'
7+
8+
import type { AiActionButton } from '@/ai/lib/service/ai-template'
9+
import { loadUserActions } from '@/ai/lib/user-actions'
10+
11+
function makeAction(overrides: Partial<AiActionButton> = {}): AiActionButton {
12+
return { action_id: 'explain', action_name: 'Explain', ...overrides }
13+
}
14+
15+
function makeTemplate(actions: AiActionButton[] | Error) {
16+
return {
17+
getUserActionButtons: vi.fn().mockResolvedValue(actions),
18+
execUserActionButton: vi.fn()
19+
}
20+
}
21+
22+
afterEach(() => {
23+
vi.clearAllMocks()
24+
})
25+
26+
describe('loadUserActions', () => {
27+
test('returns the error when getUserActionButtons resolves to an Error', async () => {
28+
const error = new Error('network failure')
29+
const template = makeTemplate(error)
30+
31+
const result = await loadUserActions(template as never)
32+
33+
expect(result).toBe(error)
34+
})
35+
36+
test('returns an empty array when there are no actions', async () => {
37+
const template = makeTemplate([])
38+
39+
const result = await loadUserActions(template as never)
40+
41+
expect(result).toEqual([])
42+
})
43+
44+
test('returns all actions when multiple actions are present', async () => {
45+
const actions = [makeAction({ action_id: 'a' }), makeAction({ action_id: 'b' })]
46+
const template = makeTemplate(actions)
47+
48+
const result = await loadUserActions(template as never)
49+
50+
expect(result).toEqual(actions)
51+
})
52+
53+
test('auto-executes the action when there is exactly one explain_this_service action', async () => {
54+
const action = makeAction({ action_id: 'explain_this_service' })
55+
const template = makeTemplate([action])
56+
57+
await loadUserActions(template as never)
58+
59+
expect(template.execUserActionButton).toHaveBeenCalledOnce()
60+
expect(template.execUserActionButton).toHaveBeenCalledWith(action)
61+
})
62+
63+
test('does NOT auto-execute when there are multiple actions', async () => {
64+
const actions = [
65+
makeAction({ action_id: 'explain_this_service' }),
66+
makeAction({ action_id: 'other' })
67+
]
68+
const template = makeTemplate(actions)
69+
70+
await loadUserActions(template as never)
71+
72+
expect(template.execUserActionButton).not.toHaveBeenCalled()
73+
})
74+
75+
test('does NOT auto-execute when the single action has a different id', async () => {
76+
const action = makeAction({ action_id: 'something_else' })
77+
const template = makeTemplate([action])
78+
79+
await loadUserActions(template as never)
80+
81+
expect(template.execUserActionButton).not.toHaveBeenCalled()
82+
})
83+
84+
test('does NOT auto-execute when autoExecuteSingleAction is false', async () => {
85+
const action = makeAction({ action_id: 'explain_this_service' })
86+
const template = makeTemplate([action])
87+
88+
await loadUserActions(template as never, { autoExecuteSingleAction: false })
89+
90+
expect(template.execUserActionButton).not.toHaveBeenCalled()
91+
})
92+
93+
test('returns empty array and does not throw when template is null', async () => {
94+
const result = await loadUserActions(null)
95+
96+
expect(result).toEqual([])
97+
})
98+
})

0 commit comments

Comments
 (0)