diff --git a/packages/cozy-pouch-link/src/jsonapi.js b/packages/cozy-pouch-link/src/jsonapi.js index 83bb7baf7..1fbc2981b 100644 --- a/packages/cozy-pouch-link/src/jsonapi.js +++ b/packages/cozy-pouch-link/src/jsonapi.js @@ -132,7 +132,6 @@ export const computeFileFullpath = async (client, file) => { // No need to compute directory path: it is always here return file } - if (file.path) { // If a file path exists, check it is complete, i.e. it includes the name. // The stack typically does not include the name in the path, which is useful to search on it @@ -146,17 +145,20 @@ export const computeFileFullpath = async (client, file) => { return file } const filePath = getFilePath(file._id) - if (filePath) { - // File path exists in memory - file.path = filePath + const parentPath = getFilePath(file.dir_id) + if (parentPath && filePath) { + // Check if file path is up to date + const builtPath = buildPathWithName(parentPath, file.name) + if (filePath !== builtPath) { + setFilePath(file._id, builtPath) + } + file.path = builtPath return file } - - const parentPath = getFilePath(file.dir_id) if (parentPath) { - // Parent path exists in memory + // Parent path exists in memory: use it to compute file path and save in memory const path = buildPathWithName(parentPath, file.name) - setFilePath(file._id, path) // Add the path in memory + setFilePath(file._id, path) file.path = path return file } @@ -166,12 +168,12 @@ export const computeFileFullpath = async (client, file) => { logger.warn(`Missing dir_id for file ${file._id}`) return file } - const parentDir = await queryFileById(client, file.dir_id) + const { data: parentDir } = await queryFileById(client, file.dir_id) if (parentDir?.path) { - const path = buildPathWithName(parentDir?.path, file.name) + const path = buildPathWithName(parentDir.path, file.name) file.path = path - // Add the paths in memory + // Add the computed paths in memory setFilePath(file.dir_id, parentDir.path) setFilePath(file._id, path) } diff --git a/packages/cozy-pouch-link/src/jsonapi.spec.js b/packages/cozy-pouch-link/src/jsonapi.spec.js index e6c1e9e19..1b18719c9 100644 --- a/packages/cozy-pouch-link/src/jsonapi.spec.js +++ b/packages/cozy-pouch-link/src/jsonapi.spec.js @@ -1,6 +1,16 @@ import CozyClient from 'cozy-client' -import { fromPouchResult, normalizeDoc } from './jsonapi' +import { + computeFileFullpath, + fromPouchResult, + normalizeDoc, + resetAllPaths +} from './jsonapi' +import { queryFileById } from './files' + +jest.mock('./files', () => ({ + queryFileById: jest.fn() +})) const BART_FIXTURE = { id: 1, @@ -642,3 +652,71 @@ const multipleDocRes = { } ] } + +describe('computeFileFullpath', () => { + afterEach(() => { + resetAllPaths() + }) + const dir = { + _id: '123', + _type: 'io.cozy.files', + type: 'directory', + dir_id: 'ROOT', + name: 'MYDIR', + path: 'ROOT/MYDIR' + } + const fileWithFullpath = { + _id: '456', + _type: 'io.cozy.files', + type: 'file', + dir_id: '123', + name: 'file1', + path: 'ROOT/MYDIR/file1' + } + const fileWithStackPath = { + _id: '789', + _type: 'io.cozy.files', + type: 'file', + dir_id: '123', + name: 'file2', + path: 'ROOT/MYDIR' + } + const filewithNoPath = { + _id: '000', + _type: 'io.cozy.files', + type: 'file', + dir_id: '123', + name: 'file3' + } + it('should handle directory', async () => { + const res = await computeFileFullpath(client, dir) + expect(res).toEqual(dir) + }) + + it('should handle file with complete path', async () => { + const res = await computeFileFullpath(client, fileWithFullpath) + expect(res).toEqual(fileWithFullpath) + }) + + it('should compute fullpath for file with incomplete path', async () => { + const res = await computeFileFullpath(client, fileWithStackPath) + expect(res.path).toEqual('ROOT/MYDIR/file2') + }) + + it('should compute fullpath for file with no path', async () => { + // eslint-disable-next-line prettier/prettier + queryFileById.mockResolvedValue({ data: dir }) + const res = await computeFileFullpath(client, filewithNoPath) + expect(res.path).toEqual('ROOT/MYDIR/file3') + }) + + it('should handle updates on path', async () => { + queryFileById.mockResolvedValue({ data: dir }) + const res1 = await computeFileFullpath(client, filewithNoPath) + expect(res1.path).toEqual('ROOT/MYDIR/file3') + + const updFile = { ...filewithNoPath, name: 'file3.1', path: undefined } + const res2 = await computeFileFullpath(client, updFile) + expect(res2.path).toEqual('ROOT/MYDIR/file3.1') + }) +})