Skip to content
Open
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
4 changes: 2 additions & 2 deletions src/extension.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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 () => {
Expand Down
8 changes: 2 additions & 6 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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<void> {
await extensionApi.authentication.getSession('github-authentication', [], { createIfNone: false });
await providerSessionManager.createSessionEntry();
}

// Deactivate the extension
Expand Down
12 changes: 12 additions & 0 deletions src/provider-session-manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
8 changes: 8 additions & 0 deletions src/provider-session-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ export class ProviderSessionManager {
this.ghSessions = [];
}
}

async createSessionEntry(): Promise<void> {
await extensionApi.authentication.getSession('github-authentication', [], { createIfNone: false });
}

async createSession(scopes: string[]): Promise<extensionApi.AuthenticationSession> {
let newAuthSession: extensionApi.AuthenticationSession;
Expand Down Expand Up @@ -83,6 +87,10 @@ export class ProviderSessionManager {
this.onDidChangeSessions.fire({
removed: [removedSession],
});

if (this.ghSessions.length === 0 ) {
await this.createSessionEntry();
}
}

async registerAuthenticationProvider(): Promise<void> {
Expand Down