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: 17 additions & 7 deletions src/components/Mailbox.vue
Original file line number Diff line number Diff line change
Expand Up @@ -229,18 +229,28 @@ export default {
},

methods: {
initializeCache() {
async initializeCache() {
this.loadingCacheInitialization = true
this.error = false

logger.debug(`syncing folder ${this.mailbox.databaseId} (${this.query}) during cache initialization`)
this.sync(true)
.then(() => {
this.loadingCacheInitialization = false
this.syncedMailboxes.add(this.mailbox.databaseId + (this.searchQuery ?? ''))

return this.loadEnvelopes()
try {
await this.sync(true)
this.syncedMailboxes.add(this.mailbox.databaseId + (this.searchQuery ?? ''))
} catch (error) {
await matchError(error, {
[MailboxLockedError.getName()]: () => {
logger.info(`Folder ${this.mailbox.databaseId} (${this.searchQuery}) is already being initialized, loading its messages instead`)
},
default: (error) => {
throw error
},
})
} finally {
this.loadingCacheInitialization = false
}

await this.loadEnvelopes()
},

async loadEnvelopes() {
Expand Down
84 changes: 84 additions & 0 deletions src/tests/unit/components/Mailbox.vue.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import { shallowMount } from '@vue/test-utils'
import { createPinia, setActivePinia } from 'pinia'
import Mailbox from '../../../components/Mailbox.vue'
import MailboxLockedError from '../../../errors/MailboxLockedError.js'
import MailboxNotCachedError from '../../../errors/MailboxNotCachedError.js'
import Nextcloud from '../../../mixins/Nextcloud.js'
import useMainStore from '../../../store/mainStore.js'

describe('Mailbox', () => {
let store

beforeEach(() => {
setActivePinia(createPinia())

store = useMainStore()
store.hasFetchedInitialEnvelopes = true
store.getEnvelopes = vi.fn().mockReturnValue([])
store.fetchEnvelopes = vi.fn()
.mockRejectedValueOnce(new MailboxNotCachedError('22 is not cached'))
.mockResolvedValue([])
store.syncEnvelopes = vi.fn()
store.updateSyncTimestamp = vi.fn()
})

afterEach(() => {
vi.restoreAllMocks()
})

function mountMailbox() {
return shallowMount(Mailbox, {
props: {
account: { id: 4 },
mailbox: { databaseId: 22, accountId: 4 },
bus: { on: vi.fn(), off: vi.fn() },
searchQuery: 'is:pi-other',
},
global: {
mixins: [Nextcloud],
},
})
}

it('initializes an uncached folder and then loads its messages', async () => {
store.syncEnvelopes.mockResolvedValue([])
const view = mountMailbox()

await view.vm.loadEnvelopes()

expect(store.syncEnvelopes).toHaveBeenCalledWith({ mailboxId: 22, query: 'is:pi-other', init: true })
expect(store.fetchEnvelopes).toHaveBeenCalledTimes(2)
expect(view.vm.syncedMailboxes.has('22is:pi-other')).toBe(true)
expect(view.vm.loadingCacheInitialization).toBe(false)
expect(view.vm.error).toBe(false)
})

it('loads the messages when another section is already initializing the folder', async () => {
store.syncEnvelopes.mockRejectedValue(new MailboxLockedError('22 is already being synced'))
const view = mountMailbox()

await view.vm.loadEnvelopes()

expect(store.syncEnvelopes).toHaveBeenCalledTimes(1)
expect(store.fetchEnvelopes).toHaveBeenCalledTimes(2)
expect(view.vm.loadingCacheInitialization).toBe(false)
expect(view.vm.error).toBe(false)
})

it('shows an error when the initialization fails otherwise', async () => {
const error = new Error('server error')
store.syncEnvelopes.mockRejectedValue(error)
const view = mountMailbox()

await view.vm.loadEnvelopes()

expect(store.fetchEnvelopes).toHaveBeenCalledTimes(1)
expect(view.vm.error).toBe(error)
expect(view.vm.loadingCacheInitialization).toBe(false)
})
})
Loading