|
| 1 | +import { describe, expect, test } from 'vitest'; |
| 2 | +import { AlgebraicType } from '../src/index'; |
| 3 | + |
| 4 | +describe('AlgebraicType', () => { |
| 5 | + test('intoMapKey handles all primitive types', () => { |
| 6 | + const primitiveTypes: Array<[any['tag'], any]> = [ |
| 7 | + ['Bool', true], |
| 8 | + ['I8', -8], |
| 9 | + ['U8', 8], |
| 10 | + ['I16', -16], |
| 11 | + ['U16', 16], |
| 12 | + ['I32', -32], |
| 13 | + ['U32', 32], |
| 14 | + ['I64', -64n], |
| 15 | + ['U64', 64n], |
| 16 | + ['I128', -128n], |
| 17 | + ['U128', 128n], |
| 18 | + ['I256', -256n], |
| 19 | + ['U256', 256n], |
| 20 | + ['F32', 32.32], |
| 21 | + ['F64', 64.64], |
| 22 | + ['String', 'hello'], |
| 23 | + ]; |
| 24 | + |
| 25 | + for (const [tag, value] of primitiveTypes) { |
| 26 | + const algebraicType = { tag, value: undefined }; |
| 27 | + const mapKey = AlgebraicType.intoMapKey(algebraicType, value); |
| 28 | + expect(mapKey).toBe(value); |
| 29 | + } |
| 30 | + }); |
| 31 | + |
| 32 | + test('intoMapKey handles complex types', () => { |
| 33 | + const productType = AlgebraicType.Product({ |
| 34 | + elements: [{ name: 'a', algebraicType: AlgebraicType.I32 }], |
| 35 | + }); |
| 36 | + const productValue = { a: 42 }; |
| 37 | + |
| 38 | + const mapKey = AlgebraicType.intoMapKey(productType, productValue); |
| 39 | + // Fallback for complex types is base64 encoding of serialized value |
| 40 | + expect(typeof mapKey).toBe('string'); |
| 41 | + // 42 as i32 little-endian is 2A000000, which is KgAAAA== in base64 |
| 42 | + expect(mapKey).toBe('KgAAAA=='); |
| 43 | + }); |
| 44 | + |
| 45 | + test('intoMapKey fallback serializes array types', () => { |
| 46 | + const arrayType = AlgebraicType.Array(AlgebraicType.U16); |
| 47 | + const arrayValue = [1, 2, 3]; |
| 48 | + |
| 49 | + const mapKey = AlgebraicType.intoMapKey(arrayType, arrayValue); |
| 50 | + expect(typeof mapKey).toBe('string'); |
| 51 | + // Serialized as: [len (u32), val1 (u16), val2 (u16), val3 (u16)] |
| 52 | + expect(mapKey).toBe('AwAAAAEAAgADAA=='); |
| 53 | + }); |
| 54 | +}); |
0 commit comments