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 | import { createAppPlatformClient } from "./app-platform-client"; |
| 5 |
| 6 | function createJsonResponse(payload: unknown, status = 200): Response { |
| 7 | return new Response(JSON.stringify(payload), { |
| 8 | status, |
| 9 | headers: { "Content-Type": "application/json" }, |
| 10 | }); |
| 11 | } |
| 12 |
| 13 | describe("ColineApiClient", () => { |
| 14 | it("creates a raw generated app-platform client with auth headers", async () => { |
| 15 | const fetchMock = vi.fn().mockResolvedValue( |
| 16 | createJsonResponse({ |
| 17 | data: { |
| 18 | apps: [], |
| 19 | }, |
| 20 | }), |
| 21 | ); |
| 22 |
| 23 | const client = createAppPlatformClient({ |
| 24 | baseUrl: "https://api.coline.app", |
| 25 | apiKey: "test-key", |
| 26 | fetch: fetchMock, |
| 27 | }); |
| 28 |
| 29 | const result = await client.GET("/api/v1/apps"); |
| 30 | expect(result.data?.data.apps).toEqual([]); |
| 31 |
| 32 | const headers = fetchMock.mock.calls[0]?.[1]?.headers as Headers; |
| 33 | expect(headers.get("Authorization")).toBe("Bearer test-key"); |
| 34 | }); |
| 35 |
| 36 | it("registers apps and creates new versions through the v1 app publisher routes", async () => { |
| 37 | const fetchMock = vi |
| 38 | .fn() |
| 39 | .mockResolvedValueOnce( |
| 40 | createJsonResponse({ |
| 41 | data: { |
| 42 | appId: "app_123", |
| 43 | appKey: "com.coline.notes", |
| 44 | versionId: "ver_123", |
| 45 | version: "0.1.0", |
| 46 | }, |
| 47 | }), |
| 48 | ) |
| 49 | .mockResolvedValueOnce( |
| 50 | createJsonResponse({ |
| 51 | data: { |
| 52 | appId: "app_123", |
| 53 | appKey: "com.coline.notes", |
| 54 | versionId: "ver_124", |
| 55 | version: "0.1.1", |
| 56 | }, |
| 57 | }), |
| 58 | ); |
| 59 |
| 60 | const client = new ColineApiClient({ |
| 61 | baseUrl: "https://api.coline.app", |
| 62 | apiKey: "test-key", |
| 63 | fetch: fetchMock, |
| 64 | }); |
| 65 |
| 66 | await client.registerApp({ |
| 67 | version: "0.1.0", |
| 68 | manifest: { |
| 69 | key: "com.coline.notes", |
| 70 | name: "Notes", |
| 71 | permissions: ["app.home.read"], |
| 72 | notificationChannels: [], |
| 73 | hosting: { |
| 74 | mode: "external", |
| 75 | baseUrl: "https://notes.example.com", |
| 76 | }, |
| 77 | }, |
| 78 | fileTypes: [], |
| 79 | }); |
| 80 |
| 81 | await client.createAppVersion("com.coline.notes", { |
| 82 | version: "0.1.1", |
| 83 | manifest: { |
| 84 | key: "com.coline.notes", |
| 85 | name: "Notes", |
| 86 | permissions: ["app.home.read"], |
| 87 | notificationChannels: [], |
| 88 | hosting: { |
| 89 | mode: "external", |
| 90 | baseUrl: "https://notes.example.com", |
| 91 | }, |
| 92 | }, |
| 93 | fileTypes: [], |
| 94 | }); |
| 95 |
| 96 | expect(fetchMock).toHaveBeenNthCalledWith( |
| 97 | 1, |
| 98 | expect.any(Request), |
| 99 | expect.objectContaining({ |
| 100 | headers: expect.any(Headers), |
| 101 | }), |
| 102 | ); |
| 103 | expect((fetchMock.mock.calls[0]?.[0] as Request).url).toBe( |
| 104 | "https://api.coline.app/api/v1/apps", |
| 105 | ); |
| 106 | expect((fetchMock.mock.calls[0]?.[0] as Request).method).toBe("POST"); |
| 107 |
| 108 | expect(fetchMock).toHaveBeenNthCalledWith( |
| 109 | 2, |
| 110 | expect.any(Request), |
| 111 | expect.objectContaining({ |
| 112 | headers: expect.any(Headers), |
| 113 | }), |
| 114 | ); |
| 115 | expect((fetchMock.mock.calls[1]?.[0] as Request).url).toBe( |
| 116 | "https://api.coline.app/api/v1/apps/com.coline.notes/versions", |
| 117 | ); |
| 118 | expect((fetchMock.mock.calls[1]?.[0] as Request).method).toBe("POST"); |
| 119 | }); |
| 120 |
| 121 | it("installs a workspace app with explicit granted permissions", async () => { |
| 122 | const fetchMock = vi.fn().mockResolvedValue( |
| 123 | createJsonResponse({ |
| 124 | data: { |
| 125 | installId: "install_123", |
| 126 | appId: "app_123", |
| 127 | appKey: "com.coline.notes", |
| 128 | version: "0.1.0", |
| 129 | grantedPermissions: ["app.home.read"], |
| 130 | }, |
| 131 | }), |
| 132 | ); |
| 133 |
| 134 | const client = new ColineApiClient({ |
| 135 | baseUrl: "https://api.coline.app", |
| 136 | apiKey: "test-key", |
| 137 | fetch: fetchMock, |
| 138 | }); |
| 139 |
| 140 | const result = await client.installWorkspaceApp("acme", { |
| 141 | appId: "app_123", |
| 142 | grantedPermissions: ["app.home.read"], |
| 143 | }); |
| 144 |
| 145 | expect(result.installId).toBe("install_123"); |
| 146 | expect(fetchMock).toHaveBeenCalledWith( |
| 147 | expect.any(Request), |
| 148 | expect.objectContaining({ |
| 149 | headers: expect.any(Headers), |
| 150 | }), |
| 151 | ); |
| 152 | expect((fetchMock.mock.calls[0]?.[0] as Request).url).toBe( |
| 153 | "https://api.coline.app/api/v1/workspaces/acme/apps", |
| 154 | ); |
| 155 | expect((fetchMock.mock.calls[0]?.[0] as Request).method).toBe("POST"); |
| 156 |
| 157 | const headers = fetchMock.mock.calls[0]?.[1]?.headers as Headers; |
| 158 | expect(headers.get("Authorization")).toBe("Bearer test-key"); |
| 159 | }); |
| 160 |
| 161 | it("loads and updates workspace app permissions", async () => { |
| 162 | const fetchMock = vi |
| 163 | .fn() |
| 164 | .mockResolvedValueOnce( |
| 165 | createJsonResponse({ |
| 166 | data: { |
| 167 | install: { |
| 168 | installId: "install_123", |
| 169 | appId: "app_123", |
| 170 | appKey: "com.coline.notes", |
| 171 | name: "Notes", |
| 172 | description: null, |
| 173 | iconUrl: null, |
| 174 | status: "active", |
| 175 | installScope: "workspace", |
| 176 | scopeSubjectId: "workspace_123", |
| 177 | version: "0.1.0", |
| 178 | }, |
| 179 | availablePermissions: ["app.home.read", "files.write"], |
| 180 | grantedPermissions: ["app.home.read"], |
| 181 | permissions: { |
| 182 | canManageApps: true, |
| 183 | }, |
| 184 | }, |
| 185 | }), |
| 186 | ) |
| 187 | .mockResolvedValueOnce( |
| 188 | createJsonResponse({ |
| 189 | data: { |
| 190 | install: { |
| 191 | installId: "install_123", |
| 192 | appId: "app_123", |
| 193 | appKey: "com.coline.notes", |
| 194 | name: "Notes", |
| 195 | description: null, |
| 196 | iconUrl: null, |
| 197 | status: "active", |
| 198 | installScope: "workspace", |
| 199 | scopeSubjectId: "workspace_123", |
| 200 | version: "0.1.0", |
| 201 | }, |
| 202 | availablePermissions: ["app.home.read", "files.write"], |
| 203 | grantedPermissions: ["app.home.read", "files.write"], |
| 204 | permissions: { |
| 205 | canManageApps: true, |
| 206 | }, |
| 207 | }, |
| 208 | }), |
| 209 | ); |
| 210 |
| 211 | const client = new ColineApiClient({ |
| 212 | baseUrl: "https://api.coline.app", |
| 213 | fetch: fetchMock, |
| 214 | }); |
| 215 |
| 216 | const currentPermissions = await client.getWorkspaceAppPermissions( |
| 217 | "acme", |
| 218 | "com.coline.notes", |
| 219 | ); |
| 220 | expect(currentPermissions.grantedPermissions).toEqual(["app.home.read"]); |
| 221 |
| 222 | const updatedPermissions = await client.updateWorkspaceAppPermissions( |
| 223 | "acme", |
| 224 | "com.coline.notes", |
| 225 | { |
| 226 | grantedPermissions: ["app.home.read", "files.write"], |
| 227 | }, |
| 228 | ); |
| 229 |
| 230 | expect(updatedPermissions.grantedPermissions).toEqual([ |
| 231 | "app.home.read", |
| 232 | "files.write", |
| 233 | ]); |
| 234 | }); |
| 235 |
| 236 | it("lists and creates app-backed files through the app runtime routes", async () => { |
| 237 | const fetchMock = vi |
| 238 | .fn() |
| 239 | .mockResolvedValueOnce( |
| 240 | createJsonResponse({ |
| 241 | data: { |
| 242 | files: [], |
| 243 | availableFileTypes: [ |
| 244 | { |
| 245 | typeKey: "acme.customer", |
| 246 | name: "Customer", |
| 247 | description: null, |
| 248 | }, |
| 249 | ], |
| 250 | }, |
| 251 | }), |
| 252 | ) |
| 253 | .mockResolvedValueOnce( |
| 254 | createJsonResponse({ |
| 255 | data: { |
| 256 | file: { |
| 257 | id: "file_123", |
| 258 | driveId: "drive_123", |
| 259 | parentId: null, |
| 260 | name: "Acme Corp", |
| 261 | fileType: "acme.customer", |
| 262 | typeLabel: "Customer", |
| 263 | appKey: "acme-crm", |
| 264 | appName: "Acme CRM", |
| 265 | openHref: "/acme/acme-crm/files/file_123", |
| 266 | updatedAt: "2026-04-02T00:00:00.000Z", |
| 267 | createdAt: "2026-04-02T00:00:00.000Z", |
| 268 | version: 1, |
| 269 | capabilities: { |
| 270 | canRead: true, |
| 271 | canWrite: true, |
| 272 | canManage: false, |
| 273 | }, |
| 274 | }, |
| 275 | document: { |
| 276 | status: "lead", |
| 277 | }, |
| 278 | version: 1, |
| 279 | }, |
| 280 | }), |
| 281 | ); |
| 282 |
| 283 | const client = new ColineApiClient({ |
| 284 | baseUrl: "https://api.coline.app", |
| 285 | apiKey: "test-key", |
| 286 | fetch: fetchMock, |
| 287 | }); |
| 288 |
| 289 | const files = await client.listAppFiles("acme", "acme-crm", { |
| 290 | limit: 20, |
| 291 | }); |
| 292 | expect(files.availableFileTypes[0]?.typeKey).toBe("acme.customer"); |
| 293 |
| 294 | const created = await client.createAppFile("acme", "acme-crm", { |
| 295 | name: "Acme Corp", |
| 296 | typeKey: "acme.customer", |
| 297 | document: { status: "lead" }, |
| 298 | }); |
| 299 | expect(created.file.id).toBe("file_123"); |
| 300 |
| 301 | expect((fetchMock.mock.calls[0]?.[0] as Request).url).toBe( |
| 302 | "https://api.coline.app/api/v1/workspaces/acme/apps/acme-crm/files?limit=20", |
| 303 | ); |
| 304 | expect((fetchMock.mock.calls[1]?.[0] as Request).url).toBe( |
| 305 | "https://api.coline.app/api/v1/workspaces/acme/apps/acme-crm/files", |
| 306 | ); |
| 307 | }); |
| 308 |
| 309 | it("executes hosted app actions through the public app runtime route", async () => { |
| 310 | const fetchMock = vi.fn().mockResolvedValue( |
| 311 | createJsonResponse({ |
| 312 | data: { |
| 313 | type: "navigate", |
| 314 | href: "/acme/acme-crm/files/file_123", |
| 315 | }, |
| 316 | }), |
| 317 | ); |
| 318 |
| 319 | const client = new ColineApiClient({ |
| 320 | baseUrl: "https://api.coline.app", |
| 321 | apiKey: "test-key", |
| 322 | fetch: fetchMock, |
| 323 | }); |
| 324 |
| 325 | const result = await client.executeAppAction("acme", "acme-crm", { |
| 326 | action: { |
| 327 | type: "crm.sync", |
| 328 | payload: { mode: "delta" }, |
| 329 | }, |
| 330 | fileId: "file_123", |
| 331 | }); |
| 332 |
| 333 | expect(result).toEqual({ |
| 334 | type: "navigate", |
| 335 | href: "/acme/acme-crm/files/file_123", |
| 336 | }); |
| 337 | expect((fetchMock.mock.calls[0]?.[0] as Request).url).toBe( |
| 338 | "https://api.coline.app/api/v1/workspaces/acme/apps/acme-crm/actions", |
| 339 | ); |
| 340 | }); |
| 341 |
| 342 | it("manages workspace app secrets, oauth connections, deliveries, and review submission", async () => { |
| 343 | const fetchMock = vi |
| 344 | .fn() |
| 345 | .mockResolvedValueOnce( |
| 346 | createJsonResponse({ |
| 347 | data: { |
| 348 | install: { |
| 349 | installId: "install_123", |
| 350 | appId: "app_123", |
| 351 | appKey: "com.coline.notes", |
| 352 | name: "Notes", |
| 353 | description: null, |
| 354 | iconUrl: null, |
| 355 | status: "active", |
| 356 | installScope: "workspace", |
| 357 | scopeSubjectId: "workspace_123", |
| 358 | version: "1.0.0", |
| 359 | }, |
| 360 | secrets: [], |
| 361 | permissions: { canManageApps: true }, |
| 362 | }, |
| 363 | }), |
| 364 | ) |
| 365 | .mockResolvedValueOnce( |
| 366 | createJsonResponse({ |
| 367 | data: { |
| 368 | secret: { |
| 369 | id: "secret_123", |
| 370 | key: "OPENAI_API_KEY", |
| 371 | createdAt: "2026-04-02T00:00:00.000Z", |
| 372 | updatedAt: "2026-04-02T00:00:00.000Z", |
| 373 | }, |
| 374 | value: "sk-test", |
| 375 | }, |
| 376 | }), |
| 377 | ) |
| 378 | .mockResolvedValueOnce(createJsonResponse({ data: { ok: true } })) |
| 379 | .mockResolvedValueOnce( |
| 380 | createJsonResponse({ |
| 381 | data: { |
| 382 | install: { |
| 383 | installId: "install_123", |
| 384 | appId: "app_123", |
| 385 | appKey: "com.coline.notes", |
| 386 | name: "Notes", |
| 387 | description: null, |
| 388 | iconUrl: null, |
| 389 | status: "active", |
| 390 | installScope: "workspace", |
| 391 | scopeSubjectId: "workspace_123", |
| 392 | version: "1.0.0", |
| 393 | }, |
| 394 | connections: [], |
| 395 | permissions: { canManageApps: true }, |
| 396 | }, |
| 397 | }), |
| 398 | ) |
| 399 | .mockResolvedValueOnce( |
| 400 | createJsonResponse({ |
| 401 | data: { |
| 402 | connection: { |
| 403 | id: "conn_123", |
| 404 | providerKey: "github", |
| 405 | externalAccountId: "octocat", |
| 406 | scopes: ["repo"], |
| 407 | tokenExpiresAt: null, |
| 408 | createdAt: "2026-04-02T00:00:00.000Z", |
| 409 | updatedAt: "2026-04-02T00:00:00.000Z", |
| 410 | }, |
| 411 | }, |
| 412 | }), |
| 413 | ) |
| 414 | .mockResolvedValueOnce(createJsonResponse({ data: { ok: true } })) |
| 415 | .mockResolvedValueOnce( |
| 416 | createJsonResponse({ |
| 417 | data: { |
| 418 | install: { |
| 419 | installId: "install_123", |
| 420 | appId: "app_123", |
| 421 | appKey: "com.coline.notes", |
| 422 | name: "Notes", |
| 423 | description: null, |
| 424 | iconUrl: null, |
| 425 | status: "active", |
| 426 | installScope: "workspace", |
| 427 | scopeSubjectId: "workspace_123", |
| 428 | version: "1.0.0", |
| 429 | }, |
| 430 | deliveries: [], |
| 431 | permissions: { canManageApps: true }, |
| 432 | }, |
| 433 | }), |
| 434 | ) |
| 435 | .mockResolvedValueOnce( |
| 436 | createJsonResponse({ |
| 437 | data: { |
| 438 | delivery: { |
| 439 | id: "delivery_123", |
| 440 | eventType: "app.test", |
| 441 | targetUrl: "https://notes.example.com/coline/events", |
| 442 | status: "delivered", |
| 443 | attemptCount: 1, |
| 444 | lastStatusCode: 200, |
| 445 | createdAt: "2026-04-02T00:00:00.000Z", |
| 446 | updatedAt: "2026-04-02T00:00:00.000Z", |
| 447 | }, |
| 448 | }, |
| 449 | }), |
| 450 | ) |
| 451 | .mockResolvedValueOnce( |
| 452 | createJsonResponse({ |
| 453 | data: { |
| 454 | delivery: { |
| 455 | id: "delivery_123", |
| 456 | eventType: "app.test", |
| 457 | targetUrl: "https://notes.example.com/coline/events", |
| 458 | status: "delivered", |
| 459 | attemptCount: 2, |
| 460 | lastStatusCode: 200, |
| 461 | createdAt: "2026-04-02T00:00:00.000Z", |
| 462 | updatedAt: "2026-04-02T00:01:00.000Z", |
| 463 | }, |
| 464 | }, |
| 465 | }), |
| 466 | ) |
| 467 | .mockResolvedValueOnce( |
| 468 | createJsonResponse({ |
| 469 | data: { |
| 470 | appId: "app_123", |
| 471 | appKey: "com.coline.notes", |
| 472 | versionId: "ver_123", |
| 473 | version: "1.0.0", |
| 474 | reviewStatus: "submitted", |
| 475 | storeListed: true, |
| 476 | }, |
| 477 | }), |
| 478 | ); |
| 479 |
| 480 | const client = new ColineApiClient({ |
| 481 | baseUrl: "https://api.coline.app", |
| 482 | fetch: fetchMock, |
| 483 | }); |
| 484 |
| 485 | await client.listWorkspaceAppSecrets("acme", "com.coline.notes"); |
| 486 | await client.createWorkspaceAppSecret("acme", "com.coline.notes", { |
| 487 | key: "OPENAI_API_KEY", |
| 488 | value: "sk-test", |
| 489 | }); |
| 490 | await client.deleteWorkspaceAppSecret( |
| 491 | "acme", |
| 492 | "com.coline.notes", |
| 493 | "secret_123", |
| 494 | ); |
| 495 |
| 496 | await client.listWorkspaceAppOauthConnections("acme", "com.coline.notes"); |
| 497 | await client.createWorkspaceAppOauthConnection("acme", "com.coline.notes", { |
| 498 | providerKey: "github", |
| 499 | accessToken: "gho_test", |
| 500 | scopes: ["repo"], |
| 501 | externalAccountId: "octocat", |
| 502 | }); |
| 503 | await client.deleteWorkspaceAppOauthConnection( |
| 504 | "acme", |
| 505 | "com.coline.notes", |
| 506 | "conn_123", |
| 507 | ); |
| 508 |
| 509 | await client.listWorkspaceAppDeliveries("acme", "com.coline.notes"); |
| 510 | await client.sendWorkspaceAppTestDelivery("acme", "com.coline.notes", { |
| 511 | eventType: "app.test", |
| 512 | payload: { ok: true }, |
| 513 | }); |
| 514 | await client.replayWorkspaceAppDelivery( |
| 515 | "acme", |
| 516 | "com.coline.notes", |
| 517 | "delivery_123", |
| 518 | ); |
| 519 |
| 520 | await client.submitAppVersionForReview("com.coline.notes", "ver_123", { |
| 521 | storeListed: true, |
| 522 | }); |
| 523 |
| 524 | expect(fetchMock).toHaveBeenNthCalledWith( |
| 525 | 1, |
| 526 | expect.any(Request), |
| 527 | expect.any(Object), |
| 528 | ); |
| 529 | expect((fetchMock.mock.calls[0]?.[0] as Request).url).toBe( |
| 530 | "https://api.coline.app/api/v1/workspaces/acme/apps/com.coline.notes/secrets", |
| 531 | ); |
| 532 | expect(fetchMock).toHaveBeenNthCalledWith( |
| 533 | 5, |
| 534 | expect.any(Request), |
| 535 | expect.any(Object), |
| 536 | ); |
| 537 | expect((fetchMock.mock.calls[4]?.[0] as Request).url).toBe( |
| 538 | "https://api.coline.app/api/v1/workspaces/acme/apps/com.coline.notes/oauth-connections", |
| 539 | ); |
| 540 | expect(fetchMock).toHaveBeenNthCalledWith( |
| 541 | 8, |
| 542 | expect.any(Request), |
| 543 | expect.any(Object), |
| 544 | ); |
| 545 | expect((fetchMock.mock.calls[7]?.[0] as Request).url).toBe( |
| 546 | "https://api.coline.app/api/v1/workspaces/acme/apps/com.coline.notes/deliveries", |
| 547 | ); |
| 548 | expect(fetchMock).toHaveBeenNthCalledWith( |
| 549 | 9, |
| 550 | expect.any(Request), |
| 551 | expect.any(Object), |
| 552 | ); |
| 553 | expect((fetchMock.mock.calls[8]?.[0] as Request).url).toBe( |
| 554 | "https://api.coline.app/api/v1/workspaces/acme/apps/com.coline.notes/deliveries/delivery_123/replay", |
| 555 | ); |
| 556 | expect(fetchMock).toHaveBeenNthCalledWith( |
| 557 | 10, |
| 558 | expect.any(Request), |
| 559 | expect.any(Object), |
| 560 | ); |
| 561 | expect((fetchMock.mock.calls[9]?.[0] as Request).url).toBe( |
| 562 | "https://api.coline.app/api/v1/apps/com.coline.notes/versions/ver_123/submit", |
| 563 | ); |
| 564 | }); |
| 565 |
| 566 | it("loads app detail, app versions, installed app, and uninstalls it", async () => { |
| 567 | const fetchMock = vi |
| 568 | .fn() |
| 569 | .mockResolvedValueOnce( |
| 570 | createJsonResponse({ |
| 571 | data: { |
| 572 | app: { |
| 573 | appId: "app_123", |
| 574 | appKey: "com.coline.notes", |
| 575 | name: "Notes", |
| 576 | description: null, |
| 577 | iconUrl: null, |
| 578 | status: "active", |
| 579 | ownerUserId: "user_123", |
| 580 | latestPublishedVersion: "0.1.0", |
| 581 | latestPublishedVersionId: "ver_123", |
| 582 | }, |
| 583 | }, |
| 584 | }), |
| 585 | ) |
| 586 | .mockResolvedValueOnce( |
| 587 | createJsonResponse({ |
| 588 | data: { |
| 589 | versions: [ |
| 590 | { |
| 591 | versionId: "ver_123", |
| 592 | version: "0.1.0", |
| 593 | status: "published", |
| 594 | apiBaseUrl: "https://notes.example.com", |
| 595 | permissions: ["app.home.read"], |
| 596 | kairo: { |
| 597 | enabled: true, |
| 598 | description: "Notes documents can be searched by Kairo.", |
| 599 | instructions: "Search notes before answering document questions.", |
| 600 | documentTypes: [ |
| 601 | { |
| 602 | documentType: "note", |
| 603 | name: "Note", |
| 604 | metadata: [], |
| 605 | }, |
| 606 | ], |
| 607 | }, |
| 608 | fileTypes: [], |
| 609 | createdAt: "2026-04-01T00:00:00.000Z", |
| 610 | updatedAt: "2026-04-01T00:00:00.000Z", |
| 611 | }, |
| 612 | ], |
| 613 | }, |
| 614 | }), |
| 615 | ) |
| 616 | .mockResolvedValueOnce( |
| 617 | createJsonResponse({ |
| 618 | data: { |
| 619 | install: { |
| 620 | installId: "install_123", |
| 621 | appId: "app_123", |
| 622 | appKey: "com.coline.notes", |
| 623 | name: "Notes", |
| 624 | description: null, |
| 625 | iconUrl: null, |
| 626 | status: "active", |
| 627 | installScope: "workspace", |
| 628 | scopeSubjectId: "workspace_123", |
| 629 | version: "0.1.0", |
| 630 | }, |
| 631 | permissions: { |
| 632 | canManageApps: true, |
| 633 | }, |
| 634 | }, |
| 635 | }), |
| 636 | ) |
| 637 | .mockResolvedValueOnce(createJsonResponse({ data: { ok: true } })); |
| 638 |
| 639 | const client = new ColineApiClient({ |
| 640 | baseUrl: "https://api.coline.app", |
| 641 | fetch: fetchMock, |
| 642 | }); |
| 643 |
| 644 | const app = await client.getApp("com.coline.notes"); |
| 645 | expect(app.app.appKey).toBe("com.coline.notes"); |
| 646 |
| 647 | const versions = await client.listAppVersions("com.coline.notes"); |
| 648 | expect(versions.versions[0]?.version).toBe("0.1.0"); |
| 649 | const kairo = versions.versions[0]?.kairo as |
| 650 | | { documentTypes?: Array<{ documentType?: string }> } |
| 651 | | null |
| 652 | | undefined; |
| 653 | expect(kairo?.documentTypes?.[0]?.documentType).toBe("note"); |
| 654 |
| 655 | const installed = await client.getWorkspaceInstalledApp("acme", "com.coline.notes"); |
| 656 | expect(installed.install.installId).toBe("install_123"); |
| 657 |
| 658 | const uninstallResult = await client.uninstallWorkspaceApp( |
| 659 | "acme", |
| 660 | "com.coline.notes", |
| 661 | ); |
| 662 | expect(uninstallResult.ok).toBe(true); |
| 663 | }); |
| 664 |
| 665 | it("loads and updates app file documents through the public runtime API", async () => { |
| 666 | const fetchMock = vi |
| 667 | .fn() |
| 668 | .mockResolvedValueOnce( |
| 669 | createJsonResponse({ |
| 670 | data: { |
| 671 | file: { |
| 672 | id: "file_123", |
| 673 | name: "Customer", |
| 674 | fileType: "com.acme.customer", |
| 675 | appKey: "acme-crm", |
| 676 | appName: "Acme CRM", |
| 677 | typeLabel: "Customer", |
| 678 | openHref: "/acme/acme-crm/files/file_123", |
| 679 | updatedAt: "2026-03-18T08:00:00.000Z", |
| 680 | }, |
| 681 | document: { status: "active" }, |
| 682 | version: 2, |
| 683 | capabilities: { |
| 684 | canRead: true, |
| 685 | canWrite: true, |
| 686 | canManage: false, |
| 687 | }, |
| 688 | }, |
| 689 | }), |
| 690 | ) |
| 691 | .mockResolvedValueOnce( |
| 692 | createJsonResponse({ |
| 693 | data: { |
| 694 | document: { status: "archived" }, |
| 695 | version: 3, |
| 696 | updatedAt: "2026-03-18T09:00:00.000Z", |
| 697 | }, |
| 698 | }), |
| 699 | ); |
| 700 |
| 701 | const client = new ColineApiClient({ |
| 702 | baseUrl: "https://api.coline.app", |
| 703 | fetch: fetchMock, |
| 704 | }); |
| 705 |
| 706 | const file = await client.getAppFileDocument("acme", "acme-crm", "file_123"); |
| 707 | expect(file.file.id).toBe("file_123"); |
| 708 | expect(file.version).toBe(2); |
| 709 |
| 710 | const updated = await client.updateAppFileDocument( |
| 711 | "acme", |
| 712 | "acme-crm", |
| 713 | "file_123", |
| 714 | { status: "archived" }, |
| 715 | ); |
| 716 | expect(updated.version).toBe(3); |
| 717 | }); |
| 718 |
| 719 | it("scopes publisher routes through fluent app handles", async () => { |
| 720 | const fetchMock = vi |
| 721 | .fn() |
| 722 | .mockResolvedValueOnce( |
| 723 | createJsonResponse({ |
| 724 | data: { |
| 725 | app: { |
| 726 | appId: "app_123", |
| 727 | appKey: "com.coline.notes", |
| 728 | name: "Notes", |
| 729 | description: null, |
| 730 | iconUrl: null, |
| 731 | status: "active", |
| 732 | ownerUserId: "user_123", |
| 733 | latestPublishedVersion: "0.1.0", |
| 734 | latestPublishedVersionId: "ver_123", |
| 735 | }, |
| 736 | }, |
| 737 | }), |
| 738 | ) |
| 739 | .mockResolvedValueOnce( |
| 740 | createJsonResponse({ |
| 741 | data: { |
| 742 | versions: [], |
| 743 | }, |
| 744 | }), |
| 745 | ) |
| 746 | .mockResolvedValueOnce( |
| 747 | createJsonResponse({ |
| 748 | data: { |
| 749 | appId: "app_123", |
| 750 | appKey: "com.coline.notes", |
| 751 | versionId: "ver_124", |
| 752 | version: "0.1.1", |
| 753 | }, |
| 754 | }), |
| 755 | ) |
| 756 | .mockResolvedValueOnce( |
| 757 | createJsonResponse({ |
| 758 | data: { |
| 759 | appId: "app_123", |
| 760 | appKey: "com.coline.notes", |
| 761 | versionId: "ver_124", |
| 762 | version: "0.1.1", |
| 763 | reviewStatus: "submitted", |
| 764 | storeListed: true, |
| 765 | }, |
| 766 | }), |
| 767 | ); |
| 768 |
| 769 | const client = new ColineApiClient({ |
| 770 | baseUrl: "https://api.coline.app", |
| 771 | fetch: fetchMock, |
| 772 | }); |
| 773 |
| 774 | const app = client.app("com.coline.notes"); |
| 775 | await app.get(); |
| 776 | await app.listVersions(); |
| 777 | await app.createVersion({ |
| 778 | version: "0.1.1", |
| 779 | manifest: { |
| 780 | key: "com.coline.notes", |
| 781 | name: "Notes", |
| 782 | permissions: ["app.home.read"], |
| 783 | notificationChannels: [], |
| 784 | hosting: { |
| 785 | mode: "external", |
| 786 | baseUrl: "https://notes.example.com", |
| 787 | }, |
| 788 | }, |
| 789 | fileTypes: [], |
| 790 | }); |
| 791 | await app.submitVersionForReview("ver_124", { storeListed: true }); |
| 792 |
| 793 | expect((fetchMock.mock.calls[0]?.[0] as Request).url).toBe( |
| 794 | "https://api.coline.app/api/v1/apps/com.coline.notes", |
| 795 | ); |
| 796 | expect((fetchMock.mock.calls[1]?.[0] as Request).url).toBe( |
| 797 | "https://api.coline.app/api/v1/apps/com.coline.notes/versions", |
| 798 | ); |
| 799 | expect((fetchMock.mock.calls[2]?.[0] as Request).url).toBe( |
| 800 | "https://api.coline.app/api/v1/apps/com.coline.notes/versions", |
| 801 | ); |
| 802 | expect((fetchMock.mock.calls[3]?.[0] as Request).url).toBe( |
| 803 | "https://api.coline.app/api/v1/apps/com.coline.notes/versions/ver_124/submit", |
| 804 | ); |
| 805 | }); |
| 806 |
| 807 | it("scopes workspace app runtime routes through fluent handles", async () => { |
| 808 | const fetchMock = vi |
| 809 | .fn() |
| 810 | .mockResolvedValueOnce( |
| 811 | createJsonResponse({ |
| 812 | data: { |
| 813 | installedApps: [], |
| 814 | availableApps: [], |
| 815 | }, |
| 816 | }), |
| 817 | ) |
| 818 | .mockResolvedValueOnce( |
| 819 | createJsonResponse({ |
| 820 | data: { |
| 821 | installId: "install_123", |
| 822 | appId: "app_123", |
| 823 | appKey: "acme-crm", |
| 824 | version: "0.1.0", |
| 825 | grantedPermissions: ["files.write"], |
| 826 | }, |
| 827 | }), |
| 828 | ) |
| 829 | .mockResolvedValueOnce( |
| 830 | createJsonResponse({ |
| 831 | data: { |
| 832 | install: { |
| 833 | installId: "install_123", |
| 834 | appId: "app_123", |
| 835 | appKey: "acme-crm", |
| 836 | name: "Acme CRM", |
| 837 | description: null, |
| 838 | iconUrl: null, |
| 839 | status: "active", |
| 840 | installScope: "workspace", |
| 841 | scopeSubjectId: "workspace_123", |
| 842 | version: "0.1.0", |
| 843 | }, |
| 844 | permissions: { |
| 845 | canManageApps: true, |
| 846 | }, |
| 847 | }, |
| 848 | }), |
| 849 | ) |
| 850 | .mockResolvedValueOnce( |
| 851 | createJsonResponse({ |
| 852 | data: { |
| 853 | files: [], |
| 854 | availableFileTypes: [], |
| 855 | }, |
| 856 | }), |
| 857 | ) |
| 858 | .mockResolvedValueOnce( |
| 859 | createJsonResponse({ |
| 860 | data: { |
| 861 | file: { |
| 862 | id: "file_123", |
| 863 | driveId: "drive_123", |
| 864 | parentId: null, |
| 865 | name: "Acme Corp", |
| 866 | fileType: "acme.customer", |
| 867 | typeLabel: "Customer", |
| 868 | appKey: "acme-crm", |
| 869 | appName: "Acme CRM", |
| 870 | openHref: "/acme/acme-crm/files/file_123", |
| 871 | updatedAt: "2026-04-02T00:00:00.000Z", |
| 872 | createdAt: "2026-04-02T00:00:00.000Z", |
| 873 | version: 1, |
| 874 | capabilities: { |
| 875 | canRead: true, |
| 876 | canWrite: true, |
| 877 | canManage: false, |
| 878 | }, |
| 879 | }, |
| 880 | document: { |
| 881 | status: "lead", |
| 882 | }, |
| 883 | version: 1, |
| 884 | }, |
| 885 | }), |
| 886 | ) |
| 887 | .mockResolvedValueOnce( |
| 888 | createJsonResponse({ |
| 889 | data: { |
| 890 | file: { |
| 891 | id: "file_123", |
| 892 | name: "Customer", |
| 893 | fileType: "acme.customer", |
| 894 | appKey: "acme-crm", |
| 895 | appName: "Acme CRM", |
| 896 | typeLabel: "Customer", |
| 897 | openHref: "/acme/acme-crm/files/file_123", |
| 898 | updatedAt: "2026-03-18T08:00:00.000Z", |
| 899 | }, |
| 900 | document: { status: "active" }, |
| 901 | version: 2, |
| 902 | capabilities: { |
| 903 | canRead: true, |
| 904 | canWrite: true, |
| 905 | canManage: false, |
| 906 | }, |
| 907 | }, |
| 908 | }), |
| 909 | ) |
| 910 | .mockResolvedValueOnce( |
| 911 | createJsonResponse({ |
| 912 | data: { |
| 913 | document: { status: "archived" }, |
| 914 | version: 3, |
| 915 | updatedAt: "2026-03-18T09:00:00.000Z", |
| 916 | }, |
| 917 | }), |
| 918 | ); |
| 919 |
| 920 | const client = new ColineApiClient({ |
| 921 | baseUrl: "https://api.coline.app", |
| 922 | fetch: fetchMock, |
| 923 | }); |
| 924 |
| 925 | const ws = client.workspace("acme"); |
| 926 | await ws.listApps(); |
| 927 | await ws.installApp({ appId: "app_123", grantedPermissions: ["files.write"] }); |
| 928 |
| 929 | const app = ws.app("acme-crm"); |
| 930 | await app.get(); |
| 931 | await app.listFiles({ limit: 20 }); |
| 932 | await app.createFile({ |
| 933 | name: "Acme Corp", |
| 934 | typeKey: "acme.customer", |
| 935 | document: { status: "lead" }, |
| 936 | }); |
| 937 | await app.file("file_123").get(); |
| 938 | await app.file("file_123").updateDocument({ status: "archived" }); |
| 939 |
| 940 | expect((fetchMock.mock.calls[0]?.[0] as Request).url).toBe( |
| 941 | "https://api.coline.app/api/v1/workspaces/acme/apps", |
| 942 | ); |
| 943 | expect((fetchMock.mock.calls[1]?.[0] as Request).url).toBe( |
| 944 | "https://api.coline.app/api/v1/workspaces/acme/apps", |
| 945 | ); |
| 946 | expect((fetchMock.mock.calls[2]?.[0] as Request).url).toBe( |
| 947 | "https://api.coline.app/api/v1/workspaces/acme/apps/acme-crm", |
| 948 | ); |
| 949 | expect((fetchMock.mock.calls[3]?.[0] as Request).url).toBe( |
| 950 | "https://api.coline.app/api/v1/workspaces/acme/apps/acme-crm/files?limit=20", |
| 951 | ); |
| 952 | expect((fetchMock.mock.calls[4]?.[0] as Request).url).toBe( |
| 953 | "https://api.coline.app/api/v1/workspaces/acme/apps/acme-crm/files", |
| 954 | ); |
| 955 | expect((fetchMock.mock.calls[5]?.[0] as Request).url).toBe( |
| 956 | "https://api.coline.app/api/v1/workspaces/acme/apps/acme-crm/files/file_123", |
| 957 | ); |
| 958 | expect((fetchMock.mock.calls[6]?.[0] as Request).url).toBe( |
| 959 | "https://api.coline.app/api/v1/workspaces/acme/apps/acme-crm/files/file_123", |
| 960 | ); |
| 961 | }); |
| 962 |
| 963 | it("lists Tab models and creates OpenAI-compatible Tab completions", async () => { |
| 964 | const fetchMock = vi |
| 965 | .fn() |
| 966 | .mockResolvedValueOnce( |
| 967 | createJsonResponse({ |
| 968 | data: { |
| 969 | object: "list", |
| 970 | data: [ |
| 971 | { |
| 972 | id: "tab-v1", |
| 973 | object: "model", |
| 974 | created: 1775200000, |
| 975 | owned_by: "coline", |
| 976 | providerModelId: "gemini-2.5-flash-lite", |
| 977 | pricing: { |
| 978 | inputPerMillionUsd: 0.2, |
| 979 | outputPerMillionUsd: 0.5, |
| 980 | }, |
| 981 | capabilities: { |
| 982 | streaming: true, |
| 983 | surfaces: ["notes", "docs"], |
| 984 | }, |
| 985 | }, |
| 986 | ], |
| 987 | }, |
| 988 | }), |
| 989 | ) |
| 990 | .mockResolvedValueOnce( |
| 991 | createJsonResponse({ |
| 992 | data: { |
| 993 | id: "chatcmpl_123", |
| 994 | object: "chat.completion", |
| 995 | created: 1775200001, |
| 996 | model: "tab-v1", |
| 997 | choices: [ |
| 998 | { |
| 999 | index: 0, |
| 1000 | message: { |
| 1001 | role: "assistant", |
| 1002 | content: " world", |
| 1003 | }, |
| 1004 | finish_reason: "stop", |
| 1005 | }, |
| 1006 | ], |
| 1007 | usage: { |
| 1008 | prompt_tokens: 10, |
| 1009 | completion_tokens: 2, |
| 1010 | total_tokens: 12, |
| 1011 | }, |
| 1012 | }, |
| 1013 | }), |
| 1014 | ); |
| 1015 |
| 1016 | const client = new ColineApiClient({ |
| 1017 | baseUrl: "https://api.coline.app", |
| 1018 | fetch: fetchMock, |
| 1019 | }); |
| 1020 |
| 1021 | const models = await client.listTabModels(); |
| 1022 | expect(models.data[0]?.id).toBe("tab-v1"); |
| 1023 |
| 1024 | const completion = await client.createTabChatCompletion({ |
| 1025 | model: "tab-v1", |
| 1026 | tab_context: { |
| 1027 | surface: "notes", |
| 1028 | workspace_slug: "acme", |
| 1029 | entity_id: "note_123", |
| 1030 | document_type: "markdown", |
| 1031 | active_text_before_cursor: "Hello", |
| 1032 | active_text_after_cursor: "", |
| 1033 | surrounding_context: "Title: Roadmap", |
| 1034 | }, |
| 1035 | }); |
| 1036 |
| 1037 | expect(completion.choices[0]?.message.content).toBe(" world"); |
| 1038 | }); |
| 1039 |
| 1040 | it("builds Login with Coline authorize URLs and exchanges OAuth tokens", async () => { |
| 1041 | const fetchMock = vi |
| 1042 | .fn() |
| 1043 | .mockResolvedValueOnce( |
| 1044 | createJsonResponse({ |
| 1045 | access_token: "col_at_123", |
| 1046 | token_type: "Bearer", |
| 1047 | expires_in: 3600, |
| 1048 | refresh_token: "col_rt_123", |
| 1049 | scope: "openid profile email", |
| 1050 | }), |
| 1051 | ) |
| 1052 | .mockResolvedValueOnce( |
| 1053 | createJsonResponse({ |
| 1054 | sub: "user_123", |
| 1055 | email: "radin@example.com", |
| 1056 | email_verified: true, |
| 1057 | name: "Radin", |
| 1058 | given_name: "Radin", |
| 1059 | family_name: null, |
| 1060 | picture: null, |
| 1061 | preferred_username: "radin", |
| 1062 | scope: "openid profile email", |
| 1063 | }), |
| 1064 | ); |
| 1065 |
| 1066 | const client = new ColineApiClient({ |
| 1067 | baseUrl: "https://api.coline.app", |
| 1068 | fetch: fetchMock, |
| 1069 | }); |
| 1070 |
| 1071 | const authorizeUrl = client.buildLoginWithColineAuthorizeUrl({ |
| 1072 | clientId: "col_client_123", |
| 1073 | redirectUri: "https://example.com/callback", |
| 1074 | scope: "openid profile email", |
| 1075 | state: "state_123", |
| 1076 | }); |
| 1077 | expect(authorizeUrl).toContain("/api/v1/oauth/authorize"); |
| 1078 | expect(authorizeUrl).toContain("client_id=col_client_123"); |
| 1079 |
| 1080 | const token = await client.exchangeOAuthCode({ |
| 1081 | clientId: "col_client_123", |
| 1082 | clientSecret: "col_secret_123", |
| 1083 | code: "col_code_123", |
| 1084 | redirectUri: "https://example.com/callback", |
| 1085 | }); |
| 1086 | expect(token.access_token).toBe("col_at_123"); |
| 1087 |
| 1088 | const userInfo = await client.getOAuthUserInfo("col_at_123"); |
| 1089 | expect(userInfo.sub).toBe("user_123"); |
| 1090 | }); |
| 1091 |
| 1092 | it("supports client cloning with headers and api keys", async () => { |
| 1093 | const fetchMock = vi.fn().mockResolvedValue( |
| 1094 | createJsonResponse({ |
| 1095 | data: { |
| 1096 | apps: [], |
| 1097 | }, |
| 1098 | }), |
| 1099 | ); |
| 1100 |
| 1101 | const baseClient = new ColineApiClient({ |
| 1102 | baseUrl: "https://api.coline.app", |
| 1103 | fetch: fetchMock, |
| 1104 | headers: { |
| 1105 | "X-Test": "base", |
| 1106 | }, |
| 1107 | }); |
| 1108 |
| 1109 | const client = baseClient.withApiKey("test-key").withHeaders({ |
| 1110 | "X-Request-Source": "sdk-test", |
| 1111 | }); |
| 1112 |
| 1113 | await client.listApps(); |
| 1114 |
| 1115 | const headers = fetchMock.mock.calls[0]?.[1]?.headers as Headers; |
| 1116 | expect(headers.get("Authorization")).toBe("Bearer test-key"); |
| 1117 | expect(headers.get("X-Test")).toBe("base"); |
| 1118 | expect(headers.get("X-Request-Source")).toBe("sdk-test"); |
| 1119 | }); |
| 1120 |
| 1121 | it("throws structured api errors", async () => { |
| 1122 | const fetchMock = vi.fn().mockResolvedValue( |
| 1123 | createJsonResponse({ |
| 1124 | error: { |
| 1125 | code: "FORBIDDEN", |
| 1126 | message: "No access.", |
| 1127 | }, |
| 1128 | }, 403), |
| 1129 | ); |
| 1130 |
| 1131 | const client = new ColineApiClient({ |
| 1132 | baseUrl: "https://api.coline.app", |
| 1133 | fetch: fetchMock, |
| 1134 | }); |
| 1135 |
| 1136 | await expect(client.listApps()).rejects.toBeInstanceOf(ColineApiError); |
| 1137 | }); |
| 1138 | }); |
| 1139 |