Files
baserow/web-frontend/test/unit/integrations/ai/aiAgentServiceForm.spec.js
Tsering PaljorandJeremie Pardou 250748c8dc feat: Workspace level integrations (#6547)
* Add ADR-013 for workspace level integrations

* Add implementation

* Improve context section and remove technical points

* Move bulk replacement point to v2

* Remove workspace-level ownership on import since manual promotion should handle this.

* Add a glossary section

* Do Lazy creation + Celery migration

* Remove demotion

* Tighten the Options considered section

* Add Decision describing where the integrations are managed

* Update implementation plan after changes to ADR

* Sync plan and adr

* Rename adr and implementation

* Add the shared application that owns workspace integrations

* Create the shared application of every workspace

* Let only workspace admins manage workspace integrations

* Let applications use the integrations of their workspace.

* Trash workspace integrations in their workspace.

* Copy workspace integrations into exports and keep them in duplicates.

* Workspace admins can share integrations with every application in the workspace.

* Load the shared application of each workspace in the frontend and keep it out of application lists.

* Manage workspace integrations from an Integrations page in workspace settings.

* Offer workspace integrations in the integration dropdowns of every application.

* Move promoted integrations to the workspace list in other open tabs.

* Let admins make an app's integration available to the whole workspace from its integration settings.

* Preselect a workspace integration for new nodes and data sources when the app has none of its own.

* fix tests

* Delete implementation file

* More fixes after rebase

* Fix tests

* Let builders of an application see and use the workspace integrations from it.

* Show the workspace integrations, read-only, in the integration settings of applications.

* Keep a dashboard's integration out of promotion to the workspace.

* Leave the shared applications out of the admin dashboard's application count.

* Create the shared application of the workspaces made by the fill commands.

* Say in the promotion modal that an integration nobody authorized will run as the promoting admin.

* Say in the promotion modal that undoing it won't make the integration run as the previous person again.

* Ignore integration ids in sample data when publishing copies the workspace integrations an app uses.

* Let only undoing a promotion move an integration out of the workspace.

* Let the shared application backfill stop itself after 5 minutes and skip workspaces another transaction has locked.

* Say in the workspace integrations settings that published applications and workflows keep their own copy until they're published again.

* Leave the shared application out of the assistant evals' application counts.

* Refresh the workspace integrations when the Builder's data, events, users and integrations panels open.

* Say that anything new using a promoted integration blocks undoing the promotion, even from its own application.

* Warn when a database button sends through a workspace integration with no host or token.

* Refuse roles and invitations on the shared application.

* Fix tests

* Show an application only the workspace integrations of types it can use.

* Say in the promotion modal what builders of other applications can do as the integration.

* Leave the shared application type out of the API docs for creating and updating applications.

* Check who receives a workspace integration event in one permission call.

* Say which workspaces have a shared application and who creates it.

* Let a redone promotion record what uses the integration again.

* Update docstrings

* Stop template visitors from listing the integrations of the template's workspace.

* Let database buttons find their own copy of a workspace integration after an import.

* Say what holds the workspace row lock while a shared application is created.

* Fix tests

* Stop the workspace integration form component from being made reactive.

* Make test runs use a new copy of the workflow after an integration it uses changes.

* Hide the Members settings tab from members who can't list the workspace members.

* Add changelog

* Fix migration conflict

* Fix migration conflict

* Fix test

* Update comments and ADR number

* Link to the workspace integrations from the sidebar's Settings menu.

* fix: refresh agent action errors when workspace integrations change

* fix: move workspace integrations into workspace settings

* fix: use share icon for workspace integration promotion

* refactor: use explicit shared application relations

* feat: centralize default integration selection

* refactor: share integration change notifications

* refactor: prepare integration mappings before import

* refactor: collect integration references during export

* refactor: prepare integration ownership changes without a new hook

* test: restore registered service model after mocking

* test: cover shared integration defaults in Builder

* test: include Slack in Agent workspace integration choices

* Fix migrations

* Stop template visitors from listing the workspace integrations through an application of the template.

* Only offer integrations of the user source's integration type when updating a user source.

* Say the workspace integration copy means published Application Builder apps and automation workflows.

* Use the same spacing under both integration settings sections and enlarge their headings.

* Keep the shared application out of automatic snapshot schedules.

* Fix test after rebase

---------

Co-authored-by: Jeremie Pardou <571533+jrmi@users.noreply.github.com>
2026-11-27 15:49:32 +04:00

561 lines
17 KiB
JavaScript

import { defineComponent, reactive } from 'vue'
import { mountSuspended } from '@nuxt/test-utils/runtime'
import { enableAutoUnmount, flushPromises } from '@vue/test-utils'
import AIAgentServiceForm from '@baserow/modules/integrations/ai/components/services/AIAgentServiceForm'
import { AIAgentServiceType } from '@baserow/modules/integrations/ai/serviceTypes'
const FormGroupStub = defineComponent({
name: 'FormGroup',
props: {
label: { type: String, default: null },
error: { type: Boolean, default: false },
},
template:
'<div class="form-group" :data-label="label" :data-error="error"><slot /><slot name="helper" /><slot name="error" /></div>',
})
const DropdownStub = defineComponent({
name: 'Dropdown',
props: { modelValue: { type: [String, Number], default: null } },
emits: ['update:modelValue'],
template:
'<select :value="modelValue" @change="$emit(\'update:modelValue\', $event.target.value)"><slot /></select>',
})
const DropdownItemStub = defineComponent({
name: 'DropdownItem',
props: {
name: { type: String, required: true },
value: { type: String, required: true },
disabled: { type: Boolean, default: false },
},
template:
'<option :value="value" :data-value="value" :disabled="disabled" :aria-disabled="disabled">{{ name }}</option>',
})
const IntegrationDropdownStub = defineComponent({
name: 'IntegrationDropdown',
props: {
modelValue: { type: Number, default: null },
integrations: { type: Array, required: true },
},
emits: ['update:modelValue'],
template:
'<select :value="modelValue" @change="$emit(\'update:modelValue\', Number($event.target.value))"><option v-for="integration in integrations" :key="integration.id" :value="integration.id">{{ integration.id }}</option></select>',
})
const PassthroughStub = defineComponent({ template: '<div />' })
enableAutoUnmount(afterEach)
const openAIModelType = {
getType: () => 'openai',
getName: () => 'OpenAI',
getMaxTemperature: () => 2,
isIntegrationSettingsComplete: (settings) => Boolean(settings.api_key),
isBuiltInProviderType: () => true,
}
const anthropicModelType = {
getType: () => 'anthropic',
getName: () => 'Anthropic',
getMaxTemperature: () => 1,
isIntegrationSettingsComplete: (settings) => Boolean(settings.api_key),
isBuiltInProviderType: () => true,
}
const modelTypes = { openai: openAIModelType, anthropic: anthropicModelType }
const workspace = {
id: 1,
generative_ai_models_enabled: { openai: ['legacy-model'] },
ai_features: { ai_agent: { models: { openai: ['db-model'] } } },
}
async function mountForm({
integration,
integrations = [integration],
defaultValues,
workspace: workspaceValue = workspace,
}) {
return await mountSuspended(AIAgentServiceForm, {
props: {
application: { id: 1, workspace: { id: 1 } },
defaultValues,
},
global: {
stubs: {
FormGroup: FormGroupStub,
Dropdown: DropdownStub,
DropdownItem: DropdownItemStub,
IntegrationDropdown: IntegrationDropdownStub,
InjectedFormulaInput: PassthroughStub,
RadioGroup: PassthroughStub,
FormInput: PassthroughStub,
Button: PassthroughStub,
},
mocks: {
$t: (key) => key,
$store: {
getters: {
'integration/getUsableIntegrations': () => integrations,
'integration/getIntegrationById': (application, id) =>
integrations.find((candidate) => candidate.id === id),
'workspace/get': () => workspaceValue,
},
},
$registry: {
getAll: () => modelTypes,
get: (namespace, type) => {
if (namespace !== 'generativeAIModel') {
return {}
}
if (!modelTypes[type]) {
throw new Error(`Missing model type: ${type}`)
}
return modelTypes[type]
},
},
},
},
})
}
describe('AIAgentServiceForm', () => {
test('lists models available to the ai_agent feature', async () => {
const wrapper = await mountForm({
integration: { id: 5, type: 'ai', ai_settings: {} },
defaultValues: { integration_id: 5, ai_generative_ai_type: 'openai' },
})
await flushPromises()
expect(wrapper.find('[data-value="db-model"]').exists()).toBe(true)
expect(wrapper.find('[data-value="legacy-model"]').exists()).toBe(false)
})
test('uses generic models when feature availability is missing', async () => {
const wrapper = await mountForm({
workspace: { ...workspace, ai_features: undefined },
integration: { id: 5, type: 'ai', ai_settings: {} },
defaultValues: { integration_id: 5, ai_generative_ai_type: 'openai' },
})
await flushPromises()
expect(wrapper.find('[data-value="legacy-model"]').exists()).toBe(true)
expect(wrapper.find('[data-value="db-model"]').exists()).toBe(false)
})
test('preserves the selected model through availability updates', async () => {
const workspaceValue = reactive({
...workspace,
ai_features: undefined,
})
const wrapper = await mountForm({
workspace: workspaceValue,
integration: { id: 5, type: 'ai', ai_settings: {} },
defaultValues: {
integration_id: 5,
ai_generative_ai_type: 'openai',
ai_generative_ai_model: 'legacy-model',
},
})
await flushPromises()
const modelField = wrapper.get(
'[data-label="aiAgentServiceForm.modelLabel"]'
)
expect(modelField.get('select').element.value).toBe('legacy-model')
expect(modelField.attributes('data-error')).toBe('false')
workspaceValue.ai_features = {
ai_agent: { models: { openai: ['db-model'] } },
}
await flushPromises()
expect(modelField.get('select').element.value).toBe('legacy-model')
expect(modelField.attributes('data-error')).toBe('true')
expect(
modelField.get('[data-value="legacy-model"]').attributes('aria-disabled')
).toBe('true')
expect(modelField.get('[data-value="db-model"]').exists()).toBe(true)
workspaceValue.ai_features.ai_agent.models.openai.push('legacy-model')
await flushPromises()
expect(modelField.get('select').element.value).toBe('legacy-model')
expect(modelField.attributes('data-error')).toBe('false')
expect(
modelField.get('[data-value="legacy-model"]').attributes('aria-disabled')
).toBe('false')
workspaceValue.ai_features.ai_agent.models.openai = ['legacy-model']
await flushPromises()
expect(modelField.get('select').element.value).toBe('legacy-model')
expect(modelField.find('[data-value="db-model"]').exists()).toBe(false)
expect(modelField.attributes('data-error')).toBe('false')
})
test('marks a selected model unavailable when its feature eligibility is removed', async () => {
const workspaceValue = reactive({
...workspace,
ai_features: { ai_agent: { models: { openai: ['db-model'] } } },
})
const wrapper = await mountForm({
workspace: workspaceValue,
integration: { id: 5, type: 'ai', ai_settings: {} },
defaultValues: {
integration_id: 5,
ai_generative_ai_type: 'openai',
ai_generative_ai_model: 'db-model',
},
})
await flushPromises()
workspaceValue.ai_features.ai_agent.models = {}
await flushPromises()
const modelField = wrapper.get(
'[data-label="aiAgentServiceForm.modelLabel"]'
)
expect(modelField.get('select').element.value).toBe('db-model')
expect(modelField.attributes('data-error')).toBe('true')
expect(modelField.text()).toContain('selectAIModelForm.modelUnavailable')
expect(modelField.find('[data-value="legacy-model"]').exists()).toBe(false)
})
test('limits partial integration settings to generic workspace models when feature availability is missing', async () => {
const wrapper = await mountForm({
workspace: { ...workspace, ai_features: undefined },
integration: {
id: 5,
type: 'ai',
ai_settings: {
openai: { models: ['integration-only-model', 'legacy-model'] },
},
},
defaultValues: { integration_id: 5, ai_generative_ai_type: 'openai' },
})
await flushPromises()
expect(wrapper.find('[data-value="legacy-model"]').exists()).toBe(true)
expect(wrapper.find('[data-value="integration-only-model"]').exists()).toBe(
false
)
})
test('complete integration settings override the workspace models', async () => {
const wrapper = await mountForm({
integration: {
id: 5,
type: 'ai',
ai_settings: {
openai: { api_key: 'integration-key', models: ['blob-model'] },
},
},
defaultValues: { integration_id: 5, ai_generative_ai_type: 'openai' },
})
await flushPromises()
expect(wrapper.find('[data-value="blob-model"]').exists()).toBe(true)
expect(wrapper.find('[data-value="db-model"]').exists()).toBe(false)
})
test.each([
{
workspaceValue: workspace,
source: 'feature availability',
available: 'db-model',
excluded: 'legacy-model',
},
{
workspaceValue: { ...workspace, ai_features: undefined },
source: 'generic availability',
available: 'legacy-model',
excluded: 'db-model',
},
])(
'inherits $source for an own-key override without models',
async ({ workspaceValue, available, excluded }) => {
const wrapper = await mountForm({
workspace: workspaceValue,
integration: {
id: 5,
type: 'ai',
ai_settings: { openai: { api_key: 'integration-key' } },
},
defaultValues: {
integration_id: 5,
ai_generative_ai_type: 'openai',
ai_generative_ai_model: available,
},
})
await flushPromises()
expect(
wrapper.get(`[data-value="${available}"]`).attributes('aria-disabled')
).toBe('false')
expect(wrapper.find(`[data-value="${excluded}"]`).exists()).toBe(false)
expect(
wrapper
.get('[data-label="aiAgentServiceForm.modelLabel"]')
.attributes('data-error')
).toBe('false')
}
)
test('keeps an explicit empty own-key model list empty', async () => {
const wrapper = await mountForm({
integration: {
id: 5,
type: 'ai',
ai_settings: { openai: { api_key: 'integration-key', models: [] } },
},
defaultValues: { integration_id: 5, ai_generative_ai_type: 'openai' },
})
await flushPromises()
expect(wrapper.find('[data-value="db-model"]').exists()).toBe(false)
expect(wrapper.find('[data-value="legacy-model"]').exists()).toBe(false)
})
test('limits partial integration model settings to workspace ai_agent models', async () => {
const wrapper = await mountForm({
integration: {
id: 5,
type: 'ai',
ai_settings: {
openai: { models: ['integration-only-model', 'db-model'] },
},
},
defaultValues: { integration_id: 5, ai_generative_ai_type: 'openai' },
})
await flushPromises()
expect(wrapper.find('[data-value="db-model"]').exists()).toBe(true)
expect(wrapper.find('[data-value="integration-only-model"]').exists()).toBe(
false
)
})
test('keeps a stale model visible but marks it unavailable', async () => {
const wrapper = await mountForm({
integration: { id: 5, type: 'ai', ai_settings: {} },
defaultValues: {
integration_id: 5,
ai_generative_ai_type: 'openai',
ai_generative_ai_model: 'gone-model',
},
})
await flushPromises()
const staleOption = wrapper.get('[data-value="gone-model"]')
expect(staleOption.attributes('aria-disabled')).toBe('true')
expect(staleOption.text()).toContain('gone-model')
const modelField = wrapper.get(
'[data-label="aiAgentServiceForm.modelLabel"]'
)
expect(modelField.attributes('data-error')).toBe('true')
expect(modelField.text()).toContain('selectAIModelForm.modelUnavailable')
})
test('keeps a stale provider visible while editing', async () => {
const wrapper = await mountForm({
integration: { id: 5, type: 'ai', ai_settings: {} },
defaultValues: {
integration_id: 5,
ai_generative_ai_type: 'anthropic',
ai_generative_ai_model: 'gone-model',
},
})
await flushPromises()
expect(wrapper.get('[data-value="anthropic"]').text()).toBe('Anthropic')
})
test('marks a provider from an uninstalled extension as unavailable', async () => {
const wrapper = await mountForm({
integration: { id: 5, type: 'ai', ai_settings: {} },
workspace: {
...workspace,
ai_features: {
ai_agent: { models: { 'removed-provider': ['removed-model'] } },
},
},
defaultValues: {
integration_id: 5,
ai_generative_ai_type: 'removed-provider',
ai_generative_ai_model: 'removed-model',
},
})
await flushPromises()
expect(wrapper.get('[data-value="removed-model"]').exists()).toBe(true)
expect(
wrapper
.get('[data-label="aiAgentServiceForm.modelLabel"]')
.attributes('data-error')
).toBe('true')
})
test('re-validates the selection when switching integration', async () => {
const wrapper = await mountForm({
integrations: [
{
id: 5,
type: 'ai',
ai_settings: {
openai: { api_key: 'first-key', models: ['gpt-4'] },
},
},
{
id: 6,
type: 'ai',
ai_settings: {
openai: { api_key: 'second-key', models: ['gpt-3.5'] },
},
},
],
defaultValues: {
integration_id: 5,
ai_generative_ai_type: 'openai',
ai_generative_ai_model: 'gpt-4',
},
})
await flushPromises()
await wrapper
.get('[data-label="aiAgentServiceForm.integrationLabel"] select')
.setValue('6')
await flushPromises()
expect(
wrapper.get('[data-label="aiAgentServiceForm.modelLabel"] select').element
.value
).toBe('gpt-3.5')
expect(wrapper.find('[data-value="gpt-4"]').exists()).toBe(false)
})
})
describe('AIAgentServiceType', () => {
const makeServiceType = (integration = null) =>
new AIAgentServiceType({
app: {
$i18n: { t: (key) => key },
$registry: {
getAll: () => modelTypes,
get: () => ({ getErrorMessage: () => null }),
},
$store: {
getters: {
'integration/getIntegrationById': () => integration,
},
},
},
})
const makeService = (model, integrationId = null) => ({
integration_id: integrationId,
ai_generative_ai_type: 'openai',
ai_generative_ai_model: model,
ai_prompt: { formula: 'Prompt' },
ai_output_type: 'text',
})
test('reports a model unavailable to the workspace ai_agent feature', () => {
const serviceType = makeServiceType()
expect(
serviceType.getErrorMessage({
service: makeService('gone-model'),
workspace,
})
).toBe('serviceType.errorAIModelUnavailable')
expect(
serviceType.getErrorMessage({
service: makeService('db-model'),
workspace,
})
).toBeNull()
})
test('uses generic model availability when feature payloads have not arrived', () => {
const serviceType = makeServiceType()
expect(
serviceType.getErrorMessage({
service: makeService('legacy-model'),
workspace: { ...workspace, ai_features: undefined },
})
).toBeNull()
})
test('defers availability validation until the integration is loaded', () => {
const application = { id: 1 }
const serviceType = makeServiceType()
expect(
serviceType.getErrorMessage({
service: makeService('blob-model', 5),
workspace,
application,
})
).toBeNull()
})
test('uses a complete integration override when checking availability', () => {
const application = { id: 1 }
const serviceType = makeServiceType({
id: 5,
ai_settings: {
openai: { api_key: 'integration-key', models: ['blob-model'] },
},
})
expect(
serviceType.getErrorMessage({
service: makeService('blob-model', 5),
workspace,
application,
})
).toBeNull()
})
test('validates an own-key override without models against the ai_agent allowlist', () => {
const application = { id: 1 }
const serviceType = makeServiceType({
id: 5,
ai_settings: { openai: { api_key: 'integration-key' } },
})
expect(
serviceType.getErrorMessage({
service: makeService('db-model', 5),
workspace,
application,
})
).toBeNull()
expect(
serviceType.getErrorMessage({
service: makeService('legacy-model', 5),
workspace,
application,
})
).toBe('serviceType.errorAIModelUnavailable')
})
test('reports a provider from an uninstalled extension as unavailable', () => {
const serviceType = makeServiceType()
expect(
serviceType.getErrorMessage({
service: {
...makeService('removed-model'),
ai_generative_ai_type: 'removed-provider',
},
workspace: {
...workspace,
ai_features: {
ai_agent: { models: { 'removed-provider': ['removed-model'] } },
},
},
})
).toBe('serviceType.errorAIModelUnavailable')
})
})