bindings.spec.ts 1.6 KB

1234567891011121314151617181920212223
  1. import { describe, expect, it } from 'vitest'
  2. import { validateBindings } from '../src/bindings.ts'
  3. describe('program binding validation', () => {
  4. it.each(['$name', '', 'while', 'lambda', 'console'])('rejects unusable global %s', (global) => {
  5. expect(() => validateBindings({ program: '', bindings: [{ global, functions: {} }] })).toThrow()
  6. })
  7. it('rejects duplicated globals and accepts arbitrary own member names', () => {
  8. const functions = Object.create(null) as Record<string, () => Promise<string>>
  9. functions.__proto__ = async () => 'own'
  10. expect(validateBindings({ program: '', bindings: [{ global: 'tools', functions }] }).get('tools')?.functions).toBe(functions)
  11. expect(() => validateBindings({ program: '', bindings: [{ global: 'tools', functions }, { global: 'tools', functions }] })).toThrow('duplicate')
  12. })
  13. it.each(['$name', 'while', 'console', 'tools'])('rejects invalid error class name %s', (name) => {
  14. expect(() => validateBindings({ program: '', bindings: [{ global: 'tools', functions: {}, errorClass: { name, memberNameProperty: 'member' } }] })).toThrow()
  15. })
  16. it.each(['', 'name', '__member__'])('rejects invalid error member %s', (memberNameProperty) => {
  17. expect(() => validateBindings({ program: '', bindings: [{ global: 'tools', functions: {}, errorClass: { name: 'ToolError', memberNameProperty } }] })).toThrow()
  18. })
  19. it('rejects two injected constructors with the same name', () => {
  20. expect(() => validateBindings({ program: '', bindings: ['first', 'second'].map(global => ({ global, functions: {}, errorClass: { name: 'ToolError', memberNameProperty: 'member' } })) })).toThrow('duplicate')
  21. })
  22. })