-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix case sensitivity, add tests, remove shift-click in tree filters
- Loading branch information
Showing
6 changed files
with
184 additions
and
159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import {LocationTreeNode} from '@shared/types/Taxonomy'; | ||
import TaxonomyService from '../../server/services/taxonomyService'; | ||
import {UserInDatabase} from '@shared/schema/Auth'; | ||
import os from 'os'; | ||
|
||
type LocationRecord = {[key: string]: LocationRecord | null}; | ||
const toTreeNodes = (locations: LocationRecord, separator = '/', basePath = '') => | ||
Object.keys(locations).reduce((parentNodes, locationKey) => { | ||
const fullPath = locationKey !== '' ? basePath + separator + locationKey : locationKey; | ||
const subLocations = locations[locationKey]; | ||
if (subLocations) { | ||
const parent = { | ||
directoryName: locationKey, | ||
fullPath: fullPath, | ||
children: toTreeNodes(subLocations, separator, fullPath), | ||
containedCount: 0, | ||
containedSize: 0, | ||
}; | ||
for (const child of parent.children) { | ||
parent.containedCount += child.containedCount; | ||
parent.containedSize += child.containedSize; | ||
} | ||
parentNodes.push(parent); | ||
} else { | ||
parentNodes.push({ | ||
directoryName: locationKey, | ||
fullPath: fullPath, | ||
children: [], | ||
containedCount: '' !== locationKey ? 1 : 0, | ||
containedSize: '' !== locationKey ? 10 : 0, | ||
}); | ||
} | ||
return parentNodes; | ||
}, [] as LocationTreeNode[]); | ||
|
||
describe('taxonomyService', () => { | ||
describe('incrementLocationCountsAndSizes() - locationTree', () => { | ||
for (const locationsAndExpected of [ | ||
// No torrents | ||
{locations: [] as string[], expected: toTreeNodes({'': null})[0]}, | ||
// Single root | ||
{ | ||
locations: ['/mnt/dir1/file1', '/mnt/dir1/file2', '/mnt/dir2/file3', '/mnt/file4'], | ||
expected: toTreeNodes({ | ||
'': { | ||
mnt: {dir1: {file1: null, file2: null}, dir2: {file3: null}, file4: null}, | ||
}, | ||
})[0], | ||
}, | ||
// Multiple roots including overlapping case | ||
{ | ||
locations: ['/mnt/file1', '/mnt/file2', '/mount/directory1/file3', '/Mount/directory2/file4'], | ||
expected: toTreeNodes({ | ||
'': { | ||
mnt: {file1: null, file2: null}, | ||
mount: {directory1: {file3: null}}, | ||
Mount: {directory2: {file4: null}}, | ||
}, | ||
})[0], | ||
}, | ||
]) { | ||
const {locations, expected} = locationsAndExpected; | ||
|
||
it(`builds Linux-style case-sensitive location tree correctly from ${locations}`, () => { | ||
const taxonomyService = new TaxonomyService({} as UserInDatabase); | ||
const mock = jest.spyOn(os, 'platform'); | ||
mock.mockImplementation(() => 'linux'); | ||
|
||
for (const location of locations) taxonomyService.incrementLocationCountsAndSizes(location, 10); | ||
|
||
expect(taxonomyService.taxonomy.locationTree).toMatchObject(expected); | ||
|
||
mock.mockRestore(); | ||
}); | ||
} | ||
|
||
for (const locationsAndExpected of [ | ||
// Multiple roots including overlapping case | ||
{ | ||
locations: ['/mnt/file1', '/mnt/file2', '/mount/directory1/file3', '/Mount/directory2/file4'], | ||
expected: toTreeNodes({ | ||
'': { | ||
mnt: {file1: null, file2: null}, | ||
mount: {directory1: {file3: null}, directory2: {file4: null}}, | ||
}, | ||
})[0], | ||
}, | ||
]) { | ||
const {locations, expected} = locationsAndExpected; | ||
|
||
it(`builds Mac-style case-insensitive location tree correctly from ${locations}`, () => { | ||
const taxonomyService = new TaxonomyService({} as UserInDatabase); | ||
const mock = jest.spyOn(os, 'platform'); | ||
mock.mockImplementation(() => 'darwin'); | ||
|
||
for (const location of locations) taxonomyService.incrementLocationCountsAndSizes(location, 10); | ||
|
||
expect(taxonomyService.taxonomy.locationTree).toMatchObject(expected); | ||
|
||
mock.mockRestore(); | ||
}); | ||
} | ||
}); | ||
}); |
Oops, something went wrong.