From 5c96ee4d2941b484fe1960a8d57acaa739b62507 Mon Sep 17 00:00:00 2001 From: r266-tech Date: Mon, 16 Nov 2026 05:53:22 +0800 Subject: [PATCH] fix(channels): persist Bedrock compatibility as a boolean --- frontend/src/views/admin/ChannelsView.vue | 16 +- .../ChannelsView.bedrockCompat.spec.ts | 177 ++++++++++++++++++ 2 files changed, 181 insertions(+), 12 deletions(-) create mode 100644 frontend/src/views/admin/__tests__/ChannelsView.bedrockCompat.spec.ts diff --git a/frontend/src/views/admin/ChannelsView.vue b/frontend/src/views/admin/ChannelsView.vue index 5ee185131e..3633f27267 100644 --- a/frontend/src/views/admin/ChannelsView.vue +++ b/frontend/src/views/admin/ChannelsView.vue @@ -1196,18 +1196,10 @@ function formToAPI(): { group_ids: number[], model_pricing: ChannelModelPricing[ delete featuresConfig.codex_image_generation_bridge } - const bedrockCCCompat: Record = {} - for (const section of form.platforms) { - if (!section.enabled) continue - if (section.platform === 'anthropic') { - bedrockCCCompat[section.platform] = !!section.bedrock_cc_compat - } - } - if (Object.keys(bedrockCCCompat).length > 0) { - featuresConfig.bedrock_cc_compat = bedrockCCCompat - } else { - delete featuresConfig.bedrock_cc_compat - } + // The backend reads a channel-level boolean; send false explicitly to clear it on update. + featuresConfig.bedrock_cc_compat = form.platforms.some( + section => section.enabled && section.platform === 'anthropic' && section.bedrock_cc_compat + ) return { group_ids: uniqueGroupIds, model_pricing, model_mapping, features_config: featuresConfig } } diff --git a/frontend/src/views/admin/__tests__/ChannelsView.bedrockCompat.spec.ts b/frontend/src/views/admin/__tests__/ChannelsView.bedrockCompat.spec.ts new file mode 100644 index 0000000000..1291095480 --- /dev/null +++ b/frontend/src/views/admin/__tests__/ChannelsView.bedrockCompat.spec.ts @@ -0,0 +1,177 @@ +import { defineComponent } from 'vue' +import { flushPromises, mount } from '@vue/test-utils' +import { beforeEach, describe, expect, it, vi } from 'vitest' +import type { Channel, CreateChannelRequest, UpdateChannelRequest } from '@/api/admin/channels' +import ChannelsView from '@/views/admin/ChannelsView.vue' + +const { listChannels, createChannel, updateChannel, getGroups, showSuccess, showError } = vi.hoisted(() => ({ + listChannels: vi.fn(), + createChannel: vi.fn(), + updateChannel: vi.fn(), + getGroups: vi.fn(), + showSuccess: vi.fn(), + showError: vi.fn() +})) + +vi.mock('@/api/admin', () => ({ + adminAPI: { + channels: { list: listChannels, create: createChannel, update: updateChannel }, + groups: { getAll: getGroups }, + settings: { getWebSearchEmulationConfig: vi.fn().mockResolvedValue({ enabled: false }) } + } +})) + +vi.mock('@/stores/app', () => ({ useAppStore: () => ({ showSuccess, showError }) })) + +vi.mock('vue-i18n', async () => ({ + ...await vi.importActual('vue-i18n'), + useI18n: () => ({ t: (key: string) => key }) +})) + +function mountView() { + return mount(ChannelsView, { + global: { + stubs: { + AppLayout: defineComponent({ template: '
' }), + TablePageLayout: defineComponent({ + template: '
' + }), + BaseDialog: defineComponent({ + props: ['show'], + template: '
' + }), + DataTable: defineComponent({ + props: ['data'], + template: '
' + }), + Pagination: true, + ConfirmDialog: true, + EmptyState: true, + Select: true, + Icon: true, + PlatformIcon: true, + PricingEntryCard: true + } + } + }) +} + +type View = ReturnType + +async function clickButton(wrapper: View, text: string) { + const button = wrapper.findAll('button').find(button => button.text() === text) + expect(button).toBeDefined() + await button!.trigger('click') + await flushPromises() +} + +function platformCheckbox(wrapper: View, platform: string) { + return wrapper.findAll('label') + .find(label => label.text() === `admin.groups.platforms.${platform}`)! + .get('input[type="checkbox"]') +} + +function bedrockToggle(wrapper: View) { + return wrapper.findAll('[role="switch"]') + .find(toggle => toggle.element.parentElement?.textContent?.includes('admin.channels.form.bedrockCCCompat'))! +} + +function makeChannel(features: Record): Channel { + return { + id: 1, name: 'Bedrock relay', description: '', status: 'active', + billing_model_source: 'channel_mapped', restrict_models: false, + features_config: features, group_ids: [11, 22], model_pricing: [], model_mapping: {}, + apply_pricing_to_account_stats: false, account_stats_pricing_rules: [], + created_at: '2026-01-01T00:00:00Z', updated_at: '2026-01-01T00:00:00Z' + } +} + +describe('ChannelsView Bedrock compatibility', () => { + let stored: Channel[] + + beforeEach(() => { + vi.clearAllMocks() + localStorage.clear() + stored = [] + listChannels.mockImplementation(async () => ({ items: stored, total: stored.length })) + getGroups.mockResolvedValue([ + { id: 11, name: 'Anthropic group', platform: 'anthropic', rate_multiplier: 1 }, + { id: 22, name: 'OpenAI group', platform: 'openai', rate_multiplier: 1 } + ]) + createChannel.mockImplementation(async (request: CreateChannelRequest) => { + const channel = makeChannel(request.features_config || {}) + channel.group_ids = request.group_ids || [] + stored = [channel] + return channel + }) + updateChannel.mockImplementation(async (_id: number, request: UpdateChannelRequest) => { + stored = [{ ...stored[0], ...request } as Channel] + return stored[0] + }) + }) + + it.each([true, false])('creates and reopens a channel with compatibility %s', async enabled => { + const wrapper = mountView() + await flushPromises() + await clickButton(wrapper, 'admin.channels.createChannel') + await wrapper.get('input[required]').setValue('Bedrock relay') + await platformCheckbox(wrapper, 'anthropic').setValue(true) + await clickButton(wrapper, 'admin.groups.platforms.anthropic') + await wrapper.findAll('label').find(label => label.text().includes('Anthropic group'))! + .get('input[type="checkbox"]').setValue(true) + if (enabled) await bedrockToggle(wrapper).trigger('click') + + await wrapper.get('#channel-form').trigger('submit') + await flushPromises() + expect(showError).not.toHaveBeenCalled() + expect(createChannel).toHaveBeenCalledOnce() + expect(createChannel.mock.calls[0][0].features_config.bedrock_cc_compat).toBe(enabled) + + await clickButton(wrapper, 'common.edit') + await clickButton(wrapper, 'admin.groups.platforms.anthropic') + expect(bedrockToggle(wrapper).attributes('aria-checked')).toBe(String(enabled)) + wrapper.unmount() + }) + + it.each([true, false])('updates compatibility to %s while preserving other features', async enabled => { + stored = [makeChannel({ bedrock_cc_compat: true, future_feature: { enabled: true } })] + const wrapper = mountView() + await flushPromises() + await clickButton(wrapper, 'common.edit') + await clickButton(wrapper, 'admin.groups.platforms.anthropic') + expect(bedrockToggle(wrapper).attributes('aria-checked')).toBe('true') + if (!enabled) await bedrockToggle(wrapper).trigger('click') + + await wrapper.get('#channel-form').trigger('submit') + await flushPromises() + expect(showError).not.toHaveBeenCalled() + expect(updateChannel).toHaveBeenCalledOnce() + expect(updateChannel.mock.calls[0][1].features_config).toMatchObject({ + bedrock_cc_compat: enabled, + future_feature: { enabled: true } + }) + + await clickButton(wrapper, 'common.edit') + await clickButton(wrapper, 'admin.groups.platforms.anthropic') + expect(bedrockToggle(wrapper).attributes('aria-checked')).toBe(String(enabled)) + wrapper.unmount() + }) + + it('explicitly disables compatibility when the Anthropic section is removed', async () => { + stored = [makeChannel({ bedrock_cc_compat: true, future_feature: { enabled: true } })] + const wrapper = mountView() + await flushPromises() + await clickButton(wrapper, 'common.edit') + await platformCheckbox(wrapper, 'anthropic').setValue(false) + + await wrapper.get('#channel-form').trigger('submit') + await flushPromises() + expect(showError).not.toHaveBeenCalled() + expect(updateChannel).toHaveBeenCalledOnce() + expect(updateChannel.mock.calls[0][1]).toMatchObject({ + group_ids: [22], + features_config: { bedrock_cc_compat: false, future_feature: { enabled: true } } + }) + wrapper.unmount() + }) +})