import { afterEach, describe, expect, it, vi } from 'vitest' import { clearScopeRule, fetchScopeRules, setScopeRule } from './scopeRules' afterEach(()=>vi.unstubAllGlobals()) describe('scope rule API',()=>{ it('lists rules for exactly one configured connection',async()=>{ const fetchMock=vi.fn().mockResolvedValue(new Response(JSON.stringify({data:[{target_type:'directory',connection_id:'conn-1',directory_path:'产品',visibility:'internal',updated_by:'engineer',reason:'内部资料',updated_at:'2026-09-08T00:00:00Z'}],summary:{total:1}}))) vi.stubGlobal('fetch',fetchMock) const rules=await fetchScopeRules('conn-1') expect(rules).toHaveLength(1) expect(fetchMock).toHaveBeenCalledWith('/api/v1/scope-rules?connection_id=conn-1',expect.objectContaining({headers:{Accept:'application/json'}})) }) it('sets and clears rules with explicit audited payloads',async()=>{ const fetchMock=vi.fn().mockImplementation(()=>Promise.resolve(new Response(JSON.stringify({data:{target_type:'directory',connection_id:'conn-1',directory_path:'产品',visibility:'internal'}})))) vi.stubGlobal('fetch',fetchMock) const target={target_type:'directory' as const,connection_id:'conn-1',directory_path:'产品',source_item_id:''} await setScopeRule({...target,visibility:'internal',reason:'内部资料'}) await clearScopeRule({...target,reason:'恢复来源设置'}) expect(fetchMock.mock.calls[0][1]).toMatchObject({method:'PUT',headers:{Accept:'application/json','Content-Type':'application/json','Idempotency-Key':expect.any(String)}}) expect(JSON.parse(fetchMock.mock.calls[0][1].body)).toEqual({...target,visibility:'internal',reason:'内部资料'}) expect(fetchMock.mock.calls[1][1]).toMatchObject({method:'DELETE',headers:{Accept:'application/json','Content-Type':'application/json','Idempotency-Key':expect.any(String)}}) expect(JSON.parse(fetchMock.mock.calls[1][1].body)).not.toHaveProperty('visibility') }) })