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("manages workspace app secrets, oauth connections, deliveries, and review submission", async () => { |
| 237 | const fetchMock = vi |
| 238 | .fn() |
| 239 | .mockResolvedValueOnce( |
| 240 | createJsonResponse({ |
| 241 | data: { |
| 242 | install: { |
| 243 | installId: "install_123", |
| 244 | appId: "app_123", |
| 245 | appKey: "com.coline.notes", |
| 246 | name: "Notes", |
| 247 | description: null, |
| 248 | iconUrl: null, |
| 249 | status: "active", |
| 250 | installScope: "workspace", |
| 251 | scopeSubjectId: "workspace_123", |
| 252 | version: "1.0.0", |
| 253 | }, |
| 254 | secrets: [], |
| 255 | permissions: { canManageApps: true }, |
| 256 | }, |
| 257 | }), |
| 258 | ) |
| 259 | .mockResolvedValueOnce( |
| 260 | createJsonResponse({ |
| 261 | data: { |
| 262 | secret: { |
| 263 | id: "secret_123", |
| 264 | key: "OPENAI_API_KEY", |
| 265 | createdAt: "2026-04-02T00:00:00.000Z", |
| 266 | updatedAt: "2026-04-02T00:00:00.000Z", |
| 267 | }, |
| 268 | value: "sk-test", |
| 269 | }, |
| 270 | }), |
| 271 | ) |
| 272 | .mockResolvedValueOnce(createJsonResponse({ data: { ok: true } })) |
| 273 | .mockResolvedValueOnce( |
| 274 | createJsonResponse({ |
| 275 | data: { |
| 276 | install: { |
| 277 | installId: "install_123", |
| 278 | appId: "app_123", |
| 279 | appKey: "com.coline.notes", |
| 280 | name: "Notes", |
| 281 | description: null, |
| 282 | iconUrl: null, |
| 283 | status: "active", |
| 284 | installScope: "workspace", |
| 285 | scopeSubjectId: "workspace_123", |
| 286 | version: "1.0.0", |
| 287 | }, |
| 288 | connections: [], |
| 289 | permissions: { canManageApps: true }, |
| 290 | }, |
| 291 | }), |
| 292 | ) |
| 293 | .mockResolvedValueOnce( |
| 294 | createJsonResponse({ |
| 295 | data: { |
| 296 | connection: { |
| 297 | id: "conn_123", |
| 298 | providerKey: "github", |
| 299 | externalAccountId: "octocat", |
| 300 | scopes: ["repo"], |
| 301 | tokenExpiresAt: null, |
| 302 | createdAt: "2026-04-02T00:00:00.000Z", |
| 303 | updatedAt: "2026-04-02T00:00:00.000Z", |
| 304 | }, |
| 305 | }, |
| 306 | }), |
| 307 | ) |
| 308 | .mockResolvedValueOnce(createJsonResponse({ data: { ok: true } })) |
| 309 | .mockResolvedValueOnce( |
| 310 | createJsonResponse({ |
| 311 | data: { |
| 312 | install: { |
| 313 | installId: "install_123", |
| 314 | appId: "app_123", |
| 315 | appKey: "com.coline.notes", |
| 316 | name: "Notes", |
| 317 | description: null, |
| 318 | iconUrl: null, |
| 319 | status: "active", |
| 320 | installScope: "workspace", |
| 321 | scopeSubjectId: "workspace_123", |
| 322 | version: "1.0.0", |
| 323 | }, |
| 324 | deliveries: [], |
| 325 | permissions: { canManageApps: true }, |
| 326 | }, |
| 327 | }), |
| 328 | ) |
| 329 | .mockResolvedValueOnce( |
| 330 | createJsonResponse({ |
| 331 | data: { |
| 332 | delivery: { |
| 333 | id: "delivery_123", |
| 334 | eventType: "app.test", |
| 335 | targetUrl: "https://notes.example.com/coline/events", |
| 336 | status: "delivered", |
| 337 | attemptCount: 1, |
| 338 | lastStatusCode: 200, |
| 339 | createdAt: "2026-04-02T00:00:00.000Z", |
| 340 | updatedAt: "2026-04-02T00:00:00.000Z", |
| 341 | }, |
| 342 | }, |
| 343 | }), |
| 344 | ) |
| 345 | .mockResolvedValueOnce( |
| 346 | createJsonResponse({ |
| 347 | data: { |
| 348 | delivery: { |
| 349 | id: "delivery_123", |
| 350 | eventType: "app.test", |
| 351 | targetUrl: "https://notes.example.com/coline/events", |
| 352 | status: "delivered", |
| 353 | attemptCount: 2, |
| 354 | lastStatusCode: 200, |
| 355 | createdAt: "2026-04-02T00:00:00.000Z", |
| 356 | updatedAt: "2026-04-02T00:01:00.000Z", |
| 357 | }, |
| 358 | }, |
| 359 | }), |
| 360 | ) |
| 361 | .mockResolvedValueOnce( |
| 362 | createJsonResponse({ |
| 363 | data: { |
| 364 | appId: "app_123", |
| 365 | appKey: "com.coline.notes", |
| 366 | versionId: "ver_123", |
| 367 | version: "1.0.0", |
| 368 | reviewStatus: "submitted", |
| 369 | storeListed: true, |
| 370 | }, |
| 371 | }), |
| 372 | ); |
| 373 |
| 374 | const client = new ColineApiClient({ |
| 375 | baseUrl: "https://api.coline.app", |
| 376 | fetch: fetchMock, |
| 377 | }); |
| 378 |
| 379 | await client.listWorkspaceAppSecrets("acme", "com.coline.notes"); |
| 380 | await client.createWorkspaceAppSecret("acme", "com.coline.notes", { |
| 381 | key: "OPENAI_API_KEY", |
| 382 | value: "sk-test", |
| 383 | }); |
| 384 | await client.deleteWorkspaceAppSecret( |
| 385 | "acme", |
| 386 | "com.coline.notes", |
| 387 | "secret_123", |
| 388 | ); |
| 389 |
| 390 | await client.listWorkspaceAppOauthConnections("acme", "com.coline.notes"); |
| 391 | await client.createWorkspaceAppOauthConnection("acme", "com.coline.notes", { |
| 392 | providerKey: "github", |
| 393 | accessToken: "gho_test", |
| 394 | scopes: ["repo"], |
| 395 | externalAccountId: "octocat", |
| 396 | }); |
| 397 | await client.deleteWorkspaceAppOauthConnection( |
| 398 | "acme", |
| 399 | "com.coline.notes", |
| 400 | "conn_123", |
| 401 | ); |
| 402 |
| 403 | await client.listWorkspaceAppDeliveries("acme", "com.coline.notes"); |
| 404 | await client.sendWorkspaceAppTestDelivery("acme", "com.coline.notes", { |
| 405 | eventType: "app.test", |
| 406 | payload: { ok: true }, |
| 407 | }); |
| 408 | await client.replayWorkspaceAppDelivery( |
| 409 | "acme", |
| 410 | "com.coline.notes", |
| 411 | "delivery_123", |
| 412 | ); |
| 413 |
| 414 | await client.submitAppVersionForReview("com.coline.notes", "ver_123", { |
| 415 | storeListed: true, |
| 416 | }); |
| 417 |
| 418 | expect(fetchMock).toHaveBeenNthCalledWith( |
| 419 | 1, |
| 420 | expect.any(Request), |
| 421 | expect.any(Object), |
| 422 | ); |
| 423 | expect((fetchMock.mock.calls[0]?.[0] as Request).url).toBe( |
| 424 | "https://api.coline.app/api/v1/workspaces/acme/apps/com.coline.notes/secrets", |
| 425 | ); |
| 426 | expect(fetchMock).toHaveBeenNthCalledWith( |
| 427 | 5, |
| 428 | expect.any(Request), |
| 429 | expect.any(Object), |
| 430 | ); |
| 431 | expect((fetchMock.mock.calls[4]?.[0] as Request).url).toBe( |
| 432 | "https://api.coline.app/api/v1/workspaces/acme/apps/com.coline.notes/oauth-connections", |
| 433 | ); |
| 434 | expect(fetchMock).toHaveBeenNthCalledWith( |
| 435 | 8, |
| 436 | expect.any(Request), |
| 437 | expect.any(Object), |
| 438 | ); |
| 439 | expect((fetchMock.mock.calls[7]?.[0] as Request).url).toBe( |
| 440 | "https://api.coline.app/api/v1/workspaces/acme/apps/com.coline.notes/deliveries", |
| 441 | ); |
| 442 | expect(fetchMock).toHaveBeenNthCalledWith( |
| 443 | 9, |
| 444 | expect.any(Request), |
| 445 | expect.any(Object), |
| 446 | ); |
| 447 | expect((fetchMock.mock.calls[8]?.[0] as Request).url).toBe( |
| 448 | "https://api.coline.app/api/v1/workspaces/acme/apps/com.coline.notes/deliveries/delivery_123/replay", |
| 449 | ); |
| 450 | expect(fetchMock).toHaveBeenNthCalledWith( |
| 451 | 10, |
| 452 | expect.any(Request), |
| 453 | expect.any(Object), |
| 454 | ); |
| 455 | expect((fetchMock.mock.calls[9]?.[0] as Request).url).toBe( |
| 456 | "https://api.coline.app/api/v1/apps/com.coline.notes/versions/ver_123/submit", |
| 457 | ); |
| 458 | }); |
| 459 |
| 460 | it("loads app detail, app versions, installed app, and uninstalls it", async () => { |
| 461 | const fetchMock = vi |
| 462 | .fn() |
| 463 | .mockResolvedValueOnce( |
| 464 | createJsonResponse({ |
| 465 | data: { |
| 466 | app: { |
| 467 | appId: "app_123", |
| 468 | appKey: "com.coline.notes", |
| 469 | name: "Notes", |
| 470 | description: null, |
| 471 | iconUrl: null, |
| 472 | status: "active", |
| 473 | ownerUserId: "user_123", |
| 474 | latestPublishedVersion: "0.1.0", |
| 475 | latestPublishedVersionId: "ver_123", |
| 476 | }, |
| 477 | }, |
| 478 | }), |
| 479 | ) |
| 480 | .mockResolvedValueOnce( |
| 481 | createJsonResponse({ |
| 482 | data: { |
| 483 | versions: [ |
| 484 | { |
| 485 | versionId: "ver_123", |
| 486 | version: "0.1.0", |
| 487 | status: "published", |
| 488 | apiBaseUrl: "https://notes.example.com", |
| 489 | permissions: ["app.home.read"], |
| 490 | kairo: { |
| 491 | enabled: true, |
| 492 | description: "Notes documents can be searched by Kairo.", |
| 493 | instructions: "Search notes before answering document questions.", |
| 494 | documentTypes: [ |
| 495 | { |
| 496 | documentType: "note", |
| 497 | name: "Note", |
| 498 | metadata: [], |
| 499 | }, |
| 500 | ], |
| 501 | }, |
| 502 | fileTypes: [], |
| 503 | createdAt: "2026-04-01T00:00:00.000Z", |
| 504 | updatedAt: "2026-04-01T00:00:00.000Z", |
| 505 | }, |
| 506 | ], |
| 507 | }, |
| 508 | }), |
| 509 | ) |
| 510 | .mockResolvedValueOnce( |
| 511 | createJsonResponse({ |
| 512 | data: { |
| 513 | install: { |
| 514 | installId: "install_123", |
| 515 | appId: "app_123", |
| 516 | appKey: "com.coline.notes", |
| 517 | name: "Notes", |
| 518 | description: null, |
| 519 | iconUrl: null, |
| 520 | status: "active", |
| 521 | installScope: "workspace", |
| 522 | scopeSubjectId: "workspace_123", |
| 523 | version: "0.1.0", |
| 524 | }, |
| 525 | permissions: { |
| 526 | canManageApps: true, |
| 527 | }, |
| 528 | }, |
| 529 | }), |
| 530 | ) |
| 531 | .mockResolvedValueOnce(createJsonResponse({ data: { ok: true } })); |
| 532 |
| 533 | const client = new ColineApiClient({ |
| 534 | baseUrl: "https://api.coline.app", |
| 535 | fetch: fetchMock, |
| 536 | }); |
| 537 |
| 538 | const app = await client.getApp("com.coline.notes"); |
| 539 | expect(app.app.appKey).toBe("com.coline.notes"); |
| 540 |
| 541 | const versions = await client.listAppVersions("com.coline.notes"); |
| 542 | expect(versions.versions[0]?.version).toBe("0.1.0"); |
| 543 | const kairo = versions.versions[0]?.kairo as |
| 544 | | { documentTypes?: Array<{ documentType?: string }> } |
| 545 | | null |
| 546 | | undefined; |
| 547 | expect(kairo?.documentTypes?.[0]?.documentType).toBe("note"); |
| 548 |
| 549 | const installed = await client.getWorkspaceInstalledApp("acme", "com.coline.notes"); |
| 550 | expect(installed.install.installId).toBe("install_123"); |
| 551 |
| 552 | const uninstallResult = await client.uninstallWorkspaceApp( |
| 553 | "acme", |
| 554 | "com.coline.notes", |
| 555 | ); |
| 556 | expect(uninstallResult.ok).toBe(true); |
| 557 | }); |
| 558 |
| 559 | it("loads and updates app file documents through the public runtime API", async () => { |
| 560 | const fetchMock = vi |
| 561 | .fn() |
| 562 | .mockResolvedValueOnce( |
| 563 | createJsonResponse({ |
| 564 | data: { |
| 565 | file: { |
| 566 | id: "file_123", |
| 567 | name: "Customer", |
| 568 | fileType: "com.acme.customer", |
| 569 | appKey: "acme-crm", |
| 570 | appName: "Acme CRM", |
| 571 | typeLabel: "Customer", |
| 572 | openHref: "/acme/acme-crm/files/file_123", |
| 573 | updatedAt: "2026-03-18T08:00:00.000Z", |
| 574 | }, |
| 575 | document: { status: "active" }, |
| 576 | version: 2, |
| 577 | capabilities: { |
| 578 | canRead: true, |
| 579 | canWrite: true, |
| 580 | canManage: false, |
| 581 | }, |
| 582 | }, |
| 583 | }), |
| 584 | ) |
| 585 | .mockResolvedValueOnce( |
| 586 | createJsonResponse({ |
| 587 | data: { |
| 588 | document: { status: "archived" }, |
| 589 | version: 3, |
| 590 | updatedAt: "2026-03-18T09:00:00.000Z", |
| 591 | }, |
| 592 | }), |
| 593 | ); |
| 594 |
| 595 | const client = new ColineApiClient({ |
| 596 | baseUrl: "https://api.coline.app", |
| 597 | fetch: fetchMock, |
| 598 | }); |
| 599 |
| 600 | const file = await client.getAppFileDocument("acme", "acme-crm", "file_123"); |
| 601 | expect(file.file.id).toBe("file_123"); |
| 602 | expect(file.version).toBe(2); |
| 603 |
| 604 | const updated = await client.updateAppFileDocument( |
| 605 | "acme", |
| 606 | "acme-crm", |
| 607 | "file_123", |
| 608 | { status: "archived" }, |
| 609 | ); |
| 610 | expect(updated.version).toBe(3); |
| 611 | }); |
| 612 |
| 613 | it("lists Tab models and creates OpenAI-compatible Tab completions", async () => { |
| 614 | const fetchMock = vi |
| 615 | .fn() |
| 616 | .mockResolvedValueOnce( |
| 617 | createJsonResponse({ |
| 618 | data: { |
| 619 | object: "list", |
| 620 | data: [ |
| 621 | { |
| 622 | id: "tab-v1", |
| 623 | object: "model", |
| 624 | created: 1775200000, |
| 625 | owned_by: "coline", |
| 626 | providerModelId: "gemini-2.5-flash-lite", |
| 627 | pricing: { |
| 628 | inputPerMillionUsd: 0.2, |
| 629 | outputPerMillionUsd: 0.5, |
| 630 | }, |
| 631 | capabilities: { |
| 632 | streaming: true, |
| 633 | surfaces: ["notes", "docs"], |
| 634 | }, |
| 635 | }, |
| 636 | ], |
| 637 | }, |
| 638 | }), |
| 639 | ) |
| 640 | .mockResolvedValueOnce( |
| 641 | createJsonResponse({ |
| 642 | data: { |
| 643 | id: "chatcmpl_123", |
| 644 | object: "chat.completion", |
| 645 | created: 1775200001, |
| 646 | model: "tab-v1", |
| 647 | choices: [ |
| 648 | { |
| 649 | index: 0, |
| 650 | message: { |
| 651 | role: "assistant", |
| 652 | content: " world", |
| 653 | }, |
| 654 | finish_reason: "stop", |
| 655 | }, |
| 656 | ], |
| 657 | usage: { |
| 658 | prompt_tokens: 10, |
| 659 | completion_tokens: 2, |
| 660 | total_tokens: 12, |
| 661 | }, |
| 662 | }, |
| 663 | }), |
| 664 | ); |
| 665 |
| 666 | const client = new ColineApiClient({ |
| 667 | baseUrl: "https://api.coline.app", |
| 668 | fetch: fetchMock, |
| 669 | }); |
| 670 |
| 671 | const models = await client.listTabModels(); |
| 672 | expect(models.data[0]?.id).toBe("tab-v1"); |
| 673 |
| 674 | const completion = await client.createTabChatCompletion({ |
| 675 | model: "tab-v1", |
| 676 | tab_context: { |
| 677 | surface: "notes", |
| 678 | workspace_slug: "acme", |
| 679 | entity_id: "note_123", |
| 680 | document_type: "markdown", |
| 681 | active_text_before_cursor: "Hello", |
| 682 | active_text_after_cursor: "", |
| 683 | surrounding_context: "Title: Roadmap", |
| 684 | }, |
| 685 | }); |
| 686 |
| 687 | expect(completion.choices[0]?.message.content).toBe(" world"); |
| 688 | }); |
| 689 |
| 690 | it("builds Login with Coline authorize URLs and exchanges OAuth tokens", async () => { |
| 691 | const fetchMock = vi |
| 692 | .fn() |
| 693 | .mockResolvedValueOnce( |
| 694 | createJsonResponse({ |
| 695 | access_token: "col_at_123", |
| 696 | token_type: "Bearer", |
| 697 | expires_in: 3600, |
| 698 | refresh_token: "col_rt_123", |
| 699 | scope: "openid profile email", |
| 700 | }), |
| 701 | ) |
| 702 | .mockResolvedValueOnce( |
| 703 | createJsonResponse({ |
| 704 | sub: "user_123", |
| 705 | email: "radin@example.com", |
| 706 | email_verified: true, |
| 707 | name: "Radin", |
| 708 | given_name: "Radin", |
| 709 | family_name: null, |
| 710 | picture: null, |
| 711 | preferred_username: "radin", |
| 712 | scope: "openid profile email", |
| 713 | }), |
| 714 | ); |
| 715 |
| 716 | const client = new ColineApiClient({ |
| 717 | baseUrl: "https://api.coline.app", |
| 718 | fetch: fetchMock, |
| 719 | }); |
| 720 |
| 721 | const authorizeUrl = client.buildLoginWithColineAuthorizeUrl({ |
| 722 | clientId: "col_client_123", |
| 723 | redirectUri: "https://example.com/callback", |
| 724 | scope: "openid profile email", |
| 725 | state: "state_123", |
| 726 | }); |
| 727 | expect(authorizeUrl).toContain("/api/v1/oauth/authorize"); |
| 728 | expect(authorizeUrl).toContain("client_id=col_client_123"); |
| 729 |
| 730 | const token = await client.exchangeOAuthCode({ |
| 731 | clientId: "col_client_123", |
| 732 | clientSecret: "col_secret_123", |
| 733 | code: "col_code_123", |
| 734 | redirectUri: "https://example.com/callback", |
| 735 | }); |
| 736 | expect(token.access_token).toBe("col_at_123"); |
| 737 |
| 738 | const userInfo = await client.getOAuthUserInfo("col_at_123"); |
| 739 | expect(userInfo.sub).toBe("user_123"); |
| 740 | }); |
| 741 |
| 742 | it("supports client cloning with headers and api keys", async () => { |
| 743 | const fetchMock = vi.fn().mockResolvedValue( |
| 744 | createJsonResponse({ |
| 745 | data: { |
| 746 | apps: [], |
| 747 | }, |
| 748 | }), |
| 749 | ); |
| 750 |
| 751 | const baseClient = new ColineApiClient({ |
| 752 | baseUrl: "https://api.coline.app", |
| 753 | fetch: fetchMock, |
| 754 | headers: { |
| 755 | "X-Test": "base", |
| 756 | }, |
| 757 | }); |
| 758 |
| 759 | const client = baseClient.withApiKey("test-key").withHeaders({ |
| 760 | "X-Request-Source": "sdk-test", |
| 761 | }); |
| 762 |
| 763 | await client.listApps(); |
| 764 |
| 765 | const headers = fetchMock.mock.calls[0]?.[1]?.headers as Headers; |
| 766 | expect(headers.get("Authorization")).toBe("Bearer test-key"); |
| 767 | expect(headers.get("X-Test")).toBe("base"); |
| 768 | expect(headers.get("X-Request-Source")).toBe("sdk-test"); |
| 769 | }); |
| 770 |
| 771 | it("throws structured api errors", async () => { |
| 772 | const fetchMock = vi.fn().mockResolvedValue( |
| 773 | createJsonResponse({ |
| 774 | error: { |
| 775 | code: "FORBIDDEN", |
| 776 | message: "No access.", |
| 777 | }, |
| 778 | }, 403), |
| 779 | ); |
| 780 |
| 781 | const client = new ColineApiClient({ |
| 782 | baseUrl: "https://api.coline.app", |
| 783 | fetch: fetchMock, |
| 784 | }); |
| 785 |
| 786 | await expect(client.listApps()).rejects.toBeInstanceOf(ColineApiError); |
| 787 | }); |
| 788 | }); |
| 789 |