Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

next
ts 76 lines 2.6 KB
Raw
1 import { describe, it } from '@effect/vitest';
2 import { deepStrictEqual } from '@effect/vitest/utils';
3 import { Config, ConfigProvider, Effect } from 'effect';
4 import {
5 flag,
6 hasEnvironmentRoot,
7 optionalFlag,
8 optionalLogLevel,
9 optionalString,
10 withFallback,
11 } from 'src/config/env';
12
13 const read = <A>(config: Config.Config<A>, env: Record<string, string>) =>
14 config.parse(ConfigProvider.fromEnvRecord(env));
15
16 describe('config readers', () => {
17 it.effect('optionalString trims and treats blank as unset', () =>
18 Effect.gen(function* () {
19 deepStrictEqual(yield* read(optionalString('X'), {}), undefined);
20 deepStrictEqual(yield* read(optionalString('X'), { X: ' ' }), undefined);
21 deepStrictEqual(yield* read(optionalString('X'), { X: ' value ' }), 'value');
22 })
23 );
24
25 it.effect('flags are tolerant: blank falls back, off-words disable, anything else enables', () =>
26 Effect.gen(function* () {
27 const flags = Config.all({
28 blank: flag('BLANK', true),
29 off: flag('OFF', true),
30 typo: flag('TYPO'),
31 unset: optionalFlag('UNSET'),
32 });
33 deepStrictEqual(yield* read(flags, { BLANK: '', OFF: 'No', TYPO: 'maybe' }), {
34 blank: true,
35 off: false,
36 typo: true,
37 unset: undefined,
38 });
39 })
40 );
41
42 it.effect(
43 'optionalLogLevel matches level names case-insensitively and ignores other values',
44 () =>
45 Effect.gen(function* () {
46 const level = optionalLogLevel('LEVEL');
47 deepStrictEqual(yield* read(level, {}), undefined);
48 deepStrictEqual(yield* read(level, { LEVEL: 'debug' }), 'Debug');
49 deepStrictEqual(yield* read(level, { LEVEL: ' Error ' }), 'Error');
50 deepStrictEqual(yield* read(level, { LEVEL: 'verbose' }), undefined);
51 })
52 );
53
54 it.effect('withFallback replaces only undefined', () =>
55 Effect.gen(function* () {
56 const value = withFallback(optionalString('X'), 'fallback');
57 deepStrictEqual(yield* read(value, {}), 'fallback');
58 deepStrictEqual(yield* read(value, { X: ' ' }), 'fallback');
59 deepStrictEqual(yield* read(value, { X: 'set' }), 'set');
60 })
61 );
62
63 it.effect('hasEnvironmentRoot matches a bare root and any underscore-prefixed child', () =>
64 Effect.gen(function* () {
65 const roots = Config.all({
66 codex: hasEnvironmentRoot('CODEX'),
67 claude: hasEnvironmentRoot('CLAUDE'),
68 });
69 deepStrictEqual(yield* read(roots, { CODEX_HOME: '/x', claude_home: '/y' }), {
70 codex: true,
71 claude: false,
72 });
73 deepStrictEqual(yield* read(roots, { CLAUDE: '1' }), { codex: false, claude: true });
74 })
75 );
76 });