@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
ts 148 lines 5.96 KB
Raw
1 import { describe, expect, test } from 'bun:test';
2 import { spawnSync } from 'node:child_process';
3 import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
4 import { tmpdir } from 'node:os';
5 import { join, resolve } from 'node:path';
6 import { z } from 'zod';
7
8 const workflowSchema = z.object({
9 jobs: z.record(z.string(), z.object({
10 defaults: z.object({ run: z.object({ shell: z.string().optional() }) }).optional(),
11 steps: z.array(z.object({ name: z.string(), run: z.string().optional(), shell: z.string().optional() })),
12 })),
13 });
14 const workflows = {
15 publish: workflowSchema.parse(Bun.YAML.parse(readFileSync(
16 resolve(process.cwd(), '..', '.github/workflows/docs-kb-publish.yml'), 'utf8',
17 ))),
18 };
19
20 function runStep(name: string, root: string, env: Record<string, string> = {}) {
21 const definition = workflows.publish.jobs.publish!;
22 const step = definition.steps.find(step => step.name === name);
23 const script = step?.run;
24 if (!script) throw new Error(`Missing workflow step: ${name}`);
25 // Match the Actions runner: implicit bash omits pipefail; explicit bash enables it.
26 const shell = step?.shell ?? definition.defaults?.run.shell;
27 const shellFlags = shell === 'bash' ? ['--noprofile', '--norc', '-e', '-o', 'pipefail'] : ['-e'];
28 return spawnSync('bash', [...shellFlags, '-c', script], {
29 cwd: root,
30 encoding: 'utf8',
31 env: {
32 ...process.env,
33 PATH: `${join(root, 'bin')}:${process.env.PATH}`,
34 FIXTURE_ROOT: root,
35 RUNNER_TEMP: root,
36 GUIDE_SLUG: 'sdk-example',
37 ...env,
38 },
39 });
40 }
41
42 function fixture() {
43 const root = mkdtempSync(join(tmpdir(), 'kb-publication-'));
44 mkdirSync(join(root, 'bin'));
45 writeFileSync(join(root, 'bin/bun'), `#!/bin/bash
46 set -euo pipefail
47 if [ "$2" = verify:kb ]; then
48 cat "$FIXTURE_ROOT/report.json"
49 exit "\${VERIFY_EXIT:-0}"
50 fi
51 `, { mode: 0o755 });
52 return root;
53 }
54
55 describe('KB publication workflow', () => {
56 test('opens a PR for review and never merges it or waits on other KB PRs', () => {
57 const steps = workflows.publish.jobs.publish!.steps;
58 const raw = readFileSync(resolve(process.cwd(), '..', '.github/workflows/docs-kb-publish.yml'), 'utf8');
59
60 expect(steps.map(step => step.name)).not.toContain('Wait for earlier KB publications');
61 expect(steps.map(step => step.name)).not.toContain('Merge once checks pass');
62 expect(raw).not.toContain('gh pr merge');
63 expect(raw).not.toContain('docs/content/kb/');
64 });
65
66 test('names the PR branch from a checked portal or Zen request', () => {
67 const root = fixture();
68 try {
69 const branchFor = (env: Record<string, string>) => {
70 writeFileSync(join(root, 'output'), '');
71 const result = runStep('Check request', root, { GITHUB_OUTPUT: join(root, 'output'), ...env });
72 return result.status === 0 ? readFileSync(join(root, 'output'), 'utf8').trim() : null;
73 };
74 const zen = {
75 SOURCE: 'zen', REQUEST_ID: 'zen-composiohq-composio-5340-1',
76 ORIGIN_URL: 'https://github.com/ComposioHQ/composio/pull/5340', ORIGIN_AUTHOR: 'octo-cat',
77 };
78
79 expect(branchFor({ DRAFT_ID: '12' })).toBe('branch=docs/kb-front-desk-12');
80 expect(branchFor(zen)).toBe('branch=docs/kb-zen-composiohq-composio-5340-1');
81 expect(branchFor({ DRAFT_ID: '12; rm -rf /' })).toBeNull();
82 expect(branchFor({ ...zen, REQUEST_ID: 'Zen/../main' })).toBeNull();
83 expect(branchFor({ ...zen, ORIGIN_URL: 'https://evil.example/pull/1' })).toBeNull();
84 expect(branchFor({ ...zen, ORIGIN_AUTHOR: 'a b\nc' })).toBeNull();
85 expect(branchFor({ SOURCE: 'other', DRAFT_ID: '12' })).toBeNull();
86 } finally {
87 rmSync(root, { recursive: true, force: true });
88 }
89 });
90
91 test('blocks support-facing prose even when verification reports it as a warning', () => {
92 const root = fixture();
93 try {
94 writeFileSync(join(root, 'report.json'), JSON.stringify({
95 findings: [{ severity: 'warning', kind: 'unedited-support-prose', guideSlug: 'sdk-example' }],
96 }));
97
98 const result = runStep('Regenerate KB pages', root);
99
100 expect(result.status).toBe(1);
101 expect(result.stderr).toContain('Rewrite it for public readers');
102 } finally {
103 rmSync(root, { recursive: true, force: true });
104 }
105 });
106
107 test('allows a proposal when only another guide has support-facing prose', () => {
108 const root = fixture();
109 try {
110 writeFileSync(join(root, 'report.json'), JSON.stringify({
111 findings: [{ severity: 'warning', kind: 'unedited-support-prose', guideSlug: 'other-guide' }],
112 }));
113 expect(runStep('Regenerate KB pages', root).status).toBe(0);
114 } finally {
115 rmSync(root, { recursive: true, force: true });
116 }
117 });
118
119 test("blocks on this guide's or KB-wide errors, not on other guides' errors", () => {
120 const root = fixture();
121 try {
122 writeFileSync(join(root, 'report.json'), JSON.stringify({
123 findings: [{ severity: 'warning', kind: 'review-window-cliff' }],
124 }));
125 expect(runStep('Regenerate KB pages', root).status).toBe(0);
126
127 writeFileSync(join(root, 'report.json'), JSON.stringify({
128 findings: [{ severity: 'error', kind: 'review-window-expired', guideSlug: 'other-guide' }],
129 }));
130 expect(runStep('Regenerate KB pages', root, { VERIFY_EXIT: '1' }).status).toBe(0);
131
132 writeFileSync(join(root, 'report.json'), JSON.stringify({
133 findings: [{ severity: 'error', kind: 'unknown-tool-slug', guideSlug: 'sdk-example' }],
134 }));
135 expect(runStep('Regenerate KB pages', root, { VERIFY_EXIT: '1' }).status).toBe(1);
136
137 writeFileSync(join(root, 'report.json'), JSON.stringify({
138 findings: [{ severity: 'error', kind: 'manifest-error', guideSlug: '' }],
139 }));
140 expect(runStep('Regenerate KB pages', root, { VERIFY_EXIT: '1' }).status).toBe(1);
141
142 writeFileSync(join(root, 'report.json'), 'crashed');
143 expect(runStep('Regenerate KB pages', root, { VERIFY_EXIT: '1' }).status).not.toBe(0);
144 } finally {
145 rmSync(root, { recursive: true, force: true });
146 }
147 });
148 });