|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2025 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import { describe, it, expect } from 'vitest'; |
| 8 | +import { generateWorkspacePathError } from './workspace-error-helper.js'; |
| 9 | + |
| 10 | +describe('generateWorkspacePathError', () => { |
| 11 | + const workspaceDirs = ['/Users/user/project']; |
| 12 | + |
| 13 | + it('should return a standard error message for normal paths outside workspace', () => { |
| 14 | + const filePath = '/etc/passwd'; |
| 15 | + const result = generateWorkspacePathError(filePath, workspaceDirs); |
| 16 | + |
| 17 | + expect(result).toContain('File path is outside the workspace'); |
| 18 | + expect(result).toContain(`Requested path: ${filePath}`); |
| 19 | + expect(result).not.toContain('ERROR: Path Hallucination Detected'); |
| 20 | + }); |
| 21 | + |
| 22 | + it('should detect hallucinated path for "suyashpradhan"', () => { |
| 23 | + const filePath = '/Users/suyashpradhan/Desktop/project/file.ts'; |
| 24 | + const result = generateWorkspacePathError(filePath, workspaceDirs); |
| 25 | + |
| 26 | + expect(result).toContain('ERROR: Path Hallucination Detected'); |
| 27 | + expect(result).toContain('CRITICAL: You are attempting to use a path from your training data'); |
| 28 | + expect(result).toContain('Your current PROJECT ROOT is: /Users/user/project'); |
| 29 | + }); |
| 30 | + |
| 31 | + it('should detect generic unknown user paths (e.g., "sarah") as hallucination', () => { |
| 32 | + const filePath = '/Users/sarah/Documents/data.csv'; |
| 33 | + const result = generateWorkspacePathError(filePath, workspaceDirs); |
| 34 | + |
| 35 | + expect(result).toContain('ERROR: Path Hallucination Detected'); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should detect Windows unknown user paths as hallucination', () => { |
| 39 | + const winWorkspace = ['C:\\Users\\Eli\\Project']; |
| 40 | + const filePath = 'C:\\Users\\John\\Desktop\\file.txt'; |
| 41 | + const result = generateWorkspacePathError(filePath, winWorkspace); |
| 42 | + |
| 43 | + expect(result).toContain('ERROR: Path Hallucination Detected'); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should detect Linux unknown user paths as hallucination', () => { |
| 47 | + const linuxWorkspace = ['/home/ubuntu/project']; |
| 48 | + const filePath = '/home/suyash/data.csv'; |
| 49 | + const result = generateWorkspacePathError(filePath, linuxWorkspace); |
| 50 | + |
| 51 | + expect(result).toContain('ERROR: Path Hallucination Detected'); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should NOT flag a hallucination if the project is in a folder named after a friend', () => { |
| 55 | + // Scenario: User is 'eli', but has a project in a folder called 'richard_project' |
| 56 | + const workspace = ['/Users/eli/Documents/richard_project']; |
| 57 | + const filePath = '/Users/eli/Documents/richard_project/data.csv'; |
| 58 | + |
| 59 | + // This is just a normal out-of-workspace error (or valid if inside) |
| 60 | + // But importantly, it's NOT a hallucination of the 'richard' user. |
| 61 | + const result = generateWorkspacePathError(filePath, workspace); |
| 62 | + expect(result).not.toContain('ERROR: Path Hallucination Detected'); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should NOT flag system paths (like /tmp) as hallucinations', () => { |
| 66 | + const workspace = ['/Users/user/project']; |
| 67 | + const filePath = '/tmp/debug.log'; |
| 68 | + const result = generateWorkspacePathError(filePath, workspace); |
| 69 | + |
| 70 | + expect(result).toContain('File path is outside the workspace'); |
| 71 | + expect(result).not.toContain('ERROR: Path Hallucination Detected'); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should detect hallucinated path for "blackbox-cli"', () => { |
| 75 | + const filePath = '/some/path/blackbox-cli/package.json'; |
| 76 | + const result = generateWorkspacePathError(filePath, workspaceDirs); |
| 77 | + |
| 78 | + expect(result).toContain('ERROR: Path Hallucination Detected'); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should NOT detect "suyashpradhan" as hallucination if it is part of the actual workspace', () => { |
| 82 | + const realWorkspace = ['/Users/suyashpradhan/my-docs']; |
| 83 | + const filePath = '/Users/suyashpradhan/other-file.ts'; |
| 84 | + const result = generateWorkspacePathError(filePath, realWorkspace); |
| 85 | + |
| 86 | + expect(result).toContain('File path is outside the workspace'); |
| 87 | + expect(result).not.toContain('ERROR: Path Hallucination Detected'); |
| 88 | + }); |
| 89 | +}); |
0 commit comments