diff --git a/src/extension.spec.ts b/src/extension.spec.ts index 28c9fd1..e0ac209 100644 --- a/src/extension.spec.ts +++ b/src/extension.spec.ts @@ -18,7 +18,7 @@ import { resolve } from 'node:path'; -import * as extensionApi from '@podman-desktop/api'; +import type * as extensionApi from '@podman-desktop/api'; import { expect, test, vi } from 'vitest'; import { activate, deactivate } from './extension'; @@ -38,7 +38,7 @@ test('Session manager created and used upon extension activation', async () => { expect(ProviderSessionManager).toHaveBeenCalledWith(extensionContextMcok); expect(ProviderSessionManager.prototype.registerAuthenticationProvider).toHaveBeenCalled(); expect(ProviderSessionManager.prototype.restoreSessions).toHaveBeenCalled(); - expect(extensionApi.authentication.getSession).toHaveBeenCalledWith('github-authentication', [], { createIfNone: false }); + expect(ProviderSessionManager.prototype.createSessionEntry).toHaveBeenCalled(); }); test('save sessions upon extension deactivation', async () => { diff --git a/src/extension.ts b/src/extension.ts index c496f8a..767dcf0 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -16,7 +16,7 @@ * SPDX-License-Identifier: Apache-2.0 ***********************************************************************/ -import * as extensionApi from '@podman-desktop/api'; +import type * as extensionApi from '@podman-desktop/api'; import { ProviderSessionManager } from './provider-session-manager'; @@ -29,11 +29,7 @@ export async function activate(context: extensionApi.ExtensionContext): Promise< providerSessionManager = new ProviderSessionManager(context); await providerSessionManager.registerAuthenticationProvider(); await providerSessionManager.restoreSessions(); - await createSessionEntry(); -} - -async function createSessionEntry(): Promise { - await extensionApi.authentication.getSession('github-authentication', [], { createIfNone: false }); + await providerSessionManager.createSessionEntry(); } // Deactivate the extension diff --git a/src/provider-session-manager.spec.ts b/src/provider-session-manager.spec.ts index b0d609a..df091a4 100644 --- a/src/provider-session-manager.spec.ts +++ b/src/provider-session-manager.spec.ts @@ -151,6 +151,18 @@ test('removeSession', async () => { await expect(sessionManager.removeSession('123')).rejects.toThrowError('Session with id 123 not found'); }); +test('remove last session', async () => { + vi.mocked(extensionContextMock.secrets.get).mockResolvedValue(JSON.stringify([sessionsMock[0]])); + await sessionManager.restoreSessions(); + + await sessionManager.removeSession('session1'); + + const currentSessions = await sessionManager.getSessions(); + expect(currentSessions).toEqual([]); + + expect(extensionApi.authentication.getSession).toHaveBeenCalledWith('github-authentication', [], { createIfNone: false }); +}); + test('saveSessions', async () => { // make sure that there are no sessions saved const currentSessions = await sessionManager.getSessions(); diff --git a/src/provider-session-manager.ts b/src/provider-session-manager.ts index abcda1d..a81fcf7 100644 --- a/src/provider-session-manager.ts +++ b/src/provider-session-manager.ts @@ -38,6 +38,10 @@ export class ProviderSessionManager { this.ghSessions = []; } } + + async createSessionEntry(): Promise { + await extensionApi.authentication.getSession('github-authentication', [], { createIfNone: false }); + } async createSession(scopes: string[]): Promise { let newAuthSession: extensionApi.AuthenticationSession; @@ -83,6 +87,10 @@ export class ProviderSessionManager { this.onDidChangeSessions.fire({ removed: [removedSession], }); + + if (this.ghSessions.length === 0 ) { + await this.createSessionEntry(); + } } async registerAuthenticationProvider(): Promise {