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 | } from "../capabilities/types"; |
| 7 | import { canAccessRecord, DEFAULT_RECORD_ACCESS } from "../capabilities/record-access"; |
| 8 | import { matchesRecordWhere } from "../capabilities/record-query"; |
| 9 | import type { TestWorkspaceState } from "./fake-capabilities"; |
| 10 |
| 11 | export function createFakeCollection<T>( |
| 12 | state: TestWorkspaceState, |
| 13 | name: string, |
| 14 | actor: ColineActorRef, |
| 15 | installId: string, |
| 16 | ): ColineCollection<T> { |
| 17 | if (!state.records.has(name)) state.records.set(name, new Map()); |
| 18 | const records = state.records.get(name)!; |
| 19 | const userId = actor.kind === "user" ? actor.userId : null; |
| 20 | const fail = (code: string, message: string): never => { |
| 21 | throw Object.assign(new Error(message), { code }); |
| 22 | }; |
| 23 | const visible = (row: ColineRecordEnvelope<unknown>) => |
| 24 | canAccessRecord(row.access, row.createdBy, userId, "read"); |
| 25 | const wrap = (row: ColineRecordEnvelope<unknown>) => |
| 26 | structuredClone(row) as ColineRecordEnvelope<T>; |
| 27 | const changed = () => { |
| 28 | state.recordRevisions.set(name, (state.recordRevisions.get(name) ?? 0) + 1); |
| 29 | for (const listener of state.realtimeListeners.get("coline.records.changed") ?? []) |
| 30 | listener({ event: "coline.records.changed", data: null }); |
| 31 | }; |
| 32 | function writable(id: string, expectedVersion?: number, manage = false) { |
| 33 | const row = records.get(id); |
| 34 | if (!row || !visible(row)) return fail("record_not_found", "Record not found."); |
| 35 | if (!canAccessRecord(row.access, row.createdBy, userId, manage ? "manage" : "write")) |
| 36 | return fail("record_forbidden", "You cannot change this record."); |
| 37 | if (expectedVersion !== undefined && row.version !== expectedVersion) |
| 38 | return fail("record_conflict", "This record changed. Refresh before saving again."); |
| 39 | return row; |
| 40 | } |
| 41 | function query(input?: ColineRecordQuery<T>) { |
| 42 | const rows = [...records.values()].filter( |
| 43 | (row) => visible(row) && matchesRecordWhere(row.data, input?.where), |
| 44 | ); |
| 45 | rows.sort((a, b) => { |
| 46 | const av = input?.orderBy ? a.data[input.orderBy] : a.updatedAt; |
| 47 | const bv = input?.orderBy ? b.data[input.orderBy] : b.updatedAt; |
| 48 | const comparison = |
| 49 | String(av ?? "").localeCompare(String(bv ?? "")) || a.id.localeCompare(b.id); |
| 50 | return input?.order === "asc" ? comparison : -comparison; |
| 51 | }); |
| 52 | const offset = Number(input?.cursor ?? 0), |
| 53 | limit = input?.limit ?? 50; |
| 54 | if (!Number.isSafeInteger(offset) || offset < 0) |
| 55 | return fail("invalid_cursor", "Invalid collection cursor."); |
| 56 | if (!Number.isInteger(limit) || limit < 1 || limit > 200) |
| 57 | return fail("invalid_query", "Record limit must be 1–200."); |
| 58 | return { |
| 59 | records: rows.slice(offset, offset + limit).map(wrap), |
| 60 | nextCursor: rows.length > offset + limit ? String(offset + limit) : null, |
| 61 | }; |
| 62 | } |
| 63 | return { |
| 64 | insert: async (data, options) => { |
| 65 | const id = options?.id ?? state.nextRecordId(); |
| 66 | const existing = records.get(id); |
| 67 | if (existing) { |
| 68 | if ( |
| 69 | JSON.stringify(existing.createdBy) !== JSON.stringify(actor) || |
| 70 | JSON.stringify(existing.data) !== JSON.stringify(data) || |
| 71 | JSON.stringify(existing.access) !== |
| 72 | JSON.stringify(options?.access ?? DEFAULT_RECORD_ACCESS) |
| 73 | ) |
| 74 | return fail("record_identity_conflict", "Record identity already in use."); |
| 75 | return wrap(existing); |
| 76 | } |
| 77 | const row = { |
| 78 | id, |
| 79 | appId: "app_test", |
| 80 | installId, |
| 81 | collection: name, |
| 82 | createdBy: actor, |
| 83 | createdAt: new Date().toISOString(), |
| 84 | updatedAt: new Date().toISOString(), |
| 85 | version: 1, |
| 86 | access: structuredClone(options?.access ?? DEFAULT_RECORD_ACCESS), |
| 87 | data: structuredClone(data) as Record<string, unknown>, |
| 88 | }; |
| 89 | records.set(id, row); |
| 90 | changed(); |
| 91 | return wrap(row); |
| 92 | }, |
| 93 | get: async (id) => { |
| 94 | const row = records.get(id); |
| 95 | return row && visible(row) ? wrap(row) : null; |
| 96 | }, |
| 97 | update: async (id, patch, options) => { |
| 98 | const row = writable(id, options?.expectedVersion); |
| 99 | row.data = { ...row.data, ...structuredClone(patch) }; |
| 100 | row.version++; |
| 101 | row.updatedAt = new Date().toISOString(); |
| 102 | changed(); |
| 103 | return wrap(row); |
| 104 | }, |
| 105 | delete: async (id, options) => { |
| 106 | writable(id, options?.expectedVersion); |
| 107 | records.delete(id); |
| 108 | changed(); |
| 109 | }, |
| 110 | setAccess: async (id, access, options) => { |
| 111 | const row = writable(id, options?.expectedVersion, true); |
| 112 | row.access = structuredClone(access); |
| 113 | row.version++; |
| 114 | row.updatedAt = new Date().toISOString(); |
| 115 | changed(); |
| 116 | return wrap(row); |
| 117 | }, |
| 118 | query: async (input) => query(input), |
| 119 | sync: async (input, revision) => { |
| 120 | const current = state.recordRevisions.get(name) ?? 0; |
| 121 | return { |
| 122 | revision: current, |
| 123 | ...(revision === current ? { records: null, nextCursor: null } : query(input)), |
| 124 | }; |
| 125 | }, |
| 126 | }; |
| 127 | } |
| 128 |