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