dify/e2e/tests/marketplace-plugins.test.ts
yyh acb5ee29e1
test(e2e): validate generated console contracts (#39400)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
2026-07-22 07:48:37 +00:00

109 lines
3.6 KiB
TypeScript

import type { ConsoleClient } from '../support/api/console-client'
import { ORPCError } from '@orpc/client'
import { afterEach, describe, expect, it, vi } from 'vitest'
import { bootstrapMarketplacePlugins } from '../support/marketplace-plugins'
const createMarketplaceConsoleClient = (installError: unknown) => {
const installMarketplace = vi.fn().mockRejectedValue(installError)
const uploadPackage = vi.fn().mockResolvedValue({
unique_identifier: 'langgenius/test:1.0.0@package',
})
const installPackage = vi.fn().mockResolvedValue({ all_installed: true })
const consoleClient = {
workspaces: {
current: {
plugin: {
install: {
marketplace: { post: installMarketplace },
pkg: { post: installPackage },
},
list: {
installations: {
ids: { post: vi.fn().mockResolvedValue({ plugins: [] }) },
},
latestVersions: {
post: vi.fn().mockResolvedValue({
versions: {
'langgenius/test': {
unique_identifier: 'langgenius/test:1.0.0@marketplace',
},
},
}),
},
},
upload: { pkg: { post: uploadPackage } },
},
},
},
} as unknown as ConsoleClient
return { consoleClient, installPackage, uploadPackage }
}
const bootstrapTestPlugin = (consoleClient: ConsoleClient) =>
bootstrapMarketplacePlugins(
{ consoleClient, dryRun: false, resources: new Map() },
{
defaultPluginIds: ['langgenius/test'],
pluginIdsEnv: 'E2E_TEST_MARKETPLACE_PLUGIN_IDS',
title: 'Test marketplace plugin',
},
)
describe('bootstrapMarketplacePlugins', () => {
afterEach(() => {
vi.restoreAllMocks()
vi.unstubAllEnvs()
})
it('uses generated package upload when the API process cannot download from Marketplace', async () => {
vi.stubEnv('E2E_TEST_MARKETPLACE_PLUGIN_IDS', '')
vi.spyOn(globalThis, 'fetch').mockResolvedValue(new Response('plugin-package'))
const { consoleClient, installPackage, uploadPackage } = createMarketplaceConsoleClient(
new ORPCError('INTERNAL_SERVER_ERROR', {
data: {
body: {
message:
'Reached maximum retries (3) for URL https://marketplace.test/plugins/download',
},
},
status: 500,
}),
)
const result = await bootstrapTestPlugin(consoleClient)
expect(result.status).toBe('verified')
expect(uploadPackage).toHaveBeenCalledWith({
body: { pkg: expect.any(File) },
})
expect(installPackage).toHaveBeenCalledWith({
body: { plugin_unique_identifiers: ['langgenius/test:1.0.0@package'] },
})
})
it('does not hide unrelated generated client failures behind package upload', async () => {
vi.stubEnv('E2E_TEST_MARKETPLACE_PLUGIN_IDS', '')
const marketplaceFetch = vi
.spyOn(globalThis, 'fetch')
.mockResolvedValue(new Response('plugin-package'))
const { consoleClient, installPackage, uploadPackage } = createMarketplaceConsoleClient(
new ORPCError('INTERNAL_SERVER_ERROR', {
data: { body: { message: 'Database unavailable' } },
status: 500,
}),
)
const result = await bootstrapTestPlugin(consoleClient)
expect(result).toEqual(
expect.objectContaining({
reason: expect.stringContaining('Database unavailable'),
status: 'blocked',
}),
)
expect(marketplaceFetch).not.toHaveBeenCalled()
expect(uploadPackage).not.toHaveBeenCalled()
expect(installPackage).not.toHaveBeenCalled()
})
})