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