@devries-cameron20 / composio Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/composio.git

Synced Nov 21, 04:45 UTC

next
ts 65 lines 2.43 KB
Raw
1 import { describe, expect, it } from '@effect/vitest';
2 import { Result } from 'effect';
3 import { JsonParsingError, parseJsonRecord } from 'src/utils/parse-json';
4
5 const parse = (raw: string) => Result.getOrThrowWith(parseJsonRecord(raw), error => error);
6 const parseError = (raw: string) => Result.flip(parseJsonRecord(raw)).pipe(Result.getOrNull);
7
8 describe('parseJsonRecord', () => {
9 it('parses strict JSON objects', () => {
10 expect(parse('{"foo": "bar", "n": 1}')).toEqual({ foo: 'bar', n: 1 });
11 });
12
13 it('parses JS-style object literals (unquoted keys, single quotes, trailing commas)', () => {
14 expect(parse("{ foo: 'bar', trailing: 1, }")).toEqual({ foo: 'bar', trailing: 1 });
15 expect(parse("{ nested: { a: [1, 2,], b: 'c' } }")).toEqual({
16 nested: { a: [1, 2], b: 'c' },
17 });
18 });
19
20 it('parses JSON with comments', () => {
21 expect(
22 parse(`{
23 // line comment
24 "foo": "bar",
25 /* block comment */
26 "n": 1
27 }`)
28 ).toEqual({ foo: 'bar', n: 1 });
29 });
30
31 it('fails for JSON values that are not objects', () => {
32 expect(parseError('[1, 2, 3]')).toBeInstanceOf(JsonParsingError);
33 expect(parseError('"hello"')).toBeInstanceOf(JsonParsingError);
34 expect(parseError('42')).toBeInstanceOf(JsonParsingError);
35 expect(parseError('null')).toBeInstanceOf(JsonParsingError);
36 });
37
38 it('does not execute code embedded in the input', () => {
39 const globals = globalThis as { __pwned?: boolean };
40 delete globals.__pwned;
41
42 expect(parseError('(globalThis.__pwned = true, {})')).toBeInstanceOf(JsonParsingError);
43 expect(globals.__pwned).toBeUndefined();
44
45 expect(parseError('process.exit(0)')).toBeInstanceOf(JsonParsingError);
46 expect(parseError("require('node:child_process').execSync('true')")).toBeInstanceOf(
47 JsonParsingError
48 );
49 expect(parseError('{ foo: (() => { globalThis.__pwned = true; })() }')).toBeInstanceOf(
50 JsonParsingError
51 );
52 expect(globals.__pwned).toBeUndefined();
53 });
54
55 it('rejects plain JS expressions instead of evaluating them', () => {
56 expect(parseError('1+1')).toBeInstanceOf(JsonParsingError);
57 expect(parseError('[1, 2].map(x => x)')).toBeInstanceOf(JsonParsingError);
58 expect(parseError('new Date()')).toBeInstanceOf(JsonParsingError);
59 });
60
61 it('fails for garbage input', () => {
62 expect(parseError('not json at all!')).toBeInstanceOf(JsonParsingError);
63 expect(parseError('')).toBeInstanceOf(JsonParsingError);
64 });
65 });