The Coline App SDK: apps, tools, file types and surfaces that run inside Coline workspaces, and public websites with Coline Sites. TypeScript, with an in-memory test workspace. MIT.
| 4 |
| 5 | function createJsonResponse(payload: unknown, status = 200): Response { |
| 6 | return new Response(JSON.stringify(payload), { |
| 7 | status, |
| 8 | headers: { "Content-Type": "application/json" }, |
| 9 | }); |
| 10 | } |
| 11 |
| 12 | describe("ColineApiClient", () => { |
| 13 | it("installs a workspace app with explicit granted permissions", async () => { |
| 14 | const fetchMock = vi.fn().mockResolvedValue( |
| 15 | createJsonResponse({ |
| 16 | data: { |
| 17 | installId: "install_123", |
| 18 | appId: "app_123", |
| 19 | appKey: "com.coline.notes", |
| 20 | version: "0.1.0", |
| 21 | grantedPermissions: ["app.home.read"], |
| 22 | }, |
| 23 | }), |
| 24 | ); |
| 25 |
| 26 | const client = new ColineApiClient({ |
| 27 | baseUrl: "https://api.coline.app", |
| 28 | apiKey: "test-key", |
| 29 | fetch: fetchMock, |
| 30 | }); |
| 31 |
| 32 | const result = await client.installWorkspaceApp("acme", { |
| 33 | appId: "app_123", |
| 34 | grantedPermissions: ["app.home.read"], |
| 35 | }); |
| 36 |
| 37 | expect(result.installId).toBe("install_123"); |
| 38 | expect(fetchMock).toHaveBeenCalledWith( |
| 39 | "https://api.coline.app/api/v1/workspaces/acme/apps", |
| 40 | expect.objectContaining({ |
| 41 | method: "POST", |
| 42 | headers: expect.any(Headers), |
| 43 | }), |
| 44 | ); |
| 45 |
| 46 | const headers = fetchMock.mock.calls[0]?.[1]?.headers as Headers; |
| 47 | expect(headers.get("Authorization")).toBe("Bearer test-key"); |
| 48 | }); |
| 49 |
| 50 | it("loads and updates workspace app permissions", async () => { |
| 51 | const fetchMock = vi |
| 52 | .fn() |
| 53 | .mockResolvedValueOnce( |
| 54 | createJsonResponse({ |
| 55 | data: { |
| 56 | install: { |
| 57 | installId: "install_123", |
| 58 | appId: "app_123", |
| 59 | appKey: "com.coline.notes", |
| 60 | name: "Notes", |
| 61 | description: null, |
| 62 | iconUrl: null, |
| 63 | status: "active", |
| 64 | installScope: "workspace", |
| 65 | scopeSubjectId: "workspace_123", |
| 66 | version: "0.1.0", |
| 67 | }, |
| 68 | availablePermissions: ["app.home.read", "files.write"], |
| 69 | grantedPermissions: ["app.home.read"], |
| 70 | permissions: { |
| 71 | canManageApps: true, |
| 72 | }, |
| 73 | }, |
| 74 | }), |
| 75 | ) |
| 76 | .mockResolvedValueOnce( |
| 77 | createJsonResponse({ |
| 78 | data: { |
| 79 | install: { |
| 80 | installId: "install_123", |
| 81 | appId: "app_123", |
| 82 | appKey: "com.coline.notes", |
| 83 | name: "Notes", |
| 84 | description: null, |
| 85 | iconUrl: null, |
| 86 | status: "active", |
| 87 | installScope: "workspace", |
| 88 | scopeSubjectId: "workspace_123", |
| 89 | version: "0.1.0", |
| 90 | }, |
| 91 | availablePermissions: ["app.home.read", "files.write"], |
| 92 | grantedPermissions: ["app.home.read", "files.write"], |
| 93 | permissions: { |
| 94 | canManageApps: true, |
| 95 | }, |
| 96 | }, |
| 97 | }), |
| 98 | ); |
| 99 |
| 100 | const client = new ColineApiClient({ |
| 101 | baseUrl: "https://api.coline.app", |
| 102 | fetch: fetchMock, |
| 103 | }); |
| 104 |
| 105 | const currentPermissions = await client.getWorkspaceAppPermissions( |
| 106 | "acme", |
| 107 | "com.coline.notes", |
| 108 | ); |
| 109 | expect(currentPermissions.grantedPermissions).toEqual(["app.home.read"]); |
| 110 |
| 111 | const updatedPermissions = await client.updateWorkspaceAppPermissions( |
| 112 | "acme", |
| 113 | "com.coline.notes", |
| 114 | { |
| 115 | grantedPermissions: ["app.home.read", "files.write"], |
| 116 | }, |
| 117 | ); |
| 118 |
| 119 | expect(updatedPermissions.grantedPermissions).toEqual([ |
| 120 | "app.home.read", |
| 121 | "files.write", |
| 122 | ]); |
| 123 | }); |
| 124 |
| 125 | it("loads app detail, app versions, installed app, and uninstalls it", async () => { |
| 126 | const fetchMock = vi |
| 127 | .fn() |
| 128 | .mockResolvedValueOnce( |
| 129 | createJsonResponse({ |
| 130 | data: { |
| 131 | app: { |
| 132 | appId: "app_123", |
| 133 | appKey: "com.coline.notes", |
| 134 | name: "Notes", |
| 135 | description: null, |
| 136 | iconUrl: null, |
| 137 | status: "active", |
| 138 | ownerUserId: "user_123", |
| 139 | latestPublishedVersion: "0.1.0", |
| 140 | latestPublishedVersionId: "ver_123", |
| 141 | }, |
| 142 | }, |
| 143 | }), |
| 144 | ) |
| 145 | .mockResolvedValueOnce( |
| 146 | createJsonResponse({ |
| 147 | data: { |
| 148 | versions: [ |
| 149 | { |
| 150 | versionId: "ver_123", |
| 151 | version: "0.1.0", |
| 152 | status: "published", |
| 153 | apiBaseUrl: "https://notes.example.com", |
| 154 | permissions: ["app.home.read"], |
| 155 | kairo: { |
| 156 | enabled: true, |
| 157 | description: "Notes documents can be searched by Kairo.", |
| 158 | instructions: "Search notes before answering document questions.", |
| 159 | documentTypes: [ |
| 160 | { |
| 161 | documentType: "note", |
| 162 | name: "Note", |
| 163 | metadata: [], |
| 164 | }, |
| 165 | ], |
| 166 | }, |
| 167 | fileTypes: [], |
| 168 | createdAt: "2026-04-01T00:00:00.000Z", |
| 169 | updatedAt: "2026-04-01T00:00:00.000Z", |
| 170 | }, |
| 171 | ], |
| 172 | }, |
| 173 | }), |
| 174 | ) |
| 175 | .mockResolvedValueOnce( |
| 176 | createJsonResponse({ |
| 177 | data: { |
| 178 | install: { |
| 179 | installId: "install_123", |
| 180 | appId: "app_123", |
| 181 | appKey: "com.coline.notes", |
| 182 | name: "Notes", |
| 183 | description: null, |
| 184 | iconUrl: null, |
| 185 | status: "active", |
| 186 | installScope: "workspace", |
| 187 | scopeSubjectId: "workspace_123", |
| 188 | version: "0.1.0", |
| 189 | }, |
| 190 | permissions: { |
| 191 | canManageApps: true, |
| 192 | }, |
| 193 | }, |
| 194 | }), |
| 195 | ) |
| 196 | .mockResolvedValueOnce(createJsonResponse({ data: { ok: true } })); |
| 197 |
| 198 | const client = new ColineApiClient({ |
| 199 | baseUrl: "https://api.coline.app", |
| 200 | fetch: fetchMock, |
| 201 | }); |
| 202 |
| 203 | const app = await client.getApp("com.coline.notes"); |
| 204 | expect(app.app.appKey).toBe("com.coline.notes"); |
| 205 |
| 206 | const versions = await client.listAppVersions("com.coline.notes"); |
| 207 | expect(versions.versions[0]?.version).toBe("0.1.0"); |
| 208 | expect(versions.versions[0]?.kairo?.documentTypes[0]?.documentType).toBe("note"); |
| 209 |
| 210 | const installed = await client.getWorkspaceInstalledApp("acme", "com.coline.notes"); |
| 211 | expect(installed.install.installId).toBe("install_123"); |
| 212 |
| 213 | const uninstallResult = await client.uninstallWorkspaceApp( |
| 214 | "acme", |
| 215 | "com.coline.notes", |
| 216 | ); |
| 217 | expect(uninstallResult.ok).toBe(true); |
| 218 | }); |
| 219 |
| 220 | it("loads and updates app file documents through the public runtime API", async () => { |
| 221 | const fetchMock = vi |
| 222 | .fn() |
| 223 | .mockResolvedValueOnce( |
| 224 | createJsonResponse({ |
| 225 | data: { |
| 226 | file: { |
| 227 | id: "file_123", |
| 228 | name: "Customer", |
| 229 | fileType: "com.acme.customer", |
| 230 | appKey: "acme-crm", |
| 231 | appName: "Acme CRM", |
| 232 | typeLabel: "Customer", |
| 233 | openHref: "/acme/acme-crm/files/file_123", |
| 234 | updatedAt: "2026-03-18T08:00:00.000Z", |
| 235 | }, |
| 236 | document: { status: "active" }, |
| 237 | version: 2, |
| 238 | capabilities: { |
| 239 | canRead: true, |
| 240 | canWrite: true, |
| 241 | canManage: false, |
| 242 | }, |
| 243 | }, |
| 244 | }), |
| 245 | ) |
| 246 | .mockResolvedValueOnce( |
| 247 | createJsonResponse({ |
| 248 | data: { |
| 249 | document: { status: "archived" }, |
| 250 | version: 3, |
| 251 | updatedAt: "2026-03-18T09:00:00.000Z", |
| 252 | }, |
| 253 | }), |
| 254 | ); |
| 255 |
| 256 | const client = new ColineApiClient({ |
| 257 | baseUrl: "https://api.coline.app", |
| 258 | fetch: fetchMock, |
| 259 | }); |
| 260 |
| 261 | const file = await client.getAppFileDocument("acme", "acme-crm", "file_123"); |
| 262 | expect(file.file.id).toBe("file_123"); |
| 263 | expect(file.version).toBe(2); |
| 264 |
| 265 | const updated = await client.updateAppFileDocument( |
| 266 | "acme", |
| 267 | "acme-crm", |
| 268 | "file_123", |
| 269 | { status: "archived" }, |
| 270 | ); |
| 271 | expect(updated.version).toBe(3); |
| 272 | }); |
| 273 |
| 274 | it("lists Tab models and creates OpenAI-compatible Tab completions", async () => { |
| 275 | const fetchMock = vi |
| 276 | .fn() |
| 277 | .mockResolvedValueOnce( |
| 278 | createJsonResponse({ |
| 279 | data: { |
| 280 | object: "list", |
| 281 | data: [ |
| 282 | { |
| 283 | id: "tab-v1", |
| 284 | object: "model", |
| 285 | created: 1775200000, |
| 286 | owned_by: "coline", |
| 287 | providerModelId: "gemini-2.5-flash-lite", |
| 288 | pricing: { |
| 289 | inputPerMillionUsd: 0.2, |
| 290 | outputPerMillionUsd: 0.5, |
| 291 | }, |
| 292 | capabilities: { |
| 293 | streaming: true, |
| 294 | surfaces: ["notes", "docs"], |
| 295 | }, |
| 296 | }, |
| 297 | ], |
| 298 | }, |
| 299 | }), |
| 300 | ) |
| 301 | .mockResolvedValueOnce( |
| 302 | createJsonResponse({ |
| 303 | data: { |
| 304 | id: "chatcmpl_123", |
| 305 | object: "chat.completion", |
| 306 | created: 1775200001, |
| 307 | model: "tab-v1", |
| 308 | choices: [ |
| 309 | { |
| 310 | index: 0, |
| 311 | message: { |
| 312 | role: "assistant", |
| 313 | content: " world", |
| 314 | }, |
| 315 | finish_reason: "stop", |
| 316 | }, |
| 317 | ], |
| 318 | usage: { |
| 319 | prompt_tokens: 10, |
| 320 | completion_tokens: 2, |
| 321 | total_tokens: 12, |
| 322 | }, |
| 323 | }, |
| 324 | }), |
| 325 | ); |
| 326 |
| 327 | const client = new ColineApiClient({ |
| 328 | baseUrl: "https://api.coline.app", |
| 329 | fetch: fetchMock, |
| 330 | }); |
| 331 |
| 332 | const models = await client.listTabModels(); |
| 333 | expect(models.data[0]?.id).toBe("tab-v1"); |
| 334 |
| 335 | const completion = await client.createTabChatCompletion({ |
| 336 | model: "tab-v1", |
| 337 | tab_context: { |
| 338 | surface: "notes", |
| 339 | workspace_slug: "acme", |
| 340 | entity_id: "note_123", |
| 341 | document_type: "markdown", |
| 342 | active_text_before_cursor: "Hello", |
| 343 | active_text_after_cursor: "", |
| 344 | surrounding_context: "Title: Roadmap", |
| 345 | }, |
| 346 | }); |
| 347 |
| 348 | expect(completion.choices[0]?.message.content).toBe(" world"); |
| 349 | }); |
| 350 |
| 351 | it("builds Login with Coline authorize URLs and exchanges OAuth tokens", async () => { |
| 352 | const fetchMock = vi |
| 353 | .fn() |
| 354 | .mockResolvedValueOnce( |
| 355 | createJsonResponse({ |
| 356 | access_token: "col_at_123", |
| 357 | token_type: "Bearer", |
| 358 | expires_in: 3600, |
| 359 | refresh_token: "col_rt_123", |
| 360 | scope: "openid profile email", |
| 361 | }), |
| 362 | ) |
| 363 | .mockResolvedValueOnce( |
| 364 | createJsonResponse({ |
| 365 | sub: "user_123", |
| 366 | email: "radin@example.com", |
| 367 | email_verified: true, |
| 368 | name: "Radin", |
| 369 | given_name: "Radin", |
| 370 | family_name: null, |
| 371 | picture: null, |
| 372 | preferred_username: "radin", |
| 373 | scope: "openid profile email", |
| 374 | }), |
| 375 | ); |
| 376 |
| 377 | const client = new ColineApiClient({ |
| 378 | baseUrl: "https://api.coline.app", |
| 379 | fetch: fetchMock, |
| 380 | }); |
| 381 |
| 382 | const authorizeUrl = client.buildLoginWithColineAuthorizeUrl({ |
| 383 | clientId: "col_client_123", |
| 384 | redirectUri: "https://example.com/callback", |
| 385 | scope: "openid profile email", |
| 386 | state: "state_123", |
| 387 | }); |
| 388 | expect(authorizeUrl).toContain("/api/v1/oauth/authorize"); |
| 389 | expect(authorizeUrl).toContain("client_id=col_client_123"); |
| 390 |
| 391 | const token = await client.exchangeOAuthCode({ |
| 392 | clientId: "col_client_123", |
| 393 | clientSecret: "col_secret_123", |
| 394 | code: "col_code_123", |
| 395 | redirectUri: "https://example.com/callback", |
| 396 | }); |
| 397 | expect(token.access_token).toBe("col_at_123"); |
| 398 |
| 399 | const userInfo = await client.getOAuthUserInfo("col_at_123"); |
| 400 | expect(userInfo.sub).toBe("user_123"); |
| 401 | }); |
| 402 |
| 403 | it("supports client cloning with headers and api keys", async () => { |
| 404 | const fetchMock = vi.fn().mockResolvedValue( |
| 405 | createJsonResponse({ |
| 406 | data: { |
| 407 | apps: [], |
| 408 | }, |
| 409 | }), |
| 410 | ); |
| 411 |
| 412 | const baseClient = new ColineApiClient({ |
| 413 | baseUrl: "https://api.coline.app", |
| 414 | fetch: fetchMock, |
| 415 | headers: { |
| 416 | "X-Test": "base", |
| 417 | }, |
| 418 | }); |
| 419 |
| 420 | const client = baseClient.withApiKey("test-key").withHeaders({ |
| 421 | "X-Request-Source": "sdk-test", |
| 422 | }); |
| 423 |
| 424 | await client.listApps(); |
| 425 |
| 426 | const headers = fetchMock.mock.calls[0]?.[1]?.headers as Headers; |
| 427 | expect(headers.get("Authorization")).toBe("Bearer test-key"); |
| 428 | expect(headers.get("X-Test")).toBe("base"); |
| 429 | expect(headers.get("X-Request-Source")).toBe("sdk-test"); |
| 430 | }); |
| 431 |
| 432 | it("throws structured api errors", async () => { |
| 433 | const fetchMock = vi.fn().mockResolvedValue( |
| 434 | createJsonResponse({ |
| 435 | error: { |
| 436 | code: "FORBIDDEN", |
| 437 | message: "No access.", |
| 438 | }, |
| 439 | }, 403), |
| 440 | ); |
| 441 |
| 442 | const client = new ColineApiClient({ |
| 443 | baseUrl: "https://api.coline.app", |
| 444 | fetch: fetchMock, |
| 445 | }); |
| 446 |
| 447 | await expect(client.listApps()).rejects.toBeInstanceOf(ColineApiError); |
| 448 | }); |
| 449 | }); |
| 450 |