|
| 1 | +// these were written with ai? |
| 2 | + |
| 3 | +import { test, expect } from 'bun:test'; |
| 4 | +import driver from '../src/index.js'; |
| 5 | + |
| 6 | +test('state key ordering', () => { |
| 7 | + const result = driver({ |
| 8 | + states: { a1: true, a2: false }, |
| 9 | + }); |
| 10 | + expect(result.activeState).toBe('a1'); |
| 11 | + expect(result.activeEnum).toBe(0); |
| 12 | + expect(Object.keys(result.enums)).toEqual(['a1', 'a2']); |
| 13 | +}); |
| 14 | + |
| 15 | +test('derived keys with undefined values', () => { |
| 16 | + const result = driver({ |
| 17 | + states: { a: true, b: false }, |
| 18 | + derived: { foo: undefined }, |
| 19 | + }); |
| 20 | + expect(result.foo).toBeUndefined(); |
| 21 | +}); |
| 22 | + |
| 23 | +test('derived keys with falsy values', () => { |
| 24 | + const result = driver({ |
| 25 | + states: { a: true, b: false }, |
| 26 | + derived: { foo: 0, bar: false }, |
| 27 | + }); |
| 28 | + expect(result.foo).toBe(0); |
| 29 | + expect(result.bar).toBe(false); |
| 30 | +}); |
| 31 | + |
| 32 | +test('derived keys with functions', () => { |
| 33 | + const result = driver({ |
| 34 | + states: { a: true, b: false }, |
| 35 | + derived: { foo: (states, enums) => Object.keys(states).length }, |
| 36 | + }); |
| 37 | + expect(result.foo).toBe(2); |
| 38 | +}); |
| 39 | + |
| 40 | +test('derived keys with arrays', () => { |
| 41 | + const result = driver({ |
| 42 | + states: { a: true, b: false }, |
| 43 | + derived: { foo: ['a', 'c'] }, |
| 44 | + }); |
| 45 | + expect(result.foo).toBe(true); |
| 46 | +}); |
| 47 | + |
| 48 | +test('derived keys with objects', () => { |
| 49 | + const result = driver({ |
| 50 | + states: { a: true, b: false }, |
| 51 | + derived: { foo: { a: 'bar', b: 'baz' } }, |
| 52 | + }); |
| 53 | + expect(result.foo).toBe('bar'); |
| 54 | +}); |
| 55 | + |
| 56 | +test('empty states', () => { |
| 57 | + const result = driver({ states: {} }); |
| 58 | + expect(result.activeState).toBeUndefined(); |
| 59 | + expect(result.activeEnum).toBeUndefined(); |
| 60 | + expect(result.enums).toEqual({}); |
| 61 | +}); |
| 62 | + |
| 63 | +test('no active state', () => { |
| 64 | + const result = driver({ states: { a: false, b: false } }); |
| 65 | + expect(result.activeState).toBeUndefined(); |
| 66 | + expect(result.activeEnum).toBeUndefined(); |
| 67 | +}); |
| 68 | + |
| 69 | +test('invalid state key type', () => { |
| 70 | + expect(() => driver({ states: { 1: true } })).toThrow(); |
| 71 | +}); |
| 72 | + |
| 73 | +test('invalid derived key type', () => { |
| 74 | + expect(() => |
| 75 | + driver({ |
| 76 | + states: { a: true }, |
| 77 | + derived: { foo: 123 }, |
| 78 | + }) |
| 79 | + ).not.toThrow(); |
| 80 | +}); |
0 commit comments