Skip to content
Draft
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
2 changes: 1 addition & 1 deletion core/Listener/FeedBackHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function handleRepairFeedback(Event $event): void {
} elseif ($event instanceof RepairInfoEvent) {
$this->eventSource->send('success', $this->l10n->t('Repair info:') . ' ' . $event->getMessage());
} elseif ($event instanceof RepairWarningEvent) {
$this->eventSource->send('notice', $this->l10n->t('Repair warning:') . ' ' . $event->getMessage());
$this->eventSource->send('warning', $this->l10n->t('Repair warning:') . ' ' . $event->getMessage());
} elseif ($event instanceof RepairErrorEvent) {
$this->eventSource->send('error', $this->l10n->t('Repair error:') . ' ' . $event->getMessage());
}
Expand Down
201 changes: 201 additions & 0 deletions core/src/views/UpdaterAdmin.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import { cleanup, fireEvent, render } from '@testing-library/vue'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { nextTick } from 'vue'
import UpdaterAdmin from './UpdaterAdmin.vue'

class FakeEventSource extends EventTarget {
static instance: FakeEventSource
closed = false

constructor(public url: string) {
super()
FakeEventSource.instance = this
}

send(type: string, data: string) {
this.dispatchEvent(new MessageEvent(type, { data: JSON.stringify(data) }))
}

close() {
this.closed = true
}
}

const defaultInfo = {
appsToUpgrade: [],
incompatibleAppsList: [],
isAppsOnlyUpgrade: false,
oldTheme: null,
productName: 'Nextcloud',
version: '34.0.0',
}

function setUpdateInfo(info: object = {}) {
const input = document.createElement('input')
input.id = 'initial-state-core-updateInfo'
input.type = 'hidden'
input.value = btoa(JSON.stringify({ ...defaultInfo, ...info }))
document.head.appendChild(input)
}

async function startUpdate() {
const view = render(UpdaterAdmin)
await fireEvent.click(view.getByRole('button', { name: 'Start update' }))
return { view, source: FakeEventSource.instance }
}

describe('UpdaterAdmin', () => {
beforeEach(() => {
delete globalThis._nc_initial_state
vi.stubGlobal('EventSource', FakeEventSource)
document.head.dataset.requesttoken = 'test-token'
})

afterEach(() => {
cleanup()
document.getElementById('initial-state-core-updateInfo')?.remove()
delete globalThis._nc_initial_state
delete document.head.dataset.requesttoken
vi.useRealTimers()
vi.unstubAllGlobals()
})

it('shows apps to update even when there are no incompatible apps', () => {
setUpdateInfo({
appsToUpgrade: [{ id: 'calendar', name: 'Calendar', oldVersion: '1.0', version: '2.0' }],
})
const view = render(UpdaterAdmin)

expect(view.getByText('These apps will be updated:')).toBeInTheDocument()
expect(view.getByText('Calendar (calendar) from 1.0 to 2.0')).toBeInTheDocument()
expect(view.queryByText('These incompatible apps will be disabled:')).not.toBeInTheDocument()
})

it('does not show an empty update list when an incompatible app exists', () => {
setUpdateInfo({ incompatibleAppsList: [{ id: 'example', name: 'Example' }] })
const view = render(UpdaterAdmin)

expect(view.getByText('Example (example)')).toBeInTheDocument()
expect(view.queryByText('These apps will be updated:')).not.toBeInTheDocument()
})

it('shows success and starts the redirect countdown on done', async () => {
setUpdateInfo()
const { view, source } = await startUpdate()
vi.useFakeTimers()

expect(view.getByRole('button', { name: 'Continue to Nextcloud' })).toBeDisabled()
source.send('success', 'Preparing update')
source.send('done', '')
await nextTick()

expect(source.closed).toBe(true)
expect(view.getByText('The update completed successfully.')).toBeInTheDocument()
expect(view.getByText('You will be redirected to Nextcloud in 5 seconds.')).toBeInTheDocument()
vi.advanceTimersByTime(1000)
await nextTick()
expect(view.getByText('You will be redirected to Nextcloud in 4 seconds.')).toBeInTheDocument()
})

it('shows repair warnings and does not redirect automatically', async () => {
setUpdateInfo()
const { view, source } = await startUpdate()
vi.useFakeTimers()

source.send('warning', 'Repair warning: Unable to clear the frontend cache')
source.send('done', '')
await nextTick()

expect(view.getByText(/The update completed with warnings/)).toBeInTheDocument()
expect(view.getByText('Repair warning: Unable to clear the frontend cache')).toBeVisible()
expect(view.queryByText(/You will be redirected/)).not.toBeInTheDocument()
expect(vi.getTimerCount()).toBe(0)
})

it('keeps monitoring after a repair error until done', async () => {
setUpdateInfo()
const { view, source } = await startUpdate()

source.send('error', 'Repair error: step failed')
await nextTick()
expect(source.closed).toBe(false)
expect(view.getByRole('button', { name: 'Continue to Nextcloud' })).toBeDisabled()

source.send('success', 'Later step completed')
source.send('done', '')
await nextTick()
expect(source.closed).toBe(true)
expect(view.getByText(/The update ended with errors/)).toBeInTheDocument()
expect(view.getByText('Repair error: step failed')).toBeVisible()
expect(view.getByText('Later step completed')).toBeVisible()
expect(view.queryByText(/You will be redirected/)).not.toBeInTheDocument()
})

it('keeps monitoring after a failure that is followed by done', async () => {
setUpdateInfo()
const { view, source } = await startUpdate()

source.send('failure', 'An app could not be installed')
source.send('success', 'Later step completed')
await nextTick()
expect(source.closed).toBe(false)
expect(view.getByRole('button', { name: 'Continue to Nextcloud' })).toBeDisabled()

source.send('done', '')
await nextTick()
expect(view.getByText(/The update ended with errors/)).toBeInTheDocument()
expect(view.getByText('An app could not be installed')).toBeVisible()
expect(view.getByText('Later step completed')).toBeVisible()
expect(view.queryByText(/You will be redirected/)).not.toBeInTheDocument()
})

it('finishes with an error when the server closes after a failure without done', async () => {
setUpdateInfo()
const { view, source } = await startUpdate()

source.send('failure', 'Upgrade failed')
await nextTick()
expect(view.getByRole('button', { name: 'Continue to Nextcloud' })).toBeDisabled()
source.send('__internal__', 'close')
await nextTick()

expect(source.closed).toBe(true)
expect(view.getByText(/The update ended with errors/)).toBeInTheDocument()
expect(view.getByText('Upgrade failed')).toBeVisible()
expect(view.getByRole('button', { name: 'Continue to Nextcloud' })).toBeEnabled()
expect(view.queryByText(/You will be redirected/)).not.toBeInTheDocument()
})

it('treats a server close without done or an error as an unknown outcome', async () => {
setUpdateInfo()
const { view, source } = await startUpdate()

source.send('__internal__', 'close')
await nextTick()
expect(source.closed).toBe(true)
expect(view.getByText(/update status is unknown/)).toBeInTheDocument()
expect(view.getByRole('button', { name: 'Continue to Nextcloud' })).toBeDisabled()
})

it('treats a transport error as an unknown outcome', async () => {
setUpdateInfo()
const { view, source } = await startUpdate()

source.dispatchEvent(new Event('error'))
await nextTick()

expect(source.closed).toBe(true)
expect(view.getByText(/update status is unknown/)).toBeInTheDocument()
expect(view.getByRole('button', { name: 'Continue to Nextcloud' })).toBeDisabled()
expect(view.queryByText(/The update completed/)).not.toBeInTheDocument()
expect(view.queryByText(/You will be redirected/)).not.toBeInTheDocument()
const beforeUnload = new Event('beforeunload', { cancelable: true })
window.dispatchEvent(beforeUnload)
expect(beforeUnload.defaultPrevented).toBe(true)
})
})
55 changes: 40 additions & 15 deletions core/src/views/UpdaterAdmin.vue
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,21 @@ const updateInfo = loadState<{
const isShowingDetails = ref(false)
const isUpdateRunning = ref(false)
const isUpdateDone = ref(false)
const isOutcomeUnknown = ref(false)

const messages = ref<{ message: string, type: string }[]>([])
const wasSuccessfull = computed(() => messages.value.every((msg) => msg.type === 'success' || msg.type === 'notice'))
const hasErrors = computed(() => messages.value.some((msg) => msg.type === 'error' || msg.type === 'failure'))
const resultIcon = computed(() => wasSuccessfull.value ? mdiCheckCircleOutline : (hasErrors.value ? mdiCloseCircleOutline : mdiAlertCircleOutline))

const statusMessage = computed(() => {
if (isOutcomeUnknown.value) {
return t('core', 'The connection was lost and the update status is unknown. The update may still be running. Check the server logs and process status before reloading or retrying.')
}
if (isUpdateDone.value) {
if (!wasSuccessfull.value) {
if (hasErrors.value) {
return t('core', 'The update ended with errors. Please check the details for more information.')
} else if (!wasSuccessfull.value) {
return t('core', 'The update completed with warnings. Please check the details for more information.')
} else {
return t('core', 'The update completed successfully.')
Expand Down Expand Up @@ -89,7 +95,7 @@ function getSeverityIcon(type: string) {
* Start the update process
*/
async function onStartUpdate() {
if (isUpdateRunning.value || isUpdateDone.value) {
if (isUpdateRunning.value || isUpdateDone.value || isOutcomeUnknown.value) {
return
}

Expand All @@ -101,19 +107,38 @@ async function onStartUpdate() {
eventSource.listen('notice', (message) => {
messages.value.push({ message, type: 'notice' })
})
eventSource.listen('error', (message) => {
messages.value.push({ message, type: 'error' })
isUpdateRunning.value = false
isUpdateDone.value = true
eventSource.listen('warning', (message) => {
messages.value.push({ message, type: 'warning' })
})
eventSource.source.addEventListener('error', (event) => {
if (event instanceof MessageEvent) {
messages.value.push({ message: JSON.parse(event.data), type: 'error' })
return
}
eventSource.close()
isUpdateRunning.value = false
isOutcomeUnknown.value = true
isShowingDetails.value = true
})
eventSource.listen('failure', (message) => {
messages.value.push({ message, type: 'failure' })
})
eventSource.listen('__internal__', (message) => {
if (message === 'close' && !isUpdateDone.value && !isOutcomeUnknown.value) {
isUpdateRunning.value = false
if (hasErrors.value) {
isUpdateDone.value = true
} else {
isOutcomeUnknown.value = true
}
isShowingDetails.value = true
}
})
eventSource.listen('done', () => {
isUpdateRunning.value = false
isUpdateDone.value = true
eventSource.close()
isShowingDetails.value = !wasSuccessfull.value
updateCountdown()
})
}
Expand All @@ -139,7 +164,7 @@ function updateCountdown() {
* @param event - The beforeunload event object.
*/
function onUnload(event: BeforeUnloadEvent) {
if (isUpdateRunning.value) {
if (isUpdateRunning.value || isOutcomeUnknown.value) {
event.preventDefault()
event.returnValue = t('core', 'The update is in progress, leaving this page might interrupt the process in some environments.')
}
Expand Down Expand Up @@ -174,7 +199,7 @@ function reloadPage() {
</ul>
</NcNoteCard>

<NcNoteCard v-if="updateInfo.incompatibleAppsList.length" type="info">
<NcNoteCard v-if="updateInfo.appsToUpgrade.length" type="info">
{{ t('core', 'These apps will be updated:') }}
<ul :aria-label="t('core', 'Apps to update')" :class="$style.updater__appsList">
<li v-for="app of updateInfo.appsToUpgrade" :key="'app-update-' + app.id">
Expand All @@ -191,7 +216,7 @@ function reloadPage() {
</p>

<NcButton
v-if="!isUpdateRunning && !isUpdateDone"
v-if="!isUpdateRunning && !isUpdateDone && !isOutcomeUnknown"
:class="$style.updater__updateButton"
variant="primary"
@click="onStartUpdate">
Expand All @@ -200,23 +225,23 @@ function reloadPage() {
<NcButton
v-else
:class="$style.updater__updateButton"
:disabled="isUpdateRunning"
:disabled="isUpdateRunning || isOutcomeUnknown"
variant="primary"
@click="reloadPage">
{{ t('core', 'Continue to {productName}', { productName: updateInfo.productName }) }}
</NcButton>

<div v-if="isUpdateRunning || isUpdateDone">
<div v-if="isUpdateRunning || isUpdateDone || isOutcomeUnknown">
<h2>{{ t('core', 'Update to {version}', { version: updateInfo.version }) }}</h2>

<NcLoadingIcon v-if="isUpdateRunning" />
<NcIconSvgWrapper
v-else
:path="resultIcon"
:path="isOutcomeUnknown ? mdiAlertCircleOutline : resultIcon"
:class="{
[$style.updater__messageIcon_success]: wasSuccessfull,
[$style.updater__messageIcon_error]: hasErrors && !wasSuccessfull,
[$style.updater__messageIcon_warning]: !hasErrors && !wasSuccessfull,
[$style.updater__messageIcon_success]: !isOutcomeUnknown && wasSuccessfull,
[$style.updater__messageIcon_error]: !isOutcomeUnknown && hasErrors && !wasSuccessfull,
[$style.updater__messageIcon_warning]: isOutcomeUnknown || (!hasErrors && !wasSuccessfull),
}" />
<div aria-live="polite">
<em>{{ statusMessage }}</em><br>
Expand Down
30 changes: 30 additions & 0 deletions tests/Core/Listener/FeedBackHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace Tests\Core\Listener;

use OC\Core\Listener\FeedBackHandler;
use OC\Repair\Events\RepairWarningEvent;
use OCP\IEventSource;
use OCP\IL10N;
use Test\TestCase;

class FeedBackHandlerTest extends TestCase {
public function testRepairWarningIsSentAsWarning(): void {
$source = $this->createMock(IEventSource::class);
$source->expects($this->once())
->method('send')
->with('warning', 'Repair warning: Unable to clear the frontend cache');
$l10n = $this->createMock(IL10N::class);
$l10n->method('t')->willReturnCallback(static fn (string $text): string => $text);

$handler = new FeedBackHandler($source, $l10n);
$handler->handleRepairFeedback(new RepairWarningEvent('Unable to clear the frontend cache'));
}
}
Loading