diff --git a/web/src/components/MemoEditor/edit/hooks/useLinkMemo.ts b/web/src/components/MemoEditor/edit/hooks/useLinkMemo.ts index 081a2ad14..94e11e6b3 100644 --- a/web/src/components/MemoEditor/edit/hooks/useLinkMemo.ts +++ b/web/src/components/MemoEditor/edit/hooks/useLinkMemo.ts @@ -50,7 +50,7 @@ export const useLinkMemo = ({ isOpen, currentMemoName, existingRelations, onAddR conditions.push(creatorFilter); } if (searchText) { - conditions.push(`content.contains("${searchText}")`); + conditions.push(`content.contains(${JSON.stringify(searchText)})`); } const { memos } = await memoServiceClient.listMemos({ pageSize: DEFAULT_LIST_MEMOS_PAGE_SIZE, diff --git a/web/tests/link-memo-search.test.ts b/web/tests/link-memo-search.test.ts new file mode 100644 index 000000000..09712c8a2 --- /dev/null +++ b/web/tests/link-memo-search.test.ts @@ -0,0 +1,38 @@ +import { act, renderHook, waitFor } from "@testing-library/react"; +import { beforeEach, describe, expect, it, vi } from "vitest"; + +const mocks = vi.hoisted(() => ({ + listMemos: vi.fn(), +})); + +vi.mock("@/connect", () => ({ + memoServiceClient: { listMemos: mocks.listMemos }, +})); + +vi.mock("@/hooks/useCurrentUser", () => ({ + default: () => ({ name: "users/alice" }), +})); + +import { useLinkMemo } from "@/components/MemoEditor/edit/hooks/useLinkMemo"; + +const lastFilter = () => mocks.listMemos.mock.calls.at(-1)?.[0]?.filter; + +describe("useLinkMemo search filter", () => { + beforeEach(() => { + mocks.listMemos.mockResolvedValue({ memos: [] }); + }); + + it.each([ + ["meeting", 'creator == "users/alice" && content.contains("meeting")'], + ['say "hi"', 'creator == "users/alice" && content.contains("say \\"hi\\"")'], + ["C:\\", 'creator == "users/alice" && content.contains("C:\\\\")'], + ])("passes %j to the filter as a quoted CEL string", async (searchText, expected) => { + const { result } = renderHook(() => useLinkMemo({ isOpen: true, existingRelations: [], onAddRelation: vi.fn() })); + + act(() => { + result.current.setSearchText(searchText); + }); + + await waitFor(() => expect(lastFilter()).toBe(expected)); + }); +});