mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 12:54:20 +01:00
feat(schedules): support scheduling jobs for the Assistant workspace (#254)
* feat(schedules): support scheduling jobs for the Assistant workspace * refactor(schedules): extract assistant repo constants, rename workspace to schedule-target * feat(navigation): add schedules link to session drawer menu * fix(schedules): document Assistant repoId=0 in generated schedule-management skill
This commit is contained in:
1 parent
9479e1f662
commit
70cb62b77b
18 files changed
+402
-115
No files matched your search
@@ -33,16 +33,18 @@ describe('buildMoreItems', () => {
|
||||
|
||||
it('returns session-specific items for /repos/:id/sessions/:sid', () => {
|
||||
const items = buildMoreItems('/repos/42/sessions/abc')
|
||||
expect(items).toHaveLength(8)
|
||||
expect(items).toHaveLength(9)
|
||||
expect(items[0].key).toBe('files')
|
||||
expect(items[1].key).toBe('mcp')
|
||||
expect(items[2].key).toBe('skills')
|
||||
expect(items[3].key).toBe('lsp')
|
||||
expect(items[3].dialog).toBe('lsp')
|
||||
expect(items[4].key).toBe('reset-permissions')
|
||||
expect(items[5].key).toBe('source-control')
|
||||
expect(items[6].key).toBe('settings')
|
||||
expect(items[7].key).toBe('logout')
|
||||
expect(items[5].key).toBe('schedules')
|
||||
expect(items[5].to).toBe('/repos/42/schedules')
|
||||
expect(items[6].key).toBe('source-control')
|
||||
expect(items[7].key).toBe('settings')
|
||||
expect(items[8].key).toBe('logout')
|
||||
})
|
||||
|
||||
it('returns assistant workspace items for /repos/:id/assistant', () => {
|
||||
|
||||
@@ -75,6 +75,7 @@ export function buildNavModel(pathname: string): NavModel {
|
||||
{ key: 'skills', label: 'Skills', icon: Sparkles, dialog: 'skills' },
|
||||
{ key: 'lsp', label: 'LSP', icon: Code2, dialog: 'lsp' },
|
||||
{ key: 'reset-permissions', label: 'Reset Permissions', icon: ShieldOff, dialog: 'resetPermissions', danger: true },
|
||||
{ key: 'schedules', label: 'Schedules', icon: CalendarClock, to: `/repos/${sessionDetailMatch[1]}/schedules` },
|
||||
{ key: 'source-control', label: 'Source Control', icon: GitCommitHorizontal, dialog: 'sourceControl' },
|
||||
...baseItems,
|
||||
]
|
||||
|
||||
@@ -0,0 +1,148 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
import { render, screen, waitFor } from '@testing-library/react'
|
||||
import userEvent from '@testing-library/user-event'
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
||||
import { ScheduleJobDialog } from './ScheduleJobDialog'
|
||||
|
||||
// jsdom does not implement scrollIntoView
|
||||
Element.prototype.scrollIntoView = vi.fn()
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
templates: [] as Array<{ id: number; title: string; description: string; category: string; cadenceHint: string; suggestedName: string; suggestedDescription: string; prompt: string }>,
|
||||
useDeletePromptTemplateMutate: vi.fn(),
|
||||
}))
|
||||
|
||||
vi.mock('@/hooks/usePromptTemplates', () => ({
|
||||
usePromptTemplates: () => ({ data: mocks.templates, isLoading: false }),
|
||||
useCreatePromptTemplate: () => ({ mutate: vi.fn(), isPending: false }),
|
||||
useUpdatePromptTemplate: () => ({ mutate: vi.fn(), isPending: false }),
|
||||
useDeletePromptTemplate: () => ({ mutate: mocks.useDeletePromptTemplateMutate, isPending: false }),
|
||||
}))
|
||||
|
||||
vi.mock('@/api/providers', () => ({
|
||||
getProvidersWithModels: () => Promise.resolve([]),
|
||||
}))
|
||||
|
||||
vi.mock('@/api/opencode', () => ({
|
||||
createOpenCodeClient: () => ({
|
||||
listAgents: () => Promise.resolve([]),
|
||||
getConfig: () => Promise.resolve(null),
|
||||
}),
|
||||
}))
|
||||
|
||||
vi.mock('@/api/settings', () => ({
|
||||
settingsApi: {
|
||||
listManagedSkills: () => Promise.resolve([]),
|
||||
},
|
||||
}))
|
||||
|
||||
vi.mock('@/api/repos', () => ({
|
||||
listRepos: () => Promise.resolve([]),
|
||||
}))
|
||||
|
||||
function createWrapper() {
|
||||
const queryClient = new QueryClient({
|
||||
defaultOptions: {
|
||||
queries: { retry: false },
|
||||
mutations: { retry: false },
|
||||
},
|
||||
})
|
||||
return ({ children }: { children: React.ReactNode }) => (
|
||||
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
|
||||
)
|
||||
}
|
||||
|
||||
describe('ScheduleJobDialog — assistant create guard', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
it('renders Assistant as the first repository option', async () => {
|
||||
const onRepoChange = vi.fn()
|
||||
const onSubmit = vi.fn()
|
||||
const onOpenChange = vi.fn()
|
||||
const user = userEvent.setup()
|
||||
|
||||
render(
|
||||
<ScheduleJobDialog
|
||||
open
|
||||
onOpenChange={onOpenChange}
|
||||
showRepoSelector
|
||||
repoId={undefined}
|
||||
onRepoChange={onRepoChange}
|
||||
onSubmit={onSubmit}
|
||||
isSaving={false}
|
||||
/>,
|
||||
{ wrapper: createWrapper() },
|
||||
)
|
||||
|
||||
// Open the repo combobox by clicking the chevron/input
|
||||
const repoInput = screen.getByPlaceholderText('Select a repository')
|
||||
await user.click(repoInput)
|
||||
|
||||
// The dropdown should open and show "Assistant" as an option
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Assistant')).toBeInTheDocument()
|
||||
})
|
||||
// Assistant description should also be visible
|
||||
expect(screen.getByText('Built-in assistant')).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('disables submit when no repo is selected, enables when Assistant repo is selected', async () => {
|
||||
const onRepoChange = vi.fn()
|
||||
const onSubmit = vi.fn()
|
||||
const user = userEvent.setup()
|
||||
|
||||
const { rerender } = render(
|
||||
<ScheduleJobDialog
|
||||
open
|
||||
onOpenChange={vi.fn()}
|
||||
showRepoSelector
|
||||
repoId={undefined}
|
||||
onRepoChange={onRepoChange}
|
||||
onSubmit={onSubmit}
|
||||
isSaving={false}
|
||||
/>,
|
||||
{ wrapper: createWrapper() },
|
||||
)
|
||||
|
||||
// Wait for queries to settle
|
||||
await waitFor(() => {
|
||||
expect(screen.getByRole('button', { name: /Create schedule/i })).toBeDisabled()
|
||||
})
|
||||
|
||||
// Fill in required name field
|
||||
const nameInput = screen.getByLabelText('Name')
|
||||
await user.type(nameInput, 'Test Assistant Job')
|
||||
|
||||
// Switch to Prompt tab to fill prompt
|
||||
const promptTab = screen.getByRole('tab', { name: 'Prompt' })
|
||||
await user.click(promptTab)
|
||||
|
||||
// Fill in required prompt field
|
||||
const promptInput = screen.getByRole('textbox', { name: 'Prompt' })
|
||||
await user.type(promptInput, 'Run a test analysis')
|
||||
|
||||
// Submit button should still be disabled (repoId is undefined)
|
||||
const submitButton = screen.getByRole('button', { name: /Create schedule/i })
|
||||
expect(submitButton).toBeDisabled()
|
||||
|
||||
// Re-render with repoId={0} (Assistant selected) — name and prompt state persists
|
||||
rerender(
|
||||
<ScheduleJobDialog
|
||||
open
|
||||
onOpenChange={vi.fn()}
|
||||
showRepoSelector
|
||||
repoId={0}
|
||||
onRepoChange={onRepoChange}
|
||||
onSubmit={onSubmit}
|
||||
isSaving={false}
|
||||
/>,
|
||||
)
|
||||
|
||||
// Submit button should now be enabled
|
||||
await waitFor(() => {
|
||||
expect(screen.getByRole('button', { name: /Create schedule/i })).not.toBeDisabled()
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -18,6 +18,7 @@ import {
|
||||
type SchedulePreset,
|
||||
} from '@/components/schedules/schedule-utils'
|
||||
import { getRepoDisplayName } from '@/lib/utils'
|
||||
import { ASSISTANT_REPO_ID, ASSISTANT_REPO_NAME } from '@opencode-manager/shared/utils'
|
||||
import { Loader2 } from 'lucide-react'
|
||||
import { usePromptTemplates, useDeletePromptTemplate } from '@/hooks/usePromptTemplates'
|
||||
import { PromptTemplateDialog } from './PromptTemplateDialog'
|
||||
@@ -107,16 +108,21 @@ export function ScheduleJobDialog({ open, onOpenChange, job, isSaving, onSubmit,
|
||||
staleTime: 5 * 60 * 1000,
|
||||
})
|
||||
|
||||
const repoOptions = useMemo<ComboboxOption[]>(() =>
|
||||
repos
|
||||
const repoOptions = useMemo<ComboboxOption[]>(() => {
|
||||
const assistantOption: ComboboxOption = {
|
||||
value: ASSISTANT_REPO_ID.toString(),
|
||||
label: ASSISTANT_REPO_NAME,
|
||||
description: 'Built-in assistant',
|
||||
}
|
||||
const repoEntries = repos
|
||||
.filter((repo) => repo.cloneStatus === 'ready')
|
||||
.map((repo) => ({
|
||||
value: repo.id.toString(),
|
||||
label: getRepoDisplayName(repo.repoUrl, repo.localPath, repo.sourcePath),
|
||||
description: repo.localPath,
|
||||
})),
|
||||
[repos]
|
||||
)
|
||||
}))
|
||||
return [assistantOption, ...repoEntries]
|
||||
}, [repos])
|
||||
|
||||
const modelOptions = useMemo<ComboboxOption[]>(() => {
|
||||
const configuredModels: ComboboxOption[] = []
|
||||
@@ -335,7 +341,7 @@ export function ScheduleJobDialog({ open, onOpenChange, job, isSaving, onSubmit,
|
||||
|
||||
<div className="mt-0 shrink-0 border-t border-border px-3 sm:px-6 py-4 flex flex-row gap-2 sm:justify-end">
|
||||
<Button variant="outline" onClick={() => onOpenChange(false)} disabled={isSaving} className="flex-1 sm:flex-none">Cancel</Button>
|
||||
<Button onClick={handleSubmit} disabled={isSaving || !name.trim() || !prompt.trim() || isScheduleConfigInvalid || (!!showRepoSelector && !job && !selectedRepoId)} className="flex-1 sm:flex-none">
|
||||
<Button onClick={handleSubmit} disabled={isSaving || !name.trim() || !prompt.trim() || isScheduleConfigInvalid || (!!showRepoSelector && !job && selectedRepoId === undefined)} className="flex-1 sm:flex-none">
|
||||
{isSaving ? <Loader2 className="mr-2 h-4 w-4 animate-spin" /> : null}
|
||||
{isSaving ? 'Saving...' : job ? 'Save changes' : 'Create schedule'}
|
||||
</Button>
|
||||
|
||||
@@ -35,6 +35,10 @@ vi.mock('@/components/settings/NotificationSettings', () => ({
|
||||
NotificationSettings: () => <div data-testid="notification-settings">Notification Settings Content</div>,
|
||||
}))
|
||||
|
||||
vi.mock('@/components/settings/VersionSelectDialog', () => ({
|
||||
VersionSelectDialog: () => <div data-testid="version-select-dialog">Version Select Dialog</div>,
|
||||
}))
|
||||
|
||||
vi.mock('@/hooks/useMobile', () => ({
|
||||
useSwipeBack: vi.fn(() => ({
|
||||
bind: vi.fn(),
|
||||
|
||||
Reference in new issue
Block a user