-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Add extension registry client #18396
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
205 changes: 205 additions & 0 deletions
205
packages/cli/src/config/extensionRegistryClient.test.ts
This file contains hidden or 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,205 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2025 Google LLC | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import { | ||
| describe, | ||
| it, | ||
| expect, | ||
| vi, | ||
| beforeEach, | ||
| afterEach, | ||
| type Mock, | ||
| } from 'vitest'; | ||
| import { | ||
| ExtensionRegistryClient, | ||
| type RegistryExtension, | ||
| } from './extensionRegistryClient.js'; | ||
|
|
||
| const mockExtensions: RegistryExtension[] = [ | ||
| { | ||
| id: 'ext1', | ||
| rank: 1, | ||
| url: 'https://github.com/test/ext1', | ||
| fullName: 'test/ext1', | ||
| repoDescription: 'Test extension 1', | ||
| stars: 100, | ||
| lastUpdated: '2025-01-01T00:00:00Z', | ||
| extensionName: 'extension-one', | ||
| extensionVersion: '1.0.0', | ||
| extensionDescription: 'First test extension', | ||
| avatarUrl: 'https://example.com/avatar1.png', | ||
| hasMCP: true, | ||
| hasContext: false, | ||
| isGoogleOwned: false, | ||
| licenseKey: 'mit', | ||
| hasHooks: false, | ||
| hasCustomCommands: false, | ||
| hasSkills: false, | ||
| }, | ||
| { | ||
| id: 'ext2', | ||
| rank: 2, | ||
| url: 'https://github.com/test/ext2', | ||
| fullName: 'test/ext2', | ||
| repoDescription: 'Test extension 2', | ||
| stars: 50, | ||
| lastUpdated: '2025-01-02T00:00:00Z', | ||
| extensionName: 'extension-two', | ||
| extensionVersion: '0.5.0', | ||
| extensionDescription: 'Second test extension', | ||
| avatarUrl: 'https://example.com/avatar2.png', | ||
| hasMCP: false, | ||
| hasContext: true, | ||
| isGoogleOwned: true, | ||
| licenseKey: 'apache-2.0', | ||
| hasHooks: false, | ||
| hasCustomCommands: false, | ||
| hasSkills: false, | ||
| }, | ||
| { | ||
| id: 'ext3', | ||
| rank: 3, | ||
| url: 'https://github.com/test/ext3', | ||
| fullName: 'test/ext3', | ||
| repoDescription: 'Test extension 3', | ||
| stars: 10, | ||
| lastUpdated: '2025-01-03T00:00:00Z', | ||
| extensionName: 'extension-three', | ||
| extensionVersion: '0.1.0', | ||
| extensionDescription: 'Third test extension', | ||
| avatarUrl: 'https://example.com/avatar3.png', | ||
| hasMCP: true, | ||
| hasContext: true, | ||
| isGoogleOwned: false, | ||
| licenseKey: 'gpl-3.0', | ||
| hasHooks: false, | ||
| hasCustomCommands: false, | ||
| hasSkills: false, | ||
| }, | ||
| ]; | ||
|
|
||
| describe('ExtensionRegistryClient', () => { | ||
| let client: ExtensionRegistryClient; | ||
| let fetchMock: Mock; | ||
|
|
||
| beforeEach(() => { | ||
| client = new ExtensionRegistryClient(); | ||
| fetchMock = vi.fn(); | ||
| global.fetch = fetchMock; | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.restoreAllMocks(); | ||
| }); | ||
|
|
||
| it('should fetch and return extensions with pagination (default ranking)', async () => { | ||
| fetchMock.mockResolvedValue({ | ||
| ok: true, | ||
| json: async () => mockExtensions, | ||
| }); | ||
|
|
||
| const result = await client.getExtensions(1, 2); | ||
| expect(result.extensions).toHaveLength(2); | ||
| expect(result.extensions[0].id).toBe('ext1'); // rank 1 | ||
| expect(result.extensions[1].id).toBe('ext2'); // rank 2 | ||
| expect(result.total).toBe(3); | ||
| expect(fetchMock).toHaveBeenCalledTimes(1); | ||
| expect(fetchMock).toHaveBeenCalledWith( | ||
| 'https://geminicli.com/extensions.json', | ||
| ); | ||
| }); | ||
|
|
||
| it('should return extensions sorted alphabetically', async () => { | ||
| fetchMock.mockResolvedValue({ | ||
| ok: true, | ||
| json: async () => mockExtensions, | ||
| }); | ||
|
|
||
| const result = await client.getExtensions(1, 3, 'alphabetical'); | ||
| expect(result.extensions).toHaveLength(3); | ||
| expect(result.extensions[0].id).toBe('ext1'); | ||
| expect(result.extensions[1].id).toBe('ext3'); | ||
| expect(result.extensions[2].id).toBe('ext2'); | ||
| }); | ||
|
|
||
| it('should return the second page of extensions', async () => { | ||
| fetchMock.mockResolvedValue({ | ||
| ok: true, | ||
| json: async () => mockExtensions, | ||
| }); | ||
|
|
||
| const result = await client.getExtensions(2, 2); | ||
| expect(result.extensions).toHaveLength(1); | ||
| expect(result.extensions[0].id).toBe('ext3'); | ||
| expect(result.total).toBe(3); | ||
| }); | ||
|
|
||
| it('should search extensions by name', async () => { | ||
| fetchMock.mockResolvedValue({ | ||
| ok: true, | ||
| json: async () => mockExtensions, | ||
| }); | ||
|
|
||
| const results = await client.searchExtensions('one'); | ||
| expect(results).toHaveLength(1); | ||
| expect(results[0].id).toBe('ext1'); | ||
| }); | ||
|
|
||
| it('should search extensions by description', async () => { | ||
| fetchMock.mockResolvedValue({ | ||
| ok: true, | ||
| json: async () => mockExtensions, | ||
| }); | ||
|
|
||
| const results = await client.searchExtensions('Second'); | ||
| expect(results).toHaveLength(1); | ||
| expect(results[0].id).toBe('ext2'); | ||
| }); | ||
|
|
||
| it('should get an extension by ID', async () => { | ||
| fetchMock.mockResolvedValue({ | ||
| ok: true, | ||
| json: async () => mockExtensions, | ||
| }); | ||
|
|
||
| const result = await client.getExtension('ext2'); | ||
| expect(result).toBeDefined(); | ||
| expect(result?.id).toBe('ext2'); | ||
| }); | ||
|
|
||
| it('should return undefined if extension not found', async () => { | ||
| fetchMock.mockResolvedValue({ | ||
| ok: true, | ||
| json: async () => mockExtensions, | ||
| }); | ||
|
|
||
| const result = await client.getExtension('non-existent'); | ||
| expect(result).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('should cache the fetch result', async () => { | ||
| fetchMock.mockResolvedValue({ | ||
| ok: true, | ||
| json: async () => mockExtensions, | ||
| }); | ||
|
|
||
| await client.getExtensions(); | ||
| await client.getExtensions(); | ||
|
|
||
| expect(fetchMock).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('should throw an error if fetch fails', async () => { | ||
| fetchMock.mockResolvedValue({ | ||
| ok: false, | ||
| statusText: 'Not Found', | ||
| }); | ||
|
|
||
| await expect(client.getExtensions()).rejects.toThrow( | ||
| 'Failed to fetch extensions: Not Found', | ||
| ); | ||
| }); | ||
| }); | ||
This file contains hidden or 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,90 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2025 Google LLC | ||
|
chrstnb marked this conversation as resolved.
Outdated
|
||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
|
chrstnb marked this conversation as resolved.
|
||
| export interface RegistryExtension { | ||
| id: string; | ||
| rank: number; | ||
| url: string; | ||
| fullName: string; | ||
| repoDescription: string; | ||
| stars: number; | ||
| lastUpdated: string; | ||
| extensionName: string; | ||
| extensionVersion: string; | ||
| extensionDescription: string; | ||
| avatarUrl: string; | ||
| hasMCP: boolean; | ||
| hasContext: boolean; | ||
| hasHooks: boolean; | ||
| hasSkills: boolean; | ||
| hasCustomCommands: boolean; | ||
| isGoogleOwned: boolean; | ||
| licenseKey: string; | ||
| } | ||
|
|
||
| export class ExtensionRegistryClient { | ||
| private static readonly REGISTRY_URL = | ||
| 'https://geminicli.com/extensions.json'; | ||
| private cache: RegistryExtension[] | null = null; | ||
|
chrstnb marked this conversation as resolved.
Outdated
|
||
|
|
||
| async getExtensions( | ||
| page: number = 1, | ||
| limit: number = 10, | ||
| orderBy: 'ranking' | 'alphabetical' = 'ranking', | ||
| ): Promise<{ extensions: RegistryExtension[]; total: number }> { | ||
| const allExtensions = [...(await this.fetchAllExtensions())]; | ||
|
|
||
| switch (orderBy) { | ||
| case 'ranking': | ||
| allExtensions.sort((a, b) => a.rank - b.rank); | ||
| break; | ||
| case 'alphabetical': | ||
| allExtensions.sort((a, b) => | ||
| a.extensionName.localeCompare(b.extensionName), | ||
| ); | ||
| break; | ||
| default: | ||
| break; | ||
| } | ||
|
chrstnb marked this conversation as resolved.
|
||
|
|
||
| const startIndex = (page - 1) * limit; | ||
| const endIndex = startIndex + limit; | ||
| return { | ||
| extensions: allExtensions.slice(startIndex, endIndex), | ||
| total: allExtensions.length, | ||
| }; | ||
| } | ||
|
|
||
| async searchExtensions(query: string): Promise<RegistryExtension[]> { | ||
| const allExtensions = await this.fetchAllExtensions(); | ||
| const lowerQuery = query.toLowerCase(); | ||
| return allExtensions.filter( | ||
| (ext) => | ||
| ext.extensionName.toLowerCase().includes(lowerQuery) || | ||
|
chrstnb marked this conversation as resolved.
Outdated
|
||
| ext.extensionDescription.toLowerCase().includes(lowerQuery) || | ||
| ext.fullName.toLowerCase().includes(lowerQuery), | ||
| ); | ||
| } | ||
|
|
||
| async getExtension(id: string): Promise<RegistryExtension | undefined> { | ||
| const allExtensions = await this.fetchAllExtensions(); | ||
| return allExtensions.find((ext) => ext.id === id); | ||
| } | ||
|
|
||
| private async fetchAllExtensions(): Promise<RegistryExtension[]> { | ||
| if (this.cache) { | ||
| return this.cache; | ||
| } | ||
|
|
||
| const response = await fetch(ExtensionRegistryClient.REGISTRY_URL); | ||
| if (!response.ok) { | ||
| throw new Error(`Failed to fetch extensions: ${response.statusText}`); | ||
| } | ||
|
chrstnb marked this conversation as resolved.
Outdated
|
||
|
|
||
| this.cache = (await response.json()) as RegistryExtension[]; | ||
| return this.cache; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.