|
| 1 | +import { describe, it, expect, vi } from 'vitest'; |
| 2 | + |
| 3 | +import { |
| 4 | + callable, |
| 5 | + constant, |
| 6 | + evaluateMetadata, |
| 7 | + resolveMetaDataSpec, |
| 8 | + source, |
| 9 | +} from './metadata.ts'; |
| 10 | + |
| 11 | +describe('constant', () => { |
| 12 | + it('returns a constant spec with the given value', () => { |
| 13 | + expect(constant(42)).toStrictEqual({ kind: 'constant', value: 42 }); |
| 14 | + }); |
| 15 | + |
| 16 | + it('evaluateMetadata returns the value regardless of args', () => { |
| 17 | + const spec = resolveMetaDataSpec(constant({ cost: 7 })); |
| 18 | + expect(evaluateMetadata(spec, [])).toStrictEqual({ cost: 7 }); |
| 19 | + expect(evaluateMetadata(spec, [1, 2, 3])).toStrictEqual({ cost: 7 }); |
| 20 | + }); |
| 21 | +}); |
| 22 | + |
| 23 | +describe('callable', () => { |
| 24 | + it('returns a callable spec wrapping the function', () => { |
| 25 | + const fn = (args: unknown[]) => args[0] as number; |
| 26 | + const spec = callable(fn); |
| 27 | + expect(spec).toStrictEqual({ kind: 'callable', fn }); |
| 28 | + }); |
| 29 | + |
| 30 | + it('evaluateMetadata calls fn with args', () => { |
| 31 | + const fn = vi.fn((args: unknown[]) => (args[0] as number) * 2); |
| 32 | + const spec = resolveMetaDataSpec(callable(fn)); |
| 33 | + expect(evaluateMetadata(spec, [5])).toBe(10); |
| 34 | + expect(fn).toHaveBeenCalledWith([5]); |
| 35 | + }); |
| 36 | +}); |
| 37 | + |
| 38 | +describe('source', () => { |
| 39 | + it('returns a source spec with the src string', () => { |
| 40 | + expect(source('(args) => args[0]')).toStrictEqual({ |
| 41 | + kind: 'source', |
| 42 | + src: '(args) => args[0]', |
| 43 | + }); |
| 44 | + }); |
| 45 | + |
| 46 | + it('resolveMetaDataSpec compiles source to callable via compartment', () => { |
| 47 | + const mockFn = (args: unknown[]) => args[0] as number; |
| 48 | + const compartment = { evaluate: vi.fn(() => mockFn) }; |
| 49 | + const spec = resolveMetaDataSpec(source('(args) => args[0]'), compartment); |
| 50 | + expect(spec.kind).toBe('callable'); |
| 51 | + expect(compartment.evaluate).toHaveBeenCalledWith('(args) => args[0]'); |
| 52 | + expect(evaluateMetadata(spec, [99])).toBe(99); |
| 53 | + }); |
| 54 | +}); |
| 55 | + |
| 56 | +describe('resolveMetaDataSpec', () => { |
| 57 | + it('passes constant spec through unchanged', () => { |
| 58 | + const spec = constant(42); |
| 59 | + expect(resolveMetaDataSpec(spec)).toStrictEqual(spec); |
| 60 | + }); |
| 61 | + |
| 62 | + it('passes callable spec through unchanged', () => { |
| 63 | + const fn = (_args: unknown[]) => 0; |
| 64 | + const spec = callable(fn); |
| 65 | + expect(resolveMetaDataSpec(spec)).toStrictEqual(spec); |
| 66 | + }); |
| 67 | + |
| 68 | + it("throws if kind is 'source' and no compartment supplied", () => { |
| 69 | + expect(() => resolveMetaDataSpec(source('() => 0'))).toThrow( |
| 70 | + "compartment required to evaluate 'source' metadata", |
| 71 | + ); |
| 72 | + }); |
| 73 | +}); |
| 74 | + |
| 75 | +describe('evaluateMetadata', () => { |
| 76 | + it('returns undefined when spec is undefined', () => { |
| 77 | + expect(evaluateMetadata(undefined, [])).toBeUndefined(); |
| 78 | + expect(evaluateMetadata(undefined, [1, 2])).toBeUndefined(); |
| 79 | + }); |
| 80 | +}); |
0 commit comments