workspaceFileSearchSection.test.ts 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import type { IFileService } from "@zcode/services";
  2. import type { IServiceAccessor } from "@/hooks/useServices.js";
  3. import { readFileSync } from "node:fs";
  4. import { createElement } from "react";
  5. import { renderToStaticMarkup } from "react-dom/server";
  6. import { describe, expect, it } from "vitest";
  7. import { ServiceProvider } from "@/hooks/useServices.js";
  8. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  9. import { WorkspaceFileSearchSection } from "@/settings/WorkspaceFileSearchSection.js";
  10. // 组件顶层 hook 链(useWorkspaceServicesResolution → useServices)要求 context 存在;
  11. // 无 workspace 分支不会真正调用任何服务方法,空 accessor 即可。
  12. const emptyAccessor = {} as unknown as IServiceAccessor;
  13. function renderSection(props: { workspacePath?: string | null }): string {
  14. return renderToStaticMarkup(
  15. createElement(
  16. ServiceProvider,
  17. { services: emptyAccessor },
  18. createElement(
  19. ZCodeIntlProvider,
  20. { initialLocale: "zh-CN" },
  21. createElement(WorkspaceFileSearchSection, props),
  22. ),
  23. ),
  24. );
  25. }
  26. describe("WorkspaceFileSearch settings section", () => {
  27. it("renders a hint instead of the editor when no workspace is open", () => {
  28. // 无 workspace 分支不触达 services:直接渲染提示文案,避免误拉文件服务。
  29. const html = renderSection({ workspacePath: null });
  30. // 显式固定中文,避免系统语言不同导致相同组件在 CI 上断言失败。
  31. expect(html).toContain("当前没有打开的工作区");
  32. expect(html).not.toContain("workspace-file-search-ignore-editor");
  33. });
  34. it("wires the ignore editor to the fileService RPC contract", () => {
  35. // 契约断言(同 hooksSection 模式):编辑页必须走 fileService 的专用 RPC,
  36. // 不允许绕过服务层直接读写本地文件;分区操作必须走 applyTransform(分区重写,
  37. // 不允许整体覆盖用户内容)。
  38. const source = readFileSync(
  39. new URL("../src/settings/WorkspaceFileSearchSection.tsx", import.meta.url),
  40. "utf8",
  41. );
  42. expect(source).toContain("readWorkspaceFileSearchIgnore");
  43. expect(source).toContain("writeWorkspaceFileSearchIgnore");
  44. expect(source).toContain("applyWorkspaceFileSearchIgnoreTransform({");
  45. expect(source).toContain("transform,");
  46. expect(source).not.toContain("readTextFile");
  47. expect(source).not.toContain("window.zcode");
  48. });
  49. it("keeps the save action gated on editable-or-dirty state in source contract", () => {
  50. const source = readFileSync(
  51. new URL("../src/settings/WorkspaceFileSearchSection.tsx", import.meta.url),
  52. "utf8",
  53. );
  54. // 保存语义是"落盘编辑框内容":template 态(文件未创建)即使未编辑也可保存,
  55. // 用户第一次进页面不改任何内容也能创建 .zcodeignore;已落盘态才有修改才可保存。
  56. expect(source).toContain(
  57. 'const canSave = loaded === null || loaded.source === "template" || draft !== loaded.content;',
  58. );
  59. expect(source).toContain("disabled={!canSave || saving || loading}");
  60. // "未保存修改"提示仍只表达真实差异,template 态未编辑时不显示。
  61. expect(source).toContain(
  62. 'const dirty = loaded !== null && loaded.source === "file" && draft !== loaded.content;',
  63. );
  64. });
  65. it("matches the fileService RPC surface used by the section", () => {
  66. // IFileService 契约快照:section 依赖的两个方法必须存在于服务接口,
  67. // 防止未来接口重命名悄悄断开设置页。
  68. const fileServiceKeys: Array<keyof IFileService> = [
  69. "readWorkspaceFileSearchIgnore",
  70. "writeWorkspaceFileSearchIgnore",
  71. ];
  72. expect(fileServiceKeys.every((key) => typeof key === "string")).toBe(true);
  73. });
  74. });