|
| 1 | +import { UnitTestMocker } from "@/jest/mocks/UnitTestMocker"; |
| 2 | +import { ConfigService } from "@/services/ConfigService"; |
| 3 | +import { DirectoryScanner } from "@/services/FileManagement/DirectoryScanner"; |
| 4 | +import { DebugLogger } from "@/services/logging/DebugLogger"; |
| 5 | +import fs from "fs"; |
| 6 | +import path from "path"; |
| 7 | +import { ActionTagsExtractor } from "../ActionTagsExtractor"; |
| 8 | +import { ListDirectoryFilesAction } from "../ListDirectoryFilesAction"; |
| 9 | + |
| 10 | +describe("ListDirectoryFilesAction", () => { |
| 11 | + let action: ListDirectoryFilesAction; |
| 12 | + let mockDirectoryScanner: DirectoryScanner; |
| 13 | + let mocker: UnitTestMocker; |
| 14 | + let mockConfigService: ConfigService; |
| 15 | + let mockActionTagsExtractor: ActionTagsExtractor; |
| 16 | + let consoleErrorSpy: jest.SpyInstance; |
| 17 | + let consoleLogSpy: jest.SpyInstance; |
| 18 | + |
| 19 | + beforeEach(() => { |
| 20 | + mocker = new UnitTestMocker(); |
| 21 | + |
| 22 | + // Mock console methods |
| 23 | + consoleErrorSpy = jest.spyOn(console, "error").mockImplementation(() => {}); |
| 24 | + consoleLogSpy = jest.spyOn(console, "log").mockImplementation(() => {}); |
| 25 | + |
| 26 | + // Mock ConfigService |
| 27 | + mockConfigService = new ConfigService(); |
| 28 | + mocker.mockPrototype(ConfigService, "getConfig", { |
| 29 | + directoryScanner: { |
| 30 | + defaultIgnore: [], |
| 31 | + allFiles: true, |
| 32 | + maxDepth: 4, |
| 33 | + directoryFirst: true, |
| 34 | + excludeDirectories: [], |
| 35 | + }, |
| 36 | + }); |
| 37 | + |
| 38 | + mockDirectoryScanner = new DirectoryScanner(mockConfigService); |
| 39 | + |
| 40 | + // Mock filesystem operations |
| 41 | + mocker.mockModule(fs, "statSync", { isDirectory: () => true }); |
| 42 | + mocker.mockModule(path, "resolve", (p: string) => p); |
| 43 | + |
| 44 | + // Mock DirectoryScanner scan method |
| 45 | + mocker.mockPrototype(DirectoryScanner, "scan", { |
| 46 | + success: true, |
| 47 | + data: "file1.txt\nfile2.txt", |
| 48 | + }); |
| 49 | + |
| 50 | + // Mock ActionTagsExtractor |
| 51 | + mockActionTagsExtractor = new ActionTagsExtractor(); |
| 52 | + mocker.mockPrototypeWith( |
| 53 | + ActionTagsExtractor, |
| 54 | + "extractTag", |
| 55 | + (content: string, tag: string) => { |
| 56 | + if (tag === "path") { |
| 57 | + if (content.includes("<path>./src1</path><path>./src2</path>")) { |
| 58 | + return ["./src1", "./src2"]; |
| 59 | + } |
| 60 | + return "./src"; |
| 61 | + } |
| 62 | + if (tag === "recursive") { |
| 63 | + return content.includes("<recursive>true</recursive>") |
| 64 | + ? "true" |
| 65 | + : null; |
| 66 | + } |
| 67 | + return null; |
| 68 | + }, |
| 69 | + ); |
| 70 | + |
| 71 | + action = new ListDirectoryFilesAction( |
| 72 | + mockActionTagsExtractor, |
| 73 | + mockDirectoryScanner, |
| 74 | + new DebugLogger(), |
| 75 | + ); |
| 76 | + }); |
| 77 | + |
| 78 | + afterEach(() => { |
| 79 | + mocker.clearAllMocks(); |
| 80 | + consoleErrorSpy.mockRestore(); |
| 81 | + consoleLogSpy.mockRestore(); |
| 82 | + }); |
| 83 | + |
| 84 | + it("should list directory contents successfully", async () => { |
| 85 | + const result = await action.execute( |
| 86 | + "<list_directory_files><path>./src</path></list_directory_files>", |
| 87 | + ); |
| 88 | + |
| 89 | + expect(result.success).toBe(true); |
| 90 | + expect(result.data).toBe("file1.txt\nfile2.txt"); |
| 91 | + }); |
| 92 | + |
| 93 | + it("should handle recursive listing", async () => { |
| 94 | + mocker.mockPrototype(DirectoryScanner, "scan", { |
| 95 | + success: true, |
| 96 | + data: "dir1/file1.txt\ndir2/file2.txt", |
| 97 | + }); |
| 98 | + |
| 99 | + const result = await action.execute( |
| 100 | + "<list_directory_files><path>./src</path><recursive>true</recursive></list_directory_files>", |
| 101 | + ); |
| 102 | + |
| 103 | + expect(result.success).toBe(true); |
| 104 | + expect(result.data).toBe("dir1/file1.txt\ndir2/file2.txt"); |
| 105 | + }); |
| 106 | + |
| 107 | + it("should handle multiple paths", async () => { |
| 108 | + const scanSpy = mocker.mockPrototypeWith( |
| 109 | + DirectoryScanner, |
| 110 | + "scan", |
| 111 | + async (path: string) => { |
| 112 | + if (path === "./src1") { |
| 113 | + return { success: true, data: "src1/file1.txt\nsrc1/file2.txt" }; |
| 114 | + } |
| 115 | + return { success: true, data: "src2/file3.txt\nsrc2/file4.txt" }; |
| 116 | + }, |
| 117 | + ); |
| 118 | + |
| 119 | + const result = await action.execute( |
| 120 | + "<list_directory_files><path>./src1</path><path>./src2</path></list_directory_files>", |
| 121 | + ); |
| 122 | + |
| 123 | + expect(result.success).toBe(true); |
| 124 | + expect(result.data).toContain("src1/file1.txt"); |
| 125 | + expect(result.data).toContain("src1/file2.txt"); |
| 126 | + expect(result.data).toContain("src2/file3.txt"); |
| 127 | + expect(result.data).toContain("src2/file4.txt"); |
| 128 | + expect(scanSpy).toHaveBeenCalledTimes(2); |
| 129 | + expect(scanSpy).toHaveBeenCalledWith("./src1", expect.any(Object)); |
| 130 | + expect(scanSpy).toHaveBeenCalledWith("./src2", expect.any(Object)); |
| 131 | + }); |
| 132 | + |
| 133 | + it("should handle multiple paths with recursive option", async () => { |
| 134 | + const scanSpy = mocker.mockPrototypeWith( |
| 135 | + DirectoryScanner, |
| 136 | + "scan", |
| 137 | + async (path: string) => { |
| 138 | + if (path === "./src1") { |
| 139 | + return { success: true, data: "src1/deep/file1.txt\nsrc1/file2.txt" }; |
| 140 | + } |
| 141 | + return { success: true, data: "src2/deep/file3.txt\nsrc2/file4.txt" }; |
| 142 | + }, |
| 143 | + ); |
| 144 | + |
| 145 | + const result = await action.execute( |
| 146 | + "<list_directory_files><path>./src1</path><path>./src2</path><recursive>true</recursive></list_directory_files>", |
| 147 | + ); |
| 148 | + |
| 149 | + expect(result.success).toBe(true); |
| 150 | + expect(result.data).toContain("src1/deep/file1.txt"); |
| 151 | + expect(result.data).toContain("src1/file2.txt"); |
| 152 | + expect(result.data).toContain("src2/deep/file3.txt"); |
| 153 | + expect(result.data).toContain("src2/file4.txt"); |
| 154 | + expect(scanSpy).toHaveBeenCalledTimes(2); |
| 155 | + expect(scanSpy).toHaveBeenCalledWith( |
| 156 | + "./src1", |
| 157 | + expect.objectContaining({ maxDepth: undefined }), |
| 158 | + ); |
| 159 | + expect(scanSpy).toHaveBeenCalledWith( |
| 160 | + "./src2", |
| 161 | + expect.objectContaining({ maxDepth: undefined }), |
| 162 | + ); |
| 163 | + }); |
| 164 | +}); |
0 commit comments