@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 97 lines 3.77 KB
Raw
1 import { afterEach, describe, expect, test } from 'bun:test';
2 import { mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, writeFileSync } from 'node:fs';
3 import { tmpdir } from 'node:os';
4 import { join } from 'node:path';
5 import { publishKbGuide } from '../../scripts/publish-kb-guide';
6
7 const roots: string[] = [];
8 const now = new Date('2026-08-18T00:00:00Z');
9 const request = {
10 action: 'upsert', slug: 'sdk-example', area: 'sdk', title: 'Example',
11 description: 'Public example.', body: '## Setup\n\nFollow these steps.',
12 topics: ['authentication'], tags: ['old-tag'],
13 };
14
15 function fixture(): string {
16 const root = mkdtempSync(join(tmpdir(), 'kb-publish-'));
17 roots.push(root);
18 mkdirSync(join(root, 'kb/source'), { recursive: true });
19 mkdirSync(join(root, 'kb/articles'));
20 mkdirSync(join(root, 'kb/guides'));
21 writeFileSync(join(root, 'kb/manifest.json'), JSON.stringify({
22 schemaVersion: 2,
23 topics: [{ slug: 'authentication', title: 'Authentication', description: 'Authentication guidance.', featuredRank: 1 }],
24 }));
25 return root;
26 }
27
28 function guide(root: string) {
29 return JSON.parse(readFileSync(join(root, 'kb/guides/sdk-example.json'), 'utf8'));
30 }
31
32 function contents(root: string): Map<string, string> {
33 return new Map(readdirSync(join(root, 'kb'), { recursive: true, withFileTypes: true })
34 .filter(entry => entry.isFile())
35 .map(entry => {
36 const path = join(entry.parentPath, entry.name);
37 return [path, readFileSync(path, 'utf8')];
38 }));
39 }
40
41 afterEach(() => {
42 for (const root of roots.splice(0)) rmSync(root, { recursive: true, force: true });
43 });
44
45 describe('approved KB guide publishing', () => {
46 test('clears explicit empty tags and retains omitted tags', () => {
47 const root = fixture();
48 publishKbGuide(request, root, now);
49 publishKbGuide({ action: 'upsert', slug: request.slug, body: request.body }, root, now);
50 expect(guide(root).tags).toEqual(['old-tag']);
51 publishKbGuide({ action: 'upsert', slug: request.slug, body: request.body, tags: [] }, root, now);
52 expect(guide(root).tags).toEqual([]);
53 expect(guide(root).managedBy).toBe('support-portal');
54 });
55
56 test('creates a valid guide with no tags', () => {
57 const root = fixture();
58 publishKbGuide({ ...request, tags: [] }, root, now);
59 expect(guide(root).tags).toEqual([]);
60 expect(readFileSync(join(root, 'kb/articles/sdk-example.md'), 'utf8')).toBe(`${request.body}\n`);
61 });
62
63 test('rejects explicitly empty topics without writing', () => {
64 const root = fixture();
65 publishKbGuide(request, root, now);
66 const before = contents(root);
67 expect(() => publishKbGuide({ ...request, topics: [] }, root, now))
68 .toThrow('At least one topic is required');
69 expect(contents(root)).toEqual(before);
70 });
71
72 test.each([
73 ['path traversal', { slug: '../escape' }],
74 ['leading hyphen', { slug: '-sdk-example' }],
75 ['empty segment', { slug: 'sdk--example' }],
76 ['trailing hyphen', { slug: 'sdk-example-' }],
77 ['oversized slug', { slug: `${'a'.repeat(60_000)}!` }],
78 ['null slug', { slug: null }],
79 ['unknown action', { action: 'unsupported' }],
80 ['invalid tags', { tags: 'not-an-array' }],
81 ['invalid topics', { topics: [123] }],
82 ])('rejects %s before changing files', (_label, change) => {
83 const root = fixture();
84 const before = contents(root);
85 expect(() => publishKbGuide({ ...request, ...change }, root, now)).toThrow();
86 expect(contents(root)).toEqual(before);
87 });
88
89 test('validates staged content before changing an existing guide', () => {
90 const root = fixture();
91 publishKbGuide(request, root, now);
92 const before = contents(root);
93 expect(() => publishKbGuide({ ...request, body: 'See T-12345 for details.' }, root, now))
94 .toThrow('Plain thread reference');
95 expect(contents(root)).toEqual(before);
96 });
97 });