Skip to content

Commit

Permalink
Clean up some minor issues; add ai? tests?
Browse files Browse the repository at this point in the history
  • Loading branch information
switz committed Apr 4, 2024
1 parent c4a5e24 commit ddcd696
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ driver({
isUploaded: match.demo_uploaded,
},
derived: {
+ isDisabled: (_, stateEnums, activeEnum) => (activeEnum ?? 0) <= stateEnums.isUploading,
+ isDisabled: (_, enums, activeEnum) => (activeEnum ?? 0) <= enums.isUploading,
}
})
```
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type RequireAtLeastOne<T, Keys extends keyof T = keyof T> = Pick<T, Exclude<keyo

type DerivedFn<StateKeys extends string> = (
states: Record<StateKeys, boolean | undefined>,
stateEnums: Record<StateKeys, number>,
enums: Record<StateKeys, number>,
activeEnum: number | undefined
) => unknown;

Expand All @@ -35,7 +35,7 @@ export type Return<T extends string, K extends DerivedConfig<T>> = DerivedReturn
type MetadataReturn<T extends string> = {
activeState: T | undefined;
activeEnum: number | undefined;
stateEnums: Record<T, number>;
enums: Record<T, number>;
states: Record<T, boolean | undefined>;
};

Expand Down
80 changes: 80 additions & 0 deletions test/ai.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
6 changes: 3 additions & 3 deletions test/enums.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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...',
Expand All @@ -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...',
Expand All @@ -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...',
Expand Down
7 changes: 3 additions & 4 deletions test/types.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const derived = driver({
expectType<boolean | undefined>(derived.isDisabled);
expectType<number | undefined>(derived.activeEnum);
expectType<'hello' | 'foobar' | 'test' | undefined>(derived.activeState);
expectType<Record<'hello' | 'foobar' | 'test', number>>(derived.stateEnums);
expectType<Record<'hello' | 'foobar' | 'test', number>>(derived.enums);
expectType<string | undefined>(derived.optionalParams);

const allDerived = driver({
Expand All @@ -31,14 +31,13 @@ const allDerived = driver({
params: {
hello: 'hello',
foobar: 'hi',
test: 'foo'
test: 'foo',
},
},
});

expectType<string | undefined>(allDerived.params);


// expect an error because no params are passed into a flag
expectError(
driver({
Expand All @@ -61,7 +60,7 @@ expectError(
foobar: false,
},
derived: {
stateEnums: {
enums: {
foo: 1,
},
},
Expand Down

0 comments on commit ddcd696

Please sign in to comment.