|
| 1 | +import type { CapData, PassStyle } from '@endo/marshal'; |
| 2 | +import { passStyleOf } from '@endo/pass-style'; |
| 3 | +import { describe, it, expect, vi } from 'vitest'; |
| 4 | + |
| 5 | +import { extractSingleRef } from './extract-ref.ts'; |
| 6 | +import { kunser, krefOf } from '../../services/kernel-marshal.ts'; |
| 7 | +import type { KRef } from '../../types.ts'; |
| 8 | + |
| 9 | +vi.mock('@endo/pass-style', async () => { |
| 10 | + const actual = await vi.importActual('@endo/pass-style'); |
| 11 | + return { |
| 12 | + ...actual, |
| 13 | + passStyleOf: vi.fn(), |
| 14 | + }; |
| 15 | +}); |
| 16 | + |
| 17 | +vi.mock('../../services/kernel-marshal.ts', () => ({ |
| 18 | + kunser: vi.fn(), |
| 19 | + krefOf: vi.fn(), |
| 20 | +})); |
| 21 | + |
| 22 | +describe('extract-ref', () => { |
| 23 | + describe('extractSingleRef', () => { |
| 24 | + const mockedPassStyleOf = vi.mocked(passStyleOf); |
| 25 | + const mockedKunser = vi.mocked(kunser); |
| 26 | + const mockedKrefOf = vi.mocked(krefOf); |
| 27 | + |
| 28 | + it.each([ |
| 29 | + { type: 'remotable', expected: 'ko123' }, |
| 30 | + { type: 'promise', expected: 'kp456' }, |
| 31 | + ])('returns the kref when style is $type', ({ type, expected }) => { |
| 32 | + const capData: CapData<KRef> = { body: 'test', slots: [] }; |
| 33 | + const mockValue = { type }; |
| 34 | + mockedKunser.mockReturnValue(mockValue); |
| 35 | + mockedPassStyleOf.mockReturnValue(type as PassStyle); |
| 36 | + mockedKrefOf.mockReturnValue(expected); |
| 37 | + const result = extractSingleRef(capData); |
| 38 | + expect(mockedKunser).toHaveBeenCalledWith(capData); |
| 39 | + expect(mockedPassStyleOf).toHaveBeenCalledWith(mockValue); |
| 40 | + expect(mockedKrefOf).toHaveBeenCalledWith(mockValue); |
| 41 | + expect(result).toBe(expected); |
| 42 | + }); |
| 43 | + |
| 44 | + it('returns null when style is neither remotable nor promise', () => { |
| 45 | + const capData: CapData<KRef> = { body: 'test', slots: [] }; |
| 46 | + const mockValue = { type: 'other' }; |
| 47 | + mockedKunser.mockReturnValue(mockValue); |
| 48 | + mockedPassStyleOf.mockReturnValue('other' as PassStyle); |
| 49 | + const result = extractSingleRef(capData); |
| 50 | + expect(mockedKunser).toHaveBeenCalledWith(capData); |
| 51 | + expect(mockedPassStyleOf).toHaveBeenCalledWith(mockValue); |
| 52 | + expect(mockedKrefOf).not.toHaveBeenCalled(); |
| 53 | + expect(result).toBeNull(); |
| 54 | + }); |
| 55 | + }); |
| 56 | +}); |
0 commit comments