Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PRD-609] fix: Broken file path #2613

Merged
merged 2 commits into from
Nov 13, 2024
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import CozyClient from 'cozy-client'
import { IOCozyContact, IOCozyFile } from 'cozy-client/types/types'

import { normalizeSearchResult } from './normalizeSearchResult'
import { cleanFilePath, normalizeSearchResult } from './normalizeSearchResult'
import { FILES_DOCTYPE, TYPE_FILE } from '../consts'
import { RawSearchResult } from '../types'

const fakeFlatDomainClient = {
Expand Down Expand Up @@ -341,3 +343,46 @@ describe('Should normalize unknown doctypes', () => {
})
})
})

describe('cleanFilePath', () => {
it('should return the document unchanged if it is not an IOCozyFile', () => {
const doc = { fullname: 'name' } as IOCozyContact
expect(cleanFilePath(doc)).toEqual(doc)
})

it('should return the document unchanged if path is undefined', () => {
const doc = { _type: FILES_DOCTYPE, name: 'name' } as IOCozyFile
expect(cleanFilePath(doc)).toEqual(doc)
})

it('should return the document unchanged if not a file', () => {
const doc = {
_type: FILES_DOCTYPE,
name: 'name',
type: TYPE_FILE
} as IOCozyFile
expect(cleanFilePath(doc)).toEqual(doc)
})

it('should remove name from path if path ends with name', () => {
const doc = {
_type: FILES_DOCTYPE,
type: TYPE_FILE,
path: '/the/path/myname',
name: 'myname'
} as IOCozyFile
const expected = { ...doc, path: '/the/path' }

expect(cleanFilePath(doc)).toEqual(expected)
})

it('should return the document unchanged if path does not end with name', () => {
const doc = {
_type: FILES_DOCTYPE,
type: TYPE_FILE,
path: '/the/path/othername',
name: 'name'
} as IOCozyFile
expect(cleanFilePath(doc)).toEqual(doc)
})
})
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import CozyClient, { generateWebLink, models } from 'cozy-client'
import { IOCozyContact } from 'cozy-client/types/types'

import { APPS_DOCTYPE, TYPE_DIRECTORY } from '../consts'
import { APPS_DOCTYPE, TYPE_DIRECTORY, TYPE_FILE } from '../consts'
import {
CozyDoc,
RawSearchResult,
Expand All @@ -16,7 +16,7 @@ export const normalizeSearchResult = (
searchResults: RawSearchResult,
query: string
): SearchResult => {
const doc = searchResults.doc
const doc = cleanFilePath(searchResults.doc)
const url = buildOpenURL(client, doc)
const slug = getSearchResultSlug(doc)
const title = getSearchResultTitle(doc)
Expand All @@ -30,6 +30,23 @@ export const normalizeSearchResult = (
return normalizedRes
}

export const cleanFilePath = (doc: CozyDoc): CozyDoc => {
if (!isIOCozyFile(doc)) {
return doc
}
const { path, name, type } = doc
if (!path || type !== TYPE_FILE) {
return doc
}
let newPath = path
if (path.endsWith(`/${name}`)) {
// Remove the name from the path, which is added at indexing time to search on it
newPath = path.slice(0, -name.length - 1)
}

return { ...doc, path: newPath }
}

const getSearchResultTitle = (doc: CozyDoc): string | null => {
if (isIOCozyFile(doc)) {
return doc.name
Expand Down
Loading