Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

test(chat): check the filter menu refresh against the real hooks

Manas Raghuwanshia2a90d58893a
diff --git a/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-chat-panel-effects.test.ts b/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-chat-panel-effects.test.ts
index 8668a517a..927dfbea4 100644
--- a/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-chat-panel-effects.test.ts
+++ b/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-chat-panel-effects.test.ts
@@ -9,15 +9,43 @@
*/
import { afterEach, describe, expect, it, vi } from "vitest";
-import { cleanup, renderHook } from "@testing-library/react";
+import { act, cleanup, renderHook } from "@testing-library/react";
+import { useSqlAutocomplete, useTagAutocomplete } from "@/lib/hooks/use-sql-autocomplete";
import { useChatPanelEffects } from "./use-chat-panel-effects";
+// Requests to the local API wait until the test answers them, so the test
+// decides when each fetch finishes.
+const network = vi.hoisted(() => ({ tags: 0, apps: 0, unanswered: [] as Array<() => void> }));
+
+vi.mock("@/lib/api", () => ({
+ localFetch: vi.fn((path: string) => {
+ const isTags = path.startsWith("/tags/autocomplete");
+ if (isTags) network.tags++;
+ else network.apps++;
+ const body = isTags ? [{ name: "work", count: 3 }] : [];
+ return new Promise((resolve) => {
+ network.unanswered.push(() => resolve({ ok: true, json: async () => body }));
+ });
+ }),
+}));
+
vi.mock("@/lib/utils/tauri", () => ({
commands: { piAbortActive: vi.fn(), closeWindow: vi.fn() },
}));
afterEach(cleanup);
+const panelProps = {
+ inputRef: { current: null },
+ mentionDropdownIsOpen: false,
+ isLoading: false,
+ isStreaming: false,
+ piActiveStopRequestedRef: { current: false },
+ piSessionIdRef: { current: "" },
+ setIsLoading: vi.fn(),
+ setIsStreaming: vi.fn(),
+};
+
type MenuState = {
appFilterOpen: boolean;
appItemsLength: number;
@@ -28,21 +56,10 @@ type MenuState = {
function renderMenu(initial: MenuState) {
const refreshAppItems = vi.fn();
const refreshTagItems = vi.fn();
- const fixed = {
- inputRef: { current: null },
- mentionDropdownIsOpen: false,
- isLoading: false,
- isStreaming: false,
- piActiveStopRequestedRef: { current: false },
- piSessionIdRef: { current: "" },
- setIsLoading: vi.fn(),
- setIsStreaming: vi.fn(),
- refreshAppItems,
- refreshTagItems,
- };
- const hook = renderHook((state: MenuState) => useChatPanelEffects({ ...fixed, ...state }), {
- initialProps: initial,
- });
+ const hook = renderHook(
+ (state: MenuState) => useChatPanelEffects({ ...panelProps, refreshAppItems, refreshTagItems, ...state }),
+ { initialProps: initial },
+ );
return { ...hook, refreshAppItems, refreshTagItems };
}
@@ -79,13 +96,14 @@ describe("composer filter menu refresh", () => {
expect(refreshAppItems).toHaveBeenCalledTimes(1);
});
- it("leaves a fetch that is already running alone", () => {
- const { refreshAppItems, refreshTagItems } = renderMenu({
- ...open,
- appItemsLength: 0,
- appsLoading: true,
- tagsLoading: true,
- });
+ it("leaves a fetch that is already running alone, and does not follow it with another", () => {
+ const running = { ...open, appItemsLength: 0, appsLoading: true, tagsLoading: true };
+ const { rerender, refreshAppItems, refreshTagItems } = renderMenu(running);
+ expect(refreshAppItems).not.toHaveBeenCalled();
+ expect(refreshTagItems).not.toHaveBeenCalled();
+
+ // That fetch finishes while the menu is still open; its result is fresh.
+ rerender({ ...running, appsLoading: false, tagsLoading: false });
expect(refreshAppItems).not.toHaveBeenCalled();
expect(refreshTagItems).not.toHaveBeenCalled();
});
@@ -95,4 +113,41 @@ describe("composer filter menu refresh", () => {
expect(refreshAppItems).not.toHaveBeenCalled();
expect(refreshTagItems).not.toHaveBeenCalled();
});
+
+ it("sends one tag and one app request per opening with the real autocomplete hooks", async () => {
+ const answerRequests = () =>
+ act(async () => {
+ for (const answer of network.unanswered.splice(0)) answer();
+ await new Promise((resolve) => setTimeout(resolve, 0));
+ });
+ const { rerender } = renderHook(
+ ({ appFilterOpen }: { appFilterOpen: boolean }) => {
+ const apps = useSqlAutocomplete("app");
+ const tags = useTagAutocomplete();
+ useChatPanelEffects({
+ ...panelProps,
+ appFilterOpen,
+ appItemsLength: apps.items.length,
+ appsLoading: apps.isLoading,
+ tagsLoading: tags.isLoading,
+ refreshAppItems: apps.refresh,
+ refreshTagItems: tags.refresh,
+ });
+ },
+ { initialProps: { appFilterOpen: false } },
+ );
+ await answerRequests(); // the lists' first load, before the menu opens
+ network.tags = 0;
+ network.apps = 0;
+
+ // Every request finishes while the menu stays open; the app list stays empty.
+ rerender({ appFilterOpen: true });
+ for (let i = 0; i < 3; i++) await answerRequests();
+ expect([network.tags, network.apps]).toEqual([1, 1]);
+
+ rerender({ appFilterOpen: false });
+ rerender({ appFilterOpen: true });
+ await answerRequests();
+ expect([network.tags, network.apps]).toEqual([2, 2]);
+ });
});