diff --git a/spring-boot-admin-server-ui/src/main/frontend/views/instances/sessions/index.spec.ts b/spring-boot-admin-server-ui/src/main/frontend/views/instances/sessions/index.spec.ts new file mode 100644 index 00000000000..43e3a50024b --- /dev/null +++ b/spring-boot-admin-server-ui/src/main/frontend/views/instances/sessions/index.spec.ts @@ -0,0 +1,64 @@ +import { fireEvent, screen, waitFor } from '@testing-library/vue'; +import { flushPromises } from '@vue/test-utils'; +import { HttpResponse, http } from 'msw'; +import { expect, it, vi } from 'vitest'; + +import Sessions from './index.vue'; + +import { applications } from '@/mocks/applications/data'; +import { server } from '@/mocks/server'; +import Application from '@/services/application'; +import { render } from '@/test-utils'; + +const showSessions = (query: Record) => { + const instance = new Application(applications[0]).instances[0]; + render(Sessions, { + props: { instance }, + global: { + mocks: { + $route: { name: 'instances/sessions', query }, + $router: { replace: vi.fn() }, + }, + }, + }); + return instance; +}; + +it("keeps the current user's sessions when an earlier lookup completes last", async () => { + const release = Promise.withResolvers(); + server.use( + http.get('*/actuator/sessions', async ({ request }) => { + const username = new URL(request.url).searchParams.get('username'); + if (username === 'alice') await release.promise; + return HttpResponse.json({ sessions: [{ id: username + '-session' }] }); + }), + ); + const instance = showSessions({ username: 'alice' }); + const fetch = vi.spyOn(instance, 'fetchSessionsByUsername'); + await waitFor(() => expect(fetch).toHaveBeenCalledWith('alice')); + await fireEvent.update(screen.getByDisplayValue('alice'), 'bob'); + expect(await screen.findByText('bob-session')).toBeVisible(); + release.resolve(); + await fetch.mock.results[0].value; + await flushPromises(); + expect(screen.queryByText('alice-session')).not.toBeInTheDocument(); + expect(screen.getByText('bob-session')).toBeVisible(); +}); + +it('submits an ID search immediately without querying for a username', async () => { + server.use( + http.get('*/actuator/sessions/:id', ({ params }) => + HttpResponse.json({ id: params.id }), + ), + ); + const instance = showSessions({ sessionId: 'first-id' }); + await screen.findByText('first-id'); + const byUsername = vi.spyOn(instance, 'fetchSessionsByUsername'); + const byId = vi.spyOn(instance, 'fetchSession'); + const input = screen.getByDisplayValue('first-id'); + await fireEvent.update(input, 'second-id'); + await fireEvent.keyUp(input, { key: 'Enter' }); + expect(byId).toHaveBeenCalledWith('second-id'); + expect(byUsername).not.toHaveBeenCalled(); + expect(await screen.findByText('second-id')).toBeVisible(); +}); diff --git a/spring-boot-admin-server-ui/src/main/frontend/views/instances/sessions/index.vue b/spring-boot-admin-server-ui/src/main/frontend/views/instances/sessions/index.vue index ea317492dea..94c7f100312 100644 --- a/spring-boot-admin-server-ui/src/main/frontend/views/instances/sessions/index.vue +++ b/spring-boot-admin-server-ui/src/main/frontend/views/instances/sessions/index.vue @@ -35,7 +35,7 @@ name="filter" type="search" @paste="handlePaste" - @keyup.enter="fetchSessionsByUsername()" + @keyup.enter="fetch()" /> @@ -94,12 +94,16 @@ export default { sessions: [], isLoading: false, currentRouteName: null, + requestId: 0, + scheduleFetch: debounce(function () { + this.fetch(); + }, 250), }), watch: { '$route.query': { immediate: true, handler() { - this.filter = Object.entries(this.$route.query).reduce( + const filter = Object.entries(this.$route.query).reduce( (acc, [name, value]) => { acc.type = name; acc.value = value; @@ -107,6 +111,7 @@ export default { }, { type: 'username', value: '' }, ); + if (!isEqual(filter, this.filter)) this.filter = filter; }, }, filter: { @@ -123,40 +128,54 @@ export default { query: oldQuery, }); } - this.fetch(); + this.requestId++; + this.sessions = []; + this.error = null; + this.isLoading = Boolean(this.filter.value); + this.scheduleFetch(); }, }, }, mounted() { this.currentRouteName = this.$route.name; }, + beforeUnmount() { + this.scheduleFetch.cancel(); + this.requestId++; + }, methods: { - fetch: debounce(async function () { + async fetch() { + this.scheduleFetch.cancel(); + const requestId = ++this.requestId; this.error = null; if (!this.filter.value) { this.sessions = []; + this.isLoading = false; return; } this.isLoading = true; try { - if (this.filter.type === 'sessionId') { - this.sessions = await this.fetchSession(); - } else { - this.sessions = await this.fetchSessionsByUsername(); - } + const sessions = + this.filter.type === 'sessionId' + ? await this.fetchSession() + : await this.fetchSessionsByUsername(); + if (requestId === this.requestId) this.sessions = sessions; } catch (error) { - console.warn('Fetching sessions failed:', error); - this.error = error; + if (requestId === this.requestId) { + console.warn('Fetching sessions failed:', error); + this.error = error; + } + } finally { + if (requestId === this.requestId) this.isLoading = false; } - this.isLoading = false; - }, 250), + }, async fetchSession() { try { const response = await this.instance.fetchSession(this.filter.value); return [new Session(response.data)]; } catch (error) { - if (error.response.status === 404) { + if (error.response?.status === 404) { return []; } else { throw error;