Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions packages/cozy-pouch-link/src/jsonapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand All @@ -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)
}
Expand Down
80 changes: 79 additions & 1 deletion packages/cozy-pouch-link/src/jsonapi.spec.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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')
})
})
Loading