Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,37 @@ describe('AutomationDetailShell — a deleted automation', () => {
);
});

// Deleted from the list, an automation stays remembered as the rail's
// place; reopening it there must land on the list, not on its banner.
it('drops the stale memory and redirects to the list on a restored arrival', () => {
fixtures.pathname = '/dashboard/org-1/automations/billing__dunning/editor';
fixtures.state = { navRestore: true };
window.localStorage.setItem(
'tale.platform.automations.org-1.lastPath',
JSON.stringify(fixtures.pathname),
);
renderShell();

expect(mockNavigate).toHaveBeenCalledExactlyOnceWith({
to: '/dashboard/$id/automations',
params: { id: 'org-1' },
replace: true,
});
expect(
window.localStorage.getItem('tale.platform.automations.org-1.lastPath'),
).toBeNull();
});

it('keeps explaining the deletion to a deliberate arrival', () => {
// A shared link or a run link opens the deleted automation on purpose.
fixtures.pathname =
'/dashboard/org-1/automations/billing__dunning/runs/run-1';
renderShell();

expect(mockNavigate).not.toHaveBeenCalled();
expect(screen.getByRole('alert')).toBeVisible();
});

it('keeps plain not-found for a name that was never saved', () => {
fixtures.error = { data: { code: 'automation not found' } };
fixtures.pathname =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,17 +129,19 @@ function AutomationDetailFrame({

// A remembered automation can be deleted between visits. When the rail
// RESTORED us here, forget the stale place and fall back to the list
// rather than leaving the user on a not-found they never asked for.
// rather than leaving the user on a not-found, or on a deleted
// automation's banner, they never asked for.
const wasRestored = state.navRestore === true;
const isGone = isMissing || deletedAt !== undefined;
useEffect(() => {
if (!isMissing || !wasRestored || projectId !== undefined) return;
if (!isGone || !wasRestored || projectId !== undefined) return;
clearAutomationMemory(organizationId);
void navigate({
to: '/dashboard/$id/automations',
params: { id: organizationId },
replace: true,
});
}, [isMissing, wasRestored, projectId, organizationId, navigate]);
}, [isGone, wasRestored, projectId, organizationId, navigate]);

const tabs = useMemo<TabNavigationItem[]>(() => {
const deleted = deletedAt !== undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2273,12 +2273,6 @@ function ChatSurfaceInner({
organizationId={organizationId}
open={deleteOpen}
onOpenChange={setDeleteOpen}
onDeleted={() =>
void navigate({
to: '/dashboard/$id/chat',
params: { id: organizationId },
})
}
/>
)}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// @vitest-environment jsdom
import '@testing-library/jest-dom/vitest';
import { beforeEach, describe, expect, it, vi } from 'vitest';

import { render, screen, waitFor } from '@/tests/utils/render';

import { ThreadDeleteDialog } from './thread-delete-dialog';

// Where a delete lands: the conversation on screen is left for a fresh chat,
// never for plain `/chat` — that resumes the most recent chat, and the list
// still names the deleted one until it has read again, so the reader came
// straight back to "This chat is not available".

const { mockNavigate, mockTrash, mockToast, routeParams } = vi.hoisted(() => ({
mockNavigate: vi.fn(),
mockTrash: vi.fn<(threadId: string) => Promise<boolean>>(),
mockToast: vi.fn(),
routeParams: { current: {} as Record<string, string> },
}));

vi.mock('@tanstack/react-router', async (importOriginal) => ({
...(await importOriginal<typeof import('@tanstack/react-router')>()),
useNavigate: () => mockNavigate,
useParams: () => routeParams.current,
}));

vi.mock('../data/thread-actions', () => ({
useThreadActions: () => ({ trash: mockTrash }),
}));

vi.mock('@tale/ui/use-toast', () => ({
toast: (...args: unknown[]) => mockToast(...args),
useToast: () => ({ toast: mockToast }),
}));

function renderDialog() {
const onOpenChange = vi.fn();
const view = render(
<ThreadDeleteDialog
thread={{ id: 't1', title: 'Quarterly report' }}
organizationId="org-1"
open
onOpenChange={onOpenChange}
/>,
);
return { ...view, onOpenChange };
}

describe('ThreadDeleteDialog', () => {
beforeEach(() => {
vi.clearAllMocks();
routeParams.current = { id: 'org-1' };
});

it('leaves the deleted conversation for a fresh chat', async () => {
routeParams.current = { id: 'org-1', threadId: 't1' };
mockTrash.mockResolvedValueOnce(true);
const { user, onOpenChange } = renderDialog();

await user.click(screen.getByRole('button', { name: 'Delete chat' }));

await waitFor(() =>
expect(mockNavigate).toHaveBeenCalledExactlyOnceWith({
to: '/dashboard/$id/chat',
params: { id: 'org-1' },
search: { new: true },
replace: true,
}),
);
expect(mockTrash).toHaveBeenCalledWith('t1');
expect(onOpenChange).toHaveBeenCalledWith(false);
});

it('stays on another conversation when a row deletes this chat', async () => {
routeParams.current = { id: 'org-1', threadId: 't2' };
mockTrash.mockResolvedValueOnce(true);
const { user, onOpenChange } = renderDialog();

await user.click(screen.getByRole('button', { name: 'Delete chat' }));

await waitFor(() => expect(onOpenChange).toHaveBeenCalledWith(false));
expect(mockNavigate).not.toHaveBeenCalled();
});

it('stays where it is off the chat pages', async () => {
mockTrash.mockResolvedValueOnce(true);
const { user, onOpenChange } = renderDialog();

await user.click(screen.getByRole('button', { name: 'Delete chat' }));

await waitFor(() => expect(onOpenChange).toHaveBeenCalledWith(false));
expect(mockNavigate).not.toHaveBeenCalled();
});

it('keeps the conversation and the dialog when the delete is refused', async () => {
routeParams.current = { id: 'org-1', threadId: 't1' };
mockTrash.mockResolvedValueOnce(false);
const { user, onOpenChange } = renderDialog();

await user.click(screen.getByRole('button', { name: 'Delete chat' }));

await waitFor(() =>
expect(mockToast).toHaveBeenCalledWith({
title: "Couldn't delete chat",
variant: 'destructive',
}),
);
expect(onOpenChange).not.toHaveBeenCalled();
expect(mockNavigate).not.toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { DeleteDialog } from '@tale/ui/dialog/delete-dialog';
import { toast } from '@tale/ui/use-toast';
import { useNavigate, useParams } from '@tanstack/react-router';
import { useState } from 'react';

import { useT } from '@/lib/i18n/client';
Expand All @@ -17,16 +18,17 @@ export function ThreadDeleteDialog({
organizationId,
open,
onOpenChange,
onDeleted,
}: {
thread: Pick<ChatThreadSummary, 'id' | 'title'>;
organizationId: string;
open: boolean;
onOpenChange: (open: boolean) => void;
/** Runs after a successful delete — leave the dead conversation. */
onDeleted?: () => void;
}) {
const { t } = useT('chat');
const navigate = useNavigate();
// The conversation on screen, if any: the header menu deletes it, a row
// menu deletes it or any other chat.
const openThreadId = useParams({ strict: false }).threadId;
const actions = useThreadActions(organizationId);
const [deleting, setDeleting] = useState(false);

Expand All @@ -40,7 +42,17 @@ export function ThreadDeleteDialog({
return;
}
onOpenChange(false);
onDeleted?.();
// Leave the deleted conversation for a fresh chat, in place of its
// page. Plain `/chat` would resume the most recent chat, and the list
// still names the deleted one until it has read again.
if (openThreadId === thread.id) {
void navigate({
to: '/dashboard/$id/chat',
params: { id: organizationId },
search: { new: true },
replace: true,
});
}
})
.finally(() => setDeleting(false));
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ vi.mock('@tanstack/react-router', () => ({
</a>
),
useNavigate: () => navigateMock,
useParams: () => ({ id: 'org-1' }),
}));

const renameMock = vi.hoisted(() => vi.fn(() => Promise.resolve(true)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,6 @@ export function ThreadRowMenu({
organizationId={organizationId}
open={deleteOpen}
onOpenChange={setDeleteOpen}
onDeleted={leaveIfActive}
/>
)}
</>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @vitest-environment jsdom
import '@testing-library/jest-dom/vitest';
import { describe, it, vi } from 'vitest';
import { beforeEach, describe, expect, it, vi } from 'vitest';

import { checkAccessibility } from '@/tests/utils/a11y';
import { render } from '@/tests/utils/render';
Expand Down Expand Up @@ -29,19 +29,66 @@ vi.mock('@/app/hooks/use-organization-id', () => ({
useOrganizationId: () => 'org-1',
}));

const TRAIL = [
{ _id: 'folder-1', name: 'Documents' },
{ _id: 'folder-2', name: 'Reports' },
];

const { breadcrumbRead } = vi.hoisted(() => ({
breadcrumbRead: {
current: { data: undefined as unknown, isLoading: false },
},
}));

vi.mock('@/app/hooks/use-backend-query', () => ({
useBackendQuery: () => ({
data: [
{ _id: 'folder-1', name: 'Documents' },
{ _id: 'folder-2', name: 'Reports' },
],
isLoading: false,
}),
useBackendQuery: () => breadcrumbRead.current,
}));

import { toast } from '@tale/ui/use-toast';

import { BreadcrumbNavigation } from './breadcrumb-navigation';

beforeEach(() => {
vi.clearAllMocks();
breadcrumbRead.current = { data: TRAIL, isLoading: false };
});

describe('BreadcrumbNavigation', () => {
// A deleted folder's address answers no trail. The page leaves it for the
// root in place of the dead entry, so Back does not walk into it again.
it('leaves a folder that is gone for the root, replacing its address', () => {
breadcrumbRead.current = { data: [], isLoading: false };
const onNavigate = vi.fn();
render(
<BreadcrumbNavigation folderId="folder-gone" onNavigate={onNavigate} />,
);

expect(onNavigate).toHaveBeenCalledExactlyOnceWith(undefined, {
replace: true,
});
expect(toast).toHaveBeenCalledWith({ title: 'documents.folderNotFound' });
});

it('stays on a folder whose trail is still loading', () => {
breadcrumbRead.current = { data: undefined, isLoading: true };
const onNavigate = vi.fn();
render(
<BreadcrumbNavigation folderId="folder-2" onNavigate={onNavigate} />,
);

expect(onNavigate).not.toHaveBeenCalled();
expect(toast).not.toHaveBeenCalled();
});

it('stays on a folder with a trail', () => {
const onNavigate = vi.fn();
render(
<BreadcrumbNavigation folderId="folder-2" onNavigate={onNavigate} />,
);

expect(onNavigate).not.toHaveBeenCalled();
});

describe('accessibility', () => {
it('passes axe audit', async () => {
const { container } = render(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import { useT } from '@/lib/i18n/client';

interface BreadcrumbNavigationProps {
folderId: string;
onNavigate: (folderId: string | undefined) => void;
onNavigate: (
folderId: string | undefined,
options?: { replace?: boolean },
) => void;
}

export function BreadcrumbNavigation({
Expand All @@ -29,10 +32,13 @@ export function BreadcrumbNavigation({
organizationId ? { folderId: folderId, organizationId } : 'skip',
);

// A folder with no trail is gone (deleted, here or in another tab) or out
// of reach: leave its address for the root, in place of it, so Back does
// not walk into it again.
useEffect(() => {
if (!isLoading && breadcrumb !== undefined && breadcrumb.length === 0) {
toast({ title: t('folderNotFound') });
onNavigateRef.current(undefined);
onNavigateRef.current(undefined, { replace: true });
}
}, [breadcrumb, isLoading, t]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ interface DocumentRowActionsProps {
teamIds?: string[];
/** The folder the row sits in — the move dialog opens on it. */
currentFolderId?: string;
onFolderDeleted?: () => void;
parentFolderTeamId?: string;
/** Gates the "Reindex" action — terminal `unsupported` files (no text
* extractor exists) never get a retry affordance, on the row menu any
Expand All @@ -85,7 +84,6 @@ export function DocumentRowActions({
sourceProvider,
teamIds,
currentFolderId,
onFolderDeleted,
parentFolderTeamId,
ragStatus,
record,
Expand Down Expand Up @@ -156,10 +154,7 @@ export function DocumentRowActions({

const handleDeleteFolderConfirm = useCallback(() => {
void deleteFolder({ folderId: documentId }).then(
() => {
dialogs.setOpen.deleteFolder(false);
onFolderDeleted?.();
},
() => dialogs.setOpen.deleteFolder(false),
(error: unknown) => {
console.error('Failed to delete folder:', error);
// `AppError.message` is the serialized payload by design; the
Expand All @@ -176,7 +171,7 @@ export function DocumentRowActions({
});
},
);
}, [deleteFolder, documentId, dialogs.setOpen, tDocuments, onFolderDeleted]);
}, [deleteFolder, documentId, dialogs.setOpen, tDocuments]);

const handleDeleteClick = useCallback(() => {
if (itemType === 'folder') {
Expand Down
Loading