eigent/test/unit/lib/workspaceRelativePath.test.ts
Douglas Lai b439d5ff23
Feat/refactor session side panel (#1853)
Co-authored-by: 4pmtong <web_chentong@163.com>
2026-08-19 01:28:23 +08:00

97 lines
3.2 KiB
TypeScript

// ========= Copyright 2025-2026 @ Eigent.ai All Rights Reserved. =========
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ========= Copyright 2025-2026 @ Eigent.ai All Rights Reserved. =========
import {
getWorkspaceRelativeFilePath,
normalizeWorkspaceRelativePath,
} from '@/lib/workspaceRelativePath';
import { describe, expect, it } from 'vitest';
describe('getWorkspaceRelativeFilePath', () => {
it('shows a normalized path relative to the workspace root', () => {
expect(
getWorkspaceRelativeFilePath({
name: 'final_report.md',
type: 'md',
path: '/Users/test/workspace/reports/final_report.md',
relativePath: './research\\reports\\final_report.md',
})
).toBe('research/reports/final_report.md');
});
it('appends the file name when relativePath identifies only the directory', () => {
expect(
getWorkspaceRelativeFilePath({
name: 'final_report.md',
type: 'md',
path: '/Users/test/workspace/reports/final_report.md',
relativePath: 'reports',
})
).toBe('reports/final_report.md');
});
it('never falls back to an absolute path when a file name is available', () => {
expect(
getWorkspaceRelativeFilePath({
name: 'final_report.md',
type: 'md',
path: '/Users/test/workspace/reports/final_report.md',
})
).toBe('final_report.md');
});
it('rejects an absolute path accidentally placed in relativePath', () => {
expect(
getWorkspaceRelativeFilePath({
name: 'report.md',
type: 'md',
path: '/Users/test/workspace/report.md',
relativePath: '/Users/test/workspace/report.md',
})
).toBe('report.md');
});
it('rejects a relativePath that escapes the workspace root', () => {
expect(
getWorkspaceRelativeFilePath({
name: 'secret.txt',
type: 'txt',
path: '/Users/test/secret.txt',
relativePath: '../secret.txt',
})
).toBe('secret.txt');
});
it('does not display remote preview URLs', () => {
expect(
getWorkspaceRelativeFilePath({
name: '',
type: 'csv',
path: 'https://example.com/files/stream?path=reports%2Fdata.csv',
})
).toBe('stream');
});
it('normalizes only safe workspace-relative identities', () => {
expect(normalizeWorkspaceRelativePath('./reports\\final.md')).toBe(
'reports/final.md'
);
expect(normalizeWorkspaceRelativePath('/workspace/final.md')).toBeNull();
expect(
normalizeWorkspaceRelativePath('https://example.test/final.md')
).toBeNull();
expect(normalizeWorkspaceRelativePath('%2e%2e/secret.txt')).toBeNull();
expect(normalizeWorkspaceRelativePath('reports/%2fsecret.txt')).toBeNull();
});
});