Skip to content

TagSpaces is an offline, open source, document manager with tagging support

chore: added tests for path handling

Ilian Sapundshiev20543281daa8
diff --git a/src/renderer/utils/pathScoping.ts b/src/renderer/utils/pathScoping.ts
new file mode 100644
index 000000000..74f719517
--- /dev/null
+++ b/src/renderer/utils/pathScoping.ts
@@ -0,0 +1,73 @@
+/**
+ * TagSpaces - universal file and folder organizer
+ * Copyright (C) 2024-present TagSpaces GmbH
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License (version 3) as
+ * published by the Free Software Foundation.
+ */
+
+import {
+ cleanFrontDirSeparator,
+ cleanTrailingDirSeparator,
+} from '@tagspaces/tagspaces-common/paths';
+
+/**
+ * Normalize a path for scope comparisons.
+ *
+ * The enhanced index paths produced by `enhanceDirectoryIndex` (via
+ * `joinPaths(sep, folderPath, relPath)`) have a leading separator. But
+ * `location.path` / `currentDirectory.path` typically have no leading slash
+ * and may have a trailing one (e.g. an S3 location configured as
+ * `'my-folder/'`). Naive `.startsWith(location.path)` then silently drops
+ * every entry.
+ *
+ * This helper strips BOTH the leading and trailing separator, and
+ * normalizes backslashes to forward slashes so Windows and POSIX paths
+ * compare consistently.
+ */
+export function normalizePathForCompare(p: string | undefined | null): string {
+ return cleanTrailingDirSeparator(
+ cleanFrontDirSeparator((p || '').replaceAll('\\', '/')),
+ );
+}
+
+/**
+ * Returns true when `itemPath` is the same as `rootPath` or lives underneath
+ * it, using a separator boundary so `foo` does not match `foobar`.
+ *
+ * An empty `rootPath` (bucket-root S3 location, or unset scope) is treated
+ * as "everything is within scope" so callers can use this helper uniformly
+ * without special-casing the root.
+ */
+export function isPathWithinRoot(
+ itemPath: string | undefined | null,
+ rootPath: string | undefined | null,
+): boolean {
+ const rootNorm = normalizePathForCompare(rootPath);
+ if (!rootNorm) return true;
+ const itemNorm = normalizePathForCompare(itemPath);
+ return itemNorm === rootNorm || itemNorm.startsWith(rootNorm + '/');
+}
+
+/**
+ * Returns the portion of `itemPath` relative to `rootPath` (without a
+ * leading separator). If `itemPath` is exactly `rootPath`, returns `''`.
+ * If `rootPath` is empty, returns the normalized `itemPath` as-is.
+ * If `itemPath` is NOT within `rootPath`, returns the normalized
+ * `itemPath` as-is as a best-effort — callers that care should guard
+ * with `isPathWithinRoot` first.
+ */
+export function relativePathFromRoot(
+ itemPath: string | undefined | null,
+ rootPath: string | undefined | null,
+): string {
+ const itemNorm = normalizePathForCompare(itemPath);
+ const rootNorm = normalizePathForCompare(rootPath);
+ if (!rootNorm) return itemNorm;
+ if (itemNorm === rootNorm) return '';
+ if (itemNorm.startsWith(rootNorm + '/')) {
+ return itemNorm.slice(rootNorm.length + 1);
+ }
+ return itemNorm;
+}
diff --git a/tests/unit/pathScoping.test.js b/tests/unit/pathScoping.test.js
new file mode 100644
index 000000000..ca9844784
--- /dev/null
+++ b/tests/unit/pathScoping.test.js
@@ -0,0 +1,114 @@
+import { describe, expect, test } from '@playwright/test';
+
+import {
+ isPathWithinRoot,
+ normalizePathForCompare,
+ relativePathFromRoot,
+} from '-/utils/pathScoping';
+
+describe('normalizePathForCompare', () => {
+ test('strips leading and trailing forward slashes', () => {
+ expect(normalizePathForCompare('/demo/')).toBe('demo');
+ expect(normalizePathForCompare('demo/')).toBe('demo');
+ expect(normalizePathForCompare('/demo')).toBe('demo');
+ expect(normalizePathForCompare('demo')).toBe('demo');
+ });
+
+ test('normalizes backslashes to forward slashes', () => {
+ expect(normalizePathForCompare('C:\\Users\\me')).toBe('C:/Users/me');
+ expect(normalizePathForCompare('\\\\share\\folder\\')).toBe(
+ '/share/folder',
+ );
+ });
+
+ test('handles empty / null / undefined', () => {
+ expect(normalizePathForCompare('')).toBe('');
+ expect(normalizePathForCompare(null)).toBe('');
+ expect(normalizePathForCompare(undefined)).toBe('');
+ });
+});
+
+describe('isPathWithinRoot', () => {
+ test('bucket-root S3 (empty rootPath) accepts any item', () => {
+ expect(isPathWithinRoot('file.txt', '')).toBe(true);
+ expect(isPathWithinRoot('/file.txt', '')).toBe(true);
+ expect(isPathWithinRoot('sub/file.txt', '')).toBe(true);
+ expect(isPathWithinRoot('/sub/file.txt', null)).toBe(true);
+ expect(isPathWithinRoot('anything', undefined)).toBe(true);
+ });
+
+ test('S3 with path prefix — leading slash on item, trailing slash on root', () => {
+ // This is the concrete case that bit the perspective filters.
+ expect(isPathWithinRoot('/demo/Bookmarks', 'demo/')).toBe(true);
+ expect(isPathWithinRoot('/demo/sub/file.txt', 'demo/')).toBe(true);
+ expect(isPathWithinRoot('/demo', 'demo/')).toBe(true);
+ expect(isPathWithinRoot('/other/x', 'demo/')).toBe(false);
+ });
+
+ test('enforces separator boundary — "foo" does not match "foobar"', () => {
+ expect(isPathWithinRoot('demofoo/x', 'demo')).toBe(false);
+ expect(isPathWithinRoot('demofoo', 'demo')).toBe(false);
+ expect(isPathWithinRoot('demo/foo', 'demo')).toBe(true);
+ expect(isPathWithinRoot('demo', 'demo')).toBe(true);
+ });
+
+ test('POSIX absolute paths (Mac / Linux)', () => {
+ expect(isPathWithinRoot('/Users/me/docs/file.txt', '/Users/me')).toBe(true);
+ expect(isPathWithinRoot('/Users/me', '/Users/me')).toBe(true);
+ expect(isPathWithinRoot('/Users/foo/docs', '/Users/me')).toBe(false);
+ });
+
+ test('Windows absolute paths (backslash separators)', () => {
+ expect(
+ isPathWithinRoot('C:\\Users\\me\\docs\\file.txt', 'C:\\Users\\me'),
+ ).toBe(true);
+ // Mixed separators after enhance/persist should still match
+ expect(
+ isPathWithinRoot('C:/Users/me/docs/file.txt', 'C:\\Users\\me'),
+ ).toBe(true);
+ expect(
+ isPathWithinRoot('C:\\Users\\foo\\docs', 'C:\\Users\\me'),
+ ).toBe(false);
+ });
+
+ test('subfolder scoping — root narrower than location', () => {
+ // Viewing `demo/sub` — only items under that subfolder pass.
+ expect(isPathWithinRoot('/demo/sub/file.txt', 'demo/sub')).toBe(true);
+ expect(isPathWithinRoot('/demo/sub', 'demo/sub')).toBe(true);
+ expect(isPathWithinRoot('/demo/other/file.txt', 'demo/sub')).toBe(false);
+ expect(isPathWithinRoot('/demo/file.txt', 'demo/sub')).toBe(false);
+ });
+});
+
+describe('relativePathFromRoot', () => {
+ test('returns normalized item as-is when root is empty', () => {
+ expect(relativePathFromRoot('/file.txt', '')).toBe('file.txt');
+ expect(relativePathFromRoot('sub/file.txt', null)).toBe('sub/file.txt');
+ });
+
+ test('strips root prefix including the separator', () => {
+ expect(relativePathFromRoot('/demo/Bookmarks', 'demo/')).toBe('Bookmarks');
+ expect(relativePathFromRoot('/demo/sub/file.txt', 'demo/')).toBe(
+ 'sub/file.txt',
+ );
+ });
+
+ test('returns empty string when item equals root', () => {
+ expect(relativePathFromRoot('/demo', 'demo/')).toBe('');
+ expect(relativePathFromRoot('demo', 'demo')).toBe('');
+ });
+
+ test('returns normalized item unchanged when not within root', () => {
+ // Best-effort — callers should pre-check with isPathWithinRoot.
+ expect(relativePathFromRoot('/other/x', 'demo/')).toBe('other/x');
+ });
+
+ test('Windows paths', () => {
+ expect(
+ relativePathFromRoot(
+ 'C:\\Users\\me\\docs\\file.txt',
+ 'C:\\Users\\me',
+ ),
+ ).toBe('docs/file.txt');
+ });
+});