import { beforeEach, expect, it, vi } from 'vitest' import { flushPromises, mount } from '@vue/test-utils' import ElementPlus from 'element-plus' const calls = vi.hoisted(() => ({ getIntegrationTrust:vi.fn() })) vi.mock('../admin-api', async original => ({...await original(),...calls})) import Panel from './TrustDeliveryPanel.vue' const trust={current_key_id:'current',keys:[{key_id:'current',public_key_pem:'PUBLIC PEM',revoked:false,spki_sha256:'abc'},{key_id:'old',public_key_pem:'OLD PEM',revoked:true,spki_sha256:'def'}]} beforeEach(()=>{vi.restoreAllMocks();calls.getIntegrationTrust.mockReset();calls.getIntegrationTrust.mockResolvedValue({data:{data:trust}})}) it('exports instance binding and only unrevoked keys without credentials',async()=>{ let content:unknown const blob=vi.spyOn(globalThis,'Blob').mockImplementation(function(parts){content=JSON.parse(String(parts![0]));return {} as Blob}) Object.defineProperty(URL,'createObjectURL',{configurable:true,value:vi.fn(()=> 'blob:test')});Object.defineProperty(URL,'revokeObjectURL',{configurable:true,value:vi.fn()}) vi.spyOn(HTMLAnchorElement.prototype,'click').mockImplementation(()=>{}) const instance={id:1,instance_id:'instance-test',license_id:1,customer_id:3,product_id:4,status:'provisioned',created_at:'',freeze_state:'',observed_state:'',entitlement_state:''} const w=mount(Panel,{props:{instance},global:{plugins:[ElementPlus]}});await flushPromises() expect(w.text()).toContain('abc');expect(w.text()).toContain('客户 ID(customer_id):3') expect(w.findAll('button').filter(b=>b.text().includes('PEM')&&b.attributes('disabled')!==undefined)).toHaveLength(2) await w.get('input').setValue('https://license.example.com') await w.findAll('button').find(b=>b.text()==='下载实例信任配置 JSON')!.trigger('click') expect(content).toMatchObject({format:'yhy-license-trust',api_base_url:'https://license.example.com',instance_id:'instance-test',customer_id:3,product_id:4,keys:[trust.keys[0]]}) expect(content).not.toHaveProperty('secret');expect(blob).toHaveBeenCalledTimes(1) await w.get('input').setValue('http://external.example.com');await w.findAll('button').find(b=>b.text()==='下载实例信任配置 JSON')!.trigger('click');expect(blob).toHaveBeenCalledTimes(1) w.unmount() }) it('clears stale keys if refreshing fails',async()=>{ const w=mount(Panel,{global:{plugins:[ElementPlus]}});await flushPromises();expect(w.text()).toContain('PUBLIC PEM') calls.getIntegrationTrust.mockRejectedValueOnce(new Error('未配置 v2 公钥环')) await w.findAll('button').find(b=>b.text()==='刷新公钥')!.trigger('click');await flushPromises() expect(w.text()).toContain('未配置 v2 公钥环');expect(w.text()).not.toContain('PUBLIC PEM');w.unmount() })