import { afterEach, expect, it, vi } from 'vitest' import { renderToStaticMarkup } from 'react-dom/server' import { associateLegacySource, LegacySourceAssociation } from './LegacySourceAssociation' afterEach(() => vi.unstubAllGlobals()) const reply = (status: number, data: unknown) => new Response(JSON.stringify(data), { status }) it('only sends exact snapshot and scope; association is not publishing', async () => { const fetchMock = vi .fn() .mockResolvedValue( reply(200, { associated: true, published: false, snapshot_id: 'snapshot', source_id: 'a'.repeat(64) }) ) vi.stubGlobal('fetch', fetchMock) expect(await associateLegacySource('internal', 'snapshot')).toBe('a'.repeat(64)) expect(fetchMock.mock.calls).toHaveLength(1) expect(fetchMock.mock.calls[0][0]).toBe('/api/v1/library/source-associations') expect(JSON.parse(fetchMock.mock.calls[0][1].body)).toEqual({ scope: 'internal', snapshot_id: 'snapshot' }) }) it('rejects wrong-snapshot and incomplete readback, and shows evidence errors', async () => { for (const value of [ { associated: true, published: false, snapshot_id: 'wrong', source_id: 'a'.repeat(64) }, { associated: true, snapshot_id: 'snapshot' }, ]) { vi.stubGlobal('fetch', vi.fn().mockResolvedValue(reply(200, value))) await expect(associateLegacySource('internal', 'snapshot')).rejects.toThrow('来源关联回执') } vi.stubGlobal('fetch', vi.fn().mockResolvedValue(reply(409, { error: 'original capture identity mismatch' }))) await expect(associateLegacySource('internal', 'snapshot')).rejects.toThrow('original capture identity mismatch') }) it('hides association controls from non-admin and distinguishes it from publication', () => { expect( renderToStaticMarkup( {}} /> ) ).toBe('') const html = renderToStaticMarkup( {}} /> ) expect(html).toContain('只追加治理关联') expect(html).toContain('disabled') expect(html).toContain('核验原证据并关联') })