-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support task redirection (#1745)
* feat: support task redirection Signed-off-by: axel7083 <[email protected]> * fix: linter&prettier Signed-off-by: axel7083 <[email protected]> * fix: typecheck Signed-off-by: axel7083 <[email protected]> --------- Signed-off-by: axel7083 <[email protected]>
- Loading branch information
Showing
14 changed files
with
373 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
packages/backend/src/registries/NavigationRegistry.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
/********************************************************************** | ||
* Copyright (C) 2024 Red Hat, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
***********************************************************************/ | ||
|
||
import { beforeAll, afterAll, beforeEach, describe, expect, test, vi } from 'vitest'; | ||
import { commands, navigation, type WebviewPanel, type Disposable } from '@podman-desktop/api'; | ||
import { NavigationRegistry } from './NavigationRegistry'; | ||
import { Messages } from '@shared/Messages'; | ||
|
||
vi.mock('@podman-desktop/api', async () => ({ | ||
commands: { | ||
registerCommand: vi.fn(), | ||
}, | ||
navigation: { | ||
register: vi.fn(), | ||
}, | ||
})); | ||
|
||
const panelMock: WebviewPanel = { | ||
reveal: vi.fn(), | ||
webview: { | ||
postMessage: vi.fn(), | ||
}, | ||
} as unknown as WebviewPanel; | ||
|
||
beforeEach(() => { | ||
vi.resetAllMocks(); | ||
vi.restoreAllMocks(); | ||
}); | ||
|
||
describe('incompatible podman-desktop', () => { | ||
let register: typeof navigation.register | undefined; | ||
beforeAll(() => { | ||
register = navigation.register; | ||
(navigation.register as unknown as undefined) = undefined; | ||
}); | ||
|
||
afterAll(() => { | ||
if (!register) return; | ||
navigation.register = register; | ||
}); | ||
|
||
test('init should not register command and navigation when using old version of podman', () => { | ||
(navigation.register as unknown as undefined) = undefined; | ||
const registry = new NavigationRegistry(panelMock); | ||
registry.init(); | ||
|
||
expect(commands.registerCommand).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
test('init should register command and navigation', () => { | ||
const registry = new NavigationRegistry(panelMock); | ||
registry.init(); | ||
|
||
expect(commands.registerCommand).toHaveBeenCalled(); | ||
expect(navigation.register).toHaveBeenCalled(); | ||
}); | ||
|
||
test('dispose should dispose all command and navigation registered', () => { | ||
const registry = new NavigationRegistry(panelMock); | ||
const disposables: Disposable[] = []; | ||
vi.mocked(commands.registerCommand).mockImplementation(() => { | ||
const disposable: Disposable = { | ||
dispose: vi.fn(), | ||
}; | ||
disposables.push(disposable); | ||
return disposable; | ||
}); | ||
vi.mocked(navigation.register).mockImplementation(() => { | ||
const disposable: Disposable = { | ||
dispose: vi.fn(), | ||
}; | ||
disposables.push(disposable); | ||
return disposable; | ||
}); | ||
|
||
registry.dispose(); | ||
|
||
disposables.forEach((disposable: Disposable) => { | ||
expect(disposable.dispose).toHaveBeenCalledOnce(); | ||
}); | ||
}); | ||
|
||
test('navigateToInferenceCreate should reveal and postMessage to webview', async () => { | ||
const registry = new NavigationRegistry(panelMock); | ||
|
||
await registry.navigateToInferenceCreate('dummyTrackingId'); | ||
|
||
await vi.waitFor(() => { | ||
expect(panelMock.reveal).toHaveBeenCalledOnce(); | ||
}); | ||
|
||
expect(panelMock.webview.postMessage).toHaveBeenCalledWith({ | ||
id: Messages.MSG_NAVIGATION_ROUTE_UPDATE, | ||
body: '/service/create?trackingId=dummyTrackingId', | ||
}); | ||
}); | ||
|
||
test('navigateToRecipeStart should reveal and postMessage to webview', async () => { | ||
const registry = new NavigationRegistry(panelMock); | ||
|
||
await registry.navigateToRecipeStart('dummyRecipeId', 'dummyTrackingId'); | ||
|
||
await vi.waitFor(() => { | ||
expect(panelMock.reveal).toHaveBeenCalledOnce(); | ||
}); | ||
|
||
expect(panelMock.webview.postMessage).toHaveBeenCalledWith({ | ||
id: Messages.MSG_NAVIGATION_ROUTE_UPDATE, | ||
body: '/recipe/dummyRecipeId/start?trackingId=dummyTrackingId', | ||
}); | ||
}); | ||
|
||
test('reading the route has side-effect', async () => { | ||
const registry = new NavigationRegistry(panelMock); | ||
|
||
await registry.navigateToRecipeStart('dummyRecipeId', 'dummyTrackingId'); | ||
|
||
expect(registry.readRoute()).toBeDefined(); | ||
expect(registry.readRoute()).toBeUndefined(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/********************************************************************** | ||
* Copyright (C) 2024 Red Hat, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
***********************************************************************/ | ||
import { type Disposable, navigation, type WebviewPanel, commands } from '@podman-desktop/api'; | ||
import { Messages } from '@shared/Messages'; | ||
|
||
export const RECIPE_START_ROUTE = 'recipe.start'; | ||
export const RECIPE_START_NAVIGATE_COMMAND = 'ai-lab.navigation.recipe.start'; | ||
|
||
export const INFERENCE_CREATE_ROUTE = 'inference.create'; | ||
export const INFERENCE_CREATE_NAVIGATE_COMMAND = 'ai-lab.navigation.inference.create'; | ||
|
||
export class NavigationRegistry implements Disposable { | ||
#disposables: Disposable[] = []; | ||
#route: string | undefined = undefined; | ||
|
||
constructor(private panel: WebviewPanel) {} | ||
|
||
init(): void { | ||
if (!navigation.register) { | ||
console.warn('this version of podman-desktop do not support task actions: some feature will not be available.'); | ||
return; | ||
} | ||
|
||
// register the recipes start navigation and command | ||
this.#disposables.push( | ||
commands.registerCommand(RECIPE_START_NAVIGATE_COMMAND, this.navigateToRecipeStart.bind(this)), | ||
); | ||
this.#disposables.push(navigation.register(RECIPE_START_ROUTE, RECIPE_START_NAVIGATE_COMMAND)); | ||
|
||
// register the inference create navigation and command | ||
this.#disposables.push( | ||
commands.registerCommand(INFERENCE_CREATE_NAVIGATE_COMMAND, this.navigateToInferenceCreate.bind(this)), | ||
); | ||
this.#disposables.push(navigation.register(INFERENCE_CREATE_ROUTE, INFERENCE_CREATE_NAVIGATE_COMMAND)); | ||
} | ||
|
||
/** | ||
* This function return the route, and reset it. | ||
* Meaning after read the route is undefined | ||
*/ | ||
public readRoute(): string | undefined { | ||
const result: string | undefined = this.#route; | ||
this.#route = undefined; | ||
return result; | ||
} | ||
|
||
dispose(): void { | ||
this.#disposables.forEach(disposable => disposable.dispose()); | ||
} | ||
|
||
protected async updateRoute(route: string): Promise<void> { | ||
await this.panel.webview.postMessage({ | ||
id: Messages.MSG_NAVIGATION_ROUTE_UPDATE, | ||
body: route, | ||
}); | ||
this.#route = route; | ||
this.panel.reveal(); | ||
} | ||
|
||
public async navigateToRecipeStart(recipeId: string, trackingId: string): Promise<void> { | ||
return this.updateRoute(`/recipe/${recipeId}/start?trackingId=${trackingId}`); | ||
} | ||
|
||
public async navigateToInferenceCreate(trackingId: string): Promise<void> { | ||
return this.updateRoute(`/service/create?trackingId=${trackingId}`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.