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