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 | fileTypes: [], |
| 156 | createdAt: "2026-04-01T00:00:00.000Z", |
| 157 | updatedAt: "2026-04-01T00:00:00.000Z", |
| 158 | }, |
| 159 | ], |
| 160 | }, |
| 161 | }), |
| 162 | ) |
| 163 | .mockResolvedValueOnce( |
| 164 | createJsonResponse({ |
| 165 | data: { |
| 166 | install: { |
| 167 | installId: "install_123", |
| 168 | appId: "app_123", |
| 169 | appKey: "com.coline.notes", |
| 170 | name: "Notes", |
| 171 | description: null, |
| 172 | iconUrl: null, |
| 173 | status: "active", |
| 174 | installScope: "workspace", |
| 175 | scopeSubjectId: "workspace_123", |
| 176 | version: "0.1.0", |
| 177 | }, |
| 178 | permissions: { |
| 179 | canManageApps: true, |
| 180 | }, |
| 181 | }, |
| 182 | }), |
| 183 | ) |
| 184 | .mockResolvedValueOnce(createJsonResponse({ data: { ok: true } })); |
| 185 |
| 186 | const client = new ColineApiClient({ |
| 187 | baseUrl: "https://api.coline.app", |
| 188 | fetch: fetchMock, |
| 189 | }); |
| 190 |
| 191 | const app = await client.getApp("com.coline.notes"); |
| 192 | expect(app.app.appKey).toBe("com.coline.notes"); |
| 193 |
| 194 | const versions = await client.listAppVersions("com.coline.notes"); |
| 195 | expect(versions.versions[0]?.version).toBe("0.1.0"); |
| 196 |
| 197 | const installed = await client.getWorkspaceInstalledApp("acme", "com.coline.notes"); |
| 198 | expect(installed.install.installId).toBe("install_123"); |
| 199 |
| 200 | const uninstallResult = await client.uninstallWorkspaceApp( |
| 201 | "acme", |
| 202 | "com.coline.notes", |
| 203 | ); |
| 204 | expect(uninstallResult.ok).toBe(true); |
| 205 | }); |
| 206 |
| 207 | it("loads and updates app file documents through the public runtime API", async () => { |
| 208 | const fetchMock = vi |
| 209 | .fn() |
| 210 | .mockResolvedValueOnce( |
| 211 | createJsonResponse({ |
| 212 | data: { |
| 213 | file: { |
| 214 | id: "file_123", |
| 215 | name: "Customer", |
| 216 | fileType: "com.acme.customer", |
| 217 | appKey: "acme-crm", |
| 218 | appName: "Acme CRM", |
| 219 | typeLabel: "Customer", |
| 220 | openHref: "/acme/acme-crm/files/file_123", |
| 221 | updatedAt: "2026-03-18T08:00:00.000Z", |
| 222 | }, |
| 223 | document: { status: "active" }, |
| 224 | version: 2, |
| 225 | capabilities: { |
| 226 | canRead: true, |
| 227 | canWrite: true, |
| 228 | canManage: false, |
| 229 | }, |
| 230 | }, |
| 231 | }), |
| 232 | ) |
| 233 | .mockResolvedValueOnce( |
| 234 | createJsonResponse({ |
| 235 | data: { |
| 236 | document: { status: "archived" }, |
| 237 | version: 3, |
| 238 | updatedAt: "2026-03-18T09:00:00.000Z", |
| 239 | }, |
| 240 | }), |
| 241 | ); |
| 242 |
| 243 | const client = new ColineApiClient({ |
| 244 | baseUrl: "https://api.coline.app", |
| 245 | fetch: fetchMock, |
| 246 | }); |
| 247 |
| 248 | const file = await client.getAppFileDocument("acme", "acme-crm", "file_123"); |
| 249 | expect(file.file.id).toBe("file_123"); |
| 250 | expect(file.version).toBe(2); |
| 251 |
| 252 | const updated = await client.updateAppFileDocument( |
| 253 | "acme", |
| 254 | "acme-crm", |
| 255 | "file_123", |
| 256 | { status: "archived" }, |
| 257 | ); |
| 258 | expect(updated.version).toBe(3); |
| 259 | }); |
| 260 |
| 261 | it("lists Tab models and creates OpenAI-compatible Tab completions", async () => { |
| 262 | const fetchMock = vi |
| 263 | .fn() |
| 264 | .mockResolvedValueOnce( |
| 265 | createJsonResponse({ |
| 266 | data: { |
| 267 | object: "list", |
| 268 | data: [ |
| 269 | { |
| 270 | id: "tab-v1", |
| 271 | object: "model", |
| 272 | created: 1775200000, |
| 273 | owned_by: "coline", |
| 274 | providerModelId: "gemini-2.5-flash-lite", |
| 275 | pricing: { |
| 276 | inputPerMillionUsd: 0.2, |
| 277 | outputPerMillionUsd: 0.5, |
| 278 | }, |
| 279 | capabilities: { |
| 280 | streaming: true, |
| 281 | surfaces: ["notes", "docs"], |
| 282 | }, |
| 283 | }, |
| 284 | ], |
| 285 | }, |
| 286 | }), |
| 287 | ) |
| 288 | .mockResolvedValueOnce( |
| 289 | createJsonResponse({ |
| 290 | data: { |
| 291 | id: "chatcmpl_123", |
| 292 | object: "chat.completion", |
| 293 | created: 1775200001, |
| 294 | model: "tab-v1", |
| 295 | choices: [ |
| 296 | { |
| 297 | index: 0, |
| 298 | message: { |
| 299 | role: "assistant", |
| 300 | content: " world", |
| 301 | }, |
| 302 | finish_reason: "stop", |
| 303 | }, |
| 304 | ], |
| 305 | usage: { |
| 306 | prompt_tokens: 10, |
| 307 | completion_tokens: 2, |
| 308 | total_tokens: 12, |
| 309 | }, |
| 310 | }, |
| 311 | }), |
| 312 | ); |
| 313 |
| 314 | const client = new ColineApiClient({ |
| 315 | baseUrl: "https://api.coline.app", |
| 316 | fetch: fetchMock, |
| 317 | }); |
| 318 |
| 319 | const models = await client.listTabModels(); |
| 320 | expect(models.data[0]?.id).toBe("tab-v1"); |
| 321 |
| 322 | const completion = await client.createTabChatCompletion({ |
| 323 | model: "tab-v1", |
| 324 | tab_context: { |
| 325 | surface: "notes", |
| 326 | workspace_slug: "acme", |
| 327 | entity_id: "note_123", |
| 328 | document_type: "markdown", |
| 329 | active_text_before_cursor: "Hello", |
| 330 | active_text_after_cursor: "", |
| 331 | surrounding_context: "Title: Roadmap", |
| 332 | }, |
| 333 | }); |
| 334 |
| 335 | expect(completion.choices[0]?.message.content).toBe(" world"); |
| 336 | }); |
| 337 |
| 338 | it("builds Login with Coline authorize URLs and exchanges OAuth tokens", async () => { |
| 339 | const fetchMock = vi |
| 340 | .fn() |
| 341 | .mockResolvedValueOnce( |
| 342 | createJsonResponse({ |
| 343 | access_token: "col_at_123", |
| 344 | token_type: "Bearer", |
| 345 | expires_in: 3600, |
| 346 | refresh_token: "col_rt_123", |
| 347 | scope: "openid profile email", |
| 348 | }), |
| 349 | ) |
| 350 | .mockResolvedValueOnce( |
| 351 | createJsonResponse({ |
| 352 | sub: "user_123", |
| 353 | email: "radin@example.com", |
| 354 | email_verified: true, |
| 355 | name: "Radin", |
| 356 | given_name: "Radin", |
| 357 | family_name: null, |
| 358 | picture: null, |
| 359 | preferred_username: "radin", |
| 360 | scope: "openid profile email", |
| 361 | }), |
| 362 | ); |
| 363 |
| 364 | const client = new ColineApiClient({ |
| 365 | baseUrl: "https://api.coline.app", |
| 366 | fetch: fetchMock, |
| 367 | }); |
| 368 |
| 369 | const authorizeUrl = client.buildLoginWithColineAuthorizeUrl({ |
| 370 | clientId: "col_client_123", |
| 371 | redirectUri: "https://example.com/callback", |
| 372 | scope: "openid profile email", |
| 373 | state: "state_123", |
| 374 | }); |
| 375 | expect(authorizeUrl).toContain("/api/v1/oauth/authorize"); |
| 376 | expect(authorizeUrl).toContain("client_id=col_client_123"); |
| 377 |
| 378 | const token = await client.exchangeOAuthCode({ |
| 379 | clientId: "col_client_123", |
| 380 | clientSecret: "col_secret_123", |
| 381 | code: "col_code_123", |
| 382 | redirectUri: "https://example.com/callback", |
| 383 | }); |
| 384 | expect(token.access_token).toBe("col_at_123"); |
| 385 |
| 386 | const userInfo = await client.getOAuthUserInfo("col_at_123"); |
| 387 | expect(userInfo.sub).toBe("user_123"); |
| 388 | }); |
| 389 |
| 390 | it("supports client cloning with headers and api keys", async () => { |
| 391 | const fetchMock = vi.fn().mockResolvedValue( |
| 392 | createJsonResponse({ |
| 393 | data: { |
| 394 | apps: [], |
| 395 | }, |
| 396 | }), |
| 397 | ); |
| 398 |
| 399 | const baseClient = new ColineApiClient({ |
| 400 | baseUrl: "https://api.coline.app", |
| 401 | fetch: fetchMock, |
| 402 | headers: { |
| 403 | "X-Test": "base", |
| 404 | }, |
| 405 | }); |
| 406 |
| 407 | const client = baseClient.withApiKey("test-key").withHeaders({ |
| 408 | "X-Request-Source": "sdk-test", |
| 409 | }); |
| 410 |
| 411 | await client.listApps(); |
| 412 |
| 413 | const headers = fetchMock.mock.calls[0]?.[1]?.headers as Headers; |
| 414 | expect(headers.get("Authorization")).toBe("Bearer test-key"); |
| 415 | expect(headers.get("X-Test")).toBe("base"); |
| 416 | expect(headers.get("X-Request-Source")).toBe("sdk-test"); |
| 417 | }); |
| 418 |
| 419 | it("throws structured api errors", async () => { |
| 420 | const fetchMock = vi.fn().mockResolvedValue( |
| 421 | createJsonResponse({ |
| 422 | error: { |
| 423 | code: "FORBIDDEN", |
| 424 | message: "No access.", |
| 425 | }, |
| 426 | }, 403), |
| 427 | ); |
| 428 |
| 429 | const client = new ColineApiClient({ |
| 430 | baseUrl: "https://api.coline.app", |
| 431 | fetch: fetchMock, |
| 432 | }); |
| 433 |
| 434 | await expect(client.listApps()).rejects.toBeInstanceOf(ColineApiError); |
| 435 | }); |
| 436 | }); |
| 437 |