import { jest } from '@jest/globals'; import * as def from '../dist/definitions.js'; import { Expression } from '../dist/expression.js'; describe('definitions', () => { it('should handle parameter', () => { expect(def.isParameter(null)).toBe(false); expect(def.isParameter(() => void 0)).toBe(false); expect(def.isParameter({ key: '', name: '', description: '', type: 'real', value: 0 })).toBe(true); expect(def.isParameter({ type: 'label' })).toBe(false); expect(def.isParameter({ name: '', description: '', items: [] })).toBe(false); }); it('should handle parameter group', () => { expect(def.isParameterGroup(null)).toBe(false); expect(def.isParameterGroup(() => void 0)).toBe(false); expect(def.isParameterGroup({ name: '', description: '', items: [] })).toBe(true); expect(def.isParameterGroup({ items: [] })).toBe(false); expect(def.isParameterGroup({ key: '', name: '', description: '', items: [] })).toBe(false); }); it('should handle parameter decoration', () => { expect(def.isParameterDecoration(null)).toBe(false); expect(def.isParameterDecoration(() => void 0)).toBe(false); expect(def.isParameterDecoration({ type: 'label' })).toBe(true); expect(def.isParameterDecoration({ name: '', description: '', items: [] })).toBe(false); expect(def.isParameterDecoration({ key: '', name: '', description: '', type: 'real', value: 0 })).toBe(false); }); it('should handle parameter decoration group', () => { expect(def.isParameterDecorationGroup(null)).toBe(false); expect(def.isParameterDecorationGroup(() => void 0)).toBe(false); expect(def.isParameterDecorationGroup({ items: [] })).toBe(true); expect(def.isParameterDecorationGroup({ name: '', description: '', items: [] })).toBe(false); }); it('should handle variable', () => { expect(def.isVariable(null)).toBe(false); expect(def.isVariable(() => void 0)).toBe(false); expect(def.isVariable({ key: '', value: 0 })).toBe(true); expect(def.isVariable({ key: '', name: '', description: '', type: 'real', value: 0 })).toBe(false); }); it('should handle choice', () => { expect(def.isChoice(null)).toBe(false); expect(def.isChoice(() => void 0)).toBe(false); expect(def.isChoice({ name: '', description: '' })).toBe(true); expect(def.isChoice({ key: null, name: '', description: '' })).toBe(true); expect(def.isChoice({ key: '', name: '', description: '' })).toBe(true); }); it('should handle constraint', () => { expect(def.isConstraint(null)).toBe(false); expect(def.isConstraint(() => void 0)).toBe(false); expect(def.isConstraint({ name: '', description: '' })).toBe(false); expect(def.isConstraint({ key: null, name: '', description: '' })).toBe(false); expect(def.isConstraint({ key: '', name: '', description: '' })).toBe(false); expect(def.isConstraint({ message: '' })).toBe(false); expect(def.isConstraint({ condition: '' })).toBe(false); expect(def.isConstraint({ message: '', condition: '' })).toBe(true); expect(def.isConstraint({ message: '', condition: '', level: 'error' })).toBe(true); expect(def.isConstraint({ message: '', condition: '', level: 'error', messageData: '' })).toBe(true); expect(def.isConstraint({ message: '', condition: '', messageData: {} })).toBe(true); expect(def.isConstraint({ message: '', condition: '', messageData: null })).toBe(true); expect(def.isConstraint({ message: '', condition: '', messageData: undefined })).toBe(true); expect(def.isConstraint({ message: '', condition: '', messageData: () => void 0 })).toBe(true); expect(def.isConstraint({ message: '', condition: '', messageData: 1 })).toBe(false); }); it('should generate track id', () => { const mockRandom = jest .spyOn(Math, 'random') .mockReturnValueOnce(0) .mockReturnValueOnce(1 - 2 ** -53); const id1 = def.generateTrackId(); const id2 = def.generateTrackId(); mockRandom.mockRestore(); expect(id1).toBe(1); expect(id2).toBe(0x7fff_ffff); }); it('should convert argument value', () => { // @ts-expect-error 未知类型 expect(def.toArgumentValue(undefined, { type: 'xxx', key: '', name: '', description: '', value: 1 })).toBe( null, ); expect(def.toArgumentValue(undefined, { type: 'real', key: '', name: '', description: '', value: 1 })).toBe( null, ); expect(def.toArgumentValue(null, { type: 'real', key: '', name: '', description: '', value: 1 })).toBe(null); expect(def.toArgumentValue('12', { type: 'real', key: '', name: '', description: '', value: 1 })).toBe(12); expect(def.toArgumentValue(null, { type: 'text', key: '', name: '', description: '', value: '1' })).toBe(''); expect(def.toArgumentValue(12, { type: 'text', key: '', name: '', description: '', value: '1' })).toBe('12'); expect( def.toArgumentValue(null, { type: 'grouped', key: '', name: '', description: '', value: {}, items: [] }), ).toBe(null); expect( def.toArgumentValue(null, { type: 'choice', key: '', name: '', description: '', value: 1, choices: [] }), ).toBe(null); expect( def.toArgumentValue(null, { type: 'choice', key: '', name: '', description: '', value: 1, choices: [{ key: '0', name: '', description: '' }], }), ).toBe(''); expect( def.toArgumentValue(null, { type: 'choice', key: '', name: '', description: '', value: 1, choices: [ { key: '0', name: '', description: '' }, { key: 1, name: '', description: '' }, ], }), ).toBe(''); expect(def.toArgumentValue(null, { type: 'logical', key: '', name: '', description: '', value: 1 })).toBe(null); expect(def.toArgumentValue('0', { type: 'logical', key: '', name: '', description: '', value: 1 })).toBe(0); expect( def.toArgumentValue(null, { type: 'logical', key: '', name: '', description: '', value: false, choices: [ { key: 0, name: '', description: '' }, { key: 1, name: '', description: '' }, ], }), ).toBe(null); expect( def.toArgumentValue(null, { type: 'logical', key: '', name: '', description: '', value: true, }), ).toBe(null); expect( def.toArgumentValue(null, { type: 'logical', key: '', name: '', description: '', value: 'true', }), ).toBe(''); expect( def.toArgumentValue(null, { type: 'logical', key: '', name: '', description: '', value: 'true', choices: [ { key: '0', name: '', description: '' }, { key: '1', name: '', description: '' }, ], }), ).toBe(''); expect( def.toArgumentValue(null, { type: 'multiSelect', key: '', name: '', description: '', value: [1], choices: [], }), ).toStrictEqual([]); expect( def.toArgumentValue(1, { type: 'multiSelect', key: '', name: '', description: '', value: [1], choices: [], }), ).toStrictEqual([1]); expect( def.toArgumentValue('', { type: 'multiSelect', key: '', name: '', description: '', value: [1], choices: [], }), ).toStrictEqual(null); expect( def.toArgumentValue('', { type: 'multiSelect', key: '', name: '', description: '', value: [1], choices: Expression(''), }), ).toStrictEqual(null); expect( def.toArgumentValue(['', '1', ' 2 '], { type: 'multiSelect', key: '', name: '', description: '', value: [1], choices: [], }), ).toStrictEqual(null); expect( def.toArgumentValue(['0', '1', ' 2 '], { type: 'multiSelect', key: '', name: '', description: '', value: [1], choices: [], }), ).toStrictEqual([0, 1, 2]); expect( def.toArgumentValue(['0', '1', ' 2 '], { type: 'multiSelect', key: '', name: '', description: '', value: [], choices: [], }), ).toStrictEqual([0, 1, 2]); expect( def.toArgumentValue([1, 2], { type: 'multiSelect', key: '', name: '', description: '', value: ['1'], choices: [], }), ).toStrictEqual(['1', '2']); expect( def.toArgumentValue(['', '1', ' 2 '], { type: 'multiSelect', key: '', name: '', description: '', value: [1], choices: Expression(''), }), ).toStrictEqual(null); }); });