workspaceFileSearchSection.test.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import type { IServiceAccessor } from "@/hooks/useServices.js";
  2. import { readFileSync } from "node:fs";
  3. import { createElement } from "react";
  4. import { renderToStaticMarkup } from "react-dom/server";
  5. import { describe, expect, it } from "vitest";
  6. import { ServiceProvider } from "@/hooks/useServices.js";
  7. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  8. import { WorkspaceFileSearchSection } from "@/settings/WorkspaceFileSearchSection.js";
  9. // 组件顶层 hook 链(useWorkspaceServicesResolution → useServices)要求 context 存在;
  10. // 无 workspace 分支不会真正调用任何服务方法,空 accessor 即可。
  11. const emptyAccessor = {} as unknown as IServiceAccessor;
  12. function renderSection(props: { workspacePath?: string | null }): string {
  13. return renderToStaticMarkup(
  14. createElement(
  15. ServiceProvider,
  16. { services: emptyAccessor },
  17. createElement(
  18. ZCodeIntlProvider,
  19. { initialLocale: "zh-CN" },
  20. createElement(WorkspaceFileSearchSection, props),
  21. ),
  22. ),
  23. );
  24. }
  25. describe("WorkspaceFileSearch settings section", () => {
  26. it("renders a hint instead of the editor when no workspace is open", () => {
  27. // 无 workspace 分支不触达 services:直接渲染提示文案,避免误拉文件服务。
  28. const html = renderSection({ workspacePath: null });
  29. // 显式固定中文,避免系统语言不同导致相同组件在 CI 上断言失败。
  30. expect(html).toContain("当前没有打开的工作区");
  31. expect(html).not.toContain("workspace-file-search-ignore-editor");
  32. });
  33. it("wires the ignore editor to the fileService RPC contract", () => {
  34. // 契约断言(同 hooksSection 模式):编辑页必须走 fileService 的专用 RPC,
  35. // 不允许绕过服务层直接读写本地文件;分区操作必须走 applyTransform(分区重写,
  36. // 不允许整体覆盖用户内容)。
  37. const source = readFileSync(
  38. new URL("../src/settings/WorkspaceFileSearchSection.tsx", import.meta.url),
  39. "utf8",
  40. );
  41. expect(source).toContain("readWorkspaceFileSearchIgnore");
  42. expect(source).toContain("writeWorkspaceFileSearchIgnore");
  43. expect(source).toContain("applyWorkspaceFileSearchIgnoreTransform({");
  44. expect(source).toContain("transform,");
  45. expect(source).not.toContain("readTextFile");
  46. expect(source).not.toContain("window.zcode");
  47. });
  48. });