@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 155 lines 7.86 KB
Raw
1 import { expect, test } from 'bun:test';
2 import { inflateSync } from 'node:zlib';
3 import { GET, parseOgParams, toolkitCardTitle } from '../../app/api/og/route';
4 import { getOgImageUrl } from '../../lib/source';
5 import { HOME_OG_DESCRIPTION } from '../../lib/toolkit-count';
6
7 async function expectPng(url: string) {
8 const response = await GET(new Request(url));
9 expect(response.status).toBe(200);
10 expect(response.headers.get('content-type')).toBe('image/png');
11 const bytes = Buffer.from(await response.arrayBuffer());
12 expect(bytes.subarray(1, 4).toString()).toBe('PNG');
13 expect(bytes.readUInt32BE(16)).toBe(1200);
14 expect(bytes.readUInt32BE(20)).toBe(630);
15 }
16
17 /** Decodes the route's 8-bit RGB/RGBA PNG output into unfiltered pixel rows. */
18 function decodePng(bytes: Buffer) {
19 const width = bytes.readUInt32BE(16);
20 const height = bytes.readUInt32BE(20);
21 const [depth, colorType] = [bytes[24], bytes[25]];
22 expect(depth).toBe(8);
23 expect([2, 6]).toContain(colorType);
24 const channels = colorType === 6 ? 4 : 3;
25 const chunks: Buffer[] = [];
26 for (let at = 8; at < bytes.length; ) {
27 const length = bytes.readUInt32BE(at);
28 if (bytes.toString('ascii', at + 4, at + 8) === 'IDAT') chunks.push(bytes.subarray(at + 8, at + 8 + length));
29 at += length + 12;
30 }
31 const raw = inflateSync(Buffer.concat(chunks));
32 const stride = width * channels;
33 const pixels = Buffer.alloc(height * stride);
34 for (let y = 0; y < height; y++) {
35 const filter = raw[y * (stride + 1)];
36 for (let x = 0; x < stride; x++) {
37 const value = raw[y * (stride + 1) + 1 + x];
38 const left = x >= channels ? pixels[y * stride + x - channels] : 0;
39 const up = y > 0 ? pixels[(y - 1) * stride + x] : 0;
40 const upLeft = y > 0 && x >= channels ? pixels[(y - 1) * stride + x - channels] : 0;
41 const p = left + up - upLeft;
42 const [pa, pb, pc] = [Math.abs(p - left), Math.abs(p - up), Math.abs(p - upLeft)];
43 const predictor = [0, left, up, (left + up) >> 1, pa <= pb && pa <= pc ? left : pb <= pc ? up : upLeft][filter];
44 pixels[y * stride + x] = (value + predictor) & 0xff;
45 }
46 }
47 return { width, height, channels, pixels };
48 }
49
50 /** Horizontal center of the dark title glyphs below the header row. */
51 async function titleCenter(title: string) {
52 const response = await GET(new Request(`https://docs.composio.dev/api/og?theme=light&title=${encodeURIComponent(title)}`));
53 const { width, height, channels, pixels } = decodePng(Buffer.from(await response.arrayBuffer()));
54 let [left, right] = [width, -1];
55 for (let y = 150; y < height; y++) {
56 for (let x = 0; x < width; x++) {
57 const at = (y * width + x) * channels;
58 if (pixels[at] < 90 && pixels[at + 1] < 90 && pixels[at + 2] < 90) {
59 left = Math.min(left, x);
60 right = Math.max(right, x);
61 }
62 }
63 }
64 expect(right).toBeGreaterThan(left);
65 return (left + right) / 2;
66 }
67
68 test('titles render horizontally centered whether or not they can wrap', async () => {
69 // Single words cannot wrap; balanced wrapping once pushed them ~100px right.
70 for (const title of ['Firewall', 'Introduction', 'Tool Router']) {
71 expect(Math.abs((await titleCenter(title)) - 600)).toBeLessThan(15);
72 }
73 });
74
75 test('docs host renders a PNG for the advertised title URL', async () => {
76 for (const title of ['Welcome', 'Authentication & OAuth / setup', 'Very long title '.repeat(20), '']) {
77 const url = getOgImageUrl('docs', ['authentication'], title);
78 expect(new URL(url).origin).toBe('https://docs.composio.dev');
79 await expectPng(url);
80 }
81 });
82
83 test('every section renders its own card', async () => {
84 await expectPng(getOgImageUrl('docs', [], 'Welcome', 'Site description'));
85 await expectPng(getOgImageUrl('toolkits', ['zendesk'], 'Zendesk - Composio Toolkit', ''));
86 await expectPng(getOgImageUrl('reference', ['connected-accounts'], 'Connected Accounts', 'Manage accounts', {
87 version: 'v3.1',
88 }));
89 await expectPng(getOgImageUrl('docs', ['changelog', '2026', '09', '04'], 'September releases', 'Updates', {
90 date: 'Sep 4, 2026',
91 }));
92 });
93
94 test('the URL builder names the section and forwards extras', () => {
95 // Both the root layout and the /docs index page build this URL; the home
96 // card ignores the page description so they agree and carry the live count.
97 const home = new URL(getOgImageUrl('docs', [], 'Welcome', 'page description'));
98 expect(home.searchParams.get('section')).toBe('home');
99 expect(home.searchParams.get('description')).toBe(HOME_OG_DESCRIPTION);
100 expect(HOME_OG_DESCRIPTION).toMatch(/^Build AI agents with [\d,]+\+ apps\./);
101
102 const changelog = new URL(getOgImageUrl('docs', ['changelog', '2026', '09', '04'], 'Title', 'Updates', { date: 'Sep 4, 2026' }));
103 expect(changelog.searchParams.get('section')).toBe('changelog');
104 expect(changelog.searchParams.get('date')).toBe('Sep 4, 2026');
105
106 const toolkit = new URL(getOgImageUrl('toolkits', ['github'], 'GitHub - Composio Toolkit', 'desc', {
107 logo: 'https://logos.composio.dev/api/github',
108 }));
109 expect(toolkit.searchParams.get('section')).toBe('toolkits');
110 expect(toolkit.searchParams.get('logo')).toBe('https://logos.composio.dev/api/github');
111
112 const reference = new URL(getOgImageUrl('reference', [], 'API Reference', 'desc', { version: 'v3.1' }));
113 expect(reference.searchParams.get('version')).toBe('v3.1');
114 expect(reference.searchParams.get('title')).toBe('API Reference');
115 });
116
117 test('the route only honors toolkit logos from Composio hosts', () => {
118 const allowed = parseOgParams(new Request('https://docs.composio.dev/api/og?section=toolkits&logo=https%3A%2F%2Flogos.composio.dev%2Fapi%2Fgithub'));
119 expect(allowed.logo).toBe('https://logos.composio.dev/api/github?theme=dark');
120
121 for (const logo of ['https://evil.example/x.png', 'http://logos.composio.dev/api/github', 'not a url', 'data:image/png;base64,AAAA']) {
122 const blocked = parseOgParams(new Request(`https://docs.composio.dev/api/og?section=toolkits&logo=${encodeURIComponent(logo)}`));
123 expect(blocked.logo).toBeNull();
124 }
125 });
126
127 test('unknown sections and the legacy home variant fall back safely', () => {
128 expect(parseOgParams(new Request('https://docs.composio.dev/api/og?section=nope&title=x')).section).toBe('docs');
129 expect(parseOgParams(new Request('https://docs.composio.dev/api/og?variant=home')).section).toBe('home');
130 expect(parseOgParams(new Request('https://docs.composio.dev/api/og')).title).toBe('Composio Docs');
131 expect(parseOgParams(new Request(`https://docs.composio.dev/api/og?title=${'x'.repeat(500)}`)).title.length).toBe(120);
132 });
133
134 test('cards default to the dark surface and accept theme=light', async () => {
135 expect(parseOgParams(new Request('https://docs.composio.dev/api/og?title=x')).theme).toBe('dark');
136 const light = parseOgParams(new Request('https://docs.composio.dev/api/og?title=x&theme=light&logo=https%3A%2F%2Flogos.composio.dev%2Fapi%2Fgithub'));
137 expect(light.theme).toBe('light');
138 expect(light.logo).toBe('https://logos.composio.dev/api/github');
139 await expectPng('https://docs.composio.dev/api/og?section=toolkits&title=GitHub&theme=light');
140 await expectPng('https://docs.composio.dev/api/og?section=home&theme=light');
141 });
142
143 test('only catalog toolkit titles get the Toolkit suffix', () => {
144 expect(toolkitCardTitle('GitHub - Composio Toolkit')).toBe('GitHub Toolkit');
145 expect(toolkitCardTitle('Google Sheets - Composio Toolkit')).toBe('Google Sheets Toolkit');
146 expect(toolkitCardTitle('Toolkits')).toBe('Toolkits');
147 expect(toolkitCardTitle('Premium Tools')).toBe('Premium Tools');
148 });
149
150 test('a description that only repeats the title is dropped', () => {
151 const dup = parseOgParams(new Request('https://docs.composio.dev/api/og?section=reference&title=Connected%20Accounts&description=connected%20accounts'));
152 expect(dup.description).toBe('');
153 const real = parseOgParams(new Request('https://docs.composio.dev/api/og?section=reference&title=Connected%20Accounts&description=Manage%20accounts'));
154 expect(real.description).toBe('Manage accounts');
155 });