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: Array<{ kind: string; payload?: Record<string, unknown> }> = []; |
| 70 | readonly netRequests: ColineNetFetchInput[] = []; |
| 71 |
| 72 | private fileCounter = 0; |
| 73 | private recordCounter = 0; |
| 74 |
| 75 | nextFileId(): string { |
| 76 | this.fileCounter += 1; |
| 77 | return `file_test_${this.fileCounter}`; |
| 78 | } |
| 79 |
| 80 | nextRecordId(): string { |
| 81 | this.recordCounter += 1; |
| 82 | return `rec_test_${this.recordCounter}`; |
| 83 | } |
| 84 |
| 85 | filesByType(typeKey: string): TestFile[] { |
| 86 | return [...this.files.values()].filter( |
| 87 | (file) => file.typeKey === typeKey && !file.trashed, |
| 88 | ); |
| 89 | } |
| 90 | } |
| 91 |
| 92 | export function createFakeCapabilities( |
| 93 | state: TestWorkspaceState, |
| 94 | options: Required<Pick<TestWorkspaceOptions, "workspaceId" | "workspaceSlug" | "workspaceName" | "appKey" | "installId">> & { |
| 95 | members: ColineMemberSummary[]; |
| 96 | actor: ColineActorRef; |
| 97 | onNetFetch: TestWorkspaceOptions["onNetFetch"]; |
| 98 | onAiGenerate: TestWorkspaceOptions["onAiGenerate"]; |
| 99 | pickers: NonNullable<TestWorkspaceOptions["pickers"]>; |
| 100 | }, |
| 101 | ): ColineCapabilities { |
| 102 | const now = () => new Date().toISOString(); |
| 103 |
| 104 | return { |
| 105 | workspace: { |
| 106 | get: async () => ({ |
| 107 | workspaceId: options.workspaceId, |
| 108 | workspaceSlug: options.workspaceSlug, |
| 109 | workspaceName: options.workspaceName, |
| 110 | }), |
| 111 | }, |
| 112 | members: { |
| 113 | list: async () => ({ members: options.members }), |
| 114 | get: async (userId) => |
| 115 | options.members.find((member) => member.userId === userId) ?? null, |
| 116 | }, |
| 117 | files: { |
| 118 | list: async (input) => { |
| 119 | let files = [...state.files.values()].filter((file) => !file.trashed); |
| 120 | if (input?.typeKey) files = files.filter((file) => file.typeKey === input.typeKey); |
| 121 | if (input?.driveId) files = files.filter((file) => file.driveId === input.driveId); |
| 122 | return { files: files.slice(0, input?.limit ?? 50) }; |
| 123 | }, |
| 124 | get: async (fileId) => state.files.get(fileId) ?? null, |
| 125 | create: async (input) => { |
| 126 | const file: TestFile = { |
| 127 | fileId: state.nextFileId(), |
| 128 | name: input.name, |
| 129 | typeKey: input.typeKey, |
| 130 | driveId: input.driveId ?? "drive_test_app", |
| 131 | parentId: input.parentId ?? null, |
| 132 | version: 1, |
| 133 | createdAt: now(), |
| 134 | updatedAt: now(), |
| 135 | document: input.document ?? {}, |
| 136 | trashed: false, |
| 137 | }; |
| 138 | state.files.set(file.fileId, file); |
| 139 | return file; |
| 140 | }, |
| 141 | update: async (fileId, input) => { |
| 142 | const file = state.files.get(fileId); |
| 143 | if (!file) throw new Error(`Test file not found: ${fileId}`); |
| 144 | if (input.name) file.name = input.name; |
| 145 | file.updatedAt = now(); |
| 146 | return file; |
| 147 | }, |
| 148 | trash: async (fileId) => { |
| 149 | const file = state.files.get(fileId); |
| 150 | if (!file) throw new Error(`Test file not found: ${fileId}`); |
| 151 | file.trashed = true; |
| 152 | }, |
| 153 | getDocument: async (fileId) => { |
| 154 | const file = state.files.get(fileId); |
| 155 | if (!file) throw new Error(`Test file not found: ${fileId}`); |
| 156 | return file.document; |
| 157 | }, |
| 158 | updateDocument: async (fileId, document) => { |
| 159 | const file = state.files.get(fileId); |
| 160 | if (!file) throw new Error(`Test file not found: ${fileId}`); |
| 161 | file.document = document; |
| 162 | file.version += 1; |
| 163 | file.updatedAt = now(); |
| 164 | return { version: file.version, updatedAt: file.updatedAt }; |
| 165 | }, |
| 166 | }, |
| 167 | drives: { |
| 168 | getAppDrive: async () => ({ driveId: "drive_test_app" }), |
| 169 | }, |
| 170 | storage: { |
| 171 | kv: { |
| 172 | get: async (key) => state.kv.get(key) ?? null, |
| 173 | set: async (key, value) => { |
| 174 | state.kv.set(key, value ?? null); |
| 175 | }, |
| 176 | delete: async (key) => { |
| 177 | state.kv.delete(key); |
| 178 | }, |
| 179 | list: async (input) => ({ |
| 180 | keys: [...state.kv.keys()] |
| 181 | .filter((key) => !input?.prefix || key.startsWith(input.prefix)) |
| 182 | .sort() |
| 183 | .slice(0, input?.limit ?? 200), |
| 184 | }), |
| 185 | }, |
| 186 | collection: <TData>(name: string) => { |
| 187 | const collection = () => { |
| 188 | let map = state.records.get(name); |
| 189 | if (!map) { |
| 190 | map = new Map(); |
| 191 | state.records.set(name, map); |
| 192 | } |
| 193 | return map; |
| 194 | }; |
| 195 | const wrap = (record: ColineRecordEnvelope<Record<string, unknown>>) => |
| 196 | record as unknown as ColineRecordEnvelope<TData>; |
| 197 | return { |
| 198 | insert: async (data: TData) => { |
| 199 | const record: ColineRecordEnvelope<Record<string, unknown>> = { |
| 200 | id: state.nextRecordId(), |
| 201 | appId: "app_test", |
| 202 | installId: options.installId, |
| 203 | collection: name, |
| 204 | createdBy: options.actor, |
| 205 | createdAt: now(), |
| 206 | updatedAt: now(), |
| 207 | data: data as Record<string, unknown>, |
| 208 | }; |
| 209 | collection().set(record.id, record); |
| 210 | return wrap(record); |
| 211 | }, |
| 212 | get: async (recordId: string) => { |
| 213 | const record = collection().get(recordId); |
| 214 | return record ? wrap(record) : null; |
| 215 | }, |
| 216 | update: async (recordId: string, data: Partial<TData>) => { |
| 217 | const record = collection().get(recordId); |
| 218 | if (!record) throw new Error(`Test record not found: ${recordId}`); |
| 219 | record.data = { ...record.data, ...(data as Record<string, unknown>) }; |
| 220 | record.updatedAt = now(); |
| 221 | return wrap(record); |
| 222 | }, |
| 223 | delete: async (recordId: string) => { |
| 224 | collection().delete(recordId); |
| 225 | }, |
| 226 | query: async () => ({ |
| 227 | records: [...collection().values()].map(wrap), |
| 228 | nextCursor: null, |
| 229 | }), |
| 230 | }; |
| 231 | }, |
| 232 | }, |
| 233 | search: { |
| 234 | query: async () => ({ results: [] }), |
| 235 | index: { |
| 236 | upsert: async (input) => { |
| 237 | for (const document of input.documents) { |
| 238 | state.indexDocuments.set(document.documentKey, { |
| 239 | documentKey: document.documentKey, |
| 240 | documentType: document.documentType, |
| 241 | title: document.title, |
| 242 | body: document.body, |
| 243 | fileId: document.fileId, |
| 244 | }); |
| 245 | } |
| 246 | return { upserted: input.documents.length }; |
| 247 | }, |
| 248 | delete: async (input) => { |
| 249 | let deleted = 0; |
| 250 | for (const key of input.documentKeys) { |
| 251 | if (state.indexDocuments.delete(key)) deleted += 1; |
| 252 | } |
| 253 | return { deleted }; |
| 254 | }, |
| 255 | }, |
| 256 | }, |
| 257 | notifications: { |
| 258 | create: async (input) => { |
| 259 | state.notifications.push({ |
| 260 | channelKey: input.channelKey, |
| 261 | title: input.title, |
| 262 | body: input.body, |
| 263 | href: input.href, |
| 264 | recipients: input.recipients.map((recipient) => recipient.userId), |
| 265 | }); |
| 266 | }, |
| 267 | }, |
| 268 | ai: { |
| 269 | generate: async (prompt) => ({ |
| 270 | text: options.onAiGenerate ? await options.onAiGenerate(prompt) : `echo: ${prompt}`, |
| 271 | }), |
| 272 | }, |
| 273 | net: { |
| 274 | fetch: async (input) => { |
| 275 | state.netRequests.push(input); |
| 276 | if (!options.onNetFetch) { |
| 277 | throw new Error( |
| 278 | "coline.net.fetch called in a test without an onNetFetch handler.", |
| 279 | ); |
| 280 | } |
| 281 | return options.onNetFetch(input); |
| 282 | }, |
| 283 | }, |
| 284 | pickers: { |
| 285 | file: async () => (options.pickers.file ? options.pickers.file() : null), |
| 286 | member: async () => (options.pickers.member ? options.pickers.member() : null), |
| 287 | confirm: async () => (options.pickers.confirm ? options.pickers.confirm() : false), |
| 288 | }, |
| 289 | navigation: { |
| 290 | openFile: async (fileId) => { |
| 291 | state.navigations.push({ intent: "openFile", payload: { fileId } }); |
| 292 | }, |
| 293 | openAppHome: async () => { |
| 294 | state.navigations.push({ intent: "openAppHome", payload: {} }); |
| 295 | }, |
| 296 | navigate: async (href) => { |
| 297 | state.navigations.push({ intent: "navigate", payload: { href } }); |
| 298 | }, |
| 299 | openReference: async (reference) => { |
| 300 | state.navigations.push({ intent: "openReference", payload: { reference } }); |
| 301 | }, |
| 302 | }, |
| 303 | realtime: { |
| 304 | subscribe: async () => ({ unsubscribe: () => undefined }), |
| 305 | }, |
| 306 | events: { |
| 307 | emit: async (input) => { |
| 308 | state.events.push(...input.events); |
| 309 | }, |
| 310 | }, |
| 311 | }; |
| 312 | } |
| 313 |