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 type { ColineTask, ColineCalendarEvent, ColineMessage } from "../capabilities/workspace"; |
| 5 | import type { |
| 6 | ColineActorRef, |
| 7 | ColineCapabilities, |
| 8 | ColineFileSummaryV2, |
| 9 | ColineMemberSummary, |
| 10 | ColineNetFetchInput, |
| 11 | ColineNetFetchResult, |
| 12 | ColineRecordEnvelope, |
| 13 | } from "../capabilities/types"; |
| 14 |
| 15 | /** |
| 16 | * The in-memory capability fake behind `createTestWorkspace` (plan §14.2). |
| 17 | * Implements the same `coline.*` contract apps run against in production, |
| 18 | * backed by maps — no network, no database, fully inspectable. |
| 19 | */ |
| 20 |
| 21 | export interface TestWorkspaceOptions { |
| 22 | grantedPermissions?: import("../manifest/permissions").AppPermissionV2[]; |
| 23 | workspaceId?: string; |
| 24 | workspaceSlug?: string; |
| 25 | workspaceName?: string; |
| 26 | appKey?: string; |
| 27 | installId?: string; |
| 28 | members?: Array<Partial<ColineMemberSummary> & { userId: string }>; |
| 29 | /** Handler for coline.net.fetch; defaults to rejecting. */ |
| 30 | onNetFetch?: (input: ColineNetFetchInput) => Promise<ColineNetFetchResult>; |
| 31 | /** Models enabled by the deterministic test provider. */ |
| 32 | aiModels?: import("../capabilities/ai").ColineAiModelInfo[]; |
| 33 | onAiJob?: Parameters<typeof fakeAi>[3]; |
| 34 | /** Handler for coline.ai.generate; defaults to echoing. */ |
| 35 | onAiGenerate?: (prompt: string) => Promise<string>; |
| 36 | /** Picker results; default null (dismissed) / false (confirm). */ |
| 37 | pickers?: { |
| 38 | file?: () => Promise<{ fileId: string } | null>; |
| 39 | member?: () => Promise<{ userId: string } | null>; |
| 40 | confirm?: () => Promise<boolean>; |
| 41 | }; |
| 42 | } |
| 43 |
| 44 | export interface TestFile extends ColineFileSummaryV2 { |
| 45 | document: Record<string, unknown>; |
| 46 | trashed: boolean; |
| 47 | } |
| 48 |
| 49 | export interface TestNotification { |
| 50 | channelKey: string; |
| 51 | title: string; |
| 52 | body?: string | undefined; |
| 53 | href: string; |
| 54 | recipients: string[]; |
| 55 | } |
| 56 |
| 57 | export interface TestIndexDocument { |
| 58 | documentKey: string; |
| 59 | documentType: string; |
| 60 | title: string; |
| 61 | body: string; |
| 62 | fileId?: string | undefined; |
| 63 | } |
| 64 |
| 65 | export interface TestNavigationEvent { |
| 66 | intent: "openFile" | "openAppHome" | "navigate" | "openReference"; |
| 67 | payload: Record<string, unknown>; |
| 68 | } |
| 69 |
| 70 | export class TestWorkspaceState { |
| 71 | readonly tasks = new Map<string, ColineTask>(); |
| 72 | readonly calendar = new Map<string, ColineCalendarEvent>(); |
| 73 | readonly messages = new Map<string, ColineMessage[]>(); |
| 74 | readonly files = new Map<string, TestFile>(); |
| 75 | readonly kv = new Map<string, unknown>(); |
| 76 | readonly recordRevisions = new Map<string, number>(); |
| 77 | readonly realtimeListeners = new Map< |
| 78 | string, |
| 79 | Set<(message: { event: string; data: unknown }) => void> |
| 80 | >(); |
| 81 | readonly records = new Map<string, Map<string, ColineRecordEnvelope<Record<string, unknown>>>>(); |
| 82 | readonly notifications: TestNotification[] = []; |
| 83 | readonly indexDocuments = new Map<string, TestIndexDocument>(); |
| 84 | readonly navigations: TestNavigationEvent[] = []; |
| 85 | readonly events: { kind: string; payload?: Record<string, unknown> }[] = []; |
| 86 | readonly published: { event: string; data: unknown }[] = []; |
| 87 | readonly netRequests: ColineNetFetchInput[] = []; |
| 88 |
| 89 | private fileCounter = 0; |
| 90 | private recordCounter = 0; |
| 91 |
| 92 | nextFileId(): string { |
| 93 | this.fileCounter += 1; |
| 94 | return `file_test_${this.fileCounter}`; |
| 95 | } |
| 96 |
| 97 | nextRecordId(): string { |
| 98 | this.recordCounter += 1; |
| 99 | return `rec_test_${this.recordCounter}`; |
| 100 | } |
| 101 |
| 102 | filesByType(typeKey: string): TestFile[] { |
| 103 | return [...this.files.values()].filter((file) => file.typeKey === typeKey && !file.trashed); |
| 104 | } |
| 105 | } |
| 106 |
| 107 | export function createFakeCapabilities( |
| 108 | state: TestWorkspaceState, |
| 109 | options: Required< |
| 110 | Pick< |
| 111 | TestWorkspaceOptions, |
| 112 | "workspaceId" | "workspaceSlug" | "workspaceName" | "appKey" | "installId" |
| 113 | > |
| 114 | > & { |
| 115 | members: ColineMemberSummary[]; |
| 116 | actor: ColineActorRef; |
| 117 | onNetFetch: TestWorkspaceOptions["onNetFetch"]; |
| 118 | onAiGenerate: TestWorkspaceOptions["onAiGenerate"]; |
| 119 | aiModels?: TestWorkspaceOptions["aiModels"]; |
| 120 | onAiJob?: TestWorkspaceOptions["onAiJob"]; |
| 121 | pickers: NonNullable<TestWorkspaceOptions["pickers"]>; |
| 122 | }, |
| 123 | ): ColineCapabilities { |
| 124 | const now = () => new Date().toISOString(); |
| 125 |
| 126 | return { |
| 127 | ...createFakeWorkspaceCapabilities( |
| 128 | state, |
| 129 | options.actor.kind === "user" ? options.actor.userId : null, |
| 130 | ), |
| 131 | tools: { |
| 132 | invoke: async () => { |
| 133 | throw new Error("Use createTestWorkspace to invoke tools."); |
| 134 | }, |
| 135 | }, |
| 136 | workspace: { |
| 137 | get: async () => ({ |
| 138 | workspaceId: options.workspaceId, |
| 139 | workspaceSlug: options.workspaceSlug, |
| 140 | workspaceName: options.workspaceName, |
| 141 | }), |
| 142 | }, |
| 143 | members: { |
| 144 | list: async () => ({ members: options.members }), |
| 145 | get: async (userId) => options.members.find((member) => member.userId === userId) ?? null, |
| 146 | }, |
| 147 | files: { |
| 148 | readUrl: async () => { throw new Error("Signed file URLs are unavailable in the in-memory test workspace."); }, |
| 149 | list: async (input) => { |
| 150 | let files = [...state.files.values()].filter((file) => !file.trashed); |
| 151 | if (input?.typeKey) files = files.filter((file) => file.typeKey === input.typeKey); |
| 152 | if (input?.driveId) files = files.filter((file) => file.driveId === input.driveId); |
| 153 | const start = input?.cursor |
| 154 | ? files.findIndex((file) => file.fileId === input.cursor) + 1 |
| 155 | : 0; |
| 156 | const limit = input?.limit ?? 50; |
| 157 | const page = files.slice(start, start + limit); |
| 158 | return { |
| 159 | files: structuredClone(page), |
| 160 | nextCursor: files.length > start + limit ? page.at(-1)!.fileId : null, |
| 161 | }; |
| 162 | }, |
| 163 | get: async (fileId) => state.files.get(fileId) ?? null, |
| 164 | create: async (input) => { |
| 165 | const file: TestFile = { |
| 166 | fileId: state.nextFileId(), |
| 167 | name: input.name, |
| 168 | typeKey: input.typeKey, |
| 169 | driveId: input.driveId ?? "drive_test_app", |
| 170 | parentId: input.parentId ?? null, |
| 171 | version: 1, |
| 172 | createdAt: now(), |
| 173 | updatedAt: now(), |
| 174 | document: input.document ?? {}, |
| 175 | trashed: false, |
| 176 | }; |
| 177 | state.files.set(file.fileId, file); |
| 178 | return file; |
| 179 | }, |
| 180 | update: async (fileId, input) => { |
| 181 | const file = state.files.get(fileId); |
| 182 | if (!file) throw new Error(`Test file not found: ${fileId}`); |
| 183 | if (input.name) file.name = input.name; |
| 184 | file.updatedAt = now(); |
| 185 | return file; |
| 186 | }, |
| 187 | trash: async (fileId) => { |
| 188 | const file = state.files.get(fileId); |
| 189 | if (!file) throw new Error(`Test file not found: ${fileId}`); |
| 190 | file.trashed = true; |
| 191 | }, |
| 192 | getDocument: async (fileId) => { |
| 193 | const file = state.files.get(fileId); |
| 194 | if (!file) throw new Error(`Test file not found: ${fileId}`); |
| 195 | return file.document; |
| 196 | }, |
| 197 | updateDocument: async (fileId, document, options) => { |
| 198 | const file = state.files.get(fileId); |
| 199 | if (!file) throw new Error(`Test file not found: ${fileId}`); |
| 200 | if (options?.expectedVersion !== undefined && options.expectedVersion !== file.version) |
| 201 | throw Object.assign(new Error("The document changed. Reload before saving."), { |
| 202 | code: "document_conflict", |
| 203 | }); |
| 204 | file.document = structuredClone(document); |
| 205 | file.version += 1; |
| 206 | file.updatedAt = now(); |
| 207 | return { version: file.version, updatedAt: file.updatedAt }; |
| 208 | }, |
| 209 | }, |
| 210 | drives: { |
| 211 | getAppDrive: async () => ({ driveId: "drive_test_app" }), |
| 212 | }, |
| 213 | storage: { |
| 214 | kv: { |
| 215 | get: async (key) => state.kv.get(key) ?? null, |
| 216 | set: async (key, value) => { |
| 217 | state.kv.set(key, value ?? null); |
| 218 | }, |
| 219 | delete: async (key) => { |
| 220 | state.kv.delete(key); |
| 221 | }, |
| 222 | list: async (input) => ({ |
| 223 | keys: [...state.kv.keys()] |
| 224 | .filter((key) => !input?.prefix || key.startsWith(input.prefix)) |
| 225 | .sort() |
| 226 | .slice(0, input?.limit ?? 200), |
| 227 | }), |
| 228 | }, |
| 229 | collection: <TData>(name: string) => |
| 230 | createFakeCollection<TData>(state, name, options.actor, options.installId), |
| 231 | }, |
| 232 | search: { |
| 233 | query: async (input) => { |
| 234 | const query = input.query.toLowerCase(); |
| 235 | const matches = [...state.indexDocuments.values()].filter((doc) => |
| 236 | `${doc.title} ${doc.body}`.toLowerCase().includes(query), |
| 237 | ); |
| 238 | const offset = Number(input.cursor ?? 0), |
| 239 | limit = input.limit ?? 50; |
| 240 | return { |
| 241 | results: matches.slice(offset, offset + limit).map((doc) => ({ |
| 242 | documentKey: doc.documentKey, |
| 243 | documentType: doc.documentType, |
| 244 | title: doc.title, |
| 245 | snippet: doc.body, |
| 246 | url: null, |
| 247 | fileId: doc.fileId ?? null, |
| 248 | })), |
| 249 | nextCursor: matches.length > offset + limit ? String(offset + limit) : null, |
| 250 | }; |
| 251 | }, |
| 252 | index: { |
| 253 | upsert: async (input) => { |
| 254 | for (const document of input.documents) { |
| 255 | state.indexDocuments.set(document.documentKey, { |
| 256 | documentKey: document.documentKey, |
| 257 | documentType: document.documentType, |
| 258 | title: document.title, |
| 259 | body: document.body, |
| 260 | fileId: document.fileId, |
| 261 | }); |
| 262 | } |
| 263 | return { upserted: input.documents.length }; |
| 264 | }, |
| 265 | delete: async (input) => { |
| 266 | let deleted = 0; |
| 267 | for (const key of input.documentKeys) { |
| 268 | if (state.indexDocuments.delete(key)) deleted += 1; |
| 269 | } |
| 270 | return { deleted }; |
| 271 | }, |
| 272 | }, |
| 273 | }, |
| 274 | notifications: { |
| 275 | create: async (input) => { |
| 276 | state.notifications.push({ |
| 277 | channelKey: input.channelKey, |
| 278 | title: input.title, |
| 279 | body: input.body, |
| 280 | href: input.href, |
| 281 | recipients: input.recipients.map((recipient) => recipient.userId), |
| 282 | }); |
| 283 | }, |
| 284 | }, |
| 285 | ai: { |
| 286 | ...fakeAi(state, options.actor.kind === "user" ? options.actor.userId : null, options.aiModels ?? [], options.onAiJob), |
| 287 | generate: async (prompt) => ({ |
| 288 | text: options.onAiGenerate ? await options.onAiGenerate(prompt) : `echo: ${prompt}`, |
| 289 | }), |
| 290 | }, |
| 291 | net: { |
| 292 | fetch: async (input) => { |
| 293 | state.netRequests.push(input); |
| 294 | if (!options.onNetFetch) { |
| 295 | throw new Error("coline.net.fetch called in a test without an onNetFetch handler."); |
| 296 | } |
| 297 | return options.onNetFetch(input); |
| 298 | }, |
| 299 | }, |
| 300 | pickers: { |
| 301 | file: async () => (options.pickers.file ? options.pickers.file() : null), |
| 302 | member: async () => (options.pickers.member ? options.pickers.member() : null), |
| 303 | confirm: async () => (options.pickers.confirm ? options.pickers.confirm() : false), |
| 304 | }, |
| 305 | navigation: { |
| 306 | openFile: async (fileId) => { |
| 307 | state.navigations.push({ intent: "openFile", payload: { fileId } }); |
| 308 | }, |
| 309 | openAppHome: async () => { |
| 310 | state.navigations.push({ intent: "openAppHome", payload: {} }); |
| 311 | }, |
| 312 | navigate: async (href) => { |
| 313 | state.navigations.push({ intent: "navigate", payload: { href } }); |
| 314 | }, |
| 315 | openReference: async (reference) => { |
| 316 | state.navigations.push({ intent: "openReference", payload: { reference } }); |
| 317 | }, |
| 318 | }, |
| 319 | realtime: { |
| 320 | subscribe: async (event, handler) => { |
| 321 | const listeners = state.realtimeListeners.get(event) ?? new Set(); |
| 322 | listeners.add(handler); |
| 323 | state.realtimeListeners.set(event, listeners); |
| 324 | return { |
| 325 | unsubscribe: () => { |
| 326 | listeners.delete(handler); |
| 327 | }, |
| 328 | }; |
| 329 | }, |
| 330 | publish: async (event, data) => { |
| 331 | state.published.push({ event, data: data ?? null }); |
| 332 | for (const listener of state.realtimeListeners.get(event) ?? []) |
| 333 | listener({ event, data: data ?? null }); |
| 334 | return { delivered: true }; |
| 335 | }, |
| 336 | }, |
| 337 | events: { |
| 338 | emit: async (event, data) => { |
| 339 | state.events.push({ kind: event, ...(data ? { payload: data } : {}) }); |
| 340 | return { emitted: true }; |
| 341 | }, |
| 342 | }, |
| 343 | }; |
| 344 | } |
| 345 |