From ddcd696e1cfa25e1f2424b1c586fd45c6d70f0ab Mon Sep 17 00:00:00 2001 From: Daniel Saewitz Date: Thu, 4 Apr 2024 19:14:56 -0400 Subject: [PATCH] Clean up some minor issues; add ai? tests? --- README.md | 2 +- package.json | 2 +- src/types.ts | 4 +-- test/ai.test.ts | 80 ++++++++++++++++++++++++++++++++++++++++++++ test/enums.test.ts | 6 ++-- test/types.test-d.ts | 7 ++-- 6 files changed, 90 insertions(+), 11 deletions(-) create mode 100644 test/ai.test.ts diff --git a/README.md b/README.md index aa0d40c..41f4244 100644 --- a/README.md +++ b/README.md @@ -342,7 +342,7 @@ driver({ isUploaded: match.demo_uploaded, }, derived: { -+ isDisabled: (_, stateEnums, activeEnum) => (activeEnum ?? 0) <= stateEnums.isUploading, ++ isDisabled: (_, enums, activeEnum) => (activeEnum ?? 0) <= enums.isUploading, } }) ``` diff --git a/package.json b/package.json index 210ae8b..d4ac7e8 100644 --- a/package.json +++ b/package.json @@ -37,7 +37,7 @@ "name": "Daniel Saewitz (switz)", "url": "https://saewitz.com" }, - "version": "0.9.0", + "version": "0.10.0", "repository": { "type": "git", "url": "https://github.com/switz/driver.git" diff --git a/src/types.ts b/src/types.ts index 7427f9f..ee673de 100644 --- a/src/types.ts +++ b/src/types.ts @@ -11,7 +11,7 @@ type RequireAtLeastOne = Pick = ( states: Record, - stateEnums: Record, + enums: Record, activeEnum: number | undefined ) => unknown; @@ -35,7 +35,7 @@ export type Return> = DerivedReturn type MetadataReturn = { activeState: T | undefined; activeEnum: number | undefined; - stateEnums: Record; + enums: Record; states: Record; }; diff --git a/test/ai.test.ts b/test/ai.test.ts new file mode 100644 index 0000000..20d9b3f --- /dev/null +++ b/test/ai.test.ts @@ -0,0 +1,80 @@ +// these were written with ai? + +import { test, expect } from 'bun:test'; +import driver from '../src/index.js'; + +test('state key ordering', () => { + const result = driver({ + states: { a1: true, a2: false }, + }); + expect(result.activeState).toBe('a1'); + expect(result.activeEnum).toBe(0); + expect(Object.keys(result.enums)).toEqual(['a1', 'a2']); +}); + +test('derived keys with undefined values', () => { + const result = driver({ + states: { a: true, b: false }, + derived: { foo: undefined }, + }); + expect(result.foo).toBeUndefined(); +}); + +test('derived keys with falsy values', () => { + const result = driver({ + states: { a: true, b: false }, + derived: { foo: 0, bar: false }, + }); + expect(result.foo).toBe(0); + expect(result.bar).toBe(false); +}); + +test('derived keys with functions', () => { + const result = driver({ + states: { a: true, b: false }, + derived: { foo: (states, enums) => Object.keys(states).length }, + }); + expect(result.foo).toBe(2); +}); + +test('derived keys with arrays', () => { + const result = driver({ + states: { a: true, b: false }, + derived: { foo: ['a', 'c'] }, + }); + expect(result.foo).toBe(true); +}); + +test('derived keys with objects', () => { + const result = driver({ + states: { a: true, b: false }, + derived: { foo: { a: 'bar', b: 'baz' } }, + }); + expect(result.foo).toBe('bar'); +}); + +test('empty states', () => { + const result = driver({ states: {} }); + expect(result.activeState).toBeUndefined(); + expect(result.activeEnum).toBeUndefined(); + expect(result.enums).toEqual({}); +}); + +test('no active state', () => { + const result = driver({ states: { a: false, b: false } }); + expect(result.activeState).toBeUndefined(); + expect(result.activeEnum).toBeUndefined(); +}); + +test('invalid state key type', () => { + expect(() => driver({ states: { 1: true } })).toThrow(); +}); + +test('invalid derived key type', () => { + expect(() => + driver({ + states: { a: true }, + derived: { foo: 123 }, + }) + ).not.toThrow(); +}); diff --git a/test/enums.test.ts b/test/enums.test.ts index 99335de..6250cc8 100644 --- a/test/enums.test.ts +++ b/test/enums.test.ts @@ -12,7 +12,7 @@ test('basic enum test', () => { isUploaded: false, }, derived: { - isDisabled: (_, stateEnums, activeEnum) => (activeEnum ?? 0) <= stateEnums.isUploading, + isDisabled: (_, enums, activeEnum) => (activeEnum ?? 0) <= enums.isUploading, text: { isNotRecorded: 'Demo Disabled', isUploading: 'Demo Uploading...', @@ -36,7 +36,7 @@ test('basic enum test2', () => { isUploaded: true, }, derived: { - isDisabled: (_, stateEnums, activeEnum) => (activeEnum ?? 0) <= stateEnums.isUploading, + isDisabled: (_, enums, activeEnum) => (activeEnum ?? 0) <= enums.isUploading, text: { isNotRecorded: 'Demo Disabled', isUploading: 'Demo Uploading...', @@ -58,7 +58,7 @@ test('enums: when no state is true', () => { isUploaded: false, }, derived: { - isDisabled: (_, stateEnums, activeEnum) => (activeEnum ?? 0) <= stateEnums.isUploading, + isDisabled: (_, enums, activeEnum) => (activeEnum ?? 0) <= enums.isUploading, text: { isNotRecorded: 'Demo Disabled', isUploading: 'Demo Uploading...', diff --git a/test/types.test-d.ts b/test/types.test-d.ts index 472c5ee..ee04456 100644 --- a/test/types.test-d.ts +++ b/test/types.test-d.ts @@ -18,7 +18,7 @@ const derived = driver({ expectType(derived.isDisabled); expectType(derived.activeEnum); expectType<'hello' | 'foobar' | 'test' | undefined>(derived.activeState); -expectType>(derived.stateEnums); +expectType>(derived.enums); expectType(derived.optionalParams); const allDerived = driver({ @@ -31,14 +31,13 @@ const allDerived = driver({ params: { hello: 'hello', foobar: 'hi', - test: 'foo' + test: 'foo', }, }, }); expectType(allDerived.params); - // expect an error because no params are passed into a flag expectError( driver({ @@ -61,7 +60,7 @@ expectError( foobar: false, }, derived: { - stateEnums: { + enums: { foo: 1, }, },