import { afterEach, describe, expect, it, vi } from 'vitest' import { flushPromises, mount, type VueWrapper } from '@vue/test-utils' import { NCheckbox, NDatePicker, NInput, NInputNumber, NSelect } from 'naive-ui' import ImageAuthorizationSettings from '@/features/image-authorization/ImageAuthorizationSettings.vue' import { imageAuthorizationApi } from '@/features/image-authorization/api' import { generationConfigApi } from '@/features/generation-config/api' import { ApiError } from '@/lib/http' import { billingCatalog, imageAuthorizationsFixture } from '../../fixtures/billing' /** 保持真实 Naive 表单生命周期独立。 */ let wrapper: VueWrapper> | undefined afterEach(() => { wrapper?.unmount() vi.restoreAllMocks() }) /** * 创建隔离授权设置,捕获正式登记与撤销输入。 * @returns 请求替身及原数据 */ async function setup() { const list = imageAuthorizationsFixture() vi.spyOn(generationConfigApi, 'catalog').mockResolvedValue(billingCatalog) vi.spyOn(imageAuthorizationApi, 'list').mockResolvedValue(list) const create = vi .spyOn(imageAuthorizationApi, 'create') .mockResolvedValue({ authorizationId: 'created', status: 'approved' }) const revoke = vi.spyOn(imageAuthorizationApi, 'revoke').mockImplementation(async () => { list.authorizations[0]!.status = 'revoked' return { authorizationId: 'grant', status: 'revoked' } }) wrapper = mount(ImageAuthorizationSettings, { props: { projectId: 'billing-project', disabled: false } }) await flushPromises() return { list, create, revoke } } /** 完整填写用户实际批准的金额、次数、时间与范围。 */ async function fill() { const selects = wrapper!.findAllComponents(NSelect) selects .find(item => item.attributes('aria-label') === '授权型号')! .vm.$emit('update:value', ['gpt-image-2.5-flare']) selects.find(item => item.attributes('aria-label') === '授权范围')!.vm.$emit('update:value', ['keyframe']) for (const [label, value] of [ ['累计预算', '1.123456'], ['单次预留', '0.123456'], ['目标编号', 'shot-1,shot-2'], ['批准人', '隔离批准人'], ['批准说明', '仅隔离测试'] ] as const) wrapper! .findAll('label.workspace-form-field') .find(item => item.text().startsWith(label))! .findComponent(NInput) .vm.$emit('update:value', value) wrapper!.findComponent(NInputNumber).vm.$emit('update:value', 3) wrapper!.findComponent(NDatePicker).vm.$emit('update:value', Date.UTC(2099, 0, 1)) wrapper!.findComponent(NCheckbox).vm.$emit('update:checked', true) await flushPromises() } describe('项目生成设置中的 OpenAI 授权管理', () => { it('打开时金额、次数、日期、批准均为空,不能自动登记', async () => { const { create } = await setup() expect( wrapper! .findAllComponents(NInput) .slice(0, 2) .map(item => item.props('value')) ).toEqual(['', '']) expect(wrapper!.findComponent(NInputNumber).props('value')).toBeNull() expect(wrapper!.findComponent(NDatePicker).props('value')).toBeNull() expect(wrapper!.findComponent(NCheckbox).props('checked')).toBe(false) await wrapper!.vm.save() expect(create).not.toHaveBeenCalled() expect(wrapper!.text()).toContain('请完整填写') }) it('金额转换为整数微美元且超时后使用原幂等键,不自动重提', async () => { const { create } = await setup() create.mockRejectedValueOnce(new ApiError('网络未知', 0)) await fill() await wrapper!.vm.save() expect(create).toHaveBeenCalledOnce() expect(wrapper!.findAllComponents(NInput)[0]!.props('value')).toBe('1.123456') await wrapper!.vm.save() expect(create).toHaveBeenCalledTimes(2) expect(create.mock.calls[0]![1]).toEqual(create.mock.calls[1]![1]) expect(create.mock.calls[0]![1]).toMatchObject({ mode: 'standing', budgetMicros: 1123456, perRequestMicros: 123456, maxRequests: 3, targetIds: ['shot-1', 'shot-2'], acknowledgeVariableBilling: true, currency: 'USD' }) }) it('撤销需要明确操作者和原因,保留原授权与历史请求', async () => { const { list, create, revoke } = await setup() await wrapper! .findAll('button') .find(item => item.text() === '撤销此授权')! .trigger('click') await wrapper!.vm.save() expect(revoke).not.toHaveBeenCalled() const inputs = wrapper!.findAllComponents(NInput) inputs[0]!.vm.$emit('update:value', '隔离撤销人') inputs[1]!.vm.$emit('update:value', '停止后续领取') await flushPromises() await wrapper!.vm.save() expect(revoke).toHaveBeenCalledExactlyOnceWith('billing-project', 'grant', { revokedBy: '隔离撤销人', reason: '停止后续领取' }) expect(list.authorizations).toHaveLength(1) expect(list.authorizations[0]!.status).toBe('revoked') expect(create).not.toHaveBeenCalled() }) })