|
| 1 | +/** @vitest-environment happy-dom */ |
| 2 | + |
| 3 | +import { OmnaraClientProvider } from '@omnara/react' |
| 4 | +import { createOmnaraClient, type Skill } from '@omnara/sdk' |
| 5 | +import { listProjectAvailableSkillsInfiniteQueryKey } from '@omnara/sdk/tanstack' |
| 6 | +import { QueryClient, QueryClientProvider } from '@tanstack/react-query' |
| 7 | +import { act } from 'react' |
| 8 | +import { createRoot, type Root } from 'react-dom/client' |
| 9 | +import { afterEach, beforeEach, expect, it, vi } from 'vitest' |
| 10 | + |
| 11 | +import { AgentConfigSkillSearchbox } from '@/components/agents/AgentConfigSkillSearchbox' |
| 12 | +import { fakeId } from '@/test/fixtures' |
| 13 | +import { enableReactActEnvironment } from '@/test/react-act' |
| 14 | + |
| 15 | +const orgId = fakeId('org') |
| 16 | +const projectId = fakeId('proj') |
| 17 | + |
| 18 | +function skill(name: string, id: string): Skill { |
| 19 | + return { |
| 20 | + id, |
| 21 | + org_id: orgId, |
| 22 | + owner: { kind: 'project', project_id: projectId }, |
| 23 | + name, |
| 24 | + revision_id: fakeId('skr'), |
| 25 | + revision: 1, |
| 26 | + description: `Does ${name} things.`, |
| 27 | + created_at: '2026-09-28T00:00:00Z', |
| 28 | + updated_at: '2026-09-28T00:00:00Z', |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +const skills = [ |
| 33 | + skill('attached', `skl_${'a'.repeat(26)}`), |
| 34 | + skill('pdf-tools', `skl_${'b'.repeat(26)}`), |
| 35 | + skill('slides', `skl_${'c'.repeat(26)}`), |
| 36 | +] |
| 37 | + |
| 38 | +let root: Root |
| 39 | +let container: HTMLDivElement |
| 40 | +let restore: () => void |
| 41 | +beforeEach(() => { |
| 42 | + restore = enableReactActEnvironment() |
| 43 | + container = document.createElement('div') |
| 44 | + document.body.append(container) |
| 45 | + root = createRoot(container) |
| 46 | +}) |
| 47 | +afterEach(() => { |
| 48 | + act(() => { |
| 49 | + root.unmount() |
| 50 | + }) |
| 51 | + container.remove() |
| 52 | + restore() |
| 53 | +}) |
| 54 | + |
| 55 | +async function render(canCreate = true, expanded = true) { |
| 56 | + const client = createOmnaraClient({ baseUrl: 'https://omnara.test/api/v1' }) |
| 57 | + const queryClient = new QueryClient({ |
| 58 | + defaultOptions: { queries: { retry: false, staleTime: Infinity } }, |
| 59 | + }) |
| 60 | + const path = { orgID: orgId, projectID: projectId } |
| 61 | + queryClient.setQueryData( |
| 62 | + listProjectAvailableSkillsInfiniteQueryKey({ |
| 63 | + path, |
| 64 | + query: { sort: 'name', limit: 25 }, |
| 65 | + client, |
| 66 | + }), |
| 67 | + { |
| 68 | + pages: [ |
| 69 | + { |
| 70 | + data: skills.map((item) => ({ |
| 71 | + skill: item, |
| 72 | + project_id: projectId, |
| 73 | + availability: { source: 'direct' }, |
| 74 | + })), |
| 75 | + next_cursor: null, |
| 76 | + }, |
| 77 | + ], |
| 78 | + pageParams: [{ path }], |
| 79 | + }, |
| 80 | + ) |
| 81 | + const onSelect = vi.fn() |
| 82 | + const onCreateSkill = vi.fn() |
| 83 | + await act(async () => { |
| 84 | + root.render( |
| 85 | + <OmnaraClientProvider client={client}> |
| 86 | + <QueryClientProvider client={queryClient}> |
| 87 | + <AgentConfigSkillSearchbox |
| 88 | + orgId={orgId} |
| 89 | + projectId={projectId} |
| 90 | + excludedIds={new Set([`skl_${'a'.repeat(26)}`])} |
| 91 | + excludedNames={new Set()} |
| 92 | + active |
| 93 | + expanded={expanded} |
| 94 | + onSelect={onSelect} |
| 95 | + onCreateSkill={canCreate ? onCreateSkill : undefined} |
| 96 | + /> |
| 97 | + </QueryClientProvider> |
| 98 | + </OmnaraClientProvider>, |
| 99 | + ) |
| 100 | + await Promise.resolve() |
| 101 | + }) |
| 102 | + return { onSelect, onCreateSkill } |
| 103 | +} |
| 104 | + |
| 105 | +function options() { |
| 106 | + return [...document.querySelectorAll('[role="option"]')] |
| 107 | +} |
| 108 | + |
| 109 | +function optionNames() { |
| 110 | + return options().map((option) => option.querySelector('.font-medium')?.textContent) |
| 111 | +} |
| 112 | + |
| 113 | +async function type(value: string) { |
| 114 | + await act(async () => { |
| 115 | + const input = document.querySelector('input') |
| 116 | + const descriptor = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value') |
| 117 | + descriptor?.set?.call(input, value) |
| 118 | + input?.dispatchEvent(new InputEvent('input', { bubbles: true, inputType: 'insertText' })) |
| 119 | + await Promise.resolve() |
| 120 | + }) |
| 121 | +} |
| 122 | + |
| 123 | +it('opens focused with Create skill ahead of the unattached skills', async () => { |
| 124 | + await render() |
| 125 | + expect(document.activeElement).toBe(document.querySelector('input[aria-label="Search skills…"]')) |
| 126 | + expect(optionNames()).toEqual(['Create skill', 'pdf-tools', 'slides']) |
| 127 | +}) |
| 128 | + |
| 129 | +it('moves Create skill below matching skills so Enter attaches the match', async () => { |
| 130 | + const ctx = await render() |
| 131 | + await type('sli') |
| 132 | + expect(optionNames()).toEqual(['slides', 'Create skill']) |
| 133 | + expect(options()[0]?.hasAttribute('data-highlighted')).toBe(true) |
| 134 | + await act(async () => { |
| 135 | + document |
| 136 | + .querySelector('input') |
| 137 | + ?.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter', bubbles: true })) |
| 138 | + await Promise.resolve() |
| 139 | + }) |
| 140 | + expect(ctx.onSelect).toHaveBeenCalledWith(skills[2]) |
| 141 | + expect(ctx.onCreateSkill).not.toHaveBeenCalled() |
| 142 | +}) |
| 143 | + |
| 144 | +it('keeps Create skill first when nothing matches and opens the create flow', async () => { |
| 145 | + const ctx = await render() |
| 146 | + await type('zzz') |
| 147 | + expect(optionNames()).toEqual(['Create skill']) |
| 148 | + act(() => { |
| 149 | + options()[0]?.dispatchEvent(new MouseEvent('click', { bubbles: true })) |
| 150 | + }) |
| 151 | + expect(ctx.onCreateSkill).toHaveBeenCalledOnce() |
| 152 | + expect(ctx.onSelect).not.toHaveBeenCalled() |
| 153 | +}) |
| 154 | + |
| 155 | +it('selects a skill from the list', async () => { |
| 156 | + const ctx = await render() |
| 157 | + act(() => { |
| 158 | + options()[2]?.dispatchEvent(new MouseEvent('click', { bubbles: true })) |
| 159 | + }) |
| 160 | + expect(ctx.onSelect).toHaveBeenCalledWith(skills[2]) |
| 161 | +}) |
| 162 | + |
| 163 | +it('keeps Enter inside the search box when nothing is highlighted', async () => { |
| 164 | + const ctx = await render(false) |
| 165 | + await type('zzz') |
| 166 | + expect(options()).toEqual([]) |
| 167 | + const enter = new KeyboardEvent('keydown', { key: 'Enter', bubbles: true, cancelable: true }) |
| 168 | + act(() => { |
| 169 | + document.querySelector('input')?.dispatchEvent(enter) |
| 170 | + }) |
| 171 | + expect(enter.defaultPrevented).toBe(true) |
| 172 | + expect(ctx.onSelect).not.toHaveBeenCalled() |
| 173 | +}) |
| 174 | + |
| 175 | +it('omits Create skill without create permission', async () => { |
| 176 | + await render(false) |
| 177 | + expect(optionNames()).toEqual(['pdf-tools', 'slides']) |
| 178 | +}) |
| 179 | + |
| 180 | +it('keeps the list closed until the search box has expanded', async () => { |
| 181 | + await render(true, false) |
| 182 | + expect(document.activeElement).toBe(document.querySelector('input[aria-label="Search skills…"]')) |
| 183 | + expect(options()).toEqual([]) |
| 184 | +}) |
0 commit comments