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
| 1 | import { afterEach, describe, expect, test } from 'bun:test'; |
| 2 | import { mkdirSync, mkdtempSync, rmSync, symlinkSync, writeFileSync } from 'node:fs'; |
| 3 | import { tmpdir } from 'node:os'; |
| 4 | import { join } from 'node:path'; |
| 5 | import { buildKbCatalog } from '@/lib/kb/catalog'; |
| 6 | import { |
| 7 | createKbArticleReader, |
| 8 | createKbSourceReader, |
| 9 | getKbCatalog, |
| 10 | getPublishedKbGuides, |
| 11 | resolveKbAlias, |
| 12 | } from '@/lib/kb/repository'; |
| 13 | import type { KbManifest } from '@/lib/kb/types'; |
| 14 | |
| 15 | const temporaryDirectories: string[] = []; |
| 16 | |
| 17 | afterEach(() => { |
| 18 | for (const directory of temporaryDirectories.splice(0)) { |
| 19 | rmSync(directory, { recursive: true, force: true }); |
| 20 | } |
| 21 | }); |
| 22 | |
| 23 | const source = `--- |
| 24 | type: reference |
| 25 | title: Example |
| 26 | description: Public example. |
| 27 | category: platform/example |
| 28 | visibility: public |
| 29 | timestamp: 2026-07-20T00:00:00Z |
| 30 | tags: |
| 31 | - example |
| 32 | --- |
| 33 | # Example |
| 34 | |
| 35 | ## Stable answer |
| 36 | |
| 37 | This is safe public guidance. |
| 38 | |
| 39 | ## Second answer |
| 40 | |
| 41 | This is the second safe answer. |
| 42 | `; |
| 43 | |
| 44 | function manifest(overrides: Partial<KbManifest['guides'][number]> = {}): KbManifest { |
| 45 | return { |
| 46 | schemaVersion: 2, |
| 47 | topics: [ |
| 48 | { |
| 49 | slug: 'platform', |
| 50 | title: 'Platform', |
| 51 | description: 'Platform guidance.', |
| 52 | featuredRank: 1, |
| 53 | }, |
| 54 | ], |
| 55 | guides: [ |
| 56 | { |
| 57 | slug: 'stable-answer', |
| 58 | title: 'Stable answer', |
| 59 | description: 'A stable answer.', |
| 60 | sources: [ |
| 61 | { sourcePath: 'kb/platform/example/public.md', sourceHeading: 'Stable answer' }, |
| 62 | { sourcePath: 'kb/platform/example/public.md', sourceHeading: 'Second answer' }, |
| 63 | ], |
| 64 | topics: ['platform'], |
| 65 | tags: ['example'], |
| 66 | aliases: ['old-answer'], |
| 67 | relatedGuides: [], |
| 68 | externalResources: [], |
| 69 | updatedAt: '2026-07-20', |
| 70 | lastVerifiedAt: '2026-07-21', |
| 71 | reviewAfter: '2027-01-17', |
| 72 | freshness: 'evergreen', |
| 73 | state: 'published', |
| 74 | featured: true, |
| 75 | ...overrides, |
| 76 | }, |
| 77 | ], |
| 78 | }; |
| 79 | } |
| 80 | |
| 81 | describe('public KB catalog', () => { |
| 82 | test('assembles multiple selected public sections with their headings', () => { |
| 83 | const catalog = buildKbCatalog(manifest(), () => source, new Date('2026-07-21')); |
| 84 | expect(catalog.guides[0]?.body).toBe(`## Stable answer |
| 85 | |
| 86 | This is safe public guidance. |
| 87 | |
| 88 | ## Second answer |
| 89 | |
| 90 | This is the second safe answer.`); |
| 91 | expect(catalog.guides[0]?.sourceMetadata).toHaveLength(2); |
| 92 | }); |
| 93 | |
| 94 | test('uses an authored article body while retaining exact public source provenance', () => { |
| 95 | const article = `Start with the verified answer.\n\nThen follow the narrowed guidance.`; |
| 96 | const catalog = buildKbCatalog( |
| 97 | manifest({ articlePath: 'stable-answer.md' }), |
| 98 | () => source, |
| 99 | new Date('2026-07-21'), |
| 100 | articlePath => { |
| 101 | expect(articlePath).toBe('stable-answer.md'); |
| 102 | return article; |
| 103 | } |
| 104 | ); |
| 105 | |
| 106 | expect(catalog.guides[0]?.body).toBe(article); |
| 107 | expect(catalog.guides[0]?.sources).toEqual([ |
| 108 | { sourcePath: 'kb/platform/example/public.md', sourceHeading: 'Stable answer' }, |
| 109 | { sourcePath: 'kb/platform/example/public.md', sourceHeading: 'Second answer' }, |
| 110 | ]); |
| 111 | expect(catalog.guides[0]?.sourceMetadata).toHaveLength(2); |
| 112 | }); |
| 113 | |
| 114 | test('requires an article reader for authored articles', () => { |
| 115 | expect(() => |
| 116 | buildKbCatalog( |
| 117 | manifest({ articlePath: 'stable-answer.md' }), |
| 118 | () => source, |
| 119 | new Date('2026-07-21') |
| 120 | ) |
| 121 | ).toThrow('requires an article reader'); |
| 122 | }); |
| 123 | |
| 124 | test('propagates a missing authored article file', () => { |
| 125 | expect(() => |
| 126 | buildKbCatalog( |
| 127 | manifest({ articlePath: 'stable-answer.md' }), |
| 128 | () => source, |
| 129 | new Date('2026-07-21'), |
| 130 | () => { |
| 131 | throw new Error('ENOENT: no such file or directory'); |
| 132 | } |
| 133 | ) |
| 134 | ).toThrow('ENOENT: no such file or directory'); |
| 135 | }); |
| 136 | |
| 137 | test('rejects an authored article whose path does not match its slug', () => { |
| 138 | expect(() => |
| 139 | buildKbCatalog( |
| 140 | manifest({ articlePath: 'another-answer.md' }), |
| 141 | () => source, |
| 142 | new Date('2026-07-21'), |
| 143 | () => 'Safe body.' |
| 144 | ) |
| 145 | ).toThrow('articlePath must equal stable-answer.md'); |
| 146 | }); |
| 147 | |
| 148 | test('rejects an authored article path with traversal', () => { |
| 149 | expect(() => |
| 150 | buildKbCatalog( |
| 151 | manifest({ articlePath: '../stable-answer.md' }), |
| 152 | () => source, |
| 153 | new Date('2026-07-21'), |
| 154 | () => 'Safe body.' |
| 155 | ) |
| 156 | ).toThrow('articlePath must be a flat filename'); |
| 157 | }); |
| 158 | |
| 159 | test('rejects an empty authored article', () => { |
| 160 | expect(() => |
| 161 | buildKbCatalog( |
| 162 | manifest({ articlePath: 'stable-answer.md' }), |
| 163 | () => source, |
| 164 | new Date('2026-07-21'), |
| 165 | () => ' \n\t ' |
| 166 | ) |
| 167 | ).toThrow('stable-answer.md must not be empty'); |
| 168 | }); |
| 169 | |
| 170 | test('rejects YAML frontmatter in an authored article', () => { |
| 171 | expect(() => |
| 172 | buildKbCatalog( |
| 173 | manifest({ articlePath: 'stable-answer.md' }), |
| 174 | () => source, |
| 175 | new Date('2026-07-21'), |
| 176 | () => '---\ntitle: Private metadata\n---\nSafe body.' |
| 177 | ) |
| 178 | ).toThrow('stable-answer.md must not contain YAML frontmatter'); |
| 179 | }); |
| 180 | |
| 181 | test('rejects YAML frontmatter after leading whitespace in an authored article', () => { |
| 182 | expect(() => |
| 183 | buildKbCatalog( |
| 184 | manifest({ articlePath: 'stable-answer.md' }), |
| 185 | () => source, |
| 186 | new Date('2026-07-21'), |
| 187 | () => '\n \n---\ntitle: Private metadata\n---\nSafe body.' |
| 188 | ) |
| 189 | ).toThrow('stable-answer.md must not contain YAML frontmatter'); |
| 190 | }); |
| 191 | |
| 192 | test('rejects an empty string article path instead of treating it as omitted', () => { |
| 193 | expect(() => |
| 194 | buildKbCatalog( |
| 195 | manifest({ articlePath: '' }), |
| 196 | () => source, |
| 197 | new Date('2026-07-21'), |
| 198 | () => 'Safe body.' |
| 199 | ) |
| 200 | ).toThrow('articlePath must equal stable-answer.md'); |
| 201 | }); |
| 202 | |
| 203 | test('rejects a null article path at runtime', () => { |
| 204 | expect(() => |
| 205 | buildKbCatalog( |
| 206 | manifest({ articlePath: null } as unknown as Partial<KbManifest['guides'][number]>), |
| 207 | () => source, |
| 208 | new Date('2026-07-21'), |
| 209 | () => 'Safe body.' |
| 210 | ) |
| 211 | ).toThrow('articlePath must equal stable-answer.md'); |
| 212 | }); |
| 213 | |
| 214 | test('rejects a symlinked article that escapes the articles root', () => { |
| 215 | const root = mkdtempSync(join(tmpdir(), 'composio-kb-articles-')); |
| 216 | temporaryDirectories.push(root); |
| 217 | const articlesRoot = join(root, 'articles'); |
| 218 | const benignFile = join(root, 'benign.md'); |
| 219 | mkdirSync(articlesRoot); |
| 220 | writeFileSync(benignFile, 'Benign fixture content.', 'utf8'); |
| 221 | symlinkSync(benignFile, join(articlesRoot, 'stable-answer.md')); |
| 222 | |
| 223 | expect(() => createKbArticleReader(articlesRoot)('stable-answer.md')).toThrow( |
| 224 | 'must not be a symbolic link' |
| 225 | ); |
| 226 | }); |
| 227 | |
| 228 | test('rejects an articles root that is itself a symlink', () => { |
| 229 | const root = mkdtempSync(join(tmpdir(), 'composio-kb-articles-')); |
| 230 | temporaryDirectories.push(root); |
| 231 | const outsideArticlesRoot = join(root, 'outside-articles'); |
| 232 | const symlinkedArticlesRoot = join(root, 'articles'); |
| 233 | mkdirSync(outsideArticlesRoot); |
| 234 | writeFileSync(join(outsideArticlesRoot, 'stable-answer.md'), 'Benign fixture content.', 'utf8'); |
| 235 | symlinkSync(outsideArticlesRoot, symlinkedArticlesRoot); |
| 236 | |
| 237 | expect(() => createKbArticleReader(symlinkedArticlesRoot)('stable-answer.md')).toThrow( |
| 238 | 'KB articles root must not be a symbolic link' |
| 239 | ); |
| 240 | }); |
| 241 | |
| 242 | test('reads nested source files only from inside the source root', () => { |
| 243 | const root = mkdtempSync(join(tmpdir(), 'composio-kb-source-')); |
| 244 | temporaryDirectories.push(root); |
| 245 | const sourceRoot = join(root, 'source'); |
| 246 | mkdirSync(join(sourceRoot, 'toolkits', 'github'), { recursive: true }); |
| 247 | writeFileSync( |
| 248 | join(sourceRoot, 'toolkits', 'github', 'public.md'), |
| 249 | 'Safe source fixture.', |
| 250 | 'utf8', |
| 251 | ); |
| 252 | |
| 253 | const readSource = createKbSourceReader(sourceRoot); |
| 254 | expect(readSource('toolkits/github/public.md')).toBe('Safe source fixture.'); |
| 255 | expect(() => readSource('../outside.md')).toThrow( |
| 256 | 'KB source path escapes source directory', |
| 257 | ); |
| 258 | }); |
| 259 | |
| 260 | test('rejects a symlinked source file that escapes the source root', () => { |
| 261 | const root = mkdtempSync(join(tmpdir(), 'composio-kb-source-')); |
| 262 | temporaryDirectories.push(root); |
| 263 | const sourceRoot = join(root, 'source'); |
| 264 | const outsideFile = join(root, 'outside.md'); |
| 265 | mkdirSync(join(sourceRoot, 'toolkits', 'github'), { recursive: true }); |
| 266 | writeFileSync(outsideFile, 'Outside fixture.', 'utf8'); |
| 267 | symlinkSync(outsideFile, join(sourceRoot, 'toolkits', 'github', 'public.md')); |
| 268 | |
| 269 | expect(() => |
| 270 | createKbSourceReader(sourceRoot)('toolkits/github/public.md'), |
| 271 | ).toThrow('KB source must not be a symbolic link'); |
| 272 | }); |
| 273 | |
| 274 | test('rejects private markers in authored articles', () => { |
| 275 | expect(() => |
| 276 | buildKbCatalog( |
| 277 | manifest({ articlePath: 'stable-answer.md' }), |
| 278 | () => source, |
| 279 | new Date('2026-07-21'), |
| 280 | () => 'See Plain T-12345 for details.' |
| 281 | ) |
| 282 | ).toThrow('stable-answer.md contains Plain thread reference'); |
| 283 | }); |
| 284 | |
| 285 | test.each([ |
| 286 | ['candidate-only knowledge', 'This candidate-only answer must not be published.'], |
| 287 | ['internal-only heading', '## Support checks\n\nDo not publish these checks.'], |
| 288 | ])('rejects %s in authored articles', (_marker, article) => { |
| 289 | expect(() => |
| 290 | buildKbCatalog( |
| 291 | manifest({ articlePath: 'stable-answer.md' }), |
| 292 | () => source, |
| 293 | new Date('2026-07-21'), |
| 294 | () => article, |
| 295 | ) |
| 296 | ).toThrow('stable-answer.md contains'); |
| 297 | }); |
| 298 | |
| 299 | test.each([ |
| 300 | ['candidate-only knowledge', 'This candidate-only answer must not be published.'], |
| 301 | ['internal-only heading', '## Debug checklist\n\nDo not publish these checks.'], |
| 302 | ])('rejects %s in referenced public source content', (_marker, replacement) => { |
| 303 | expect(() => |
| 304 | buildKbCatalog( |
| 305 | manifest(), |
| 306 | () => source.replace('This is safe public guidance.', replacement), |
| 307 | new Date('2026-07-21'), |
| 308 | ) |
| 309 | ).toThrow('kb/platform/example/public.md contains'); |
| 310 | }); |
| 311 | |
| 312 | test.each([ |
| 313 | ['guide title', { title: 'See Plain T-12345 for the answer' }], |
| 314 | ['guide description', { description: 'Continue at https://acme.slack.com/archives/C123.' }], |
| 315 | ['guide tag', { tags: ['example', 'candidate-only'] }], |
| 316 | ])('rejects private markers in a published %s', (_field, overrides) => { |
| 317 | expect(() => buildKbCatalog( |
| 318 | manifest(overrides), |
| 319 | () => source, |
| 320 | new Date('2026-07-21'), |
| 321 | )).toThrow('contains'); |
| 322 | }); |
| 323 | |
| 324 | test.each([ |
| 325 | ['source title', 'title: Example', 'title: See Plain T-12345'], |
| 326 | [ |
| 327 | 'source description', |
| 328 | 'description: Public example.', |
| 329 | 'description: Read https://linear.app/composio/issue/ENG-1.', |
| 330 | ], |
| 331 | ['source tag', ' - example', ' - candidate-only'], |
| 332 | ])('rejects private markers in a public %s', (_field, current, replacement) => { |
| 333 | expect(() => buildKbCatalog( |
| 334 | manifest(), |
| 335 | () => source.replace(current, replacement), |
| 336 | new Date('2026-07-21'), |
| 337 | )).toThrow('contains'); |
| 338 | }); |
| 339 | |
| 340 | test.each([ |
| 341 | ['topic title', 'title', 'See Plain T-12345'], |
| 342 | ['topic description', 'description', 'Read https://linear.app/composio/issue/ENG-1.'], |
| 343 | ] as const)('rejects private markers in a public %s', (_field, key, value) => { |
| 344 | const input = manifest(); |
| 345 | input.topics[0] = { ...input.topics[0]!, [key]: value }; |
| 346 | |
| 347 | expect(() => buildKbCatalog( |
| 348 | input, |
| 349 | () => source, |
| 350 | new Date('2026-07-21'), |
| 351 | )).toThrow('contains'); |
| 352 | }); |
| 353 | |
| 354 | test('allows generic public metadata about authorization codes and internal apps', () => { |
| 355 | expect(() => buildKbCatalog( |
| 356 | manifest({ |
| 357 | title: 'Authorize an internal OAuth app', |
| 358 | description: 'Understand authorization code state and token rotation.', |
| 359 | tags: ['authorization-code', 'state-management'], |
| 360 | }), |
| 361 | () => source |
| 362 | .replace('title: Example', 'title: Internal OAuth app setup') |
| 363 | .replace('description: Public example.', 'description: Public authorization code guidance.') |
| 364 | .replace(' - example', ' - state-management'), |
| 365 | new Date('2026-07-21'), |
| 366 | )).not.toThrow(); |
| 367 | }); |
| 368 | |
| 369 | test('allows contact details in authoritative public content', () => { |
| 370 | expect(() => buildKbCatalog( |
| 371 | manifest(), |
| 372 | () => source.replace( |
| 373 | 'This is safe public guidance.', |
| 374 | 'Contact the published support address at help@example.com.', |
| 375 | ), |
| 376 | new Date('2026-07-21'), |
| 377 | )).not.toThrow(); |
| 378 | }); |
| 379 | |
| 380 | test('loads every published guide and resolves every imported alias', () => { |
| 381 | const published = getPublishedKbGuides(); |
| 382 | |
| 383 | // The corpus grows batch by batch, so assert the invariant — the catalog |
| 384 | // exposes exactly the manifest's published guides — rather than a count |
| 385 | // that has to be edited on every publication. |
| 386 | expect(published).toHaveLength( |
| 387 | getKbCatalog().manifest.guides.filter(guide => guide.state === 'published').length |
| 388 | ); |
| 389 | expect(published.length).toBeGreaterThan(0); |
| 390 | for (const guide of published) { |
| 391 | for (const alias of guide.aliases) { |
| 392 | expect(resolveKbAlias(alias)).toBe(`/kb/guide/${guide.slug}`); |
| 393 | } |
| 394 | } |
| 395 | const heldGuides = getKbCatalog().guides.filter(guide => guide.state === 'needs-review'); |
| 396 | // The published snapshot contains only reviewed public leaves, so the |
| 397 | // imported snapshot should not create downstream editorial holds. |
| 398 | expect(heldGuides).toHaveLength(0); |
| 399 | }); |
| 400 | |
| 401 | test('keeps an evergreen guide published after its review deadline without extending it', () => { |
| 402 | const catalog = buildKbCatalog( |
| 403 | manifest({ reviewAfter: '2026-07-22' }), |
| 404 | () => source, |
| 405 | new Date('2026-07-23') |
| 406 | ); |
| 407 | |
| 408 | expect(getPublishedKbGuides(catalog).map(guide => guide.slug)).toEqual(['stable-answer']); |
| 409 | expect(catalog.guides[0]?.reviewAfter).toBe('2026-07-22'); |
| 410 | expect(catalog.guides[0]?.lastVerifiedAt).toBe('2026-07-21'); |
| 411 | }); |
| 412 | |
| 413 | test('rejects an expired published time-sensitive review window', () => { |
| 414 | expect(() => |
| 415 | buildKbCatalog( |
| 416 | manifest({ freshness: 'time-sensitive', reviewAfter: '2026-07-20' }), |
| 417 | () => source, |
| 418 | new Date('2026-07-21') |
| 419 | ) |
| 420 | ).toThrow('review window expired'); |
| 421 | }); |
| 422 | |
| 423 | test('rejects private markers even when an entry is held', () => { |
| 424 | expect(() => |
| 425 | buildKbCatalog( |
| 426 | manifest({ state: 'needs-review', lastVerifiedAt: null, reviewAfter: null }), |
| 427 | () => source.replace('safe public guidance', 'See Plain T-12345'), |
| 428 | new Date('2026-07-21') |
| 429 | ) |
| 430 | ).toThrow('Plain thread reference'); |
| 431 | }); |
| 432 | |
| 433 | test('rejects a guide without source references', () => { |
| 434 | expect(() => |
| 435 | buildKbCatalog(manifest({ sources: [] }), () => source, new Date('2026-07-21')) |
| 436 | ).toThrow('requires at least one source'); |
| 437 | }); |
| 438 | |
| 439 | test('rejects a missing referenced heading', () => { |
| 440 | expect(() => |
| 441 | buildKbCatalog( |
| 442 | manifest({ |
| 443 | sources: [ |
| 444 | { sourcePath: 'kb/platform/example/public.md', sourceHeading: 'Missing answer' }, |
| 445 | ], |
| 446 | }), |
| 447 | () => source, |
| 448 | new Date('2026-07-21') |
| 449 | ) |
| 450 | ).toThrow('Heading "Missing answer" was not found'); |
| 451 | }); |
| 452 | |
| 453 | test('rejects a whole-document reference when level-two sections are present', () => { |
| 454 | expect(() => |
| 455 | buildKbCatalog( |
| 456 | manifest({ |
| 457 | sources: [{ sourcePath: 'kb/platform/example/public.md', sourceHeading: null }], |
| 458 | }), |
| 459 | () => source, |
| 460 | new Date('2026-07-21') |
| 461 | ) |
| 462 | ).toThrow('A source without a heading cannot contain level-two sections'); |
| 463 | }); |
| 464 | |
| 465 | test('rejects a nonpublic referenced source', () => { |
| 466 | expect(() => |
| 467 | buildKbCatalog( |
| 468 | manifest({ |
| 469 | sources: [ |
| 470 | { sourcePath: 'kb/platform/example/private.md', sourceHeading: 'Stable answer' }, |
| 471 | ], |
| 472 | }), |
| 473 | () => source.replace('visibility: public', 'visibility: internal'), |
| 474 | new Date('2026-07-21') |
| 475 | ) |
| 476 | ).toThrow('kb/platform/example/private.md is not visibility: public'); |
| 477 | }); |
| 478 | |
| 479 | test('rejects private markers in every referenced public source', () => { |
| 480 | expect(() => |
| 481 | buildKbCatalog( |
| 482 | manifest({ |
| 483 | sources: [ |
| 484 | { sourcePath: 'kb/platform/example/public.md', sourceHeading: 'Stable answer' }, |
| 485 | { sourcePath: 'kb/platform/example/other-public.md', sourceHeading: 'Second answer' }, |
| 486 | ], |
| 487 | }), |
| 488 | sourcePath => |
| 489 | sourcePath.endsWith('other-public.md') |
| 490 | ? source.replace('second safe answer', 'See Plain T-12345') |
| 491 | : source, |
| 492 | new Date('2026-07-21') |
| 493 | ) |
| 494 | ).toThrow('kb/platform/example/other-public.md contains Plain thread reference'); |
| 495 | }); |
| 496 | |
| 497 | test('resolves published aliases to canonical flat guide routes', () => { |
| 498 | const catalog = buildKbCatalog(manifest(), () => source, new Date('2026-07-21')); |
| 499 | expect(resolveKbAlias('old-answer', catalog)).toBe('/kb/guide/stable-answer'); |
| 500 | }); |
| 501 | }); |