@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, 03:37 UTC

next
mjs 131 lines 5.96 KB
Raw
1 import assert from 'node:assert/strict';
2 import { spawnSync } from 'node:child_process';
3 import {
4 mkdtempSync,
5 mkdirSync,
6 rmSync,
7 writeFileSync,
8 readFileSync,
9 copyFileSync,
10 cpSync,
11 symlinkSync,
12 } from 'node:fs';
13 import { tmpdir } from 'node:os';
14 import { join } from 'node:path';
15 import { test } from 'node:test';
16 import { compare, inventory, scan } from '../scripts/check-antislop.mjs';
17
18 test('the baseline binds allowances to source, file, rule and multiplicity', () => {
19 const finding = {
20 ruleId: 'ts-double-assertion',
21 file: 'ts/packages/core/src/user.ts',
22 lines: 'value as unknown as User',
23 };
24 const baseline = inventory([finding]);
25 assert.deepEqual(compare(inventory([finding]), baseline), { added: [], stale: [] });
26 for (const change of [
27 { lines: 'other as unknown as User' },
28 { file: 'ts/packages/cli/src/user.ts' },
29 { ruleId: 'another-rule' },
30 ]) {
31 const result = compare(inventory([{ ...finding, ...change }]), baseline);
32 assert.equal(result.added.length, 1);
33 assert.equal(result.stale.length, 1);
34 }
35 assert.equal(compare(inventory([finding, finding]), baseline).added.length, 1);
36 assert.equal(compare([], baseline).stale.length, 1);
37 assert.deepEqual(
38 inventory([{ ...finding, file: 'ts\\packages\\core\\src\\user.ts', lines: 'value\r\n' }]),
39 inventory([{ ...finding, lines: 'value\n' }])
40 );
41 assert.throws(() => compare([], [...baseline, ...baseline]), /duplicate/);
42 assert.throws(() => compare([], [{ ...baseline[0], count: -1 }]), /Invalid/);
43 });
44
45 test('scan discovers SDKs, CLI, scripts and hidden CI YAML while excluding generated code and tests', () => {
46 const cwd = mkdtempSync(join(tmpdir(), 'composio-antislop-'));
47 const write = (file, contents) => {
48 const path = join(cwd, file);
49 mkdirSync(join(path, '..'), { recursive: true });
50 writeFileSync(path, contents);
51 };
52 try {
53 copyFileSync(new URL('../sgconfig.yml', import.meta.url), join(cwd, 'sgconfig.yml'));
54 cpSync(new URL('../.ast-grep/rules', import.meta.url), join(cwd, '.ast-grep/rules'), {
55 recursive: true,
56 });
57 const cast = 'const user = value as unknown as User;';
58 for (const file of [
59 'ts/packages/core/src/user.ts',
60 'ts/packages/cli/src/view.tsx',
61 'ts/scripts/check.mts',
62 'scripts/check.cts',
63 ])
64 write(file, cast);
65 for (const file of [
66 'ts/vendor/sample.ts',
67 'ts/packages/core/generated/user.ts',
68 'ts/packages/slim/generated/user.ts',
69 'ts/packages/core/pack/generated/user.ts',
70 'ts/packages/core/src/user.test.ts',
71 'ts/packages/core/node_modules/lib/user.ts',
72 ])
73 write(file, cast);
74 write('python/composio/user.py', 'user = external() # type: ignore\n');
75 write('python/providers/sample/provider.py', 'try:\n work()\nexcept Exception:\n pass\n');
76 write('python/tests/test_user.py', 'user = external() # type: ignore\n');
77 write('.github/workflows/check.yaml', 'jobs:\n check:\n continue-on-error: true\n');
78 write('.github/actions/check/action.yml', 'runs:\n steps:\n - run: ""\n');
79 const findings = scan(cwd);
80 assert.equal(findings.length, 8);
81 assert.equal(new Set(findings.map(finding => finding.file)).size, 8);
82 assert.equal(findings.filter(finding => finding.ruleId === 'ts-double-assertion').length, 4);
83 assert.ok(findings.some(finding => finding.file === '.github/workflows/check.yaml'));
84 assert.ok(findings.some(finding => finding.file === '.github/actions/check/action.yml'));
85
86 // Exercise the public command, including the write boundary of --prune.
87 copyFileSync(
88 new URL('../scripts/check-antislop.mjs', import.meta.url),
89 join(cwd, 'scripts/check-antislop.mjs')
90 );
91 symlinkSync(new URL('../node_modules', import.meta.url), join(cwd, 'node_modules'), 'junction');
92 const baselinePath = join(cwd, '.ast-grep/baseline.json');
93 writeFileSync(baselinePath, JSON.stringify(inventory(findings)) + '\n');
94 const run = (...args) =>
95 spawnSync(process.execPath, [join(cwd, 'scripts/check-antislop.mjs'), ...args], {
96 cwd,
97 encoding: 'utf8',
98 });
99 assert.equal(run().status, 0);
100 write('ts/packages/core/src/user.ts', '\n\n' + cast);
101 assert.equal(run().status, 0, 'line-number changes must not invalidate allowances');
102 write('ts/packages/core/src/user.ts', cast + '\n' + cast);
103 assert.equal(run().status, 1, 'another identical occurrence must fail');
104 const beforePrune = readFileSync(baselinePath, 'utf8');
105 assert.equal(run('--prune').status, 1, 'pruning must reject additional findings');
106 assert.equal(readFileSync(baselinePath, 'utf8'), beforePrune);
107 write('ts/packages/core/src/user.ts', cast);
108 write('python/composio/user.py', 'user = parse_user(external())\n');
109 assert.equal(run().status, 1, 'resolved findings must be removed from the baseline');
110 assert.equal(run('--prune').status, 0);
111 assert.equal(JSON.parse(readFileSync(baselinePath, 'utf8')).length, 7);
112 assert.equal(run().status, 0);
113 assert.notEqual(run('--update-baseline').status, 0, 'unknown flags must fail');
114 write(
115 '.github/actions/check/action.yml',
116 'runs:\n steps:\n # The failure is enforced by a later outcome check.\n # ast-grep-ignore: yaml-continue-on-error\n - name: Capture audit\n continue-on-error: true\n run: pnpm audit\n'
117 );
118 assert.ok(!scan(cwd).some(finding => finding.file === '.github/actions/check/action.yml'));
119 write('ts/packages/core/src/suppressed.ts', '// Reason\n// ast-grep-ignore\n' + cast);
120 assert.ok(scan(cwd).some(finding => finding.ruleId === 'no-suppress-all'));
121 write(
122 'ts/packages/core/src/suppressed.ts',
123 '// Reason\n// ast-grep-ignore: ts-double-assertion\nconst user = parseUser(input);'
124 );
125 assert.ok(scan(cwd).some(finding => finding.ruleId === 'unused-suppression'));
126 write('sgconfig.yml', 'ruleDirs: [missing-rules]\n');
127 assert.throws(() => scan(cwd), /ast-grep failed/);
128 } finally {
129 rmSync(cwd, { recursive: true, force: true });
130 }
131 });