@bryan-thompson/inspector-assessment 1.35.2 → 1.36.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (82) hide show
  1. package/cli/build/__tests__/assess-full-e2e.test.js +35 -9
  2. package/cli/build/__tests__/assess-full.test.js +4 -1
  3. package/cli/build/__tests__/assessment-runner/assessment-executor.test.js +15 -2
  4. package/cli/build/__tests__/assessment-runner/config-builder.test.js +6 -0
  5. package/cli/build/__tests__/assessment-runner/index.test.js +9 -4
  6. package/cli/build/__tests__/assessment-runner/path-resolver.test.js +112 -0
  7. package/cli/build/__tests__/assessment-runner/server-config.test.js +3 -0
  8. package/cli/build/__tests__/assessment-runner/server-connection.test.js +9 -1
  9. package/cli/build/__tests__/assessment-runner/source-loader.test.js +121 -16
  10. package/cli/build/__tests__/assessment-runner/tool-wrapper.test.js +3 -0
  11. package/cli/build/__tests__/assessment-runner-facade.test.js +7 -2
  12. package/cli/build/__tests__/cli-build-fixes.test.js +4 -1
  13. package/cli/build/__tests__/flag-parsing.test.js +3 -2
  14. package/cli/build/__tests__/http-transport-integration.test.js +19 -5
  15. package/cli/build/__tests__/jsonl-events.test.js +1 -1
  16. package/cli/build/__tests__/lib/server-configSchemas.test.js +4 -1
  17. package/cli/build/__tests__/lib/zodErrorFormatter.test.js +4 -1
  18. package/cli/build/__tests__/profiles.test.js +19 -8
  19. package/cli/build/__tests__/security/security-pattern-count.test.js +6 -3
  20. package/cli/build/__tests__/stage3-fix-validation.test.js +4 -1
  21. package/cli/build/__tests__/testbed-integration.test.js +19 -5
  22. package/cli/build/__tests__/transport.test.js +4 -1
  23. package/cli/build/lib/__tests__/cli-parserSchemas.test.js +4 -1
  24. package/cli/build/lib/assessment-runner/__tests__/server-configSchemas.test.js +4 -1
  25. package/cli/build/lib/assessment-runner/assessment-executor.js +23 -4
  26. package/cli/build/lib/assessment-runner/index.js +2 -0
  27. package/cli/build/lib/assessment-runner/path-resolver.js +48 -0
  28. package/cli/build/lib/assessment-runner/source-loader.js +47 -5
  29. package/cli/build/lib/cli-parser.js +10 -0
  30. package/cli/package.json +1 -1
  31. package/client/dist/assets/{OAuthCallback-jfmizOMH.js → OAuthCallback-Cfp3Vzdz.js} +1 -1
  32. package/client/dist/assets/{OAuthDebugCallback-bU5kKvnt.js → OAuthDebugCallback-7BLaxlcq.js} +1 -1
  33. package/client/dist/assets/{index-Ce63ds7G.js → index-B21S7_ML.js} +4 -4
  34. package/client/dist/index.html +1 -1
  35. package/client/lib/lib/assessment/coreTypes.d.ts +23 -0
  36. package/client/lib/lib/assessment/coreTypes.d.ts.map +1 -1
  37. package/client/lib/lib/assessment/extendedTypes.d.ts +49 -2
  38. package/client/lib/lib/assessment/extendedTypes.d.ts.map +1 -1
  39. package/client/lib/lib/assessment/jsonlEventSchemas.d.ts +4 -4
  40. package/client/lib/lib/assessment/resultTypes.d.ts +32 -1
  41. package/client/lib/lib/assessment/resultTypes.d.ts.map +1 -1
  42. package/client/lib/lib/aupPatterns.d.ts +50 -0
  43. package/client/lib/lib/aupPatterns.d.ts.map +1 -1
  44. package/client/lib/lib/aupPatterns.js +140 -0
  45. package/client/lib/lib/moduleScoring.d.ts.map +1 -1
  46. package/client/lib/lib/moduleScoring.js +39 -2
  47. package/client/lib/lib/securityPatterns.d.ts.map +1 -1
  48. package/client/lib/lib/securityPatterns.js +92 -0
  49. package/client/lib/services/assessment/modules/DeveloperExperienceAssessor.d.ts +26 -1
  50. package/client/lib/services/assessment/modules/DeveloperExperienceAssessor.d.ts.map +1 -1
  51. package/client/lib/services/assessment/modules/DeveloperExperienceAssessor.js +160 -1
  52. package/client/lib/services/assessment/modules/ErrorHandlingAssessor.d.ts.map +1 -1
  53. package/client/lib/services/assessment/modules/ErrorHandlingAssessor.js +15 -0
  54. package/client/lib/services/assessment/modules/ManifestValidationAssessor.d.ts +32 -0
  55. package/client/lib/services/assessment/modules/ManifestValidationAssessor.d.ts.map +1 -1
  56. package/client/lib/services/assessment/modules/ManifestValidationAssessor.js +218 -20
  57. package/client/lib/services/assessment/modules/ProhibitedLibrariesAssessor.d.ts +5 -0
  58. package/client/lib/services/assessment/modules/ProhibitedLibrariesAssessor.d.ts.map +1 -1
  59. package/client/lib/services/assessment/modules/ProhibitedLibrariesAssessor.js +29 -0
  60. package/client/lib/services/assessment/modules/SecurityAssessor.d.ts.map +1 -1
  61. package/client/lib/services/assessment/modules/SecurityAssessor.js +13 -0
  62. package/client/lib/services/assessment/modules/annotations/AlignmentChecker.d.ts +7 -2
  63. package/client/lib/services/assessment/modules/annotations/AlignmentChecker.d.ts.map +1 -1
  64. package/client/lib/services/assessment/modules/annotations/AlignmentChecker.js +116 -18
  65. package/client/lib/services/assessment/modules/annotations/index.d.ts +1 -1
  66. package/client/lib/services/assessment/modules/annotations/index.d.ts.map +1 -1
  67. package/client/lib/services/assessment/modules/annotations/index.js +2 -1
  68. package/client/lib/services/assessment/modules/securityTests/ConfidenceScorer.d.ts.map +1 -1
  69. package/client/lib/services/assessment/modules/securityTests/ConfidenceScorer.js +28 -0
  70. package/client/lib/services/assessment/modules/securityTests/SecurityPatternLibrary.d.ts +95 -0
  71. package/client/lib/services/assessment/modules/securityTests/SecurityPatternLibrary.d.ts.map +1 -1
  72. package/client/lib/services/assessment/modules/securityTests/SecurityPatternLibrary.js +174 -0
  73. package/client/lib/services/assessment/modules/securityTests/SecurityPayloadTester.d.ts.map +1 -1
  74. package/client/lib/services/assessment/modules/securityTests/SecurityPayloadTester.js +15 -0
  75. package/client/lib/services/assessment/modules/securityTests/SecurityResponseAnalyzer.d.ts +40 -0
  76. package/client/lib/services/assessment/modules/securityTests/SecurityResponseAnalyzer.d.ts.map +1 -1
  77. package/client/lib/services/assessment/modules/securityTests/SecurityResponseAnalyzer.js +143 -131
  78. package/client/package.json +1 -1
  79. package/package.json +1 -1
  80. package/server/build/__tests__/helpers.test.js +3 -0
  81. package/server/build/__tests__/security.test.js +3 -0
  82. package/server/package.json +1 -1
@@ -14,7 +14,7 @@
14
14
  *
15
15
  * @see https://github.com/triepod-ai/inspector-assessment/issues/97
16
16
  */
17
- import { describe, it, expect, beforeAll, afterAll } from "@jest/globals";
17
+ import { jest, describe, it, expect, beforeAll, afterAll, afterEach, } from "@jest/globals";
18
18
  import { spawn } from "child_process";
19
19
  import * as fs from "fs";
20
20
  import * as path from "path";
@@ -76,6 +76,10 @@ async function spawnCLI(args, timeout = 60000) {
76
76
  // Set timeout
77
77
  const timer = setTimeout(() => {
78
78
  if (proc && !proc.killed) {
79
+ // Destroy streams before killing to prevent memory leaks
80
+ proc.stdout?.destroy();
81
+ proc.stderr?.destroy();
82
+ proc.stdin?.destroy();
79
83
  proc.kill("SIGTERM");
80
84
  exitCode = -1; // Indicate timeout
81
85
  }
@@ -83,6 +87,10 @@ async function spawnCLI(args, timeout = 60000) {
83
87
  // Handle process exit
84
88
  proc.on("close", (code) => {
85
89
  clearTimeout(timer);
90
+ // Destroy streams to prevent memory leaks
91
+ proc?.stdout?.destroy();
92
+ proc?.stderr?.destroy();
93
+ proc?.stdin?.destroy();
86
94
  // Don't overwrite timeout exit code (-1)
87
95
  if (exitCode !== -1) {
88
96
  exitCode = code;
@@ -100,6 +108,10 @@ async function spawnCLI(args, timeout = 60000) {
100
108
  // Handle errors
101
109
  proc.on("error", (err) => {
102
110
  clearTimeout(timer);
111
+ // Destroy streams to prevent memory leaks
112
+ proc?.stdout?.destroy();
113
+ proc?.stderr?.destroy();
114
+ proc?.stdin?.destroy();
103
115
  stderr += `\nProcess error: ${err.message}`;
104
116
  resolve({
105
117
  stdout,
@@ -151,10 +163,10 @@ function parseJSONLEvents(stderr) {
151
163
  * @returns True if server responds, false otherwise
152
164
  */
153
165
  async function checkServerAvailable(url) {
166
+ const controller = new AbortController();
167
+ // Give enough time to receive initial response but not wait forever
168
+ const timeoutId = setTimeout(() => controller.abort(), 5000);
154
169
  try {
155
- const controller = new AbortController();
156
- // Give enough time to receive initial response but not wait forever
157
- const timeoutId = setTimeout(() => controller.abort(), 5000);
158
170
  const response = await fetch(url, {
159
171
  method: "POST",
160
172
  headers: DEFAULT_HEADERS,
@@ -172,26 +184,22 @@ async function checkServerAvailable(url) {
172
184
  });
173
185
  // Server responded with a status code - check if it's OK
174
186
  if (response.status >= 500) {
175
- clearTimeout(timeoutId);
176
187
  return false;
177
188
  }
178
189
  // For SSE responses, check if we can read any data
179
190
  // This confirms the server is actually responding
180
191
  const reader = response.body?.getReader();
181
192
  if (!reader) {
182
- clearTimeout(timeoutId);
183
193
  return response.status < 500;
184
194
  }
185
195
  try {
186
196
  // Try to read the first chunk
187
197
  const { done, value } = await reader.read();
188
- clearTimeout(timeoutId);
189
198
  reader.cancel(); // Cancel the stream - we don't need more data
190
199
  // If we got any data, the server is available
191
200
  return !done && value && value.length > 0;
192
201
  }
193
202
  catch {
194
- clearTimeout(timeoutId);
195
203
  // If read fails after successful fetch, server still responded
196
204
  return true;
197
205
  }
@@ -199,6 +207,11 @@ async function checkServerAvailable(url) {
199
207
  catch {
200
208
  return false;
201
209
  }
210
+ finally {
211
+ // Always clean up timeout and abort controller
212
+ clearTimeout(timeoutId);
213
+ controller.abort();
214
+ }
202
215
  }
203
216
  /**
204
217
  * Create a temporary config file for testing
@@ -235,9 +248,22 @@ function createInvalidConfig(content, filename) {
235
248
  return configPath;
236
249
  }
237
250
  // ============================================================================
251
+ // E2E Skip Check
252
+ // ============================================================================
253
+ /**
254
+ * Skip E2E tests unless RUN_E2E_TESTS is set.
255
+ * This prevents long timeouts when testbed servers aren't running.
256
+ *
257
+ * To run E2E tests: RUN_E2E_TESTS=1 npm test -- --testPathPattern="e2e"
258
+ */
259
+ const describeE2E = process.env.RUN_E2E_TESTS ? describe : describe.skip;
260
+ // ============================================================================
238
261
  // Test Setup
239
262
  // ============================================================================
240
- describe("CLI E2E Integration Tests", () => {
263
+ describeE2E("CLI E2E Integration Tests", () => {
264
+ afterEach(() => {
265
+ jest.clearAllMocks();
266
+ });
241
267
  let vulnerableAvailable = false;
242
268
  let hardenedAvailable = false;
243
269
  beforeAll(async () => {
@@ -6,13 +6,16 @@
6
6
  * external dependencies. For integration testing of the full CLI, use the
7
7
  * actual CLI binary.
8
8
  */
9
- import { describe, it, expect } from "@jest/globals";
9
+ import { jest, describe, it, expect, afterEach } from "@jest/globals";
10
10
  import * as path from "path";
11
11
  /**
12
12
  * Pure function tests - these test logic concepts used in the CLI
13
13
  * without needing to import the actual module (which has side effects)
14
14
  */
15
15
  describe("CLI Argument Parsing Concepts", () => {
16
+ afterEach(() => {
17
+ jest.clearAllMocks();
18
+ });
16
19
  describe("Profile Flag Parsing", () => {
17
20
  const VALID_PROFILES = ["quick", "security", "compliance", "full"];
18
21
  function parseProfile(args) {
@@ -3,7 +3,7 @@
3
3
  *
4
4
  * Tests for runFullAssessment() orchestration logic.
5
5
  */
6
- import { jest, describe, it, expect, beforeEach, afterEach, } from "@jest/globals";
6
+ import { jest, describe, it, expect, beforeEach, afterEach, afterAll, } from "@jest/globals";
7
7
  // Mock all dependencies with explicit any types for flexibility
8
8
  const mockLoadServerConfig = jest.fn();
9
9
  const mockConnectToServer = jest.fn();
@@ -137,6 +137,19 @@ describe("runFullAssessment", () => {
137
137
  afterEach(() => {
138
138
  jest.restoreAllMocks();
139
139
  });
140
+ afterAll(() => {
141
+ // Clean up module mocks to prevent memory leaks
142
+ jest.unmock("../../lib/assessment-runner/server-config.js");
143
+ jest.unmock("../../lib/assessment-runner/server-connection.js");
144
+ jest.unmock("../../lib/assessment-runner/source-loader.js");
145
+ jest.unmock("../../lib/assessment-runner/tool-wrapper.js");
146
+ jest.unmock("../../lib/assessment-runner/config-builder.js");
147
+ jest.unmock("../../../../client/lib/services/assessment/AssessmentOrchestrator.js");
148
+ jest.unmock("../../assessmentState.js");
149
+ jest.unmock("../../lib/jsonl-events.js");
150
+ jest.unmock("fs");
151
+ jest.unmock("../../../../client/lib/lib/assessmentTypes.js");
152
+ });
140
153
  describe("orchestration flow", () => {
141
154
  it("should load server config", async () => {
142
155
  await runFullAssessment(defaultOptions);
@@ -203,7 +216,7 @@ describe("runFullAssessment", () => {
203
216
  ...defaultOptions,
204
217
  sourceCodePath: "/path/to/source",
205
218
  });
206
- expect(mockLoadSourceFiles).toHaveBeenCalledWith("/path/to/source");
219
+ expect(mockLoadSourceFiles).toHaveBeenCalledWith("/path/to/source", undefined);
207
220
  });
208
221
  it("should not load source files when path does not exist", async () => {
209
222
  const fs = await import("fs");
@@ -62,6 +62,12 @@ describe("buildConfig", () => {
62
62
  afterEach(() => {
63
63
  process.env = originalEnv;
64
64
  });
65
+ afterAll(() => {
66
+ jest.unmock("../../profiles.js");
67
+ jest.unmock("../../../../client/lib/lib/assessmentTypes.js");
68
+ jest.unmock("../../../../client/lib/services/assessment/lib/claudeCodeBridge.js");
69
+ jest.unmock("../../../../client/lib/services/assessment/config/performanceConfig.js");
70
+ });
65
71
  describe("default configuration", () => {
66
72
  it("should spread DEFAULT_ASSESSMENT_CONFIG", () => {
67
73
  const result = buildConfig({ serverName: "test" });
@@ -3,29 +3,34 @@
3
3
  *
4
4
  * Tests for the facade module exports.
5
5
  */
6
- import { describe, it, expect } from "@jest/globals";
6
+ import { jest, describe, it, expect, afterEach } from "@jest/globals";
7
7
  // Import the barrel/facade module
8
8
  import * as assessmentRunner from "../../lib/assessment-runner/index.js";
9
9
  describe("assessment-runner index exports", () => {
10
+ afterEach(() => {
11
+ jest.clearAllMocks();
12
+ });
10
13
  describe("function exports", () => {
11
- it("should export all 6 public functions", () => {
14
+ it("should export all 7 public functions", () => {
12
15
  expect(typeof assessmentRunner.loadServerConfig).toBe("function");
13
16
  expect(typeof assessmentRunner.loadSourceFiles).toBe("function");
17
+ expect(typeof assessmentRunner.resolveSourcePath).toBe("function");
14
18
  expect(typeof assessmentRunner.connectToServer).toBe("function");
15
19
  expect(typeof assessmentRunner.createCallToolWrapper).toBe("function");
16
20
  expect(typeof assessmentRunner.buildConfig).toBe("function");
17
21
  expect(typeof assessmentRunner.runFullAssessment).toBe("function");
18
22
  });
19
- it("should export exactly 6 functions", () => {
23
+ it("should export exactly 7 functions", () => {
20
24
  const functionNames = Object.keys(assessmentRunner).filter((key) => typeof assessmentRunner[key] ===
21
25
  "function");
22
- expect(functionNames).toHaveLength(6);
26
+ expect(functionNames).toHaveLength(7);
23
27
  expect(functionNames.sort()).toEqual([
24
28
  "buildConfig",
25
29
  "connectToServer",
26
30
  "createCallToolWrapper",
27
31
  "loadServerConfig",
28
32
  "loadSourceFiles",
33
+ "resolveSourcePath",
29
34
  "runFullAssessment",
30
35
  ]);
31
36
  });
@@ -0,0 +1,112 @@
1
+ /**
2
+ * Path Resolver Unit Tests
3
+ *
4
+ * Tests for resolveSourcePath() that handles path normalization.
5
+ */
6
+ import { jest, describe, it, expect, beforeEach, afterAll, } from "@jest/globals";
7
+ import * as path from "path";
8
+ import * as os from "os";
9
+ // Mock fs module
10
+ jest.unstable_mockModule("fs", () => ({
11
+ existsSync: jest.fn(),
12
+ realpathSync: jest.fn(),
13
+ }));
14
+ // Import after mocking
15
+ const fs = await import("fs");
16
+ const { resolveSourcePath } = await import("../../lib/assessment-runner/path-resolver.js");
17
+ describe("resolveSourcePath", () => {
18
+ const mockExistsSync = fs.existsSync;
19
+ const mockRealpathSync = fs.realpathSync;
20
+ beforeEach(() => {
21
+ mockExistsSync.mockReturnValue(false);
22
+ mockRealpathSync.mockReturnValue("");
23
+ });
24
+ afterEach(() => {
25
+ jest.clearAllMocks();
26
+ });
27
+ afterAll(() => {
28
+ jest.unmock("fs");
29
+ });
30
+ describe("tilde expansion", () => {
31
+ it("should expand ~ to home directory", () => {
32
+ mockExistsSync.mockReturnValue(false);
33
+ const result = resolveSourcePath("~/project");
34
+ expect(result).toBe(path.join(os.homedir(), "project"));
35
+ });
36
+ it("should expand ~/subdir/path correctly", () => {
37
+ mockExistsSync.mockReturnValue(false);
38
+ const result = resolveSourcePath("~/foo/bar/baz");
39
+ expect(result).toBe(path.join(os.homedir(), "foo/bar/baz"));
40
+ });
41
+ it("should not modify paths without tilde", () => {
42
+ mockExistsSync.mockReturnValue(false);
43
+ const result = resolveSourcePath("/absolute/path");
44
+ expect(result).toBe("/absolute/path");
45
+ });
46
+ });
47
+ describe("relative path resolution", () => {
48
+ it("should resolve relative paths to absolute", () => {
49
+ mockExistsSync.mockReturnValue(false);
50
+ const cwd = process.cwd();
51
+ const result = resolveSourcePath("./src");
52
+ expect(result).toBe(path.resolve(cwd, "./src"));
53
+ });
54
+ it("should resolve parent directory references", () => {
55
+ mockExistsSync.mockReturnValue(false);
56
+ const cwd = process.cwd();
57
+ const result = resolveSourcePath("../sibling");
58
+ expect(result).toBe(path.resolve(cwd, "../sibling"));
59
+ });
60
+ it("should handle bare directory names", () => {
61
+ mockExistsSync.mockReturnValue(false);
62
+ const cwd = process.cwd();
63
+ const result = resolveSourcePath("mydir");
64
+ expect(result).toBe(path.resolve(cwd, "mydir"));
65
+ });
66
+ });
67
+ describe("symlink resolution", () => {
68
+ it("should follow symlinks when path exists", () => {
69
+ const symlinkPath = "/tmp/symlink";
70
+ const realPath = "/actual/target/path";
71
+ mockExistsSync.mockImplementation((p) => p === symlinkPath);
72
+ mockRealpathSync.mockReturnValue(realPath);
73
+ const result = resolveSourcePath(symlinkPath);
74
+ expect(mockRealpathSync).toHaveBeenCalledWith(symlinkPath);
75
+ expect(result).toBe(realPath);
76
+ });
77
+ it("should not call realpathSync when path does not exist", () => {
78
+ mockExistsSync.mockReturnValue(false);
79
+ resolveSourcePath("/nonexistent/path");
80
+ expect(mockRealpathSync).not.toHaveBeenCalled();
81
+ });
82
+ it("should handle broken symlinks gracefully", () => {
83
+ const brokenSymlink = "/tmp/broken-symlink";
84
+ mockExistsSync.mockReturnValue(true);
85
+ mockRealpathSync.mockImplementation(() => {
86
+ throw new Error("ENOENT: no such file or directory");
87
+ });
88
+ // Should not throw, should return the resolved path without realpath
89
+ const result = resolveSourcePath(brokenSymlink);
90
+ expect(result).toBe(brokenSymlink);
91
+ });
92
+ });
93
+ describe("combined scenarios", () => {
94
+ it("should handle ~ with symlink resolution", () => {
95
+ const tildePathExpanded = path.join(os.homedir(), "project");
96
+ const realPath = "/real/project/path";
97
+ mockExistsSync.mockImplementation((p) => p === tildePathExpanded);
98
+ mockRealpathSync.mockReturnValue(realPath);
99
+ const result = resolveSourcePath("~/project");
100
+ expect(result).toBe(realPath);
101
+ });
102
+ it("should handle relative path with symlink resolution", () => {
103
+ const cwd = process.cwd();
104
+ const resolvedRelative = path.resolve(cwd, "./src");
105
+ const realPath = "/real/src/path";
106
+ mockExistsSync.mockImplementation((p) => p === resolvedRelative);
107
+ mockRealpathSync.mockReturnValue(realPath);
108
+ const result = resolveSourcePath("./src");
109
+ expect(result).toBe(realPath);
110
+ });
111
+ });
112
+ });
@@ -22,6 +22,9 @@ describe("loadServerConfig", () => {
22
22
  jest.clearAllMocks();
23
23
  mockExistsSync.mockReturnValue(false);
24
24
  });
25
+ afterAll(() => {
26
+ jest.unmock("fs");
27
+ });
25
28
  describe("config path resolution", () => {
26
29
  it("should search explicit configPath first when provided", () => {
27
30
  const configPath = "/custom/path/config.json";
@@ -39,10 +39,18 @@ const { StreamableHTTPClientTransport } = await import("@modelcontextprotocol/sd
39
39
  const { connectToServer } = await import("../../lib/assessment-runner/server-connection.js");
40
40
  describe("connectToServer", () => {
41
41
  beforeEach(() => {
42
- jest.clearAllMocks();
43
42
  mockConnect.mockResolvedValue(undefined);
44
43
  mockStdioTransport.stderr.on.mockClear();
45
44
  });
45
+ afterEach(() => {
46
+ jest.clearAllMocks();
47
+ });
48
+ afterAll(() => {
49
+ jest.unmock("@modelcontextprotocol/sdk/client/index.js");
50
+ jest.unmock("@modelcontextprotocol/sdk/client/stdio.js");
51
+ jest.unmock("@modelcontextprotocol/sdk/client/sse.js");
52
+ jest.unmock("@modelcontextprotocol/sdk/client/streamableHttp.js");
53
+ });
46
54
  describe("HTTP transport", () => {
47
55
  it("should create StreamableHTTPClientTransport for transport:http", async () => {
48
56
  const config = {
@@ -34,10 +34,15 @@ describe("loadSourceFiles", () => {
34
34
  const mockReadFileSync = fs.readFileSync;
35
35
  const mockReaddirSync = fs.readdirSync;
36
36
  beforeEach(() => {
37
- jest.clearAllMocks();
38
37
  mockExistsSync.mockReturnValue(false);
39
38
  mockReaddirSync.mockReturnValue([]);
40
39
  });
40
+ afterEach(() => {
41
+ jest.clearAllMocks();
42
+ });
43
+ afterAll(() => {
44
+ jest.unmock("fs");
45
+ });
41
46
  describe("README discovery", () => {
42
47
  it("should find README.md in source directory", () => {
43
48
  const sourcePath = "/project";
@@ -75,6 +80,98 @@ describe("loadSourceFiles", () => {
75
80
  const result = loadSourceFiles(sourcePath);
76
81
  expect(result.readmeContent).toBeUndefined();
77
82
  });
83
+ // Issue #151: Extended README patterns
84
+ it("should find README.markdown", () => {
85
+ const sourcePath = "/project";
86
+ mockExistsSync.mockImplementation((p) => p === path.join(sourcePath, "README.markdown"));
87
+ mockReadFileSync.mockReturnValue("# Markdown README");
88
+ const result = loadSourceFiles(sourcePath);
89
+ expect(result.readmeContent).toBe("# Markdown README");
90
+ });
91
+ it("should find README.txt", () => {
92
+ const sourcePath = "/project";
93
+ mockExistsSync.mockImplementation((p) => p === path.join(sourcePath, "README.txt"));
94
+ mockReadFileSync.mockReturnValue("Text README content");
95
+ const result = loadSourceFiles(sourcePath);
96
+ expect(result.readmeContent).toBe("Text README content");
97
+ });
98
+ it("should find README without extension", () => {
99
+ const sourcePath = "/project";
100
+ mockExistsSync.mockImplementation((p) => p === path.join(sourcePath, "README"));
101
+ mockReadFileSync.mockReturnValue("No extension README");
102
+ const result = loadSourceFiles(sourcePath);
103
+ expect(result.readmeContent).toBe("No extension README");
104
+ });
105
+ it("should prioritize README.md over other variants", () => {
106
+ const sourcePath = "/project";
107
+ // Multiple README files exist - README.md should be found first
108
+ mockExistsSync.mockImplementation((p) => {
109
+ return (p === path.join(sourcePath, "README.md") ||
110
+ p === path.join(sourcePath, "README.txt") ||
111
+ p === path.join(sourcePath, "README"));
112
+ });
113
+ mockReadFileSync.mockImplementation((p) => {
114
+ if (p.endsWith("README.md"))
115
+ return "# MD README";
116
+ if (p.endsWith("README.txt"))
117
+ return "TXT README";
118
+ if (p.endsWith("README"))
119
+ return "Plain README";
120
+ return "";
121
+ });
122
+ const result = loadSourceFiles(sourcePath);
123
+ expect(result.readmeContent).toBe("# MD README");
124
+ });
125
+ });
126
+ describe("debug logging", () => {
127
+ it("should log debug output when debug flag is true", () => {
128
+ const sourcePath = "/project";
129
+ const consoleSpy = jest
130
+ .spyOn(console, "log")
131
+ .mockImplementation(() => { });
132
+ try {
133
+ mockExistsSync.mockReturnValue(false);
134
+ mockReaddirSync.mockReturnValue([]);
135
+ loadSourceFiles(sourcePath, true);
136
+ // Verify debug logging was called
137
+ expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining("[source-loader]"));
138
+ }
139
+ finally {
140
+ consoleSpy.mockRestore();
141
+ }
142
+ });
143
+ it("should not log debug output when debug flag is false", () => {
144
+ const sourcePath = "/project";
145
+ const consoleSpy = jest
146
+ .spyOn(console, "log")
147
+ .mockImplementation(() => { });
148
+ try {
149
+ mockExistsSync.mockReturnValue(false);
150
+ mockReaddirSync.mockReturnValue([]);
151
+ loadSourceFiles(sourcePath, false);
152
+ // Verify debug logging was NOT called
153
+ expect(consoleSpy).not.toHaveBeenCalledWith(expect.stringContaining("[source-loader]"));
154
+ }
155
+ finally {
156
+ consoleSpy.mockRestore();
157
+ }
158
+ });
159
+ it("should not log debug output by default", () => {
160
+ const sourcePath = "/project";
161
+ const consoleSpy = jest
162
+ .spyOn(console, "log")
163
+ .mockImplementation(() => { });
164
+ try {
165
+ mockExistsSync.mockReturnValue(false);
166
+ mockReaddirSync.mockReturnValue([]);
167
+ loadSourceFiles(sourcePath);
168
+ // Verify debug logging was NOT called by default
169
+ expect(consoleSpy).not.toHaveBeenCalledWith(expect.stringContaining("[source-loader]"));
170
+ }
171
+ finally {
172
+ consoleSpy.mockRestore();
173
+ }
174
+ });
78
175
  });
79
176
  describe("package.json parsing", () => {
80
177
  it("should parse package.json when present", () => {
@@ -118,13 +215,17 @@ describe("loadSourceFiles", () => {
118
215
  const consoleSpy = jest
119
216
  .spyOn(console, "warn")
120
217
  .mockImplementation(() => { });
121
- mockExistsSync.mockImplementation((p) => p === path.join(sourcePath, "manifest.json"));
122
- mockReadFileSync.mockReturnValue("{ invalid json }");
123
- const result = loadSourceFiles(sourcePath);
124
- expect(result.manifestRaw).toBe("{ invalid json }");
125
- expect(result.manifestJson).toBeUndefined();
126
- expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining("Failed to parse manifest.json"));
127
- consoleSpy.mockRestore();
218
+ try {
219
+ mockExistsSync.mockImplementation((p) => p === path.join(sourcePath, "manifest.json"));
220
+ mockReadFileSync.mockReturnValue("{ invalid json }");
221
+ const result = loadSourceFiles(sourcePath);
222
+ expect(result.manifestRaw).toBe("{ invalid json }");
223
+ expect(result.manifestJson).toBeUndefined();
224
+ expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining("Failed to parse manifest.json"));
225
+ }
226
+ finally {
227
+ consoleSpy.mockRestore();
228
+ }
128
229
  });
129
230
  });
130
231
  describe("source file collection", () => {
@@ -276,14 +377,18 @@ describe("loadSourceFiles", () => {
276
377
  const consoleSpy = jest
277
378
  .spyOn(console, "warn")
278
379
  .mockImplementation(() => { });
279
- mockExistsSync.mockReturnValue(false);
280
- mockReaddirSync.mockImplementation(() => {
281
- throw new Error("Permission denied");
282
- });
283
- const result = loadSourceFiles(sourcePath);
284
- expect(result.sourceCodeFiles?.size).toBe(0);
285
- expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining("Could not load source files"), expect.any(Error));
286
- consoleSpy.mockRestore();
380
+ try {
381
+ mockExistsSync.mockReturnValue(false);
382
+ mockReaddirSync.mockImplementation(() => {
383
+ throw new Error("Permission denied");
384
+ });
385
+ const result = loadSourceFiles(sourcePath);
386
+ expect(result.sourceCodeFiles?.size).toBe(0);
387
+ expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining("Could not load source files"), expect.any(Error));
388
+ }
389
+ finally {
390
+ consoleSpy.mockRestore();
391
+ }
287
392
  });
288
393
  it("should skip unreadable files silently", () => {
289
394
  const sourcePath = "/project";
@@ -15,6 +15,9 @@ describe("createCallToolWrapper", () => {
15
15
  callTool: mockCallTool,
16
16
  };
17
17
  });
18
+ afterEach(() => {
19
+ jest.clearAllMocks();
20
+ });
18
21
  describe("successful tool calls", () => {
19
22
  it("should wrap successful tool response with content array", async () => {
20
23
  mockCallTool.mockResolvedValue({
@@ -6,12 +6,15 @@
6
6
  *
7
7
  * @see https://github.com/triepod-ai/inspector-assessment/issues/96
8
8
  */
9
- import { describe, it, expect } from "@jest/globals";
9
+ import { jest, describe, it, expect, afterEach } from "@jest/globals";
10
10
  // Test named imports (the primary consumer pattern)
11
11
  import { loadServerConfig, loadSourceFiles, connectToServer, createCallToolWrapper, buildConfig, runFullAssessment, } from "../lib/assessment-runner.js";
12
12
  // Test namespace import
13
13
  import * as AssessmentRunner from "../lib/assessment-runner.js";
14
14
  describe("Assessment Runner Facade", () => {
15
+ afterEach(() => {
16
+ jest.clearAllMocks();
17
+ });
15
18
  describe("Function Exports", () => {
16
19
  it("should export loadServerConfig function", () => {
17
20
  expect(typeof loadServerConfig).toBe("function");
@@ -81,13 +84,14 @@ describe("Assessment Runner Facade", () => {
81
84
  describe("Export Completeness", () => {
82
85
  it("should export exactly 6 functions", () => {
83
86
  const exportedFunctions = Object.entries(AssessmentRunner).filter(([, value]) => typeof value === "function");
84
- expect(exportedFunctions.length).toBe(6);
87
+ expect(exportedFunctions.length).toBe(7);
85
88
  expect(exportedFunctions.map(([name]) => name).sort()).toEqual([
86
89
  "buildConfig",
87
90
  "connectToServer",
88
91
  "createCallToolWrapper",
89
92
  "loadServerConfig",
90
93
  "loadSourceFiles",
94
+ "resolveSourcePath",
91
95
  "runFullAssessment",
92
96
  ]);
93
97
  });
@@ -96,6 +100,7 @@ describe("Assessment Runner Facade", () => {
96
100
  const expectedExports = [
97
101
  "loadServerConfig",
98
102
  "loadSourceFiles",
103
+ "resolveSourcePath",
99
104
  "connectToServer",
100
105
  "createCallToolWrapper",
101
106
  "buildConfig",
@@ -8,9 +8,12 @@
8
8
  * @see https://github.com/triepod-ai/inspector-assessment/issues/33
9
9
  * @see https://github.com/triepod-ai/inspector-assessment/issues/37
10
10
  */
11
- import { describe, it, expect } from "@jest/globals";
11
+ import { jest, describe, it, expect, afterEach } from "@jest/globals";
12
12
  import { ScopedListenerConfig } from "../lib/event-config.js";
13
13
  describe("CLI Build Fixes Regression Tests", () => {
14
+ afterEach(() => {
15
+ jest.clearAllMocks();
16
+ });
14
17
  describe("event-config.ts - CLI_DEFAULT_MAX_LISTENERS constant", () => {
15
18
  it("should use local constant instead of cross-workspace import", () => {
16
19
  // Fix: Replaced DEFAULT_PERFORMANCE_CONFIG.eventEmitterMaxListeners
@@ -669,8 +669,9 @@ describe("parseArgs Zod Schema Integration", () => {
669
669
  // Run any pending timers and restore
670
670
  jest.runAllTimers();
671
671
  jest.useRealTimers();
672
- processExitSpy.mockRestore();
673
- consoleErrorSpy.mockRestore();
672
+ // Use optional chaining in case spies weren't created (prevents memory leaks)
673
+ processExitSpy?.mockRestore();
674
+ consoleErrorSpy?.mockRestore();
674
675
  });
675
676
  describe("LogLevelSchema integration", () => {
676
677
  it("parseArgs validates log level with LogLevelSchema", () => {