import { act } from '@testing-library/react' import { describe, expect, it } from 'vitest' import { useSetSecret } from './useSecrets' import type { SecretScope } from '@/api/secrets' import { renderHookWithProviders } from '@/test/render' const repoScope: SecretScope = { scope: 'repo', repoId: 7 } const putRoute = '/api/repos/7/:kind/:name' function renderSetSecret() { return renderHookWithProviders(() => useSetSecret(repoScope), { auth: false, theme: false, fetchRoutes: [{ method: 'PUT', path: putRoute, respond: () => ({ status: 204 }) }], }) } describe('useSetSecret', () => { it('omits agentHidden when the caller does not provide it', async () => { const { result, fetchFake } = renderSetSecret() await act(async () => { await result.current.mutateAsync({ kind: 'secret', name: 'FOO', value: 'x' }) }) expect(fetchFake.lastRequest('PUT', putRoute)?.body).toEqual({ value: 'x' }) }) it('sends agentHidden false when the caller provides false', async () => { const { result, fetchFake } = renderSetSecret() await act(async () => { await result.current.mutateAsync({ kind: 'secret', name: 'FOO', value: 'x', agentHidden: false }) }) expect(fetchFake.lastRequest('PUT', putRoute)?.body).toEqual({ value: 'x', agentHidden: false }) }) it('sends agentHidden and the allowed hosts for a hidden secret', async () => { const { result, fetchFake } = renderSetSecret() await act(async () => { await result.current.mutateAsync({ kind: 'secret', name: 'FOO', value: 'x', agentHidden: true, allowedHosts: ['api.example.com'], }) }) expect(fetchFake.lastRequest('PUT', putRoute)?.body).toEqual({ value: 'x', agentHidden: true, allowedHosts: ['api.example.com'], }) }) it('omits agentHidden and the allowed hosts for a variable', async () => { const { result, fetchFake } = renderSetSecret() await act(async () => { await result.current.mutateAsync({ kind: 'variable', name: 'FOO', value: 'x', agentHidden: true, allowedHosts: ['api.example.com'], }) }) expect(fetchFake.lastRequest('PUT', putRoute)?.body).toEqual({ value: 'x' }) }) })