fix(hooks): keep account bindings when editing from a target tab

TargetHooks opened HookDialog without the account map, so the dialog's
picker only knew built-in Agents and saving dropped bindings such as
codex-2 from the source. The next sync would then remove that account's
hook without warning.
This commit is contained in:
Willie
2026-10-01 21:24:22 +08:00
parent bdd1fcf286
commit aa1a5ddde1
2 changed files with 16 additions and 2 deletions
+14 -1
View File
@@ -10,7 +10,7 @@ import { hookAgents } from '../../api/hooks';
import { ToastProvider } from '../Toast';
import TargetHooks from './TargetHooks';
vi.mock('../../api/hooks', async (load) => ({ ...await load<typeof import('../../api/hooks')>(), hooksApi: { syncProject: vi.fn().mockResolvedValue({ applied: [], backupIds: [] }), configure: vi.fn().mockResolvedValue({ applied: [], backupIds: [] }), preview: vi.fn(), save: vi.fn() } }));
vi.mock('../../api/hooks', async (load) => ({ ...await load<typeof import('../../api/hooks')>(), hooksApi: { syncProject: vi.fn().mockResolvedValue({ applied: [], backupIds: [] }), configure: vi.fn().mockResolvedValue({ applied: [], backupIds: [] }), preview: vi.fn(), save: vi.fn(), catalog: vi.fn().mockResolvedValue({}) } }));
const codex = { bindings: { codex: { events: { Stop: [{ hooks: [{ type: 'command', command: 'true' }] }] } } } };
const data = {
@@ -71,6 +71,19 @@ describe('Target hooks tab', () => {
});
});
describe('Target hooks editing', () => {
it("keeps the hook's account bindings when it is saved from an Agent's tab", async () => {
vi.mocked(hooksApi.save).mockResolvedValue({ applied: [], backupIds: [] });
const both = { bindings: { codex: codex.bindings.codex, 'codex-2': codex.bindings.codex } };
const inventory = { ...data, source: { ...data.source, entries: { 'global-lint': both } }, targets: [{ name: 'codex', kind: 'command' }, { name: 'codex-2', agent: 'codex', kind: 'command' }] } as unknown as HookInventory;
const user = userEvent.setup();
render(<MemoryRouter><QueryClientProvider client={new QueryClient()}><I18nProvider><ToastProvider><TargetHooks agent="codex" data={inventory} /></ToastProvider></I18nProvider></QueryClientProvider></MemoryRouter>);
await user.click(screen.getByRole('button', { name: 'Edit global-lint' }));
await user.click(await screen.findByRole('button', { name: 'Save' }));
await waitFor(() => expect(hooksApi.save).toHaveBeenCalledWith({ name: 'global-lint', entry: both }));
});
});
describe('Target hooks conflicts', () => {
const blocked = (changes: object[]) => ({ ...data, plan: { revision: 'r1', fingerprint: 'fp', sourcePath: '', blocked: true, changes } }) as unknown as HookInventory;
const show = (inventory: HookInventory) => render(
+2 -1
View File
@@ -8,7 +8,7 @@ import EmptyState from '../EmptyState';
import { useToast } from '../Toast';
import HookDialog from '../hooks/HookDialog';
import { HooksSyncDialog } from '../hooks/HooksSyncBox';
import { blockedHint, boundAgents, hookLabel, hookMessage, hookNote, isCodeAgent, scopeEntries, scopePlan, syncState, writes } from '../hooks/hooksView';
import { blockedHint, boundAgents, hookAccounts, hookLabel, hookMessage, hookNote, isCodeAgent, scopeEntries, scopePlan, syncState, writes } from '../hooks/hooksView';
import { useT } from '../../i18n';
import { queryKeys } from '../../lib/queryKeys';
@@ -98,6 +98,7 @@ export default function TargetHooks({ agent, data, project }: { agent: string; d
{syncing && <HooksSyncDialog project={project} onClose={() => setSyncing(false)} />}
{editing && (
<HookDialog
accounts={hookAccounts(data.targets)}
initial={{ name: editing, entry: entries[editing] }}
existingNames={Object.keys(entries)}
project={project}