workspaceRemovalSafety.test.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import { describe, expect, it, vi } from "vitest";
  2. import type { FileEntry } from "@zcode/shared";
  3. import {
  4. hasRunningWorkspaceChat,
  5. isWindowsReservedDevicePathSegment,
  6. scanWindowsReservedDeviceNameFiles,
  7. } from "@/lib/workspaceRemovalSafety.js";
  8. vi.mock("@/logger.js", () => ({
  9. logger: {
  10. debug: vi.fn(),
  11. },
  12. }));
  13. function createFileEntry(name: string, path: string, type: "file" | "directory" = "file"): FileEntry {
  14. return { name, path, type };
  15. }
  16. describe("workspaceRemovalSafety", () => {
  17. it("detects running draft or task runtime before workspace removal", () => {
  18. expect(
  19. hasRunningWorkspaceChat({
  20. workspaceState: {
  21. draftRuntime: { status: "streaming", error: null },
  22. taskRuntimeByTaskId: {},
  23. },
  24. taskItems: [],
  25. }),
  26. ).toBe(true);
  27. expect(
  28. hasRunningWorkspaceChat({
  29. workspaceState: {
  30. draftRuntime: { status: "idle", error: null },
  31. taskRuntimeByTaskId: {
  32. task_1: {
  33. status: "creating",
  34. error: null,
  35. contextWindow: null,
  36. usage: null,
  37. apiRetry: null,
  38. backgroundTaskControls: [],
  39. },
  40. },
  41. },
  42. taskItems: [{ taskId: "task_1" }],
  43. }),
  44. ).toBe(true);
  45. expect(
  46. hasRunningWorkspaceChat({
  47. workspaceState: {
  48. draftRuntime: { status: "idle", error: null },
  49. taskRuntimeByTaskId: {
  50. task_1: {
  51. status: "completed",
  52. error: null,
  53. contextWindow: null,
  54. usage: null,
  55. apiRetry: null,
  56. backgroundTaskControls: [],
  57. },
  58. },
  59. },
  60. taskItems: [{ taskId: "task_1" }],
  61. }),
  62. ).toBe(false);
  63. });
  64. it("detects Windows reserved device names including extension variants", () => {
  65. expect(isWindowsReservedDevicePathSegment("nul")).toBe(true);
  66. expect(isWindowsReservedDevicePathSegment("NUL")).toBe(true);
  67. expect(isWindowsReservedDevicePathSegment("nul.txt")).toBe(true);
  68. expect(isWindowsReservedDevicePathSegment("con.md")).toBe(true);
  69. expect(isWindowsReservedDevicePathSegment("aux.log")).toBe(true);
  70. expect(isWindowsReservedDevicePathSegment("COM1.json")).toBe(true);
  71. expect(isWindowsReservedDevicePathSegment("LPT9.tmp")).toBe(true);
  72. expect(isWindowsReservedDevicePathSegment("normal-null.txt")).toBe(false);
  73. expect(isWindowsReservedDevicePathSegment("component.tsx")).toBe(false);
  74. });
  75. it("scans workspace files, reports reserved names, and skips low relevance directories", async () => {
  76. const readdir = vi.fn(async ({ path }: { path: string }) => {
  77. const entriesByPath: Record<string, FileEntry[]> = {
  78. "C:/repo": [
  79. createFileEntry("src", "C:/repo/src", "directory"),
  80. createFileEntry("node_modules", "C:/repo/node_modules", "directory"),
  81. createFileEntry(".git", "C:/repo/.git", "directory"),
  82. ],
  83. "C:/repo/src": [
  84. createFileEntry("nul", "C:/repo/src/nul"),
  85. createFileEntry("COM1.json", "C:/repo/src/COM1.json"),
  86. ],
  87. "C:/repo/node_modules": [createFileEntry("con", "C:/repo/node_modules/con")],
  88. "C:/repo/.git": [createFileEntry("aux", "C:/repo/.git/aux")],
  89. };
  90. return entriesByPath[path] ?? [];
  91. });
  92. const result = await scanWindowsReservedDeviceNameFiles({ readdir }, "C:/repo");
  93. expect(result.findings).toEqual(["C:/repo/src/nul", "C:/repo/src/COM1.json"]);
  94. expect(result.truncated).toBe(false);
  95. expect(readdir).toHaveBeenCalledWith({ path: "C:/repo", includeHidden: true });
  96. expect(readdir).toHaveBeenCalledWith({ path: "C:/repo/src", includeHidden: true });
  97. expect(readdir).not.toHaveBeenCalledWith({ path: "C:/repo/node_modules", includeHidden: true });
  98. expect(readdir).not.toHaveBeenCalledWith({ path: "C:/repo/.git", includeHidden: true });
  99. });
  100. it("limits reserved-name scan findings without throwing", async () => {
  101. const readdir = vi.fn(async () => [
  102. createFileEntry("nul", "C:/repo/nul"),
  103. createFileEntry("con", "C:/repo/con"),
  104. ]);
  105. const result = await scanWindowsReservedDeviceNameFiles({ readdir }, "C:/repo", {
  106. maxFindings: 1,
  107. });
  108. expect(result.findings).toEqual(["C:/repo/nul"]);
  109. expect(result.truncated).toBe(true);
  110. });
  111. });