Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | /** |
| 2 | * Pure collection/source-mapping tests: no Python or uv required. |
| 3 | * Real mypy/SDK regression fixtures run separately in the Python-enabled CI job: |
| 4 | * bun scripts/python-snippets/test-helper.ts |
| 5 | */ |
| 6 | import { describe, test, expect } from 'bun:test'; |
| 7 | import { |
| 8 | collectPythonSnippets, |
| 9 | pythonSnippetViolations, |
| 10 | type PythonSnippet, |
| 11 | type PythonHelperResult, |
| 12 | } from '../../scripts/check-python-snippets'; |
| 13 | |
| 14 | |
| 15 | /** One snippet per call; `line` is the fence line, so snippet line N reports as `line + N`. */ |
| 16 | function snippet(id: string, code: string, line = 100): PythonSnippet { |
| 17 | return { id, path: 'content/docs/page.mdx', line, code, typeCheck: true }; |
| 18 | } |
| 19 | |
| 20 | |
| 21 | describe('collectPythonSnippets', () => { |
| 22 | const source = [ |
| 23 | '# Page', // 1 |
| 24 | '', // 2 |
| 25 | '<Tabs groupId="language" items={[\'Python\', \'TypeScript\']} persist>', // 3 |
| 26 | '<Tab value="Python">', // 4 |
| 27 | '```python', // 5 |
| 28 | 'composio.tools.get(user_id="u")', // 6 |
| 29 | '```', // 7 |
| 30 | '</Tab>', // 8 |
| 31 | '<Tab value="TypeScript">', // 9 |
| 32 | '```typescript', // 10 |
| 33 | 'await composio.tools.get("u");', // 11 |
| 34 | '```', // 12 |
| 35 | '</Tab>', // 13 |
| 36 | '</Tabs>', // 14 |
| 37 | '', // 15 |
| 38 | '{/* code-example: single-language reason="Python-only helper" */}', // 16 |
| 39 | '```py', // 17 |
| 40 | 'print("helper")', // 18 |
| 41 | '```', // 19 |
| 42 | '', // 20 |
| 43 | '<Tabs groupId="language" items={[\'Python (previous)\', \'TypeScript (previous)\']} persist>', // 21 |
| 44 | '<Tab value="Python (previous)">', // 22 |
| 45 | '```python', // 23 |
| 46 | 'from composio_openai import ComposioToolSet', // 24 |
| 47 | '```', // 25 |
| 48 | '</Tab>', // 26 |
| 49 | '<Tab value="TypeScript (previous)">', // 27 |
| 50 | '```typescript', // 28 |
| 51 | 'const toolset = new OpenAIToolSet();', // 29 |
| 52 | '```', // 30 |
| 53 | '</Tab>', // 31 |
| 54 | '</Tabs>', // 32 |
| 55 | '', // 33 |
| 56 | '{/* code-example: single-language reason="Legacy processors" */}', // 34 |
| 57 | '```python title="Python (previous)"', // 35 |
| 58 | 'toolset = ComposioToolSet()', // 36 |
| 59 | '```', // 37 |
| 60 | ].join('\n'); |
| 61 | const snippets = collectPythonSnippets([{ path: 'content/docs/page.mdx', source }]); |
| 62 | |
| 63 | test('collects python and py fences, including exempted ones, in document order', () => { |
| 64 | expect(snippets.map(({ line, code }) => [line, code])).toEqual([ |
| 65 | [5, 'composio.tools.get(user_id="u")'], |
| 66 | [17, 'print("helper")'], |
| 67 | [23, 'from composio_openai import ComposioToolSet'], |
| 68 | [35, 'toolset = ComposioToolSet()'], |
| 69 | ]); |
| 70 | }); |
| 71 | |
| 72 | test('assigns unique module ids', () => { |
| 73 | expect(new Set(snippets.map(({ id }) => id)).size).toBe(snippets.length); |
| 74 | for (const { id } of snippets) expect(id).toMatch(/^\w+$/); |
| 75 | }); |
| 76 | |
| 77 | test('skips type-checking legacy fences under a "(previous)" tab or title', () => { |
| 78 | expect(snippets.map(({ typeCheck }) => typeCheck)).toEqual([true, true, false, false]); |
| 79 | }); |
| 80 | }); |
| 81 | |
| 82 | describe('pythonSnippetViolations', () => { |
| 83 | const result: PythonHelperResult = { |
| 84 | syntax: [{ id: 's0', line: 2, message: 'invalid syntax' }], |
| 85 | api: [{ id: 's1', line: 3, message: 'SDK diagnostic [call-overload]' }], |
| 86 | other: [{ id: 's1', line: 4, message: 'Third-party diagnostic [arg-type]' }], |
| 87 | directives: [{ id: 's1', line: 5, message: 'Unused "type: ignore" comment [unused-ignore]' }], |
| 88 | mypy: [], |
| 89 | }; |
| 90 | |
| 91 | test('maps syntax, SDK, and suppression errors to source locations, keeping third-party errors separate', () => { |
| 92 | expect(pythonSnippetViolations([snippet('s0', 'pass'), snippet('s1', 'pass', 200)], result)).toEqual([ |
| 93 | { path: 'content/docs/page.mdx', line: 102, rule: 'python-syntax', message: 'invalid syntax' }, |
| 94 | { path: 'content/docs/page.mdx', line: 203, rule: 'python-api', message: 'SDK diagnostic [call-overload]' }, |
| 95 | { path: 'content/docs/page.mdx', line: 205, rule: 'python-directive', message: 'Unused "type: ignore" comment [unused-ignore]' }, |
| 96 | ]); |
| 97 | }); |
| 98 | |
| 99 | test('fails rather than silently dropping helper diagnostics for an unknown module', () => { |
| 100 | expect(() => pythonSnippetViolations([], result)).toThrow('unknown snippet s0'); |
| 101 | }); |
| 102 | }); |